xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 7e580490ac9819dd55a36be2a9b3380d1391f91b)
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 
45f9cef64fSVladimir Oltean /* We are called before felix_npi_port_init(), so ocelot->npi is -1. */
46f9cef64fSVladimir Oltean static int felix_migrate_fdbs_to_npi_port(struct dsa_switch *ds, int port,
47f9cef64fSVladimir Oltean 					  const unsigned char *addr, u16 vid,
48f9cef64fSVladimir Oltean 					  struct dsa_db db)
49f9cef64fSVladimir Oltean {
50f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
51f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
52f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
53f9cef64fSVladimir Oltean 	int err;
54f9cef64fSVladimir Oltean 
55f9cef64fSVladimir Oltean 	err = ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
56f9cef64fSVladimir Oltean 	if (err)
57f9cef64fSVladimir Oltean 		return err;
58f9cef64fSVladimir Oltean 
59f9cef64fSVladimir Oltean 	return ocelot_fdb_add(ocelot, cpu, addr, vid, bridge_dev);
60f9cef64fSVladimir Oltean }
61f9cef64fSVladimir Oltean 
62f9cef64fSVladimir Oltean static int felix_migrate_mdbs_to_npi_port(struct dsa_switch *ds, int port,
63f9cef64fSVladimir Oltean 					  const unsigned char *addr, u16 vid,
64f9cef64fSVladimir Oltean 					  struct dsa_db db)
65f9cef64fSVladimir Oltean {
66f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
67f9cef64fSVladimir Oltean 	struct switchdev_obj_port_mdb mdb;
68f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
70f9cef64fSVladimir Oltean 	int err;
71f9cef64fSVladimir Oltean 
72f9cef64fSVladimir Oltean 	memset(&mdb, 0, sizeof(mdb));
73f9cef64fSVladimir Oltean 	ether_addr_copy(mdb.addr, addr);
74f9cef64fSVladimir Oltean 	mdb.vid = vid;
75f9cef64fSVladimir Oltean 
76f9cef64fSVladimir Oltean 	err = ocelot_port_mdb_del(ocelot, port, &mdb, bridge_dev);
77f9cef64fSVladimir Oltean 	if (err)
78f9cef64fSVladimir Oltean 		return err;
79f9cef64fSVladimir Oltean 
80f9cef64fSVladimir Oltean 	return ocelot_port_mdb_add(ocelot, cpu, &mdb, bridge_dev);
81f9cef64fSVladimir Oltean }
82f9cef64fSVladimir Oltean 
83b903a6bdSVladimir Oltean static void felix_migrate_pgid_bit(struct dsa_switch *ds, int from, int to,
84b903a6bdSVladimir Oltean 				   int pgid)
85b903a6bdSVladimir Oltean {
86b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
87b903a6bdSVladimir Oltean 	bool on;
88b903a6bdSVladimir Oltean 	u32 val;
89b903a6bdSVladimir Oltean 
90b903a6bdSVladimir Oltean 	val = ocelot_read_rix(ocelot, ANA_PGID_PGID, pgid);
91b903a6bdSVladimir Oltean 	on = !!(val & BIT(from));
92b903a6bdSVladimir Oltean 	val &= ~BIT(from);
93b903a6bdSVladimir Oltean 	if (on)
94b903a6bdSVladimir Oltean 		val |= BIT(to);
95b903a6bdSVladimir Oltean 	else
96b903a6bdSVladimir Oltean 		val &= ~BIT(to);
97b903a6bdSVladimir Oltean 
98b903a6bdSVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_PGID_PGID, pgid);
99b903a6bdSVladimir Oltean }
100b903a6bdSVladimir Oltean 
101b903a6bdSVladimir Oltean static void felix_migrate_flood_to_npi_port(struct dsa_switch *ds, int port)
102b903a6bdSVladimir Oltean {
103b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104b903a6bdSVladimir Oltean 
105b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_UC);
106b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_MC);
107b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_BC);
108b903a6bdSVladimir Oltean }
109b903a6bdSVladimir Oltean 
110b903a6bdSVladimir Oltean static void
111b903a6bdSVladimir Oltean felix_migrate_flood_to_tag_8021q_port(struct dsa_switch *ds, int port)
112b903a6bdSVladimir Oltean {
113b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
114b903a6bdSVladimir Oltean 
115b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_UC);
116b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_MC);
117b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_BC);
118b903a6bdSVladimir Oltean }
119b903a6bdSVladimir Oltean 
120f9cef64fSVladimir Oltean /* ocelot->npi was already set to -1 by felix_npi_port_deinit, so
121f9cef64fSVladimir Oltean  * ocelot_fdb_add() will not redirect FDB entries towards the
122f9cef64fSVladimir Oltean  * CPU port module here, which is what we want.
123f9cef64fSVladimir Oltean  */
124f9cef64fSVladimir Oltean static int
125f9cef64fSVladimir Oltean felix_migrate_fdbs_to_tag_8021q_port(struct dsa_switch *ds, int port,
126f9cef64fSVladimir Oltean 				     const unsigned char *addr, u16 vid,
127f9cef64fSVladimir Oltean 				     struct dsa_db db)
128f9cef64fSVladimir Oltean {
129f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
130f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
131f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
132f9cef64fSVladimir Oltean 	int err;
133f9cef64fSVladimir Oltean 
134f9cef64fSVladimir Oltean 	err = ocelot_fdb_del(ocelot, cpu, addr, vid, bridge_dev);
135f9cef64fSVladimir Oltean 	if (err)
136f9cef64fSVladimir Oltean 		return err;
137f9cef64fSVladimir Oltean 
138f9cef64fSVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
139f9cef64fSVladimir Oltean }
140f9cef64fSVladimir Oltean 
141f9cef64fSVladimir Oltean static int
142f9cef64fSVladimir Oltean felix_migrate_mdbs_to_tag_8021q_port(struct dsa_switch *ds, int port,
143f9cef64fSVladimir Oltean 				     const unsigned char *addr, u16 vid,
144f9cef64fSVladimir Oltean 				     struct dsa_db db)
145f9cef64fSVladimir Oltean {
146f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
147f9cef64fSVladimir Oltean 	struct switchdev_obj_port_mdb mdb;
148f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
149f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
150f9cef64fSVladimir Oltean 	int err;
151f9cef64fSVladimir Oltean 
152f9cef64fSVladimir Oltean 	memset(&mdb, 0, sizeof(mdb));
153f9cef64fSVladimir Oltean 	ether_addr_copy(mdb.addr, addr);
154f9cef64fSVladimir Oltean 	mdb.vid = vid;
155f9cef64fSVladimir Oltean 
156f9cef64fSVladimir Oltean 	err = ocelot_port_mdb_del(ocelot, cpu, &mdb, bridge_dev);
157f9cef64fSVladimir Oltean 	if (err)
158f9cef64fSVladimir Oltean 		return err;
159f9cef64fSVladimir Oltean 
160f9cef64fSVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, &mdb, bridge_dev);
161f9cef64fSVladimir Oltean }
162f9cef64fSVladimir Oltean 
16304b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
16404b67e18SVladimir Oltean  * the tagger can perform RX source port identification.
16504b67e18SVladimir Oltean  */
16604b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid)
167e21268efSVladimir Oltean {
168e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
169e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
170e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
171e21268efSVladimir Oltean 	int key_length, upstream, err;
172e21268efSVladimir Oltean 
173e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
174e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
175e21268efSVladimir Oltean 
176e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
177e21268efSVladimir Oltean 				     GFP_KERNEL);
178e21268efSVladimir Oltean 	if (!outer_tagging_rule)
179e21268efSVladimir Oltean 		return -ENOMEM;
180e21268efSVladimir Oltean 
181e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
182e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
183c518afecSVladimir Oltean 	outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
184e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
185e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
186e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
187e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
188e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
189e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
190e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
191e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
192e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
193e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
194e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
195e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
196e21268efSVladimir Oltean 
197e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
198e21268efSVladimir Oltean 	if (err)
199e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
200e21268efSVladimir Oltean 
201e21268efSVladimir Oltean 	return err;
202e21268efSVladimir Oltean }
203e21268efSVladimir Oltean 
20404b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid)
20504b67e18SVladimir Oltean {
20604b67e18SVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
20704b67e18SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
20804b67e18SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
20904b67e18SVladimir Oltean 
21004b67e18SVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
21104b67e18SVladimir Oltean 
21204b67e18SVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
21304b67e18SVladimir Oltean 								 port, false);
21404b67e18SVladimir Oltean 	if (!outer_tagging_rule)
21504b67e18SVladimir Oltean 		return -ENOENT;
21604b67e18SVladimir Oltean 
21704b67e18SVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
21804b67e18SVladimir Oltean }
21904b67e18SVladimir Oltean 
22004b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
22104b67e18SVladimir Oltean  * rules for steering those tagged packets towards the correct destination port
22204b67e18SVladimir Oltean  */
22304b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid)
224e21268efSVladimir Oltean {
225e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
226e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
227e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
228e21268efSVladimir Oltean 	int upstream, err;
229e21268efSVladimir Oltean 
230e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
231e21268efSVladimir Oltean 	if (!untagging_rule)
232e21268efSVladimir Oltean 		return -ENOMEM;
233e21268efSVladimir Oltean 
234e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
235e21268efSVladimir Oltean 	if (!redirect_rule) {
236e21268efSVladimir Oltean 		kfree(untagging_rule);
237e21268efSVladimir Oltean 		return -ENOMEM;
238e21268efSVladimir Oltean 	}
239e21268efSVladimir Oltean 
240e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
241e21268efSVladimir Oltean 
242e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
243e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
244e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
245e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
246e21268efSVladimir Oltean 	untagging_rule->prio = 1;
247c518afecSVladimir Oltean 	untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
248e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
249e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
250e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
251e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
252e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
253e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
254e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
255e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
256e21268efSVladimir Oltean 
257e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
258e21268efSVladimir Oltean 	if (err) {
259e21268efSVladimir Oltean 		kfree(untagging_rule);
260e21268efSVladimir Oltean 		kfree(redirect_rule);
261e21268efSVladimir Oltean 		return err;
262e21268efSVladimir Oltean 	}
263e21268efSVladimir Oltean 
264e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
265e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
266e21268efSVladimir Oltean 	redirect_rule->pag = port;
267e21268efSVladimir Oltean 	redirect_rule->prio = 1;
268c518afecSVladimir Oltean 	redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
269e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
270e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
271e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
272e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
273e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
274e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
275e21268efSVladimir Oltean 
276e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
277e21268efSVladimir Oltean 	if (err) {
278e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
279e21268efSVladimir Oltean 		kfree(redirect_rule);
280e21268efSVladimir Oltean 		return err;
281e21268efSVladimir Oltean 	}
282e21268efSVladimir Oltean 
283e21268efSVladimir Oltean 	return 0;
284e21268efSVladimir Oltean }
285e21268efSVladimir Oltean 
28604b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid)
287e21268efSVladimir Oltean {
288e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
289e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
290e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
291e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
292e21268efSVladimir Oltean 	int err;
293e21268efSVladimir Oltean 
294e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
295e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
296e21268efSVladimir Oltean 
297e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
298e21268efSVladimir Oltean 							     port, false);
299e21268efSVladimir Oltean 	if (!untagging_rule)
30008f44db3SVladimir Oltean 		return -ENOENT;
301e21268efSVladimir Oltean 
302e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
303e21268efSVladimir Oltean 	if (err)
304e21268efSVladimir Oltean 		return err;
305e21268efSVladimir Oltean 
306e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
307e21268efSVladimir Oltean 							    port, false);
308e21268efSVladimir Oltean 	if (!redirect_rule)
30904b67e18SVladimir Oltean 		return -ENOENT;
310e21268efSVladimir Oltean 
311e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
312e21268efSVladimir Oltean }
313e21268efSVladimir Oltean 
31404b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
31504b67e18SVladimir Oltean 				    u16 flags)
31604b67e18SVladimir Oltean {
31704b67e18SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
31804b67e18SVladimir Oltean 	int err;
31904b67e18SVladimir Oltean 
32004b67e18SVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
32104b67e18SVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
32204b67e18SVladimir Oltean 	 * for the CPU port.
32304b67e18SVladimir Oltean 	 */
32404b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
32504b67e18SVladimir Oltean 		return 0;
32604b67e18SVladimir Oltean 
32704b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
32804b67e18SVladimir Oltean 	if (err)
32904b67e18SVladimir Oltean 		return err;
33004b67e18SVladimir Oltean 
33104b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid);
33204b67e18SVladimir Oltean 	if (err) {
33304b67e18SVladimir Oltean 		felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
33404b67e18SVladimir Oltean 		return err;
33504b67e18SVladimir Oltean 	}
33604b67e18SVladimir Oltean 
33704b67e18SVladimir Oltean 	return 0;
33804b67e18SVladimir Oltean }
33904b67e18SVladimir Oltean 
340e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
341e21268efSVladimir Oltean {
342e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
34304b67e18SVladimir Oltean 	int err;
344e21268efSVladimir Oltean 
34504b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
34604b67e18SVladimir Oltean 		return 0;
347e21268efSVladimir Oltean 
34804b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
34904b67e18SVladimir Oltean 	if (err)
35004b67e18SVladimir Oltean 		return err;
35104b67e18SVladimir Oltean 
35204b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid);
35304b67e18SVladimir Oltean 	if (err) {
35404b67e18SVladimir Oltean 		felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
35504b67e18SVladimir Oltean 		return err;
35604b67e18SVladimir Oltean 	}
357e21268efSVladimir Oltean 
358e21268efSVladimir Oltean 	return 0;
359e21268efSVladimir Oltean }
360e21268efSVladimir Oltean 
361e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
362e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
363e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
364e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
365e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
366e21268efSVladimir Oltean  * register-based MMIO.
367e21268efSVladimir Oltean  */
368e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
369e21268efSVladimir Oltean {
3708abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
3718abe1970SVladimir Oltean 
37254c31984SVladimir Oltean 	ocelot_port_set_dsa_8021q_cpu(ocelot, port);
373e21268efSVladimir Oltean 
374e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
375e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
376e21268efSVladimir Oltean 
3778abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3788abe1970SVladimir Oltean 
3798abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
380e21268efSVladimir Oltean }
381e21268efSVladimir Oltean 
382e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
383e21268efSVladimir Oltean {
3848abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
3858abe1970SVladimir Oltean 
38654c31984SVladimir Oltean 	ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
387e21268efSVladimir Oltean 
388e21268efSVladimir Oltean 	/* Restore PGID_CPU */
389e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
390e21268efSVladimir Oltean 			 PGID_CPU);
391e21268efSVladimir Oltean 
3928abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3938abe1970SVladimir Oltean 
3948abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
395e21268efSVladimir Oltean }
396e21268efSVladimir Oltean 
39799348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
39899348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
39999348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
400c8c0ba4fSVladimir Oltean  */
40199348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
40299348004SVladimir Oltean 					      bool using_tag_8021q)
403c8c0ba4fSVladimir Oltean {
40499348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
40599348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
40699348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
40799348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
40899348004SVladimir Oltean 	unsigned long port_mask;
4092960bb14SVladimir Oltean 	struct dsa_port *dp;
41099348004SVladimir Oltean 	bool cpu_copy_ena;
41199348004SVladimir Oltean 	int cpu = -1, err;
412c8c0ba4fSVladimir Oltean 
41399348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
41499348004SVladimir Oltean 		return 0;
415c8c0ba4fSVladimir Oltean 
41699348004SVladimir Oltean 	/* Figure out the current CPU port */
4172960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
4182960bb14SVladimir Oltean 		cpu = dp->index;
4198d5f7954SVladimir Oltean 		break;
420c8c0ba4fSVladimir Oltean 	}
4218d5f7954SVladimir Oltean 
422d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
423d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
424d78637a8SVladimir Oltean 	 */
425c8c0ba4fSVladimir Oltean 
42699348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
42799348004SVladimir Oltean 	list_for_each_entry(trap, &ocelot->traps, trap_list) {
42899348004SVladimir Oltean 		/* Figure out the current trapping destination */
42999348004SVladimir Oltean 		if (using_tag_8021q) {
43099348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
43199348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
43299348004SVladimir Oltean 			 * port module.
433c8c0ba4fSVladimir Oltean 			 */
43499348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
43599348004SVladimir Oltean 			port_mask = BIT(cpu);
43699348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
437c8c0ba4fSVladimir Oltean 		} else {
43899348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
43999348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
440c8c0ba4fSVladimir Oltean 			 */
44199348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
44299348004SVladimir Oltean 			port_mask = 0;
44399348004SVladimir Oltean 			cpu_copy_ena = true;
444c8c0ba4fSVladimir Oltean 		}
445c8c0ba4fSVladimir Oltean 
44699348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
44799348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
44899348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
44999348004SVladimir Oltean 			continue;
450c8c0ba4fSVladimir Oltean 
45199348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
45299348004SVladimir Oltean 		trap->action.port_mask = port_mask;
45399348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
4540a6f17c6SVladimir Oltean 
45599348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
456c8c0ba4fSVladimir Oltean 		if (err)
457c8c0ba4fSVladimir Oltean 			return err;
45899348004SVladimir Oltean 	}
459c8c0ba4fSVladimir Oltean 
46099348004SVladimir Oltean 	return 0;
461c8c0ba4fSVladimir Oltean }
462c8c0ba4fSVladimir Oltean 
463c69f40acSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
464e21268efSVladimir Oltean {
465e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4662960bb14SVladimir Oltean 	struct dsa_port *dp;
4672960bb14SVladimir Oltean 	int err;
468e21268efSVladimir Oltean 
469e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
470e21268efSVladimir Oltean 
4712960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
472e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
473e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
474e21268efSVladimir Oltean 		 * for 2 reasons:
475e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
476e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
477e21268efSVladimir Oltean 		 *   into the system.
478e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
479e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
480e21268efSVladimir Oltean 		 *   port module.
481e21268efSVladimir Oltean 		 */
482e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
483e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4842960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
485e21268efSVladimir Oltean 	}
486e21268efSVladimir Oltean 
4875da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
488e21268efSVladimir Oltean 	if (err)
489d7b1fd52SVladimir Oltean 		return err;
490d7b1fd52SVladimir Oltean 
491c69f40acSVladimir Oltean 	err = dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_tag_8021q_port);
492d7b1fd52SVladimir Oltean 	if (err)
493d7b1fd52SVladimir Oltean 		goto out_tag_8021q_unregister;
494e21268efSVladimir Oltean 
495c69f40acSVladimir Oltean 	err = dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_tag_8021q_port);
496f9cef64fSVladimir Oltean 	if (err)
497f9cef64fSVladimir Oltean 		goto out_migrate_fdbs;
498b903a6bdSVladimir Oltean 
499b903a6bdSVladimir Oltean 	felix_migrate_flood_to_tag_8021q_port(ds, cpu);
500f9cef64fSVladimir Oltean 
501f9cef64fSVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
502f9cef64fSVladimir Oltean 	if (err)
503b903a6bdSVladimir Oltean 		goto out_migrate_flood;
504f9cef64fSVladimir Oltean 
50599348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
50699348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
50799348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
50899348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
50999348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
51099348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
51199348004SVladimir Oltean 	 */
51299348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
51399348004SVladimir Oltean 
514e21268efSVladimir Oltean 	return 0;
515e21268efSVladimir Oltean 
516b903a6bdSVladimir Oltean out_migrate_flood:
517b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
518f9cef64fSVladimir Oltean 	dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
519f9cef64fSVladimir Oltean out_migrate_fdbs:
520f9cef64fSVladimir Oltean 	dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
521d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
522d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
523e21268efSVladimir Oltean 	return err;
524e21268efSVladimir Oltean }
525e21268efSVladimir Oltean 
526e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
527e21268efSVladimir Oltean {
528e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5292960bb14SVladimir Oltean 	struct dsa_port *dp;
5302960bb14SVladimir Oltean 	int err;
531e21268efSVladimir Oltean 
53299348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
533c8c0ba4fSVladimir Oltean 	if (err)
534c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
535c8c0ba4fSVladimir Oltean 			err);
536c8c0ba4fSVladimir Oltean 
537d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
538e21268efSVladimir Oltean 
5392960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
540e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
541e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
542e21268efSVladimir Oltean 		 */
543e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
544e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
545e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
5462960bb14SVladimir Oltean 				 dp->index);
547e21268efSVladimir Oltean 	}
548e21268efSVladimir Oltean 
549e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
550e21268efSVladimir Oltean }
551e21268efSVladimir Oltean 
552adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
553adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
554adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
555adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
556adb3dccfSVladimir Oltean  * DSA master.
557adb3dccfSVladimir Oltean  */
558adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
559adb3dccfSVladimir Oltean {
560adb3dccfSVladimir Oltean 	ocelot->npi = port;
561adb3dccfSVladimir Oltean 
562adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
563adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
564adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
565adb3dccfSVladimir Oltean 
566adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
567adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
568adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
569adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
570adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
571adb3dccfSVladimir Oltean 
572adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
573adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
574adb3dccfSVladimir Oltean }
575adb3dccfSVladimir Oltean 
576adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
577adb3dccfSVladimir Oltean {
578adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
579adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
580adb3dccfSVladimir Oltean 
581adb3dccfSVladimir Oltean 	ocelot->npi = -1;
582adb3dccfSVladimir Oltean 
583adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
584adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
585adb3dccfSVladimir Oltean 
586adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
587adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
588adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
589adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
590adb3dccfSVladimir Oltean 
591adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
592adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
593adb3dccfSVladimir Oltean }
594adb3dccfSVladimir Oltean 
595c69f40acSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
596adb3dccfSVladimir Oltean {
597adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
598f9cef64fSVladimir Oltean 	int err;
599f9cef64fSVladimir Oltean 
600c69f40acSVladimir Oltean 	err = dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
601f9cef64fSVladimir Oltean 	if (err)
602f9cef64fSVladimir Oltean 		return err;
603f9cef64fSVladimir Oltean 
604c69f40acSVladimir Oltean 	err = dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
605f9cef64fSVladimir Oltean 	if (err)
606f9cef64fSVladimir Oltean 		goto out_migrate_fdbs;
607b903a6bdSVladimir Oltean 
608b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
609adb3dccfSVladimir Oltean 
610adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
611adb3dccfSVladimir Oltean 
612adb3dccfSVladimir Oltean 	return 0;
613f9cef64fSVladimir Oltean 
614f9cef64fSVladimir Oltean out_migrate_fdbs:
615c69f40acSVladimir Oltean 	dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_tag_8021q_port);
616f9cef64fSVladimir Oltean 
617f9cef64fSVladimir Oltean 	return err;
618adb3dccfSVladimir Oltean }
619adb3dccfSVladimir Oltean 
620adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
621adb3dccfSVladimir Oltean {
622adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
623adb3dccfSVladimir Oltean 
624adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
625adb3dccfSVladimir Oltean }
626adb3dccfSVladimir Oltean 
627adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
628c69f40acSVladimir Oltean 				  enum dsa_tag_protocol proto)
629adb3dccfSVladimir Oltean {
630adb3dccfSVladimir Oltean 	int err;
631adb3dccfSVladimir Oltean 
632adb3dccfSVladimir Oltean 	switch (proto) {
6337c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
634adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
635c69f40acSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
636adb3dccfSVladimir Oltean 		break;
637e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
638c69f40acSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
639e21268efSVladimir Oltean 		break;
640adb3dccfSVladimir Oltean 	default:
641adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
642adb3dccfSVladimir Oltean 	}
643adb3dccfSVladimir Oltean 
644adb3dccfSVladimir Oltean 	return err;
645adb3dccfSVladimir Oltean }
646adb3dccfSVladimir Oltean 
647adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
648adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
649adb3dccfSVladimir Oltean {
650adb3dccfSVladimir Oltean 	switch (proto) {
6517c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
652adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
653adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
654adb3dccfSVladimir Oltean 		break;
655e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
656e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
657e21268efSVladimir Oltean 		break;
658adb3dccfSVladimir Oltean 	default:
659adb3dccfSVladimir Oltean 		break;
660adb3dccfSVladimir Oltean 	}
661adb3dccfSVladimir Oltean }
662adb3dccfSVladimir Oltean 
663e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
664e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
665e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
666e21268efSVladimir Oltean  */
667adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
668adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
669adb3dccfSVladimir Oltean {
670adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
671adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
672adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
673adb3dccfSVladimir Oltean 	int err;
674adb3dccfSVladimir Oltean 
6757c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
6767c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
677e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
678adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
679adb3dccfSVladimir Oltean 
680adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
681adb3dccfSVladimir Oltean 
682c69f40acSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
683adb3dccfSVladimir Oltean 	if (err) {
684c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
685adb3dccfSVladimir Oltean 		return err;
686adb3dccfSVladimir Oltean 	}
687adb3dccfSVladimir Oltean 
688adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
689adb3dccfSVladimir Oltean 
690adb3dccfSVladimir Oltean 	return 0;
691adb3dccfSVladimir Oltean }
692adb3dccfSVladimir Oltean 
69356051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6944d776482SFlorian Fainelli 						    int port,
6954d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
69656051948SVladimir Oltean {
697adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
698adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
699adb3dccfSVladimir Oltean 
700adb3dccfSVladimir Oltean 	return felix->tag_proto;
70156051948SVladimir Oltean }
70256051948SVladimir Oltean 
70356051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
70456051948SVladimir Oltean 				 unsigned int ageing_time)
70556051948SVladimir Oltean {
70656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70756051948SVladimir Oltean 
70856051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
70956051948SVladimir Oltean 
71056051948SVladimir Oltean 	return 0;
71156051948SVladimir Oltean }
71256051948SVladimir Oltean 
7135cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
7145cad43a5SVladimir Oltean {
7155cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7165cad43a5SVladimir Oltean 	int err;
7175cad43a5SVladimir Oltean 
7185cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
7195cad43a5SVladimir Oltean 	if (err)
7205cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
7215cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7225cad43a5SVladimir Oltean }
7235cad43a5SVladimir Oltean 
72456051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
72556051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
72656051948SVladimir Oltean {
72756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72856051948SVladimir Oltean 
72956051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
73056051948SVladimir Oltean }
73156051948SVladimir Oltean 
73256051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
733c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
734c2693363SVladimir Oltean 			 struct dsa_db db)
73556051948SVladimir Oltean {
73654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
73756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73856051948SVladimir Oltean 
73954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
74054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
74154c31984SVladimir Oltean 
742*7e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
743*7e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
744*7e580490SVladimir Oltean 		return 0;
745*7e580490SVladimir Oltean 
74654c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
74756051948SVladimir Oltean }
74856051948SVladimir Oltean 
74956051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
750c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
751c2693363SVladimir Oltean 			 struct dsa_db db)
75256051948SVladimir Oltean {
75354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
75456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
75556051948SVladimir Oltean 
75654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
75754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
75854c31984SVladimir Oltean 
759*7e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
760*7e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
761*7e580490SVladimir Oltean 		return 0;
762*7e580490SVladimir Oltean 
76354c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
76456051948SVladimir Oltean }
76556051948SVladimir Oltean 
766961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
767c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
768c2693363SVladimir Oltean 			     struct dsa_db db)
769961d8b69SVladimir Oltean {
77054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
771961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
772961d8b69SVladimir Oltean 
77354c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
77454c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
77554c31984SVladimir Oltean 
77654c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
777961d8b69SVladimir Oltean }
778961d8b69SVladimir Oltean 
779961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
780c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
781c2693363SVladimir Oltean 			     struct dsa_db db)
782961d8b69SVladimir Oltean {
78354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
784961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
785961d8b69SVladimir Oltean 
78654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
78754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
78854c31984SVladimir Oltean 
78954c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
790961d8b69SVladimir Oltean }
791961d8b69SVladimir Oltean 
792a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
793c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
794c2693363SVladimir Oltean 			 struct dsa_db db)
795209edf95SVladimir Oltean {
79654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
797209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
798209edf95SVladimir Oltean 
79954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
80054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
80154c31984SVladimir Oltean 
802*7e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
803*7e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
804*7e580490SVladimir Oltean 		return 0;
805*7e580490SVladimir Oltean 
80654c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
807209edf95SVladimir Oltean }
808209edf95SVladimir Oltean 
809209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
810c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
811c2693363SVladimir Oltean 			 struct dsa_db db)
812209edf95SVladimir Oltean {
81354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
814209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
815209edf95SVladimir Oltean 
81654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
81754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
81854c31984SVladimir Oltean 
819*7e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
820*7e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
821*7e580490SVladimir Oltean 		return 0;
822*7e580490SVladimir Oltean 
82354c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
824209edf95SVladimir Oltean }
825209edf95SVladimir Oltean 
82656051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
82756051948SVladimir Oltean 				       u8 state)
82856051948SVladimir Oltean {
82956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
83056051948SVladimir Oltean 
83156051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
83256051948SVladimir Oltean }
83356051948SVladimir Oltean 
834421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
835421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
836421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
837421741eaSVladimir Oltean {
838421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
839421741eaSVladimir Oltean 
840421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
841421741eaSVladimir Oltean }
842421741eaSVladimir Oltean 
843421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
844421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
845421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
846421741eaSVladimir Oltean {
847421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
848421741eaSVladimir Oltean 
849421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
850421741eaSVladimir Oltean 
851421741eaSVladimir Oltean 	return 0;
852421741eaSVladimir Oltean }
853421741eaSVladimir Oltean 
85456051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
85506b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
85606b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
85756051948SVladimir Oltean {
85856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85956051948SVladimir Oltean 
86054c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
86154c31984SVladimir Oltean 				       extack);
86256051948SVladimir Oltean }
86356051948SVladimir Oltean 
86456051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
865d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
86656051948SVladimir Oltean {
86756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
86856051948SVladimir Oltean 
869d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
87056051948SVladimir Oltean }
87156051948SVladimir Oltean 
8728fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
873dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8748fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8758fe6832eSVladimir Oltean {
8768fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8778fe6832eSVladimir Oltean 
878dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8798fe6832eSVladimir Oltean }
8808fe6832eSVladimir Oltean 
8818fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
882dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8838fe6832eSVladimir Oltean {
8848fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8858fe6832eSVladimir Oltean 
886dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8878fe6832eSVladimir Oltean 
8888fe6832eSVladimir Oltean 	return 0;
8898fe6832eSVladimir Oltean }
8908fe6832eSVladimir Oltean 
8918fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8928fe6832eSVladimir Oltean {
8938fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8948fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8958fe6832eSVladimir Oltean 
8968fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8978fe6832eSVladimir Oltean 
8988fe6832eSVladimir Oltean 	return 0;
8998fe6832eSVladimir Oltean }
9008fe6832eSVladimir Oltean 
90156051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
90201af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
90301af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
90456051948SVladimir Oltean {
9052f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
906b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
9072f0402feSVladimir Oltean 
9089a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
9099a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
9109a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
9119a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
9129a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
9139a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
9149a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
9159a720680SVladimir Oltean 	 */
9169a720680SVladimir Oltean 	if (port == ocelot->npi)
9179a720680SVladimir Oltean 		return 0;
9189a720680SVladimir Oltean 
919b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9202f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
92101af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
92201af940eSVladimir Oltean 				   extack);
92356051948SVladimir Oltean }
92456051948SVladimir Oltean 
92589153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
92689153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
92756051948SVladimir Oltean {
92856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
92956051948SVladimir Oltean 
9303b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
93156051948SVladimir Oltean }
93256051948SVladimir Oltean 
9331958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
93431046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
93531046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
93656051948SVladimir Oltean {
93756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
938183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9391958d581SVladimir Oltean 	int err;
94056051948SVladimir Oltean 
94101af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9421958d581SVladimir Oltean 	if (err)
9431958d581SVladimir Oltean 		return err;
9441958d581SVladimir Oltean 
9451958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
946183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
947183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
94856051948SVladimir Oltean }
94956051948SVladimir Oltean 
95056051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
95156051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
95256051948SVladimir Oltean {
95356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
95456051948SVladimir Oltean 
955b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
95656051948SVladimir Oltean }
95756051948SVladimir Oltean 
95879fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
95979fda660SRussell King (Oracle) 				   struct phylink_config *config)
96079fda660SRussell King (Oracle) {
96179fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
96279fda660SRussell King (Oracle) 
963f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
964f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
965f6f04c02SRussell King (Oracle) 	 * as non-legacy.
966f6f04c02SRussell King (Oracle) 	 */
967f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
968f6f04c02SRussell King (Oracle) 
96979fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
97079fda660SRussell King (Oracle) 		  config->supported_interfaces);
97179fda660SRussell King (Oracle) }
97279fda660SRussell King (Oracle) 
973bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
974bdeced75SVladimir Oltean 				   unsigned long *supported,
975bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
976bdeced75SVladimir Oltean {
977bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
978375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
979bdeced75SVladimir Oltean 
980375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
981375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
982bdeced75SVladimir Oltean }
983bdeced75SVladimir Oltean 
984864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
985864ba485SRussell King (Oracle) 							int port,
986864ba485SRussell King (Oracle) 							phy_interface_t iface)
987bdeced75SVladimir Oltean {
988bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
989bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
990864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
991bdeced75SVladimir Oltean 
99249af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
993864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
994864ba485SRussell King (Oracle) 
995864ba485SRussell King (Oracle) 	return pcs;
996bdeced75SVladimir Oltean }
997bdeced75SVladimir Oltean 
998bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
999bdeced75SVladimir Oltean 					unsigned int link_an_mode,
1000bdeced75SVladimir Oltean 					phy_interface_t interface)
1001bdeced75SVladimir Oltean {
1002bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1003bdeced75SVladimir Oltean 
1004e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1005e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
1006bdeced75SVladimir Oltean }
1007bdeced75SVladimir Oltean 
1008bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
1009bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
1010bdeced75SVladimir Oltean 				      phy_interface_t interface,
10115b502a7bSRussell King 				      struct phy_device *phydev,
10125b502a7bSRussell King 				      int speed, int duplex,
10135b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
1014bdeced75SVladimir Oltean {
1015bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10167e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1017bdeced75SVladimir Oltean 
1018e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1019e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1020e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10217e14a2dcSVladimir Oltean 
10227e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10237e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1024bdeced75SVladimir Oltean }
1025bdeced75SVladimir Oltean 
1026bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1027bd2b3161SXiaoliang Yang {
1028bd2b3161SXiaoliang Yang 	int i;
1029bd2b3161SXiaoliang Yang 
1030bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1031bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1032bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1033bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1034bd2b3161SXiaoliang Yang 		       port);
1035bd2b3161SXiaoliang Yang 
103670d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1037bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1038bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1039bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1040bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1041bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1042bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1043bd2b3161SXiaoliang Yang 			      port, i);
1044bd2b3161SXiaoliang Yang 	}
1045bd2b3161SXiaoliang Yang }
1046bd2b3161SXiaoliang Yang 
104756051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
104856051948SVladimir Oltean 			      u32 stringset, u8 *data)
104956051948SVladimir Oltean {
105056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105156051948SVladimir Oltean 
105256051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
105356051948SVladimir Oltean }
105456051948SVladimir Oltean 
105556051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
105656051948SVladimir Oltean {
105756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105856051948SVladimir Oltean 
105956051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
106056051948SVladimir Oltean }
106156051948SVladimir Oltean 
106256051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
106356051948SVladimir Oltean {
106456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
106556051948SVladimir Oltean 
106656051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
106756051948SVladimir Oltean }
106856051948SVladimir Oltean 
106956051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
107056051948SVladimir Oltean 			     struct ethtool_ts_info *info)
107156051948SVladimir Oltean {
107256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
107356051948SVladimir Oltean 
107456051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
107556051948SVladimir Oltean }
107656051948SVladimir Oltean 
1077acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1078acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1079acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1080acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1081acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1082acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1083acf242fcSColin Foster };
1084acf242fcSColin Foster 
1085acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1086acf242fcSColin Foster 				   phy_interface_t phy_mode)
1087acf242fcSColin Foster {
1088acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1089acf242fcSColin Foster 
1090acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1091acf242fcSColin Foster 		return 0;
1092acf242fcSColin Foster 	return -EOPNOTSUPP;
1093acf242fcSColin Foster }
1094acf242fcSColin Foster 
1095bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1096bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1097bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1098bdeced75SVladimir Oltean {
1099bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1100bdeced75SVladimir Oltean 	struct device_node *child;
1101bdeced75SVladimir Oltean 
110237fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1103bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1104bdeced75SVladimir Oltean 		u32 port;
1105bdeced75SVladimir Oltean 		int err;
1106bdeced75SVladimir Oltean 
1107bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1108bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1109bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1110bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1111bdeced75SVladimir Oltean 			of_node_put(child);
1112bdeced75SVladimir Oltean 			return -ENODEV;
1113bdeced75SVladimir Oltean 		}
1114bdeced75SVladimir Oltean 
1115bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1116bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1117bdeced75SVladimir Oltean 		if (err) {
1118bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1119bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1120bdeced75SVladimir Oltean 				port);
1121bdeced75SVladimir Oltean 			of_node_put(child);
1122bdeced75SVladimir Oltean 			return -ENODEV;
1123bdeced75SVladimir Oltean 		}
1124bdeced75SVladimir Oltean 
1125acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1126bdeced75SVladimir Oltean 		if (err < 0) {
1127bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1128bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
112959ebb430SSumera Priyadarsini 			of_node_put(child);
1130bdeced75SVladimir Oltean 			return err;
1131bdeced75SVladimir Oltean 		}
1132bdeced75SVladimir Oltean 
1133bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1134bdeced75SVladimir Oltean 	}
1135bdeced75SVladimir Oltean 
1136bdeced75SVladimir Oltean 	return 0;
1137bdeced75SVladimir Oltean }
1138bdeced75SVladimir Oltean 
1139bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1140bdeced75SVladimir Oltean {
1141bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1142bdeced75SVladimir Oltean 	struct device_node *switch_node;
1143bdeced75SVladimir Oltean 	struct device_node *ports_node;
1144bdeced75SVladimir Oltean 	int err;
1145bdeced75SVladimir Oltean 
1146bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1147bdeced75SVladimir Oltean 
1148bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1149abecbfcdSVladimir Oltean 	if (!ports_node)
1150abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1151bdeced75SVladimir Oltean 	if (!ports_node) {
1152abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1153bdeced75SVladimir Oltean 		return -ENODEV;
1154bdeced75SVladimir Oltean 	}
1155bdeced75SVladimir Oltean 
1156bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1157bdeced75SVladimir Oltean 	of_node_put(ports_node);
1158bdeced75SVladimir Oltean 
1159bdeced75SVladimir Oltean 	return err;
1160bdeced75SVladimir Oltean }
1161bdeced75SVladimir Oltean 
116256051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
116356051948SVladimir Oltean {
116456051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1165bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1166b4024c9eSClaudiu Manoil 	struct resource res;
116756051948SVladimir Oltean 	int port, i, err;
116856051948SVladimir Oltean 
116956051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
117056051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
117156051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
117256051948SVladimir Oltean 	if (!ocelot->ports)
117356051948SVladimir Oltean 		return -ENOMEM;
117456051948SVladimir Oltean 
117556051948SVladimir Oltean 	ocelot->map		= felix->info->map;
117656051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
117756051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
117821ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
117907d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
118077043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
118177043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
118277043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
118377043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
118456051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1185cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1186cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1187f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
118856051948SVladimir Oltean 
1189bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1190bdeced75SVladimir Oltean 				 GFP_KERNEL);
1191bdeced75SVladimir Oltean 	if (!port_phy_modes)
1192bdeced75SVladimir Oltean 		return -ENOMEM;
1193bdeced75SVladimir Oltean 
1194bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1195bdeced75SVladimir Oltean 	if (err) {
1196bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1197bdeced75SVladimir Oltean 		return err;
1198bdeced75SVladimir Oltean 	}
1199bdeced75SVladimir Oltean 
120056051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
120156051948SVladimir Oltean 		struct regmap *target;
120256051948SVladimir Oltean 
120356051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
120456051948SVladimir Oltean 			continue;
120556051948SVladimir Oltean 
1206b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1207b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1208375e1314SVladimir Oltean 		res.start += felix->switch_base;
1209375e1314SVladimir Oltean 		res.end += felix->switch_base;
121056051948SVladimir Oltean 
1211242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
121256051948SVladimir Oltean 		if (IS_ERR(target)) {
121356051948SVladimir Oltean 			dev_err(ocelot->dev,
121456051948SVladimir Oltean 				"Failed to map device memory space\n");
1215bdeced75SVladimir Oltean 			kfree(port_phy_modes);
121656051948SVladimir Oltean 			return PTR_ERR(target);
121756051948SVladimir Oltean 		}
121856051948SVladimir Oltean 
121956051948SVladimir Oltean 		ocelot->targets[i] = target;
122056051948SVladimir Oltean 	}
122156051948SVladimir Oltean 
122256051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
122356051948SVladimir Oltean 	if (err) {
122456051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1225bdeced75SVladimir Oltean 		kfree(port_phy_modes);
122656051948SVladimir Oltean 		return err;
122756051948SVladimir Oltean 	}
122856051948SVladimir Oltean 
122956051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
123056051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
123191c724cfSVladimir Oltean 		struct regmap *target;
123256051948SVladimir Oltean 
123356051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
123456051948SVladimir Oltean 					   sizeof(struct ocelot_port),
123556051948SVladimir Oltean 					   GFP_KERNEL);
123656051948SVladimir Oltean 		if (!ocelot_port) {
123756051948SVladimir Oltean 			dev_err(ocelot->dev,
123856051948SVladimir Oltean 				"failed to allocate port memory\n");
1239bdeced75SVladimir Oltean 			kfree(port_phy_modes);
124056051948SVladimir Oltean 			return -ENOMEM;
124156051948SVladimir Oltean 		}
124256051948SVladimir Oltean 
1243b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1244b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1245375e1314SVladimir Oltean 		res.start += felix->switch_base;
1246375e1314SVladimir Oltean 		res.end += felix->switch_base;
124756051948SVladimir Oltean 
1248242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
124991c724cfSVladimir Oltean 		if (IS_ERR(target)) {
125056051948SVladimir Oltean 			dev_err(ocelot->dev,
125191c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
125291c724cfSVladimir Oltean 				port);
1253bdeced75SVladimir Oltean 			kfree(port_phy_modes);
125491c724cfSVladimir Oltean 			return PTR_ERR(target);
125556051948SVladimir Oltean 		}
125656051948SVladimir Oltean 
1257bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
125856051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
125991c724cfSVladimir Oltean 		ocelot_port->target = target;
126056051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
126156051948SVladimir Oltean 	}
126256051948SVladimir Oltean 
1263bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1264bdeced75SVladimir Oltean 
1265bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1266bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1267bdeced75SVladimir Oltean 		if (err < 0)
1268bdeced75SVladimir Oltean 			return err;
1269bdeced75SVladimir Oltean 	}
1270bdeced75SVladimir Oltean 
127156051948SVladimir Oltean 	return 0;
127256051948SVladimir Oltean }
127356051948SVladimir Oltean 
12741328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12751328a883SVladimir Oltean 					   struct sk_buff *skb)
12761328a883SVladimir Oltean {
12771328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12781328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12791328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12801328a883SVladimir Oltean 	unsigned long flags;
12811328a883SVladimir Oltean 
12821328a883SVladimir Oltean 	if (!clone)
12831328a883SVladimir Oltean 		return;
12841328a883SVladimir Oltean 
12851328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12861328a883SVladimir Oltean 
12871328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12881328a883SVladimir Oltean 		if (skb != clone)
12891328a883SVladimir Oltean 			continue;
12901328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12911328a883SVladimir Oltean 		skb_match = skb;
12921328a883SVladimir Oltean 		break;
12931328a883SVladimir Oltean 	}
12941328a883SVladimir Oltean 
12951328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12961328a883SVladimir Oltean 
12971328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12981328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12991328a883SVladimir Oltean }
13001328a883SVladimir Oltean 
130149f885b2SVladimir Oltean #define work_to_xmit_work(w) \
130249f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
130349f885b2SVladimir Oltean 
130449f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
130549f885b2SVladimir Oltean {
130649f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
130749f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
130849f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
130949f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
131049f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
131149f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
131249f885b2SVladimir Oltean 	int retries = 10;
131349f885b2SVladimir Oltean 
131449f885b2SVladimir Oltean 	do {
131549f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
131649f885b2SVladimir Oltean 			break;
131749f885b2SVladimir Oltean 
131849f885b2SVladimir Oltean 		cpu_relax();
131949f885b2SVladimir Oltean 	} while (--retries);
132049f885b2SVladimir Oltean 
132149f885b2SVladimir Oltean 	if (!retries) {
132249f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
132349f885b2SVladimir Oltean 			port);
13241328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
132549f885b2SVladimir Oltean 		kfree_skb(skb);
132649f885b2SVladimir Oltean 		return;
132749f885b2SVladimir Oltean 	}
132849f885b2SVladimir Oltean 
132949f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
133049f885b2SVladimir Oltean 
133149f885b2SVladimir Oltean 	consume_skb(skb);
133249f885b2SVladimir Oltean 	kfree(xmit_work);
133349f885b2SVladimir Oltean }
133449f885b2SVladimir Oltean 
133535d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
133635d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
133749f885b2SVladimir Oltean {
133835d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
133949f885b2SVladimir Oltean 
134035d97680SVladimir Oltean 	switch (proto) {
134135d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
134235d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
134335d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
134449f885b2SVladimir Oltean 		return 0;
134535d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
134635d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
134749f885b2SVladimir Oltean 		return 0;
134835d97680SVladimir Oltean 	default:
134935d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
135049f885b2SVladimir Oltean 	}
135149f885b2SVladimir Oltean }
135249f885b2SVladimir Oltean 
135356051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
135456051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
135556051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
135656051948SVladimir Oltean  * (which will not work).
135756051948SVladimir Oltean  */
135856051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
135956051948SVladimir Oltean {
136056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
136156051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1362f2e2662cSVladimir Oltean 	unsigned long cpu_flood;
13632960bb14SVladimir Oltean 	struct dsa_port *dp;
13642960bb14SVladimir Oltean 	int err;
136556051948SVladimir Oltean 
136656051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
136756051948SVladimir Oltean 	if (err)
136856051948SVladimir Oltean 		return err;
136956051948SVladimir Oltean 
1370d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1371d1cc0e93SVladimir Oltean 	if (err)
13726b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1373d1cc0e93SVladimir Oltean 
13742b49d128SYangbo Lu 	if (ocelot->ptp) {
13752ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13762b49d128SYangbo Lu 		if (err) {
13772b49d128SYangbo Lu 			dev_err(ocelot->dev,
13782b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13792b49d128SYangbo Lu 			ocelot->ptp = 0;
13802b49d128SYangbo Lu 		}
13812b49d128SYangbo Lu 	}
138256051948SVladimir Oltean 
13832960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13842960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1385bd2b3161SXiaoliang Yang 
1386bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1387bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1388bd2b3161SXiaoliang Yang 		 */
13892960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
139056051948SVladimir Oltean 	}
139156051948SVladimir Oltean 
1392f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1393f59fd9caSVladimir Oltean 	if (err)
13946b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1395f59fd9caSVladimir Oltean 
13962960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1397adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1398adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
13991cf3299bSVladimir Oltean 		 */
1400c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
1401f2e2662cSVladimir Oltean 
1402f2e2662cSVladimir Oltean 		/* Start off with flooding disabled towards the NPI port
1403f2e2662cSVladimir Oltean 		 * (actually CPU port module).
1404f2e2662cSVladimir Oltean 		 */
1405f2e2662cSVladimir Oltean 		cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
1406f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
1407f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
1408f2e2662cSVladimir Oltean 
14098d5f7954SVladimir Oltean 		break;
1410adb3dccfSVladimir Oltean 	}
14111cf3299bSVladimir Oltean 
14120b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1413c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
141454c31984SVladimir Oltean 	ds->fdb_isolation = true;
141554c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1416bdeced75SVladimir Oltean 
141756051948SVladimir Oltean 	return 0;
14186b73b7c9SVladimir Oltean 
14196b73b7c9SVladimir Oltean out_deinit_ports:
14202960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14212960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
14226b73b7c9SVladimir Oltean 
14236b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
14246b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
14256b73b7c9SVladimir Oltean 
14266b73b7c9SVladimir Oltean out_mdiobus_free:
14276b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14286b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14296b73b7c9SVladimir Oltean 
14306b73b7c9SVladimir Oltean 	return err;
143156051948SVladimir Oltean }
143256051948SVladimir Oltean 
143356051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
143456051948SVladimir Oltean {
143556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1436bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14372960bb14SVladimir Oltean 	struct dsa_port *dp;
1438bdeced75SVladimir Oltean 
14392960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14402960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14418d5f7954SVladimir Oltean 		break;
1442adb3dccfSVladimir Oltean 	}
1443adb3dccfSVladimir Oltean 
14442960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14452960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1446d19741b0SVladimir Oltean 
144749f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
144849f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
144949f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
145049f885b2SVladimir Oltean 
1451d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1452d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
145356051948SVladimir Oltean }
145456051948SVladimir Oltean 
1455c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1456c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1457c0bcf537SYangbo Lu {
1458c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1459c0bcf537SYangbo Lu 
1460c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1461c0bcf537SYangbo Lu }
1462c0bcf537SYangbo Lu 
1463c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1464c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1465c0bcf537SYangbo Lu {
1466c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
146799348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
146899348004SVladimir Oltean 	bool using_tag_8021q;
146999348004SVladimir Oltean 	int err;
1470c0bcf537SYangbo Lu 
147199348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
147299348004SVladimir Oltean 	if (err)
147399348004SVladimir Oltean 		return err;
147499348004SVladimir Oltean 
147599348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
147699348004SVladimir Oltean 
147799348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1478c0bcf537SYangbo Lu }
1479c0bcf537SYangbo Lu 
1480d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
14810a6f17c6SVladimir Oltean {
14820a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1483dbd03285SVladimir Oltean 	int err = 0, grp = 0;
14840a6f17c6SVladimir Oltean 
14850a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14860a6f17c6SVladimir Oltean 		return false;
14870a6f17c6SVladimir Oltean 
14880a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14890a6f17c6SVladimir Oltean 		return false;
14900a6f17c6SVladimir Oltean 
14910a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14920a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14930a6f17c6SVladimir Oltean 		unsigned int type;
14940a6f17c6SVladimir Oltean 
14950a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14960a6f17c6SVladimir Oltean 		if (err)
14970a6f17c6SVladimir Oltean 			goto out;
14980a6f17c6SVladimir Oltean 
14990a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
15000a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
15010a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
15020a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
15030a6f17c6SVladimir Oltean 		 * here and dropping those.
15040a6f17c6SVladimir Oltean 		 */
15050a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
15060a6f17c6SVladimir Oltean 
15070a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
15080a6f17c6SVladimir Oltean 
15090a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
15100a6f17c6SVladimir Oltean 
15110a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
15120a6f17c6SVladimir Oltean 			kfree_skb(skb);
15130a6f17c6SVladimir Oltean 			continue;
15140a6f17c6SVladimir Oltean 		}
15150a6f17c6SVladimir Oltean 
15160a6f17c6SVladimir Oltean 		netif_rx(skb);
15170a6f17c6SVladimir Oltean 	}
15180a6f17c6SVladimir Oltean 
15190a6f17c6SVladimir Oltean out:
15205d3bb7ddSVladimir Oltean 	if (err < 0) {
15215d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
15225d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
15235d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
15240a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
15255d3bb7ddSVladimir Oltean 	}
15260a6f17c6SVladimir Oltean 
15270a6f17c6SVladimir Oltean 	return true;
15280a6f17c6SVladimir Oltean }
15290a6f17c6SVladimir Oltean 
1530c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1531c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1532c0bcf537SYangbo Lu {
153392f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1534c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1535c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1536c0bcf537SYangbo Lu 	struct timespec64 ts;
153792f62485SVladimir Oltean 	u32 tstamp_hi;
153892f62485SVladimir Oltean 	u64 tstamp;
1539c0bcf537SYangbo Lu 
15400a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15410a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15420a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15430a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15440a6f17c6SVladimir Oltean 	 */
1545d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
15460a6f17c6SVladimir Oltean 		kfree_skb(skb);
15470a6f17c6SVladimir Oltean 		return true;
15480a6f17c6SVladimir Oltean 	}
15490a6f17c6SVladimir Oltean 
1550c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1551c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1552c0bcf537SYangbo Lu 
1553c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1554c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1555c0bcf537SYangbo Lu 		tstamp_hi--;
1556c0bcf537SYangbo Lu 
1557c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1558c0bcf537SYangbo Lu 
1559c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1560c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1561c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1562c0bcf537SYangbo Lu 	return false;
1563c0bcf537SYangbo Lu }
1564c0bcf537SYangbo Lu 
15655c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15665c5416f5SYangbo Lu 			   struct sk_buff *skb)
1567c0bcf537SYangbo Lu {
1568c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1569682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1570c0bcf537SYangbo Lu 
1571682eaad9SYangbo Lu 	if (!ocelot->ptp)
15725c5416f5SYangbo Lu 		return;
1573c0bcf537SYangbo Lu 
157452849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
157552849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
157652849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
157752849bcfSVladimir Oltean 				    port);
1578682eaad9SYangbo Lu 		return;
157952849bcfSVladimir Oltean 	}
1580682eaad9SYangbo Lu 
1581682eaad9SYangbo Lu 	if (clone)
1582c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15835c5416f5SYangbo Lu }
1584c0bcf537SYangbo Lu 
15850b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15860b912fc9SVladimir Oltean {
15870b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15880b912fc9SVladimir Oltean 
15890b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15900b912fc9SVladimir Oltean 
15910b912fc9SVladimir Oltean 	return 0;
15920b912fc9SVladimir Oltean }
15930b912fc9SVladimir Oltean 
15940b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15950b912fc9SVladimir Oltean {
15960b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15970b912fc9SVladimir Oltean 
15980b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15990b912fc9SVladimir Oltean }
16000b912fc9SVladimir Oltean 
160107d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
160207d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
160307d985eeSVladimir Oltean {
160407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
160599348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
160699348004SVladimir Oltean 	bool using_tag_8021q;
160799348004SVladimir Oltean 	int err;
160807d985eeSVladimir Oltean 
160999348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
161099348004SVladimir Oltean 	if (err)
161199348004SVladimir Oltean 		return err;
161299348004SVladimir Oltean 
161399348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
161499348004SVladimir Oltean 
161599348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
161607d985eeSVladimir Oltean }
161707d985eeSVladimir Oltean 
161807d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
161907d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
162007d985eeSVladimir Oltean {
162107d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
162207d985eeSVladimir Oltean 
162307d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
162407d985eeSVladimir Oltean }
162507d985eeSVladimir Oltean 
162607d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
162707d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
162807d985eeSVladimir Oltean {
162907d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
163007d985eeSVladimir Oltean 
163107d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
163207d985eeSVladimir Oltean }
163307d985eeSVladimir Oltean 
1634fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1635fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1636fc411eaaSVladimir Oltean {
1637fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1638fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1639fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16405f035af7SPo Liu 		.burst = policer->burst,
1641fc411eaaSVladimir Oltean 	};
1642fc411eaaSVladimir Oltean 
1643fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1644fc411eaaSVladimir Oltean }
1645fc411eaaSVladimir Oltean 
1646fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1647fc411eaaSVladimir Oltean {
1648fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1649fc411eaaSVladimir Oltean 
1650fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1651fc411eaaSVladimir Oltean }
1652fc411eaaSVladimir Oltean 
1653de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1654de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1655de143c0eSXiaoliang Yang 			       void *type_data)
1656de143c0eSXiaoliang Yang {
1657de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1658de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1659de143c0eSXiaoliang Yang 
1660de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1661de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1662de143c0eSXiaoliang Yang 	else
1663de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1664de143c0eSXiaoliang Yang }
1665de143c0eSXiaoliang Yang 
1666f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1667f59fd9caSVladimir Oltean 			     u16 pool_index,
1668f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1669f59fd9caSVladimir Oltean {
1670f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1671f59fd9caSVladimir Oltean 
1672f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1673f59fd9caSVladimir Oltean }
1674f59fd9caSVladimir Oltean 
1675f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1676f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1677f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1678f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1679f59fd9caSVladimir Oltean {
1680f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1681f59fd9caSVladimir Oltean 
1682f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1683f59fd9caSVladimir Oltean 				  threshold_type, extack);
1684f59fd9caSVladimir Oltean }
1685f59fd9caSVladimir Oltean 
1686f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1687f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1688f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1689f59fd9caSVladimir Oltean {
1690f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1691f59fd9caSVladimir Oltean 
1692f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1693f59fd9caSVladimir Oltean 				       p_threshold);
1694f59fd9caSVladimir Oltean }
1695f59fd9caSVladimir Oltean 
1696f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1697f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1698f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1699f59fd9caSVladimir Oltean {
1700f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1701f59fd9caSVladimir Oltean 
1702f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1703f59fd9caSVladimir Oltean 				       threshold, extack);
1704f59fd9caSVladimir Oltean }
1705f59fd9caSVladimir Oltean 
1706f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1707f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1708f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1709f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1710f59fd9caSVladimir Oltean {
1711f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1712f59fd9caSVladimir Oltean 
1713f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1714f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1715f59fd9caSVladimir Oltean 					  p_threshold);
1716f59fd9caSVladimir Oltean }
1717f59fd9caSVladimir Oltean 
1718f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1719f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1720f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1721f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1722f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1723f59fd9caSVladimir Oltean {
1724f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1725f59fd9caSVladimir Oltean 
1726f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1727f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1728f59fd9caSVladimir Oltean 					  extack);
1729f59fd9caSVladimir Oltean }
1730f59fd9caSVladimir Oltean 
1731f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1732f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1733f59fd9caSVladimir Oltean {
1734f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1735f59fd9caSVladimir Oltean 
1736f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1737f59fd9caSVladimir Oltean }
1738f59fd9caSVladimir Oltean 
1739f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1740f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1741f59fd9caSVladimir Oltean {
1742f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1743f59fd9caSVladimir Oltean 
1744f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1745f59fd9caSVladimir Oltean }
1746f59fd9caSVladimir Oltean 
1747f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1748f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1749f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1750f59fd9caSVladimir Oltean {
1751f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1752f59fd9caSVladimir Oltean 
1753f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1754f59fd9caSVladimir Oltean 					   p_cur, p_max);
1755f59fd9caSVladimir Oltean }
1756f59fd9caSVladimir Oltean 
1757f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1758f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1759f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1760f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1761f59fd9caSVladimir Oltean {
1762f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1763f59fd9caSVladimir Oltean 
1764f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1765f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1766f59fd9caSVladimir Oltean }
1767f59fd9caSVladimir Oltean 
1768a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1769a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1770a026c50bSHoratiu Vultur {
1771a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1772a026c50bSHoratiu Vultur 
1773a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1774a026c50bSHoratiu Vultur }
1775a026c50bSHoratiu Vultur 
1776a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1777a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1778a026c50bSHoratiu Vultur {
1779a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1780a026c50bSHoratiu Vultur 
1781a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1782a026c50bSHoratiu Vultur }
1783a026c50bSHoratiu Vultur 
1784a026c50bSHoratiu Vultur static int
1785a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1786a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1787a026c50bSHoratiu Vultur {
1788a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1789a026c50bSHoratiu Vultur 
1790a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1791a026c50bSHoratiu Vultur }
1792a026c50bSHoratiu Vultur 
1793a026c50bSHoratiu Vultur static int
1794a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1795a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1796a026c50bSHoratiu Vultur {
1797a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1798a026c50bSHoratiu Vultur 
1799a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1800a026c50bSHoratiu Vultur }
1801a026c50bSHoratiu Vultur 
1802375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
180356051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1804adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
180535d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
180656051948SVladimir Oltean 	.setup				= felix_setup,
180756051948SVladimir Oltean 	.teardown			= felix_teardown,
180856051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
180956051948SVladimir Oltean 	.get_strings			= felix_get_strings,
181056051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
181156051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
181256051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
181379fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1814bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1815864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1816bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1817bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18185cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
181956051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
182056051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
182156051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1822961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1823961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1824209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1825209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1826421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1827421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
182856051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
182956051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18308fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18318fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18328fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
183356051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
183456051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
183556051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
183656051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1837c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1838c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1839c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1840c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18410b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18420b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1843fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1844fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
184507d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
184607d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
184707d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1848de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1849f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1850f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1851f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1852f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1853f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1854f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1855f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1856f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1857f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1858f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1859a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1860a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1861a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1862a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18635da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18645da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
186556051948SVladimir Oltean };
1866319e4dd1SVladimir Oltean 
1867319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1868319e4dd1SVladimir Oltean {
1869319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1870319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1871319e4dd1SVladimir Oltean 
1872319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1873319e4dd1SVladimir Oltean 		return NULL;
1874319e4dd1SVladimir Oltean 
1875319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1876319e4dd1SVladimir Oltean }
1877319e4dd1SVladimir Oltean 
1878319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1879319e4dd1SVladimir Oltean {
1880319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1881319e4dd1SVladimir Oltean 
1882319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1883319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1884319e4dd1SVladimir Oltean 		return -EINVAL;
1885319e4dd1SVladimir Oltean 
1886319e4dd1SVladimir Oltean 	return dp->index;
1887319e4dd1SVladimir Oltean }
1888