xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision f9cef64fa23f6c1ff177be5082113c5a94e34e5d)
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 
28*f9cef64fSVladimir Oltean /* Translate the DSA database API into the ocelot switch library API,
29*f9cef64fSVladimir Oltean  * which uses VID 0 for all ports that aren't part of a bridge,
30*f9cef64fSVladimir Oltean  * and expects the bridge_dev to be NULL in that case.
31*f9cef64fSVladimir Oltean  */
32*f9cef64fSVladimir Oltean static struct net_device *felix_classify_db(struct dsa_db db)
33*f9cef64fSVladimir Oltean {
34*f9cef64fSVladimir Oltean 	switch (db.type) {
35*f9cef64fSVladimir Oltean 	case DSA_DB_PORT:
36*f9cef64fSVladimir Oltean 	case DSA_DB_LAG:
37*f9cef64fSVladimir Oltean 		return NULL;
38*f9cef64fSVladimir Oltean 	case DSA_DB_BRIDGE:
39*f9cef64fSVladimir Oltean 		return db.bridge.dev;
40*f9cef64fSVladimir Oltean 	default:
41*f9cef64fSVladimir Oltean 		return ERR_PTR(-EOPNOTSUPP);
42*f9cef64fSVladimir Oltean 	}
43*f9cef64fSVladimir Oltean }
44*f9cef64fSVladimir Oltean 
45*f9cef64fSVladimir Oltean /* We are called before felix_npi_port_init(), so ocelot->npi is -1. */
46*f9cef64fSVladimir Oltean static int felix_migrate_fdbs_to_npi_port(struct dsa_switch *ds, int port,
47*f9cef64fSVladimir Oltean 					  const unsigned char *addr, u16 vid,
48*f9cef64fSVladimir Oltean 					  struct dsa_db db)
49*f9cef64fSVladimir Oltean {
50*f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
51*f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
52*f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
53*f9cef64fSVladimir Oltean 	int err;
54*f9cef64fSVladimir Oltean 
55*f9cef64fSVladimir Oltean 	err = ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
56*f9cef64fSVladimir Oltean 	if (err)
57*f9cef64fSVladimir Oltean 		return err;
58*f9cef64fSVladimir Oltean 
59*f9cef64fSVladimir Oltean 	return ocelot_fdb_add(ocelot, cpu, addr, vid, bridge_dev);
60*f9cef64fSVladimir Oltean }
61*f9cef64fSVladimir Oltean 
62*f9cef64fSVladimir Oltean static int felix_migrate_mdbs_to_npi_port(struct dsa_switch *ds, int port,
63*f9cef64fSVladimir Oltean 					  const unsigned char *addr, u16 vid,
64*f9cef64fSVladimir Oltean 					  struct dsa_db db)
65*f9cef64fSVladimir Oltean {
66*f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
67*f9cef64fSVladimir Oltean 	struct switchdev_obj_port_mdb mdb;
68*f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
69*f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
70*f9cef64fSVladimir Oltean 	int err;
71*f9cef64fSVladimir Oltean 
72*f9cef64fSVladimir Oltean 	memset(&mdb, 0, sizeof(mdb));
73*f9cef64fSVladimir Oltean 	ether_addr_copy(mdb.addr, addr);
74*f9cef64fSVladimir Oltean 	mdb.vid = vid;
75*f9cef64fSVladimir Oltean 
76*f9cef64fSVladimir Oltean 	err = ocelot_port_mdb_del(ocelot, port, &mdb, bridge_dev);
77*f9cef64fSVladimir Oltean 	if (err)
78*f9cef64fSVladimir Oltean 		return err;
79*f9cef64fSVladimir Oltean 
80*f9cef64fSVladimir Oltean 	return ocelot_port_mdb_add(ocelot, cpu, &mdb, bridge_dev);
81*f9cef64fSVladimir Oltean }
82*f9cef64fSVladimir Oltean 
83*f9cef64fSVladimir Oltean /* ocelot->npi was already set to -1 by felix_npi_port_deinit, so
84*f9cef64fSVladimir Oltean  * ocelot_fdb_add() will not redirect FDB entries towards the
85*f9cef64fSVladimir Oltean  * CPU port module here, which is what we want.
86*f9cef64fSVladimir Oltean  */
87*f9cef64fSVladimir Oltean static int
88*f9cef64fSVladimir Oltean felix_migrate_fdbs_to_tag_8021q_port(struct dsa_switch *ds, int port,
89*f9cef64fSVladimir Oltean 				     const unsigned char *addr, u16 vid,
90*f9cef64fSVladimir Oltean 				     struct dsa_db db)
91*f9cef64fSVladimir Oltean {
92*f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
93*f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
94*f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
95*f9cef64fSVladimir Oltean 	int err;
96*f9cef64fSVladimir Oltean 
97*f9cef64fSVladimir Oltean 	err = ocelot_fdb_del(ocelot, cpu, addr, vid, bridge_dev);
98*f9cef64fSVladimir Oltean 	if (err)
99*f9cef64fSVladimir Oltean 		return err;
100*f9cef64fSVladimir Oltean 
101*f9cef64fSVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
102*f9cef64fSVladimir Oltean }
103*f9cef64fSVladimir Oltean 
104*f9cef64fSVladimir Oltean static int
105*f9cef64fSVladimir Oltean felix_migrate_mdbs_to_tag_8021q_port(struct dsa_switch *ds, int port,
106*f9cef64fSVladimir Oltean 				     const unsigned char *addr, u16 vid,
107*f9cef64fSVladimir Oltean 				     struct dsa_db db)
108*f9cef64fSVladimir Oltean {
109*f9cef64fSVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
110*f9cef64fSVladimir Oltean 	struct switchdev_obj_port_mdb mdb;
111*f9cef64fSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
112*f9cef64fSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
113*f9cef64fSVladimir Oltean 	int err;
114*f9cef64fSVladimir Oltean 
115*f9cef64fSVladimir Oltean 	memset(&mdb, 0, sizeof(mdb));
116*f9cef64fSVladimir Oltean 	ether_addr_copy(mdb.addr, addr);
117*f9cef64fSVladimir Oltean 	mdb.vid = vid;
118*f9cef64fSVladimir Oltean 
119*f9cef64fSVladimir Oltean 	err = ocelot_port_mdb_del(ocelot, cpu, &mdb, bridge_dev);
120*f9cef64fSVladimir Oltean 	if (err)
121*f9cef64fSVladimir Oltean 		return err;
122*f9cef64fSVladimir Oltean 
123*f9cef64fSVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, &mdb, bridge_dev);
124*f9cef64fSVladimir Oltean }
125*f9cef64fSVladimir Oltean 
12604b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
12704b67e18SVladimir Oltean  * the tagger can perform RX source port identification.
12804b67e18SVladimir Oltean  */
12904b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid)
130e21268efSVladimir Oltean {
131e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
132e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
133e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
134e21268efSVladimir Oltean 	int key_length, upstream, err;
135e21268efSVladimir Oltean 
136e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
137e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
138e21268efSVladimir Oltean 
139e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
140e21268efSVladimir Oltean 				     GFP_KERNEL);
141e21268efSVladimir Oltean 	if (!outer_tagging_rule)
142e21268efSVladimir Oltean 		return -ENOMEM;
143e21268efSVladimir Oltean 
144e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
145e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
146c518afecSVladimir Oltean 	outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port);
147e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
148e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
149e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
150e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
151e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
152e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
153e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
154e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
155e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
156e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
157e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
158e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
159e21268efSVladimir Oltean 
160e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
161e21268efSVladimir Oltean 	if (err)
162e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
163e21268efSVladimir Oltean 
164e21268efSVladimir Oltean 	return err;
165e21268efSVladimir Oltean }
166e21268efSVladimir Oltean 
16704b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid)
16804b67e18SVladimir Oltean {
16904b67e18SVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
17004b67e18SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
17104b67e18SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
17204b67e18SVladimir Oltean 
17304b67e18SVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
17404b67e18SVladimir Oltean 
17504b67e18SVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
17604b67e18SVladimir Oltean 								 port, false);
17704b67e18SVladimir Oltean 	if (!outer_tagging_rule)
17804b67e18SVladimir Oltean 		return -ENOENT;
17904b67e18SVladimir Oltean 
18004b67e18SVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
18104b67e18SVladimir Oltean }
18204b67e18SVladimir Oltean 
18304b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
18404b67e18SVladimir Oltean  * rules for steering those tagged packets towards the correct destination port
18504b67e18SVladimir Oltean  */
18604b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid)
187e21268efSVladimir Oltean {
188e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
189e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
190e21268efSVladimir Oltean 	struct dsa_switch *ds = felix->ds;
191e21268efSVladimir Oltean 	int upstream, err;
192e21268efSVladimir Oltean 
193e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
194e21268efSVladimir Oltean 	if (!untagging_rule)
195e21268efSVladimir Oltean 		return -ENOMEM;
196e21268efSVladimir Oltean 
197e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
198e21268efSVladimir Oltean 	if (!redirect_rule) {
199e21268efSVladimir Oltean 		kfree(untagging_rule);
200e21268efSVladimir Oltean 		return -ENOMEM;
201e21268efSVladimir Oltean 	}
202e21268efSVladimir Oltean 
203e21268efSVladimir Oltean 	upstream = dsa_upstream_port(ds, port);
204e21268efSVladimir Oltean 
205e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
206e21268efSVladimir Oltean 	untagging_rule->ingress_port_mask = BIT(upstream);
207e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
208e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
209e21268efSVladimir Oltean 	untagging_rule->prio = 1;
210c518afecSVladimir Oltean 	untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
211e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
212e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
213e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
214e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
215e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
216e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
217e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
218e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
219e21268efSVladimir Oltean 
220e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
221e21268efSVladimir Oltean 	if (err) {
222e21268efSVladimir Oltean 		kfree(untagging_rule);
223e21268efSVladimir Oltean 		kfree(redirect_rule);
224e21268efSVladimir Oltean 		return err;
225e21268efSVladimir Oltean 	}
226e21268efSVladimir Oltean 
227e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
228e21268efSVladimir Oltean 	redirect_rule->ingress_port_mask = BIT(upstream);
229e21268efSVladimir Oltean 	redirect_rule->pag = port;
230e21268efSVladimir Oltean 	redirect_rule->prio = 1;
231c518afecSVladimir Oltean 	redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
232e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
233e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
234e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
235e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
236e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
237e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
238e21268efSVladimir Oltean 
239e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
240e21268efSVladimir Oltean 	if (err) {
241e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
242e21268efSVladimir Oltean 		kfree(redirect_rule);
243e21268efSVladimir Oltean 		return err;
244e21268efSVladimir Oltean 	}
245e21268efSVladimir Oltean 
246e21268efSVladimir Oltean 	return 0;
247e21268efSVladimir Oltean }
248e21268efSVladimir Oltean 
24904b67e18SVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid)
250e21268efSVladimir Oltean {
251e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
252e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
253e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
254e21268efSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
255e21268efSVladimir Oltean 	int err;
256e21268efSVladimir Oltean 
257e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
258e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
259e21268efSVladimir Oltean 
260e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
261e21268efSVladimir Oltean 							     port, false);
262e21268efSVladimir Oltean 	if (!untagging_rule)
26308f44db3SVladimir Oltean 		return -ENOENT;
264e21268efSVladimir Oltean 
265e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
266e21268efSVladimir Oltean 	if (err)
267e21268efSVladimir Oltean 		return err;
268e21268efSVladimir Oltean 
269e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
270e21268efSVladimir Oltean 							    port, false);
271e21268efSVladimir Oltean 	if (!redirect_rule)
27204b67e18SVladimir Oltean 		return -ENOENT;
273e21268efSVladimir Oltean 
274e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
275e21268efSVladimir Oltean }
276e21268efSVladimir Oltean 
27704b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
27804b67e18SVladimir Oltean 				    u16 flags)
27904b67e18SVladimir Oltean {
28004b67e18SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
28104b67e18SVladimir Oltean 	int err;
28204b67e18SVladimir Oltean 
28304b67e18SVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
28404b67e18SVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
28504b67e18SVladimir Oltean 	 * for the CPU port.
28604b67e18SVladimir Oltean 	 */
28704b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
28804b67e18SVladimir Oltean 		return 0;
28904b67e18SVladimir Oltean 
29004b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
29104b67e18SVladimir Oltean 	if (err)
29204b67e18SVladimir Oltean 		return err;
29304b67e18SVladimir Oltean 
29404b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid);
29504b67e18SVladimir Oltean 	if (err) {
29604b67e18SVladimir Oltean 		felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
29704b67e18SVladimir Oltean 		return err;
29804b67e18SVladimir Oltean 	}
29904b67e18SVladimir Oltean 
30004b67e18SVladimir Oltean 	return 0;
30104b67e18SVladimir Oltean }
30204b67e18SVladimir Oltean 
303e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
304e21268efSVladimir Oltean {
305e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
30604b67e18SVladimir Oltean 	int err;
307e21268efSVladimir Oltean 
30804b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
30904b67e18SVladimir Oltean 		return 0;
310e21268efSVladimir Oltean 
31104b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid);
31204b67e18SVladimir Oltean 	if (err)
31304b67e18SVladimir Oltean 		return err;
31404b67e18SVladimir Oltean 
31504b67e18SVladimir Oltean 	err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid);
31604b67e18SVladimir Oltean 	if (err) {
31704b67e18SVladimir Oltean 		felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid);
31804b67e18SVladimir Oltean 		return err;
31904b67e18SVladimir Oltean 	}
320e21268efSVladimir Oltean 
321e21268efSVladimir Oltean 	return 0;
322e21268efSVladimir Oltean }
323e21268efSVladimir Oltean 
324e21268efSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
325e21268efSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
326e21268efSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
327e21268efSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
328e21268efSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
329e21268efSVladimir Oltean  * register-based MMIO.
330e21268efSVladimir Oltean  */
331e21268efSVladimir Oltean static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port)
332e21268efSVladimir Oltean {
3338abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
3348abe1970SVladimir Oltean 
33554c31984SVladimir Oltean 	ocelot_port_set_dsa_8021q_cpu(ocelot, port);
336e21268efSVladimir Oltean 	ocelot->npi = -1;
337e21268efSVladimir Oltean 
338e21268efSVladimir Oltean 	/* Overwrite PGID_CPU with the non-tagging port */
339e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU);
340e21268efSVladimir Oltean 
3418abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3428abe1970SVladimir Oltean 
3438abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
344e21268efSVladimir Oltean }
345e21268efSVladimir Oltean 
346e21268efSVladimir Oltean static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port)
347e21268efSVladimir Oltean {
3488abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
3498abe1970SVladimir Oltean 
350e21268efSVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
35154c31984SVladimir Oltean 	ocelot_port_unset_dsa_8021q_cpu(ocelot, port);
352e21268efSVladimir Oltean 
353e21268efSVladimir Oltean 	/* Restore PGID_CPU */
354e21268efSVladimir Oltean 	ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID,
355e21268efSVladimir Oltean 			 PGID_CPU);
356e21268efSVladimir Oltean 
3578abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
3588abe1970SVladimir Oltean 
3598abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
360e21268efSVladimir Oltean }
361e21268efSVladimir Oltean 
36299348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
36399348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
36499348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
365c8c0ba4fSVladimir Oltean  */
36699348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
36799348004SVladimir Oltean 					      bool using_tag_8021q)
368c8c0ba4fSVladimir Oltean {
36999348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
37099348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
37199348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
37299348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
37399348004SVladimir Oltean 	unsigned long port_mask;
3742960bb14SVladimir Oltean 	struct dsa_port *dp;
37599348004SVladimir Oltean 	bool cpu_copy_ena;
37699348004SVladimir Oltean 	int cpu = -1, err;
377c8c0ba4fSVladimir Oltean 
37899348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
37999348004SVladimir Oltean 		return 0;
380c8c0ba4fSVladimir Oltean 
38199348004SVladimir Oltean 	/* Figure out the current CPU port */
3822960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
3832960bb14SVladimir Oltean 		cpu = dp->index;
3848d5f7954SVladimir Oltean 		break;
385c8c0ba4fSVladimir Oltean 	}
3868d5f7954SVladimir Oltean 
387d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
388d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
389d78637a8SVladimir Oltean 	 */
390c8c0ba4fSVladimir Oltean 
39199348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
39299348004SVladimir Oltean 	list_for_each_entry(trap, &ocelot->traps, trap_list) {
39399348004SVladimir Oltean 		/* Figure out the current trapping destination */
39499348004SVladimir Oltean 		if (using_tag_8021q) {
39599348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
39699348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
39799348004SVladimir Oltean 			 * port module.
398c8c0ba4fSVladimir Oltean 			 */
39999348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
40099348004SVladimir Oltean 			port_mask = BIT(cpu);
40199348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
402c8c0ba4fSVladimir Oltean 		} else {
40399348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
40499348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
405c8c0ba4fSVladimir Oltean 			 */
40699348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
40799348004SVladimir Oltean 			port_mask = 0;
40899348004SVladimir Oltean 			cpu_copy_ena = true;
409c8c0ba4fSVladimir Oltean 		}
410c8c0ba4fSVladimir Oltean 
41199348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
41299348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
41399348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
41499348004SVladimir Oltean 			continue;
415c8c0ba4fSVladimir Oltean 
41699348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
41799348004SVladimir Oltean 		trap->action.port_mask = port_mask;
41899348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
4190a6f17c6SVladimir Oltean 
42099348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
421c8c0ba4fSVladimir Oltean 		if (err)
422c8c0ba4fSVladimir Oltean 			return err;
42399348004SVladimir Oltean 	}
424c8c0ba4fSVladimir Oltean 
42599348004SVladimir Oltean 	return 0;
426c8c0ba4fSVladimir Oltean }
427c8c0ba4fSVladimir Oltean 
428*f9cef64fSVladimir Oltean static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu, bool change)
429e21268efSVladimir Oltean {
430e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
431e21268efSVladimir Oltean 	unsigned long cpu_flood;
4322960bb14SVladimir Oltean 	struct dsa_port *dp;
4332960bb14SVladimir Oltean 	int err;
434e21268efSVladimir Oltean 
435e21268efSVladimir Oltean 	felix_8021q_cpu_port_init(ocelot, cpu);
436e21268efSVladimir Oltean 
4372960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
438e21268efSVladimir Oltean 		/* This overwrites ocelot_init():
439e21268efSVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
440e21268efSVladimir Oltean 		 * for 2 reasons:
441e21268efSVladimir Oltean 		 * - When these packets are injected from the tag_8021q
442e21268efSVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
443e21268efSVladimir Oltean 		 *   into the system.
444e21268efSVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
445e21268efSVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
446e21268efSVladimir Oltean 		 *   port module.
447e21268efSVladimir Oltean 		 */
448e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
449e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
4502960bb14SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
451e21268efSVladimir Oltean 	}
452e21268efSVladimir Oltean 
453c8c0ba4fSVladimir Oltean 	/* In tag_8021q mode, the CPU port module is unused, except for PTP
454c8c0ba4fSVladimir Oltean 	 * frames. So we want to disable flooding of any kind to the CPU port
455c8c0ba4fSVladimir Oltean 	 * module, since packets going there will end in a black hole.
456e21268efSVladimir Oltean 	 */
457e21268efSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
458e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC);
459e21268efSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC);
460b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC);
461e21268efSVladimir Oltean 
4625da11eb4SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
463e21268efSVladimir Oltean 	if (err)
464d7b1fd52SVladimir Oltean 		return err;
465d7b1fd52SVladimir Oltean 
466*f9cef64fSVladimir Oltean 	if (change) {
467*f9cef64fSVladimir Oltean 		err = dsa_port_walk_fdbs(ds, cpu,
468*f9cef64fSVladimir Oltean 					 felix_migrate_fdbs_to_tag_8021q_port);
469d7b1fd52SVladimir Oltean 		if (err)
470d7b1fd52SVladimir Oltean 			goto out_tag_8021q_unregister;
471e21268efSVladimir Oltean 
472*f9cef64fSVladimir Oltean 		err = dsa_port_walk_mdbs(ds, cpu,
473*f9cef64fSVladimir Oltean 					 felix_migrate_mdbs_to_tag_8021q_port);
474*f9cef64fSVladimir Oltean 		if (err)
475*f9cef64fSVladimir Oltean 			goto out_migrate_fdbs;
476*f9cef64fSVladimir Oltean 	}
477*f9cef64fSVladimir Oltean 
478*f9cef64fSVladimir Oltean 	err = felix_update_trapping_destinations(ds, true);
479*f9cef64fSVladimir Oltean 	if (err)
480*f9cef64fSVladimir Oltean 		goto out_migrate_mdbs;
481*f9cef64fSVladimir Oltean 
48299348004SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
48399348004SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
48499348004SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
48599348004SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
48699348004SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
48799348004SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
48899348004SVladimir Oltean 	 */
48999348004SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
49099348004SVladimir Oltean 
491e21268efSVladimir Oltean 	return 0;
492e21268efSVladimir Oltean 
493*f9cef64fSVladimir Oltean out_migrate_mdbs:
494*f9cef64fSVladimir Oltean 	if (change)
495*f9cef64fSVladimir Oltean 		dsa_port_walk_mdbs(ds, cpu, felix_migrate_mdbs_to_npi_port);
496*f9cef64fSVladimir Oltean out_migrate_fdbs:
497*f9cef64fSVladimir Oltean 	if (change)
498*f9cef64fSVladimir Oltean 		dsa_port_walk_fdbs(ds, cpu, felix_migrate_fdbs_to_npi_port);
499d7b1fd52SVladimir Oltean out_tag_8021q_unregister:
500d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
501e21268efSVladimir Oltean 	return err;
502e21268efSVladimir Oltean }
503e21268efSVladimir Oltean 
504e21268efSVladimir Oltean static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu)
505e21268efSVladimir Oltean {
506e21268efSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5072960bb14SVladimir Oltean 	struct dsa_port *dp;
5082960bb14SVladimir Oltean 	int err;
509e21268efSVladimir Oltean 
51099348004SVladimir Oltean 	err = felix_update_trapping_destinations(ds, false);
511c8c0ba4fSVladimir Oltean 	if (err)
512c8c0ba4fSVladimir Oltean 		dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d",
513c8c0ba4fSVladimir Oltean 			err);
514c8c0ba4fSVladimir Oltean 
515d7b1fd52SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
516e21268efSVladimir Oltean 
5172960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
518e21268efSVladimir Oltean 		/* Restore the logic from ocelot_init:
519e21268efSVladimir Oltean 		 * do not forward BPDU frames to the front ports.
520e21268efSVladimir Oltean 		 */
521e21268efSVladimir Oltean 		ocelot_write_gix(ocelot,
522e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
523e21268efSVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
5242960bb14SVladimir Oltean 				 dp->index);
525e21268efSVladimir Oltean 	}
526e21268efSVladimir Oltean 
527e21268efSVladimir Oltean 	felix_8021q_cpu_port_deinit(ocelot, cpu);
528e21268efSVladimir Oltean }
529e21268efSVladimir Oltean 
530adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
531adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
532adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
533adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
534adb3dccfSVladimir Oltean  * DSA master.
535adb3dccfSVladimir Oltean  */
536adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
537adb3dccfSVladimir Oltean {
538adb3dccfSVladimir Oltean 	ocelot->npi = port;
539adb3dccfSVladimir Oltean 
540adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
541adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
542adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
543adb3dccfSVladimir Oltean 
544adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
545adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
546adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
547adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
548adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
549adb3dccfSVladimir Oltean 
550adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
551adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
552adb3dccfSVladimir Oltean }
553adb3dccfSVladimir Oltean 
554adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
555adb3dccfSVladimir Oltean {
556adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
557adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
558adb3dccfSVladimir Oltean 
559adb3dccfSVladimir Oltean 	ocelot->npi = -1;
560adb3dccfSVladimir Oltean 
561adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
562adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
563adb3dccfSVladimir Oltean 
564adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
565adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
566adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
567adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
568adb3dccfSVladimir Oltean 
569adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
570adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
571adb3dccfSVladimir Oltean }
572adb3dccfSVladimir Oltean 
573*f9cef64fSVladimir Oltean static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu, bool change)
574adb3dccfSVladimir Oltean {
575adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
576adb3dccfSVladimir Oltean 	unsigned long cpu_flood;
577*f9cef64fSVladimir Oltean 	int err;
578*f9cef64fSVladimir Oltean 
579*f9cef64fSVladimir Oltean 	if (change) {
580*f9cef64fSVladimir Oltean 		err = dsa_port_walk_fdbs(ds, cpu,
581*f9cef64fSVladimir Oltean 					 felix_migrate_fdbs_to_npi_port);
582*f9cef64fSVladimir Oltean 		if (err)
583*f9cef64fSVladimir Oltean 			return err;
584*f9cef64fSVladimir Oltean 
585*f9cef64fSVladimir Oltean 		err = dsa_port_walk_mdbs(ds, cpu,
586*f9cef64fSVladimir Oltean 					 felix_migrate_mdbs_to_npi_port);
587*f9cef64fSVladimir Oltean 		if (err)
588*f9cef64fSVladimir Oltean 			goto out_migrate_fdbs;
589*f9cef64fSVladimir Oltean 	}
590adb3dccfSVladimir Oltean 
591adb3dccfSVladimir Oltean 	felix_npi_port_init(ocelot, cpu);
592adb3dccfSVladimir Oltean 
593adb3dccfSVladimir Oltean 	/* Include the CPU port module (and indirectly, the NPI port)
594adb3dccfSVladimir Oltean 	 * in the forwarding mask for unknown unicast - the hardware
595adb3dccfSVladimir Oltean 	 * default value for ANA_FLOODING_FLD_UNICAST excludes
596adb3dccfSVladimir Oltean 	 * BIT(ocelot->num_phys_ports), and so does ocelot_init,
597adb3dccfSVladimir Oltean 	 * since Ocelot relies on whitelisting MAC addresses towards
598adb3dccfSVladimir Oltean 	 * PGID_CPU.
599adb3dccfSVladimir Oltean 	 * We do this because DSA does not yet perform RX filtering,
600adb3dccfSVladimir Oltean 	 * and the NPI port does not perform source address learning,
601adb3dccfSVladimir Oltean 	 * so traffic sent to Linux is effectively unknown from the
602adb3dccfSVladimir Oltean 	 * switch's perspective.
603adb3dccfSVladimir Oltean 	 */
604adb3dccfSVladimir Oltean 	cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports));
605adb3dccfSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC);
6066edb9e8dSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC);
607b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC);
608adb3dccfSVladimir Oltean 
609adb3dccfSVladimir Oltean 	return 0;
610*f9cef64fSVladimir Oltean 
611*f9cef64fSVladimir Oltean out_migrate_fdbs:
612*f9cef64fSVladimir Oltean 	if (change)
613*f9cef64fSVladimir Oltean 		dsa_port_walk_fdbs(ds, cpu,
614*f9cef64fSVladimir Oltean 				   felix_migrate_fdbs_to_tag_8021q_port);
615*f9cef64fSVladimir Oltean 
616*f9cef64fSVladimir Oltean 	return err;
617adb3dccfSVladimir Oltean }
618adb3dccfSVladimir Oltean 
619adb3dccfSVladimir Oltean static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu)
620adb3dccfSVladimir Oltean {
621adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
622adb3dccfSVladimir Oltean 
623adb3dccfSVladimir Oltean 	felix_npi_port_deinit(ocelot, cpu);
624adb3dccfSVladimir Oltean }
625adb3dccfSVladimir Oltean 
626adb3dccfSVladimir Oltean static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu,
627*f9cef64fSVladimir Oltean 				  enum dsa_tag_protocol proto, bool change)
628adb3dccfSVladimir Oltean {
629adb3dccfSVladimir Oltean 	int err;
630adb3dccfSVladimir Oltean 
631adb3dccfSVladimir Oltean 	switch (proto) {
6327c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
633adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
634*f9cef64fSVladimir Oltean 		err = felix_setup_tag_npi(ds, cpu, change);
635adb3dccfSVladimir Oltean 		break;
636e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
637*f9cef64fSVladimir Oltean 		err = felix_setup_tag_8021q(ds, cpu, change);
638e21268efSVladimir Oltean 		break;
639adb3dccfSVladimir Oltean 	default:
640adb3dccfSVladimir Oltean 		err = -EPROTONOSUPPORT;
641adb3dccfSVladimir Oltean 	}
642adb3dccfSVladimir Oltean 
643adb3dccfSVladimir Oltean 	return err;
644adb3dccfSVladimir Oltean }
645adb3dccfSVladimir Oltean 
646adb3dccfSVladimir Oltean static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu,
647adb3dccfSVladimir Oltean 				   enum dsa_tag_protocol proto)
648adb3dccfSVladimir Oltean {
649adb3dccfSVladimir Oltean 	switch (proto) {
6507c4bb540SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
651adb3dccfSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
652adb3dccfSVladimir Oltean 		felix_teardown_tag_npi(ds, cpu);
653adb3dccfSVladimir Oltean 		break;
654e21268efSVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
655e21268efSVladimir Oltean 		felix_teardown_tag_8021q(ds, cpu);
656e21268efSVladimir Oltean 		break;
657adb3dccfSVladimir Oltean 	default:
658adb3dccfSVladimir Oltean 		break;
659adb3dccfSVladimir Oltean 	}
660adb3dccfSVladimir Oltean }
661adb3dccfSVladimir Oltean 
662e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
663e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
664e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
665e21268efSVladimir Oltean  */
666adb3dccfSVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu,
667adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
668adb3dccfSVladimir Oltean {
669adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
670adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
671adb3dccfSVladimir Oltean 	enum dsa_tag_protocol old_proto = felix->tag_proto;
672adb3dccfSVladimir Oltean 	int err;
673adb3dccfSVladimir Oltean 
6747c4bb540SVladimir Oltean 	if (proto != DSA_TAG_PROTO_SEVILLE &&
6757c4bb540SVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT &&
676e21268efSVladimir Oltean 	    proto != DSA_TAG_PROTO_OCELOT_8021Q)
677adb3dccfSVladimir Oltean 		return -EPROTONOSUPPORT;
678adb3dccfSVladimir Oltean 
679adb3dccfSVladimir Oltean 	felix_del_tag_protocol(ds, cpu, old_proto);
680adb3dccfSVladimir Oltean 
681*f9cef64fSVladimir Oltean 	err = felix_set_tag_protocol(ds, cpu, proto, true);
682adb3dccfSVladimir Oltean 	if (err) {
683*f9cef64fSVladimir Oltean 		felix_set_tag_protocol(ds, cpu, old_proto, true);
684adb3dccfSVladimir Oltean 		return err;
685adb3dccfSVladimir Oltean 	}
686adb3dccfSVladimir Oltean 
687adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
688adb3dccfSVladimir Oltean 
689adb3dccfSVladimir Oltean 	return 0;
690adb3dccfSVladimir Oltean }
691adb3dccfSVladimir Oltean 
69256051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
6934d776482SFlorian Fainelli 						    int port,
6944d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
69556051948SVladimir Oltean {
696adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
697adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
698adb3dccfSVladimir Oltean 
699adb3dccfSVladimir Oltean 	return felix->tag_proto;
70056051948SVladimir Oltean }
70156051948SVladimir Oltean 
70256051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
70356051948SVladimir Oltean 				 unsigned int ageing_time)
70456051948SVladimir Oltean {
70556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
70656051948SVladimir Oltean 
70756051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
70856051948SVladimir Oltean 
70956051948SVladimir Oltean 	return 0;
71056051948SVladimir Oltean }
71156051948SVladimir Oltean 
7125cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
7135cad43a5SVladimir Oltean {
7145cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7155cad43a5SVladimir Oltean 	int err;
7165cad43a5SVladimir Oltean 
7175cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
7185cad43a5SVladimir Oltean 	if (err)
7195cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
7205cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7215cad43a5SVladimir Oltean }
7225cad43a5SVladimir Oltean 
72356051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
72456051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
72556051948SVladimir Oltean {
72656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72756051948SVladimir Oltean 
72856051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
72956051948SVladimir Oltean }
73056051948SVladimir Oltean 
73156051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
732c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
733c2693363SVladimir Oltean 			 struct dsa_db db)
73456051948SVladimir Oltean {
73554c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
73656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73756051948SVladimir Oltean 
73854c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
73954c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
74054c31984SVladimir Oltean 
74154c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
74256051948SVladimir Oltean }
74356051948SVladimir Oltean 
74456051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
745c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
746c2693363SVladimir Oltean 			 struct dsa_db db)
74756051948SVladimir Oltean {
74854c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
74956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
75056051948SVladimir Oltean 
75154c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
75254c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
75354c31984SVladimir Oltean 
75454c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
75556051948SVladimir Oltean }
75656051948SVladimir Oltean 
757961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
758c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
759c2693363SVladimir Oltean 			     struct dsa_db db)
760961d8b69SVladimir Oltean {
76154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
762961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
763961d8b69SVladimir Oltean 
76454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
76554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
76654c31984SVladimir Oltean 
76754c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
768961d8b69SVladimir Oltean }
769961d8b69SVladimir Oltean 
770961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
771c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
772c2693363SVladimir Oltean 			     struct dsa_db db)
773961d8b69SVladimir Oltean {
77454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
775961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
776961d8b69SVladimir Oltean 
77754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
77854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
77954c31984SVladimir Oltean 
78054c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
781961d8b69SVladimir Oltean }
782961d8b69SVladimir Oltean 
783a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
784c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
785c2693363SVladimir Oltean 			 struct dsa_db db)
786209edf95SVladimir Oltean {
78754c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
788209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
789209edf95SVladimir Oltean 
79054c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
79154c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
79254c31984SVladimir Oltean 
79354c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
794209edf95SVladimir Oltean }
795209edf95SVladimir Oltean 
796209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
797c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
798c2693363SVladimir Oltean 			 struct dsa_db db)
799209edf95SVladimir Oltean {
80054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
801209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
802209edf95SVladimir Oltean 
80354c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
80454c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
80554c31984SVladimir Oltean 
80654c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
807209edf95SVladimir Oltean }
808209edf95SVladimir Oltean 
80956051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
81056051948SVladimir Oltean 				       u8 state)
81156051948SVladimir Oltean {
81256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
81356051948SVladimir Oltean 
81456051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
81556051948SVladimir Oltean }
81656051948SVladimir Oltean 
817421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
818421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
819421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
820421741eaSVladimir Oltean {
821421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
822421741eaSVladimir Oltean 
823421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
824421741eaSVladimir Oltean }
825421741eaSVladimir Oltean 
826421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
827421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
828421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
829421741eaSVladimir Oltean {
830421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
831421741eaSVladimir Oltean 
832421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
833421741eaSVladimir Oltean 
834421741eaSVladimir Oltean 	return 0;
835421741eaSVladimir Oltean }
836421741eaSVladimir Oltean 
83756051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
83806b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
83906b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
84056051948SVladimir Oltean {
84156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
84256051948SVladimir Oltean 
84354c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
84454c31984SVladimir Oltean 				       extack);
84556051948SVladimir Oltean }
84656051948SVladimir Oltean 
84756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
848d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
84956051948SVladimir Oltean {
85056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
85156051948SVladimir Oltean 
852d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
85356051948SVladimir Oltean }
85456051948SVladimir Oltean 
8558fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
856dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
8578fe6832eSVladimir Oltean 			  struct netdev_lag_upper_info *info)
8588fe6832eSVladimir Oltean {
8598fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8608fe6832eSVladimir Oltean 
861dedd6a00SVladimir Oltean 	return ocelot_port_lag_join(ocelot, port, lag.dev, info);
8628fe6832eSVladimir Oltean }
8638fe6832eSVladimir Oltean 
8648fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
865dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
8668fe6832eSVladimir Oltean {
8678fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8688fe6832eSVladimir Oltean 
869dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
8708fe6832eSVladimir Oltean 
8718fe6832eSVladimir Oltean 	return 0;
8728fe6832eSVladimir Oltean }
8738fe6832eSVladimir Oltean 
8748fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
8758fe6832eSVladimir Oltean {
8768fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
8778fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8788fe6832eSVladimir Oltean 
8798fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
8808fe6832eSVladimir Oltean 
8818fe6832eSVladimir Oltean 	return 0;
8828fe6832eSVladimir Oltean }
8838fe6832eSVladimir Oltean 
88456051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
88501af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
88601af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
88756051948SVladimir Oltean {
8882f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
889b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
8902f0402feSVladimir Oltean 
8919a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
8929a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
8939a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
8949a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
8959a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
8969a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
8979a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
8989a720680SVladimir Oltean 	 */
8999a720680SVladimir Oltean 	if (port == ocelot->npi)
9009a720680SVladimir Oltean 		return 0;
9019a720680SVladimir Oltean 
902b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
9032f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
90401af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
90501af940eSVladimir Oltean 				   extack);
90656051948SVladimir Oltean }
90756051948SVladimir Oltean 
90889153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
90989153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
91056051948SVladimir Oltean {
91156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
91256051948SVladimir Oltean 
9133b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
91456051948SVladimir Oltean }
91556051948SVladimir Oltean 
9161958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
91731046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
91831046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
91956051948SVladimir Oltean {
92056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
921183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
9221958d581SVladimir Oltean 	int err;
92356051948SVladimir Oltean 
92401af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
9251958d581SVladimir Oltean 	if (err)
9261958d581SVladimir Oltean 		return err;
9271958d581SVladimir Oltean 
9281958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
929183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
930183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
93156051948SVladimir Oltean }
93256051948SVladimir Oltean 
93356051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
93456051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
93556051948SVladimir Oltean {
93656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
93756051948SVladimir Oltean 
938b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
93956051948SVladimir Oltean }
94056051948SVladimir Oltean 
94179fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
94279fda660SRussell King (Oracle) 				   struct phylink_config *config)
94379fda660SRussell King (Oracle) {
94479fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
94579fda660SRussell King (Oracle) 
946f6f04c02SRussell King (Oracle) 	/* This driver does not make use of the speed, duplex, pause or the
947f6f04c02SRussell King (Oracle) 	 * advertisement in its mac_config, so it is safe to mark this driver
948f6f04c02SRussell King (Oracle) 	 * as non-legacy.
949f6f04c02SRussell King (Oracle) 	 */
950f6f04c02SRussell King (Oracle) 	config->legacy_pre_march2020 = false;
951f6f04c02SRussell King (Oracle) 
95279fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
95379fda660SRussell King (Oracle) 		  config->supported_interfaces);
95479fda660SRussell King (Oracle) }
95579fda660SRussell King (Oracle) 
956bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
957bdeced75SVladimir Oltean 				   unsigned long *supported,
958bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
959bdeced75SVladimir Oltean {
960bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
961375e1314SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
962bdeced75SVladimir Oltean 
963375e1314SVladimir Oltean 	if (felix->info->phylink_validate)
964375e1314SVladimir Oltean 		felix->info->phylink_validate(ocelot, port, supported, state);
965bdeced75SVladimir Oltean }
966bdeced75SVladimir Oltean 
967864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
968864ba485SRussell King (Oracle) 							int port,
969864ba485SRussell King (Oracle) 							phy_interface_t iface)
970bdeced75SVladimir Oltean {
971bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
972bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
973864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
974bdeced75SVladimir Oltean 
97549af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
976864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
977864ba485SRussell King (Oracle) 
978864ba485SRussell King (Oracle) 	return pcs;
979bdeced75SVladimir Oltean }
980bdeced75SVladimir Oltean 
981bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
982bdeced75SVladimir Oltean 					unsigned int link_an_mode,
983bdeced75SVladimir Oltean 					phy_interface_t interface)
984bdeced75SVladimir Oltean {
985bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
986bdeced75SVladimir Oltean 
987e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
988e6e12df6SVladimir Oltean 				     FELIX_MAC_QUIRKS);
989bdeced75SVladimir Oltean }
990bdeced75SVladimir Oltean 
991bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
992bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
993bdeced75SVladimir Oltean 				      phy_interface_t interface,
9945b502a7bSRussell King 				      struct phy_device *phydev,
9955b502a7bSRussell King 				      int speed, int duplex,
9965b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
997bdeced75SVladimir Oltean {
998bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9997e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1000bdeced75SVladimir Oltean 
1001e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1002e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
1003e6e12df6SVladimir Oltean 				   FELIX_MAC_QUIRKS);
10047e14a2dcSVladimir Oltean 
10057e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
10067e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1007bdeced75SVladimir Oltean }
1008bdeced75SVladimir Oltean 
1009bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1010bd2b3161SXiaoliang Yang {
1011bd2b3161SXiaoliang Yang 	int i;
1012bd2b3161SXiaoliang Yang 
1013bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1014bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1015bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1016bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1017bd2b3161SXiaoliang Yang 		       port);
1018bd2b3161SXiaoliang Yang 
101970d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1020bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1021bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1022bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1023bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1024bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1025bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1026bd2b3161SXiaoliang Yang 			      port, i);
1027bd2b3161SXiaoliang Yang 	}
1028bd2b3161SXiaoliang Yang }
1029bd2b3161SXiaoliang Yang 
103056051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
103156051948SVladimir Oltean 			      u32 stringset, u8 *data)
103256051948SVladimir Oltean {
103356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103456051948SVladimir Oltean 
103556051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
103656051948SVladimir Oltean }
103756051948SVladimir Oltean 
103856051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
103956051948SVladimir Oltean {
104056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104156051948SVladimir Oltean 
104256051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
104356051948SVladimir Oltean }
104456051948SVladimir Oltean 
104556051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
104656051948SVladimir Oltean {
104756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
104856051948SVladimir Oltean 
104956051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
105056051948SVladimir Oltean }
105156051948SVladimir Oltean 
105256051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
105356051948SVladimir Oltean 			     struct ethtool_ts_info *info)
105456051948SVladimir Oltean {
105556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105656051948SVladimir Oltean 
105756051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
105856051948SVladimir Oltean }
105956051948SVladimir Oltean 
1060acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1061acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1062acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1063acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1064acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
1065acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1066acf242fcSColin Foster };
1067acf242fcSColin Foster 
1068acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1069acf242fcSColin Foster 				   phy_interface_t phy_mode)
1070acf242fcSColin Foster {
1071acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1072acf242fcSColin Foster 
1073acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1074acf242fcSColin Foster 		return 0;
1075acf242fcSColin Foster 	return -EOPNOTSUPP;
1076acf242fcSColin Foster }
1077acf242fcSColin Foster 
1078bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1079bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1080bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1081bdeced75SVladimir Oltean {
1082bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1083bdeced75SVladimir Oltean 	struct device_node *child;
1084bdeced75SVladimir Oltean 
108537fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1086bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1087bdeced75SVladimir Oltean 		u32 port;
1088bdeced75SVladimir Oltean 		int err;
1089bdeced75SVladimir Oltean 
1090bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1091bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1092bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1093bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1094bdeced75SVladimir Oltean 			of_node_put(child);
1095bdeced75SVladimir Oltean 			return -ENODEV;
1096bdeced75SVladimir Oltean 		}
1097bdeced75SVladimir Oltean 
1098bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1099bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1100bdeced75SVladimir Oltean 		if (err) {
1101bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1102bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1103bdeced75SVladimir Oltean 				port);
1104bdeced75SVladimir Oltean 			of_node_put(child);
1105bdeced75SVladimir Oltean 			return -ENODEV;
1106bdeced75SVladimir Oltean 		}
1107bdeced75SVladimir Oltean 
1108acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1109bdeced75SVladimir Oltean 		if (err < 0) {
1110bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
1111bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
111259ebb430SSumera Priyadarsini 			of_node_put(child);
1113bdeced75SVladimir Oltean 			return err;
1114bdeced75SVladimir Oltean 		}
1115bdeced75SVladimir Oltean 
1116bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1117bdeced75SVladimir Oltean 	}
1118bdeced75SVladimir Oltean 
1119bdeced75SVladimir Oltean 	return 0;
1120bdeced75SVladimir Oltean }
1121bdeced75SVladimir Oltean 
1122bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1123bdeced75SVladimir Oltean {
1124bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1125bdeced75SVladimir Oltean 	struct device_node *switch_node;
1126bdeced75SVladimir Oltean 	struct device_node *ports_node;
1127bdeced75SVladimir Oltean 	int err;
1128bdeced75SVladimir Oltean 
1129bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1130bdeced75SVladimir Oltean 
1131bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1132abecbfcdSVladimir Oltean 	if (!ports_node)
1133abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1134bdeced75SVladimir Oltean 	if (!ports_node) {
1135abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1136bdeced75SVladimir Oltean 		return -ENODEV;
1137bdeced75SVladimir Oltean 	}
1138bdeced75SVladimir Oltean 
1139bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1140bdeced75SVladimir Oltean 	of_node_put(ports_node);
1141bdeced75SVladimir Oltean 
1142bdeced75SVladimir Oltean 	return err;
1143bdeced75SVladimir Oltean }
1144bdeced75SVladimir Oltean 
114556051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
114656051948SVladimir Oltean {
114756051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1148bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
1149b4024c9eSClaudiu Manoil 	struct resource res;
115056051948SVladimir Oltean 	int port, i, err;
115156051948SVladimir Oltean 
115256051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
115356051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
115456051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
115556051948SVladimir Oltean 	if (!ocelot->ports)
115656051948SVladimir Oltean 		return -ENOMEM;
115756051948SVladimir Oltean 
115856051948SVladimir Oltean 	ocelot->map		= felix->info->map;
115956051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
116056051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
116121ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
116207d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
116377043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
116477043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
116577043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
116677043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
116756051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1168cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1169cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1170f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
117156051948SVladimir Oltean 
1172bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1173bdeced75SVladimir Oltean 				 GFP_KERNEL);
1174bdeced75SVladimir Oltean 	if (!port_phy_modes)
1175bdeced75SVladimir Oltean 		return -ENOMEM;
1176bdeced75SVladimir Oltean 
1177bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1178bdeced75SVladimir Oltean 	if (err) {
1179bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1180bdeced75SVladimir Oltean 		return err;
1181bdeced75SVladimir Oltean 	}
1182bdeced75SVladimir Oltean 
118356051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
118456051948SVladimir Oltean 		struct regmap *target;
118556051948SVladimir Oltean 
118656051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
118756051948SVladimir Oltean 			continue;
118856051948SVladimir Oltean 
1189b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
1190b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1191375e1314SVladimir Oltean 		res.start += felix->switch_base;
1192375e1314SVladimir Oltean 		res.end += felix->switch_base;
119356051948SVladimir Oltean 
1194242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
119556051948SVladimir Oltean 		if (IS_ERR(target)) {
119656051948SVladimir Oltean 			dev_err(ocelot->dev,
119756051948SVladimir Oltean 				"Failed to map device memory space\n");
1198bdeced75SVladimir Oltean 			kfree(port_phy_modes);
119956051948SVladimir Oltean 			return PTR_ERR(target);
120056051948SVladimir Oltean 		}
120156051948SVladimir Oltean 
120256051948SVladimir Oltean 		ocelot->targets[i] = target;
120356051948SVladimir Oltean 	}
120456051948SVladimir Oltean 
120556051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
120656051948SVladimir Oltean 	if (err) {
120756051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1208bdeced75SVladimir Oltean 		kfree(port_phy_modes);
120956051948SVladimir Oltean 		return err;
121056051948SVladimir Oltean 	}
121156051948SVladimir Oltean 
121256051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
121356051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
121491c724cfSVladimir Oltean 		struct regmap *target;
121556051948SVladimir Oltean 
121656051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
121756051948SVladimir Oltean 					   sizeof(struct ocelot_port),
121856051948SVladimir Oltean 					   GFP_KERNEL);
121956051948SVladimir Oltean 		if (!ocelot_port) {
122056051948SVladimir Oltean 			dev_err(ocelot->dev,
122156051948SVladimir Oltean 				"failed to allocate port memory\n");
1222bdeced75SVladimir Oltean 			kfree(port_phy_modes);
122356051948SVladimir Oltean 			return -ENOMEM;
122456051948SVladimir Oltean 		}
122556051948SVladimir Oltean 
1226b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
1227b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
1228375e1314SVladimir Oltean 		res.start += felix->switch_base;
1229375e1314SVladimir Oltean 		res.end += felix->switch_base;
123056051948SVladimir Oltean 
1231242bd0c1SColin Foster 		target = felix->info->init_regmap(ocelot, &res);
123291c724cfSVladimir Oltean 		if (IS_ERR(target)) {
123356051948SVladimir Oltean 			dev_err(ocelot->dev,
123491c724cfSVladimir Oltean 				"Failed to map memory space for port %d\n",
123591c724cfSVladimir Oltean 				port);
1236bdeced75SVladimir Oltean 			kfree(port_phy_modes);
123791c724cfSVladimir Oltean 			return PTR_ERR(target);
123856051948SVladimir Oltean 		}
123956051948SVladimir Oltean 
1240bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
124156051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
124291c724cfSVladimir Oltean 		ocelot_port->target = target;
124356051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
124456051948SVladimir Oltean 	}
124556051948SVladimir Oltean 
1246bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1247bdeced75SVladimir Oltean 
1248bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1249bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1250bdeced75SVladimir Oltean 		if (err < 0)
1251bdeced75SVladimir Oltean 			return err;
1252bdeced75SVladimir Oltean 	}
1253bdeced75SVladimir Oltean 
125456051948SVladimir Oltean 	return 0;
125556051948SVladimir Oltean }
125656051948SVladimir Oltean 
12571328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
12581328a883SVladimir Oltean 					   struct sk_buff *skb)
12591328a883SVladimir Oltean {
12601328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12611328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
12621328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
12631328a883SVladimir Oltean 	unsigned long flags;
12641328a883SVladimir Oltean 
12651328a883SVladimir Oltean 	if (!clone)
12661328a883SVladimir Oltean 		return;
12671328a883SVladimir Oltean 
12681328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
12691328a883SVladimir Oltean 
12701328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
12711328a883SVladimir Oltean 		if (skb != clone)
12721328a883SVladimir Oltean 			continue;
12731328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
12741328a883SVladimir Oltean 		skb_match = skb;
12751328a883SVladimir Oltean 		break;
12761328a883SVladimir Oltean 	}
12771328a883SVladimir Oltean 
12781328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
12791328a883SVladimir Oltean 
12801328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
12811328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
12821328a883SVladimir Oltean }
12831328a883SVladimir Oltean 
128449f885b2SVladimir Oltean #define work_to_xmit_work(w) \
128549f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
128649f885b2SVladimir Oltean 
128749f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
128849f885b2SVladimir Oltean {
128949f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
129049f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
129149f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
129249f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
129349f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
129449f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
129549f885b2SVladimir Oltean 	int retries = 10;
129649f885b2SVladimir Oltean 
129749f885b2SVladimir Oltean 	do {
129849f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
129949f885b2SVladimir Oltean 			break;
130049f885b2SVladimir Oltean 
130149f885b2SVladimir Oltean 		cpu_relax();
130249f885b2SVladimir Oltean 	} while (--retries);
130349f885b2SVladimir Oltean 
130449f885b2SVladimir Oltean 	if (!retries) {
130549f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
130649f885b2SVladimir Oltean 			port);
13071328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
130849f885b2SVladimir Oltean 		kfree_skb(skb);
130949f885b2SVladimir Oltean 		return;
131049f885b2SVladimir Oltean 	}
131149f885b2SVladimir Oltean 
131249f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
131349f885b2SVladimir Oltean 
131449f885b2SVladimir Oltean 	consume_skb(skb);
131549f885b2SVladimir Oltean 	kfree(xmit_work);
131649f885b2SVladimir Oltean }
131749f885b2SVladimir Oltean 
131835d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
131935d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
132049f885b2SVladimir Oltean {
132135d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
132249f885b2SVladimir Oltean 
132335d97680SVladimir Oltean 	switch (proto) {
132435d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
132535d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
132635d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
132749f885b2SVladimir Oltean 		return 0;
132835d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
132935d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
133049f885b2SVladimir Oltean 		return 0;
133135d97680SVladimir Oltean 	default:
133235d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
133349f885b2SVladimir Oltean 	}
133449f885b2SVladimir Oltean }
133549f885b2SVladimir Oltean 
133656051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
133756051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
133856051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
133956051948SVladimir Oltean  * (which will not work).
134056051948SVladimir Oltean  */
134156051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
134256051948SVladimir Oltean {
134356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
134456051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
13452960bb14SVladimir Oltean 	struct dsa_port *dp;
13462960bb14SVladimir Oltean 	int err;
134756051948SVladimir Oltean 
134856051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
134956051948SVladimir Oltean 	if (err)
135056051948SVladimir Oltean 		return err;
135156051948SVladimir Oltean 
1352d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1353d1cc0e93SVladimir Oltean 	if (err)
13546b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1355d1cc0e93SVladimir Oltean 
13562b49d128SYangbo Lu 	if (ocelot->ptp) {
13572ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
13582b49d128SYangbo Lu 		if (err) {
13592b49d128SYangbo Lu 			dev_err(ocelot->dev,
13602b49d128SYangbo Lu 				"Timestamp initialization failed\n");
13612b49d128SYangbo Lu 			ocelot->ptp = 0;
13622b49d128SYangbo Lu 		}
13632b49d128SYangbo Lu 	}
136456051948SVladimir Oltean 
13652960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
13662960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1367bd2b3161SXiaoliang Yang 
1368bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1369bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1370bd2b3161SXiaoliang Yang 		 */
13712960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
137256051948SVladimir Oltean 	}
137356051948SVladimir Oltean 
1374f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1375f59fd9caSVladimir Oltean 	if (err)
13766b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1377f59fd9caSVladimir Oltean 
13782960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
1379adb3dccfSVladimir Oltean 		/* The initial tag protocol is NPI which always returns 0, so
1380adb3dccfSVladimir Oltean 		 * there's no real point in checking for errors.
13811cf3299bSVladimir Oltean 		 */
1382*f9cef64fSVladimir Oltean 		felix_set_tag_protocol(ds, dp->index, felix->tag_proto, false);
13838d5f7954SVladimir Oltean 		break;
1384adb3dccfSVladimir Oltean 	}
13851cf3299bSVladimir Oltean 
13860b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1387c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
138854c31984SVladimir Oltean 	ds->fdb_isolation = true;
138954c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1390bdeced75SVladimir Oltean 
139156051948SVladimir Oltean 	return 0;
13926b73b7c9SVladimir Oltean 
13936b73b7c9SVladimir Oltean out_deinit_ports:
13942960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
13952960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
13966b73b7c9SVladimir Oltean 
13976b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
13986b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
13996b73b7c9SVladimir Oltean 
14006b73b7c9SVladimir Oltean out_mdiobus_free:
14016b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
14026b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
14036b73b7c9SVladimir Oltean 
14046b73b7c9SVladimir Oltean 	return err;
140556051948SVladimir Oltean }
140656051948SVladimir Oltean 
140756051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
140856051948SVladimir Oltean {
140956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1410bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14112960bb14SVladimir Oltean 	struct dsa_port *dp;
1412bdeced75SVladimir Oltean 
14132960bb14SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds) {
14142960bb14SVladimir Oltean 		felix_del_tag_protocol(ds, dp->index, felix->tag_proto);
14158d5f7954SVladimir Oltean 		break;
1416adb3dccfSVladimir Oltean 	}
1417adb3dccfSVladimir Oltean 
14182960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
14192960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1420d19741b0SVladimir Oltean 
142149f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
142249f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
142349f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
142449f885b2SVladimir Oltean 
1425d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1426d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
142756051948SVladimir Oltean }
142856051948SVladimir Oltean 
1429c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1430c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1431c0bcf537SYangbo Lu {
1432c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1433c0bcf537SYangbo Lu 
1434c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1435c0bcf537SYangbo Lu }
1436c0bcf537SYangbo Lu 
1437c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1438c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1439c0bcf537SYangbo Lu {
1440c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
144199348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
144299348004SVladimir Oltean 	bool using_tag_8021q;
144399348004SVladimir Oltean 	int err;
1444c0bcf537SYangbo Lu 
144599348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
144699348004SVladimir Oltean 	if (err)
144799348004SVladimir Oltean 		return err;
144899348004SVladimir Oltean 
144999348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
145099348004SVladimir Oltean 
145199348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1452c0bcf537SYangbo Lu }
1453c0bcf537SYangbo Lu 
14540a6f17c6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type)
14550a6f17c6SVladimir Oltean {
14560a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
14570a6f17c6SVladimir Oltean 	int err, grp = 0;
14580a6f17c6SVladimir Oltean 
14590a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
14600a6f17c6SVladimir Oltean 		return false;
14610a6f17c6SVladimir Oltean 
14620a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
14630a6f17c6SVladimir Oltean 		return false;
14640a6f17c6SVladimir Oltean 
14650a6f17c6SVladimir Oltean 	if (ptp_type == PTP_CLASS_NONE)
14660a6f17c6SVladimir Oltean 		return false;
14670a6f17c6SVladimir Oltean 
14680a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
14690a6f17c6SVladimir Oltean 		struct sk_buff *skb;
14700a6f17c6SVladimir Oltean 		unsigned int type;
14710a6f17c6SVladimir Oltean 
14720a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
14730a6f17c6SVladimir Oltean 		if (err)
14740a6f17c6SVladimir Oltean 			goto out;
14750a6f17c6SVladimir Oltean 
14760a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
14770a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
14780a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
14790a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
14800a6f17c6SVladimir Oltean 		 * here and dropping those.
14810a6f17c6SVladimir Oltean 		 */
14820a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
14830a6f17c6SVladimir Oltean 
14840a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
14850a6f17c6SVladimir Oltean 
14860a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
14870a6f17c6SVladimir Oltean 
14880a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
14890a6f17c6SVladimir Oltean 			kfree_skb(skb);
14900a6f17c6SVladimir Oltean 			continue;
14910a6f17c6SVladimir Oltean 		}
14920a6f17c6SVladimir Oltean 
14930a6f17c6SVladimir Oltean 		netif_rx(skb);
14940a6f17c6SVladimir Oltean 	}
14950a6f17c6SVladimir Oltean 
14960a6f17c6SVladimir Oltean out:
14970a6f17c6SVladimir Oltean 	if (err < 0)
14980a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
14990a6f17c6SVladimir Oltean 
15000a6f17c6SVladimir Oltean 	return true;
15010a6f17c6SVladimir Oltean }
15020a6f17c6SVladimir Oltean 
1503c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1504c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1505c0bcf537SYangbo Lu {
150692f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1507c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1508c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1509c0bcf537SYangbo Lu 	struct timespec64 ts;
151092f62485SVladimir Oltean 	u32 tstamp_hi;
151192f62485SVladimir Oltean 	u64 tstamp;
1512c0bcf537SYangbo Lu 
15130a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
15140a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
15150a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
15160a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
15170a6f17c6SVladimir Oltean 	 */
15180a6f17c6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot, type)) {
15190a6f17c6SVladimir Oltean 		kfree_skb(skb);
15200a6f17c6SVladimir Oltean 		return true;
15210a6f17c6SVladimir Oltean 	}
15220a6f17c6SVladimir Oltean 
1523c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1524c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1525c0bcf537SYangbo Lu 
1526c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1527c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1528c0bcf537SYangbo Lu 		tstamp_hi--;
1529c0bcf537SYangbo Lu 
1530c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1531c0bcf537SYangbo Lu 
1532c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1533c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1534c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1535c0bcf537SYangbo Lu 	return false;
1536c0bcf537SYangbo Lu }
1537c0bcf537SYangbo Lu 
15385c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
15395c5416f5SYangbo Lu 			   struct sk_buff *skb)
1540c0bcf537SYangbo Lu {
1541c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1542682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1543c0bcf537SYangbo Lu 
1544682eaad9SYangbo Lu 	if (!ocelot->ptp)
15455c5416f5SYangbo Lu 		return;
1546c0bcf537SYangbo Lu 
154752849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
154852849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
154952849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
155052849bcfSVladimir Oltean 				    port);
1551682eaad9SYangbo Lu 		return;
155252849bcfSVladimir Oltean 	}
1553682eaad9SYangbo Lu 
1554682eaad9SYangbo Lu 	if (clone)
1555c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
15565c5416f5SYangbo Lu }
1557c0bcf537SYangbo Lu 
15580b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
15590b912fc9SVladimir Oltean {
15600b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15610b912fc9SVladimir Oltean 
15620b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
15630b912fc9SVladimir Oltean 
15640b912fc9SVladimir Oltean 	return 0;
15650b912fc9SVladimir Oltean }
15660b912fc9SVladimir Oltean 
15670b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
15680b912fc9SVladimir Oltean {
15690b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15700b912fc9SVladimir Oltean 
15710b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
15720b912fc9SVladimir Oltean }
15730b912fc9SVladimir Oltean 
157407d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
157507d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
157607d985eeSVladimir Oltean {
157707d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
157899348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
157999348004SVladimir Oltean 	bool using_tag_8021q;
158099348004SVladimir Oltean 	int err;
158107d985eeSVladimir Oltean 
158299348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
158399348004SVladimir Oltean 	if (err)
158499348004SVladimir Oltean 		return err;
158599348004SVladimir Oltean 
158699348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
158799348004SVladimir Oltean 
158899348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
158907d985eeSVladimir Oltean }
159007d985eeSVladimir Oltean 
159107d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
159207d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
159307d985eeSVladimir Oltean {
159407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
159507d985eeSVladimir Oltean 
159607d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
159707d985eeSVladimir Oltean }
159807d985eeSVladimir Oltean 
159907d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
160007d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
160107d985eeSVladimir Oltean {
160207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
160307d985eeSVladimir Oltean 
160407d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
160507d985eeSVladimir Oltean }
160607d985eeSVladimir Oltean 
1607fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1608fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1609fc411eaaSVladimir Oltean {
1610fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1611fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1612fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
16135f035af7SPo Liu 		.burst = policer->burst,
1614fc411eaaSVladimir Oltean 	};
1615fc411eaaSVladimir Oltean 
1616fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1617fc411eaaSVladimir Oltean }
1618fc411eaaSVladimir Oltean 
1619fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1620fc411eaaSVladimir Oltean {
1621fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1622fc411eaaSVladimir Oltean 
1623fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1624fc411eaaSVladimir Oltean }
1625fc411eaaSVladimir Oltean 
1626de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1627de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1628de143c0eSXiaoliang Yang 			       void *type_data)
1629de143c0eSXiaoliang Yang {
1630de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1631de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1632de143c0eSXiaoliang Yang 
1633de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1634de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1635de143c0eSXiaoliang Yang 	else
1636de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1637de143c0eSXiaoliang Yang }
1638de143c0eSXiaoliang Yang 
1639f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1640f59fd9caSVladimir Oltean 			     u16 pool_index,
1641f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1642f59fd9caSVladimir Oltean {
1643f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1644f59fd9caSVladimir Oltean 
1645f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1646f59fd9caSVladimir Oltean }
1647f59fd9caSVladimir Oltean 
1648f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1649f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1650f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1651f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1652f59fd9caSVladimir Oltean {
1653f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1654f59fd9caSVladimir Oltean 
1655f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1656f59fd9caSVladimir Oltean 				  threshold_type, extack);
1657f59fd9caSVladimir Oltean }
1658f59fd9caSVladimir Oltean 
1659f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1660f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1661f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1662f59fd9caSVladimir Oltean {
1663f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1664f59fd9caSVladimir Oltean 
1665f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1666f59fd9caSVladimir Oltean 				       p_threshold);
1667f59fd9caSVladimir Oltean }
1668f59fd9caSVladimir Oltean 
1669f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1670f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1671f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1672f59fd9caSVladimir Oltean {
1673f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1674f59fd9caSVladimir Oltean 
1675f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1676f59fd9caSVladimir Oltean 				       threshold, extack);
1677f59fd9caSVladimir Oltean }
1678f59fd9caSVladimir Oltean 
1679f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1680f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1681f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1682f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1683f59fd9caSVladimir Oltean {
1684f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1685f59fd9caSVladimir Oltean 
1686f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1687f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1688f59fd9caSVladimir Oltean 					  p_threshold);
1689f59fd9caSVladimir Oltean }
1690f59fd9caSVladimir Oltean 
1691f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1692f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1693f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1694f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1695f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1696f59fd9caSVladimir Oltean {
1697f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1698f59fd9caSVladimir Oltean 
1699f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1700f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1701f59fd9caSVladimir Oltean 					  extack);
1702f59fd9caSVladimir Oltean }
1703f59fd9caSVladimir Oltean 
1704f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1705f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1706f59fd9caSVladimir Oltean {
1707f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1708f59fd9caSVladimir Oltean 
1709f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1710f59fd9caSVladimir Oltean }
1711f59fd9caSVladimir Oltean 
1712f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1713f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1714f59fd9caSVladimir Oltean {
1715f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1716f59fd9caSVladimir Oltean 
1717f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1718f59fd9caSVladimir Oltean }
1719f59fd9caSVladimir Oltean 
1720f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1721f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1722f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1723f59fd9caSVladimir Oltean {
1724f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1725f59fd9caSVladimir Oltean 
1726f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1727f59fd9caSVladimir Oltean 					   p_cur, p_max);
1728f59fd9caSVladimir Oltean }
1729f59fd9caSVladimir Oltean 
1730f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1731f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1732f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1733f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1734f59fd9caSVladimir Oltean {
1735f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1736f59fd9caSVladimir Oltean 
1737f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1738f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1739f59fd9caSVladimir Oltean }
1740f59fd9caSVladimir Oltean 
1741a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
1742a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1743a026c50bSHoratiu Vultur {
1744a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1745a026c50bSHoratiu Vultur 
1746a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1747a026c50bSHoratiu Vultur }
1748a026c50bSHoratiu Vultur 
1749a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
1750a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
1751a026c50bSHoratiu Vultur {
1752a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1753a026c50bSHoratiu Vultur 
1754a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
1755a026c50bSHoratiu Vultur }
1756a026c50bSHoratiu Vultur 
1757a026c50bSHoratiu Vultur static int
1758a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
1759a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1760a026c50bSHoratiu Vultur {
1761a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1762a026c50bSHoratiu Vultur 
1763a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1764a026c50bSHoratiu Vultur }
1765a026c50bSHoratiu Vultur 
1766a026c50bSHoratiu Vultur static int
1767a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
1768a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
1769a026c50bSHoratiu Vultur {
1770a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
1771a026c50bSHoratiu Vultur 
1772a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1773a026c50bSHoratiu Vultur }
1774a026c50bSHoratiu Vultur 
1775375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
177656051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
1777adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
177835d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
177956051948SVladimir Oltean 	.setup				= felix_setup,
178056051948SVladimir Oltean 	.teardown			= felix_teardown,
178156051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
178256051948SVladimir Oltean 	.get_strings			= felix_get_strings,
178356051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
178456051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
178556051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
178679fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
1787bdeced75SVladimir Oltean 	.phylink_validate		= felix_phylink_validate,
1788864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
1789bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
1790bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
17915cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
179256051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
179356051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
179456051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
1795961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
1796961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
1797209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
1798209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
1799421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
1800421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
180156051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
180256051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
18038fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
18048fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
18058fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
180656051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
180756051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
180856051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
180956051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
1810c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
1811c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
1812c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
1813c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
18140b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
18150b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
1816fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
1817fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
181807d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
181907d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
182007d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
1821de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
1822f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
1823f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
1824f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
1825f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
1826f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
1827f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
1828f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
1829f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
1830f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
1831f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
1832a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
1833a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
1834a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
1835a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
18365da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
18375da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
183856051948SVladimir Oltean };
1839319e4dd1SVladimir Oltean 
1840319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
1841319e4dd1SVladimir Oltean {
1842319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1843319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
1844319e4dd1SVladimir Oltean 
1845319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1846319e4dd1SVladimir Oltean 		return NULL;
1847319e4dd1SVladimir Oltean 
1848319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
1849319e4dd1SVladimir Oltean }
1850319e4dd1SVladimir Oltean 
1851319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
1852319e4dd1SVladimir Oltean {
1853319e4dd1SVladimir Oltean 	struct dsa_port *dp;
1854319e4dd1SVladimir Oltean 
1855319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
1856319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
1857319e4dd1SVladimir Oltean 		return -EINVAL;
1858319e4dd1SVladimir Oltean 
1859319e4dd1SVladimir Oltean 	return dp->index;
1860319e4dd1SVladimir Oltean }
1861