xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision d219b4b674e9424ff047ecc568b79401427ad709)
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 
386e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
38754c31984SVladimir Oltean 	ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
388e21268efSVladimir Oltean 
389e21268efSVladimir Oltean 	/* Restore PGID_CPU */
390e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
391e21268efSVladimir Oltean 			 PGID_CPU);
392e21268efSVladimir Oltean 
3938abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3948abe1970SVladimir Oltean 
3958abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
396e21268efSVladimir Oltean }
397e21268efSVladimir Oltean 
39899348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
39999348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
40099348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
401c8c0ba4fSVladimir Oltean  */
40299348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
40399348004SVladimir Oltean 					      bool using_tag_8021q)
404c8c0ba4fSVladimir Oltean {
40599348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
40699348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
40799348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
40899348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
40999348004SVladimir Oltean 	unsigned long port_mask;
4102960bb14SVladimir Oltean 	struct dsa_port *dp;
41199348004SVladimir Oltean 	bool cpu_copy_ena;
41299348004SVladimir Oltean 	int cpu = -1, err;
413c8c0ba4fSVladimir Oltean 
41499348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
41599348004SVladimir Oltean 		return 0;
416c8c0ba4fSVladimir Oltean 
41799348004SVladimir Oltean 	/* Figure out the current CPU port */
4182960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
4192960bb14SVladimir Oltean 		cpu = dp->index;
4208d5f7954SVladimir Oltean 		break;
421c8c0ba4fSVladimir Oltean 	}
4228d5f7954SVladimir Oltean 
423d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
424d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
425d78637a8SVladimir Oltean 	 */
426c8c0ba4fSVladimir Oltean 
42799348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
42899348004SVladimir Oltean 	list_for_each_entry(trap, &ocelot->traps, trap_list) {
42999348004SVladimir Oltean 		/* Figure out the current trapping destination */
43099348004SVladimir Oltean 		if (using_tag_8021q) {
43199348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
43299348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
43399348004SVladimir Oltean 			 * port module.
434c8c0ba4fSVladimir Oltean 			 */
43599348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
43699348004SVladimir Oltean 			port_mask = BIT(cpu);
43799348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
438c8c0ba4fSVladimir Oltean 		} else {
43999348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
44099348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
441c8c0ba4fSVladimir Oltean 			 */
44299348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
44399348004SVladimir Oltean 			port_mask = 0;
44499348004SVladimir Oltean 			cpu_copy_ena = true;
445c8c0ba4fSVladimir Oltean 		}
446c8c0ba4fSVladimir Oltean 
44799348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
44899348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
44999348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
45099348004SVladimir Oltean 			continue;
451c8c0ba4fSVladimir Oltean 
45299348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
45399348004SVladimir Oltean 		trap->action.port_mask = port_mask;
45499348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
4550a6f17c6SVladimir Oltean 
45699348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
457c8c0ba4fSVladimir Oltean 		if (err)
458c8c0ba4fSVladimir Oltean 			return err;
45999348004SVladimir Oltean 	}
460c8c0ba4fSVladimir Oltean 
46199348004SVladimir Oltean 	return 0;
462c8c0ba4fSVladimir Oltean }
463c8c0ba4fSVladimir Oltean 
464f9cef64fSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu, bool change)
465e21268efSVladimir Oltean {
466e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4672960bb14SVladimir Oltean 	struct dsa_port *dp;
4682960bb14SVladimir Oltean 	int err;
469e21268efSVladimir Oltean 
470e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
471e21268efSVladimir Oltean 
4722960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
473e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
474e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
475e21268efSVladimir Oltean 		 * for 2 reasons:
476e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
477e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
478e21268efSVladimir Oltean 		 *   into the system.
479e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
480e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
481e21268efSVladimir Oltean 		 *   port module.
482e21268efSVladimir Oltean 		 */
483e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
484e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4852960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
486e21268efSVladimir Oltean 	}
487e21268efSVladimir Oltean 
4885da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
489e21268efSVladimir Oltean 	if (err)
490d7b1fd52SVladimir Oltean 		return err;
491d7b1fd52SVladimir Oltean 
492f9cef64fSVladimir Oltean 	if (change) {
493f9cef64fSVladimir Oltean 		err = dsa_port_walk_fdbs(ds, cpu,
494f9cef64fSVladimir Oltean 					 felix_migrate_fdbs_to_tag_8021q_port);
495d7b1fd52SVladimir Oltean 		if (err)
496d7b1fd52SVladimir Oltean 			goto out_tag_8021q_unregister;
497e21268efSVladimir Oltean 
498f9cef64fSVladimir Oltean 		err = dsa_port_walk_mdbs(ds, cpu,
499f9cef64fSVladimir Oltean 					 felix_migrate_mdbs_to_tag_8021q_port);
500f9cef64fSVladimir Oltean 		if (err)
501f9cef64fSVladimir Oltean 			goto out_migrate_fdbs;
502b903a6bdSVladimir Oltean 
503b903a6bdSVladimir Oltean 		felix_migrate_flood_to_tag_8021q_port(ds, cpu);
504f9cef64fSVladimir Oltean 	}
505f9cef64fSVladimir Oltean 
506f9cef64fSVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
507f9cef64fSVladimir Oltean 	if (err)
508b903a6bdSVladimir Oltean 		goto out_migrate_flood;
509f9cef64fSVladimir Oltean 
51099348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
51199348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
51299348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
51399348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
51499348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
51599348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
51699348004SVladimir Oltean 	 */
51799348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
51899348004SVladimir Oltean 
519e21268efSVladimir Oltean 	return 0;
520e21268efSVladimir Oltean 
521b903a6bdSVladimir Oltean out_migrate_flood:
522b903a6bdSVladimir Oltean 	if (change)
523b903a6bdSVladimir Oltean 		felix_migrate_flood_to_npi_port(ds, cpu);
524f9cef64fSVladimir Oltean 	if (change)
525f9cef64fSVladimir Oltean 		dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
526f9cef64fSVladimir Oltean out_migrate_fdbs:
527f9cef64fSVladimir Oltean 	if (change)
528f9cef64fSVladimir Oltean 		dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
529d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
530d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
531e21268efSVladimir Oltean 	return err;
532e21268efSVladimir Oltean }
533e21268efSVladimir Oltean 
534e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
535e21268efSVladimir Oltean {
536e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5372960bb14SVladimir Oltean 	struct dsa_port *dp;
5382960bb14SVladimir Oltean 	int err;
539e21268efSVladimir Oltean 
54099348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
541c8c0ba4fSVladimir Oltean 	if (err)
542c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
543c8c0ba4fSVladimir Oltean 			err);
544c8c0ba4fSVladimir Oltean 
545d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
546e21268efSVladimir Oltean 
5472960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
548e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
549e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
550e21268efSVladimir Oltean 		 */
551e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
552e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
553e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
5542960bb14SVladimir Oltean 				 dp->index);
555e21268efSVladimir Oltean 	}
556e21268efSVladimir Oltean 
557e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
558e21268efSVladimir Oltean }
559e21268efSVladimir Oltean 
560adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
561adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
562adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
563adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
564adb3dccfSVladimir Oltean  * DSA master.
565adb3dccfSVladimir Oltean  */
566adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
567adb3dccfSVladimir Oltean {
568adb3dccfSVladimir Oltean 	ocelot->npi = port;
569adb3dccfSVladimir Oltean 
570adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
571adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
572adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
573adb3dccfSVladimir Oltean 
574adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
575adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
576adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
577adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
578adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
579adb3dccfSVladimir Oltean 
580adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
581adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
582adb3dccfSVladimir Oltean }
583adb3dccfSVladimir Oltean 
584adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
585adb3dccfSVladimir Oltean {
586adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
587adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
588adb3dccfSVladimir Oltean 
589adb3dccfSVladimir Oltean 	ocelot->npi = -1;
590adb3dccfSVladimir Oltean 
591adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
592adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
593adb3dccfSVladimir Oltean 
594adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
595adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
596adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
597adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
598adb3dccfSVladimir Oltean 
599adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
600adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
601adb3dccfSVladimir Oltean }
602adb3dccfSVladimir Oltean 
603f9cef64fSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu, bool change)
604adb3dccfSVladimir Oltean {
605adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
606f9cef64fSVladimir Oltean 	int err;
607f9cef64fSVladimir Oltean 
608f9cef64fSVladimir Oltean 	if (change) {
609f9cef64fSVladimir Oltean 		err = dsa_port_walk_fdbs(ds, cpu,
610f9cef64fSVladimir Oltean 					 felix_migrate_fdbs_to_npi_port);
611f9cef64fSVladimir Oltean 		if (err)
612f9cef64fSVladimir Oltean 			return err;
613f9cef64fSVladimir Oltean 
614f9cef64fSVladimir Oltean 		err = dsa_port_walk_mdbs(ds, cpu,
615f9cef64fSVladimir Oltean 					 felix_migrate_mdbs_to_npi_port);
616f9cef64fSVladimir Oltean 		if (err)
617f9cef64fSVladimir Oltean 			goto out_migrate_fdbs;
618b903a6bdSVladimir Oltean 
619b903a6bdSVladimir Oltean 		felix_migrate_flood_to_npi_port(ds, cpu);
620f9cef64fSVladimir Oltean 	}
621adb3dccfSVladimir Oltean 
622adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
623adb3dccfSVladimir Oltean 
624adb3dccfSVladimir Oltean 	return 0;
625f9cef64fSVladimir Oltean 
626f9cef64fSVladimir Oltean out_migrate_fdbs:
627f9cef64fSVladimir Oltean 	if (change)
628f9cef64fSVladimir Oltean 		dsa_port_walk_fdbs(ds, cpu,
629f9cef64fSVladimir Oltean 				   felix_migrate_fdbs_to_tag_8021q_port);
630f9cef64fSVladimir Oltean 
631f9cef64fSVladimir Oltean 	return err;
632adb3dccfSVladimir Oltean }
633adb3dccfSVladimir Oltean 
634adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
635adb3dccfSVladimir Oltean {
636adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
637adb3dccfSVladimir Oltean 
638adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
639adb3dccfSVladimir Oltean }
640adb3dccfSVladimir Oltean 
641adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
642f9cef64fSVladimir Oltean 				  enum dsa_tag_protocol proto, bool change)
643adb3dccfSVladimir Oltean {
644adb3dccfSVladimir Oltean 	int err;
645adb3dccfSVladimir Oltean 
646adb3dccfSVladimir Oltean 	switch (proto) {
6477c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
648adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
649f9cef64fSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu, change);
650adb3dccfSVladimir Oltean 		break;
651e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
652f9cef64fSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu, change);
653e21268efSVladimir Oltean 		break;
654adb3dccfSVladimir Oltean 	default:
655adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
656adb3dccfSVladimir Oltean 	}
657adb3dccfSVladimir Oltean 
658adb3dccfSVladimir Oltean 	return err;
659adb3dccfSVladimir Oltean }
660adb3dccfSVladimir Oltean 
661adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
662adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
663adb3dccfSVladimir Oltean {
664adb3dccfSVladimir Oltean 	switch (proto) {
6657c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
666adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
667adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
668adb3dccfSVladimir Oltean 		break;
669e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
670e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
671e21268efSVladimir Oltean 		break;
672adb3dccfSVladimir Oltean 	default:
673adb3dccfSVladimir Oltean 		break;
674adb3dccfSVladimir Oltean 	}
675adb3dccfSVladimir Oltean }
676adb3dccfSVladimir Oltean 
677e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
678e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
679e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
680e21268efSVladimir Oltean  */
681adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
682adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
683adb3dccfSVladimir Oltean {
684adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
685adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
686adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
687adb3dccfSVladimir Oltean 	int err;
688adb3dccfSVladimir Oltean 
6897c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
6907c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
691e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
692adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
693adb3dccfSVladimir Oltean 
694adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
695adb3dccfSVladimir Oltean 
696f9cef64fSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto, true);
697adb3dccfSVladimir Oltean 	if (err) {
698f9cef64fSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto, true);
699adb3dccfSVladimir Oltean 		return err;
700adb3dccfSVladimir Oltean 	}
701adb3dccfSVladimir Oltean 
702adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
703adb3dccfSVladimir Oltean 
704adb3dccfSVladimir Oltean 	return 0;
705adb3dccfSVladimir Oltean }
706adb3dccfSVladimir Oltean 
70756051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
7084d776482SFlorian Fainelli 						    int port,
7094d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
71056051948SVladimir Oltean {
711adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
712adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
713adb3dccfSVladimir Oltean 
714adb3dccfSVladimir Oltean 	return felix->tag_proto;
71556051948SVladimir Oltean }
71656051948SVladimir Oltean 
71756051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
71856051948SVladimir Oltean 				 unsigned int ageing_time)
71956051948SVladimir Oltean {
72056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72156051948SVladimir Oltean 
72256051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
72356051948SVladimir Oltean 
72456051948SVladimir Oltean 	return 0;
72556051948SVladimir Oltean }
72656051948SVladimir Oltean 
7275cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
7285cad43a5SVladimir Oltean {
7295cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7305cad43a5SVladimir Oltean 	int err;
7315cad43a5SVladimir Oltean 
7325cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
7335cad43a5SVladimir Oltean 	if (err)
7345cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
7355cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7365cad43a5SVladimir Oltean }
7375cad43a5SVladimir Oltean 
73856051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
73956051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
74056051948SVladimir Oltean {
74156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
74256051948SVladimir Oltean 
74356051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
74456051948SVladimir Oltean }
74556051948SVladimir Oltean 
74656051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
747c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
748c2693363SVladimir Oltean 			 struct dsa_db db)
74956051948SVladimir Oltean {
75054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
75156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
75256051948SVladimir Oltean 
75354c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
75454c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
75554c31984SVladimir Oltean 
75654c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
75756051948SVladimir Oltean }
75856051948SVladimir Oltean 
75956051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
760c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
761c2693363SVladimir Oltean 			 struct dsa_db db)
76256051948SVladimir Oltean {
76354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
76456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
76556051948SVladimir Oltean 
76654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
76754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76854c31984SVladimir Oltean 
76954c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
77056051948SVladimir Oltean }
77156051948SVladimir Oltean 
772961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
773c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
774c2693363SVladimir Oltean 			     struct dsa_db db)
775961d8b69SVladimir Oltean {
77654c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
777961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
778961d8b69SVladimir Oltean 
77954c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
78054c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
78154c31984SVladimir Oltean 
78254c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
783961d8b69SVladimir Oltean }
784961d8b69SVladimir Oltean 
785961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
786c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
787c2693363SVladimir Oltean 			     struct dsa_db db)
788961d8b69SVladimir Oltean {
78954c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
790961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
791961d8b69SVladimir Oltean 
79254c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
79354c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
79454c31984SVladimir Oltean 
79554c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
796961d8b69SVladimir Oltean }
797961d8b69SVladimir Oltean 
798a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
799c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
800c2693363SVladimir Oltean 			 struct dsa_db db)
801209edf95SVladimir Oltean {
80254c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
803209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
804209edf95SVladimir Oltean 
80554c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
80654c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
80754c31984SVladimir Oltean 
80854c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
809209edf95SVladimir Oltean }
810209edf95SVladimir Oltean 
811209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
812c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
813c2693363SVladimir Oltean 			 struct dsa_db db)
814209edf95SVladimir Oltean {
81554c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
816209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
817209edf95SVladimir Oltean 
81854c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
81954c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
82054c31984SVladimir Oltean 
82154c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
822209edf95SVladimir Oltean }
823209edf95SVladimir Oltean 
82456051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
82556051948SVladimir Oltean 				       u8 state)
82656051948SVladimir Oltean {
82756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
82856051948SVladimir Oltean 
82956051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
83056051948SVladimir Oltean }
83156051948SVladimir Oltean 
832421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
833421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
834421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
835421741eaSVladimir Oltean {
836421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
837421741eaSVladimir Oltean 
838421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
839421741eaSVladimir Oltean }
840421741eaSVladimir Oltean 
841421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
842421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
843421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
844421741eaSVladimir Oltean {
845421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
846421741eaSVladimir Oltean 
847421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
848421741eaSVladimir Oltean 
849421741eaSVladimir Oltean 	return 0;
850421741eaSVladimir Oltean }
851421741eaSVladimir Oltean 
85256051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
85306b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
85406b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
85556051948SVladimir Oltean {
85656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85756051948SVladimir Oltean 
85854c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
85954c31984SVladimir Oltean 				       extack);
86056051948SVladimir Oltean }
86156051948SVladimir Oltean 
86256051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
863d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
86456051948SVladimir Oltean {
86556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
86656051948SVladimir Oltean 
867d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
86856051948SVladimir Oltean }
86956051948SVladimir Oltean 
8708fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
871dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8728fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8738fe6832eSVladimir Oltean {
8748fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8758fe6832eSVladimir Oltean 
876dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8778fe6832eSVladimir Oltean }
8788fe6832eSVladimir Oltean 
8798fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
880dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8818fe6832eSVladimir Oltean {
8828fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8838fe6832eSVladimir Oltean 
884dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8858fe6832eSVladimir Oltean 
8868fe6832eSVladimir Oltean 	return 0;
8878fe6832eSVladimir Oltean }
8888fe6832eSVladimir Oltean 
8898fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8908fe6832eSVladimir Oltean {
8918fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8928fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8938fe6832eSVladimir Oltean 
8948fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8958fe6832eSVladimir Oltean 
8968fe6832eSVladimir Oltean 	return 0;
8978fe6832eSVladimir Oltean }
8988fe6832eSVladimir Oltean 
89956051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
90001af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
90101af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
90256051948SVladimir Oltean {
9032f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
904b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
9052f0402feSVladimir Oltean 
9069a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
9079a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
9089a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
9099a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
9109a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
9119a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
9129a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
9139a720680SVladimir Oltean 	 */
9149a720680SVladimir Oltean 	if (port == ocelot->npi)
9159a720680SVladimir Oltean 		return 0;
9169a720680SVladimir Oltean 
917b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9182f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
91901af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
92001af940eSVladimir Oltean 				   extack);
92156051948SVladimir Oltean }
92256051948SVladimir Oltean 
92389153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
92489153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
92556051948SVladimir Oltean {
92656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
92756051948SVladimir Oltean 
9283b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
92956051948SVladimir Oltean }
93056051948SVladimir Oltean 
9311958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
93231046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
93331046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
93456051948SVladimir Oltean {
93556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
936183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9371958d581SVladimir Oltean 	int err;
93856051948SVladimir Oltean 
93901af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9401958d581SVladimir Oltean 	if (err)
9411958d581SVladimir Oltean 		return err;
9421958d581SVladimir Oltean 
9431958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
944183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
945183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
94656051948SVladimir Oltean }
94756051948SVladimir Oltean 
94856051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
94956051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
95056051948SVladimir Oltean {
95156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
95256051948SVladimir Oltean 
953b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
95456051948SVladimir Oltean }
95556051948SVladimir Oltean 
95679fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
95779fda660SRussell King (Oracle) 				   struct phylink_config *config)
95879fda660SRussell King (Oracle) {
95979fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
96079fda660SRussell King (Oracle) 
961f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
962f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
963f6f04c02SRussell King (Oracle) 	 * as non-legacy.
964f6f04c02SRussell King (Oracle) 	 */
965f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
966f6f04c02SRussell King (Oracle) 
96779fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
96879fda660SRussell King (Oracle) 		  config->supported_interfaces);
96979fda660SRussell King (Oracle) }
97079fda660SRussell King (Oracle) 
971bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
972bdeced75SVladimir Oltean 				   unsigned long *supported,
973bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
974bdeced75SVladimir Oltean {
975bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
976375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
977bdeced75SVladimir Oltean 
978375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
979375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
980bdeced75SVladimir Oltean }
981bdeced75SVladimir Oltean 
982864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
983864ba485SRussell King (Oracle) 							int port,
984864ba485SRussell King (Oracle) 							phy_interface_t iface)
985bdeced75SVladimir Oltean {
986bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
987bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
988864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
989bdeced75SVladimir Oltean 
99049af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
991864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
992864ba485SRussell King (Oracle) 
993864ba485SRussell King (Oracle) 	return pcs;
994bdeced75SVladimir Oltean }
995bdeced75SVladimir Oltean 
996bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
997bdeced75SVladimir Oltean 					unsigned int link_an_mode,
998bdeced75SVladimir Oltean 					phy_interface_t interface)
999bdeced75SVladimir Oltean {
1000bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1001bdeced75SVladimir Oltean 
1002e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1003e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
1004bdeced75SVladimir Oltean }
1005bdeced75SVladimir Oltean 
1006bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
1007bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
1008bdeced75SVladimir Oltean 				      phy_interface_t interface,
10095b502a7bSRussell King 				      struct phy_device *phydev,
10105b502a7bSRussell King 				      int speed, int duplex,
10115b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
1012bdeced75SVladimir Oltean {
1013bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10147e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1015bdeced75SVladimir Oltean 
1016e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1017e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1018e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10197e14a2dcSVladimir Oltean 
10207e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10217e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1022bdeced75SVladimir Oltean }
1023bdeced75SVladimir Oltean 
1024bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1025bd2b3161SXiaoliang Yang {
1026bd2b3161SXiaoliang Yang 	int i;
1027bd2b3161SXiaoliang Yang 
1028bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1029bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1030bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1031bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1032bd2b3161SXiaoliang Yang 		       port);
1033bd2b3161SXiaoliang Yang 
103470d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1035bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1036bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1037bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1038bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1039bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1040bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1041bd2b3161SXiaoliang Yang 			      port, i);
1042bd2b3161SXiaoliang Yang 	}
1043bd2b3161SXiaoliang Yang }
1044bd2b3161SXiaoliang Yang 
104556051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
104656051948SVladimir Oltean 			      u32 stringset, u8 *data)
104756051948SVladimir Oltean {
104856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104956051948SVladimir Oltean 
105056051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
105156051948SVladimir Oltean }
105256051948SVladimir Oltean 
105356051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
105456051948SVladimir Oltean {
105556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105656051948SVladimir Oltean 
105756051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
105856051948SVladimir Oltean }
105956051948SVladimir Oltean 
106056051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
106156051948SVladimir Oltean {
106256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
106356051948SVladimir Oltean 
106456051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
106556051948SVladimir Oltean }
106656051948SVladimir Oltean 
106756051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
106856051948SVladimir Oltean 			     struct ethtool_ts_info *info)
106956051948SVladimir Oltean {
107056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
107156051948SVladimir Oltean 
107256051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
107356051948SVladimir Oltean }
107456051948SVladimir Oltean 
1075acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1076acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1077acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1078acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1079acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1080acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1081acf242fcSColin Foster };
1082acf242fcSColin Foster 
1083acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1084acf242fcSColin Foster 				   phy_interface_t phy_mode)
1085acf242fcSColin Foster {
1086acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1087acf242fcSColin Foster 
1088acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1089acf242fcSColin Foster 		return 0;
1090acf242fcSColin Foster 	return -EOPNOTSUPP;
1091acf242fcSColin Foster }
1092acf242fcSColin Foster 
1093bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1094bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1095bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1096bdeced75SVladimir Oltean {
1097bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1098bdeced75SVladimir Oltean 	struct device_node *child;
1099bdeced75SVladimir Oltean 
110037fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1101bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1102bdeced75SVladimir Oltean 		u32 port;
1103bdeced75SVladimir Oltean 		int err;
1104bdeced75SVladimir Oltean 
1105bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1106bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1107bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1108bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1109bdeced75SVladimir Oltean 			of_node_put(child);
1110bdeced75SVladimir Oltean 			return -ENODEV;
1111bdeced75SVladimir Oltean 		}
1112bdeced75SVladimir Oltean 
1113bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1114bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1115bdeced75SVladimir Oltean 		if (err) {
1116bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1117bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1118bdeced75SVladimir Oltean 				port);
1119bdeced75SVladimir Oltean 			of_node_put(child);
1120bdeced75SVladimir Oltean 			return -ENODEV;
1121bdeced75SVladimir Oltean 		}
1122bdeced75SVladimir Oltean 
1123acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1124bdeced75SVladimir Oltean 		if (err < 0) {
1125bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1126bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
112759ebb430SSumera Priyadarsini 			of_node_put(child);
1128bdeced75SVladimir Oltean 			return err;
1129bdeced75SVladimir Oltean 		}
1130bdeced75SVladimir Oltean 
1131bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1132bdeced75SVladimir Oltean 	}
1133bdeced75SVladimir Oltean 
1134bdeced75SVladimir Oltean 	return 0;
1135bdeced75SVladimir Oltean }
1136bdeced75SVladimir Oltean 
1137bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1138bdeced75SVladimir Oltean {
1139bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1140bdeced75SVladimir Oltean 	struct device_node *switch_node;
1141bdeced75SVladimir Oltean 	struct device_node *ports_node;
1142bdeced75SVladimir Oltean 	int err;
1143bdeced75SVladimir Oltean 
1144bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1145bdeced75SVladimir Oltean 
1146bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1147abecbfcdSVladimir Oltean 	if (!ports_node)
1148abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1149bdeced75SVladimir Oltean 	if (!ports_node) {
1150abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1151bdeced75SVladimir Oltean 		return -ENODEV;
1152bdeced75SVladimir Oltean 	}
1153bdeced75SVladimir Oltean 
1154bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1155bdeced75SVladimir Oltean 	of_node_put(ports_node);
1156bdeced75SVladimir Oltean 
1157bdeced75SVladimir Oltean 	return err;
1158bdeced75SVladimir Oltean }
1159bdeced75SVladimir Oltean 
116056051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
116156051948SVladimir Oltean {
116256051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1163bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1164b4024c9eSClaudiu Manoil 	struct resource res;
116556051948SVladimir Oltean 	int port, i, err;
116656051948SVladimir Oltean 
116756051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
116856051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
116956051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
117056051948SVladimir Oltean 	if (!ocelot->ports)
117156051948SVladimir Oltean 		return -ENOMEM;
117256051948SVladimir Oltean 
117356051948SVladimir Oltean 	ocelot->map		= felix->info->map;
117456051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
117556051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
117621ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
117707d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
117877043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
117977043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
118077043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
118177043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
118256051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1183cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1184cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1185f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
118656051948SVladimir Oltean 
1187bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1188bdeced75SVladimir Oltean 				 GFP_KERNEL);
1189bdeced75SVladimir Oltean 	if (!port_phy_modes)
1190bdeced75SVladimir Oltean 		return -ENOMEM;
1191bdeced75SVladimir Oltean 
1192bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1193bdeced75SVladimir Oltean 	if (err) {
1194bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1195bdeced75SVladimir Oltean 		return err;
1196bdeced75SVladimir Oltean 	}
1197bdeced75SVladimir Oltean 
119856051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
119956051948SVladimir Oltean 		struct regmap *target;
120056051948SVladimir Oltean 
120156051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
120256051948SVladimir Oltean 			continue;
120356051948SVladimir Oltean 
1204b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1205b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1206375e1314SVladimir Oltean 		res.start += felix->switch_base;
1207375e1314SVladimir Oltean 		res.end += felix->switch_base;
120856051948SVladimir Oltean 
1209242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
121056051948SVladimir Oltean 		if (IS_ERR(target)) {
121156051948SVladimir Oltean 			dev_err(ocelot->dev,
121256051948SVladimir Oltean 				"Failed to map device memory space\n");
1213bdeced75SVladimir Oltean 			kfree(port_phy_modes);
121456051948SVladimir Oltean 			return PTR_ERR(target);
121556051948SVladimir Oltean 		}
121656051948SVladimir Oltean 
121756051948SVladimir Oltean 		ocelot->targets[i] = target;
121856051948SVladimir Oltean 	}
121956051948SVladimir Oltean 
122056051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
122156051948SVladimir Oltean 	if (err) {
122256051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1223bdeced75SVladimir Oltean 		kfree(port_phy_modes);
122456051948SVladimir Oltean 		return err;
122556051948SVladimir Oltean 	}
122656051948SVladimir Oltean 
122756051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
122856051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
122991c724cfSVladimir Oltean 		struct regmap *target;
123056051948SVladimir Oltean 
123156051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
123256051948SVladimir Oltean 					   sizeof(struct ocelot_port),
123356051948SVladimir Oltean 					   GFP_KERNEL);
123456051948SVladimir Oltean 		if (!ocelot_port) {
123556051948SVladimir Oltean 			dev_err(ocelot->dev,
123656051948SVladimir Oltean 				"failed to allocate port memory\n");
1237bdeced75SVladimir Oltean 			kfree(port_phy_modes);
123856051948SVladimir Oltean 			return -ENOMEM;
123956051948SVladimir Oltean 		}
124056051948SVladimir Oltean 
1241b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1242b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1243375e1314SVladimir Oltean 		res.start += felix->switch_base;
1244375e1314SVladimir Oltean 		res.end += felix->switch_base;
124556051948SVladimir Oltean 
1246242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
124791c724cfSVladimir Oltean 		if (IS_ERR(target)) {
124856051948SVladimir Oltean 			dev_err(ocelot->dev,
124991c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
125091c724cfSVladimir Oltean 				port);
1251bdeced75SVladimir Oltean 			kfree(port_phy_modes);
125291c724cfSVladimir Oltean 			return PTR_ERR(target);
125356051948SVladimir Oltean 		}
125456051948SVladimir Oltean 
1255bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
125656051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
125791c724cfSVladimir Oltean 		ocelot_port->target = target;
125856051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
125956051948SVladimir Oltean 	}
126056051948SVladimir Oltean 
1261bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1262bdeced75SVladimir Oltean 
1263bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1264bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1265bdeced75SVladimir Oltean 		if (err < 0)
1266bdeced75SVladimir Oltean 			return err;
1267bdeced75SVladimir Oltean 	}
1268bdeced75SVladimir Oltean 
126956051948SVladimir Oltean 	return 0;
127056051948SVladimir Oltean }
127156051948SVladimir Oltean 
12721328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12731328a883SVladimir Oltean 					   struct sk_buff *skb)
12741328a883SVladimir Oltean {
12751328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12761328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12771328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12781328a883SVladimir Oltean 	unsigned long flags;
12791328a883SVladimir Oltean 
12801328a883SVladimir Oltean 	if (!clone)
12811328a883SVladimir Oltean 		return;
12821328a883SVladimir Oltean 
12831328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12841328a883SVladimir Oltean 
12851328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12861328a883SVladimir Oltean 		if (skb != clone)
12871328a883SVladimir Oltean 			continue;
12881328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12891328a883SVladimir Oltean 		skb_match = skb;
12901328a883SVladimir Oltean 		break;
12911328a883SVladimir Oltean 	}
12921328a883SVladimir Oltean 
12931328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12941328a883SVladimir Oltean 
12951328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12961328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12971328a883SVladimir Oltean }
12981328a883SVladimir Oltean 
129949f885b2SVladimir Oltean #define work_to_xmit_work(w) \
130049f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
130149f885b2SVladimir Oltean 
130249f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
130349f885b2SVladimir Oltean {
130449f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
130549f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
130649f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
130749f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
130849f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
130949f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
131049f885b2SVladimir Oltean 	int retries = 10;
131149f885b2SVladimir Oltean 
131249f885b2SVladimir Oltean 	do {
131349f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
131449f885b2SVladimir Oltean 			break;
131549f885b2SVladimir Oltean 
131649f885b2SVladimir Oltean 		cpu_relax();
131749f885b2SVladimir Oltean 	} while (--retries);
131849f885b2SVladimir Oltean 
131949f885b2SVladimir Oltean 	if (!retries) {
132049f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
132149f885b2SVladimir Oltean 			port);
13221328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
132349f885b2SVladimir Oltean 		kfree_skb(skb);
132449f885b2SVladimir Oltean 		return;
132549f885b2SVladimir Oltean 	}
132649f885b2SVladimir Oltean 
132749f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
132849f885b2SVladimir Oltean 
132949f885b2SVladimir Oltean 	consume_skb(skb);
133049f885b2SVladimir Oltean 	kfree(xmit_work);
133149f885b2SVladimir Oltean }
133249f885b2SVladimir Oltean 
133335d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
133435d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
133549f885b2SVladimir Oltean {
133635d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
133749f885b2SVladimir Oltean 
133835d97680SVladimir Oltean 	switch (proto) {
133935d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
134035d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
134135d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
134249f885b2SVladimir Oltean 		return 0;
134335d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
134435d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
134549f885b2SVladimir Oltean 		return 0;
134635d97680SVladimir Oltean 	default:
134735d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
134849f885b2SVladimir Oltean 	}
134949f885b2SVladimir Oltean }
135049f885b2SVladimir Oltean 
135156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
135256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
135356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
135456051948SVladimir Oltean  * (which will not work).
135556051948SVladimir Oltean  */
135656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
135756051948SVladimir Oltean {
135856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
135956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13602960bb14SVladimir Oltean 	struct dsa_port *dp;
13612960bb14SVladimir Oltean 	int err;
136256051948SVladimir Oltean 
136356051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
136456051948SVladimir Oltean 	if (err)
136556051948SVladimir Oltean 		return err;
136656051948SVladimir Oltean 
1367d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1368d1cc0e93SVladimir Oltean 	if (err)
13696b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1370d1cc0e93SVladimir Oltean 
13712b49d128SYangbo Lu 	if (ocelot->ptp) {
13722ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13732b49d128SYangbo Lu 		if (err) {
13742b49d128SYangbo Lu 			dev_err(ocelot->dev,
13752b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13762b49d128SYangbo Lu 			ocelot->ptp = 0;
13772b49d128SYangbo Lu 		}
13782b49d128SYangbo Lu 	}
137956051948SVladimir Oltean 
13802960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13812960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1382bd2b3161SXiaoliang Yang 
1383bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1384bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1385bd2b3161SXiaoliang Yang 		 */
13862960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
138756051948SVladimir Oltean 	}
138856051948SVladimir Oltean 
1389f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1390f59fd9caSVladimir Oltean 	if (err)
13916b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1392f59fd9caSVladimir Oltean 
13932960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1394adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1395adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
13961cf3299bSVladimir Oltean 		 */
1397f9cef64fSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto, false);
13988d5f7954SVladimir Oltean 		break;
1399adb3dccfSVladimir Oltean 	}
14001cf3299bSVladimir Oltean 
14010b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1402c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
140354c31984SVladimir Oltean 	ds->fdb_isolation = true;
140454c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1405bdeced75SVladimir Oltean 
140656051948SVladimir Oltean 	return 0;
14076b73b7c9SVladimir Oltean 
14086b73b7c9SVladimir Oltean out_deinit_ports:
14092960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14102960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
14116b73b7c9SVladimir Oltean 
14126b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
14136b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
14146b73b7c9SVladimir Oltean 
14156b73b7c9SVladimir Oltean out_mdiobus_free:
14166b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14176b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14186b73b7c9SVladimir Oltean 
14196b73b7c9SVladimir Oltean 	return err;
142056051948SVladimir Oltean }
142156051948SVladimir Oltean 
142256051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
142356051948SVladimir Oltean {
142456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1425bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14262960bb14SVladimir Oltean 	struct dsa_port *dp;
1427bdeced75SVladimir Oltean 
14282960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14292960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14308d5f7954SVladimir Oltean 		break;
1431adb3dccfSVladimir Oltean 	}
1432adb3dccfSVladimir Oltean 
14332960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14342960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1435d19741b0SVladimir Oltean 
143649f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
143749f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
143849f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
143949f885b2SVladimir Oltean 
1440d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1441d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
144256051948SVladimir Oltean }
144356051948SVladimir Oltean 
1444c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1445c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1446c0bcf537SYangbo Lu {
1447c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1448c0bcf537SYangbo Lu 
1449c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1450c0bcf537SYangbo Lu }
1451c0bcf537SYangbo Lu 
1452c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1453c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1454c0bcf537SYangbo Lu {
1455c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
145699348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
145799348004SVladimir Oltean 	bool using_tag_8021q;
145899348004SVladimir Oltean 	int err;
1459c0bcf537SYangbo Lu 
146099348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
146199348004SVladimir Oltean 	if (err)
146299348004SVladimir Oltean 		return err;
146399348004SVladimir Oltean 
146499348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
146599348004SVladimir Oltean 
146699348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1467c0bcf537SYangbo Lu }
1468c0bcf537SYangbo Lu 
1469*d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
14700a6f17c6SVladimir Oltean {
14710a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14720a6f17c6SVladimir Oltean 	int err, grp = 0;
14730a6f17c6SVladimir Oltean 
14740a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14750a6f17c6SVladimir Oltean 		return false;
14760a6f17c6SVladimir Oltean 
14770a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14780a6f17c6SVladimir Oltean 		return false;
14790a6f17c6SVladimir Oltean 
14800a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14810a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14820a6f17c6SVladimir Oltean 		unsigned int type;
14830a6f17c6SVladimir Oltean 
14840a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14850a6f17c6SVladimir Oltean 		if (err)
14860a6f17c6SVladimir Oltean 			goto out;
14870a6f17c6SVladimir Oltean 
14880a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
14890a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
14900a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
14910a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
14920a6f17c6SVladimir Oltean 		 * here and dropping those.
14930a6f17c6SVladimir Oltean 		 */
14940a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
14950a6f17c6SVladimir Oltean 
14960a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
14970a6f17c6SVladimir Oltean 
14980a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
14990a6f17c6SVladimir Oltean 
15000a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
15010a6f17c6SVladimir Oltean 			kfree_skb(skb);
15020a6f17c6SVladimir Oltean 			continue;
15030a6f17c6SVladimir Oltean 		}
15040a6f17c6SVladimir Oltean 
15050a6f17c6SVladimir Oltean 		netif_rx(skb);
15060a6f17c6SVladimir Oltean 	}
15070a6f17c6SVladimir Oltean 
15080a6f17c6SVladimir Oltean out:
15090a6f17c6SVladimir Oltean 	if (err < 0)
15100a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
15110a6f17c6SVladimir Oltean 
15120a6f17c6SVladimir Oltean 	return true;
15130a6f17c6SVladimir Oltean }
15140a6f17c6SVladimir Oltean 
1515c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1516c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1517c0bcf537SYangbo Lu {
151892f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1519c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1520c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1521c0bcf537SYangbo Lu 	struct timespec64 ts;
152292f62485SVladimir Oltean 	u32 tstamp_hi;
152392f62485SVladimir Oltean 	u64 tstamp;
1524c0bcf537SYangbo Lu 
15250a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15260a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15270a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15280a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15290a6f17c6SVladimir Oltean 	 */
1530*d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
15310a6f17c6SVladimir Oltean 		kfree_skb(skb);
15320a6f17c6SVladimir Oltean 		return true;
15330a6f17c6SVladimir Oltean 	}
15340a6f17c6SVladimir Oltean 
1535c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1536c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1537c0bcf537SYangbo Lu 
1538c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1539c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1540c0bcf537SYangbo Lu 		tstamp_hi--;
1541c0bcf537SYangbo Lu 
1542c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1543c0bcf537SYangbo Lu 
1544c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1545c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1546c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1547c0bcf537SYangbo Lu 	return false;
1548c0bcf537SYangbo Lu }
1549c0bcf537SYangbo Lu 
15505c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15515c5416f5SYangbo Lu 			   struct sk_buff *skb)
1552c0bcf537SYangbo Lu {
1553c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1554682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1555c0bcf537SYangbo Lu 
1556682eaad9SYangbo Lu 	if (!ocelot->ptp)
15575c5416f5SYangbo Lu 		return;
1558c0bcf537SYangbo Lu 
155952849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
156052849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
156152849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
156252849bcfSVladimir Oltean 				    port);
1563682eaad9SYangbo Lu 		return;
156452849bcfSVladimir Oltean 	}
1565682eaad9SYangbo Lu 
1566682eaad9SYangbo Lu 	if (clone)
1567c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15685c5416f5SYangbo Lu }
1569c0bcf537SYangbo Lu 
15700b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15710b912fc9SVladimir Oltean {
15720b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15730b912fc9SVladimir Oltean 
15740b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15750b912fc9SVladimir Oltean 
15760b912fc9SVladimir Oltean 	return 0;
15770b912fc9SVladimir Oltean }
15780b912fc9SVladimir Oltean 
15790b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15800b912fc9SVladimir Oltean {
15810b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15820b912fc9SVladimir Oltean 
15830b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15840b912fc9SVladimir Oltean }
15850b912fc9SVladimir Oltean 
158607d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
158707d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
158807d985eeSVladimir Oltean {
158907d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
159099348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
159199348004SVladimir Oltean 	bool using_tag_8021q;
159299348004SVladimir Oltean 	int err;
159307d985eeSVladimir Oltean 
159499348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
159599348004SVladimir Oltean 	if (err)
159699348004SVladimir Oltean 		return err;
159799348004SVladimir Oltean 
159899348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
159999348004SVladimir Oltean 
160099348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
160107d985eeSVladimir Oltean }
160207d985eeSVladimir Oltean 
160307d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
160407d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
160507d985eeSVladimir Oltean {
160607d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
160707d985eeSVladimir Oltean 
160807d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
160907d985eeSVladimir Oltean }
161007d985eeSVladimir Oltean 
161107d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
161207d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
161307d985eeSVladimir Oltean {
161407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
161507d985eeSVladimir Oltean 
161607d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
161707d985eeSVladimir Oltean }
161807d985eeSVladimir Oltean 
1619fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1620fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1621fc411eaaSVladimir Oltean {
1622fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1623fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1624fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16255f035af7SPo Liu 		.burst = policer->burst,
1626fc411eaaSVladimir Oltean 	};
1627fc411eaaSVladimir Oltean 
1628fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1629fc411eaaSVladimir Oltean }
1630fc411eaaSVladimir Oltean 
1631fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1632fc411eaaSVladimir Oltean {
1633fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1634fc411eaaSVladimir Oltean 
1635fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1636fc411eaaSVladimir Oltean }
1637fc411eaaSVladimir Oltean 
1638de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1639de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1640de143c0eSXiaoliang Yang 			       void *type_data)
1641de143c0eSXiaoliang Yang {
1642de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1643de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1644de143c0eSXiaoliang Yang 
1645de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1646de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1647de143c0eSXiaoliang Yang 	else
1648de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1649de143c0eSXiaoliang Yang }
1650de143c0eSXiaoliang Yang 
1651f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1652f59fd9caSVladimir Oltean 			     u16 pool_index,
1653f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1654f59fd9caSVladimir Oltean {
1655f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1656f59fd9caSVladimir Oltean 
1657f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1658f59fd9caSVladimir Oltean }
1659f59fd9caSVladimir Oltean 
1660f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1661f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1662f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1663f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1664f59fd9caSVladimir Oltean {
1665f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1666f59fd9caSVladimir Oltean 
1667f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1668f59fd9caSVladimir Oltean 				  threshold_type, extack);
1669f59fd9caSVladimir Oltean }
1670f59fd9caSVladimir Oltean 
1671f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1672f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1673f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1674f59fd9caSVladimir Oltean {
1675f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1676f59fd9caSVladimir Oltean 
1677f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1678f59fd9caSVladimir Oltean 				       p_threshold);
1679f59fd9caSVladimir Oltean }
1680f59fd9caSVladimir Oltean 
1681f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1682f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1683f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1684f59fd9caSVladimir Oltean {
1685f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1686f59fd9caSVladimir Oltean 
1687f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1688f59fd9caSVladimir Oltean 				       threshold, extack);
1689f59fd9caSVladimir Oltean }
1690f59fd9caSVladimir Oltean 
1691f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1692f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1693f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1694f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1695f59fd9caSVladimir Oltean {
1696f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1697f59fd9caSVladimir Oltean 
1698f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1699f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1700f59fd9caSVladimir Oltean 					  p_threshold);
1701f59fd9caSVladimir Oltean }
1702f59fd9caSVladimir Oltean 
1703f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1704f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1705f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1706f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1707f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1708f59fd9caSVladimir Oltean {
1709f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1710f59fd9caSVladimir Oltean 
1711f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1712f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1713f59fd9caSVladimir Oltean 					  extack);
1714f59fd9caSVladimir Oltean }
1715f59fd9caSVladimir Oltean 
1716f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1717f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1718f59fd9caSVladimir Oltean {
1719f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1720f59fd9caSVladimir Oltean 
1721f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1722f59fd9caSVladimir Oltean }
1723f59fd9caSVladimir Oltean 
1724f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1725f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1726f59fd9caSVladimir Oltean {
1727f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1728f59fd9caSVladimir Oltean 
1729f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1730f59fd9caSVladimir Oltean }
1731f59fd9caSVladimir Oltean 
1732f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1733f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1734f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1735f59fd9caSVladimir Oltean {
1736f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1737f59fd9caSVladimir Oltean 
1738f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1739f59fd9caSVladimir Oltean 					   p_cur, p_max);
1740f59fd9caSVladimir Oltean }
1741f59fd9caSVladimir Oltean 
1742f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1743f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1744f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1745f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1746f59fd9caSVladimir Oltean {
1747f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1748f59fd9caSVladimir Oltean 
1749f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1750f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1751f59fd9caSVladimir Oltean }
1752f59fd9caSVladimir Oltean 
1753a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1754a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1755a026c50bSHoratiu Vultur {
1756a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1757a026c50bSHoratiu Vultur 
1758a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1759a026c50bSHoratiu Vultur }
1760a026c50bSHoratiu Vultur 
1761a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1762a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1763a026c50bSHoratiu Vultur {
1764a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1765a026c50bSHoratiu Vultur 
1766a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1767a026c50bSHoratiu Vultur }
1768a026c50bSHoratiu Vultur 
1769a026c50bSHoratiu Vultur static int
1770a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1771a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1772a026c50bSHoratiu Vultur {
1773a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1774a026c50bSHoratiu Vultur 
1775a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1776a026c50bSHoratiu Vultur }
1777a026c50bSHoratiu Vultur 
1778a026c50bSHoratiu Vultur static int
1779a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1780a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1781a026c50bSHoratiu Vultur {
1782a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1783a026c50bSHoratiu Vultur 
1784a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1785a026c50bSHoratiu Vultur }
1786a026c50bSHoratiu Vultur 
1787375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
178856051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1789adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
179035d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
179156051948SVladimir Oltean 	.setup				= felix_setup,
179256051948SVladimir Oltean 	.teardown			= felix_teardown,
179356051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
179456051948SVladimir Oltean 	.get_strings			= felix_get_strings,
179556051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
179656051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
179756051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
179879fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1799bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1800864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1801bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1802bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18035cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
180456051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
180556051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
180656051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1807961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1808961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1809209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1810209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1811421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1812421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
181356051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
181456051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18158fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18168fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18178fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
181856051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
181956051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
182056051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
182156051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1822c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1823c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1824c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1825c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18260b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18270b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1828fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1829fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
183007d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
183107d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
183207d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1833de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1834f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1835f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1836f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1837f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1838f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1839f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1840f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1841f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1842f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1843f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1844a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1845a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1846a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1847a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18485da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18495da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
185056051948SVladimir Oltean };
1851319e4dd1SVladimir Oltean 
1852319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1853319e4dd1SVladimir Oltean {
1854319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1855319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1856319e4dd1SVladimir Oltean 
1857319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1858319e4dd1SVladimir Oltean 		return NULL;
1859319e4dd1SVladimir Oltean 
1860319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1861319e4dd1SVladimir Oltean }
1862319e4dd1SVladimir Oltean 
1863319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1864319e4dd1SVladimir Oltean {
1865319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1866319e4dd1SVladimir Oltean 
1867319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1868319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1869319e4dd1SVladimir Oltean 		return -EINVAL;
1870319e4dd1SVladimir Oltean 
1871319e4dd1SVladimir Oltean 	return dp->index;
1872319e4dd1SVladimir Oltean }
1873