xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision c69f40ac60060e09ca8f8c2ac7e6057f8a4c9f28)
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 
463*c69f40acSVladimir 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 
491*c69f40acSVladimir 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 
495*c69f40acSVladimir 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 
595*c69f40acSVladimir 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 
600*c69f40acSVladimir Oltean 	err = dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
601f9cef64fSVladimir Oltean 	if (err)
602f9cef64fSVladimir Oltean 		return err;
603f9cef64fSVladimir Oltean 
604*c69f40acSVladimir 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:
615*c69f40acSVladimir 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,
628*c69f40acSVladimir 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:
635*c69f40acSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu);
636adb3dccfSVladimir Oltean 		break;
637e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
638*c69f40acSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu);
639e21268efSVladimir Oltean 		break;
640adb3dccfSVladimir Oltean 	default:
641adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
642adb3dccfSVladimir Oltean 	}
643adb3dccfSVladimir Oltean 
644adb3dccfSVladimir Oltean 	return err;
645adb3dccfSVladimir Oltean }
646adb3dccfSVladimir Oltean 
647adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
648adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
649adb3dccfSVladimir Oltean {
650adb3dccfSVladimir Oltean 	switch (proto) {
6517c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
652adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
653adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
654adb3dccfSVladimir Oltean 		break;
655e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
656e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
657e21268efSVladimir Oltean 		break;
658adb3dccfSVladimir Oltean 	default:
659adb3dccfSVladimir Oltean 		break;
660adb3dccfSVladimir Oltean 	}
661adb3dccfSVladimir Oltean }
662adb3dccfSVladimir Oltean 
663e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
664e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
665e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
666e21268efSVladimir Oltean  */
667adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
668adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
669adb3dccfSVladimir Oltean {
670adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
671adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
672adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
673adb3dccfSVladimir Oltean 	int err;
674adb3dccfSVladimir Oltean 
6757c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
6767c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
677e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
678adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
679adb3dccfSVladimir Oltean 
680adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
681adb3dccfSVladimir Oltean 
682*c69f40acSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto);
683adb3dccfSVladimir Oltean 	if (err) {
684*c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto);
685adb3dccfSVladimir Oltean 		return err;
686adb3dccfSVladimir Oltean 	}
687adb3dccfSVladimir Oltean 
688adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
689adb3dccfSVladimir Oltean 
690adb3dccfSVladimir Oltean 	return 0;
691adb3dccfSVladimir Oltean }
692adb3dccfSVladimir Oltean 
69356051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6944d776482SFlorian Fainelli 						    int port,
6954d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
69656051948SVladimir Oltean {
697adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
698adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
699adb3dccfSVladimir Oltean 
700adb3dccfSVladimir Oltean 	return felix->tag_proto;
70156051948SVladimir Oltean }
70256051948SVladimir Oltean 
70356051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
70456051948SVladimir Oltean 				 unsigned int ageing_time)
70556051948SVladimir Oltean {
70656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70756051948SVladimir Oltean 
70856051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
70956051948SVladimir Oltean 
71056051948SVladimir Oltean 	return 0;
71156051948SVladimir Oltean }
71256051948SVladimir Oltean 
7135cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
7145cad43a5SVladimir Oltean {
7155cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7165cad43a5SVladimir Oltean 	int err;
7175cad43a5SVladimir Oltean 
7185cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
7195cad43a5SVladimir Oltean 	if (err)
7205cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
7215cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7225cad43a5SVladimir Oltean }
7235cad43a5SVladimir Oltean 
72456051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
72556051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
72656051948SVladimir Oltean {
72756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72856051948SVladimir Oltean 
72956051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
73056051948SVladimir Oltean }
73156051948SVladimir Oltean 
73256051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
733c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
734c2693363SVladimir Oltean 			 struct dsa_db db)
73556051948SVladimir Oltean {
73654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
73756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73856051948SVladimir Oltean 
73954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
74054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
74154c31984SVladimir Oltean 
74254c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
74356051948SVladimir Oltean }
74456051948SVladimir Oltean 
74556051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
746c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
747c2693363SVladimir Oltean 			 struct dsa_db db)
74856051948SVladimir Oltean {
74954c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
75056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
75156051948SVladimir Oltean 
75254c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
75354c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
75454c31984SVladimir Oltean 
75554c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
75656051948SVladimir Oltean }
75756051948SVladimir Oltean 
758961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
759c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
760c2693363SVladimir Oltean 			     struct dsa_db db)
761961d8b69SVladimir Oltean {
76254c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
763961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
764961d8b69SVladimir Oltean 
76554c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
76654c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76754c31984SVladimir Oltean 
76854c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
769961d8b69SVladimir Oltean }
770961d8b69SVladimir Oltean 
771961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
772c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
773c2693363SVladimir Oltean 			     struct dsa_db db)
774961d8b69SVladimir Oltean {
77554c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
776961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
777961d8b69SVladimir Oltean 
77854c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
77954c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
78054c31984SVladimir Oltean 
78154c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
782961d8b69SVladimir Oltean }
783961d8b69SVladimir Oltean 
784a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
785c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
786c2693363SVladimir Oltean 			 struct dsa_db db)
787209edf95SVladimir Oltean {
78854c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
789209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
790209edf95SVladimir Oltean 
79154c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
79254c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
79354c31984SVladimir Oltean 
79454c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
795209edf95SVladimir Oltean }
796209edf95SVladimir Oltean 
797209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
798c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
799c2693363SVladimir Oltean 			 struct dsa_db db)
800209edf95SVladimir Oltean {
80154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
802209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
803209edf95SVladimir Oltean 
80454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
80554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
80654c31984SVladimir Oltean 
80754c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
808209edf95SVladimir Oltean }
809209edf95SVladimir Oltean 
81056051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
81156051948SVladimir Oltean 				       u8 state)
81256051948SVladimir Oltean {
81356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
81456051948SVladimir Oltean 
81556051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
81656051948SVladimir Oltean }
81756051948SVladimir Oltean 
818421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
819421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
820421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
821421741eaSVladimir Oltean {
822421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
823421741eaSVladimir Oltean 
824421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
825421741eaSVladimir Oltean }
826421741eaSVladimir Oltean 
827421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
828421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
829421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
830421741eaSVladimir Oltean {
831421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
832421741eaSVladimir Oltean 
833421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
834421741eaSVladimir Oltean 
835421741eaSVladimir Oltean 	return 0;
836421741eaSVladimir Oltean }
837421741eaSVladimir Oltean 
83856051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
83906b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
84006b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
84156051948SVladimir Oltean {
84256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84356051948SVladimir Oltean 
84454c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
84554c31984SVladimir Oltean 				       extack);
84656051948SVladimir Oltean }
84756051948SVladimir Oltean 
84856051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
849d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
85056051948SVladimir Oltean {
85156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85256051948SVladimir Oltean 
853d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
85456051948SVladimir Oltean }
85556051948SVladimir Oltean 
8568fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
857dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8588fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8598fe6832eSVladimir Oltean {
8608fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8618fe6832eSVladimir Oltean 
862dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8638fe6832eSVladimir Oltean }
8648fe6832eSVladimir Oltean 
8658fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
866dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8678fe6832eSVladimir Oltean {
8688fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8698fe6832eSVladimir Oltean 
870dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8718fe6832eSVladimir Oltean 
8728fe6832eSVladimir Oltean 	return 0;
8738fe6832eSVladimir Oltean }
8748fe6832eSVladimir Oltean 
8758fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8768fe6832eSVladimir Oltean {
8778fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8788fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8798fe6832eSVladimir Oltean 
8808fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8818fe6832eSVladimir Oltean 
8828fe6832eSVladimir Oltean 	return 0;
8838fe6832eSVladimir Oltean }
8848fe6832eSVladimir Oltean 
88556051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
88601af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
88701af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
88856051948SVladimir Oltean {
8892f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
890b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
8912f0402feSVladimir Oltean 
8929a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
8939a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
8949a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
8959a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
8969a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
8979a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
8989a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
8999a720680SVladimir Oltean 	 */
9009a720680SVladimir Oltean 	if (port == ocelot->npi)
9019a720680SVladimir Oltean 		return 0;
9029a720680SVladimir Oltean 
903b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9042f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
90501af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
90601af940eSVladimir Oltean 				   extack);
90756051948SVladimir Oltean }
90856051948SVladimir Oltean 
90989153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
91089153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
91156051948SVladimir Oltean {
91256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
91356051948SVladimir Oltean 
9143b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
91556051948SVladimir Oltean }
91656051948SVladimir Oltean 
9171958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
91831046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
91931046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
92056051948SVladimir Oltean {
92156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
922183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9231958d581SVladimir Oltean 	int err;
92456051948SVladimir Oltean 
92501af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9261958d581SVladimir Oltean 	if (err)
9271958d581SVladimir Oltean 		return err;
9281958d581SVladimir Oltean 
9291958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
930183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
931183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
93256051948SVladimir Oltean }
93356051948SVladimir Oltean 
93456051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
93556051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
93656051948SVladimir Oltean {
93756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
93856051948SVladimir Oltean 
939b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
94056051948SVladimir Oltean }
94156051948SVladimir Oltean 
94279fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
94379fda660SRussell King (Oracle) 				   struct phylink_config *config)
94479fda660SRussell King (Oracle) {
94579fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
94679fda660SRussell King (Oracle) 
947f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
948f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
949f6f04c02SRussell King (Oracle) 	 * as non-legacy.
950f6f04c02SRussell King (Oracle) 	 */
951f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
952f6f04c02SRussell King (Oracle) 
95379fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
95479fda660SRussell King (Oracle) 		  config->supported_interfaces);
95579fda660SRussell King (Oracle) }
95679fda660SRussell King (Oracle) 
957bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
958bdeced75SVladimir Oltean 				   unsigned long *supported,
959bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
960bdeced75SVladimir Oltean {
961bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
962375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
963bdeced75SVladimir Oltean 
964375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
965375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
966bdeced75SVladimir Oltean }
967bdeced75SVladimir Oltean 
968864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
969864ba485SRussell King (Oracle) 							int port,
970864ba485SRussell King (Oracle) 							phy_interface_t iface)
971bdeced75SVladimir Oltean {
972bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
973bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
974864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
975bdeced75SVladimir Oltean 
97649af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
977864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
978864ba485SRussell King (Oracle) 
979864ba485SRussell King (Oracle) 	return pcs;
980bdeced75SVladimir Oltean }
981bdeced75SVladimir Oltean 
982bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
983bdeced75SVladimir Oltean 					unsigned int link_an_mode,
984bdeced75SVladimir Oltean 					phy_interface_t interface)
985bdeced75SVladimir Oltean {
986bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
987bdeced75SVladimir Oltean 
988e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
989e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
990bdeced75SVladimir Oltean }
991bdeced75SVladimir Oltean 
992bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
993bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
994bdeced75SVladimir Oltean 				      phy_interface_t interface,
9955b502a7bSRussell King 				      struct phy_device *phydev,
9965b502a7bSRussell King 				      int speed, int duplex,
9975b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
998bdeced75SVladimir Oltean {
999bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10007e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1001bdeced75SVladimir Oltean 
1002e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1003e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1004e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10057e14a2dcSVladimir Oltean 
10067e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10077e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1008bdeced75SVladimir Oltean }
1009bdeced75SVladimir Oltean 
1010bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1011bd2b3161SXiaoliang Yang {
1012bd2b3161SXiaoliang Yang 	int i;
1013bd2b3161SXiaoliang Yang 
1014bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1015bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1016bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1017bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1018bd2b3161SXiaoliang Yang 		       port);
1019bd2b3161SXiaoliang Yang 
102070d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1021bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1022bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1023bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1024bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1025bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1026bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1027bd2b3161SXiaoliang Yang 			      port, i);
1028bd2b3161SXiaoliang Yang 	}
1029bd2b3161SXiaoliang Yang }
1030bd2b3161SXiaoliang Yang 
103156051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
103256051948SVladimir Oltean 			      u32 stringset, u8 *data)
103356051948SVladimir Oltean {
103456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103556051948SVladimir Oltean 
103656051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
103756051948SVladimir Oltean }
103856051948SVladimir Oltean 
103956051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
104056051948SVladimir Oltean {
104156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104256051948SVladimir Oltean 
104356051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
104456051948SVladimir Oltean }
104556051948SVladimir Oltean 
104656051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
104756051948SVladimir Oltean {
104856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104956051948SVladimir Oltean 
105056051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
105156051948SVladimir Oltean }
105256051948SVladimir Oltean 
105356051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
105456051948SVladimir Oltean 			     struct ethtool_ts_info *info)
105556051948SVladimir Oltean {
105656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105756051948SVladimir Oltean 
105856051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
105956051948SVladimir Oltean }
106056051948SVladimir Oltean 
1061acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1062acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1063acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1064acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1065acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1066acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1067acf242fcSColin Foster };
1068acf242fcSColin Foster 
1069acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1070acf242fcSColin Foster 				   phy_interface_t phy_mode)
1071acf242fcSColin Foster {
1072acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1073acf242fcSColin Foster 
1074acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1075acf242fcSColin Foster 		return 0;
1076acf242fcSColin Foster 	return -EOPNOTSUPP;
1077acf242fcSColin Foster }
1078acf242fcSColin Foster 
1079bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1080bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1081bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1082bdeced75SVladimir Oltean {
1083bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1084bdeced75SVladimir Oltean 	struct device_node *child;
1085bdeced75SVladimir Oltean 
108637fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1087bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1088bdeced75SVladimir Oltean 		u32 port;
1089bdeced75SVladimir Oltean 		int err;
1090bdeced75SVladimir Oltean 
1091bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1092bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1093bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1094bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1095bdeced75SVladimir Oltean 			of_node_put(child);
1096bdeced75SVladimir Oltean 			return -ENODEV;
1097bdeced75SVladimir Oltean 		}
1098bdeced75SVladimir Oltean 
1099bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1100bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1101bdeced75SVladimir Oltean 		if (err) {
1102bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1103bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1104bdeced75SVladimir Oltean 				port);
1105bdeced75SVladimir Oltean 			of_node_put(child);
1106bdeced75SVladimir Oltean 			return -ENODEV;
1107bdeced75SVladimir Oltean 		}
1108bdeced75SVladimir Oltean 
1109acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1110bdeced75SVladimir Oltean 		if (err < 0) {
1111bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1112bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
111359ebb430SSumera Priyadarsini 			of_node_put(child);
1114bdeced75SVladimir Oltean 			return err;
1115bdeced75SVladimir Oltean 		}
1116bdeced75SVladimir Oltean 
1117bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1118bdeced75SVladimir Oltean 	}
1119bdeced75SVladimir Oltean 
1120bdeced75SVladimir Oltean 	return 0;
1121bdeced75SVladimir Oltean }
1122bdeced75SVladimir Oltean 
1123bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1124bdeced75SVladimir Oltean {
1125bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1126bdeced75SVladimir Oltean 	struct device_node *switch_node;
1127bdeced75SVladimir Oltean 	struct device_node *ports_node;
1128bdeced75SVladimir Oltean 	int err;
1129bdeced75SVladimir Oltean 
1130bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1131bdeced75SVladimir Oltean 
1132bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1133abecbfcdSVladimir Oltean 	if (!ports_node)
1134abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1135bdeced75SVladimir Oltean 	if (!ports_node) {
1136abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1137bdeced75SVladimir Oltean 		return -ENODEV;
1138bdeced75SVladimir Oltean 	}
1139bdeced75SVladimir Oltean 
1140bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1141bdeced75SVladimir Oltean 	of_node_put(ports_node);
1142bdeced75SVladimir Oltean 
1143bdeced75SVladimir Oltean 	return err;
1144bdeced75SVladimir Oltean }
1145bdeced75SVladimir Oltean 
114656051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
114756051948SVladimir Oltean {
114856051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1149bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1150b4024c9eSClaudiu Manoil 	struct resource res;
115156051948SVladimir Oltean 	int port, i, err;
115256051948SVladimir Oltean 
115356051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
115456051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
115556051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
115656051948SVladimir Oltean 	if (!ocelot->ports)
115756051948SVladimir Oltean 		return -ENOMEM;
115856051948SVladimir Oltean 
115956051948SVladimir Oltean 	ocelot->map		= felix->info->map;
116056051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
116156051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
116221ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
116307d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
116477043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
116577043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
116677043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
116777043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
116856051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1169cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1170cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1171f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
117256051948SVladimir Oltean 
1173bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1174bdeced75SVladimir Oltean 				 GFP_KERNEL);
1175bdeced75SVladimir Oltean 	if (!port_phy_modes)
1176bdeced75SVladimir Oltean 		return -ENOMEM;
1177bdeced75SVladimir Oltean 
1178bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1179bdeced75SVladimir Oltean 	if (err) {
1180bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1181bdeced75SVladimir Oltean 		return err;
1182bdeced75SVladimir Oltean 	}
1183bdeced75SVladimir Oltean 
118456051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
118556051948SVladimir Oltean 		struct regmap *target;
118656051948SVladimir Oltean 
118756051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
118856051948SVladimir Oltean 			continue;
118956051948SVladimir Oltean 
1190b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1191b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1192375e1314SVladimir Oltean 		res.start += felix->switch_base;
1193375e1314SVladimir Oltean 		res.end += felix->switch_base;
119456051948SVladimir Oltean 
1195242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
119656051948SVladimir Oltean 		if (IS_ERR(target)) {
119756051948SVladimir Oltean 			dev_err(ocelot->dev,
119856051948SVladimir Oltean 				"Failed to map device memory space\n");
1199bdeced75SVladimir Oltean 			kfree(port_phy_modes);
120056051948SVladimir Oltean 			return PTR_ERR(target);
120156051948SVladimir Oltean 		}
120256051948SVladimir Oltean 
120356051948SVladimir Oltean 		ocelot->targets[i] = target;
120456051948SVladimir Oltean 	}
120556051948SVladimir Oltean 
120656051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
120756051948SVladimir Oltean 	if (err) {
120856051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1209bdeced75SVladimir Oltean 		kfree(port_phy_modes);
121056051948SVladimir Oltean 		return err;
121156051948SVladimir Oltean 	}
121256051948SVladimir Oltean 
121356051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
121456051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
121591c724cfSVladimir Oltean 		struct regmap *target;
121656051948SVladimir Oltean 
121756051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
121856051948SVladimir Oltean 					   sizeof(struct ocelot_port),
121956051948SVladimir Oltean 					   GFP_KERNEL);
122056051948SVladimir Oltean 		if (!ocelot_port) {
122156051948SVladimir Oltean 			dev_err(ocelot->dev,
122256051948SVladimir Oltean 				"failed to allocate port memory\n");
1223bdeced75SVladimir Oltean 			kfree(port_phy_modes);
122456051948SVladimir Oltean 			return -ENOMEM;
122556051948SVladimir Oltean 		}
122656051948SVladimir Oltean 
1227b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1228b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1229375e1314SVladimir Oltean 		res.start += felix->switch_base;
1230375e1314SVladimir Oltean 		res.end += felix->switch_base;
123156051948SVladimir Oltean 
1232242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
123391c724cfSVladimir Oltean 		if (IS_ERR(target)) {
123456051948SVladimir Oltean 			dev_err(ocelot->dev,
123591c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
123691c724cfSVladimir Oltean 				port);
1237bdeced75SVladimir Oltean 			kfree(port_phy_modes);
123891c724cfSVladimir Oltean 			return PTR_ERR(target);
123956051948SVladimir Oltean 		}
124056051948SVladimir Oltean 
1241bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
124256051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
124391c724cfSVladimir Oltean 		ocelot_port->target = target;
124456051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
124556051948SVladimir Oltean 	}
124656051948SVladimir Oltean 
1247bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1248bdeced75SVladimir Oltean 
1249bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1250bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1251bdeced75SVladimir Oltean 		if (err < 0)
1252bdeced75SVladimir Oltean 			return err;
1253bdeced75SVladimir Oltean 	}
1254bdeced75SVladimir Oltean 
125556051948SVladimir Oltean 	return 0;
125656051948SVladimir Oltean }
125756051948SVladimir Oltean 
12581328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12591328a883SVladimir Oltean 					   struct sk_buff *skb)
12601328a883SVladimir Oltean {
12611328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12621328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12631328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12641328a883SVladimir Oltean 	unsigned long flags;
12651328a883SVladimir Oltean 
12661328a883SVladimir Oltean 	if (!clone)
12671328a883SVladimir Oltean 		return;
12681328a883SVladimir Oltean 
12691328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12701328a883SVladimir Oltean 
12711328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12721328a883SVladimir Oltean 		if (skb != clone)
12731328a883SVladimir Oltean 			continue;
12741328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12751328a883SVladimir Oltean 		skb_match = skb;
12761328a883SVladimir Oltean 		break;
12771328a883SVladimir Oltean 	}
12781328a883SVladimir Oltean 
12791328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12801328a883SVladimir Oltean 
12811328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12821328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12831328a883SVladimir Oltean }
12841328a883SVladimir Oltean 
128549f885b2SVladimir Oltean #define work_to_xmit_work(w) \
128649f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
128749f885b2SVladimir Oltean 
128849f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
128949f885b2SVladimir Oltean {
129049f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
129149f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
129249f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
129349f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
129449f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
129549f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
129649f885b2SVladimir Oltean 	int retries = 10;
129749f885b2SVladimir Oltean 
129849f885b2SVladimir Oltean 	do {
129949f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
130049f885b2SVladimir Oltean 			break;
130149f885b2SVladimir Oltean 
130249f885b2SVladimir Oltean 		cpu_relax();
130349f885b2SVladimir Oltean 	} while (--retries);
130449f885b2SVladimir Oltean 
130549f885b2SVladimir Oltean 	if (!retries) {
130649f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
130749f885b2SVladimir Oltean 			port);
13081328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
130949f885b2SVladimir Oltean 		kfree_skb(skb);
131049f885b2SVladimir Oltean 		return;
131149f885b2SVladimir Oltean 	}
131249f885b2SVladimir Oltean 
131349f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
131449f885b2SVladimir Oltean 
131549f885b2SVladimir Oltean 	consume_skb(skb);
131649f885b2SVladimir Oltean 	kfree(xmit_work);
131749f885b2SVladimir Oltean }
131849f885b2SVladimir Oltean 
131935d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
132035d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
132149f885b2SVladimir Oltean {
132235d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
132349f885b2SVladimir Oltean 
132435d97680SVladimir Oltean 	switch (proto) {
132535d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
132635d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
132735d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
132849f885b2SVladimir Oltean 		return 0;
132935d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
133035d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
133149f885b2SVladimir Oltean 		return 0;
133235d97680SVladimir Oltean 	default:
133335d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
133449f885b2SVladimir Oltean 	}
133549f885b2SVladimir Oltean }
133649f885b2SVladimir Oltean 
133756051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
133856051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
133956051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
134056051948SVladimir Oltean  * (which will not work).
134156051948SVladimir Oltean  */
134256051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
134356051948SVladimir Oltean {
134456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
134556051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13462960bb14SVladimir Oltean 	struct dsa_port *dp;
13472960bb14SVladimir Oltean 	int err;
134856051948SVladimir Oltean 
134956051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
135056051948SVladimir Oltean 	if (err)
135156051948SVladimir Oltean 		return err;
135256051948SVladimir Oltean 
1353d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1354d1cc0e93SVladimir Oltean 	if (err)
13556b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1356d1cc0e93SVladimir Oltean 
13572b49d128SYangbo Lu 	if (ocelot->ptp) {
13582ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13592b49d128SYangbo Lu 		if (err) {
13602b49d128SYangbo Lu 			dev_err(ocelot->dev,
13612b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13622b49d128SYangbo Lu 			ocelot->ptp = 0;
13632b49d128SYangbo Lu 		}
13642b49d128SYangbo Lu 	}
136556051948SVladimir Oltean 
13662960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13672960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1368bd2b3161SXiaoliang Yang 
1369bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1370bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1371bd2b3161SXiaoliang Yang 		 */
13722960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
137356051948SVladimir Oltean 	}
137456051948SVladimir Oltean 
1375f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1376f59fd9caSVladimir Oltean 	if (err)
13776b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1378f59fd9caSVladimir Oltean 
13792960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1380adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1381adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
13821cf3299bSVladimir Oltean 		 */
1383*c69f40acSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto);
13848d5f7954SVladimir Oltean 		break;
1385adb3dccfSVladimir Oltean 	}
13861cf3299bSVladimir Oltean 
13870b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1388c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
138954c31984SVladimir Oltean 	ds->fdb_isolation = true;
139054c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1391bdeced75SVladimir Oltean 
139256051948SVladimir Oltean 	return 0;
13936b73b7c9SVladimir Oltean 
13946b73b7c9SVladimir Oltean out_deinit_ports:
13952960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
13962960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
13976b73b7c9SVladimir Oltean 
13986b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
13996b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
14006b73b7c9SVladimir Oltean 
14016b73b7c9SVladimir Oltean out_mdiobus_free:
14026b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14036b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14046b73b7c9SVladimir Oltean 
14056b73b7c9SVladimir Oltean 	return err;
140656051948SVladimir Oltean }
140756051948SVladimir Oltean 
140856051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
140956051948SVladimir Oltean {
141056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1411bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14122960bb14SVladimir Oltean 	struct dsa_port *dp;
1413bdeced75SVladimir Oltean 
14142960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14152960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14168d5f7954SVladimir Oltean 		break;
1417adb3dccfSVladimir Oltean 	}
1418adb3dccfSVladimir Oltean 
14192960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14202960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1421d19741b0SVladimir Oltean 
142249f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
142349f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
142449f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
142549f885b2SVladimir Oltean 
1426d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1427d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
142856051948SVladimir Oltean }
142956051948SVladimir Oltean 
1430c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1431c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1432c0bcf537SYangbo Lu {
1433c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1434c0bcf537SYangbo Lu 
1435c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1436c0bcf537SYangbo Lu }
1437c0bcf537SYangbo Lu 
1438c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1439c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1440c0bcf537SYangbo Lu {
1441c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
144299348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
144399348004SVladimir Oltean 	bool using_tag_8021q;
144499348004SVladimir Oltean 	int err;
1445c0bcf537SYangbo Lu 
144699348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
144799348004SVladimir Oltean 	if (err)
144899348004SVladimir Oltean 		return err;
144999348004SVladimir Oltean 
145099348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
145199348004SVladimir Oltean 
145299348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1453c0bcf537SYangbo Lu }
1454c0bcf537SYangbo Lu 
1455d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
14560a6f17c6SVladimir Oltean {
14570a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1458dbd03285SVladimir Oltean 	int err = 0, grp = 0;
14590a6f17c6SVladimir Oltean 
14600a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14610a6f17c6SVladimir Oltean 		return false;
14620a6f17c6SVladimir Oltean 
14630a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14640a6f17c6SVladimir Oltean 		return false;
14650a6f17c6SVladimir Oltean 
14660a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14670a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14680a6f17c6SVladimir Oltean 		unsigned int type;
14690a6f17c6SVladimir Oltean 
14700a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14710a6f17c6SVladimir Oltean 		if (err)
14720a6f17c6SVladimir Oltean 			goto out;
14730a6f17c6SVladimir Oltean 
14740a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
14750a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
14760a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
14770a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
14780a6f17c6SVladimir Oltean 		 * here and dropping those.
14790a6f17c6SVladimir Oltean 		 */
14800a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
14810a6f17c6SVladimir Oltean 
14820a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
14830a6f17c6SVladimir Oltean 
14840a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
14850a6f17c6SVladimir Oltean 
14860a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
14870a6f17c6SVladimir Oltean 			kfree_skb(skb);
14880a6f17c6SVladimir Oltean 			continue;
14890a6f17c6SVladimir Oltean 		}
14900a6f17c6SVladimir Oltean 
14910a6f17c6SVladimir Oltean 		netif_rx(skb);
14920a6f17c6SVladimir Oltean 	}
14930a6f17c6SVladimir Oltean 
14940a6f17c6SVladimir Oltean out:
14955d3bb7ddSVladimir Oltean 	if (err < 0) {
14965d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
14975d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
14985d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
14990a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
15005d3bb7ddSVladimir Oltean 	}
15010a6f17c6SVladimir Oltean 
15020a6f17c6SVladimir Oltean 	return true;
15030a6f17c6SVladimir Oltean }
15040a6f17c6SVladimir Oltean 
1505c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1506c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1507c0bcf537SYangbo Lu {
150892f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1509c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1510c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1511c0bcf537SYangbo Lu 	struct timespec64 ts;
151292f62485SVladimir Oltean 	u32 tstamp_hi;
151392f62485SVladimir Oltean 	u64 tstamp;
1514c0bcf537SYangbo Lu 
15150a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15160a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15170a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15180a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15190a6f17c6SVladimir Oltean 	 */
1520d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
15210a6f17c6SVladimir Oltean 		kfree_skb(skb);
15220a6f17c6SVladimir Oltean 		return true;
15230a6f17c6SVladimir Oltean 	}
15240a6f17c6SVladimir Oltean 
1525c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1526c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1527c0bcf537SYangbo Lu 
1528c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1529c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1530c0bcf537SYangbo Lu 		tstamp_hi--;
1531c0bcf537SYangbo Lu 
1532c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1533c0bcf537SYangbo Lu 
1534c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1535c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1536c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1537c0bcf537SYangbo Lu 	return false;
1538c0bcf537SYangbo Lu }
1539c0bcf537SYangbo Lu 
15405c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15415c5416f5SYangbo Lu 			   struct sk_buff *skb)
1542c0bcf537SYangbo Lu {
1543c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1544682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1545c0bcf537SYangbo Lu 
1546682eaad9SYangbo Lu 	if (!ocelot->ptp)
15475c5416f5SYangbo Lu 		return;
1548c0bcf537SYangbo Lu 
154952849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
155052849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
155152849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
155252849bcfSVladimir Oltean 				    port);
1553682eaad9SYangbo Lu 		return;
155452849bcfSVladimir Oltean 	}
1555682eaad9SYangbo Lu 
1556682eaad9SYangbo Lu 	if (clone)
1557c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15585c5416f5SYangbo Lu }
1559c0bcf537SYangbo Lu 
15600b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15610b912fc9SVladimir Oltean {
15620b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15630b912fc9SVladimir Oltean 
15640b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15650b912fc9SVladimir Oltean 
15660b912fc9SVladimir Oltean 	return 0;
15670b912fc9SVladimir Oltean }
15680b912fc9SVladimir Oltean 
15690b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15700b912fc9SVladimir Oltean {
15710b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15720b912fc9SVladimir Oltean 
15730b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15740b912fc9SVladimir Oltean }
15750b912fc9SVladimir Oltean 
157607d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
157707d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
157807d985eeSVladimir Oltean {
157907d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
158099348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
158199348004SVladimir Oltean 	bool using_tag_8021q;
158299348004SVladimir Oltean 	int err;
158307d985eeSVladimir Oltean 
158499348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
158599348004SVladimir Oltean 	if (err)
158699348004SVladimir Oltean 		return err;
158799348004SVladimir Oltean 
158899348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
158999348004SVladimir Oltean 
159099348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
159107d985eeSVladimir Oltean }
159207d985eeSVladimir Oltean 
159307d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
159407d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
159507d985eeSVladimir Oltean {
159607d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
159707d985eeSVladimir Oltean 
159807d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
159907d985eeSVladimir Oltean }
160007d985eeSVladimir Oltean 
160107d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
160207d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
160307d985eeSVladimir Oltean {
160407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
160507d985eeSVladimir Oltean 
160607d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
160707d985eeSVladimir Oltean }
160807d985eeSVladimir Oltean 
1609fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1610fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1611fc411eaaSVladimir Oltean {
1612fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1613fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1614fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16155f035af7SPo Liu 		.burst = policer->burst,
1616fc411eaaSVladimir Oltean 	};
1617fc411eaaSVladimir Oltean 
1618fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1619fc411eaaSVladimir Oltean }
1620fc411eaaSVladimir Oltean 
1621fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1622fc411eaaSVladimir Oltean {
1623fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1624fc411eaaSVladimir Oltean 
1625fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1626fc411eaaSVladimir Oltean }
1627fc411eaaSVladimir Oltean 
1628de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1629de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1630de143c0eSXiaoliang Yang 			       void *type_data)
1631de143c0eSXiaoliang Yang {
1632de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1633de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1634de143c0eSXiaoliang Yang 
1635de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1636de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1637de143c0eSXiaoliang Yang 	else
1638de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1639de143c0eSXiaoliang Yang }
1640de143c0eSXiaoliang Yang 
1641f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1642f59fd9caSVladimir Oltean 			     u16 pool_index,
1643f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1644f59fd9caSVladimir Oltean {
1645f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1646f59fd9caSVladimir Oltean 
1647f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1648f59fd9caSVladimir Oltean }
1649f59fd9caSVladimir Oltean 
1650f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1651f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1652f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1653f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1654f59fd9caSVladimir Oltean {
1655f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1656f59fd9caSVladimir Oltean 
1657f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1658f59fd9caSVladimir Oltean 				  threshold_type, extack);
1659f59fd9caSVladimir Oltean }
1660f59fd9caSVladimir Oltean 
1661f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1662f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1663f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1664f59fd9caSVladimir Oltean {
1665f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1666f59fd9caSVladimir Oltean 
1667f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1668f59fd9caSVladimir Oltean 				       p_threshold);
1669f59fd9caSVladimir Oltean }
1670f59fd9caSVladimir Oltean 
1671f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1672f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1673f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1674f59fd9caSVladimir Oltean {
1675f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1676f59fd9caSVladimir Oltean 
1677f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1678f59fd9caSVladimir Oltean 				       threshold, extack);
1679f59fd9caSVladimir Oltean }
1680f59fd9caSVladimir Oltean 
1681f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1682f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1683f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1684f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1685f59fd9caSVladimir Oltean {
1686f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1687f59fd9caSVladimir Oltean 
1688f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1689f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1690f59fd9caSVladimir Oltean 					  p_threshold);
1691f59fd9caSVladimir Oltean }
1692f59fd9caSVladimir Oltean 
1693f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1694f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1695f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1696f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1697f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1698f59fd9caSVladimir Oltean {
1699f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1700f59fd9caSVladimir Oltean 
1701f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1702f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1703f59fd9caSVladimir Oltean 					  extack);
1704f59fd9caSVladimir Oltean }
1705f59fd9caSVladimir Oltean 
1706f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1707f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1708f59fd9caSVladimir Oltean {
1709f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1710f59fd9caSVladimir Oltean 
1711f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1712f59fd9caSVladimir Oltean }
1713f59fd9caSVladimir Oltean 
1714f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1715f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1716f59fd9caSVladimir Oltean {
1717f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1718f59fd9caSVladimir Oltean 
1719f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1720f59fd9caSVladimir Oltean }
1721f59fd9caSVladimir Oltean 
1722f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1723f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1724f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1725f59fd9caSVladimir Oltean {
1726f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1727f59fd9caSVladimir Oltean 
1728f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1729f59fd9caSVladimir Oltean 					   p_cur, p_max);
1730f59fd9caSVladimir Oltean }
1731f59fd9caSVladimir Oltean 
1732f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1733f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1734f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1735f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1736f59fd9caSVladimir Oltean {
1737f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1738f59fd9caSVladimir Oltean 
1739f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1740f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1741f59fd9caSVladimir Oltean }
1742f59fd9caSVladimir Oltean 
1743a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1744a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1745a026c50bSHoratiu Vultur {
1746a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1747a026c50bSHoratiu Vultur 
1748a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1749a026c50bSHoratiu Vultur }
1750a026c50bSHoratiu Vultur 
1751a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1752a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1753a026c50bSHoratiu Vultur {
1754a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1755a026c50bSHoratiu Vultur 
1756a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1757a026c50bSHoratiu Vultur }
1758a026c50bSHoratiu Vultur 
1759a026c50bSHoratiu Vultur static int
1760a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1761a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1762a026c50bSHoratiu Vultur {
1763a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1764a026c50bSHoratiu Vultur 
1765a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1766a026c50bSHoratiu Vultur }
1767a026c50bSHoratiu Vultur 
1768a026c50bSHoratiu Vultur static int
1769a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1770a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1771a026c50bSHoratiu Vultur {
1772a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1773a026c50bSHoratiu Vultur 
1774a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1775a026c50bSHoratiu Vultur }
1776a026c50bSHoratiu Vultur 
1777375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
177856051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1779adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
178035d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
178156051948SVladimir Oltean 	.setup				= felix_setup,
178256051948SVladimir Oltean 	.teardown			= felix_teardown,
178356051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
178456051948SVladimir Oltean 	.get_strings			= felix_get_strings,
178556051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
178656051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
178756051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
178879fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1789bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1790864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1791bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1792bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
17935cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
179456051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
179556051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
179656051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1797961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1798961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1799209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1800209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1801421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1802421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
180356051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
180456051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18058fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18068fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18078fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
180856051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
180956051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
181056051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
181156051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1812c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1813c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1814c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1815c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18160b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18170b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1818fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1819fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
182007d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
182107d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
182207d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1823de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1824f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1825f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1826f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1827f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1828f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1829f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1830f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1831f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1832f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1833f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1834a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1835a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1836a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1837a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18385da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18395da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
184056051948SVladimir Oltean };
1841319e4dd1SVladimir Oltean 
1842319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1843319e4dd1SVladimir Oltean {
1844319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1845319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1846319e4dd1SVladimir Oltean 
1847319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1848319e4dd1SVladimir Oltean 		return NULL;
1849319e4dd1SVladimir Oltean 
1850319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1851319e4dd1SVladimir Oltean }
1852319e4dd1SVladimir Oltean 
1853319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1854319e4dd1SVladimir Oltean {
1855319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1856319e4dd1SVladimir Oltean 
1857319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1858319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1859319e4dd1SVladimir Oltean 		return -EINVAL;
1860319e4dd1SVladimir Oltean 
1861319e4dd1SVladimir Oltean 	return dp->index;
1862319e4dd1SVladimir Oltean }
1863