xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision b903a6bd2e1921598d569be4da91f5927745382b)
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 
83*b903a6bdSVladimir Oltean static void felix_migrate_pgid_bit(struct dsa_switch *ds, int from, int to,
84*b903a6bdSVladimir Oltean 				   int pgid)
85*b903a6bdSVladimir Oltean {
86*b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
87*b903a6bdSVladimir Oltean 	bool on;
88*b903a6bdSVladimir Oltean 	u32 val;
89*b903a6bdSVladimir Oltean 
90*b903a6bdSVladimir Oltean 	val = ocelot_read_rix(ocelot, ANA_PGID_PGID, pgid);
91*b903a6bdSVladimir Oltean 	on = !!(val & BIT(from));
92*b903a6bdSVladimir Oltean 	val &= ~BIT(from);
93*b903a6bdSVladimir Oltean 	if (on)
94*b903a6bdSVladimir Oltean 		val |= BIT(to);
95*b903a6bdSVladimir Oltean 	else
96*b903a6bdSVladimir Oltean 		val &= ~BIT(to);
97*b903a6bdSVladimir Oltean 
98*b903a6bdSVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_PGID_PGID, pgid);
99*b903a6bdSVladimir Oltean }
100*b903a6bdSVladimir Oltean 
101*b903a6bdSVladimir Oltean static void felix_migrate_flood_to_npi_port(struct dsa_switch *ds, int port)
102*b903a6bdSVladimir Oltean {
103*b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104*b903a6bdSVladimir Oltean 
105*b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_UC);
106*b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_MC);
107*b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, port, ocelot->num_phys_ports, PGID_BC);
108*b903a6bdSVladimir Oltean }
109*b903a6bdSVladimir Oltean 
110*b903a6bdSVladimir Oltean static void
111*b903a6bdSVladimir Oltean felix_migrate_flood_to_tag_8021q_port(struct dsa_switch *ds, int port)
112*b903a6bdSVladimir Oltean {
113*b903a6bdSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
114*b903a6bdSVladimir Oltean 
115*b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_UC);
116*b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_MC);
117*b903a6bdSVladimir Oltean 	felix_migrate_pgid_bit(ds, ocelot->num_phys_ports, port, PGID_BC);
118*b903a6bdSVladimir Oltean }
119*b903a6bdSVladimir 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 	ocelot->npi = -1;
374e21268efSVladimir Oltean 
375e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
376e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
377e21268efSVladimir Oltean 
3788abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3798abe1970SVladimir Oltean 
3808abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
381e21268efSVladimir Oltean }
382e21268efSVladimir Oltean 
383e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
384e21268efSVladimir Oltean {
3858abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
3868abe1970SVladimir Oltean 
387e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
38854c31984SVladimir Oltean 	ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
389e21268efSVladimir Oltean 
390e21268efSVladimir Oltean 	/* Restore PGID_CPU */
391e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
392e21268efSVladimir Oltean 			 PGID_CPU);
393e21268efSVladimir Oltean 
3948abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3958abe1970SVladimir Oltean 
3968abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
397e21268efSVladimir Oltean }
398e21268efSVladimir Oltean 
39999348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
40099348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
40199348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
402c8c0ba4fSVladimir Oltean  */
40399348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
40499348004SVladimir Oltean 					      bool using_tag_8021q)
405c8c0ba4fSVladimir Oltean {
40699348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
40799348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
40899348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
40999348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
41099348004SVladimir Oltean 	unsigned long port_mask;
4112960bb14SVladimir Oltean 	struct dsa_port *dp;
41299348004SVladimir Oltean 	bool cpu_copy_ena;
41399348004SVladimir Oltean 	int cpu = -1, err;
414c8c0ba4fSVladimir Oltean 
41599348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
41699348004SVladimir Oltean 		return 0;
417c8c0ba4fSVladimir Oltean 
41899348004SVladimir Oltean 	/* Figure out the current CPU port */
4192960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
4202960bb14SVladimir Oltean 		cpu = dp->index;
4218d5f7954SVladimir Oltean 		break;
422c8c0ba4fSVladimir Oltean 	}
4238d5f7954SVladimir Oltean 
424d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
425d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
426d78637a8SVladimir Oltean 	 */
427c8c0ba4fSVladimir Oltean 
42899348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
42999348004SVladimir Oltean 	list_for_each_entry(trap, &ocelot->traps, trap_list) {
43099348004SVladimir Oltean 		/* Figure out the current trapping destination */
43199348004SVladimir Oltean 		if (using_tag_8021q) {
43299348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
43399348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
43499348004SVladimir Oltean 			 * port module.
435c8c0ba4fSVladimir Oltean 			 */
43699348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
43799348004SVladimir Oltean 			port_mask = BIT(cpu);
43899348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
439c8c0ba4fSVladimir Oltean 		} else {
44099348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
44199348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
442c8c0ba4fSVladimir Oltean 			 */
44399348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
44499348004SVladimir Oltean 			port_mask = 0;
44599348004SVladimir Oltean 			cpu_copy_ena = true;
446c8c0ba4fSVladimir Oltean 		}
447c8c0ba4fSVladimir Oltean 
44899348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
44999348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
45099348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
45199348004SVladimir Oltean 			continue;
452c8c0ba4fSVladimir Oltean 
45399348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
45499348004SVladimir Oltean 		trap->action.port_mask = port_mask;
45599348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
4560a6f17c6SVladimir Oltean 
45799348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
458c8c0ba4fSVladimir Oltean 		if (err)
459c8c0ba4fSVladimir Oltean 			return err;
46099348004SVladimir Oltean 	}
461c8c0ba4fSVladimir Oltean 
46299348004SVladimir Oltean 	return 0;
463c8c0ba4fSVladimir Oltean }
464c8c0ba4fSVladimir Oltean 
465f9cef64fSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu, bool change)
466e21268efSVladimir Oltean {
467e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
468e21268efSVladimir Oltean 	unsigned long cpu_flood;
4692960bb14SVladimir Oltean 	struct dsa_port *dp;
4702960bb14SVladimir Oltean 	int err;
471e21268efSVladimir Oltean 
472e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
473e21268efSVladimir Oltean 
4742960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
475e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
476e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
477e21268efSVladimir Oltean 		 * for 2 reasons:
478e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
479e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
480e21268efSVladimir Oltean 		 *   into the system.
481e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
482e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
483e21268efSVladimir Oltean 		 *   port module.
484e21268efSVladimir Oltean 		 */
485e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
486e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4872960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
488e21268efSVladimir Oltean 	}
489e21268efSVladimir Oltean 
490c8c0ba4fSVladimir Oltean 	/* In tag_8021q mode, the CPU port module is unused, except for PTP
491c8c0ba4fSVladimir Oltean 	 * frames. So we want to disable flooding of any kind to the CPU port
492c8c0ba4fSVladimir Oltean 	 * module, since packets going there will end in a black hole.
493e21268efSVladimir Oltean 	 */
494e21268efSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
495e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
496e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
497b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
498e21268efSVladimir Oltean 
4995da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
500e21268efSVladimir Oltean 	if (err)
501d7b1fd52SVladimir Oltean 		return err;
502d7b1fd52SVladimir Oltean 
503f9cef64fSVladimir Oltean 	if (change) {
504f9cef64fSVladimir Oltean 		err = dsa_port_walk_fdbs(ds, cpu,
505f9cef64fSVladimir Oltean 					 felix_migrate_fdbs_to_tag_8021q_port);
506d7b1fd52SVladimir Oltean 		if (err)
507d7b1fd52SVladimir Oltean 			goto out_tag_8021q_unregister;
508e21268efSVladimir Oltean 
509f9cef64fSVladimir Oltean 		err = dsa_port_walk_mdbs(ds, cpu,
510f9cef64fSVladimir Oltean 					 felix_migrate_mdbs_to_tag_8021q_port);
511f9cef64fSVladimir Oltean 		if (err)
512f9cef64fSVladimir Oltean 			goto out_migrate_fdbs;
513*b903a6bdSVladimir Oltean 
514*b903a6bdSVladimir Oltean 		felix_migrate_flood_to_tag_8021q_port(ds, cpu);
515f9cef64fSVladimir Oltean 	}
516f9cef64fSVladimir Oltean 
517f9cef64fSVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
518f9cef64fSVladimir Oltean 	if (err)
519*b903a6bdSVladimir Oltean 		goto out_migrate_flood;
520f9cef64fSVladimir Oltean 
52199348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
52299348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
52399348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
52499348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
52599348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
52699348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
52799348004SVladimir Oltean 	 */
52899348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
52999348004SVladimir Oltean 
530e21268efSVladimir Oltean 	return 0;
531e21268efSVladimir Oltean 
532*b903a6bdSVladimir Oltean out_migrate_flood:
533*b903a6bdSVladimir Oltean 	if (change)
534*b903a6bdSVladimir Oltean 		felix_migrate_flood_to_npi_port(ds, cpu);
535f9cef64fSVladimir Oltean 	if (change)
536f9cef64fSVladimir Oltean 		dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
537f9cef64fSVladimir Oltean out_migrate_fdbs:
538f9cef64fSVladimir Oltean 	if (change)
539f9cef64fSVladimir Oltean 		dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
540d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
541d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
542e21268efSVladimir Oltean 	return err;
543e21268efSVladimir Oltean }
544e21268efSVladimir Oltean 
545e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
546e21268efSVladimir Oltean {
547e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5482960bb14SVladimir Oltean 	struct dsa_port *dp;
5492960bb14SVladimir Oltean 	int err;
550e21268efSVladimir Oltean 
55199348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
552c8c0ba4fSVladimir Oltean 	if (err)
553c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
554c8c0ba4fSVladimir Oltean 			err);
555c8c0ba4fSVladimir Oltean 
556d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
557e21268efSVladimir Oltean 
5582960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
559e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
560e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
561e21268efSVladimir Oltean 		 */
562e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
563e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
564e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
5652960bb14SVladimir Oltean 				 dp->index);
566e21268efSVladimir Oltean 	}
567e21268efSVladimir Oltean 
568e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
569e21268efSVladimir Oltean }
570e21268efSVladimir Oltean 
571adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
572adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
573adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
574adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
575adb3dccfSVladimir Oltean  * DSA master.
576adb3dccfSVladimir Oltean  */
577adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
578adb3dccfSVladimir Oltean {
579adb3dccfSVladimir Oltean 	ocelot->npi = port;
580adb3dccfSVladimir Oltean 
581adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
582adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
583adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
584adb3dccfSVladimir Oltean 
585adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
586adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
587adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
588adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
589adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
590adb3dccfSVladimir Oltean 
591adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
592adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
593adb3dccfSVladimir Oltean }
594adb3dccfSVladimir Oltean 
595adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
596adb3dccfSVladimir Oltean {
597adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
598adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
599adb3dccfSVladimir Oltean 
600adb3dccfSVladimir Oltean 	ocelot->npi = -1;
601adb3dccfSVladimir Oltean 
602adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
603adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
604adb3dccfSVladimir Oltean 
605adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
606adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
607adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
608adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
609adb3dccfSVladimir Oltean 
610adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
611adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
612adb3dccfSVladimir Oltean }
613adb3dccfSVladimir Oltean 
614f9cef64fSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu, bool change)
615adb3dccfSVladimir Oltean {
616adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
617adb3dccfSVladimir Oltean 	unsigned long cpu_flood;
618f9cef64fSVladimir Oltean 	int err;
619f9cef64fSVladimir Oltean 
620f9cef64fSVladimir Oltean 	if (change) {
621f9cef64fSVladimir Oltean 		err = dsa_port_walk_fdbs(ds, cpu,
622f9cef64fSVladimir Oltean 					 felix_migrate_fdbs_to_npi_port);
623f9cef64fSVladimir Oltean 		if (err)
624f9cef64fSVladimir Oltean 			return err;
625f9cef64fSVladimir Oltean 
626f9cef64fSVladimir Oltean 		err = dsa_port_walk_mdbs(ds, cpu,
627f9cef64fSVladimir Oltean 					 felix_migrate_mdbs_to_npi_port);
628f9cef64fSVladimir Oltean 		if (err)
629f9cef64fSVladimir Oltean 			goto out_migrate_fdbs;
630*b903a6bdSVladimir Oltean 
631*b903a6bdSVladimir Oltean 		felix_migrate_flood_to_npi_port(ds, cpu);
632f9cef64fSVladimir Oltean 	}
633adb3dccfSVladimir Oltean 
634adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
635adb3dccfSVladimir Oltean 
636adb3dccfSVladimir Oltean 	/* Include the CPU port module (and indirectly, the NPI port)
637adb3dccfSVladimir Oltean 	 * in the forwarding mask for unknown unicast - the hardware
638adb3dccfSVladimir Oltean 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
639adb3dccfSVladimir Oltean 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
640adb3dccfSVladimir Oltean 	 * since Ocelot relies on whitelisting MAC addresses towards
641adb3dccfSVladimir Oltean 	 * PGID_CPU.
642adb3dccfSVladimir Oltean 	 * We do this because DSA does not yet perform RX filtering,
643adb3dccfSVladimir Oltean 	 * and the NPI port does not perform source address learning,
644adb3dccfSVladimir Oltean 	 * so traffic sent to Linux is effectively unknown from the
645adb3dccfSVladimir Oltean 	 * switch's perspective.
646adb3dccfSVladimir Oltean 	 */
647adb3dccfSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
648adb3dccfSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
6496edb9e8dSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
650b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
651adb3dccfSVladimir Oltean 
652adb3dccfSVladimir Oltean 	return 0;
653f9cef64fSVladimir Oltean 
654f9cef64fSVladimir Oltean out_migrate_fdbs:
655f9cef64fSVladimir Oltean 	if (change)
656f9cef64fSVladimir Oltean 		dsa_port_walk_fdbs(ds, cpu,
657f9cef64fSVladimir Oltean 				   felix_migrate_fdbs_to_tag_8021q_port);
658f9cef64fSVladimir Oltean 
659f9cef64fSVladimir Oltean 	return err;
660adb3dccfSVladimir Oltean }
661adb3dccfSVladimir Oltean 
662adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
663adb3dccfSVladimir Oltean {
664adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
665adb3dccfSVladimir Oltean 
666adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
667adb3dccfSVladimir Oltean }
668adb3dccfSVladimir Oltean 
669adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
670f9cef64fSVladimir Oltean 				  enum dsa_tag_protocol proto, bool change)
671adb3dccfSVladimir Oltean {
672adb3dccfSVladimir Oltean 	int err;
673adb3dccfSVladimir Oltean 
674adb3dccfSVladimir Oltean 	switch (proto) {
6757c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
676adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
677f9cef64fSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu, change);
678adb3dccfSVladimir Oltean 		break;
679e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
680f9cef64fSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu, change);
681e21268efSVladimir Oltean 		break;
682adb3dccfSVladimir Oltean 	default:
683adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
684adb3dccfSVladimir Oltean 	}
685adb3dccfSVladimir Oltean 
686adb3dccfSVladimir Oltean 	return err;
687adb3dccfSVladimir Oltean }
688adb3dccfSVladimir Oltean 
689adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
690adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
691adb3dccfSVladimir Oltean {
692adb3dccfSVladimir Oltean 	switch (proto) {
6937c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
694adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
695adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
696adb3dccfSVladimir Oltean 		break;
697e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
698e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
699e21268efSVladimir Oltean 		break;
700adb3dccfSVladimir Oltean 	default:
701adb3dccfSVladimir Oltean 		break;
702adb3dccfSVladimir Oltean 	}
703adb3dccfSVladimir Oltean }
704adb3dccfSVladimir Oltean 
705e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
706e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
707e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
708e21268efSVladimir Oltean  */
709adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
710adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
711adb3dccfSVladimir Oltean {
712adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
713adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
714adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
715adb3dccfSVladimir Oltean 	int err;
716adb3dccfSVladimir Oltean 
7177c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
7187c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
719e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
720adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
721adb3dccfSVladimir Oltean 
722adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
723adb3dccfSVladimir Oltean 
724f9cef64fSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto, true);
725adb3dccfSVladimir Oltean 	if (err) {
726f9cef64fSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto, true);
727adb3dccfSVladimir Oltean 		return err;
728adb3dccfSVladimir Oltean 	}
729adb3dccfSVladimir Oltean 
730adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
731adb3dccfSVladimir Oltean 
732adb3dccfSVladimir Oltean 	return 0;
733adb3dccfSVladimir Oltean }
734adb3dccfSVladimir Oltean 
73556051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
7364d776482SFlorian Fainelli 						    int port,
7374d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
73856051948SVladimir Oltean {
739adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
740adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
741adb3dccfSVladimir Oltean 
742adb3dccfSVladimir Oltean 	return felix->tag_proto;
74356051948SVladimir Oltean }
74456051948SVladimir Oltean 
74556051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
74656051948SVladimir Oltean 				 unsigned int ageing_time)
74756051948SVladimir Oltean {
74856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
74956051948SVladimir Oltean 
75056051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
75156051948SVladimir Oltean 
75256051948SVladimir Oltean 	return 0;
75356051948SVladimir Oltean }
75456051948SVladimir Oltean 
7555cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
7565cad43a5SVladimir Oltean {
7575cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7585cad43a5SVladimir Oltean 	int err;
7595cad43a5SVladimir Oltean 
7605cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
7615cad43a5SVladimir Oltean 	if (err)
7625cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
7635cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7645cad43a5SVladimir Oltean }
7655cad43a5SVladimir Oltean 
76656051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
76756051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
76856051948SVladimir Oltean {
76956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
77056051948SVladimir Oltean 
77156051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
77256051948SVladimir Oltean }
77356051948SVladimir Oltean 
77456051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
775c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
776c2693363SVladimir Oltean 			 struct dsa_db db)
77756051948SVladimir Oltean {
77854c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
77956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
78056051948SVladimir Oltean 
78154c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
78254c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
78354c31984SVladimir Oltean 
78454c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
78556051948SVladimir Oltean }
78656051948SVladimir Oltean 
78756051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
788c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
789c2693363SVladimir Oltean 			 struct dsa_db db)
79056051948SVladimir Oltean {
79154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
79256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
79356051948SVladimir Oltean 
79454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
79554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
79654c31984SVladimir Oltean 
79754c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
79856051948SVladimir Oltean }
79956051948SVladimir Oltean 
800961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
801c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
802c2693363SVladimir Oltean 			     struct dsa_db db)
803961d8b69SVladimir Oltean {
80454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
805961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
806961d8b69SVladimir Oltean 
80754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
80854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
80954c31984SVladimir Oltean 
81054c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
811961d8b69SVladimir Oltean }
812961d8b69SVladimir Oltean 
813961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
814c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
815c2693363SVladimir Oltean 			     struct dsa_db db)
816961d8b69SVladimir Oltean {
81754c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
818961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
819961d8b69SVladimir Oltean 
82054c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
82154c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
82254c31984SVladimir Oltean 
82354c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
824961d8b69SVladimir Oltean }
825961d8b69SVladimir Oltean 
826a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
827c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
828c2693363SVladimir Oltean 			 struct dsa_db db)
829209edf95SVladimir Oltean {
83054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
831209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
832209edf95SVladimir Oltean 
83354c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
83454c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
83554c31984SVladimir Oltean 
83654c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
837209edf95SVladimir Oltean }
838209edf95SVladimir Oltean 
839209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
840c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
841c2693363SVladimir Oltean 			 struct dsa_db db)
842209edf95SVladimir Oltean {
84354c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
844209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
845209edf95SVladimir Oltean 
84654c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
84754c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
84854c31984SVladimir Oltean 
84954c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
850209edf95SVladimir Oltean }
851209edf95SVladimir Oltean 
85256051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
85356051948SVladimir Oltean 				       u8 state)
85456051948SVladimir Oltean {
85556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85656051948SVladimir Oltean 
85756051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
85856051948SVladimir Oltean }
85956051948SVladimir Oltean 
860421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
861421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
862421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
863421741eaSVladimir Oltean {
864421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
865421741eaSVladimir Oltean 
866421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
867421741eaSVladimir Oltean }
868421741eaSVladimir Oltean 
869421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
870421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
871421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
872421741eaSVladimir Oltean {
873421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
874421741eaSVladimir Oltean 
875421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
876421741eaSVladimir Oltean 
877421741eaSVladimir Oltean 	return 0;
878421741eaSVladimir Oltean }
879421741eaSVladimir Oltean 
88056051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
88106b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
88206b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
88356051948SVladimir Oltean {
88456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
88556051948SVladimir Oltean 
88654c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
88754c31984SVladimir Oltean 				       extack);
88856051948SVladimir Oltean }
88956051948SVladimir Oltean 
89056051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
891d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
89256051948SVladimir Oltean {
89356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
89456051948SVladimir Oltean 
895d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
89656051948SVladimir Oltean }
89756051948SVladimir Oltean 
8988fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
899dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
9008fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
9018fe6832eSVladimir Oltean {
9028fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9038fe6832eSVladimir Oltean 
904dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
9058fe6832eSVladimir Oltean }
9068fe6832eSVladimir Oltean 
9078fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
908dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
9098fe6832eSVladimir Oltean {
9108fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9118fe6832eSVladimir Oltean 
912dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
9138fe6832eSVladimir Oltean 
9148fe6832eSVladimir Oltean 	return 0;
9158fe6832eSVladimir Oltean }
9168fe6832eSVladimir Oltean 
9178fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
9188fe6832eSVladimir Oltean {
9198fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
9208fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9218fe6832eSVladimir Oltean 
9228fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
9238fe6832eSVladimir Oltean 
9248fe6832eSVladimir Oltean 	return 0;
9258fe6832eSVladimir Oltean }
9268fe6832eSVladimir Oltean 
92756051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
92801af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
92901af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
93056051948SVladimir Oltean {
9312f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
932b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
9332f0402feSVladimir Oltean 
9349a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
9359a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
9369a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
9379a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
9389a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
9399a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
9409a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
9419a720680SVladimir Oltean 	 */
9429a720680SVladimir Oltean 	if (port == ocelot->npi)
9439a720680SVladimir Oltean 		return 0;
9449a720680SVladimir Oltean 
945b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9462f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
94701af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
94801af940eSVladimir Oltean 				   extack);
94956051948SVladimir Oltean }
95056051948SVladimir Oltean 
95189153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
95289153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
95356051948SVladimir Oltean {
95456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
95556051948SVladimir Oltean 
9563b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
95756051948SVladimir Oltean }
95856051948SVladimir Oltean 
9591958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
96031046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
96131046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
96256051948SVladimir Oltean {
96356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
964183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9651958d581SVladimir Oltean 	int err;
96656051948SVladimir Oltean 
96701af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9681958d581SVladimir Oltean 	if (err)
9691958d581SVladimir Oltean 		return err;
9701958d581SVladimir Oltean 
9711958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
972183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
973183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
97456051948SVladimir Oltean }
97556051948SVladimir Oltean 
97656051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
97756051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
97856051948SVladimir Oltean {
97956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
98056051948SVladimir Oltean 
981b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
98256051948SVladimir Oltean }
98356051948SVladimir Oltean 
98479fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
98579fda660SRussell King (Oracle) 				   struct phylink_config *config)
98679fda660SRussell King (Oracle) {
98779fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
98879fda660SRussell King (Oracle) 
989f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
990f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
991f6f04c02SRussell King (Oracle) 	 * as non-legacy.
992f6f04c02SRussell King (Oracle) 	 */
993f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
994f6f04c02SRussell King (Oracle) 
99579fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
99679fda660SRussell King (Oracle) 		  config->supported_interfaces);
99779fda660SRussell King (Oracle) }
99879fda660SRussell King (Oracle) 
999bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
1000bdeced75SVladimir Oltean 				   unsigned long *supported,
1001bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
1002bdeced75SVladimir Oltean {
1003bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1004375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1005bdeced75SVladimir Oltean 
1006375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
1007375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
1008bdeced75SVladimir Oltean }
1009bdeced75SVladimir Oltean 
1010864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
1011864ba485SRussell King (Oracle) 							int port,
1012864ba485SRussell King (Oracle) 							phy_interface_t iface)
1013bdeced75SVladimir Oltean {
1014bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1015bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1016864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
1017bdeced75SVladimir Oltean 
101849af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
1019864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
1020864ba485SRussell King (Oracle) 
1021864ba485SRussell King (Oracle) 	return pcs;
1022bdeced75SVladimir Oltean }
1023bdeced75SVladimir Oltean 
1024bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
1025bdeced75SVladimir Oltean 					unsigned int link_an_mode,
1026bdeced75SVladimir Oltean 					phy_interface_t interface)
1027bdeced75SVladimir Oltean {
1028bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1029bdeced75SVladimir Oltean 
1030e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1031e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
1032bdeced75SVladimir Oltean }
1033bdeced75SVladimir Oltean 
1034bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
1035bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
1036bdeced75SVladimir Oltean 				      phy_interface_t interface,
10375b502a7bSRussell King 				      struct phy_device *phydev,
10385b502a7bSRussell King 				      int speed, int duplex,
10395b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
1040bdeced75SVladimir Oltean {
1041bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10427e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1043bdeced75SVladimir Oltean 
1044e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1045e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1046e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10477e14a2dcSVladimir Oltean 
10487e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10497e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1050bdeced75SVladimir Oltean }
1051bdeced75SVladimir Oltean 
1052bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1053bd2b3161SXiaoliang Yang {
1054bd2b3161SXiaoliang Yang 	int i;
1055bd2b3161SXiaoliang Yang 
1056bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1057bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1058bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1059bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1060bd2b3161SXiaoliang Yang 		       port);
1061bd2b3161SXiaoliang Yang 
106270d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1063bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1064bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1065bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1066bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1067bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1068bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1069bd2b3161SXiaoliang Yang 			      port, i);
1070bd2b3161SXiaoliang Yang 	}
1071bd2b3161SXiaoliang Yang }
1072bd2b3161SXiaoliang Yang 
107356051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
107456051948SVladimir Oltean 			      u32 stringset, u8 *data)
107556051948SVladimir Oltean {
107656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
107756051948SVladimir Oltean 
107856051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
107956051948SVladimir Oltean }
108056051948SVladimir Oltean 
108156051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
108256051948SVladimir Oltean {
108356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
108456051948SVladimir Oltean 
108556051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
108656051948SVladimir Oltean }
108756051948SVladimir Oltean 
108856051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
108956051948SVladimir Oltean {
109056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
109156051948SVladimir Oltean 
109256051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
109356051948SVladimir Oltean }
109456051948SVladimir Oltean 
109556051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
109656051948SVladimir Oltean 			     struct ethtool_ts_info *info)
109756051948SVladimir Oltean {
109856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
109956051948SVladimir Oltean 
110056051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
110156051948SVladimir Oltean }
110256051948SVladimir Oltean 
1103acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1104acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1105acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1106acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1107acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1108acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1109acf242fcSColin Foster };
1110acf242fcSColin Foster 
1111acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1112acf242fcSColin Foster 				   phy_interface_t phy_mode)
1113acf242fcSColin Foster {
1114acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1115acf242fcSColin Foster 
1116acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1117acf242fcSColin Foster 		return 0;
1118acf242fcSColin Foster 	return -EOPNOTSUPP;
1119acf242fcSColin Foster }
1120acf242fcSColin Foster 
1121bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1122bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1123bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1124bdeced75SVladimir Oltean {
1125bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1126bdeced75SVladimir Oltean 	struct device_node *child;
1127bdeced75SVladimir Oltean 
112837fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1129bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1130bdeced75SVladimir Oltean 		u32 port;
1131bdeced75SVladimir Oltean 		int err;
1132bdeced75SVladimir Oltean 
1133bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1134bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1135bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1136bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1137bdeced75SVladimir Oltean 			of_node_put(child);
1138bdeced75SVladimir Oltean 			return -ENODEV;
1139bdeced75SVladimir Oltean 		}
1140bdeced75SVladimir Oltean 
1141bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1142bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1143bdeced75SVladimir Oltean 		if (err) {
1144bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1145bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1146bdeced75SVladimir Oltean 				port);
1147bdeced75SVladimir Oltean 			of_node_put(child);
1148bdeced75SVladimir Oltean 			return -ENODEV;
1149bdeced75SVladimir Oltean 		}
1150bdeced75SVladimir Oltean 
1151acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1152bdeced75SVladimir Oltean 		if (err < 0) {
1153bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1154bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
115559ebb430SSumera Priyadarsini 			of_node_put(child);
1156bdeced75SVladimir Oltean 			return err;
1157bdeced75SVladimir Oltean 		}
1158bdeced75SVladimir Oltean 
1159bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1160bdeced75SVladimir Oltean 	}
1161bdeced75SVladimir Oltean 
1162bdeced75SVladimir Oltean 	return 0;
1163bdeced75SVladimir Oltean }
1164bdeced75SVladimir Oltean 
1165bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1166bdeced75SVladimir Oltean {
1167bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1168bdeced75SVladimir Oltean 	struct device_node *switch_node;
1169bdeced75SVladimir Oltean 	struct device_node *ports_node;
1170bdeced75SVladimir Oltean 	int err;
1171bdeced75SVladimir Oltean 
1172bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1173bdeced75SVladimir Oltean 
1174bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1175abecbfcdSVladimir Oltean 	if (!ports_node)
1176abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1177bdeced75SVladimir Oltean 	if (!ports_node) {
1178abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1179bdeced75SVladimir Oltean 		return -ENODEV;
1180bdeced75SVladimir Oltean 	}
1181bdeced75SVladimir Oltean 
1182bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1183bdeced75SVladimir Oltean 	of_node_put(ports_node);
1184bdeced75SVladimir Oltean 
1185bdeced75SVladimir Oltean 	return err;
1186bdeced75SVladimir Oltean }
1187bdeced75SVladimir Oltean 
118856051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
118956051948SVladimir Oltean {
119056051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1191bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1192b4024c9eSClaudiu Manoil 	struct resource res;
119356051948SVladimir Oltean 	int port, i, err;
119456051948SVladimir Oltean 
119556051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
119656051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
119756051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
119856051948SVladimir Oltean 	if (!ocelot->ports)
119956051948SVladimir Oltean 		return -ENOMEM;
120056051948SVladimir Oltean 
120156051948SVladimir Oltean 	ocelot->map		= felix->info->map;
120256051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
120356051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
120421ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
120507d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
120677043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
120777043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
120877043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
120977043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
121056051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1211cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1212cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1213f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
121456051948SVladimir Oltean 
1215bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1216bdeced75SVladimir Oltean 				 GFP_KERNEL);
1217bdeced75SVladimir Oltean 	if (!port_phy_modes)
1218bdeced75SVladimir Oltean 		return -ENOMEM;
1219bdeced75SVladimir Oltean 
1220bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1221bdeced75SVladimir Oltean 	if (err) {
1222bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1223bdeced75SVladimir Oltean 		return err;
1224bdeced75SVladimir Oltean 	}
1225bdeced75SVladimir Oltean 
122656051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
122756051948SVladimir Oltean 		struct regmap *target;
122856051948SVladimir Oltean 
122956051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
123056051948SVladimir Oltean 			continue;
123156051948SVladimir Oltean 
1232b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1233b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1234375e1314SVladimir Oltean 		res.start += felix->switch_base;
1235375e1314SVladimir Oltean 		res.end += felix->switch_base;
123656051948SVladimir Oltean 
1237242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
123856051948SVladimir Oltean 		if (IS_ERR(target)) {
123956051948SVladimir Oltean 			dev_err(ocelot->dev,
124056051948SVladimir Oltean 				"Failed to map device memory space\n");
1241bdeced75SVladimir Oltean 			kfree(port_phy_modes);
124256051948SVladimir Oltean 			return PTR_ERR(target);
124356051948SVladimir Oltean 		}
124456051948SVladimir Oltean 
124556051948SVladimir Oltean 		ocelot->targets[i] = target;
124656051948SVladimir Oltean 	}
124756051948SVladimir Oltean 
124856051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
124956051948SVladimir Oltean 	if (err) {
125056051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1251bdeced75SVladimir Oltean 		kfree(port_phy_modes);
125256051948SVladimir Oltean 		return err;
125356051948SVladimir Oltean 	}
125456051948SVladimir Oltean 
125556051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
125656051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
125791c724cfSVladimir Oltean 		struct regmap *target;
125856051948SVladimir Oltean 
125956051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
126056051948SVladimir Oltean 					   sizeof(struct ocelot_port),
126156051948SVladimir Oltean 					   GFP_KERNEL);
126256051948SVladimir Oltean 		if (!ocelot_port) {
126356051948SVladimir Oltean 			dev_err(ocelot->dev,
126456051948SVladimir Oltean 				"failed to allocate port memory\n");
1265bdeced75SVladimir Oltean 			kfree(port_phy_modes);
126656051948SVladimir Oltean 			return -ENOMEM;
126756051948SVladimir Oltean 		}
126856051948SVladimir Oltean 
1269b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1270b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1271375e1314SVladimir Oltean 		res.start += felix->switch_base;
1272375e1314SVladimir Oltean 		res.end += felix->switch_base;
127356051948SVladimir Oltean 
1274242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
127591c724cfSVladimir Oltean 		if (IS_ERR(target)) {
127656051948SVladimir Oltean 			dev_err(ocelot->dev,
127791c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
127891c724cfSVladimir Oltean 				port);
1279bdeced75SVladimir Oltean 			kfree(port_phy_modes);
128091c724cfSVladimir Oltean 			return PTR_ERR(target);
128156051948SVladimir Oltean 		}
128256051948SVladimir Oltean 
1283bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
128456051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
128591c724cfSVladimir Oltean 		ocelot_port->target = target;
128656051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
128756051948SVladimir Oltean 	}
128856051948SVladimir Oltean 
1289bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1290bdeced75SVladimir Oltean 
1291bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1292bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1293bdeced75SVladimir Oltean 		if (err < 0)
1294bdeced75SVladimir Oltean 			return err;
1295bdeced75SVladimir Oltean 	}
1296bdeced75SVladimir Oltean 
129756051948SVladimir Oltean 	return 0;
129856051948SVladimir Oltean }
129956051948SVladimir Oltean 
13001328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
13011328a883SVladimir Oltean 					   struct sk_buff *skb)
13021328a883SVladimir Oltean {
13031328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
13041328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
13051328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
13061328a883SVladimir Oltean 	unsigned long flags;
13071328a883SVladimir Oltean 
13081328a883SVladimir Oltean 	if (!clone)
13091328a883SVladimir Oltean 		return;
13101328a883SVladimir Oltean 
13111328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
13121328a883SVladimir Oltean 
13131328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
13141328a883SVladimir Oltean 		if (skb != clone)
13151328a883SVladimir Oltean 			continue;
13161328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
13171328a883SVladimir Oltean 		skb_match = skb;
13181328a883SVladimir Oltean 		break;
13191328a883SVladimir Oltean 	}
13201328a883SVladimir Oltean 
13211328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
13221328a883SVladimir Oltean 
13231328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
13241328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
13251328a883SVladimir Oltean }
13261328a883SVladimir Oltean 
132749f885b2SVladimir Oltean #define work_to_xmit_work(w) \
132849f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
132949f885b2SVladimir Oltean 
133049f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
133149f885b2SVladimir Oltean {
133249f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
133349f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
133449f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
133549f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
133649f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
133749f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
133849f885b2SVladimir Oltean 	int retries = 10;
133949f885b2SVladimir Oltean 
134049f885b2SVladimir Oltean 	do {
134149f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
134249f885b2SVladimir Oltean 			break;
134349f885b2SVladimir Oltean 
134449f885b2SVladimir Oltean 		cpu_relax();
134549f885b2SVladimir Oltean 	} while (--retries);
134649f885b2SVladimir Oltean 
134749f885b2SVladimir Oltean 	if (!retries) {
134849f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
134949f885b2SVladimir Oltean 			port);
13501328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
135149f885b2SVladimir Oltean 		kfree_skb(skb);
135249f885b2SVladimir Oltean 		return;
135349f885b2SVladimir Oltean 	}
135449f885b2SVladimir Oltean 
135549f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
135649f885b2SVladimir Oltean 
135749f885b2SVladimir Oltean 	consume_skb(skb);
135849f885b2SVladimir Oltean 	kfree(xmit_work);
135949f885b2SVladimir Oltean }
136049f885b2SVladimir Oltean 
136135d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
136235d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
136349f885b2SVladimir Oltean {
136435d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
136549f885b2SVladimir Oltean 
136635d97680SVladimir Oltean 	switch (proto) {
136735d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
136835d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
136935d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
137049f885b2SVladimir Oltean 		return 0;
137135d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
137235d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
137349f885b2SVladimir Oltean 		return 0;
137435d97680SVladimir Oltean 	default:
137535d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
137649f885b2SVladimir Oltean 	}
137749f885b2SVladimir Oltean }
137849f885b2SVladimir Oltean 
137956051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
138056051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
138156051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
138256051948SVladimir Oltean  * (which will not work).
138356051948SVladimir Oltean  */
138456051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
138556051948SVladimir Oltean {
138656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
138756051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13882960bb14SVladimir Oltean 	struct dsa_port *dp;
13892960bb14SVladimir Oltean 	int err;
139056051948SVladimir Oltean 
139156051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
139256051948SVladimir Oltean 	if (err)
139356051948SVladimir Oltean 		return err;
139456051948SVladimir Oltean 
1395d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1396d1cc0e93SVladimir Oltean 	if (err)
13976b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1398d1cc0e93SVladimir Oltean 
13992b49d128SYangbo Lu 	if (ocelot->ptp) {
14002ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
14012b49d128SYangbo Lu 		if (err) {
14022b49d128SYangbo Lu 			dev_err(ocelot->dev,
14032b49d128SYangbo Lu 				"Timestamp initialization failed\n");
14042b49d128SYangbo Lu 			ocelot->ptp = 0;
14052b49d128SYangbo Lu 		}
14062b49d128SYangbo Lu 	}
140756051948SVladimir Oltean 
14082960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
14092960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1410bd2b3161SXiaoliang Yang 
1411bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1412bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1413bd2b3161SXiaoliang Yang 		 */
14142960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
141556051948SVladimir Oltean 	}
141656051948SVladimir Oltean 
1417f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1418f59fd9caSVladimir Oltean 	if (err)
14196b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1420f59fd9caSVladimir Oltean 
14212960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1422adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1423adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
14241cf3299bSVladimir Oltean 		 */
1425f9cef64fSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto, false);
14268d5f7954SVladimir Oltean 		break;
1427adb3dccfSVladimir Oltean 	}
14281cf3299bSVladimir Oltean 
14290b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1430c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
143154c31984SVladimir Oltean 	ds->fdb_isolation = true;
143254c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1433bdeced75SVladimir Oltean 
143456051948SVladimir Oltean 	return 0;
14356b73b7c9SVladimir Oltean 
14366b73b7c9SVladimir Oltean out_deinit_ports:
14372960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14382960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
14396b73b7c9SVladimir Oltean 
14406b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
14416b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
14426b73b7c9SVladimir Oltean 
14436b73b7c9SVladimir Oltean out_mdiobus_free:
14446b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14456b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14466b73b7c9SVladimir Oltean 
14476b73b7c9SVladimir Oltean 	return err;
144856051948SVladimir Oltean }
144956051948SVladimir Oltean 
145056051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
145156051948SVladimir Oltean {
145256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1453bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14542960bb14SVladimir Oltean 	struct dsa_port *dp;
1455bdeced75SVladimir Oltean 
14562960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14572960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14588d5f7954SVladimir Oltean 		break;
1459adb3dccfSVladimir Oltean 	}
1460adb3dccfSVladimir Oltean 
14612960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14622960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1463d19741b0SVladimir Oltean 
146449f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
146549f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
146649f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
146749f885b2SVladimir Oltean 
1468d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1469d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
147056051948SVladimir Oltean }
147156051948SVladimir Oltean 
1472c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1473c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1474c0bcf537SYangbo Lu {
1475c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1476c0bcf537SYangbo Lu 
1477c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1478c0bcf537SYangbo Lu }
1479c0bcf537SYangbo Lu 
1480c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1481c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1482c0bcf537SYangbo Lu {
1483c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
148499348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
148599348004SVladimir Oltean 	bool using_tag_8021q;
148699348004SVladimir Oltean 	int err;
1487c0bcf537SYangbo Lu 
148899348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
148999348004SVladimir Oltean 	if (err)
149099348004SVladimir Oltean 		return err;
149199348004SVladimir Oltean 
149299348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
149399348004SVladimir Oltean 
149499348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1495c0bcf537SYangbo Lu }
1496c0bcf537SYangbo Lu 
14970a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type)
14980a6f17c6SVladimir Oltean {
14990a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
15000a6f17c6SVladimir Oltean 	int err, grp = 0;
15010a6f17c6SVladimir Oltean 
15020a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
15030a6f17c6SVladimir Oltean 		return false;
15040a6f17c6SVladimir Oltean 
15050a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
15060a6f17c6SVladimir Oltean 		return false;
15070a6f17c6SVladimir Oltean 
15080a6f17c6SVladimir Oltean 	if (ptp_type == PTP_CLASS_NONE)
15090a6f17c6SVladimir Oltean 		return false;
15100a6f17c6SVladimir Oltean 
15110a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
15120a6f17c6SVladimir Oltean 		struct sk_buff *skb;
15130a6f17c6SVladimir Oltean 		unsigned int type;
15140a6f17c6SVladimir Oltean 
15150a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
15160a6f17c6SVladimir Oltean 		if (err)
15170a6f17c6SVladimir Oltean 			goto out;
15180a6f17c6SVladimir Oltean 
15190a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
15200a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
15210a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
15220a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
15230a6f17c6SVladimir Oltean 		 * here and dropping those.
15240a6f17c6SVladimir Oltean 		 */
15250a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
15260a6f17c6SVladimir Oltean 
15270a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
15280a6f17c6SVladimir Oltean 
15290a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
15300a6f17c6SVladimir Oltean 
15310a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
15320a6f17c6SVladimir Oltean 			kfree_skb(skb);
15330a6f17c6SVladimir Oltean 			continue;
15340a6f17c6SVladimir Oltean 		}
15350a6f17c6SVladimir Oltean 
15360a6f17c6SVladimir Oltean 		netif_rx(skb);
15370a6f17c6SVladimir Oltean 	}
15380a6f17c6SVladimir Oltean 
15390a6f17c6SVladimir Oltean out:
15400a6f17c6SVladimir Oltean 	if (err < 0)
15410a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
15420a6f17c6SVladimir Oltean 
15430a6f17c6SVladimir Oltean 	return true;
15440a6f17c6SVladimir Oltean }
15450a6f17c6SVladimir Oltean 
1546c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1547c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1548c0bcf537SYangbo Lu {
154992f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1550c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1551c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1552c0bcf537SYangbo Lu 	struct timespec64 ts;
155392f62485SVladimir Oltean 	u32 tstamp_hi;
155492f62485SVladimir Oltean 	u64 tstamp;
1555c0bcf537SYangbo Lu 
15560a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15570a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15580a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15590a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15600a6f17c6SVladimir Oltean 	 */
15610a6f17c6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot, type)) {
15620a6f17c6SVladimir Oltean 		kfree_skb(skb);
15630a6f17c6SVladimir Oltean 		return true;
15640a6f17c6SVladimir Oltean 	}
15650a6f17c6SVladimir Oltean 
1566c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1567c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1568c0bcf537SYangbo Lu 
1569c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1570c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1571c0bcf537SYangbo Lu 		tstamp_hi--;
1572c0bcf537SYangbo Lu 
1573c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1574c0bcf537SYangbo Lu 
1575c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1576c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1577c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1578c0bcf537SYangbo Lu 	return false;
1579c0bcf537SYangbo Lu }
1580c0bcf537SYangbo Lu 
15815c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15825c5416f5SYangbo Lu 			   struct sk_buff *skb)
1583c0bcf537SYangbo Lu {
1584c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1585682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1586c0bcf537SYangbo Lu 
1587682eaad9SYangbo Lu 	if (!ocelot->ptp)
15885c5416f5SYangbo Lu 		return;
1589c0bcf537SYangbo Lu 
159052849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
159152849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
159252849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
159352849bcfSVladimir Oltean 				    port);
1594682eaad9SYangbo Lu 		return;
159552849bcfSVladimir Oltean 	}
1596682eaad9SYangbo Lu 
1597682eaad9SYangbo Lu 	if (clone)
1598c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15995c5416f5SYangbo Lu }
1600c0bcf537SYangbo Lu 
16010b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
16020b912fc9SVladimir Oltean {
16030b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16040b912fc9SVladimir Oltean 
16050b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
16060b912fc9SVladimir Oltean 
16070b912fc9SVladimir Oltean 	return 0;
16080b912fc9SVladimir Oltean }
16090b912fc9SVladimir Oltean 
16100b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
16110b912fc9SVladimir Oltean {
16120b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
16130b912fc9SVladimir Oltean 
16140b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
16150b912fc9SVladimir Oltean }
16160b912fc9SVladimir Oltean 
161707d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
161807d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
161907d985eeSVladimir Oltean {
162007d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
162199348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
162299348004SVladimir Oltean 	bool using_tag_8021q;
162399348004SVladimir Oltean 	int err;
162407d985eeSVladimir Oltean 
162599348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
162699348004SVladimir Oltean 	if (err)
162799348004SVladimir Oltean 		return err;
162899348004SVladimir Oltean 
162999348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
163099348004SVladimir Oltean 
163199348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
163207d985eeSVladimir Oltean }
163307d985eeSVladimir Oltean 
163407d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
163507d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
163607d985eeSVladimir Oltean {
163707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
163807d985eeSVladimir Oltean 
163907d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
164007d985eeSVladimir Oltean }
164107d985eeSVladimir Oltean 
164207d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
164307d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
164407d985eeSVladimir Oltean {
164507d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
164607d985eeSVladimir Oltean 
164707d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
164807d985eeSVladimir Oltean }
164907d985eeSVladimir Oltean 
1650fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1651fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1652fc411eaaSVladimir Oltean {
1653fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1654fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1655fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16565f035af7SPo Liu 		.burst = policer->burst,
1657fc411eaaSVladimir Oltean 	};
1658fc411eaaSVladimir Oltean 
1659fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1660fc411eaaSVladimir Oltean }
1661fc411eaaSVladimir Oltean 
1662fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1663fc411eaaSVladimir Oltean {
1664fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1665fc411eaaSVladimir Oltean 
1666fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1667fc411eaaSVladimir Oltean }
1668fc411eaaSVladimir Oltean 
1669de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1670de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1671de143c0eSXiaoliang Yang 			       void *type_data)
1672de143c0eSXiaoliang Yang {
1673de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1674de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1675de143c0eSXiaoliang Yang 
1676de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1677de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1678de143c0eSXiaoliang Yang 	else
1679de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1680de143c0eSXiaoliang Yang }
1681de143c0eSXiaoliang Yang 
1682f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1683f59fd9caSVladimir Oltean 			     u16 pool_index,
1684f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1685f59fd9caSVladimir Oltean {
1686f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1687f59fd9caSVladimir Oltean 
1688f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1689f59fd9caSVladimir Oltean }
1690f59fd9caSVladimir Oltean 
1691f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1692f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1693f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1694f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1695f59fd9caSVladimir Oltean {
1696f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1697f59fd9caSVladimir Oltean 
1698f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1699f59fd9caSVladimir Oltean 				  threshold_type, extack);
1700f59fd9caSVladimir Oltean }
1701f59fd9caSVladimir Oltean 
1702f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1703f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1704f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1705f59fd9caSVladimir Oltean {
1706f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1707f59fd9caSVladimir Oltean 
1708f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1709f59fd9caSVladimir Oltean 				       p_threshold);
1710f59fd9caSVladimir Oltean }
1711f59fd9caSVladimir Oltean 
1712f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1713f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1714f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1715f59fd9caSVladimir Oltean {
1716f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1717f59fd9caSVladimir Oltean 
1718f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1719f59fd9caSVladimir Oltean 				       threshold, extack);
1720f59fd9caSVladimir Oltean }
1721f59fd9caSVladimir Oltean 
1722f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1723f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1724f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1725f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1726f59fd9caSVladimir Oltean {
1727f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1728f59fd9caSVladimir Oltean 
1729f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1730f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1731f59fd9caSVladimir Oltean 					  p_threshold);
1732f59fd9caSVladimir Oltean }
1733f59fd9caSVladimir Oltean 
1734f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1735f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1736f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1737f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1738f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1739f59fd9caSVladimir Oltean {
1740f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1741f59fd9caSVladimir Oltean 
1742f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1743f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1744f59fd9caSVladimir Oltean 					  extack);
1745f59fd9caSVladimir Oltean }
1746f59fd9caSVladimir Oltean 
1747f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1748f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1749f59fd9caSVladimir Oltean {
1750f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1751f59fd9caSVladimir Oltean 
1752f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1753f59fd9caSVladimir Oltean }
1754f59fd9caSVladimir Oltean 
1755f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1756f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1757f59fd9caSVladimir Oltean {
1758f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1759f59fd9caSVladimir Oltean 
1760f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1761f59fd9caSVladimir Oltean }
1762f59fd9caSVladimir Oltean 
1763f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1764f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1765f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1766f59fd9caSVladimir Oltean {
1767f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1768f59fd9caSVladimir Oltean 
1769f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1770f59fd9caSVladimir Oltean 					   p_cur, p_max);
1771f59fd9caSVladimir Oltean }
1772f59fd9caSVladimir Oltean 
1773f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1774f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1775f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1776f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1777f59fd9caSVladimir Oltean {
1778f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1779f59fd9caSVladimir Oltean 
1780f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1781f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1782f59fd9caSVladimir Oltean }
1783f59fd9caSVladimir Oltean 
1784a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1785a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1786a026c50bSHoratiu Vultur {
1787a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1788a026c50bSHoratiu Vultur 
1789a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1790a026c50bSHoratiu Vultur }
1791a026c50bSHoratiu Vultur 
1792a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1793a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1794a026c50bSHoratiu Vultur {
1795a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1796a026c50bSHoratiu Vultur 
1797a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1798a026c50bSHoratiu Vultur }
1799a026c50bSHoratiu Vultur 
1800a026c50bSHoratiu Vultur static int
1801a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1802a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1803a026c50bSHoratiu Vultur {
1804a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1805a026c50bSHoratiu Vultur 
1806a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1807a026c50bSHoratiu Vultur }
1808a026c50bSHoratiu Vultur 
1809a026c50bSHoratiu Vultur static int
1810a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1811a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1812a026c50bSHoratiu Vultur {
1813a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1814a026c50bSHoratiu Vultur 
1815a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1816a026c50bSHoratiu Vultur }
1817a026c50bSHoratiu Vultur 
1818375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
181956051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1820adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
182135d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
182256051948SVladimir Oltean 	.setup				= felix_setup,
182356051948SVladimir Oltean 	.teardown			= felix_teardown,
182456051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
182556051948SVladimir Oltean 	.get_strings			= felix_get_strings,
182656051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
182756051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
182856051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
182979fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1830bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1831864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1832bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1833bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
18345cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
183556051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
183656051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
183756051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1838961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1839961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1840209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1841209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1842421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1843421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
184456051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
184556051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18468fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18478fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18488fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
184956051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
185056051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
185156051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
185256051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1853c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1854c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1855c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1856c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18570b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18580b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1859fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1860fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
186107d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
186207d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
186307d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1864de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1865f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1866f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1867f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1868f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1869f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1870f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1871f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1872f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1873f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1874f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1875a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1876a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1877a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1878a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18795da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18805da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
188156051948SVladimir Oltean };
1882319e4dd1SVladimir Oltean 
1883319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1884319e4dd1SVladimir Oltean {
1885319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1886319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1887319e4dd1SVladimir Oltean 
1888319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1889319e4dd1SVladimir Oltean 		return NULL;
1890319e4dd1SVladimir Oltean 
1891319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1892319e4dd1SVladimir Oltean }
1893319e4dd1SVladimir Oltean 
1894319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1895319e4dd1SVladimir Oltean {
1896319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1897319e4dd1SVladimir Oltean 
1898319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1899319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1900319e4dd1SVladimir Oltean 		return -EINVAL;
1901319e4dd1SVladimir Oltean 
1902319e4dd1SVladimir Oltean 	return dp->index;
1903319e4dd1SVladimir Oltean }
1904