xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 00fa91bc9cc2a9d340f963af5e457610ad4b2f9c)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
23c9cfb52SVladimir Oltean /* Copyright 2019-2021 NXP
3375e1314SVladimir Oltean  *
4375e1314SVladimir Oltean  * This is an umbrella module for all network switches that are
5375e1314SVladimir Oltean  * register-compatible with Ocelot and that perform I/O to their host CPU
6375e1314SVladimir Oltean  * through an NPI (Node Processor Interface) Ethernet port.
756051948SVladimir Oltean  */
856051948SVladimir Oltean #include <uapi/linux/if_bridge.h>
907d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h>
11bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h>
12bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h>
13bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h>
142b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h>
1556051948SVladimir Oltean #include <soc/mscc/ocelot.h>
16e21268efSVladimir Oltean #include <linux/dsa/8021q.h>
1740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
1884705fc1SMaxim Kochetkov #include <linux/platform_device.h>
190a6f17c6SVladimir Oltean #include <linux/ptp_classify.h>
2056051948SVladimir Oltean #include <linux/module.h>
21bdeced75SVladimir Oltean #include <linux/of_net.h>
2256051948SVladimir Oltean #include <linux/pci.h>
2356051948SVladimir Oltean #include <linux/of.h>
24fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
2556051948SVladimir Oltean #include <net/dsa.h>
2656051948SVladimir Oltean #include "felix.h"
2756051948SVladimir Oltean 
28f9cef64fSVladimir Oltean /* Translate the DSA database API into the ocelot switch library API,
29f9cef64fSVladimir Oltean  * which uses VID 0 for all ports that aren't part of a bridge,
30f9cef64fSVladimir Oltean  * and expects the bridge_dev to be NULL in that case.
31f9cef64fSVladimir Oltean  */
32f9cef64fSVladimir Oltean static struct net_device *felix_classify_db(struct dsa_db db)
33f9cef64fSVladimir Oltean {
34f9cef64fSVladimir Oltean 	switch (db.type) {
35f9cef64fSVladimir Oltean 	case DSA_DB_PORT:
36f9cef64fSVladimir Oltean 	case DSA_DB_LAG:
37f9cef64fSVladimir Oltean 		return NULL;
38f9cef64fSVladimir Oltean 	case DSA_DB_BRIDGE:
39f9cef64fSVladimir Oltean 		return db.bridge.dev;
40f9cef64fSVladimir Oltean 	default:
41f9cef64fSVladimir Oltean 		return ERR_PTR(-EOPNOTSUPP);
42f9cef64fSVladimir Oltean 	}
43f9cef64fSVladimir Oltean }
44f9cef64fSVladimir Oltean 
45f9cef64fSVladimir Oltean /* We are called before felix_npi_port_init(), so ocelot->npi is -1. */
46f9cef64fSVladimir Oltean static int felix_migrate_fdbs_to_npi_port(struct dsa_switch *ds, int port,
47f9cef64fSVladimir Oltean 					  const unsigned char *addr, u16 vid,
48f9cef64fSVladimir Oltean 					  struct dsa_db db)
49f9cef64fSVladimir Oltean {
50f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
51f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
52f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
53f9cef64fSVladimir Oltean 	int err;
54f9cef64fSVladimir Oltean 
55f9cef64fSVladimir Oltean 	err = ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
56f9cef64fSVladimir Oltean 	if (err)
57f9cef64fSVladimir Oltean 		return err;
58f9cef64fSVladimir Oltean 
59f9cef64fSVladimir Oltean 	return ocelot_fdb_add(ocelot, cpu, addr, vid, bridge_dev);
60f9cef64fSVladimir Oltean }
61f9cef64fSVladimir Oltean 
62f9cef64fSVladimir Oltean static int felix_migrate_mdbs_to_npi_port(struct dsa_switch *ds, int port,
63f9cef64fSVladimir Oltean 					  const unsigned char *addr, u16 vid,
64f9cef64fSVladimir Oltean 					  struct dsa_db db)
65f9cef64fSVladimir Oltean {
66f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
67f9cef64fSVladimir Oltean 	struct switchdev_obj_port_mdb mdb;
68f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
70f9cef64fSVladimir Oltean 	int err;
71f9cef64fSVladimir Oltean 
72f9cef64fSVladimir Oltean 	memset(&mdb, 0, sizeof(mdb));
73f9cef64fSVladimir Oltean 	ether_addr_copy(mdb.addr, addr);
74f9cef64fSVladimir Oltean 	mdb.vid = vid;
75f9cef64fSVladimir Oltean 
76f9cef64fSVladimir Oltean 	err = ocelot_port_mdb_del(ocelot, port, &mdb, bridge_dev);
77f9cef64fSVladimir Oltean 	if (err)
78f9cef64fSVladimir Oltean 		return err;
79f9cef64fSVladimir Oltean 
80f9cef64fSVladimir Oltean 	return ocelot_port_mdb_add(ocelot, cpu, &mdb, bridge_dev);
81f9cef64fSVladimir Oltean }
82f9cef64fSVladimir Oltean 
83b903a6bdSVladimir Oltean static void felix_migrate_pgid_bit(struct dsa_switch *ds, int from, int to,
84b903a6bdSVladimir Oltean 				   int pgid)
85b903a6bdSVladimir Oltean {
86b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
87b903a6bdSVladimir Oltean 	bool on;
88b903a6bdSVladimir Oltean 	u32 val;
89b903a6bdSVladimir Oltean 
90b903a6bdSVladimir Oltean 	val = ocelot_read_rix(ocelot, ANA_PGID_PGID, pgid);
91b903a6bdSVladimir Oltean 	on = !!(val & BIT(from));
92b903a6bdSVladimir Oltean 	val &= ~BIT(from);
93b903a6bdSVladimir Oltean 	if (on)
94b903a6bdSVladimir Oltean 		val |= BIT(to);
95b903a6bdSVladimir Oltean 	else
96b903a6bdSVladimir Oltean 		val &= ~BIT(to);
97b903a6bdSVladimir Oltean 
98b903a6bdSVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_PGID_PGID, pgid);
99b903a6bdSVladimir Oltean }
100b903a6bdSVladimir Oltean 
101b903a6bdSVladimir Oltean static void felix_migrate_flood_to_npi_port(struct dsa_switch *ds, int port)
102b903a6bdSVladimir Oltean {
103b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104b903a6bdSVladimir Oltean 
105b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_UC);
106b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_MC);
107b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_BC);
108b903a6bdSVladimir Oltean }
109b903a6bdSVladimir Oltean 
110b903a6bdSVladimir Oltean static void
111b903a6bdSVladimir Oltean felix_migrate_flood_to_tag_8021q_port(struct dsa_switch *ds, int port)
112b903a6bdSVladimir Oltean {
113b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
114b903a6bdSVladimir Oltean 
115b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_UC);
116b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_MC);
117b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_BC);
118b903a6bdSVladimir Oltean }
119b903a6bdSVladimir Oltean 
120f9cef64fSVladimir Oltean /* ocelot->npi was already set to -1 by felix_npi_port_deinit, so
121f9cef64fSVladimir Oltean  * ocelot_fdb_add() will not redirect FDB entries towards the
122f9cef64fSVladimir Oltean  * CPU port module here, which is what we want.
123f9cef64fSVladimir Oltean  */
124f9cef64fSVladimir Oltean static int
125f9cef64fSVladimir Oltean felix_migrate_fdbs_to_tag_8021q_port(struct dsa_switch *ds, int port,
126f9cef64fSVladimir Oltean 				     const unsigned char *addr, u16 vid,
127f9cef64fSVladimir Oltean 				     struct dsa_db db)
128f9cef64fSVladimir Oltean {
129f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
130f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
131f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
132f9cef64fSVladimir Oltean 	int err;
133f9cef64fSVladimir Oltean 
134f9cef64fSVladimir Oltean 	err = ocelot_fdb_del(ocelot, cpu, addr, vid, bridge_dev);
135f9cef64fSVladimir Oltean 	if (err)
136f9cef64fSVladimir Oltean 		return err;
137f9cef64fSVladimir Oltean 
138f9cef64fSVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
139f9cef64fSVladimir Oltean }
140f9cef64fSVladimir Oltean 
141f9cef64fSVladimir Oltean static int
142f9cef64fSVladimir Oltean felix_migrate_mdbs_to_tag_8021q_port(struct dsa_switch *ds, int port,
143f9cef64fSVladimir Oltean 				     const unsigned char *addr, u16 vid,
144f9cef64fSVladimir Oltean 				     struct dsa_db db)
145f9cef64fSVladimir Oltean {
146f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
147f9cef64fSVladimir Oltean 	struct switchdev_obj_port_mdb mdb;
148f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
149f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
150f9cef64fSVladimir Oltean 	int err;
151f9cef64fSVladimir Oltean 
152f9cef64fSVladimir Oltean 	memset(&mdb, 0, sizeof(mdb));
153f9cef64fSVladimir Oltean 	ether_addr_copy(mdb.addr, addr);
154f9cef64fSVladimir Oltean 	mdb.vid = vid;
155f9cef64fSVladimir Oltean 
156f9cef64fSVladimir Oltean 	err = ocelot_port_mdb_del(ocelot, cpu, &mdb, bridge_dev);
157f9cef64fSVladimir Oltean 	if (err)
158f9cef64fSVladimir Oltean 		return err;
159f9cef64fSVladimir Oltean 
160f9cef64fSVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, &mdb, bridge_dev);
161f9cef64fSVladimir Oltean }
162f9cef64fSVladimir Oltean 
16304b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
16404b67e18SVladimir Oltean  * the tagger can perform RX source port identification.
16504b67e18SVladimir Oltean  */
16604b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid)
167e21268efSVladimir Oltean {
168e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
169e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
170e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
171e21268efSVladimir Oltean 	int key_length, upstream, err;
172e21268efSVladimir Oltean 
173e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
174e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
175e21268efSVladimir Oltean 
176e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
177e21268efSVladimir Oltean 				     GFP_KERNEL);
178e21268efSVladimir Oltean 	if (!outer_tagging_rule)
179e21268efSVladimir Oltean 		return -ENOMEM;
180e21268efSVladimir Oltean 
181e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
182e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
183c518afecSVladimir Oltean 	outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
184e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
185e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
186e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
187e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
188e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
189e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
190e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
191e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
192e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
193e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
194e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
195e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
196e21268efSVladimir Oltean 
197e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
198e21268efSVladimir Oltean 	if (err)
199e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
200e21268efSVladimir Oltean 
201e21268efSVladimir Oltean 	return err;
202e21268efSVladimir Oltean }
203e21268efSVladimir Oltean 
20404b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid)
20504b67e18SVladimir Oltean {
20604b67e18SVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
20704b67e18SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
20804b67e18SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
20904b67e18SVladimir Oltean 
21004b67e18SVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
21104b67e18SVladimir Oltean 
21204b67e18SVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
21304b67e18SVladimir Oltean 								 port, false);
21404b67e18SVladimir Oltean 	if (!outer_tagging_rule)
21504b67e18SVladimir Oltean 		return -ENOENT;
21604b67e18SVladimir Oltean 
21704b67e18SVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
21804b67e18SVladimir Oltean }
21904b67e18SVladimir Oltean 
22004b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
22104b67e18SVladimir Oltean  * rules for steering those tagged packets towards the correct destination port
22204b67e18SVladimir Oltean  */
22304b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid)
224e21268efSVladimir Oltean {
225e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
226e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
227e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
228e21268efSVladimir Oltean 	int upstream, err;
229e21268efSVladimir Oltean 
230e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
231e21268efSVladimir Oltean 	if (!untagging_rule)
232e21268efSVladimir Oltean 		return -ENOMEM;
233e21268efSVladimir Oltean 
234e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
235e21268efSVladimir Oltean 	if (!redirect_rule) {
236e21268efSVladimir Oltean 		kfree(untagging_rule);
237e21268efSVladimir Oltean 		return -ENOMEM;
238e21268efSVladimir Oltean 	}
239e21268efSVladimir Oltean 
240e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
241e21268efSVladimir Oltean 
242e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
243e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
244e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
245e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
246e21268efSVladimir Oltean 	untagging_rule->prio = 1;
247c518afecSVladimir Oltean 	untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
248e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
249e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
250e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
251e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
252e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
253e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
254e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
255e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
256e21268efSVladimir Oltean 
257e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
258e21268efSVladimir Oltean 	if (err) {
259e21268efSVladimir Oltean 		kfree(untagging_rule);
260e21268efSVladimir Oltean 		kfree(redirect_rule);
261e21268efSVladimir Oltean 		return err;
262e21268efSVladimir Oltean 	}
263e21268efSVladimir Oltean 
264e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
265e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
266e21268efSVladimir Oltean 	redirect_rule->pag = port;
267e21268efSVladimir Oltean 	redirect_rule->prio = 1;
268c518afecSVladimir Oltean 	redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
269e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
270e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
271e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
272e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
273e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
274e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
275e21268efSVladimir Oltean 
276e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
277e21268efSVladimir Oltean 	if (err) {
278e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
279e21268efSVladimir Oltean 		kfree(redirect_rule);
280e21268efSVladimir Oltean 		return err;
281e21268efSVladimir Oltean 	}
282e21268efSVladimir Oltean 
283e21268efSVladimir Oltean 	return 0;
284e21268efSVladimir Oltean }
285e21268efSVladimir Oltean 
28604b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid)
287e21268efSVladimir Oltean {
288e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
289e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
290e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
291e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
292e21268efSVladimir Oltean 	int err;
293e21268efSVladimir Oltean 
294e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
295e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
296e21268efSVladimir Oltean 
297e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
298e21268efSVladimir Oltean 							     port, false);
299e21268efSVladimir Oltean 	if (!untagging_rule)
30008f44db3SVladimir Oltean 		return -ENOENT;
301e21268efSVladimir Oltean 
302e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
303e21268efSVladimir Oltean 	if (err)
304e21268efSVladimir Oltean 		return err;
305e21268efSVladimir Oltean 
306e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
307e21268efSVladimir Oltean 							    port, false);
308e21268efSVladimir Oltean 	if (!redirect_rule)
30904b67e18SVladimir Oltean 		return -ENOENT;
310e21268efSVladimir Oltean 
311e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
312e21268efSVladimir Oltean }
313e21268efSVladimir Oltean 
31404b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
31504b67e18SVladimir Oltean 				    u16 flags)
31604b67e18SVladimir Oltean {
31704b67e18SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
31804b67e18SVladimir Oltean 	int err;
31904b67e18SVladimir Oltean 
32004b67e18SVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
32104b67e18SVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
32204b67e18SVladimir Oltean 	 * for the CPU port.
32304b67e18SVladimir Oltean 	 */
32404b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
32504b67e18SVladimir Oltean 		return 0;
32604b67e18SVladimir Oltean 
32704b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
32804b67e18SVladimir Oltean 	if (err)
32904b67e18SVladimir Oltean 		return err;
33004b67e18SVladimir Oltean 
33104b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid);
33204b67e18SVladimir Oltean 	if (err) {
33304b67e18SVladimir Oltean 		felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
33404b67e18SVladimir Oltean 		return err;
33504b67e18SVladimir Oltean 	}
33604b67e18SVladimir Oltean 
33704b67e18SVladimir Oltean 	return 0;
33804b67e18SVladimir Oltean }
33904b67e18SVladimir Oltean 
340e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
341e21268efSVladimir Oltean {
342e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
34304b67e18SVladimir Oltean 	int err;
344e21268efSVladimir Oltean 
34504b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
34604b67e18SVladimir Oltean 		return 0;
347e21268efSVladimir Oltean 
34804b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
34904b67e18SVladimir Oltean 	if (err)
35004b67e18SVladimir Oltean 		return err;
35104b67e18SVladimir Oltean 
35204b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid);
35304b67e18SVladimir Oltean 	if (err) {
35404b67e18SVladimir Oltean 		felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
35504b67e18SVladimir Oltean 		return err;
35604b67e18SVladimir Oltean 	}
357e21268efSVladimir Oltean 
358e21268efSVladimir Oltean 	return 0;
359e21268efSVladimir Oltean }
360e21268efSVladimir Oltean 
361e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
362e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
363e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
364e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
365e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
366e21268efSVladimir Oltean  * register-based MMIO.
367e21268efSVladimir Oltean  */
368e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
369e21268efSVladimir Oltean {
3708abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
3718abe1970SVladimir Oltean 
37254c31984SVladimir Oltean 	ocelot_port_set_dsa_8021q_cpu(ocelot, port);
373e21268efSVladimir Oltean 
374e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
375e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
376e21268efSVladimir Oltean 
3778abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3788abe1970SVladimir Oltean 
3798abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
380e21268efSVladimir Oltean }
381e21268efSVladimir Oltean 
382e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
383e21268efSVladimir Oltean {
3848abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
3858abe1970SVladimir Oltean 
38654c31984SVladimir Oltean 	ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
387e21268efSVladimir Oltean 
388e21268efSVladimir Oltean 	/* Restore PGID_CPU */
389e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
390e21268efSVladimir Oltean 			 PGID_CPU);
391e21268efSVladimir Oltean 
3928abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3938abe1970SVladimir Oltean 
3948abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
395e21268efSVladimir Oltean }
396e21268efSVladimir Oltean 
39799348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
39899348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
39999348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
400c8c0ba4fSVladimir Oltean  */
40199348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
40299348004SVladimir Oltean 					      bool using_tag_8021q)
403c8c0ba4fSVladimir Oltean {
40499348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
40599348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
40699348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
40799348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
40899348004SVladimir Oltean 	unsigned long port_mask;
4092960bb14SVladimir Oltean 	struct dsa_port *dp;
41099348004SVladimir Oltean 	bool cpu_copy_ena;
41199348004SVladimir Oltean 	int cpu = -1, err;
412c8c0ba4fSVladimir Oltean 
41399348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
41499348004SVladimir Oltean 		return 0;
415c8c0ba4fSVladimir Oltean 
41699348004SVladimir Oltean 	/* Figure out the current CPU port */
4172960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
4182960bb14SVladimir Oltean 		cpu = dp->index;
4198d5f7954SVladimir Oltean 		break;
420c8c0ba4fSVladimir Oltean 	}
4218d5f7954SVladimir Oltean 
422d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
423d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
424d78637a8SVladimir Oltean 	 */
425c8c0ba4fSVladimir Oltean 
42699348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
42799348004SVladimir Oltean 	list_for_each_entry(trap, &ocelot->traps, trap_list) {
42899348004SVladimir Oltean 		/* Figure out the current trapping destination */
42999348004SVladimir Oltean 		if (using_tag_8021q) {
43099348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
43199348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
43299348004SVladimir Oltean 			 * port module.
433c8c0ba4fSVladimir Oltean 			 */
43499348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
43599348004SVladimir Oltean 			port_mask = BIT(cpu);
43699348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
437c8c0ba4fSVladimir Oltean 		} else {
43899348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
43999348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
440c8c0ba4fSVladimir Oltean 			 */
44199348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
44299348004SVladimir Oltean 			port_mask = 0;
44399348004SVladimir Oltean 			cpu_copy_ena = true;
444c8c0ba4fSVladimir Oltean 		}
445c8c0ba4fSVladimir Oltean 
44699348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
44799348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
44899348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
44999348004SVladimir Oltean 			continue;
450c8c0ba4fSVladimir Oltean 
45199348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
45299348004SVladimir Oltean 		trap->action.port_mask = port_mask;
45399348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
4540a6f17c6SVladimir Oltean 
45599348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
456c8c0ba4fSVladimir Oltean 		if (err)
457c8c0ba4fSVladimir Oltean 			return err;
45899348004SVladimir Oltean 	}
459c8c0ba4fSVladimir Oltean 
46099348004SVladimir Oltean 	return 0;
461c8c0ba4fSVladimir Oltean }
462c8c0ba4fSVladimir Oltean 
463c69f40acSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu)
464e21268efSVladimir Oltean {
465e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4662960bb14SVladimir Oltean 	struct dsa_port *dp;
4672960bb14SVladimir Oltean 	int err;
468e21268efSVladimir Oltean 
469e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
470e21268efSVladimir Oltean 
4712960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
472e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
473e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
474e21268efSVladimir Oltean 		 * for 2 reasons:
475e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
476e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
477e21268efSVladimir Oltean 		 *   into the system.
478e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
479e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
480e21268efSVladimir Oltean 		 *   port module.
481e21268efSVladimir Oltean 		 */
482e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
483e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4842960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
485e21268efSVladimir Oltean 	}
486e21268efSVladimir Oltean 
4875da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
488e21268efSVladimir Oltean 	if (err)
489d7b1fd52SVladimir Oltean 		return err;
490d7b1fd52SVladimir Oltean 
491c69f40acSVladimir Oltean 	err = dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_tag_8021q_port);
492d7b1fd52SVladimir Oltean 	if (err)
493d7b1fd52SVladimir Oltean 		goto out_tag_8021q_unregister;
494e21268efSVladimir Oltean 
495c69f40acSVladimir Oltean 	err = dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_tag_8021q_port);
496f9cef64fSVladimir Oltean 	if (err)
497f9cef64fSVladimir Oltean 		goto out_migrate_fdbs;
498b903a6bdSVladimir Oltean 
499b903a6bdSVladimir Oltean 	felix_migrate_flood_to_tag_8021q_port(ds, cpu);
500f9cef64fSVladimir Oltean 
501f9cef64fSVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
502f9cef64fSVladimir Oltean 	if (err)
503b903a6bdSVladimir Oltean 		goto out_migrate_flood;
504f9cef64fSVladimir Oltean 
50599348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
50699348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
50799348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
50899348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
50999348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
51099348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
51199348004SVladimir Oltean 	 */
51299348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
51399348004SVladimir Oltean 
514e21268efSVladimir Oltean 	return 0;
515e21268efSVladimir Oltean 
516b903a6bdSVladimir Oltean out_migrate_flood:
517b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
518f9cef64fSVladimir Oltean 	dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
519f9cef64fSVladimir Oltean out_migrate_fdbs:
520f9cef64fSVladimir Oltean 	dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
521d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
522d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
523e21268efSVladimir Oltean 	return err;
524e21268efSVladimir Oltean }
525e21268efSVladimir Oltean 
526e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
527e21268efSVladimir Oltean {
528e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5292960bb14SVladimir Oltean 	struct dsa_port *dp;
5302960bb14SVladimir Oltean 	int err;
531e21268efSVladimir Oltean 
53299348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
533c8c0ba4fSVladimir Oltean 	if (err)
534c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
535c8c0ba4fSVladimir Oltean 			err);
536c8c0ba4fSVladimir Oltean 
537d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
538e21268efSVladimir Oltean 
5392960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
540e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
541e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
542e21268efSVladimir Oltean 		 */
543e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
544e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
545e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
5462960bb14SVladimir Oltean 				 dp->index);
547e21268efSVladimir Oltean 	}
548e21268efSVladimir Oltean 
549e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
550e21268efSVladimir Oltean }
551e21268efSVladimir Oltean 
552adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
553adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
554adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
555adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
556adb3dccfSVladimir Oltean  * DSA master.
557adb3dccfSVladimir Oltean  */
558adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
559adb3dccfSVladimir Oltean {
560adb3dccfSVladimir Oltean 	ocelot->npi = port;
561adb3dccfSVladimir Oltean 
562adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
563adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
564adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
565adb3dccfSVladimir Oltean 
566adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
567adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
568adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
569adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
570adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
571adb3dccfSVladimir Oltean 
572adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
573adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
574adb3dccfSVladimir Oltean }
575adb3dccfSVladimir Oltean 
576adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
577adb3dccfSVladimir Oltean {
578adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
579adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
580adb3dccfSVladimir Oltean 
581adb3dccfSVladimir Oltean 	ocelot->npi = -1;
582adb3dccfSVladimir Oltean 
583adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
584adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
585adb3dccfSVladimir Oltean 
586adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
587adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
588adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
589adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
590adb3dccfSVladimir Oltean 
591adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
592adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
593adb3dccfSVladimir Oltean }
594adb3dccfSVladimir Oltean 
595c69f40acSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu)
596adb3dccfSVladimir Oltean {
597adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
598f9cef64fSVladimir Oltean 	int err;
599f9cef64fSVladimir Oltean 
600c69f40acSVladimir Oltean 	err = dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
601f9cef64fSVladimir Oltean 	if (err)
602f9cef64fSVladimir Oltean 		return err;
603f9cef64fSVladimir Oltean 
604c69f40acSVladimir Oltean 	err = dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
605f9cef64fSVladimir Oltean 	if (err)
606f9cef64fSVladimir Oltean 		goto out_migrate_fdbs;
607b903a6bdSVladimir Oltean 
608b903a6bdSVladimir Oltean 	felix_migrate_flood_to_npi_port(ds, cpu);
609adb3dccfSVladimir Oltean 
610adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
611adb3dccfSVladimir Oltean 
612adb3dccfSVladimir Oltean 	return 0;
613f9cef64fSVladimir Oltean 
614f9cef64fSVladimir Oltean out_migrate_fdbs:
615c69f40acSVladimir Oltean 	dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_tag_8021q_port);
616f9cef64fSVladimir Oltean 
617f9cef64fSVladimir Oltean 	return err;
618adb3dccfSVladimir Oltean }
619adb3dccfSVladimir Oltean 
620adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
621adb3dccfSVladimir Oltean {
622adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
623adb3dccfSVladimir Oltean 
624adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
625adb3dccfSVladimir Oltean }
626adb3dccfSVladimir Oltean 
627adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
628c69f40acSVladimir Oltean 				  enum dsa_tag_protocol proto)
629adb3dccfSVladimir Oltean {
630adb3dccfSVladimir Oltean 	int err;
631adb3dccfSVladimir Oltean 
632adb3dccfSVladimir Oltean 	switch (proto) {
6337c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
634adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
635c69f40acSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
636adb3dccfSVladimir Oltean 		break;
637e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
638c69f40acSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
639e21268efSVladimir Oltean 		break;
640adb3dccfSVladimir Oltean 	default:
641adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
642adb3dccfSVladimir Oltean 	}
643adb3dccfSVladimir Oltean 
644adb3dccfSVladimir Oltean 	return err;
645adb3dccfSVladimir Oltean }
646adb3dccfSVladimir Oltean 
647adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
648adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
649adb3dccfSVladimir Oltean {
650adb3dccfSVladimir Oltean 	switch (proto) {
6517c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
652adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
653adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
654adb3dccfSVladimir Oltean 		break;
655e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
656e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
657e21268efSVladimir Oltean 		break;
658adb3dccfSVladimir Oltean 	default:
659adb3dccfSVladimir Oltean 		break;
660adb3dccfSVladimir Oltean 	}
661adb3dccfSVladimir Oltean }
662adb3dccfSVladimir Oltean 
663e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
664e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
665e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
666e21268efSVladimir Oltean  */
667adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
668adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
669adb3dccfSVladimir Oltean {
670adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
671adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
672adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
673*00fa91bcSVladimir Oltean 	bool cpu_port_active = false;
674*00fa91bcSVladimir Oltean 	struct dsa_port *dp;
675adb3dccfSVladimir Oltean 	int err;
676adb3dccfSVladimir Oltean 
6777c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
6787c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
679e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
680adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
681adb3dccfSVladimir Oltean 
682*00fa91bcSVladimir Oltean 	/* We don't support multiple CPU ports, yet the DT blob may have
683*00fa91bcSVladimir Oltean 	 * multiple CPU ports defined. The first CPU port is the active one,
684*00fa91bcSVladimir Oltean 	 * the others are inactive. In this case, DSA will call
685*00fa91bcSVladimir Oltean 	 * ->change_tag_protocol() multiple times, once per CPU port.
686*00fa91bcSVladimir Oltean 	 * Since we implement the tagging protocol change towards "ocelot" or
687*00fa91bcSVladimir Oltean 	 * "seville" as effectively initializing the NPI port, what we are
688*00fa91bcSVladimir Oltean 	 * doing is effectively changing who the NPI port is to the last @cpu
689*00fa91bcSVladimir Oltean 	 * argument passed, which is an unused DSA CPU port and not the one
690*00fa91bcSVladimir Oltean 	 * that should actively pass traffic.
691*00fa91bcSVladimir Oltean 	 * Suppress DSA's calls on CPU ports that are inactive.
692*00fa91bcSVladimir Oltean 	 */
693*00fa91bcSVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds) {
694*00fa91bcSVladimir Oltean 		if (dp->cpu_dp->index == cpu) {
695*00fa91bcSVladimir Oltean 			cpu_port_active = true;
696*00fa91bcSVladimir Oltean 			break;
697*00fa91bcSVladimir Oltean 		}
698*00fa91bcSVladimir Oltean 	}
699*00fa91bcSVladimir Oltean 
700*00fa91bcSVladimir Oltean 	if (!cpu_port_active)
701*00fa91bcSVladimir Oltean 		return 0;
702*00fa91bcSVladimir Oltean 
703adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
704adb3dccfSVladimir Oltean 
705c69f40acSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
706adb3dccfSVladimir Oltean 	if (err) {
707c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
708adb3dccfSVladimir Oltean 		return err;
709adb3dccfSVladimir Oltean 	}
710adb3dccfSVladimir Oltean 
711adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
712adb3dccfSVladimir Oltean 
713adb3dccfSVladimir Oltean 	return 0;
714adb3dccfSVladimir Oltean }
715adb3dccfSVladimir Oltean 
71656051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
7174d776482SFlorian Fainelli 						    int port,
7184d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
71956051948SVladimir Oltean {
720adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
721adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
722adb3dccfSVladimir Oltean 
723adb3dccfSVladimir Oltean 	return felix->tag_proto;
72456051948SVladimir Oltean }
72556051948SVladimir Oltean 
72656051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
72756051948SVladimir Oltean 				 unsigned int ageing_time)
72856051948SVladimir Oltean {
72956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73056051948SVladimir Oltean 
73156051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
73256051948SVladimir Oltean 
73356051948SVladimir Oltean 	return 0;
73456051948SVladimir Oltean }
73556051948SVladimir Oltean 
7365cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
7375cad43a5SVladimir Oltean {
7385cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7395cad43a5SVladimir Oltean 	int err;
7405cad43a5SVladimir Oltean 
7415cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
7425cad43a5SVladimir Oltean 	if (err)
7435cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
7445cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7455cad43a5SVladimir Oltean }
7465cad43a5SVladimir Oltean 
74756051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
74856051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
74956051948SVladimir Oltean {
75056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
75156051948SVladimir Oltean 
75256051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
75356051948SVladimir Oltean }
75456051948SVladimir Oltean 
75556051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
756c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
757c2693363SVladimir Oltean 			 struct dsa_db db)
75856051948SVladimir Oltean {
75954c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
76056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
76156051948SVladimir Oltean 
76254c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
76354c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76454c31984SVladimir Oltean 
7657e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7667e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7677e580490SVladimir Oltean 		return 0;
7687e580490SVladimir Oltean 
76954c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
77056051948SVladimir Oltean }
77156051948SVladimir Oltean 
77256051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
773c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
774c2693363SVladimir Oltean 			 struct dsa_db db)
77556051948SVladimir Oltean {
77654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
77756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
77856051948SVladimir Oltean 
77954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
78054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
78154c31984SVladimir Oltean 
7827e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
7837e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7847e580490SVladimir Oltean 		return 0;
7857e580490SVladimir Oltean 
78654c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
78756051948SVladimir Oltean }
78856051948SVladimir Oltean 
789961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
790c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
791c2693363SVladimir Oltean 			     struct dsa_db db)
792961d8b69SVladimir Oltean {
79354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
794961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
795961d8b69SVladimir Oltean 
79654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
79754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
79854c31984SVladimir Oltean 
79954c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
800961d8b69SVladimir Oltean }
801961d8b69SVladimir Oltean 
802961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
803c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
804c2693363SVladimir Oltean 			     struct dsa_db db)
805961d8b69SVladimir Oltean {
80654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
807961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
808961d8b69SVladimir Oltean 
80954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
81054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
81154c31984SVladimir Oltean 
81254c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
813961d8b69SVladimir Oltean }
814961d8b69SVladimir Oltean 
815a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
816c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
817c2693363SVladimir Oltean 			 struct dsa_db db)
818209edf95SVladimir Oltean {
81954c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
820209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
821209edf95SVladimir Oltean 
82254c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
82354c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
82454c31984SVladimir Oltean 
8257e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
8267e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
8277e580490SVladimir Oltean 		return 0;
8287e580490SVladimir Oltean 
82954c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
830209edf95SVladimir Oltean }
831209edf95SVladimir Oltean 
832209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
833c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
834c2693363SVladimir Oltean 			 struct dsa_db db)
835209edf95SVladimir Oltean {
83654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
837209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
838209edf95SVladimir Oltean 
83954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
84054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
84154c31984SVladimir Oltean 
8427e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
8437e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
8447e580490SVladimir Oltean 		return 0;
8457e580490SVladimir Oltean 
84654c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
847209edf95SVladimir Oltean }
848209edf95SVladimir Oltean 
84956051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
85056051948SVladimir Oltean 				       u8 state)
85156051948SVladimir Oltean {
85256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85356051948SVladimir Oltean 
85456051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
85556051948SVladimir Oltean }
85656051948SVladimir Oltean 
857421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
858421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
859421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
860421741eaSVladimir Oltean {
861421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
862421741eaSVladimir Oltean 
863421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
864421741eaSVladimir Oltean }
865421741eaSVladimir Oltean 
866421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
867421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
868421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
869421741eaSVladimir Oltean {
870421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
871421741eaSVladimir Oltean 
872421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
873421741eaSVladimir Oltean 
874421741eaSVladimir Oltean 	return 0;
875421741eaSVladimir Oltean }
876421741eaSVladimir Oltean 
87756051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
87806b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
87906b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
88056051948SVladimir Oltean {
88156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
88256051948SVladimir Oltean 
88354c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
88454c31984SVladimir Oltean 				       extack);
88556051948SVladimir Oltean }
88656051948SVladimir Oltean 
88756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
888d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
88956051948SVladimir Oltean {
89056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
89156051948SVladimir Oltean 
892d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
89356051948SVladimir Oltean }
89456051948SVladimir Oltean 
8958fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
896dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8978fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8988fe6832eSVladimir Oltean {
8998fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9008fe6832eSVladimir Oltean 
901dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
9028fe6832eSVladimir Oltean }
9038fe6832eSVladimir Oltean 
9048fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
905dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
9068fe6832eSVladimir Oltean {
9078fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9088fe6832eSVladimir Oltean 
909dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
9108fe6832eSVladimir Oltean 
9118fe6832eSVladimir Oltean 	return 0;
9128fe6832eSVladimir Oltean }
9138fe6832eSVladimir Oltean 
9148fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
9158fe6832eSVladimir Oltean {
9168fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
9178fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9188fe6832eSVladimir Oltean 
9198fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
9208fe6832eSVladimir Oltean 
9218fe6832eSVladimir Oltean 	return 0;
9228fe6832eSVladimir Oltean }
9238fe6832eSVladimir Oltean 
92456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
92501af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
92601af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
92756051948SVladimir Oltean {
9282f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
929b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
9302f0402feSVladimir Oltean 
9319a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
9329a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
9339a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
9349a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
9359a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
9369a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
9379a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
9389a720680SVladimir Oltean 	 */
9399a720680SVladimir Oltean 	if (port == ocelot->npi)
9409a720680SVladimir Oltean 		return 0;
9419a720680SVladimir Oltean 
942b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9432f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
94401af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
94501af940eSVladimir Oltean 				   extack);
94656051948SVladimir Oltean }
94756051948SVladimir Oltean 
94889153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
94989153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
95056051948SVladimir Oltean {
95156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
95256051948SVladimir Oltean 
9533b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
95456051948SVladimir Oltean }
95556051948SVladimir Oltean 
9561958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
95731046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
95831046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
95956051948SVladimir Oltean {
96056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
961183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9621958d581SVladimir Oltean 	int err;
96356051948SVladimir Oltean 
96401af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9651958d581SVladimir Oltean 	if (err)
9661958d581SVladimir Oltean 		return err;
9671958d581SVladimir Oltean 
9681958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
969183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
970183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
97156051948SVladimir Oltean }
97256051948SVladimir Oltean 
97356051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
97456051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
97556051948SVladimir Oltean {
97656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
97756051948SVladimir Oltean 
978b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
97956051948SVladimir Oltean }
98056051948SVladimir Oltean 
98179fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
98279fda660SRussell King (Oracle) 				   struct phylink_config *config)
98379fda660SRussell King (Oracle) {
98479fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
98579fda660SRussell King (Oracle) 
986f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
987f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
988f6f04c02SRussell King (Oracle) 	 * as non-legacy.
989f6f04c02SRussell King (Oracle) 	 */
990f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
991f6f04c02SRussell King (Oracle) 
99279fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
99379fda660SRussell King (Oracle) 		  config->supported_interfaces);
99479fda660SRussell King (Oracle) }
99579fda660SRussell King (Oracle) 
996bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
997bdeced75SVladimir Oltean 				   unsigned long *supported,
998bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
999bdeced75SVladimir Oltean {
1000bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1001375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1002bdeced75SVladimir Oltean 
1003375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
1004375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
1005bdeced75SVladimir Oltean }
1006bdeced75SVladimir Oltean 
1007864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
1008864ba485SRussell King (Oracle) 							int port,
1009864ba485SRussell King (Oracle) 							phy_interface_t iface)
1010bdeced75SVladimir Oltean {
1011bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1012bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1013864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
1014bdeced75SVladimir Oltean 
101549af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
1016864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
1017864ba485SRussell King (Oracle) 
1018864ba485SRussell King (Oracle) 	return pcs;
1019bdeced75SVladimir Oltean }
1020bdeced75SVladimir Oltean 
1021bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
1022bdeced75SVladimir Oltean 					unsigned int link_an_mode,
1023bdeced75SVladimir Oltean 					phy_interface_t interface)
1024bdeced75SVladimir Oltean {
1025bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1026bdeced75SVladimir Oltean 
1027e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1028e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
1029bdeced75SVladimir Oltean }
1030bdeced75SVladimir Oltean 
1031bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
1032bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
1033bdeced75SVladimir Oltean 				      phy_interface_t interface,
10345b502a7bSRussell King 				      struct phy_device *phydev,
10355b502a7bSRussell King 				      int speed, int duplex,
10365b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
1037bdeced75SVladimir Oltean {
1038bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10397e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1040bdeced75SVladimir Oltean 
1041e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1042e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1043e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10447e14a2dcSVladimir Oltean 
10457e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10467e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1047bdeced75SVladimir Oltean }
1048bdeced75SVladimir Oltean 
1049bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1050bd2b3161SXiaoliang Yang {
1051bd2b3161SXiaoliang Yang 	int i;
1052bd2b3161SXiaoliang Yang 
1053bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1054bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1055bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1056bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1057bd2b3161SXiaoliang Yang 		       port);
1058bd2b3161SXiaoliang Yang 
105970d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1060bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1061bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1062bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1063bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1064bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1065bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1066bd2b3161SXiaoliang Yang 			      port, i);
1067bd2b3161SXiaoliang Yang 	}
1068bd2b3161SXiaoliang Yang }
1069bd2b3161SXiaoliang Yang 
107056051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
107156051948SVladimir Oltean 			      u32 stringset, u8 *data)
107256051948SVladimir Oltean {
107356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
107456051948SVladimir Oltean 
107556051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
107656051948SVladimir Oltean }
107756051948SVladimir Oltean 
107856051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
107956051948SVladimir Oltean {
108056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
108156051948SVladimir Oltean 
108256051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
108356051948SVladimir Oltean }
108456051948SVladimir Oltean 
108556051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
108656051948SVladimir Oltean {
108756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
108856051948SVladimir Oltean 
108956051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
109056051948SVladimir Oltean }
109156051948SVladimir Oltean 
109256051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
109356051948SVladimir Oltean 			     struct ethtool_ts_info *info)
109456051948SVladimir Oltean {
109556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
109656051948SVladimir Oltean 
109756051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
109856051948SVladimir Oltean }
109956051948SVladimir Oltean 
1100acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1101acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1102acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1103acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1104acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1105acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1106acf242fcSColin Foster };
1107acf242fcSColin Foster 
1108acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1109acf242fcSColin Foster 				   phy_interface_t phy_mode)
1110acf242fcSColin Foster {
1111acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1112acf242fcSColin Foster 
1113acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1114acf242fcSColin Foster 		return 0;
1115acf242fcSColin Foster 	return -EOPNOTSUPP;
1116acf242fcSColin Foster }
1117acf242fcSColin Foster 
1118bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1119bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1120bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1121bdeced75SVladimir Oltean {
1122bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1123bdeced75SVladimir Oltean 	struct device_node *child;
1124bdeced75SVladimir Oltean 
112537fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1126bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1127bdeced75SVladimir Oltean 		u32 port;
1128bdeced75SVladimir Oltean 		int err;
1129bdeced75SVladimir Oltean 
1130bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1131bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1132bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1133bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1134bdeced75SVladimir Oltean 			of_node_put(child);
1135bdeced75SVladimir Oltean 			return -ENODEV;
1136bdeced75SVladimir Oltean 		}
1137bdeced75SVladimir Oltean 
1138bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1139bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1140bdeced75SVladimir Oltean 		if (err) {
1141bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1142bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1143bdeced75SVladimir Oltean 				port);
1144bdeced75SVladimir Oltean 			of_node_put(child);
1145bdeced75SVladimir Oltean 			return -ENODEV;
1146bdeced75SVladimir Oltean 		}
1147bdeced75SVladimir Oltean 
1148acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1149bdeced75SVladimir Oltean 		if (err < 0) {
1150bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1151bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
115259ebb430SSumera Priyadarsini 			of_node_put(child);
1153bdeced75SVladimir Oltean 			return err;
1154bdeced75SVladimir Oltean 		}
1155bdeced75SVladimir Oltean 
1156bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1157bdeced75SVladimir Oltean 	}
1158bdeced75SVladimir Oltean 
1159bdeced75SVladimir Oltean 	return 0;
1160bdeced75SVladimir Oltean }
1161bdeced75SVladimir Oltean 
1162bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1163bdeced75SVladimir Oltean {
1164bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1165bdeced75SVladimir Oltean 	struct device_node *switch_node;
1166bdeced75SVladimir Oltean 	struct device_node *ports_node;
1167bdeced75SVladimir Oltean 	int err;
1168bdeced75SVladimir Oltean 
1169bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1170bdeced75SVladimir Oltean 
1171bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1172abecbfcdSVladimir Oltean 	if (!ports_node)
1173abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1174bdeced75SVladimir Oltean 	if (!ports_node) {
1175abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1176bdeced75SVladimir Oltean 		return -ENODEV;
1177bdeced75SVladimir Oltean 	}
1178bdeced75SVladimir Oltean 
1179bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1180bdeced75SVladimir Oltean 	of_node_put(ports_node);
1181bdeced75SVladimir Oltean 
1182bdeced75SVladimir Oltean 	return err;
1183bdeced75SVladimir Oltean }
1184bdeced75SVladimir Oltean 
118556051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
118656051948SVladimir Oltean {
118756051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1188bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1189b4024c9eSClaudiu Manoil 	struct resource res;
119056051948SVladimir Oltean 	int port, i, err;
119156051948SVladimir Oltean 
119256051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
119356051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
119456051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
119556051948SVladimir Oltean 	if (!ocelot->ports)
119656051948SVladimir Oltean 		return -ENOMEM;
119756051948SVladimir Oltean 
119856051948SVladimir Oltean 	ocelot->map		= felix->info->map;
119956051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
120056051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
120121ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
120207d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
120377043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
120477043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
120577043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
120677043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
120756051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1208cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1209cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1210f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
121156051948SVladimir Oltean 
1212bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1213bdeced75SVladimir Oltean 				 GFP_KERNEL);
1214bdeced75SVladimir Oltean 	if (!port_phy_modes)
1215bdeced75SVladimir Oltean 		return -ENOMEM;
1216bdeced75SVladimir Oltean 
1217bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1218bdeced75SVladimir Oltean 	if (err) {
1219bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1220bdeced75SVladimir Oltean 		return err;
1221bdeced75SVladimir Oltean 	}
1222bdeced75SVladimir Oltean 
122356051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
122456051948SVladimir Oltean 		struct regmap *target;
122556051948SVladimir Oltean 
122656051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
122756051948SVladimir Oltean 			continue;
122856051948SVladimir Oltean 
1229b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1230b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1231375e1314SVladimir Oltean 		res.start += felix->switch_base;
1232375e1314SVladimir Oltean 		res.end += felix->switch_base;
123356051948SVladimir Oltean 
1234242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
123556051948SVladimir Oltean 		if (IS_ERR(target)) {
123656051948SVladimir Oltean 			dev_err(ocelot->dev,
123756051948SVladimir Oltean 				"Failed to map device memory space\n");
1238bdeced75SVladimir Oltean 			kfree(port_phy_modes);
123956051948SVladimir Oltean 			return PTR_ERR(target);
124056051948SVladimir Oltean 		}
124156051948SVladimir Oltean 
124256051948SVladimir Oltean 		ocelot->targets[i] = target;
124356051948SVladimir Oltean 	}
124456051948SVladimir Oltean 
124556051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
124656051948SVladimir Oltean 	if (err) {
124756051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1248bdeced75SVladimir Oltean 		kfree(port_phy_modes);
124956051948SVladimir Oltean 		return err;
125056051948SVladimir Oltean 	}
125156051948SVladimir Oltean 
125256051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
125356051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
125491c724cfSVladimir Oltean 		struct regmap *target;
125556051948SVladimir Oltean 
125656051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
125756051948SVladimir Oltean 					   sizeof(struct ocelot_port),
125856051948SVladimir Oltean 					   GFP_KERNEL);
125956051948SVladimir Oltean 		if (!ocelot_port) {
126056051948SVladimir Oltean 			dev_err(ocelot->dev,
126156051948SVladimir Oltean 				"failed to allocate port memory\n");
1262bdeced75SVladimir Oltean 			kfree(port_phy_modes);
126356051948SVladimir Oltean 			return -ENOMEM;
126456051948SVladimir Oltean 		}
126556051948SVladimir Oltean 
1266b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1267b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1268375e1314SVladimir Oltean 		res.start += felix->switch_base;
1269375e1314SVladimir Oltean 		res.end += felix->switch_base;
127056051948SVladimir Oltean 
1271242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
127291c724cfSVladimir Oltean 		if (IS_ERR(target)) {
127356051948SVladimir Oltean 			dev_err(ocelot->dev,
127491c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
127591c724cfSVladimir Oltean 				port);
1276bdeced75SVladimir Oltean 			kfree(port_phy_modes);
127791c724cfSVladimir Oltean 			return PTR_ERR(target);
127856051948SVladimir Oltean 		}
127956051948SVladimir Oltean 
1280bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
128156051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
128291c724cfSVladimir Oltean 		ocelot_port->target = target;
128356051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
128456051948SVladimir Oltean 	}
128556051948SVladimir Oltean 
1286bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1287bdeced75SVladimir Oltean 
1288bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1289bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1290bdeced75SVladimir Oltean 		if (err < 0)
1291bdeced75SVladimir Oltean 			return err;
1292bdeced75SVladimir Oltean 	}
1293bdeced75SVladimir Oltean 
129456051948SVladimir Oltean 	return 0;
129556051948SVladimir Oltean }
129656051948SVladimir Oltean 
12971328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12981328a883SVladimir Oltean 					   struct sk_buff *skb)
12991328a883SVladimir Oltean {
13001328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
13011328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
13021328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
13031328a883SVladimir Oltean 	unsigned long flags;
13041328a883SVladimir Oltean 
13051328a883SVladimir Oltean 	if (!clone)
13061328a883SVladimir Oltean 		return;
13071328a883SVladimir Oltean 
13081328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
13091328a883SVladimir Oltean 
13101328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
13111328a883SVladimir Oltean 		if (skb != clone)
13121328a883SVladimir Oltean 			continue;
13131328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
13141328a883SVladimir Oltean 		skb_match = skb;
13151328a883SVladimir Oltean 		break;
13161328a883SVladimir Oltean 	}
13171328a883SVladimir Oltean 
13181328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
13191328a883SVladimir Oltean 
13201328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
13211328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
13221328a883SVladimir Oltean }
13231328a883SVladimir Oltean 
132449f885b2SVladimir Oltean #define work_to_xmit_work(w) \
132549f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
132649f885b2SVladimir Oltean 
132749f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
132849f885b2SVladimir Oltean {
132949f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
133049f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
133149f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
133249f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
133349f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
133449f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
133549f885b2SVladimir Oltean 	int retries = 10;
133649f885b2SVladimir Oltean 
133749f885b2SVladimir Oltean 	do {
133849f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
133949f885b2SVladimir Oltean 			break;
134049f885b2SVladimir Oltean 
134149f885b2SVladimir Oltean 		cpu_relax();
134249f885b2SVladimir Oltean 	} while (--retries);
134349f885b2SVladimir Oltean 
134449f885b2SVladimir Oltean 	if (!retries) {
134549f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
134649f885b2SVladimir Oltean 			port);
13471328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
134849f885b2SVladimir Oltean 		kfree_skb(skb);
134949f885b2SVladimir Oltean 		return;
135049f885b2SVladimir Oltean 	}
135149f885b2SVladimir Oltean 
135249f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
135349f885b2SVladimir Oltean 
135449f885b2SVladimir Oltean 	consume_skb(skb);
135549f885b2SVladimir Oltean 	kfree(xmit_work);
135649f885b2SVladimir Oltean }
135749f885b2SVladimir Oltean 
135835d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
135935d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
136049f885b2SVladimir Oltean {
136135d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
136249f885b2SVladimir Oltean 
136335d97680SVladimir Oltean 	switch (proto) {
136435d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
136535d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
136635d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
136749f885b2SVladimir Oltean 		return 0;
136835d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
136935d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
137049f885b2SVladimir Oltean 		return 0;
137135d97680SVladimir Oltean 	default:
137235d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
137349f885b2SVladimir Oltean 	}
137449f885b2SVladimir Oltean }
137549f885b2SVladimir Oltean 
137656051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
137756051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
137856051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
137956051948SVladimir Oltean  * (which will not work).
138056051948SVladimir Oltean  */
138156051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
138256051948SVladimir Oltean {
138356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
138456051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1385f2e2662cSVladimir Oltean 	unsigned long cpu_flood;
13862960bb14SVladimir Oltean 	struct dsa_port *dp;
13872960bb14SVladimir Oltean 	int err;
138856051948SVladimir Oltean 
138956051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
139056051948SVladimir Oltean 	if (err)
139156051948SVladimir Oltean 		return err;
139256051948SVladimir Oltean 
1393d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1394d1cc0e93SVladimir Oltean 	if (err)
13956b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1396d1cc0e93SVladimir Oltean 
13972b49d128SYangbo Lu 	if (ocelot->ptp) {
13982ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13992b49d128SYangbo Lu 		if (err) {
14002b49d128SYangbo Lu 			dev_err(ocelot->dev,
14012b49d128SYangbo Lu 				"Timestamp initialization failed\n");
14022b49d128SYangbo Lu 			ocelot->ptp = 0;
14032b49d128SYangbo Lu 		}
14042b49d128SYangbo Lu 	}
140556051948SVladimir Oltean 
14062960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
14072960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1408bd2b3161SXiaoliang Yang 
1409bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1410bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1411bd2b3161SXiaoliang Yang 		 */
14122960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
141356051948SVladimir Oltean 	}
141456051948SVladimir Oltean 
1415f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1416f59fd9caSVladimir Oltean 	if (err)
14176b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1418f59fd9caSVladimir Oltean 
14192960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1420adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1421adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
14221cf3299bSVladimir Oltean 		 */
1423c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
1424f2e2662cSVladimir Oltean 
1425f2e2662cSVladimir Oltean 		/* Start off with flooding disabled towards the NPI port
1426f2e2662cSVladimir Oltean 		 * (actually CPU port module).
1427f2e2662cSVladimir Oltean 		 */
1428f2e2662cSVladimir Oltean 		cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
1429f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
1430f2e2662cSVladimir Oltean 		ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
1431f2e2662cSVladimir Oltean 
14328d5f7954SVladimir Oltean 		break;
1433adb3dccfSVladimir Oltean 	}
14341cf3299bSVladimir Oltean 
14350b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1436c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
143754c31984SVladimir Oltean 	ds->fdb_isolation = true;
143854c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1439bdeced75SVladimir Oltean 
144056051948SVladimir Oltean 	return 0;
14416b73b7c9SVladimir Oltean 
14426b73b7c9SVladimir Oltean out_deinit_ports:
14432960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14442960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
14456b73b7c9SVladimir Oltean 
14466b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
14476b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
14486b73b7c9SVladimir Oltean 
14496b73b7c9SVladimir Oltean out_mdiobus_free:
14506b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14516b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14526b73b7c9SVladimir Oltean 
14536b73b7c9SVladimir Oltean 	return err;
145456051948SVladimir Oltean }
145556051948SVladimir Oltean 
145656051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
145756051948SVladimir Oltean {
145856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1459bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14602960bb14SVladimir Oltean 	struct dsa_port *dp;
1461bdeced75SVladimir Oltean 
14622960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14632960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14648d5f7954SVladimir Oltean 		break;
1465adb3dccfSVladimir Oltean 	}
1466adb3dccfSVladimir Oltean 
14672960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14682960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1469d19741b0SVladimir Oltean 
147049f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
147149f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
147249f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
147349f885b2SVladimir Oltean 
1474d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1475d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
147656051948SVladimir Oltean }
147756051948SVladimir Oltean 
1478c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1479c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1480c0bcf537SYangbo Lu {
1481c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1482c0bcf537SYangbo Lu 
1483c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1484c0bcf537SYangbo Lu }
1485c0bcf537SYangbo Lu 
1486c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1487c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1488c0bcf537SYangbo Lu {
1489c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
149099348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
149199348004SVladimir Oltean 	bool using_tag_8021q;
149299348004SVladimir Oltean 	int err;
1493c0bcf537SYangbo Lu 
149499348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
149599348004SVladimir Oltean 	if (err)
149699348004SVladimir Oltean 		return err;
149799348004SVladimir Oltean 
149899348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
149999348004SVladimir Oltean 
150099348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1501c0bcf537SYangbo Lu }
1502c0bcf537SYangbo Lu 
1503d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
15040a6f17c6SVladimir Oltean {
15050a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1506dbd03285SVladimir Oltean 	int err = 0, grp = 0;
15070a6f17c6SVladimir Oltean 
15080a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
15090a6f17c6SVladimir Oltean 		return false;
15100a6f17c6SVladimir Oltean 
15110a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
15120a6f17c6SVladimir Oltean 		return false;
15130a6f17c6SVladimir Oltean 
15140a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
15150a6f17c6SVladimir Oltean 		struct sk_buff *skb;
15160a6f17c6SVladimir Oltean 		unsigned int type;
15170a6f17c6SVladimir Oltean 
15180a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
15190a6f17c6SVladimir Oltean 		if (err)
15200a6f17c6SVladimir Oltean 			goto out;
15210a6f17c6SVladimir Oltean 
15220a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
15230a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
15240a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
15250a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
15260a6f17c6SVladimir Oltean 		 * here and dropping those.
15270a6f17c6SVladimir Oltean 		 */
15280a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
15290a6f17c6SVladimir Oltean 
15300a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
15310a6f17c6SVladimir Oltean 
15320a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
15330a6f17c6SVladimir Oltean 
15340a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
15350a6f17c6SVladimir Oltean 			kfree_skb(skb);
15360a6f17c6SVladimir Oltean 			continue;
15370a6f17c6SVladimir Oltean 		}
15380a6f17c6SVladimir Oltean 
15390a6f17c6SVladimir Oltean 		netif_rx(skb);
15400a6f17c6SVladimir Oltean 	}
15410a6f17c6SVladimir Oltean 
15420a6f17c6SVladimir Oltean out:
15435d3bb7ddSVladimir Oltean 	if (err < 0) {
15445d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
15455d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
15465d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
15470a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
15485d3bb7ddSVladimir Oltean 	}
15490a6f17c6SVladimir Oltean 
15500a6f17c6SVladimir Oltean 	return true;
15510a6f17c6SVladimir Oltean }
15520a6f17c6SVladimir Oltean 
1553c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1554c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1555c0bcf537SYangbo Lu {
155692f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1557c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1558c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1559c0bcf537SYangbo Lu 	struct timespec64 ts;
156092f62485SVladimir Oltean 	u32 tstamp_hi;
156192f62485SVladimir Oltean 	u64 tstamp;
1562c0bcf537SYangbo Lu 
15630a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15640a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15650a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15660a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15670a6f17c6SVladimir Oltean 	 */
1568d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
15690a6f17c6SVladimir Oltean 		kfree_skb(skb);
15700a6f17c6SVladimir Oltean 		return true;
15710a6f17c6SVladimir Oltean 	}
15720a6f17c6SVladimir Oltean 
1573c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1574c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1575c0bcf537SYangbo Lu 
1576c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1577c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1578c0bcf537SYangbo Lu 		tstamp_hi--;
1579c0bcf537SYangbo Lu 
1580c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1581c0bcf537SYangbo Lu 
1582c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1583c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1584c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1585c0bcf537SYangbo Lu 	return false;
1586c0bcf537SYangbo Lu }
1587c0bcf537SYangbo Lu 
15885c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15895c5416f5SYangbo Lu 			   struct sk_buff *skb)
1590c0bcf537SYangbo Lu {
1591c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1592682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1593c0bcf537SYangbo Lu 
1594682eaad9SYangbo Lu 	if (!ocelot->ptp)
15955c5416f5SYangbo Lu 		return;
1596c0bcf537SYangbo Lu 
159752849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
159852849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
159952849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
160052849bcfSVladimir Oltean 				    port);
1601682eaad9SYangbo Lu 		return;
160252849bcfSVladimir Oltean 	}
1603682eaad9SYangbo Lu 
1604682eaad9SYangbo Lu 	if (clone)
1605c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
16065c5416f5SYangbo Lu }
1607c0bcf537SYangbo Lu 
16080b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
16090b912fc9SVladimir Oltean {
16100b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16110b912fc9SVladimir Oltean 
16120b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
16130b912fc9SVladimir Oltean 
16140b912fc9SVladimir Oltean 	return 0;
16150b912fc9SVladimir Oltean }
16160b912fc9SVladimir Oltean 
16170b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
16180b912fc9SVladimir Oltean {
16190b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16200b912fc9SVladimir Oltean 
16210b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
16220b912fc9SVladimir Oltean }
16230b912fc9SVladimir Oltean 
162407d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
162507d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
162607d985eeSVladimir Oltean {
162707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
162899348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
162999348004SVladimir Oltean 	bool using_tag_8021q;
163099348004SVladimir Oltean 	int err;
163107d985eeSVladimir Oltean 
163299348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
163399348004SVladimir Oltean 	if (err)
163499348004SVladimir Oltean 		return err;
163599348004SVladimir Oltean 
163699348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
163799348004SVladimir Oltean 
163899348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
163907d985eeSVladimir Oltean }
164007d985eeSVladimir Oltean 
164107d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
164207d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
164307d985eeSVladimir Oltean {
164407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
164507d985eeSVladimir Oltean 
164607d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
164707d985eeSVladimir Oltean }
164807d985eeSVladimir Oltean 
164907d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
165007d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
165107d985eeSVladimir Oltean {
165207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
165307d985eeSVladimir Oltean 
165407d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
165507d985eeSVladimir Oltean }
165607d985eeSVladimir Oltean 
1657fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1658fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1659fc411eaaSVladimir Oltean {
1660fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1661fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1662fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16635f035af7SPo Liu 		.burst = policer->burst,
1664fc411eaaSVladimir Oltean 	};
1665fc411eaaSVladimir Oltean 
1666fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1667fc411eaaSVladimir Oltean }
1668fc411eaaSVladimir Oltean 
1669fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1670fc411eaaSVladimir Oltean {
1671fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1672fc411eaaSVladimir Oltean 
1673fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1674fc411eaaSVladimir Oltean }
1675fc411eaaSVladimir Oltean 
16765e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port,
16775e497497SVladimir Oltean 				 struct dsa_mall_mirror_tc_entry *mirror,
16785e497497SVladimir Oltean 				 bool ingress, struct netlink_ext_ack *extack)
16795e497497SVladimir Oltean {
16805e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16815e497497SVladimir Oltean 
16825e497497SVladimir Oltean 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
16835e497497SVladimir Oltean 				      ingress, extack);
16845e497497SVladimir Oltean }
16855e497497SVladimir Oltean 
16865e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port,
16875e497497SVladimir Oltean 				  struct dsa_mall_mirror_tc_entry *mirror)
16885e497497SVladimir Oltean {
16895e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16905e497497SVladimir Oltean 
16915e497497SVladimir Oltean 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
16925e497497SVladimir Oltean }
16935e497497SVladimir Oltean 
1694de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1695de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1696de143c0eSXiaoliang Yang 			       void *type_data)
1697de143c0eSXiaoliang Yang {
1698de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1699de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1700de143c0eSXiaoliang Yang 
1701de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1702de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1703de143c0eSXiaoliang Yang 	else
1704de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1705de143c0eSXiaoliang Yang }
1706de143c0eSXiaoliang Yang 
1707f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1708f59fd9caSVladimir Oltean 			     u16 pool_index,
1709f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1710f59fd9caSVladimir Oltean {
1711f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1712f59fd9caSVladimir Oltean 
1713f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1714f59fd9caSVladimir Oltean }
1715f59fd9caSVladimir Oltean 
1716f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1717f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1718f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1719f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1720f59fd9caSVladimir Oltean {
1721f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1722f59fd9caSVladimir Oltean 
1723f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1724f59fd9caSVladimir Oltean 				  threshold_type, extack);
1725f59fd9caSVladimir Oltean }
1726f59fd9caSVladimir Oltean 
1727f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1728f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1729f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1730f59fd9caSVladimir Oltean {
1731f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1732f59fd9caSVladimir Oltean 
1733f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1734f59fd9caSVladimir Oltean 				       p_threshold);
1735f59fd9caSVladimir Oltean }
1736f59fd9caSVladimir Oltean 
1737f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1738f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1739f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1740f59fd9caSVladimir Oltean {
1741f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1742f59fd9caSVladimir Oltean 
1743f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1744f59fd9caSVladimir Oltean 				       threshold, extack);
1745f59fd9caSVladimir Oltean }
1746f59fd9caSVladimir Oltean 
1747f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1748f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1749f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1750f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1751f59fd9caSVladimir Oltean {
1752f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1753f59fd9caSVladimir Oltean 
1754f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1755f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1756f59fd9caSVladimir Oltean 					  p_threshold);
1757f59fd9caSVladimir Oltean }
1758f59fd9caSVladimir Oltean 
1759f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1760f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1761f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1762f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1763f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1764f59fd9caSVladimir Oltean {
1765f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1766f59fd9caSVladimir Oltean 
1767f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1768f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1769f59fd9caSVladimir Oltean 					  extack);
1770f59fd9caSVladimir Oltean }
1771f59fd9caSVladimir Oltean 
1772f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1773f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1774f59fd9caSVladimir Oltean {
1775f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1776f59fd9caSVladimir Oltean 
1777f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1778f59fd9caSVladimir Oltean }
1779f59fd9caSVladimir Oltean 
1780f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1781f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1782f59fd9caSVladimir Oltean {
1783f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1784f59fd9caSVladimir Oltean 
1785f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1786f59fd9caSVladimir Oltean }
1787f59fd9caSVladimir Oltean 
1788f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1789f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1790f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1791f59fd9caSVladimir Oltean {
1792f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1793f59fd9caSVladimir Oltean 
1794f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1795f59fd9caSVladimir Oltean 					   p_cur, p_max);
1796f59fd9caSVladimir Oltean }
1797f59fd9caSVladimir Oltean 
1798f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1799f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1800f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1801f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1802f59fd9caSVladimir Oltean {
1803f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1804f59fd9caSVladimir Oltean 
1805f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1806f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1807f59fd9caSVladimir Oltean }
1808f59fd9caSVladimir Oltean 
1809a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1810a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1811a026c50bSHoratiu Vultur {
1812a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1813a026c50bSHoratiu Vultur 
1814a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1815a026c50bSHoratiu Vultur }
1816a026c50bSHoratiu Vultur 
1817a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1818a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1819a026c50bSHoratiu Vultur {
1820a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1821a026c50bSHoratiu Vultur 
1822a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1823a026c50bSHoratiu Vultur }
1824a026c50bSHoratiu Vultur 
1825a026c50bSHoratiu Vultur static int
1826a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1827a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1828a026c50bSHoratiu Vultur {
1829a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1830a026c50bSHoratiu Vultur 
1831a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1832a026c50bSHoratiu Vultur }
1833a026c50bSHoratiu Vultur 
1834a026c50bSHoratiu Vultur static int
1835a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1836a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1837a026c50bSHoratiu Vultur {
1838a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1839a026c50bSHoratiu Vultur 
1840a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1841a026c50bSHoratiu Vultur }
1842a026c50bSHoratiu Vultur 
1843978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
1844978777d0SVladimir Oltean {
1845978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1846978777d0SVladimir Oltean 
1847978777d0SVladimir Oltean 	return ocelot_port_get_default_prio(ocelot, port);
1848978777d0SVladimir Oltean }
1849978777d0SVladimir Oltean 
1850978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
1851978777d0SVladimir Oltean 				       u8 prio)
1852978777d0SVladimir Oltean {
1853978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1854978777d0SVladimir Oltean 
1855978777d0SVladimir Oltean 	return ocelot_port_set_default_prio(ocelot, port, prio);
1856978777d0SVladimir Oltean }
1857978777d0SVladimir Oltean 
1858978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
1859978777d0SVladimir Oltean {
1860978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1861978777d0SVladimir Oltean 
1862978777d0SVladimir Oltean 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
1863978777d0SVladimir Oltean }
1864978777d0SVladimir Oltean 
1865978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1866978777d0SVladimir Oltean 				    u8 prio)
1867978777d0SVladimir Oltean {
1868978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1869978777d0SVladimir Oltean 
1870978777d0SVladimir Oltean 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
1871978777d0SVladimir Oltean }
1872978777d0SVladimir Oltean 
1873978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
1874978777d0SVladimir Oltean 				    u8 prio)
1875978777d0SVladimir Oltean {
1876978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1877978777d0SVladimir Oltean 
1878978777d0SVladimir Oltean 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
1879978777d0SVladimir Oltean }
1880978777d0SVladimir Oltean 
1881375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
188256051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1883adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
188435d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
188556051948SVladimir Oltean 	.setup				= felix_setup,
188656051948SVladimir Oltean 	.teardown			= felix_teardown,
188756051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
188856051948SVladimir Oltean 	.get_strings			= felix_get_strings,
188956051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
189056051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
189156051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
189279fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1893bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1894864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1895bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1896bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18975cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
189856051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
189956051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
190056051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1901961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1902961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1903209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1904209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1905421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1906421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
190756051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
190856051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
19098fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
19108fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
19118fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
191256051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
191356051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
191456051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
191556051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1916c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1917c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1918c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1919c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
19200b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
19210b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1922fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1923fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
19245e497497SVladimir Oltean 	.port_mirror_add		= felix_port_mirror_add,
19255e497497SVladimir Oltean 	.port_mirror_del		= felix_port_mirror_del,
192607d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
192707d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
192807d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1929de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1930f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1931f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1932f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1933f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1934f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1935f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1936f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1937f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1938f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1939f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1940a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1941a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1942a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1943a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
19445da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
19455da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
1946978777d0SVladimir Oltean 	.port_get_default_prio		= felix_port_get_default_prio,
1947978777d0SVladimir Oltean 	.port_set_default_prio		= felix_port_set_default_prio,
1948978777d0SVladimir Oltean 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
1949978777d0SVladimir Oltean 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
1950978777d0SVladimir Oltean 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
195156051948SVladimir Oltean };
1952319e4dd1SVladimir Oltean 
1953319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1954319e4dd1SVladimir Oltean {
1955319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1956319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1957319e4dd1SVladimir Oltean 
1958319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1959319e4dd1SVladimir Oltean 		return NULL;
1960319e4dd1SVladimir Oltean 
1961319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1962319e4dd1SVladimir Oltean }
1963319e4dd1SVladimir Oltean 
1964319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1965319e4dd1SVladimir Oltean {
1966319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1967319e4dd1SVladimir Oltean 
1968319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1969319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1970319e4dd1SVladimir Oltean 		return -EINVAL;
1971319e4dd1SVladimir Oltean 
1972319e4dd1SVladimir Oltean 	return dp->index;
1973319e4dd1SVladimir Oltean }
1974