xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision e1846cff2fe614d93a2f89461b5935678fd34bd9)
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);
406*e1846cffSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
40799348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
40899348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
40999348004SVladimir Oltean 	unsigned long port_mask;
4102960bb14SVladimir Oltean 	struct dsa_port *dp;
41199348004SVladimir Oltean 	bool cpu_copy_ena;
41299348004SVladimir Oltean 	int cpu = -1, err;
413c8c0ba4fSVladimir Oltean 
41499348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
41599348004SVladimir Oltean 		return 0;
416c8c0ba4fSVladimir Oltean 
41799348004SVladimir Oltean 	/* Figure out the current CPU port */
4182960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
4192960bb14SVladimir Oltean 		cpu = dp->index;
4208d5f7954SVladimir Oltean 		break;
421c8c0ba4fSVladimir Oltean 	}
4228d5f7954SVladimir Oltean 
423d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
424d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
425d78637a8SVladimir Oltean 	 */
426*e1846cffSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
427c8c0ba4fSVladimir Oltean 
42899348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
429*e1846cffSVladimir Oltean 	list_for_each_entry(trap, &block_vcap_is2->rules, list) {
430*e1846cffSVladimir Oltean 		if (!trap->is_trap)
431*e1846cffSVladimir Oltean 			continue;
432*e1846cffSVladimir Oltean 
43399348004SVladimir Oltean 		/* Figure out the current trapping destination */
43499348004SVladimir Oltean 		if (using_tag_8021q) {
43599348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
43699348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
43799348004SVladimir Oltean 			 * port module.
438c8c0ba4fSVladimir Oltean 			 */
43999348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
44099348004SVladimir Oltean 			port_mask = BIT(cpu);
44199348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
442c8c0ba4fSVladimir Oltean 		} else {
44399348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
44499348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
445c8c0ba4fSVladimir Oltean 			 */
44699348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
44799348004SVladimir Oltean 			port_mask = 0;
44899348004SVladimir Oltean 			cpu_copy_ena = true;
449c8c0ba4fSVladimir Oltean 		}
450c8c0ba4fSVladimir Oltean 
45199348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
45299348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
45399348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
45499348004SVladimir Oltean 			continue;
455c8c0ba4fSVladimir Oltean 
45699348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
45799348004SVladimir Oltean 		trap->action.port_mask = port_mask;
45899348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
4590a6f17c6SVladimir Oltean 
46099348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
461c8c0ba4fSVladimir Oltean 		if (err)
462c8c0ba4fSVladimir Oltean 			return err;
46399348004SVladimir Oltean 	}
464c8c0ba4fSVladimir Oltean 
46599348004SVladimir Oltean 	return 0;
466c8c0ba4fSVladimir Oltean }
467c8c0ba4fSVladimir Oltean 
468c69f40acSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
469e21268efSVladimir Oltean {
470e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4712960bb14SVladimir Oltean 	struct dsa_port *dp;
4722960bb14SVladimir Oltean 	int err;
473e21268efSVladimir Oltean 
474e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
475e21268efSVladimir Oltean 
4762960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
477e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
478e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
479e21268efSVladimir Oltean 		 * for 2 reasons:
480e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
481e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
482e21268efSVladimir Oltean 		 *   into the system.
483e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
484e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
485e21268efSVladimir Oltean 		 *   port module.
486e21268efSVladimir Oltean 		 */
487e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
488e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4892960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
490e21268efSVladimir Oltean 	}
491e21268efSVladimir Oltean 
4925da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
493e21268efSVladimir Oltean 	if (err)
494d7b1fd52SVladimir Oltean 		return err;
495d7b1fd52SVladimir Oltean 
496c69f40acSVladimir Oltean 	err = dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_tag_8021q_port);
497d7b1fd52SVladimir Oltean 	if (err)
498d7b1fd52SVladimir Oltean 		goto out_tag_8021q_unregister;
499e21268efSVladimir Oltean 
500c69f40acSVladimir Oltean 	err = dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_tag_8021q_port);
501f9cef64fSVladimir Oltean 	if (err)
502f9cef64fSVladimir Oltean 		goto out_migrate_fdbs;
503b903a6bdSVladimir Oltean 
504b903a6bdSVladimir Oltean 	felix_migrate_flood_to_tag_8021q_port(ds, cpu);
505f9cef64fSVladimir Oltean 
506f9cef64fSVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
507f9cef64fSVladimir Oltean 	if (err)
508b903a6bdSVladimir Oltean 		goto out_migrate_flood;
509f9cef64fSVladimir Oltean 
51099348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
51199348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
51299348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
51399348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
51499348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
51599348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
51699348004SVladimir Oltean 	 */
51799348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
51899348004SVladimir Oltean 
519e21268efSVladimir Oltean 	return 0;
520e21268efSVladimir Oltean 
521b903a6bdSVladimir Oltean out_migrate_flood:
522b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
523f9cef64fSVladimir Oltean 	dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
524f9cef64fSVladimir Oltean out_migrate_fdbs:
525f9cef64fSVladimir Oltean 	dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
526d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
527d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
528e21268efSVladimir Oltean 	return err;
529e21268efSVladimir Oltean }
530e21268efSVladimir Oltean 
531e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
532e21268efSVladimir Oltean {
533e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5342960bb14SVladimir Oltean 	struct dsa_port *dp;
5352960bb14SVladimir Oltean 	int err;
536e21268efSVladimir Oltean 
53799348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
538c8c0ba4fSVladimir Oltean 	if (err)
539c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
540c8c0ba4fSVladimir Oltean 			err);
541c8c0ba4fSVladimir Oltean 
542d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
543e21268efSVladimir Oltean 
5442960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
545e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
546e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
547e21268efSVladimir Oltean 		 */
548e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
549e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
550e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
5512960bb14SVladimir Oltean 				 dp->index);
552e21268efSVladimir Oltean 	}
553e21268efSVladimir Oltean 
554e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
555e21268efSVladimir Oltean }
556e21268efSVladimir Oltean 
557adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
558adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
559adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
560adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
561adb3dccfSVladimir Oltean  * DSA master.
562adb3dccfSVladimir Oltean  */
563adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
564adb3dccfSVladimir Oltean {
565adb3dccfSVladimir Oltean 	ocelot->npi = port;
566adb3dccfSVladimir Oltean 
567adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
568adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
569adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
570adb3dccfSVladimir Oltean 
571adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
572adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
573adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
574adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
575adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
576adb3dccfSVladimir Oltean 
577adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
578adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
579adb3dccfSVladimir Oltean }
580adb3dccfSVladimir Oltean 
581adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
582adb3dccfSVladimir Oltean {
583adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
584adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
585adb3dccfSVladimir Oltean 
586adb3dccfSVladimir Oltean 	ocelot->npi = -1;
587adb3dccfSVladimir Oltean 
588adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
589adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
590adb3dccfSVladimir Oltean 
591adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
592adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
593adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
594adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
595adb3dccfSVladimir Oltean 
596adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
597adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
598adb3dccfSVladimir Oltean }
599adb3dccfSVladimir Oltean 
600c69f40acSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
601adb3dccfSVladimir Oltean {
602adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
603f9cef64fSVladimir Oltean 	int err;
604f9cef64fSVladimir Oltean 
605c69f40acSVladimir Oltean 	err = dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
606f9cef64fSVladimir Oltean 	if (err)
607f9cef64fSVladimir Oltean 		return err;
608f9cef64fSVladimir Oltean 
609c69f40acSVladimir Oltean 	err = dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
610f9cef64fSVladimir Oltean 	if (err)
611f9cef64fSVladimir Oltean 		goto out_migrate_fdbs;
612b903a6bdSVladimir Oltean 
613b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
614adb3dccfSVladimir Oltean 
615adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
616adb3dccfSVladimir Oltean 
617adb3dccfSVladimir Oltean 	return 0;
618f9cef64fSVladimir Oltean 
619f9cef64fSVladimir Oltean out_migrate_fdbs:
620c69f40acSVladimir Oltean 	dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_tag_8021q_port);
621f9cef64fSVladimir Oltean 
622f9cef64fSVladimir Oltean 	return err;
623adb3dccfSVladimir Oltean }
624adb3dccfSVladimir Oltean 
625adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
626adb3dccfSVladimir Oltean {
627adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
628adb3dccfSVladimir Oltean 
629adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
630adb3dccfSVladimir Oltean }
631adb3dccfSVladimir Oltean 
632adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
633c69f40acSVladimir Oltean 				  enum dsa_tag_protocol proto)
634adb3dccfSVladimir Oltean {
635adb3dccfSVladimir Oltean 	int err;
636adb3dccfSVladimir Oltean 
637adb3dccfSVladimir Oltean 	switch (proto) {
6387c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
639adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
640c69f40acSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
641adb3dccfSVladimir Oltean 		break;
642e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
643c69f40acSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
644e21268efSVladimir Oltean 		break;
645adb3dccfSVladimir Oltean 	default:
646adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
647adb3dccfSVladimir Oltean 	}
648adb3dccfSVladimir Oltean 
649adb3dccfSVladimir Oltean 	return err;
650adb3dccfSVladimir Oltean }
651adb3dccfSVladimir Oltean 
652adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
653adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
654adb3dccfSVladimir Oltean {
655adb3dccfSVladimir Oltean 	switch (proto) {
6567c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
657adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
658adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
659adb3dccfSVladimir Oltean 		break;
660e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
661e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
662e21268efSVladimir Oltean 		break;
663adb3dccfSVladimir Oltean 	default:
664adb3dccfSVladimir Oltean 		break;
665adb3dccfSVladimir Oltean 	}
666adb3dccfSVladimir Oltean }
667adb3dccfSVladimir Oltean 
668e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
669e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
670e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
671e21268efSVladimir Oltean  */
672adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
673adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
674adb3dccfSVladimir Oltean {
675adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
676adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
677adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
67800fa91bcSVladimir Oltean 	bool cpu_port_active = false;
67900fa91bcSVladimir Oltean 	struct dsa_port *dp;
680adb3dccfSVladimir Oltean 	int err;
681adb3dccfSVladimir Oltean 
6827c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
6837c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
684e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
685adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
686adb3dccfSVladimir Oltean 
68700fa91bcSVladimir Oltean 	/* We don't support multiple CPU ports, yet the DT blob may have
68800fa91bcSVladimir Oltean 	 * multiple CPU ports defined. The first CPU port is the active one,
68900fa91bcSVladimir Oltean 	 * the others are inactive. In this case, DSA will call
69000fa91bcSVladimir Oltean 	 * ->change_tag_protocol() multiple times, once per CPU port.
69100fa91bcSVladimir Oltean 	 * Since we implement the tagging protocol change towards "ocelot" or
69200fa91bcSVladimir Oltean 	 * "seville" as effectively initializing the NPI port, what we are
69300fa91bcSVladimir Oltean 	 * doing is effectively changing who the NPI port is to the last @cpu
69400fa91bcSVladimir Oltean 	 * argument passed, which is an unused DSA CPU port and not the one
69500fa91bcSVladimir Oltean 	 * that should actively pass traffic.
69600fa91bcSVladimir Oltean 	 * Suppress DSA's calls on CPU ports that are inactive.
69700fa91bcSVladimir Oltean 	 */
69800fa91bcSVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds) {
69900fa91bcSVladimir Oltean 		if (dp->cpu_dp->index == cpu) {
70000fa91bcSVladimir Oltean 			cpu_port_active = true;
70100fa91bcSVladimir Oltean 			break;
70200fa91bcSVladimir Oltean 		}
70300fa91bcSVladimir Oltean 	}
70400fa91bcSVladimir Oltean 
70500fa91bcSVladimir Oltean 	if (!cpu_port_active)
70600fa91bcSVladimir Oltean 		return 0;
70700fa91bcSVladimir Oltean 
708adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
709adb3dccfSVladimir Oltean 
710c69f40acSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
711adb3dccfSVladimir Oltean 	if (err) {
712c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
713adb3dccfSVladimir Oltean 		return err;
714adb3dccfSVladimir Oltean 	}
715adb3dccfSVladimir Oltean 
716adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
717adb3dccfSVladimir Oltean 
718adb3dccfSVladimir Oltean 	return 0;
719adb3dccfSVladimir Oltean }
720adb3dccfSVladimir Oltean 
72156051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
7224d776482SFlorian Fainelli 						    int port,
7234d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
72456051948SVladimir Oltean {
725adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
726adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
727adb3dccfSVladimir Oltean 
728adb3dccfSVladimir Oltean 	return felix->tag_proto;
72956051948SVladimir Oltean }
73056051948SVladimir Oltean 
73156051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
73256051948SVladimir Oltean 				 unsigned int ageing_time)
73356051948SVladimir Oltean {
73456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73556051948SVladimir Oltean 
73656051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
73756051948SVladimir Oltean 
73856051948SVladimir Oltean 	return 0;
73956051948SVladimir Oltean }
74056051948SVladimir Oltean 
7415cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
7425cad43a5SVladimir Oltean {
7435cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7445cad43a5SVladimir Oltean 	int err;
7455cad43a5SVladimir Oltean 
7465cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
7475cad43a5SVladimir Oltean 	if (err)
7485cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
7495cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7505cad43a5SVladimir Oltean }
7515cad43a5SVladimir Oltean 
75256051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
75356051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
75456051948SVladimir Oltean {
75556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
75656051948SVladimir Oltean 
75756051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
75856051948SVladimir Oltean }
75956051948SVladimir Oltean 
76056051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
761c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
762c2693363SVladimir Oltean 			 struct dsa_db db)
76356051948SVladimir Oltean {
76454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
76556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
76656051948SVladimir Oltean 
76754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
76854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76954c31984SVladimir Oltean 
7707e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7717e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7727e580490SVladimir Oltean 		return 0;
7737e580490SVladimir Oltean 
77454c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
77556051948SVladimir Oltean }
77656051948SVladimir Oltean 
77756051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
778c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
779c2693363SVladimir Oltean 			 struct dsa_db db)
78056051948SVladimir Oltean {
78154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
78256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
78356051948SVladimir Oltean 
78454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
78554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
78654c31984SVladimir Oltean 
7877e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7887e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7897e580490SVladimir Oltean 		return 0;
7907e580490SVladimir Oltean 
79154c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
79256051948SVladimir Oltean }
79356051948SVladimir Oltean 
794961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
795c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
796c2693363SVladimir Oltean 			     struct dsa_db db)
797961d8b69SVladimir Oltean {
79854c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
799961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
800961d8b69SVladimir Oltean 
80154c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
80254c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
80354c31984SVladimir Oltean 
80454c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
805961d8b69SVladimir Oltean }
806961d8b69SVladimir Oltean 
807961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
808c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
809c2693363SVladimir Oltean 			     struct dsa_db db)
810961d8b69SVladimir Oltean {
81154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
812961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
813961d8b69SVladimir Oltean 
81454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
81554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
81654c31984SVladimir Oltean 
81754c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
818961d8b69SVladimir Oltean }
819961d8b69SVladimir Oltean 
820a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
821c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
822c2693363SVladimir Oltean 			 struct dsa_db db)
823209edf95SVladimir Oltean {
82454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
825209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
826209edf95SVladimir Oltean 
82754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
82854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
82954c31984SVladimir Oltean 
8307e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
8317e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
8327e580490SVladimir Oltean 		return 0;
8337e580490SVladimir Oltean 
83454c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
835209edf95SVladimir Oltean }
836209edf95SVladimir Oltean 
837209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
838c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
839c2693363SVladimir Oltean 			 struct dsa_db db)
840209edf95SVladimir Oltean {
84154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
842209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
843209edf95SVladimir Oltean 
84454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
84554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
84654c31984SVladimir Oltean 
8477e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
8487e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
8497e580490SVladimir Oltean 		return 0;
8507e580490SVladimir Oltean 
85154c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
852209edf95SVladimir Oltean }
853209edf95SVladimir Oltean 
85456051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
85556051948SVladimir Oltean 				       u8 state)
85656051948SVladimir Oltean {
85756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85856051948SVladimir Oltean 
85956051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
86056051948SVladimir Oltean }
86156051948SVladimir Oltean 
862421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
863421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
864421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
865421741eaSVladimir Oltean {
866421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
867421741eaSVladimir Oltean 
868421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
869421741eaSVladimir Oltean }
870421741eaSVladimir Oltean 
871421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
872421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
873421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
874421741eaSVladimir Oltean {
875421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
876421741eaSVladimir Oltean 
877421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
878421741eaSVladimir Oltean 
879421741eaSVladimir Oltean 	return 0;
880421741eaSVladimir Oltean }
881421741eaSVladimir Oltean 
88256051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
88306b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
88406b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
88556051948SVladimir Oltean {
88656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
88756051948SVladimir Oltean 
88854c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
88954c31984SVladimir Oltean 				       extack);
89056051948SVladimir Oltean }
89156051948SVladimir Oltean 
89256051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
893d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
89456051948SVladimir Oltean {
89556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
89656051948SVladimir Oltean 
897d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
89856051948SVladimir Oltean }
89956051948SVladimir Oltean 
9008fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
901dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
9028fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
9038fe6832eSVladimir Oltean {
9048fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9058fe6832eSVladimir Oltean 
906dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
9078fe6832eSVladimir Oltean }
9088fe6832eSVladimir Oltean 
9098fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
910dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
9118fe6832eSVladimir Oltean {
9128fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9138fe6832eSVladimir Oltean 
914dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
9158fe6832eSVladimir Oltean 
9168fe6832eSVladimir Oltean 	return 0;
9178fe6832eSVladimir Oltean }
9188fe6832eSVladimir Oltean 
9198fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
9208fe6832eSVladimir Oltean {
9218fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
9228fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9238fe6832eSVladimir Oltean 
9248fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
9258fe6832eSVladimir Oltean 
9268fe6832eSVladimir Oltean 	return 0;
9278fe6832eSVladimir Oltean }
9288fe6832eSVladimir Oltean 
92956051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
93001af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
93101af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
93256051948SVladimir Oltean {
9332f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
934b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
9352f0402feSVladimir Oltean 
9369a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
9379a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
9389a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
9399a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
9409a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
9419a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
9429a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
9439a720680SVladimir Oltean 	 */
9449a720680SVladimir Oltean 	if (port == ocelot->npi)
9459a720680SVladimir Oltean 		return 0;
9469a720680SVladimir Oltean 
947b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9482f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
94901af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
95001af940eSVladimir Oltean 				   extack);
95156051948SVladimir Oltean }
95256051948SVladimir Oltean 
95389153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
95489153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
95556051948SVladimir Oltean {
95656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
95756051948SVladimir Oltean 
9583b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
95956051948SVladimir Oltean }
96056051948SVladimir Oltean 
9611958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
96231046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
96331046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
96456051948SVladimir Oltean {
96556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
966183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9671958d581SVladimir Oltean 	int err;
96856051948SVladimir Oltean 
96901af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9701958d581SVladimir Oltean 	if (err)
9711958d581SVladimir Oltean 		return err;
9721958d581SVladimir Oltean 
9731958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
974183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
975183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
97656051948SVladimir Oltean }
97756051948SVladimir Oltean 
97856051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
97956051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
98056051948SVladimir Oltean {
98156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
98256051948SVladimir Oltean 
983b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
98456051948SVladimir Oltean }
98556051948SVladimir Oltean 
98679fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
98779fda660SRussell King (Oracle) 				   struct phylink_config *config)
98879fda660SRussell King (Oracle) {
98979fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
99079fda660SRussell King (Oracle) 
991f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
992f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
993f6f04c02SRussell King (Oracle) 	 * as non-legacy.
994f6f04c02SRussell King (Oracle) 	 */
995f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
996f6f04c02SRussell King (Oracle) 
99779fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
99879fda660SRussell King (Oracle) 		  config->supported_interfaces);
99979fda660SRussell King (Oracle) }
100079fda660SRussell King (Oracle) 
1001bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
1002bdeced75SVladimir Oltean 				   unsigned long *supported,
1003bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
1004bdeced75SVladimir Oltean {
1005bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1006375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1007bdeced75SVladimir Oltean 
1008375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
1009375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
1010bdeced75SVladimir Oltean }
1011bdeced75SVladimir Oltean 
1012864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
1013864ba485SRussell King (Oracle) 							int port,
1014864ba485SRussell King (Oracle) 							phy_interface_t iface)
1015bdeced75SVladimir Oltean {
1016bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1017bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1018864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
1019bdeced75SVladimir Oltean 
102049af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
1021864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
1022864ba485SRussell King (Oracle) 
1023864ba485SRussell King (Oracle) 	return pcs;
1024bdeced75SVladimir Oltean }
1025bdeced75SVladimir Oltean 
1026bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
1027bdeced75SVladimir Oltean 					unsigned int link_an_mode,
1028bdeced75SVladimir Oltean 					phy_interface_t interface)
1029bdeced75SVladimir Oltean {
1030bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1031bdeced75SVladimir Oltean 
1032e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1033e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
1034bdeced75SVladimir Oltean }
1035bdeced75SVladimir Oltean 
1036bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
1037bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
1038bdeced75SVladimir Oltean 				      phy_interface_t interface,
10395b502a7bSRussell King 				      struct phy_device *phydev,
10405b502a7bSRussell King 				      int speed, int duplex,
10415b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
1042bdeced75SVladimir Oltean {
1043bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10447e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1045bdeced75SVladimir Oltean 
1046e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1047e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1048e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10497e14a2dcSVladimir Oltean 
10507e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10517e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1052bdeced75SVladimir Oltean }
1053bdeced75SVladimir Oltean 
1054bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1055bd2b3161SXiaoliang Yang {
1056bd2b3161SXiaoliang Yang 	int i;
1057bd2b3161SXiaoliang Yang 
1058bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1059bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1060bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1061bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1062bd2b3161SXiaoliang Yang 		       port);
1063bd2b3161SXiaoliang Yang 
106470d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1065bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1066bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1067bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1068bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1069bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1070bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1071bd2b3161SXiaoliang Yang 			      port, i);
1072bd2b3161SXiaoliang Yang 	}
1073bd2b3161SXiaoliang Yang }
1074bd2b3161SXiaoliang Yang 
107556051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
107656051948SVladimir Oltean 			      u32 stringset, u8 *data)
107756051948SVladimir Oltean {
107856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
107956051948SVladimir Oltean 
108056051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
108156051948SVladimir Oltean }
108256051948SVladimir Oltean 
108356051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
108456051948SVladimir Oltean {
108556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
108656051948SVladimir Oltean 
108756051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
108856051948SVladimir Oltean }
108956051948SVladimir Oltean 
109056051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
109156051948SVladimir Oltean {
109256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
109356051948SVladimir Oltean 
109456051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
109556051948SVladimir Oltean }
109656051948SVladimir Oltean 
109756051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
109856051948SVladimir Oltean 			     struct ethtool_ts_info *info)
109956051948SVladimir Oltean {
110056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
110156051948SVladimir Oltean 
110256051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
110356051948SVladimir Oltean }
110456051948SVladimir Oltean 
1105acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1106acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1107acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1108acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1109acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1110acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1111acf242fcSColin Foster };
1112acf242fcSColin Foster 
1113acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1114acf242fcSColin Foster 				   phy_interface_t phy_mode)
1115acf242fcSColin Foster {
1116acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1117acf242fcSColin Foster 
1118acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1119acf242fcSColin Foster 		return 0;
1120acf242fcSColin Foster 	return -EOPNOTSUPP;
1121acf242fcSColin Foster }
1122acf242fcSColin Foster 
1123bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1124bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1125bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1126bdeced75SVladimir Oltean {
1127bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1128bdeced75SVladimir Oltean 	struct device_node *child;
1129bdeced75SVladimir Oltean 
113037fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1131bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1132bdeced75SVladimir Oltean 		u32 port;
1133bdeced75SVladimir Oltean 		int err;
1134bdeced75SVladimir Oltean 
1135bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1136bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1137bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1138bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1139bdeced75SVladimir Oltean 			of_node_put(child);
1140bdeced75SVladimir Oltean 			return -ENODEV;
1141bdeced75SVladimir Oltean 		}
1142bdeced75SVladimir Oltean 
1143bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1144bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1145bdeced75SVladimir Oltean 		if (err) {
1146bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1147bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1148bdeced75SVladimir Oltean 				port);
1149bdeced75SVladimir Oltean 			of_node_put(child);
1150bdeced75SVladimir Oltean 			return -ENODEV;
1151bdeced75SVladimir Oltean 		}
1152bdeced75SVladimir Oltean 
1153acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1154bdeced75SVladimir Oltean 		if (err < 0) {
1155bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1156bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
115759ebb430SSumera Priyadarsini 			of_node_put(child);
1158bdeced75SVladimir Oltean 			return err;
1159bdeced75SVladimir Oltean 		}
1160bdeced75SVladimir Oltean 
1161bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1162bdeced75SVladimir Oltean 	}
1163bdeced75SVladimir Oltean 
1164bdeced75SVladimir Oltean 	return 0;
1165bdeced75SVladimir Oltean }
1166bdeced75SVladimir Oltean 
1167bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1168bdeced75SVladimir Oltean {
1169bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1170bdeced75SVladimir Oltean 	struct device_node *switch_node;
1171bdeced75SVladimir Oltean 	struct device_node *ports_node;
1172bdeced75SVladimir Oltean 	int err;
1173bdeced75SVladimir Oltean 
1174bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1175bdeced75SVladimir Oltean 
1176bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1177abecbfcdSVladimir Oltean 	if (!ports_node)
1178abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1179bdeced75SVladimir Oltean 	if (!ports_node) {
1180abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1181bdeced75SVladimir Oltean 		return -ENODEV;
1182bdeced75SVladimir Oltean 	}
1183bdeced75SVladimir Oltean 
1184bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1185bdeced75SVladimir Oltean 	of_node_put(ports_node);
1186bdeced75SVladimir Oltean 
1187bdeced75SVladimir Oltean 	return err;
1188bdeced75SVladimir Oltean }
1189bdeced75SVladimir Oltean 
119056051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
119156051948SVladimir Oltean {
119256051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1193bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1194b4024c9eSClaudiu Manoil 	struct resource res;
119556051948SVladimir Oltean 	int port, i, err;
119656051948SVladimir Oltean 
119756051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
119856051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
119956051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
120056051948SVladimir Oltean 	if (!ocelot->ports)
120156051948SVladimir Oltean 		return -ENOMEM;
120256051948SVladimir Oltean 
120356051948SVladimir Oltean 	ocelot->map		= felix->info->map;
120456051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
120556051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
120621ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
120707d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
120877043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
120977043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
121077043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
121177043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
121256051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1213cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1214cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1215f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
121656051948SVladimir Oltean 
1217bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1218bdeced75SVladimir Oltean 				 GFP_KERNEL);
1219bdeced75SVladimir Oltean 	if (!port_phy_modes)
1220bdeced75SVladimir Oltean 		return -ENOMEM;
1221bdeced75SVladimir Oltean 
1222bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1223bdeced75SVladimir Oltean 	if (err) {
1224bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1225bdeced75SVladimir Oltean 		return err;
1226bdeced75SVladimir Oltean 	}
1227bdeced75SVladimir Oltean 
122856051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
122956051948SVladimir Oltean 		struct regmap *target;
123056051948SVladimir Oltean 
123156051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
123256051948SVladimir Oltean 			continue;
123356051948SVladimir Oltean 
1234b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1235b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1236375e1314SVladimir Oltean 		res.start += felix->switch_base;
1237375e1314SVladimir Oltean 		res.end += felix->switch_base;
123856051948SVladimir Oltean 
1239242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
124056051948SVladimir Oltean 		if (IS_ERR(target)) {
124156051948SVladimir Oltean 			dev_err(ocelot->dev,
124256051948SVladimir Oltean 				"Failed to map device memory space\n");
1243bdeced75SVladimir Oltean 			kfree(port_phy_modes);
124456051948SVladimir Oltean 			return PTR_ERR(target);
124556051948SVladimir Oltean 		}
124656051948SVladimir Oltean 
124756051948SVladimir Oltean 		ocelot->targets[i] = target;
124856051948SVladimir Oltean 	}
124956051948SVladimir Oltean 
125056051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
125156051948SVladimir Oltean 	if (err) {
125256051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1253bdeced75SVladimir Oltean 		kfree(port_phy_modes);
125456051948SVladimir Oltean 		return err;
125556051948SVladimir Oltean 	}
125656051948SVladimir Oltean 
125756051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
125856051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
125991c724cfSVladimir Oltean 		struct regmap *target;
126056051948SVladimir Oltean 
126156051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
126256051948SVladimir Oltean 					   sizeof(struct ocelot_port),
126356051948SVladimir Oltean 					   GFP_KERNEL);
126456051948SVladimir Oltean 		if (!ocelot_port) {
126556051948SVladimir Oltean 			dev_err(ocelot->dev,
126656051948SVladimir Oltean 				"failed to allocate port memory\n");
1267bdeced75SVladimir Oltean 			kfree(port_phy_modes);
126856051948SVladimir Oltean 			return -ENOMEM;
126956051948SVladimir Oltean 		}
127056051948SVladimir Oltean 
1271b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1272b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1273375e1314SVladimir Oltean 		res.start += felix->switch_base;
1274375e1314SVladimir Oltean 		res.end += felix->switch_base;
127556051948SVladimir Oltean 
1276242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
127791c724cfSVladimir Oltean 		if (IS_ERR(target)) {
127856051948SVladimir Oltean 			dev_err(ocelot->dev,
127991c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
128091c724cfSVladimir Oltean 				port);
1281bdeced75SVladimir Oltean 			kfree(port_phy_modes);
128291c724cfSVladimir Oltean 			return PTR_ERR(target);
128356051948SVladimir Oltean 		}
128456051948SVladimir Oltean 
1285bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
128656051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
128791c724cfSVladimir Oltean 		ocelot_port->target = target;
128856051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
128956051948SVladimir Oltean 	}
129056051948SVladimir Oltean 
1291bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1292bdeced75SVladimir Oltean 
1293bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1294bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1295bdeced75SVladimir Oltean 		if (err < 0)
1296bdeced75SVladimir Oltean 			return err;
1297bdeced75SVladimir Oltean 	}
1298bdeced75SVladimir Oltean 
129956051948SVladimir Oltean 	return 0;
130056051948SVladimir Oltean }
130156051948SVladimir Oltean 
13021328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
13031328a883SVladimir Oltean 					   struct sk_buff *skb)
13041328a883SVladimir Oltean {
13051328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
13061328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
13071328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
13081328a883SVladimir Oltean 	unsigned long flags;
13091328a883SVladimir Oltean 
13101328a883SVladimir Oltean 	if (!clone)
13111328a883SVladimir Oltean 		return;
13121328a883SVladimir Oltean 
13131328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
13141328a883SVladimir Oltean 
13151328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
13161328a883SVladimir Oltean 		if (skb != clone)
13171328a883SVladimir Oltean 			continue;
13181328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
13191328a883SVladimir Oltean 		skb_match = skb;
13201328a883SVladimir Oltean 		break;
13211328a883SVladimir Oltean 	}
13221328a883SVladimir Oltean 
13231328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
13241328a883SVladimir Oltean 
13251328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
13261328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
13271328a883SVladimir Oltean }
13281328a883SVladimir Oltean 
132949f885b2SVladimir Oltean #define work_to_xmit_work(w) \
133049f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
133149f885b2SVladimir Oltean 
133249f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
133349f885b2SVladimir Oltean {
133449f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
133549f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
133649f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
133749f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
133849f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
133949f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
134049f885b2SVladimir Oltean 	int retries = 10;
134149f885b2SVladimir Oltean 
134249f885b2SVladimir Oltean 	do {
134349f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
134449f885b2SVladimir Oltean 			break;
134549f885b2SVladimir Oltean 
134649f885b2SVladimir Oltean 		cpu_relax();
134749f885b2SVladimir Oltean 	} while (--retries);
134849f885b2SVladimir Oltean 
134949f885b2SVladimir Oltean 	if (!retries) {
135049f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
135149f885b2SVladimir Oltean 			port);
13521328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
135349f885b2SVladimir Oltean 		kfree_skb(skb);
135449f885b2SVladimir Oltean 		return;
135549f885b2SVladimir Oltean 	}
135649f885b2SVladimir Oltean 
135749f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
135849f885b2SVladimir Oltean 
135949f885b2SVladimir Oltean 	consume_skb(skb);
136049f885b2SVladimir Oltean 	kfree(xmit_work);
136149f885b2SVladimir Oltean }
136249f885b2SVladimir Oltean 
136335d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
136435d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
136549f885b2SVladimir Oltean {
136635d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
136749f885b2SVladimir Oltean 
136835d97680SVladimir Oltean 	switch (proto) {
136935d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
137035d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
137135d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
137249f885b2SVladimir Oltean 		return 0;
137335d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
137435d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
137549f885b2SVladimir Oltean 		return 0;
137635d97680SVladimir Oltean 	default:
137735d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
137849f885b2SVladimir Oltean 	}
137949f885b2SVladimir Oltean }
138049f885b2SVladimir Oltean 
138156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
138256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
138356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
138456051948SVladimir Oltean  * (which will not work).
138556051948SVladimir Oltean  */
138656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
138756051948SVladimir Oltean {
138856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
138956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1390f2e2662cSVladimir Oltean 	unsigned long cpu_flood;
13912960bb14SVladimir Oltean 	struct dsa_port *dp;
13922960bb14SVladimir Oltean 	int err;
139356051948SVladimir Oltean 
139456051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
139556051948SVladimir Oltean 	if (err)
139656051948SVladimir Oltean 		return err;
139756051948SVladimir Oltean 
1398d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1399d1cc0e93SVladimir Oltean 	if (err)
14006b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1401d1cc0e93SVladimir Oltean 
14022b49d128SYangbo Lu 	if (ocelot->ptp) {
14032ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
14042b49d128SYangbo Lu 		if (err) {
14052b49d128SYangbo Lu 			dev_err(ocelot->dev,
14062b49d128SYangbo Lu 				"Timestamp initialization failed\n");
14072b49d128SYangbo Lu 			ocelot->ptp = 0;
14082b49d128SYangbo Lu 		}
14092b49d128SYangbo Lu 	}
141056051948SVladimir Oltean 
14112960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
14122960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1413bd2b3161SXiaoliang Yang 
1414bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1415bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1416bd2b3161SXiaoliang Yang 		 */
14172960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
141856051948SVladimir Oltean 	}
141956051948SVladimir Oltean 
1420f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1421f59fd9caSVladimir Oltean 	if (err)
14226b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1423f59fd9caSVladimir Oltean 
14242960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1425adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1426adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
14271cf3299bSVladimir Oltean 		 */
1428c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
1429f2e2662cSVladimir Oltean 
1430f2e2662cSVladimir Oltean 		/* Start off with flooding disabled towards the NPI port
1431f2e2662cSVladimir Oltean 		 * (actually CPU port module).
1432f2e2662cSVladimir Oltean 		 */
1433f2e2662cSVladimir Oltean 		cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
1434f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
1435f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
1436f2e2662cSVladimir Oltean 
14378d5f7954SVladimir Oltean 		break;
1438adb3dccfSVladimir Oltean 	}
14391cf3299bSVladimir Oltean 
14400b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1441c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
144254c31984SVladimir Oltean 	ds->fdb_isolation = true;
144354c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1444bdeced75SVladimir Oltean 
144556051948SVladimir Oltean 	return 0;
14466b73b7c9SVladimir Oltean 
14476b73b7c9SVladimir Oltean out_deinit_ports:
14482960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14492960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
14506b73b7c9SVladimir Oltean 
14516b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
14526b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
14536b73b7c9SVladimir Oltean 
14546b73b7c9SVladimir Oltean out_mdiobus_free:
14556b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14566b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14576b73b7c9SVladimir Oltean 
14586b73b7c9SVladimir Oltean 	return err;
145956051948SVladimir Oltean }
146056051948SVladimir Oltean 
146156051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
146256051948SVladimir Oltean {
146356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1464bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14652960bb14SVladimir Oltean 	struct dsa_port *dp;
1466bdeced75SVladimir Oltean 
14672960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14682960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14698d5f7954SVladimir Oltean 		break;
1470adb3dccfSVladimir Oltean 	}
1471adb3dccfSVladimir Oltean 
14722960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14732960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1474d19741b0SVladimir Oltean 
147549f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
147649f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
147749f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
147849f885b2SVladimir Oltean 
1479d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1480d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
148156051948SVladimir Oltean }
148256051948SVladimir Oltean 
1483c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1484c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1485c0bcf537SYangbo Lu {
1486c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1487c0bcf537SYangbo Lu 
1488c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1489c0bcf537SYangbo Lu }
1490c0bcf537SYangbo Lu 
1491c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1492c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1493c0bcf537SYangbo Lu {
1494c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
149599348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
149699348004SVladimir Oltean 	bool using_tag_8021q;
149799348004SVladimir Oltean 	int err;
1498c0bcf537SYangbo Lu 
149999348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
150099348004SVladimir Oltean 	if (err)
150199348004SVladimir Oltean 		return err;
150299348004SVladimir Oltean 
150399348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
150499348004SVladimir Oltean 
150599348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1506c0bcf537SYangbo Lu }
1507c0bcf537SYangbo Lu 
1508d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
15090a6f17c6SVladimir Oltean {
15100a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1511dbd03285SVladimir Oltean 	int err = 0, grp = 0;
15120a6f17c6SVladimir Oltean 
15130a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
15140a6f17c6SVladimir Oltean 		return false;
15150a6f17c6SVladimir Oltean 
15160a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
15170a6f17c6SVladimir Oltean 		return false;
15180a6f17c6SVladimir Oltean 
15190a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
15200a6f17c6SVladimir Oltean 		struct sk_buff *skb;
15210a6f17c6SVladimir Oltean 		unsigned int type;
15220a6f17c6SVladimir Oltean 
15230a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
15240a6f17c6SVladimir Oltean 		if (err)
15250a6f17c6SVladimir Oltean 			goto out;
15260a6f17c6SVladimir Oltean 
15270a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
15280a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
15290a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
15300a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
15310a6f17c6SVladimir Oltean 		 * here and dropping those.
15320a6f17c6SVladimir Oltean 		 */
15330a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
15340a6f17c6SVladimir Oltean 
15350a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
15360a6f17c6SVladimir Oltean 
15370a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
15380a6f17c6SVladimir Oltean 
15390a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
15400a6f17c6SVladimir Oltean 			kfree_skb(skb);
15410a6f17c6SVladimir Oltean 			continue;
15420a6f17c6SVladimir Oltean 		}
15430a6f17c6SVladimir Oltean 
15440a6f17c6SVladimir Oltean 		netif_rx(skb);
15450a6f17c6SVladimir Oltean 	}
15460a6f17c6SVladimir Oltean 
15470a6f17c6SVladimir Oltean out:
15485d3bb7ddSVladimir Oltean 	if (err < 0) {
15495d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
15505d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
15515d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
15520a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
15535d3bb7ddSVladimir Oltean 	}
15540a6f17c6SVladimir Oltean 
15550a6f17c6SVladimir Oltean 	return true;
15560a6f17c6SVladimir Oltean }
15570a6f17c6SVladimir Oltean 
1558c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1559c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1560c0bcf537SYangbo Lu {
156192f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1562c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1563c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1564c0bcf537SYangbo Lu 	struct timespec64 ts;
156592f62485SVladimir Oltean 	u32 tstamp_hi;
156692f62485SVladimir Oltean 	u64 tstamp;
1567c0bcf537SYangbo Lu 
15680a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15690a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15700a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15710a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15720a6f17c6SVladimir Oltean 	 */
1573d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
15740a6f17c6SVladimir Oltean 		kfree_skb(skb);
15750a6f17c6SVladimir Oltean 		return true;
15760a6f17c6SVladimir Oltean 	}
15770a6f17c6SVladimir Oltean 
1578c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1579c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1580c0bcf537SYangbo Lu 
1581c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1582c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1583c0bcf537SYangbo Lu 		tstamp_hi--;
1584c0bcf537SYangbo Lu 
1585c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1586c0bcf537SYangbo Lu 
1587c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1588c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1589c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1590c0bcf537SYangbo Lu 	return false;
1591c0bcf537SYangbo Lu }
1592c0bcf537SYangbo Lu 
15935c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15945c5416f5SYangbo Lu 			   struct sk_buff *skb)
1595c0bcf537SYangbo Lu {
1596c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1597682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1598c0bcf537SYangbo Lu 
1599682eaad9SYangbo Lu 	if (!ocelot->ptp)
16005c5416f5SYangbo Lu 		return;
1601c0bcf537SYangbo Lu 
160252849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
160352849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
160452849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
160552849bcfSVladimir Oltean 				    port);
1606682eaad9SYangbo Lu 		return;
160752849bcfSVladimir Oltean 	}
1608682eaad9SYangbo Lu 
1609682eaad9SYangbo Lu 	if (clone)
1610c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
16115c5416f5SYangbo Lu }
1612c0bcf537SYangbo Lu 
16130b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
16140b912fc9SVladimir Oltean {
16150b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16160b912fc9SVladimir Oltean 
16170b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
16180b912fc9SVladimir Oltean 
16190b912fc9SVladimir Oltean 	return 0;
16200b912fc9SVladimir Oltean }
16210b912fc9SVladimir Oltean 
16220b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
16230b912fc9SVladimir Oltean {
16240b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16250b912fc9SVladimir Oltean 
16260b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
16270b912fc9SVladimir Oltean }
16280b912fc9SVladimir Oltean 
162907d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
163007d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
163107d985eeSVladimir Oltean {
163207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
163399348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
163499348004SVladimir Oltean 	bool using_tag_8021q;
163599348004SVladimir Oltean 	int err;
163607d985eeSVladimir Oltean 
163799348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
163899348004SVladimir Oltean 	if (err)
163999348004SVladimir Oltean 		return err;
164099348004SVladimir Oltean 
164199348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
164299348004SVladimir Oltean 
164399348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
164407d985eeSVladimir Oltean }
164507d985eeSVladimir Oltean 
164607d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
164707d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
164807d985eeSVladimir Oltean {
164907d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
165007d985eeSVladimir Oltean 
165107d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
165207d985eeSVladimir Oltean }
165307d985eeSVladimir Oltean 
165407d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
165507d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
165607d985eeSVladimir Oltean {
165707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
165807d985eeSVladimir Oltean 
165907d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
166007d985eeSVladimir Oltean }
166107d985eeSVladimir Oltean 
1662fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1663fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1664fc411eaaSVladimir Oltean {
1665fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1666fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1667fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16685f035af7SPo Liu 		.burst = policer->burst,
1669fc411eaaSVladimir Oltean 	};
1670fc411eaaSVladimir Oltean 
1671fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1672fc411eaaSVladimir Oltean }
1673fc411eaaSVladimir Oltean 
1674fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1675fc411eaaSVladimir Oltean {
1676fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1677fc411eaaSVladimir Oltean 
1678fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1679fc411eaaSVladimir Oltean }
1680fc411eaaSVladimir Oltean 
16815e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port,
16825e497497SVladimir Oltean 				 struct dsa_mall_mirror_tc_entry *mirror,
16835e497497SVladimir Oltean 				 bool ingress, struct netlink_ext_ack *extack)
16845e497497SVladimir Oltean {
16855e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16865e497497SVladimir Oltean 
16875e497497SVladimir Oltean 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
16885e497497SVladimir Oltean 				      ingress, extack);
16895e497497SVladimir Oltean }
16905e497497SVladimir Oltean 
16915e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port,
16925e497497SVladimir Oltean 				  struct dsa_mall_mirror_tc_entry *mirror)
16935e497497SVladimir Oltean {
16945e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16955e497497SVladimir Oltean 
16965e497497SVladimir Oltean 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
16975e497497SVladimir Oltean }
16985e497497SVladimir Oltean 
1699de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1700de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1701de143c0eSXiaoliang Yang 			       void *type_data)
1702de143c0eSXiaoliang Yang {
1703de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1704de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1705de143c0eSXiaoliang Yang 
1706de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1707de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1708de143c0eSXiaoliang Yang 	else
1709de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1710de143c0eSXiaoliang Yang }
1711de143c0eSXiaoliang Yang 
1712f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1713f59fd9caSVladimir Oltean 			     u16 pool_index,
1714f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1715f59fd9caSVladimir Oltean {
1716f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1717f59fd9caSVladimir Oltean 
1718f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1719f59fd9caSVladimir Oltean }
1720f59fd9caSVladimir Oltean 
1721f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1722f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1723f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1724f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1725f59fd9caSVladimir Oltean {
1726f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1727f59fd9caSVladimir Oltean 
1728f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1729f59fd9caSVladimir Oltean 				  threshold_type, extack);
1730f59fd9caSVladimir Oltean }
1731f59fd9caSVladimir Oltean 
1732f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1733f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1734f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1735f59fd9caSVladimir Oltean {
1736f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1737f59fd9caSVladimir Oltean 
1738f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1739f59fd9caSVladimir Oltean 				       p_threshold);
1740f59fd9caSVladimir Oltean }
1741f59fd9caSVladimir Oltean 
1742f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1743f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1744f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1745f59fd9caSVladimir Oltean {
1746f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1747f59fd9caSVladimir Oltean 
1748f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1749f59fd9caSVladimir Oltean 				       threshold, extack);
1750f59fd9caSVladimir Oltean }
1751f59fd9caSVladimir Oltean 
1752f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1753f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1754f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1755f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1756f59fd9caSVladimir Oltean {
1757f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1758f59fd9caSVladimir Oltean 
1759f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1760f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1761f59fd9caSVladimir Oltean 					  p_threshold);
1762f59fd9caSVladimir Oltean }
1763f59fd9caSVladimir Oltean 
1764f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1765f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1766f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1767f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1768f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1769f59fd9caSVladimir Oltean {
1770f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1771f59fd9caSVladimir Oltean 
1772f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1773f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1774f59fd9caSVladimir Oltean 					  extack);
1775f59fd9caSVladimir Oltean }
1776f59fd9caSVladimir Oltean 
1777f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1778f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1779f59fd9caSVladimir Oltean {
1780f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1781f59fd9caSVladimir Oltean 
1782f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1783f59fd9caSVladimir Oltean }
1784f59fd9caSVladimir Oltean 
1785f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1786f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1787f59fd9caSVladimir Oltean {
1788f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1789f59fd9caSVladimir Oltean 
1790f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1791f59fd9caSVladimir Oltean }
1792f59fd9caSVladimir Oltean 
1793f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1794f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1795f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1796f59fd9caSVladimir Oltean {
1797f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1798f59fd9caSVladimir Oltean 
1799f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1800f59fd9caSVladimir Oltean 					   p_cur, p_max);
1801f59fd9caSVladimir Oltean }
1802f59fd9caSVladimir Oltean 
1803f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1804f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1805f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1806f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1807f59fd9caSVladimir Oltean {
1808f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1809f59fd9caSVladimir Oltean 
1810f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1811f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1812f59fd9caSVladimir Oltean }
1813f59fd9caSVladimir Oltean 
1814a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1815a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1816a026c50bSHoratiu Vultur {
1817a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1818a026c50bSHoratiu Vultur 
1819a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1820a026c50bSHoratiu Vultur }
1821a026c50bSHoratiu Vultur 
1822a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1823a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1824a026c50bSHoratiu Vultur {
1825a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1826a026c50bSHoratiu Vultur 
1827a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1828a026c50bSHoratiu Vultur }
1829a026c50bSHoratiu Vultur 
1830a026c50bSHoratiu Vultur static int
1831a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1832a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1833a026c50bSHoratiu Vultur {
1834a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1835a026c50bSHoratiu Vultur 
1836a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1837a026c50bSHoratiu Vultur }
1838a026c50bSHoratiu Vultur 
1839a026c50bSHoratiu Vultur static int
1840a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1841a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1842a026c50bSHoratiu Vultur {
1843a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1844a026c50bSHoratiu Vultur 
1845a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1846a026c50bSHoratiu Vultur }
1847a026c50bSHoratiu Vultur 
1848978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
1849978777d0SVladimir Oltean {
1850978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1851978777d0SVladimir Oltean 
1852978777d0SVladimir Oltean 	return ocelot_port_get_default_prio(ocelot, port);
1853978777d0SVladimir Oltean }
1854978777d0SVladimir Oltean 
1855978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
1856978777d0SVladimir Oltean 				       u8 prio)
1857978777d0SVladimir Oltean {
1858978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1859978777d0SVladimir Oltean 
1860978777d0SVladimir Oltean 	return ocelot_port_set_default_prio(ocelot, port, prio);
1861978777d0SVladimir Oltean }
1862978777d0SVladimir Oltean 
1863978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
1864978777d0SVladimir Oltean {
1865978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1866978777d0SVladimir Oltean 
1867978777d0SVladimir Oltean 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
1868978777d0SVladimir Oltean }
1869978777d0SVladimir Oltean 
1870978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1871978777d0SVladimir Oltean 				    u8 prio)
1872978777d0SVladimir Oltean {
1873978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1874978777d0SVladimir Oltean 
1875978777d0SVladimir Oltean 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
1876978777d0SVladimir Oltean }
1877978777d0SVladimir Oltean 
1878978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1879978777d0SVladimir Oltean 				    u8 prio)
1880978777d0SVladimir Oltean {
1881978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1882978777d0SVladimir Oltean 
1883978777d0SVladimir Oltean 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
1884978777d0SVladimir Oltean }
1885978777d0SVladimir Oltean 
1886375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
188756051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1888adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
188935d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
189056051948SVladimir Oltean 	.setup				= felix_setup,
189156051948SVladimir Oltean 	.teardown			= felix_teardown,
189256051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
189356051948SVladimir Oltean 	.get_strings			= felix_get_strings,
189456051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
189556051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
189656051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
189779fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1898bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1899864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1900bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1901bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
19025cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
190356051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
190456051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
190556051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1906961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1907961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1908209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1909209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1910421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1911421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
191256051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
191356051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
19148fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
19158fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
19168fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
191756051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
191856051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
191956051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
192056051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1921c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1922c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1923c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1924c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
19250b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
19260b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1927fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1928fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
19295e497497SVladimir Oltean 	.port_mirror_add		= felix_port_mirror_add,
19305e497497SVladimir Oltean 	.port_mirror_del		= felix_port_mirror_del,
193107d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
193207d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
193307d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1934de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1935f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1936f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1937f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1938f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1939f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1940f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1941f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1942f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1943f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1944f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1945a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1946a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1947a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1948a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
19495da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
19505da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
1951978777d0SVladimir Oltean 	.port_get_default_prio		= felix_port_get_default_prio,
1952978777d0SVladimir Oltean 	.port_set_default_prio		= felix_port_set_default_prio,
1953978777d0SVladimir Oltean 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
1954978777d0SVladimir Oltean 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
1955978777d0SVladimir Oltean 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
195656051948SVladimir Oltean };
1957319e4dd1SVladimir Oltean 
1958319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1959319e4dd1SVladimir Oltean {
1960319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1961319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1962319e4dd1SVladimir Oltean 
1963319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1964319e4dd1SVladimir Oltean 		return NULL;
1965319e4dd1SVladimir Oltean 
1966319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1967319e4dd1SVladimir Oltean }
1968319e4dd1SVladimir Oltean 
1969319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1970319e4dd1SVladimir Oltean {
1971319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1972319e4dd1SVladimir Oltean 
1973319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1974319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1975319e4dd1SVladimir Oltean 		return -EINVAL;
1976319e4dd1SVladimir Oltean 
1977319e4dd1SVladimir Oltean 	return dp->index;
1978319e4dd1SVladimir Oltean }
1979