xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
23c9cfb52SVladimir Oltean /* Copyright 2019-2021 NXP
3375e1314SVladimir Oltean  *
4375e1314SVladimir Oltean  * This is an umbrella module for all network switches that are
5375e1314SVladimir Oltean  * register-compatible with Ocelot and that perform I/O to their host CPU
6375e1314SVladimir Oltean  * through an NPI (Node Processor Interface) Ethernet port.
756051948SVladimir Oltean  */
856051948SVladimir Oltean #include <uapi/linux/if_bridge.h>
907d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h>
11bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h>
12bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h>
13bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h>
142b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h>
1556051948SVladimir Oltean #include <soc/mscc/ocelot.h>
16e21268efSVladimir Oltean #include <linux/dsa/8021q.h>
1740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
1884705fc1SMaxim Kochetkov #include <linux/platform_device.h>
190a6f17c6SVladimir Oltean #include <linux/ptp_classify.h>
2056051948SVladimir Oltean #include <linux/module.h>
21bdeced75SVladimir Oltean #include <linux/of_net.h>
2256051948SVladimir Oltean #include <linux/pci.h>
2356051948SVladimir Oltean #include <linux/of.h>
24fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
2556051948SVladimir Oltean #include <net/dsa.h>
2656051948SVladimir Oltean #include "felix.h"
2756051948SVladimir Oltean 
28f9cef64fSVladimir Oltean /* Translate the DSA database API into the ocelot switch library API,
29f9cef64fSVladimir Oltean  * which uses VID 0 for all ports that aren't part of a bridge,
30f9cef64fSVladimir Oltean  * and expects the bridge_dev to be NULL in that case.
31f9cef64fSVladimir Oltean  */
felix_classify_db(struct dsa_db db)32f9cef64fSVladimir Oltean static struct net_device *felix_classify_db(struct dsa_db db)
33f9cef64fSVladimir Oltean {
34f9cef64fSVladimir Oltean 	switch (db.type) {
35f9cef64fSVladimir Oltean 	case DSA_DB_PORT:
36f9cef64fSVladimir Oltean 	case DSA_DB_LAG:
37f9cef64fSVladimir Oltean 		return NULL;
38f9cef64fSVladimir Oltean 	case DSA_DB_BRIDGE:
39f9cef64fSVladimir Oltean 		return db.bridge.dev;
40f9cef64fSVladimir Oltean 	default:
41f9cef64fSVladimir Oltean 		return ERR_PTR(-EOPNOTSUPP);
42f9cef64fSVladimir Oltean 	}
43f9cef64fSVladimir Oltean }
44f9cef64fSVladimir Oltean 
felix_cpu_port_for_master(struct dsa_switch * ds,struct net_device * master)45eca70102SVladimir Oltean static int felix_cpu_port_for_master(struct dsa_switch *ds,
46eca70102SVladimir Oltean 				     struct net_device *master)
47eca70102SVladimir Oltean {
48eca70102SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
49eca70102SVladimir Oltean 	struct dsa_port *cpu_dp;
50eca70102SVladimir Oltean 	int lag;
51eca70102SVladimir Oltean 
52eca70102SVladimir Oltean 	if (netif_is_lag_master(master)) {
53eca70102SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
54eca70102SVladimir Oltean 		lag = ocelot_bond_get_id(ocelot, master);
55eca70102SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
56eca70102SVladimir Oltean 
57eca70102SVladimir Oltean 		return lag;
58eca70102SVladimir Oltean 	}
59eca70102SVladimir Oltean 
60eca70102SVladimir Oltean 	cpu_dp = master->dsa_ptr;
61eca70102SVladimir Oltean 	return cpu_dp->index;
62eca70102SVladimir Oltean }
63eca70102SVladimir Oltean 
6404b67e18SVladimir Oltean /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
6504b67e18SVladimir Oltean  * the tagger can perform RX source port identification.
6604b67e18SVladimir Oltean  */
felix_tag_8021q_vlan_add_rx(struct dsa_switch * ds,int port,int upstream,u16 vid)67a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_add_rx(struct dsa_switch *ds, int port,
68a4e044dcSVladimir Oltean 				       int upstream, u16 vid)
69e21268efSVladimir Oltean {
70e21268efSVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
71a4e044dcSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72a4e044dcSVladimir Oltean 	unsigned long cookie;
73a4e044dcSVladimir Oltean 	int key_length, err;
74e21268efSVladimir Oltean 
75e21268efSVladimir Oltean 	key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
76e21268efSVladimir Oltean 
77e21268efSVladimir Oltean 	outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
78e21268efSVladimir Oltean 				     GFP_KERNEL);
79e21268efSVladimir Oltean 	if (!outer_tagging_rule)
80e21268efSVladimir Oltean 		return -ENOMEM;
81e21268efSVladimir Oltean 
82a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port, upstream);
83a4e044dcSVladimir Oltean 
84e21268efSVladimir Oltean 	outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
85e21268efSVladimir Oltean 	outer_tagging_rule->prio = 1;
86a4e044dcSVladimir Oltean 	outer_tagging_rule->id.cookie = cookie;
87e21268efSVladimir Oltean 	outer_tagging_rule->id.tc_offload = false;
88e21268efSVladimir Oltean 	outer_tagging_rule->block_id = VCAP_ES0;
89e21268efSVladimir Oltean 	outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
90e21268efSVladimir Oltean 	outer_tagging_rule->lookup = 0;
91e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.value = port;
92e21268efSVladimir Oltean 	outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
93e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.value = upstream;
94e21268efSVladimir Oltean 	outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
95e21268efSVladimir Oltean 	outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
96e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
97e21268efSVladimir Oltean 	outer_tagging_rule->action.tag_a_vid_sel = 1;
98e21268efSVladimir Oltean 	outer_tagging_rule->action.vid_a_val = vid;
99e21268efSVladimir Oltean 
100e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
101e21268efSVladimir Oltean 	if (err)
102e21268efSVladimir Oltean 		kfree(outer_tagging_rule);
103e21268efSVladimir Oltean 
104e21268efSVladimir Oltean 	return err;
105e21268efSVladimir Oltean }
106e21268efSVladimir Oltean 
felix_tag_8021q_vlan_del_rx(struct dsa_switch * ds,int port,int upstream,u16 vid)107a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_del_rx(struct dsa_switch *ds, int port,
108a4e044dcSVladimir Oltean 				       int upstream, u16 vid)
10904b67e18SVladimir Oltean {
11004b67e18SVladimir Oltean 	struct ocelot_vcap_filter *outer_tagging_rule;
11104b67e18SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_es0;
112a4e044dcSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
113a4e044dcSVladimir Oltean 	unsigned long cookie;
11404b67e18SVladimir Oltean 
11504b67e18SVladimir Oltean 	block_vcap_es0 = &ocelot->block[VCAP_ES0];
116a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port, upstream);
11704b67e18SVladimir Oltean 
11804b67e18SVladimir Oltean 	outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
119a4e044dcSVladimir Oltean 								 cookie, false);
12004b67e18SVladimir Oltean 	if (!outer_tagging_rule)
12104b67e18SVladimir Oltean 		return -ENOENT;
12204b67e18SVladimir Oltean 
12304b67e18SVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
12404b67e18SVladimir Oltean }
12504b67e18SVladimir Oltean 
12604b67e18SVladimir Oltean /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
12704b67e18SVladimir Oltean  * rules for steering those tagged packets towards the correct destination port
12804b67e18SVladimir Oltean  */
felix_tag_8021q_vlan_add_tx(struct dsa_switch * ds,int port,u16 vid)129a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_add_tx(struct dsa_switch *ds, int port,
130a4e044dcSVladimir Oltean 				       u16 vid)
131e21268efSVladimir Oltean {
132e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
133a4e044dcSVladimir Oltean 	unsigned long cpu_ports = dsa_cpu_ports(ds);
134a4e044dcSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
135a4e044dcSVladimir Oltean 	unsigned long cookie;
136a4e044dcSVladimir Oltean 	int err;
137e21268efSVladimir Oltean 
138e21268efSVladimir Oltean 	untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
139e21268efSVladimir Oltean 	if (!untagging_rule)
140e21268efSVladimir Oltean 		return -ENOMEM;
141e21268efSVladimir Oltean 
142e21268efSVladimir Oltean 	redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
143e21268efSVladimir Oltean 	if (!redirect_rule) {
144e21268efSVladimir Oltean 		kfree(untagging_rule);
145e21268efSVladimir Oltean 		return -ENOMEM;
146e21268efSVladimir Oltean 	}
147e21268efSVladimir Oltean 
148a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
149e21268efSVladimir Oltean 
150e21268efSVladimir Oltean 	untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
151a4e044dcSVladimir Oltean 	untagging_rule->ingress_port_mask = cpu_ports;
152e21268efSVladimir Oltean 	untagging_rule->vlan.vid.value = vid;
153e21268efSVladimir Oltean 	untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
154e21268efSVladimir Oltean 	untagging_rule->prio = 1;
155a4e044dcSVladimir Oltean 	untagging_rule->id.cookie = cookie;
156e21268efSVladimir Oltean 	untagging_rule->id.tc_offload = false;
157e21268efSVladimir Oltean 	untagging_rule->block_id = VCAP_IS1;
158e21268efSVladimir Oltean 	untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
159e21268efSVladimir Oltean 	untagging_rule->lookup = 0;
160e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt_ena = true;
161e21268efSVladimir Oltean 	untagging_rule->action.vlan_pop_cnt = 1;
162e21268efSVladimir Oltean 	untagging_rule->action.pag_override_mask = 0xff;
163e21268efSVladimir Oltean 	untagging_rule->action.pag_val = port;
164e21268efSVladimir Oltean 
165e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
166e21268efSVladimir Oltean 	if (err) {
167e21268efSVladimir Oltean 		kfree(untagging_rule);
168e21268efSVladimir Oltean 		kfree(redirect_rule);
169e21268efSVladimir Oltean 		return err;
170e21268efSVladimir Oltean 	}
171e21268efSVladimir Oltean 
172a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
173a4e044dcSVladimir Oltean 
174e21268efSVladimir Oltean 	redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
175a4e044dcSVladimir Oltean 	redirect_rule->ingress_port_mask = cpu_ports;
176e21268efSVladimir Oltean 	redirect_rule->pag = port;
177e21268efSVladimir Oltean 	redirect_rule->prio = 1;
178a4e044dcSVladimir Oltean 	redirect_rule->id.cookie = cookie;
179e21268efSVladimir Oltean 	redirect_rule->id.tc_offload = false;
180e21268efSVladimir Oltean 	redirect_rule->block_id = VCAP_IS2;
181e21268efSVladimir Oltean 	redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
182e21268efSVladimir Oltean 	redirect_rule->lookup = 0;
183e21268efSVladimir Oltean 	redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
184e21268efSVladimir Oltean 	redirect_rule->action.port_mask = BIT(port);
185e21268efSVladimir Oltean 
186e21268efSVladimir Oltean 	err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
187e21268efSVladimir Oltean 	if (err) {
188e21268efSVladimir Oltean 		ocelot_vcap_filter_del(ocelot, untagging_rule);
189e21268efSVladimir Oltean 		kfree(redirect_rule);
190e21268efSVladimir Oltean 		return err;
191e21268efSVladimir Oltean 	}
192e21268efSVladimir Oltean 
193e21268efSVladimir Oltean 	return 0;
194e21268efSVladimir Oltean }
195e21268efSVladimir Oltean 
felix_tag_8021q_vlan_del_tx(struct dsa_switch * ds,int port,u16 vid)196a4e044dcSVladimir Oltean static int felix_tag_8021q_vlan_del_tx(struct dsa_switch *ds, int port, u16 vid)
197e21268efSVladimir Oltean {
198e21268efSVladimir Oltean 	struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
199e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is1;
200e21268efSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
201a4e044dcSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
202a4e044dcSVladimir Oltean 	unsigned long cookie;
203e21268efSVladimir Oltean 	int err;
204e21268efSVladimir Oltean 
205e21268efSVladimir Oltean 	block_vcap_is1 = &ocelot->block[VCAP_IS1];
206e21268efSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
207e21268efSVladimir Oltean 
208a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
209e21268efSVladimir Oltean 	untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
210a4e044dcSVladimir Oltean 							     cookie, false);
211e21268efSVladimir Oltean 	if (!untagging_rule)
21208f44db3SVladimir Oltean 		return -ENOENT;
213e21268efSVladimir Oltean 
214e21268efSVladimir Oltean 	err = ocelot_vcap_filter_del(ocelot, untagging_rule);
215e21268efSVladimir Oltean 	if (err)
216e21268efSVladimir Oltean 		return err;
217e21268efSVladimir Oltean 
218a4e044dcSVladimir Oltean 	cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
219e21268efSVladimir Oltean 	redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
220a4e044dcSVladimir Oltean 							    cookie, false);
221e21268efSVladimir Oltean 	if (!redirect_rule)
22204b67e18SVladimir Oltean 		return -ENOENT;
223e21268efSVladimir Oltean 
224e21268efSVladimir Oltean 	return ocelot_vcap_filter_del(ocelot, redirect_rule);
225e21268efSVladimir Oltean }
226e21268efSVladimir Oltean 
felix_tag_8021q_vlan_add(struct dsa_switch * ds,int port,u16 vid,u16 flags)22704b67e18SVladimir Oltean static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
22804b67e18SVladimir Oltean 				    u16 flags)
22904b67e18SVladimir Oltean {
230a4e044dcSVladimir Oltean 	struct dsa_port *cpu_dp;
23104b67e18SVladimir Oltean 	int err;
23204b67e18SVladimir Oltean 
23304b67e18SVladimir Oltean 	/* tag_8021q.c assumes we are implementing this via port VLAN
23404b67e18SVladimir Oltean 	 * membership, which we aren't. So we don't need to add any VCAP filter
23504b67e18SVladimir Oltean 	 * for the CPU port.
23604b67e18SVladimir Oltean 	 */
23704b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
23804b67e18SVladimir Oltean 		return 0;
23904b67e18SVladimir Oltean 
240a4e044dcSVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
241a4e044dcSVladimir Oltean 		err = felix_tag_8021q_vlan_add_rx(ds, port, cpu_dp->index, vid);
24204b67e18SVladimir Oltean 		if (err)
24304b67e18SVladimir Oltean 			return err;
24404b67e18SVladimir Oltean 	}
24504b67e18SVladimir Oltean 
246a4e044dcSVladimir Oltean 	err = felix_tag_8021q_vlan_add_tx(ds, port, vid);
247a4e044dcSVladimir Oltean 	if (err)
248a4e044dcSVladimir Oltean 		goto add_tx_failed;
249a4e044dcSVladimir Oltean 
25004b67e18SVladimir Oltean 	return 0;
251a4e044dcSVladimir Oltean 
252a4e044dcSVladimir Oltean add_tx_failed:
253a4e044dcSVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
254a4e044dcSVladimir Oltean 		felix_tag_8021q_vlan_del_rx(ds, port, cpu_dp->index, vid);
255a4e044dcSVladimir Oltean 
256a4e044dcSVladimir Oltean 	return err;
25704b67e18SVladimir Oltean }
25804b67e18SVladimir Oltean 
felix_tag_8021q_vlan_del(struct dsa_switch * ds,int port,u16 vid)259e21268efSVladimir Oltean static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
260e21268efSVladimir Oltean {
261a4e044dcSVladimir Oltean 	struct dsa_port *cpu_dp;
26204b67e18SVladimir Oltean 	int err;
263e21268efSVladimir Oltean 
26404b67e18SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
26504b67e18SVladimir Oltean 		return 0;
266e21268efSVladimir Oltean 
267a4e044dcSVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
268a4e044dcSVladimir Oltean 		err = felix_tag_8021q_vlan_del_rx(ds, port, cpu_dp->index, vid);
26904b67e18SVladimir Oltean 		if (err)
27004b67e18SVladimir Oltean 			return err;
27104b67e18SVladimir Oltean 	}
272e21268efSVladimir Oltean 
273a4e044dcSVladimir Oltean 	err = felix_tag_8021q_vlan_del_tx(ds, port, vid);
274a4e044dcSVladimir Oltean 	if (err)
275a4e044dcSVladimir Oltean 		goto del_tx_failed;
276a4e044dcSVladimir Oltean 
277e21268efSVladimir Oltean 	return 0;
278a4e044dcSVladimir Oltean 
279a4e044dcSVladimir Oltean del_tx_failed:
280a4e044dcSVladimir Oltean 	dsa_switch_for_each_cpu_port(cpu_dp, ds)
281a4e044dcSVladimir Oltean 		felix_tag_8021q_vlan_add_rx(ds, port, cpu_dp->index, vid);
282a4e044dcSVladimir Oltean 
283a4e044dcSVladimir Oltean 	return err;
284e21268efSVladimir Oltean }
285e21268efSVladimir Oltean 
felix_trap_get_cpu_port(struct dsa_switch * ds,const struct ocelot_vcap_filter * trap)286c352e5e8SVladimir Oltean static int felix_trap_get_cpu_port(struct dsa_switch *ds,
287c352e5e8SVladimir Oltean 				   const struct ocelot_vcap_filter *trap)
288c352e5e8SVladimir Oltean {
289c352e5e8SVladimir Oltean 	struct dsa_port *dp;
290c352e5e8SVladimir Oltean 	int first_port;
291c352e5e8SVladimir Oltean 
292c352e5e8SVladimir Oltean 	if (WARN_ON(!trap->ingress_port_mask))
293c352e5e8SVladimir Oltean 		return -1;
294c352e5e8SVladimir Oltean 
295c352e5e8SVladimir Oltean 	first_port = __ffs(trap->ingress_port_mask);
296c352e5e8SVladimir Oltean 	dp = dsa_to_port(ds, first_port);
297c352e5e8SVladimir Oltean 
298c352e5e8SVladimir Oltean 	return dp->cpu_dp->index;
299c352e5e8SVladimir Oltean }
300c352e5e8SVladimir Oltean 
30199348004SVladimir Oltean /* On switches with no extraction IRQ wired, trapped packets need to be
30299348004SVladimir Oltean  * replicated over Ethernet as well, otherwise we'd get no notification of
30399348004SVladimir Oltean  * their arrival when using the ocelot-8021q tagging protocol.
304c8c0ba4fSVladimir Oltean  */
felix_update_trapping_destinations(struct dsa_switch * ds,bool using_tag_8021q)30599348004SVladimir Oltean static int felix_update_trapping_destinations(struct dsa_switch *ds,
30699348004SVladimir Oltean 					      bool using_tag_8021q)
307c8c0ba4fSVladimir Oltean {
30899348004SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
30999348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
310e1846cffSVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
31199348004SVladimir Oltean 	struct ocelot_vcap_filter *trap;
31299348004SVladimir Oltean 	enum ocelot_mask_mode mask_mode;
31399348004SVladimir Oltean 	unsigned long port_mask;
31499348004SVladimir Oltean 	bool cpu_copy_ena;
315c352e5e8SVladimir Oltean 	int err;
316c8c0ba4fSVladimir Oltean 
31799348004SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
31899348004SVladimir Oltean 		return 0;
319c8c0ba4fSVladimir Oltean 
320d78637a8SVladimir Oltean 	/* We are sure that "cpu" was found, otherwise
321d78637a8SVladimir Oltean 	 * dsa_tree_setup_default_cpu() would have failed earlier.
322d78637a8SVladimir Oltean 	 */
323e1846cffSVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
324c8c0ba4fSVladimir Oltean 
32599348004SVladimir Oltean 	/* Make sure all traps are set up for that destination */
326e1846cffSVladimir Oltean 	list_for_each_entry(trap, &block_vcap_is2->rules, list) {
327e1846cffSVladimir Oltean 		if (!trap->is_trap)
328e1846cffSVladimir Oltean 			continue;
329e1846cffSVladimir Oltean 
33099348004SVladimir Oltean 		/* Figure out the current trapping destination */
33199348004SVladimir Oltean 		if (using_tag_8021q) {
33299348004SVladimir Oltean 			/* Redirect to the tag_8021q CPU port. If timestamps
33399348004SVladimir Oltean 			 * are necessary, also copy trapped packets to the CPU
33499348004SVladimir Oltean 			 * port module.
335c8c0ba4fSVladimir Oltean 			 */
33699348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_REDIRECT;
337c352e5e8SVladimir Oltean 			port_mask = BIT(felix_trap_get_cpu_port(ds, trap));
33899348004SVladimir Oltean 			cpu_copy_ena = !!trap->take_ts;
339c8c0ba4fSVladimir Oltean 		} else {
34099348004SVladimir Oltean 			/* Trap packets only to the CPU port module, which is
34199348004SVladimir Oltean 			 * redirected to the NPI port (the DSA CPU port)
342c8c0ba4fSVladimir Oltean 			 */
34399348004SVladimir Oltean 			mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
34499348004SVladimir Oltean 			port_mask = 0;
34599348004SVladimir Oltean 			cpu_copy_ena = true;
346c8c0ba4fSVladimir Oltean 		}
347c8c0ba4fSVladimir Oltean 
34899348004SVladimir Oltean 		if (trap->action.mask_mode == mask_mode &&
34999348004SVladimir Oltean 		    trap->action.port_mask == port_mask &&
35099348004SVladimir Oltean 		    trap->action.cpu_copy_ena == cpu_copy_ena)
35199348004SVladimir Oltean 			continue;
352c8c0ba4fSVladimir Oltean 
35399348004SVladimir Oltean 		trap->action.mask_mode = mask_mode;
35499348004SVladimir Oltean 		trap->action.port_mask = port_mask;
35599348004SVladimir Oltean 		trap->action.cpu_copy_ena = cpu_copy_ena;
3560a6f17c6SVladimir Oltean 
35799348004SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
358c8c0ba4fSVladimir Oltean 		if (err)
359c8c0ba4fSVladimir Oltean 			return err;
36099348004SVladimir Oltean 	}
361c8c0ba4fSVladimir Oltean 
36299348004SVladimir Oltean 	return 0;
363c8c0ba4fSVladimir Oltean }
364c8c0ba4fSVladimir Oltean 
365adb3dccfSVladimir Oltean /* The CPU port module is connected to the Node Processor Interface (NPI). This
366adb3dccfSVladimir Oltean  * is the mode through which frames can be injected from and extracted to an
367adb3dccfSVladimir Oltean  * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
368adb3dccfSVladimir Oltean  * running Linux, and this forms a DSA setup together with the enetc or fman
369adb3dccfSVladimir Oltean  * DSA master.
370adb3dccfSVladimir Oltean  */
felix_npi_port_init(struct ocelot * ocelot,int port)371adb3dccfSVladimir Oltean static void felix_npi_port_init(struct ocelot *ocelot, int port)
372adb3dccfSVladimir Oltean {
373adb3dccfSVladimir Oltean 	ocelot->npi = port;
374adb3dccfSVladimir Oltean 
375adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
376adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
377adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
378adb3dccfSVladimir Oltean 
379adb3dccfSVladimir Oltean 	/* NPI port Injection/Extraction configuration */
380adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
381adb3dccfSVladimir Oltean 			    ocelot->npi_xtr_prefix);
382adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
383adb3dccfSVladimir Oltean 			    ocelot->npi_inj_prefix);
384adb3dccfSVladimir Oltean 
385adb3dccfSVladimir Oltean 	/* Disable transmission of pause frames */
386adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
387adb3dccfSVladimir Oltean }
388adb3dccfSVladimir Oltean 
felix_npi_port_deinit(struct ocelot * ocelot,int port)389adb3dccfSVladimir Oltean static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
390adb3dccfSVladimir Oltean {
391adb3dccfSVladimir Oltean 	/* Restore hardware defaults */
392adb3dccfSVladimir Oltean 	int unused_port = ocelot->num_phys_ports + 2;
393adb3dccfSVladimir Oltean 
394adb3dccfSVladimir Oltean 	ocelot->npi = -1;
395adb3dccfSVladimir Oltean 
396adb3dccfSVladimir Oltean 	ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
397adb3dccfSVladimir Oltean 		     QSYS_EXT_CPU_CFG);
398adb3dccfSVladimir Oltean 
399adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
400adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
401adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
402adb3dccfSVladimir Oltean 			    OCELOT_TAG_PREFIX_DISABLED);
403adb3dccfSVladimir Oltean 
404adb3dccfSVladimir Oltean 	/* Enable transmission of pause frames */
405adb3dccfSVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
406adb3dccfSVladimir Oltean }
407adb3dccfSVladimir Oltean 
felix_tag_npi_setup(struct dsa_switch * ds)4087a29d220SVladimir Oltean static int felix_tag_npi_setup(struct dsa_switch *ds)
409adb3dccfSVladimir Oltean {
4107a29d220SVladimir Oltean 	struct dsa_port *dp, *first_cpu_dp = NULL;
411adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
412f9cef64fSVladimir Oltean 
4137a29d220SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds) {
4147a29d220SVladimir Oltean 		if (first_cpu_dp && dp->cpu_dp != first_cpu_dp) {
4157a29d220SVladimir Oltean 			dev_err(ds->dev, "Multiple NPI ports not supported\n");
4167a29d220SVladimir Oltean 			return -EINVAL;
4177a29d220SVladimir Oltean 		}
418b903a6bdSVladimir Oltean 
4197a29d220SVladimir Oltean 		first_cpu_dp = dp->cpu_dp;
4207a29d220SVladimir Oltean 	}
421adb3dccfSVladimir Oltean 
4227a29d220SVladimir Oltean 	if (!first_cpu_dp)
4237a29d220SVladimir Oltean 		return -EINVAL;
4247a29d220SVladimir Oltean 
4257a29d220SVladimir Oltean 	felix_npi_port_init(ocelot, first_cpu_dp->index);
426adb3dccfSVladimir Oltean 
427adb3dccfSVladimir Oltean 	return 0;
428adb3dccfSVladimir Oltean }
429adb3dccfSVladimir Oltean 
felix_tag_npi_teardown(struct dsa_switch * ds)4307a29d220SVladimir Oltean static void felix_tag_npi_teardown(struct dsa_switch *ds)
431adb3dccfSVladimir Oltean {
432adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
433adb3dccfSVladimir Oltean 
4347a29d220SVladimir Oltean 	felix_npi_port_deinit(ocelot, ocelot->npi);
435adb3dccfSVladimir Oltean }
436adb3dccfSVladimir Oltean 
felix_tag_npi_get_host_fwd_mask(struct dsa_switch * ds)4377a29d220SVladimir Oltean static unsigned long felix_tag_npi_get_host_fwd_mask(struct dsa_switch *ds)
438adb3dccfSVladimir Oltean {
4397a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4407a29d220SVladimir Oltean 
4417a29d220SVladimir Oltean 	return BIT(ocelot->num_phys_ports);
4427a29d220SVladimir Oltean }
4437a29d220SVladimir Oltean 
felix_tag_npi_change_master(struct dsa_switch * ds,int port,struct net_device * master,struct netlink_ext_ack * extack)444eca70102SVladimir Oltean static int felix_tag_npi_change_master(struct dsa_switch *ds, int port,
445eca70102SVladimir Oltean 				       struct net_device *master,
446eca70102SVladimir Oltean 				       struct netlink_ext_ack *extack)
447eca70102SVladimir Oltean {
448eca70102SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
449eca70102SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
450eca70102SVladimir Oltean 
451eca70102SVladimir Oltean 	if (netif_is_lag_master(master)) {
452eca70102SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
453eca70102SVladimir Oltean 				   "LAG DSA master only supported using ocelot-8021q");
454eca70102SVladimir Oltean 		return -EOPNOTSUPP;
455eca70102SVladimir Oltean 	}
456eca70102SVladimir Oltean 
457eca70102SVladimir Oltean 	/* Changing the NPI port breaks user ports still assigned to the old
458eca70102SVladimir Oltean 	 * one, so only allow it while they're down, and don't allow them to
459eca70102SVladimir Oltean 	 * come back up until they're all changed to the new one.
460eca70102SVladimir Oltean 	 */
461eca70102SVladimir Oltean 	dsa_switch_for_each_user_port(other_dp, ds) {
462eca70102SVladimir Oltean 		struct net_device *slave = other_dp->slave;
463eca70102SVladimir Oltean 
464eca70102SVladimir Oltean 		if (other_dp != dp && (slave->flags & IFF_UP) &&
465eca70102SVladimir Oltean 		    dsa_port_to_master(other_dp) != master) {
466eca70102SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
467eca70102SVladimir Oltean 					   "Cannot change while old master still has users");
468eca70102SVladimir Oltean 			return -EOPNOTSUPP;
469eca70102SVladimir Oltean 		}
470eca70102SVladimir Oltean 	}
471eca70102SVladimir Oltean 
472eca70102SVladimir Oltean 	felix_npi_port_deinit(ocelot, ocelot->npi);
473eca70102SVladimir Oltean 	felix_npi_port_init(ocelot, felix_cpu_port_for_master(ds, master));
474eca70102SVladimir Oltean 
475eca70102SVladimir Oltean 	return 0;
476eca70102SVladimir Oltean }
477eca70102SVladimir Oltean 
4788c166acbSVladimir Oltean /* Alternatively to using the NPI functionality, that same hardware MAC
4798c166acbSVladimir Oltean  * connected internally to the enetc or fman DSA master can be configured to
4808c166acbSVladimir Oltean  * use the software-defined tag_8021q frame format. As far as the hardware is
4818c166acbSVladimir Oltean  * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
4828c166acbSVladimir Oltean  * module are now disconnected from it, but can still be accessed through
4838c166acbSVladimir Oltean  * register-based MMIO.
4848c166acbSVladimir Oltean  */
4857a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_npi_proto_ops = {
4867a29d220SVladimir Oltean 	.setup			= felix_tag_npi_setup,
4877a29d220SVladimir Oltean 	.teardown		= felix_tag_npi_teardown,
4887a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_npi_get_host_fwd_mask,
489eca70102SVladimir Oltean 	.change_master		= felix_tag_npi_change_master,
4907a29d220SVladimir Oltean };
4917a29d220SVladimir Oltean 
felix_tag_8021q_setup(struct dsa_switch * ds)4927a29d220SVladimir Oltean static int felix_tag_8021q_setup(struct dsa_switch *ds)
4937a29d220SVladimir Oltean {
4947a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
495c295f983SVladimir Oltean 	struct dsa_port *dp;
496adb3dccfSVladimir Oltean 	int err;
497adb3dccfSVladimir Oltean 
4987a29d220SVladimir Oltean 	err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
4997a29d220SVladimir Oltean 	if (err)
500adb3dccfSVladimir Oltean 		return err;
5017a29d220SVladimir Oltean 
50236a0bf44SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds)
50336a0bf44SVladimir Oltean 		ocelot_port_setup_dsa_8021q_cpu(ocelot, dp->index);
50436a0bf44SVladimir Oltean 
505c295f983SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
506c295f983SVladimir Oltean 		ocelot_port_assign_dsa_8021q_cpu(ocelot, dp->index,
507c295f983SVladimir Oltean 						 dp->cpu_dp->index);
5087a29d220SVladimir Oltean 
509c295f983SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
5107a29d220SVladimir Oltean 		/* This overwrites ocelot_init():
5117a29d220SVladimir Oltean 		 * Do not forward BPDU frames to the CPU port module,
5127a29d220SVladimir Oltean 		 * for 2 reasons:
5137a29d220SVladimir Oltean 		 * - When these packets are injected from the tag_8021q
5147a29d220SVladimir Oltean 		 *   CPU port, we want them to go out, not loop back
5157a29d220SVladimir Oltean 		 *   into the system.
5167a29d220SVladimir Oltean 		 * - STP traffic ingressing on a user port should go to
5177a29d220SVladimir Oltean 		 *   the tag_8021q CPU port, not to the hardware CPU
5187a29d220SVladimir Oltean 		 *   port module.
5197a29d220SVladimir Oltean 		 */
5207a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
5217a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
5227a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
5237a29d220SVladimir Oltean 
5247a29d220SVladimir Oltean 	/* The ownership of the CPU port module's queues might have just been
5257a29d220SVladimir Oltean 	 * transferred to the tag_8021q tagger from the NPI-based tagger.
5267a29d220SVladimir Oltean 	 * So there might still be all sorts of crap in the queues. On the
5277a29d220SVladimir Oltean 	 * other hand, the MMIO-based matching of PTP frames is very brittle,
5287a29d220SVladimir Oltean 	 * so we need to be careful that there are no extra frames to be
5297a29d220SVladimir Oltean 	 * dequeued over MMIO, since we would never know to discard them.
5307a29d220SVladimir Oltean 	 */
531*e83b49ecSVladimir Oltean 	ocelot_lock_xtr_grp_bh(ocelot, 0);
5327a29d220SVladimir Oltean 	ocelot_drain_cpu_queue(ocelot, 0);
533*e83b49ecSVladimir Oltean 	ocelot_unlock_xtr_grp_bh(ocelot, 0);
5347a29d220SVladimir Oltean 
5357a29d220SVladimir Oltean 	return 0;
5367a29d220SVladimir Oltean }
5377a29d220SVladimir Oltean 
felix_tag_8021q_teardown(struct dsa_switch * ds)5387a29d220SVladimir Oltean static void felix_tag_8021q_teardown(struct dsa_switch *ds)
539adb3dccfSVladimir Oltean {
5407a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
541c295f983SVladimir Oltean 	struct dsa_port *dp;
5427a29d220SVladimir Oltean 
543c295f983SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
5447a29d220SVladimir Oltean 		/* Restore the logic from ocelot_init:
5457a29d220SVladimir Oltean 		 * do not forward BPDU frames to the front ports.
5467a29d220SVladimir Oltean 		 */
5477a29d220SVladimir Oltean 		ocelot_write_gix(ocelot,
5487a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
5497a29d220SVladimir Oltean 				 ANA_PORT_CPU_FWD_BPDU_CFG,
5507a29d220SVladimir Oltean 				 dp->index);
5517a29d220SVladimir Oltean 
552c295f983SVladimir Oltean 	dsa_switch_for_each_user_port(dp, ds)
553c295f983SVladimir Oltean 		ocelot_port_unassign_dsa_8021q_cpu(ocelot, dp->index);
5547a29d220SVladimir Oltean 
55536a0bf44SVladimir Oltean 	dsa_switch_for_each_cpu_port(dp, ds)
55636a0bf44SVladimir Oltean 		ocelot_port_teardown_dsa_8021q_cpu(ocelot, dp->index);
55736a0bf44SVladimir Oltean 
5587a29d220SVladimir Oltean 	dsa_tag_8021q_unregister(ds);
5597a29d220SVladimir Oltean }
5607a29d220SVladimir Oltean 
felix_tag_8021q_get_host_fwd_mask(struct dsa_switch * ds)5617a29d220SVladimir Oltean static unsigned long felix_tag_8021q_get_host_fwd_mask(struct dsa_switch *ds)
5627a29d220SVladimir Oltean {
5637a29d220SVladimir Oltean 	return dsa_cpu_ports(ds);
5647a29d220SVladimir Oltean }
5657a29d220SVladimir Oltean 
felix_tag_8021q_change_master(struct dsa_switch * ds,int port,struct net_device * master,struct netlink_ext_ack * extack)566eca70102SVladimir Oltean static int felix_tag_8021q_change_master(struct dsa_switch *ds, int port,
567eca70102SVladimir Oltean 					 struct net_device *master,
568eca70102SVladimir Oltean 					 struct netlink_ext_ack *extack)
569eca70102SVladimir Oltean {
570eca70102SVladimir Oltean 	int cpu = felix_cpu_port_for_master(ds, master);
571eca70102SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
572eca70102SVladimir Oltean 
573eca70102SVladimir Oltean 	ocelot_port_unassign_dsa_8021q_cpu(ocelot, port);
574eca70102SVladimir Oltean 	ocelot_port_assign_dsa_8021q_cpu(ocelot, port, cpu);
575eca70102SVladimir Oltean 
576eca70102SVladimir Oltean 	return felix_update_trapping_destinations(ds, true);
577eca70102SVladimir Oltean }
578eca70102SVladimir Oltean 
5797a29d220SVladimir Oltean static const struct felix_tag_proto_ops felix_tag_8021q_proto_ops = {
5807a29d220SVladimir Oltean 	.setup			= felix_tag_8021q_setup,
5817a29d220SVladimir Oltean 	.teardown		= felix_tag_8021q_teardown,
5827a29d220SVladimir Oltean 	.get_host_fwd_mask	= felix_tag_8021q_get_host_fwd_mask,
583eca70102SVladimir Oltean 	.change_master		= felix_tag_8021q_change_master,
5847a29d220SVladimir Oltean };
5857a29d220SVladimir Oltean 
felix_set_host_flood(struct dsa_switch * ds,unsigned long mask,bool uc,bool mc,bool bc)5867a29d220SVladimir Oltean static void felix_set_host_flood(struct dsa_switch *ds, unsigned long mask,
5877a29d220SVladimir Oltean 				 bool uc, bool mc, bool bc)
5887a29d220SVladimir Oltean {
5897a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5907a29d220SVladimir Oltean 	unsigned long val;
5917a29d220SVladimir Oltean 
5927a29d220SVladimir Oltean 	val = uc ? mask : 0;
5937a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC);
5947a29d220SVladimir Oltean 
5957a29d220SVladimir Oltean 	val = mc ? mask : 0;
5967a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC);
5977a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4);
5987a29d220SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6);
599129b7532SVladimir Oltean 
600129b7532SVladimir Oltean 	val = bc ? mask : 0;
601129b7532SVladimir Oltean 	ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_BC);
6027a29d220SVladimir Oltean }
6037a29d220SVladimir Oltean 
6047a29d220SVladimir Oltean static void
felix_migrate_host_flood(struct dsa_switch * ds,const struct felix_tag_proto_ops * proto_ops,const struct felix_tag_proto_ops * old_proto_ops)6057a29d220SVladimir Oltean felix_migrate_host_flood(struct dsa_switch *ds,
6067a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *proto_ops,
6077a29d220SVladimir Oltean 			 const struct felix_tag_proto_ops *old_proto_ops)
6087a29d220SVladimir Oltean {
6097a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6107a29d220SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
6117a29d220SVladimir Oltean 	unsigned long mask;
6127a29d220SVladimir Oltean 
6137a29d220SVladimir Oltean 	if (old_proto_ops) {
6147a29d220SVladimir Oltean 		mask = old_proto_ops->get_host_fwd_mask(ds);
6157a29d220SVladimir Oltean 		felix_set_host_flood(ds, mask, false, false, false);
6167a29d220SVladimir Oltean 	}
6177a29d220SVladimir Oltean 
6187a29d220SVladimir Oltean 	mask = proto_ops->get_host_fwd_mask(ds);
6197a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
6207a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
6217a29d220SVladimir Oltean }
6227a29d220SVladimir Oltean 
felix_migrate_mdbs(struct dsa_switch * ds,const struct felix_tag_proto_ops * proto_ops,const struct felix_tag_proto_ops * old_proto_ops)6237a29d220SVladimir Oltean static int felix_migrate_mdbs(struct dsa_switch *ds,
6247a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *proto_ops,
6257a29d220SVladimir Oltean 			      const struct felix_tag_proto_ops *old_proto_ops)
6267a29d220SVladimir Oltean {
6277a29d220SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6287a29d220SVladimir Oltean 	unsigned long from, to;
6297a29d220SVladimir Oltean 
6307a29d220SVladimir Oltean 	if (!old_proto_ops)
6317a29d220SVladimir Oltean 		return 0;
6327a29d220SVladimir Oltean 
6337a29d220SVladimir Oltean 	from = old_proto_ops->get_host_fwd_mask(ds);
6347a29d220SVladimir Oltean 	to = proto_ops->get_host_fwd_mask(ds);
6357a29d220SVladimir Oltean 
6367a29d220SVladimir Oltean 	return ocelot_migrate_mdbs(ocelot, from, to);
6377a29d220SVladimir Oltean }
6387a29d220SVladimir Oltean 
6397a29d220SVladimir Oltean /* Configure the shared hardware resources for a transition between
6407a29d220SVladimir Oltean  * @old_proto_ops and @proto_ops.
6417a29d220SVladimir Oltean  * Manual migration is needed because as far as DSA is concerned, no change of
6427a29d220SVladimir Oltean  * the CPU port is taking place here, just of the tagging protocol.
6437a29d220SVladimir Oltean  */
6447a29d220SVladimir Oltean static int
felix_tag_proto_setup_shared(struct dsa_switch * ds,const struct felix_tag_proto_ops * proto_ops,const struct felix_tag_proto_ops * old_proto_ops)6457a29d220SVladimir Oltean felix_tag_proto_setup_shared(struct dsa_switch *ds,
6467a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *proto_ops,
6477a29d220SVladimir Oltean 			     const struct felix_tag_proto_ops *old_proto_ops)
6487a29d220SVladimir Oltean {
6497a29d220SVladimir Oltean 	bool using_tag_8021q = (proto_ops == &felix_tag_8021q_proto_ops);
6507a29d220SVladimir Oltean 	int err;
6517a29d220SVladimir Oltean 
6527a29d220SVladimir Oltean 	err = felix_migrate_mdbs(ds, proto_ops, old_proto_ops);
6537a29d220SVladimir Oltean 	if (err)
6547a29d220SVladimir Oltean 		return err;
6557a29d220SVladimir Oltean 
6567a29d220SVladimir Oltean 	felix_update_trapping_destinations(ds, using_tag_8021q);
6577a29d220SVladimir Oltean 
6587a29d220SVladimir Oltean 	felix_migrate_host_flood(ds, proto_ops, old_proto_ops);
6597a29d220SVladimir Oltean 
6607a29d220SVladimir Oltean 	return 0;
661adb3dccfSVladimir Oltean }
662adb3dccfSVladimir Oltean 
663e21268efSVladimir Oltean /* This always leaves the switch in a consistent state, because although the
664e21268efSVladimir Oltean  * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
665e21268efSVladimir Oltean  * or the restoration is guaranteed to work.
666e21268efSVladimir Oltean  */
felix_change_tag_protocol(struct dsa_switch * ds,enum dsa_tag_protocol proto)667bacf93b0SVladimir Oltean static int felix_change_tag_protocol(struct dsa_switch *ds,
668adb3dccfSVladimir Oltean 				     enum dsa_tag_protocol proto)
669adb3dccfSVladimir Oltean {
6707a29d220SVladimir Oltean 	const struct felix_tag_proto_ops *old_proto_ops, *proto_ops;
671adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
672adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
673adb3dccfSVladimir Oltean 	int err;
674adb3dccfSVladimir Oltean 
6757a29d220SVladimir Oltean 	switch (proto) {
6767a29d220SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
6777a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
6787a29d220SVladimir Oltean 		proto_ops = &felix_tag_npi_proto_ops;
679bacf93b0SVladimir Oltean 		break;
6807a29d220SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
6817a29d220SVladimir Oltean 		proto_ops = &felix_tag_8021q_proto_ops;
6827a29d220SVladimir Oltean 		break;
6837a29d220SVladimir Oltean 	default:
6847a29d220SVladimir Oltean 		return -EPROTONOSUPPORT;
685bacf93b0SVladimir Oltean 	}
686bacf93b0SVladimir Oltean 
6877a29d220SVladimir Oltean 	old_proto_ops = felix->tag_proto_ops;
6887a29d220SVladimir Oltean 
6894c46bb49SVladimir Oltean 	if (proto_ops == old_proto_ops)
6904c46bb49SVladimir Oltean 		return 0;
6914c46bb49SVladimir Oltean 
6927a29d220SVladimir Oltean 	err = proto_ops->setup(ds);
6937a29d220SVladimir Oltean 	if (err)
6947a29d220SVladimir Oltean 		goto setup_failed;
6957a29d220SVladimir Oltean 
6967a29d220SVladimir Oltean 	err = felix_tag_proto_setup_shared(ds, proto_ops, old_proto_ops);
6977a29d220SVladimir Oltean 	if (err)
6987a29d220SVladimir Oltean 		goto setup_shared_failed;
6997a29d220SVladimir Oltean 
7007a29d220SVladimir Oltean 	if (old_proto_ops)
7017a29d220SVladimir Oltean 		old_proto_ops->teardown(ds);
7027a29d220SVladimir Oltean 
7037a29d220SVladimir Oltean 	felix->tag_proto_ops = proto_ops;
704adb3dccfSVladimir Oltean 	felix->tag_proto = proto;
705adb3dccfSVladimir Oltean 
706adb3dccfSVladimir Oltean 	return 0;
7077a29d220SVladimir Oltean 
7087a29d220SVladimir Oltean setup_shared_failed:
7097a29d220SVladimir Oltean 	proto_ops->teardown(ds);
7107a29d220SVladimir Oltean setup_failed:
7117a29d220SVladimir Oltean 	return err;
712adb3dccfSVladimir Oltean }
713adb3dccfSVladimir Oltean 
felix_get_tag_protocol(struct dsa_switch * ds,int port,enum dsa_tag_protocol mp)71456051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
7154d776482SFlorian Fainelli 						    int port,
7164d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
71756051948SVladimir Oltean {
718adb3dccfSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
719adb3dccfSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
720adb3dccfSVladimir Oltean 
721adb3dccfSVladimir Oltean 	return felix->tag_proto;
72256051948SVladimir Oltean }
72356051948SVladimir Oltean 
felix_port_set_host_flood(struct dsa_switch * ds,int port,bool uc,bool mc)72472c3b0c7SVladimir Oltean static void felix_port_set_host_flood(struct dsa_switch *ds, int port,
72572c3b0c7SVladimir Oltean 				      bool uc, bool mc)
72672c3b0c7SVladimir Oltean {
72772c3b0c7SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72872c3b0c7SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
7297a29d220SVladimir Oltean 	unsigned long mask;
73072c3b0c7SVladimir Oltean 
73172c3b0c7SVladimir Oltean 	if (uc)
73272c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask |= BIT(port);
73372c3b0c7SVladimir Oltean 	else
73472c3b0c7SVladimir Oltean 		felix->host_flood_uc_mask &= ~BIT(port);
73572c3b0c7SVladimir Oltean 
73672c3b0c7SVladimir Oltean 	if (mc)
73772c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask |= BIT(port);
73872c3b0c7SVladimir Oltean 	else
73972c3b0c7SVladimir Oltean 		felix->host_flood_mc_mask &= ~BIT(port);
74072c3b0c7SVladimir Oltean 
7417a29d220SVladimir Oltean 	mask = felix->tag_proto_ops->get_host_fwd_mask(ds);
7427a29d220SVladimir Oltean 	felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
7437a29d220SVladimir Oltean 			     !!felix->host_flood_mc_mask, true);
74472c3b0c7SVladimir Oltean }
74572c3b0c7SVladimir Oltean 
felix_port_change_master(struct dsa_switch * ds,int port,struct net_device * master,struct netlink_ext_ack * extack)746eca70102SVladimir Oltean static int felix_port_change_master(struct dsa_switch *ds, int port,
747eca70102SVladimir Oltean 				    struct net_device *master,
748eca70102SVladimir Oltean 				    struct netlink_ext_ack *extack)
749eca70102SVladimir Oltean {
750eca70102SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
751eca70102SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
752eca70102SVladimir Oltean 
753eca70102SVladimir Oltean 	return felix->tag_proto_ops->change_master(ds, port, master, extack);
754eca70102SVladimir Oltean }
755eca70102SVladimir Oltean 
felix_set_ageing_time(struct dsa_switch * ds,unsigned int ageing_time)75656051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
75756051948SVladimir Oltean 				 unsigned int ageing_time)
75856051948SVladimir Oltean {
75956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
76056051948SVladimir Oltean 
76156051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
76256051948SVladimir Oltean 
76356051948SVladimir Oltean 	return 0;
76456051948SVladimir Oltean }
76556051948SVladimir Oltean 
felix_port_fast_age(struct dsa_switch * ds,int port)7665cad43a5SVladimir Oltean static void felix_port_fast_age(struct dsa_switch *ds, int port)
7675cad43a5SVladimir Oltean {
7685cad43a5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7695cad43a5SVladimir Oltean 	int err;
7705cad43a5SVladimir Oltean 
7715cad43a5SVladimir Oltean 	err = ocelot_mact_flush(ocelot, port);
7725cad43a5SVladimir Oltean 	if (err)
7735cad43a5SVladimir Oltean 		dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
7745cad43a5SVladimir Oltean 			port, ERR_PTR(err));
7755cad43a5SVladimir Oltean }
7765cad43a5SVladimir Oltean 
felix_fdb_dump(struct dsa_switch * ds,int port,dsa_fdb_dump_cb_t * cb,void * data)77756051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
77856051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
77956051948SVladimir Oltean {
78056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
78156051948SVladimir Oltean 
78256051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
78356051948SVladimir Oltean }
78456051948SVladimir Oltean 
felix_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid,struct dsa_db db)78556051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
786c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
787c2693363SVladimir Oltean 			 struct dsa_db db)
78856051948SVladimir Oltean {
78954c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
790e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
79156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
79256051948SVladimir Oltean 
79354c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
79454c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
79554c31984SVladimir Oltean 
796e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
7977e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
7987e580490SVladimir Oltean 		return 0;
7997e580490SVladimir Oltean 
800e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
801e9b3ba43SVladimir Oltean 		port = PGID_CPU;
802e9b3ba43SVladimir Oltean 
80354c31984SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
80456051948SVladimir Oltean }
80556051948SVladimir Oltean 
felix_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid,struct dsa_db db)80656051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
807c2693363SVladimir Oltean 			 const unsigned char *addr, u16 vid,
808c2693363SVladimir Oltean 			 struct dsa_db db)
80956051948SVladimir Oltean {
81054c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
811e9b3ba43SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
81256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
81356051948SVladimir Oltean 
81454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
81554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
81654c31984SVladimir Oltean 
817e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp) && !bridge_dev &&
8187e580490SVladimir Oltean 	    dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
8197e580490SVladimir Oltean 		return 0;
8207e580490SVladimir Oltean 
821e9b3ba43SVladimir Oltean 	if (dsa_port_is_cpu(dp))
822e9b3ba43SVladimir Oltean 		port = PGID_CPU;
823e9b3ba43SVladimir Oltean 
82454c31984SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
82556051948SVladimir Oltean }
82656051948SVladimir Oltean 
felix_lag_fdb_add(struct dsa_switch * ds,struct dsa_lag lag,const unsigned char * addr,u16 vid,struct dsa_db db)827961d8b69SVladimir Oltean static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
828c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
829c2693363SVladimir Oltean 			     struct dsa_db db)
830961d8b69SVladimir Oltean {
83154c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
832961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
833961d8b69SVladimir Oltean 
83454c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
83554c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
83654c31984SVladimir Oltean 
83754c31984SVladimir Oltean 	return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
838961d8b69SVladimir Oltean }
839961d8b69SVladimir Oltean 
felix_lag_fdb_del(struct dsa_switch * ds,struct dsa_lag lag,const unsigned char * addr,u16 vid,struct dsa_db db)840961d8b69SVladimir Oltean static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
841c2693363SVladimir Oltean 			     const unsigned char *addr, u16 vid,
842c2693363SVladimir Oltean 			     struct dsa_db db)
843961d8b69SVladimir Oltean {
84454c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
845961d8b69SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
846961d8b69SVladimir Oltean 
84754c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
84854c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
84954c31984SVladimir Oltean 
85054c31984SVladimir Oltean 	return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
851961d8b69SVladimir Oltean }
852961d8b69SVladimir Oltean 
felix_mdb_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb,struct dsa_db db)853a52b2da7SVladimir Oltean static int felix_mdb_add(struct dsa_switch *ds, int port,
854c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
855c2693363SVladimir Oltean 			 struct dsa_db db)
856209edf95SVladimir Oltean {
85754c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
858209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
859209edf95SVladimir Oltean 
86054c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
86154c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
86254c31984SVladimir Oltean 
8637e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
8647e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
8657e580490SVladimir Oltean 		return 0;
8667e580490SVladimir Oltean 
8670ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
8680ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
8690ddf83cdSVladimir Oltean 
87054c31984SVladimir Oltean 	return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
871209edf95SVladimir Oltean }
872209edf95SVladimir Oltean 
felix_mdb_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_mdb * mdb,struct dsa_db db)873209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
874c2693363SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb,
875c2693363SVladimir Oltean 			 struct dsa_db db)
876209edf95SVladimir Oltean {
87754c31984SVladimir Oltean 	struct net_device *bridge_dev = felix_classify_db(db);
878209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
879209edf95SVladimir Oltean 
88054c31984SVladimir Oltean 	if (IS_ERR(bridge_dev))
88154c31984SVladimir Oltean 		return PTR_ERR(bridge_dev);
88254c31984SVladimir Oltean 
8837e580490SVladimir Oltean 	if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
8847e580490SVladimir Oltean 	    dsa_mdb_present_in_other_db(ds, port, mdb, db))
8857e580490SVladimir Oltean 		return 0;
8867e580490SVladimir Oltean 
8870ddf83cdSVladimir Oltean 	if (port == ocelot->npi)
8880ddf83cdSVladimir Oltean 		port = ocelot->num_phys_ports;
8890ddf83cdSVladimir Oltean 
89054c31984SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
891209edf95SVladimir Oltean }
892209edf95SVladimir Oltean 
felix_bridge_stp_state_set(struct dsa_switch * ds,int port,u8 state)89356051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
89456051948SVladimir Oltean 				       u8 state)
89556051948SVladimir Oltean {
89656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
89756051948SVladimir Oltean 
89856051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
89956051948SVladimir Oltean }
90056051948SVladimir Oltean 
felix_pre_bridge_flags(struct dsa_switch * ds,int port,struct switchdev_brport_flags val,struct netlink_ext_ack * extack)901421741eaSVladimir Oltean static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
902421741eaSVladimir Oltean 				  struct switchdev_brport_flags val,
903421741eaSVladimir Oltean 				  struct netlink_ext_ack *extack)
904421741eaSVladimir Oltean {
905421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
906421741eaSVladimir Oltean 
907421741eaSVladimir Oltean 	return ocelot_port_pre_bridge_flags(ocelot, port, val);
908421741eaSVladimir Oltean }
909421741eaSVladimir Oltean 
felix_bridge_flags(struct dsa_switch * ds,int port,struct switchdev_brport_flags val,struct netlink_ext_ack * extack)910421741eaSVladimir Oltean static int felix_bridge_flags(struct dsa_switch *ds, int port,
911421741eaSVladimir Oltean 			      struct switchdev_brport_flags val,
912421741eaSVladimir Oltean 			      struct netlink_ext_ack *extack)
913421741eaSVladimir Oltean {
914421741eaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
915421741eaSVladimir Oltean 
916910ee6ccSVladimir Oltean 	if (port == ocelot->npi)
917910ee6ccSVladimir Oltean 		port = ocelot->num_phys_ports;
918910ee6ccSVladimir Oltean 
919421741eaSVladimir Oltean 	ocelot_port_bridge_flags(ocelot, port, val);
920421741eaSVladimir Oltean 
921421741eaSVladimir Oltean 	return 0;
922421741eaSVladimir Oltean }
923421741eaSVladimir Oltean 
felix_bridge_join(struct dsa_switch * ds,int port,struct dsa_bridge bridge,bool * tx_fwd_offload,struct netlink_ext_ack * extack)92456051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
92506b9cce4SVladimir Oltean 			     struct dsa_bridge bridge, bool *tx_fwd_offload,
92606b9cce4SVladimir Oltean 			     struct netlink_ext_ack *extack)
92756051948SVladimir Oltean {
92856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
92956051948SVladimir Oltean 
93054c31984SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
93154c31984SVladimir Oltean 				       extack);
93256051948SVladimir Oltean }
93356051948SVladimir Oltean 
felix_bridge_leave(struct dsa_switch * ds,int port,struct dsa_bridge bridge)93456051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
935d3eed0e5SVladimir Oltean 			       struct dsa_bridge bridge)
93656051948SVladimir Oltean {
93756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
93856051948SVladimir Oltean 
939d3eed0e5SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, bridge.dev);
94056051948SVladimir Oltean }
94156051948SVladimir Oltean 
felix_lag_join(struct dsa_switch * ds,int port,struct dsa_lag lag,struct netdev_lag_upper_info * info,struct netlink_ext_ack * extack)9428fe6832eSVladimir Oltean static int felix_lag_join(struct dsa_switch *ds, int port,
943dedd6a00SVladimir Oltean 			  struct dsa_lag lag,
9442e359b00SVladimir Oltean 			  struct netdev_lag_upper_info *info,
9452e359b00SVladimir Oltean 			  struct netlink_ext_ack *extack)
9468fe6832eSVladimir Oltean {
9478fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
948eca70102SVladimir Oltean 	int err;
9498fe6832eSVladimir Oltean 
950eca70102SVladimir Oltean 	err = ocelot_port_lag_join(ocelot, port, lag.dev, info, extack);
951eca70102SVladimir Oltean 	if (err)
952eca70102SVladimir Oltean 		return err;
953eca70102SVladimir Oltean 
954eca70102SVladimir Oltean 	/* Update the logical LAG port that serves as tag_8021q CPU port */
955eca70102SVladimir Oltean 	if (!dsa_is_cpu_port(ds, port))
956eca70102SVladimir Oltean 		return 0;
957eca70102SVladimir Oltean 
958eca70102SVladimir Oltean 	return felix_port_change_master(ds, port, lag.dev, extack);
9598fe6832eSVladimir Oltean }
9608fe6832eSVladimir Oltean 
felix_lag_leave(struct dsa_switch * ds,int port,struct dsa_lag lag)9618fe6832eSVladimir Oltean static int felix_lag_leave(struct dsa_switch *ds, int port,
962dedd6a00SVladimir Oltean 			   struct dsa_lag lag)
9638fe6832eSVladimir Oltean {
9648fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9658fe6832eSVladimir Oltean 
966dedd6a00SVladimir Oltean 	ocelot_port_lag_leave(ocelot, port, lag.dev);
9678fe6832eSVladimir Oltean 
968eca70102SVladimir Oltean 	/* Update the logical LAG port that serves as tag_8021q CPU port */
969eca70102SVladimir Oltean 	if (!dsa_is_cpu_port(ds, port))
9708fe6832eSVladimir Oltean 		return 0;
971eca70102SVladimir Oltean 
972eca70102SVladimir Oltean 	return felix_port_change_master(ds, port, lag.dev, NULL);
9738fe6832eSVladimir Oltean }
9748fe6832eSVladimir Oltean 
felix_lag_change(struct dsa_switch * ds,int port)9758fe6832eSVladimir Oltean static int felix_lag_change(struct dsa_switch *ds, int port)
9768fe6832eSVladimir Oltean {
9778fe6832eSVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
9788fe6832eSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9798fe6832eSVladimir Oltean 
9808fe6832eSVladimir Oltean 	ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
9818fe6832eSVladimir Oltean 
9828fe6832eSVladimir Oltean 	return 0;
9838fe6832eSVladimir Oltean }
9848fe6832eSVladimir Oltean 
felix_vlan_prepare(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan,struct netlink_ext_ack * extack)98556051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
98601af940eSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
98701af940eSVladimir Oltean 			      struct netlink_ext_ack *extack)
98856051948SVladimir Oltean {
9892f0402feSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
990b7a9e0daSVladimir Oltean 	u16 flags = vlan->flags;
9912f0402feSVladimir Oltean 
9929a720680SVladimir Oltean 	/* Ocelot switches copy frames as-is to the CPU, so the flags:
9939a720680SVladimir Oltean 	 * egress-untagged or not, pvid or not, make no difference. This
9949a720680SVladimir Oltean 	 * behavior is already better than what DSA just tries to approximate
9959a720680SVladimir Oltean 	 * when it installs the VLAN with the same flags on the CPU port.
9969a720680SVladimir Oltean 	 * Just accept any configuration, and don't let ocelot deny installing
9979a720680SVladimir Oltean 	 * multiple native VLANs on the NPI port, because the switch doesn't
9989a720680SVladimir Oltean 	 * look at the port tag settings towards the NPI interface anyway.
9999a720680SVladimir Oltean 	 */
10009a720680SVladimir Oltean 	if (port == ocelot->npi)
10019a720680SVladimir Oltean 		return 0;
10029a720680SVladimir Oltean 
1003b7a9e0daSVladimir Oltean 	return ocelot_vlan_prepare(ocelot, port, vlan->vid,
10042f0402feSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_PVID,
100501af940eSVladimir Oltean 				   flags & BRIDGE_VLAN_INFO_UNTAGGED,
100601af940eSVladimir Oltean 				   extack);
100756051948SVladimir Oltean }
100856051948SVladimir Oltean 
felix_vlan_filtering(struct dsa_switch * ds,int port,bool enabled,struct netlink_ext_ack * extack)100989153ed6SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
101089153ed6SVladimir Oltean 				struct netlink_ext_ack *extack)
101156051948SVladimir Oltean {
101256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
101356051948SVladimir Oltean 
10143b95d1b2SVladimir Oltean 	return ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
101556051948SVladimir Oltean }
101656051948SVladimir Oltean 
felix_vlan_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan,struct netlink_ext_ack * extack)10171958d581SVladimir Oltean static int felix_vlan_add(struct dsa_switch *ds, int port,
101831046a5fSVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan,
101931046a5fSVladimir Oltean 			  struct netlink_ext_ack *extack)
102056051948SVladimir Oltean {
102156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1022183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
10231958d581SVladimir Oltean 	int err;
102456051948SVladimir Oltean 
102501af940eSVladimir Oltean 	err = felix_vlan_prepare(ds, port, vlan, extack);
10261958d581SVladimir Oltean 	if (err)
10271958d581SVladimir Oltean 		return err;
10281958d581SVladimir Oltean 
10291958d581SVladimir Oltean 	return ocelot_vlan_add(ocelot, port, vlan->vid,
1030183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_PVID,
1031183be6f9SVladimir Oltean 			       flags & BRIDGE_VLAN_INFO_UNTAGGED);
103256051948SVladimir Oltean }
103356051948SVladimir Oltean 
felix_vlan_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)103456051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
103556051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
103656051948SVladimir Oltean {
103756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
103856051948SVladimir Oltean 
1039b7a9e0daSVladimir Oltean 	return ocelot_vlan_del(ocelot, port, vlan->vid);
104056051948SVladimir Oltean }
104156051948SVladimir Oltean 
felix_phylink_get_caps(struct dsa_switch * ds,int port,struct phylink_config * config)104279fda660SRussell King (Oracle) static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
104379fda660SRussell King (Oracle) 				   struct phylink_config *config)
104479fda660SRussell King (Oracle) {
104579fda660SRussell King (Oracle) 	struct ocelot *ocelot = ds->priv;
104679fda660SRussell King (Oracle) 
10473e7e7832SVladimir Oltean 	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
10483e7e7832SVladimir Oltean 				   MAC_10 | MAC_100 | MAC_1000FD |
10493e7e7832SVladimir Oltean 				   MAC_2500FD;
10503e7e7832SVladimir Oltean 
105179fda660SRussell King (Oracle) 	__set_bit(ocelot->ports[port]->phy_mode,
105279fda660SRussell King (Oracle) 		  config->supported_interfaces);
105379fda660SRussell King (Oracle) }
105479fda660SRussell King (Oracle) 
felix_phylink_mac_config(struct dsa_switch * ds,int port,unsigned int mode,const struct phylink_link_state * state)1055544435c9SColin Foster static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
1056544435c9SColin Foster 				     unsigned int mode,
1057544435c9SColin Foster 				     const struct phylink_link_state *state)
1058544435c9SColin Foster {
1059544435c9SColin Foster 	struct ocelot *ocelot = ds->priv;
1060544435c9SColin Foster 	struct felix *felix = ocelot_to_felix(ocelot);
1061544435c9SColin Foster 
1062544435c9SColin Foster 	if (felix->info->phylink_mac_config)
1063544435c9SColin Foster 		felix->info->phylink_mac_config(ocelot, port, mode, state);
1064544435c9SColin Foster }
1065544435c9SColin Foster 
felix_phylink_mac_select_pcs(struct dsa_switch * ds,int port,phy_interface_t iface)1066864ba485SRussell King (Oracle) static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds,
1067864ba485SRussell King (Oracle) 							int port,
1068864ba485SRussell King (Oracle) 							phy_interface_t iface)
1069bdeced75SVladimir Oltean {
1070bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1071bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1072864ba485SRussell King (Oracle) 	struct phylink_pcs *pcs = NULL;
1073bdeced75SVladimir Oltean 
107449af6a76SColin Foster 	if (felix->pcs && felix->pcs[port])
1075864ba485SRussell King (Oracle) 		pcs = felix->pcs[port];
1076864ba485SRussell King (Oracle) 
1077864ba485SRussell King (Oracle) 	return pcs;
1078bdeced75SVladimir Oltean }
1079bdeced75SVladimir Oltean 
felix_phylink_mac_link_down(struct dsa_switch * ds,int port,unsigned int link_an_mode,phy_interface_t interface)1080bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
1081bdeced75SVladimir Oltean 					unsigned int link_an_mode,
1082bdeced75SVladimir Oltean 					phy_interface_t interface)
1083bdeced75SVladimir Oltean {
1084bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10851dc6a2a0SColin Foster 	struct felix *felix;
10861dc6a2a0SColin Foster 
10871dc6a2a0SColin Foster 	felix = ocelot_to_felix(ocelot);
1088bdeced75SVladimir Oltean 
1089e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
10901dc6a2a0SColin Foster 				     felix->info->quirks);
1091bdeced75SVladimir Oltean }
1092bdeced75SVladimir Oltean 
felix_phylink_mac_link_up(struct dsa_switch * ds,int port,unsigned int link_an_mode,phy_interface_t interface,struct phy_device * phydev,int speed,int duplex,bool tx_pause,bool rx_pause)1093bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
1094bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
1095bdeced75SVladimir Oltean 				      phy_interface_t interface,
10965b502a7bSRussell King 				      struct phy_device *phydev,
10975b502a7bSRussell King 				      int speed, int duplex,
10985b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
1099bdeced75SVladimir Oltean {
1100bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
11017e14a2dcSVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1102bdeced75SVladimir Oltean 
1103e6e12df6SVladimir Oltean 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1104e6e12df6SVladimir Oltean 				   interface, speed, duplex, tx_pause, rx_pause,
11051dc6a2a0SColin Foster 				   felix->info->quirks);
11067e14a2dcSVladimir Oltean 
11077e14a2dcSVladimir Oltean 	if (felix->info->port_sched_speed_set)
11087e14a2dcSVladimir Oltean 		felix->info->port_sched_speed_set(ocelot, port, speed);
1109bdeced75SVladimir Oltean }
1110bdeced75SVladimir Oltean 
felix_port_enable(struct dsa_switch * ds,int port,struct phy_device * phydev)1111eca70102SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
1112eca70102SVladimir Oltean 			     struct phy_device *phydev)
1113eca70102SVladimir Oltean {
1114eca70102SVladimir Oltean 	struct dsa_port *dp = dsa_to_port(ds, port);
1115eca70102SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1116eca70102SVladimir Oltean 
1117eca70102SVladimir Oltean 	if (!dsa_port_is_user(dp))
1118eca70102SVladimir Oltean 		return 0;
1119eca70102SVladimir Oltean 
1120eca70102SVladimir Oltean 	if (ocelot->npi >= 0) {
1121eca70102SVladimir Oltean 		struct net_device *master = dsa_port_to_master(dp);
1122eca70102SVladimir Oltean 
1123eca70102SVladimir Oltean 		if (felix_cpu_port_for_master(ds, master) != ocelot->npi) {
1124eca70102SVladimir Oltean 			dev_err(ds->dev, "Multiple masters are not allowed\n");
1125eca70102SVladimir Oltean 			return -EINVAL;
1126eca70102SVladimir Oltean 		}
1127eca70102SVladimir Oltean 	}
1128eca70102SVladimir Oltean 
1129eca70102SVladimir Oltean 	return 0;
1130eca70102SVladimir Oltean }
1131eca70102SVladimir Oltean 
felix_port_qos_map_init(struct ocelot * ocelot,int port)1132bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
1133bd2b3161SXiaoliang Yang {
1134bd2b3161SXiaoliang Yang 	int i;
1135bd2b3161SXiaoliang Yang 
1136bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
1137bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1138bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
1139bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
1140bd2b3161SXiaoliang Yang 		       port);
1141bd2b3161SXiaoliang Yang 
114270d39a6eSVladimir Oltean 	for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
1143bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
1144bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
1145bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
1146bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
1147bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
1148bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
1149bd2b3161SXiaoliang Yang 			      port, i);
1150bd2b3161SXiaoliang Yang 	}
1151bd2b3161SXiaoliang Yang }
1152bd2b3161SXiaoliang Yang 
felix_get_stats64(struct dsa_switch * ds,int port,struct rtnl_link_stats64 * stats)1153776b71e5SVladimir Oltean static void felix_get_stats64(struct dsa_switch *ds, int port,
1154776b71e5SVladimir Oltean 			      struct rtnl_link_stats64 *stats)
1155776b71e5SVladimir Oltean {
1156776b71e5SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1157776b71e5SVladimir Oltean 
1158776b71e5SVladimir Oltean 	ocelot_port_get_stats64(ocelot, port, stats);
1159776b71e5SVladimir Oltean }
1160776b71e5SVladimir Oltean 
felix_get_pause_stats(struct dsa_switch * ds,int port,struct ethtool_pause_stats * pause_stats)1161e32036e1SVladimir Oltean static void felix_get_pause_stats(struct dsa_switch *ds, int port,
1162e32036e1SVladimir Oltean 				  struct ethtool_pause_stats *pause_stats)
1163e32036e1SVladimir Oltean {
1164e32036e1SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1165e32036e1SVladimir Oltean 
1166e32036e1SVladimir Oltean 	ocelot_port_get_pause_stats(ocelot, port, pause_stats);
1167e32036e1SVladimir Oltean }
1168e32036e1SVladimir Oltean 
felix_get_rmon_stats(struct dsa_switch * ds,int port,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** ranges)1169e32036e1SVladimir Oltean static void felix_get_rmon_stats(struct dsa_switch *ds, int port,
1170e32036e1SVladimir Oltean 				 struct ethtool_rmon_stats *rmon_stats,
1171e32036e1SVladimir Oltean 				 const struct ethtool_rmon_hist_range **ranges)
1172e32036e1SVladimir Oltean {
1173e32036e1SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1174e32036e1SVladimir Oltean 
1175e32036e1SVladimir Oltean 	ocelot_port_get_rmon_stats(ocelot, port, rmon_stats, ranges);
1176e32036e1SVladimir Oltean }
1177e32036e1SVladimir Oltean 
felix_get_eth_ctrl_stats(struct dsa_switch * ds,int port,struct ethtool_eth_ctrl_stats * ctrl_stats)1178e32036e1SVladimir Oltean static void felix_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
1179e32036e1SVladimir Oltean 				     struct ethtool_eth_ctrl_stats *ctrl_stats)
1180e32036e1SVladimir Oltean {
1181e32036e1SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1182e32036e1SVladimir Oltean 
1183e32036e1SVladimir Oltean 	ocelot_port_get_eth_ctrl_stats(ocelot, port, ctrl_stats);
1184e32036e1SVladimir Oltean }
1185e32036e1SVladimir Oltean 
felix_get_eth_mac_stats(struct dsa_switch * ds,int port,struct ethtool_eth_mac_stats * mac_stats)1186e32036e1SVladimir Oltean static void felix_get_eth_mac_stats(struct dsa_switch *ds, int port,
1187e32036e1SVladimir Oltean 				    struct ethtool_eth_mac_stats *mac_stats)
1188e32036e1SVladimir Oltean {
1189e32036e1SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1190e32036e1SVladimir Oltean 
1191e32036e1SVladimir Oltean 	ocelot_port_get_eth_mac_stats(ocelot, port, mac_stats);
1192e32036e1SVladimir Oltean }
1193e32036e1SVladimir Oltean 
felix_get_eth_phy_stats(struct dsa_switch * ds,int port,struct ethtool_eth_phy_stats * phy_stats)1194e32036e1SVladimir Oltean static void felix_get_eth_phy_stats(struct dsa_switch *ds, int port,
1195e32036e1SVladimir Oltean 				    struct ethtool_eth_phy_stats *phy_stats)
1196e32036e1SVladimir Oltean {
1197e32036e1SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1198e32036e1SVladimir Oltean 
1199e32036e1SVladimir Oltean 	ocelot_port_get_eth_phy_stats(ocelot, port, phy_stats);
1200e32036e1SVladimir Oltean }
1201e32036e1SVladimir Oltean 
felix_get_strings(struct dsa_switch * ds,int port,u32 stringset,u8 * data)120256051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
120356051948SVladimir Oltean 			      u32 stringset, u8 *data)
120456051948SVladimir Oltean {
120556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
120656051948SVladimir Oltean 
120756051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
120856051948SVladimir Oltean }
120956051948SVladimir Oltean 
felix_get_ethtool_stats(struct dsa_switch * ds,int port,u64 * data)121056051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
121156051948SVladimir Oltean {
121256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
121356051948SVladimir Oltean 
121456051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
121556051948SVladimir Oltean }
121656051948SVladimir Oltean 
felix_get_sset_count(struct dsa_switch * ds,int port,int sset)121756051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
121856051948SVladimir Oltean {
121956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
122056051948SVladimir Oltean 
122156051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
122256051948SVladimir Oltean }
122356051948SVladimir Oltean 
felix_get_ts_info(struct dsa_switch * ds,int port,struct ethtool_ts_info * info)122456051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
122556051948SVladimir Oltean 			     struct ethtool_ts_info *info)
122656051948SVladimir Oltean {
122756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
122856051948SVladimir Oltean 
122956051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
123056051948SVladimir Oltean }
123156051948SVladimir Oltean 
1232acf242fcSColin Foster static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
1233acf242fcSColin Foster 	[PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
1234acf242fcSColin Foster 	[PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
1235acf242fcSColin Foster 	[PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
1236acf242fcSColin Foster 	[PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
123711ecf341SVladimir Oltean 	[PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
1238acf242fcSColin Foster 	[PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
1239acf242fcSColin Foster };
1240acf242fcSColin Foster 
felix_validate_phy_mode(struct felix * felix,int port,phy_interface_t phy_mode)1241acf242fcSColin Foster static int felix_validate_phy_mode(struct felix *felix, int port,
1242acf242fcSColin Foster 				   phy_interface_t phy_mode)
1243acf242fcSColin Foster {
1244acf242fcSColin Foster 	u32 modes = felix->info->port_modes[port];
1245acf242fcSColin Foster 
1246acf242fcSColin Foster 	if (felix_phy_match_table[phy_mode] & modes)
1247acf242fcSColin Foster 		return 0;
1248acf242fcSColin Foster 	return -EOPNOTSUPP;
1249acf242fcSColin Foster }
1250acf242fcSColin Foster 
felix_parse_ports_node(struct felix * felix,struct device_node * ports_node,phy_interface_t * port_phy_modes)1251bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
1252bdeced75SVladimir Oltean 				  struct device_node *ports_node,
1253bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
1254bdeced75SVladimir Oltean {
1255bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1256bdeced75SVladimir Oltean 	struct device_node *child;
1257bdeced75SVladimir Oltean 
125837fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
1259bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
1260bdeced75SVladimir Oltean 		u32 port;
1261bdeced75SVladimir Oltean 		int err;
1262bdeced75SVladimir Oltean 
1263bdeced75SVladimir Oltean 		/* Get switch port number from DT */
1264bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
1265bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
1266bdeced75SVladimir Oltean 				"(property \"reg\")\n");
1267bdeced75SVladimir Oltean 			of_node_put(child);
1268bdeced75SVladimir Oltean 			return -ENODEV;
1269bdeced75SVladimir Oltean 		}
1270bdeced75SVladimir Oltean 
1271bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
1272bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
1273bdeced75SVladimir Oltean 		if (err) {
1274bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
1275bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
1276bdeced75SVladimir Oltean 				port);
1277bdeced75SVladimir Oltean 			of_node_put(child);
1278bdeced75SVladimir Oltean 			return -ENODEV;
1279bdeced75SVladimir Oltean 		}
1280bdeced75SVladimir Oltean 
1281acf242fcSColin Foster 		err = felix_validate_phy_mode(felix, port, phy_mode);
1282bdeced75SVladimir Oltean 		if (err < 0) {
1283de879a01SColin Foster 			dev_info(dev, "Unsupported PHY mode %s on port %d\n",
1284bdeced75SVladimir Oltean 				 phy_modes(phy_mode), port);
1285de879a01SColin Foster 
1286de879a01SColin Foster 			/* Leave port_phy_modes[port] = 0, which is also
1287de879a01SColin Foster 			 * PHY_INTERFACE_MODE_NA. This will perform a
1288de879a01SColin Foster 			 * best-effort to bring up as many ports as possible.
1289de879a01SColin Foster 			 */
1290de879a01SColin Foster 			continue;
1291bdeced75SVladimir Oltean 		}
1292bdeced75SVladimir Oltean 
1293bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
1294bdeced75SVladimir Oltean 	}
1295bdeced75SVladimir Oltean 
1296bdeced75SVladimir Oltean 	return 0;
1297bdeced75SVladimir Oltean }
1298bdeced75SVladimir Oltean 
felix_parse_dt(struct felix * felix,phy_interface_t * port_phy_modes)1299bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
1300bdeced75SVladimir Oltean {
1301bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
1302bdeced75SVladimir Oltean 	struct device_node *switch_node;
1303bdeced75SVladimir Oltean 	struct device_node *ports_node;
1304bdeced75SVladimir Oltean 	int err;
1305bdeced75SVladimir Oltean 
1306bdeced75SVladimir Oltean 	switch_node = dev->of_node;
1307bdeced75SVladimir Oltean 
1308bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
1309abecbfcdSVladimir Oltean 	if (!ports_node)
1310abecbfcdSVladimir Oltean 		ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
1311bdeced75SVladimir Oltean 	if (!ports_node) {
1312abecbfcdSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
1313bdeced75SVladimir Oltean 		return -ENODEV;
1314bdeced75SVladimir Oltean 	}
1315bdeced75SVladimir Oltean 
1316bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
1317bdeced75SVladimir Oltean 	of_node_put(ports_node);
1318bdeced75SVladimir Oltean 
1319bdeced75SVladimir Oltean 	return err;
1320bdeced75SVladimir Oltean }
1321bdeced75SVladimir Oltean 
felix_request_regmap_by_name(struct felix * felix,const char * resource_name)13221109b97bSVladimir Oltean static struct regmap *felix_request_regmap_by_name(struct felix *felix,
13231109b97bSVladimir Oltean 						   const char *resource_name)
13241109b97bSVladimir Oltean {
13251109b97bSVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
13261109b97bSVladimir Oltean 	struct resource res;
13271109b97bSVladimir Oltean 	int i;
13281109b97bSVladimir Oltean 
1329dc454fa4SColin Foster 	/* In an MFD configuration, regmaps are registered directly to the
1330dc454fa4SColin Foster 	 * parent device before the child devices are probed, so there is no
1331dc454fa4SColin Foster 	 * need to initialize a new one.
1332dc454fa4SColin Foster 	 */
1333dc454fa4SColin Foster 	if (!felix->info->resources)
1334dc454fa4SColin Foster 		return dev_get_regmap(ocelot->dev->parent, resource_name);
1335dc454fa4SColin Foster 
13361109b97bSVladimir Oltean 	for (i = 0; i < felix->info->num_resources; i++) {
13371109b97bSVladimir Oltean 		if (strcmp(resource_name, felix->info->resources[i].name))
13381109b97bSVladimir Oltean 			continue;
13391109b97bSVladimir Oltean 
13401109b97bSVladimir Oltean 		memcpy(&res, &felix->info->resources[i], sizeof(res));
13411109b97bSVladimir Oltean 		res.start += felix->switch_base;
13421109b97bSVladimir Oltean 		res.end += felix->switch_base;
13431109b97bSVladimir Oltean 
13441109b97bSVladimir Oltean 		return ocelot_regmap_init(ocelot, &res);
13451109b97bSVladimir Oltean 	}
13461109b97bSVladimir Oltean 
13471109b97bSVladimir Oltean 	return ERR_PTR(-ENOENT);
13481109b97bSVladimir Oltean }
13491109b97bSVladimir Oltean 
felix_request_regmap(struct felix * felix,enum ocelot_target target)13501109b97bSVladimir Oltean static struct regmap *felix_request_regmap(struct felix *felix,
13511109b97bSVladimir Oltean 					   enum ocelot_target target)
13521109b97bSVladimir Oltean {
13531109b97bSVladimir Oltean 	const char *resource_name = felix->info->resource_names[target];
13541109b97bSVladimir Oltean 
13551109b97bSVladimir Oltean 	/* If the driver didn't provide a resource name for the target,
13561109b97bSVladimir Oltean 	 * the resource is optional.
13571109b97bSVladimir Oltean 	 */
13581109b97bSVladimir Oltean 	if (!resource_name)
13591109b97bSVladimir Oltean 		return NULL;
13601109b97bSVladimir Oltean 
13611109b97bSVladimir Oltean 	return felix_request_regmap_by_name(felix, resource_name);
13621109b97bSVladimir Oltean }
13631109b97bSVladimir Oltean 
felix_request_port_regmap(struct felix * felix,int port)13641109b97bSVladimir Oltean static struct regmap *felix_request_port_regmap(struct felix *felix, int port)
13651109b97bSVladimir Oltean {
13661109b97bSVladimir Oltean 	char resource_name[32];
13671109b97bSVladimir Oltean 
13681109b97bSVladimir Oltean 	sprintf(resource_name, "port%d", port);
13691109b97bSVladimir Oltean 
13701109b97bSVladimir Oltean 	return felix_request_regmap_by_name(felix, resource_name);
13711109b97bSVladimir Oltean }
13721109b97bSVladimir Oltean 
felix_init_structs(struct felix * felix,int num_phys_ports)137356051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
137456051948SVladimir Oltean {
137556051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
1376bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
13771109b97bSVladimir Oltean 	struct regmap *target;
137856051948SVladimir Oltean 	int port, i, err;
137956051948SVladimir Oltean 
138056051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
138156051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
138256051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
138356051948SVladimir Oltean 	if (!ocelot->ports)
138456051948SVladimir Oltean 		return -ENOMEM;
138556051948SVladimir Oltean 
138656051948SVladimir Oltean 	ocelot->map		= felix->info->map;
138721ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
138807d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
138977043c37SXiaoliang Yang 	ocelot->vcap_pol.base	= felix->info->vcap_pol_base;
139077043c37SXiaoliang Yang 	ocelot->vcap_pol.max	= felix->info->vcap_pol_max;
139177043c37SXiaoliang Yang 	ocelot->vcap_pol.base2	= felix->info->vcap_pol_base2;
139277043c37SXiaoliang Yang 	ocelot->vcap_pol.max2	= felix->info->vcap_pol_max2;
139356051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
1394cacea62fSVladimir Oltean 	ocelot->npi_inj_prefix	= OCELOT_TAG_PREFIX_SHORT;
1395cacea62fSVladimir Oltean 	ocelot->npi_xtr_prefix	= OCELOT_TAG_PREFIX_SHORT;
1396f59fd9caSVladimir Oltean 	ocelot->devlink		= felix->ds->devlink;
139756051948SVladimir Oltean 
1398bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
1399bdeced75SVladimir Oltean 				 GFP_KERNEL);
1400bdeced75SVladimir Oltean 	if (!port_phy_modes)
1401bdeced75SVladimir Oltean 		return -ENOMEM;
1402bdeced75SVladimir Oltean 
1403bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
1404bdeced75SVladimir Oltean 	if (err) {
1405bdeced75SVladimir Oltean 		kfree(port_phy_modes);
1406bdeced75SVladimir Oltean 		return err;
1407bdeced75SVladimir Oltean 	}
1408bdeced75SVladimir Oltean 
140956051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
14101109b97bSVladimir Oltean 		target = felix_request_regmap(felix, i);
141156051948SVladimir Oltean 		if (IS_ERR(target)) {
141256051948SVladimir Oltean 			dev_err(ocelot->dev,
14131109b97bSVladimir Oltean 				"Failed to map device memory space: %pe\n",
14141109b97bSVladimir Oltean 				target);
1415bdeced75SVladimir Oltean 			kfree(port_phy_modes);
141656051948SVladimir Oltean 			return PTR_ERR(target);
141756051948SVladimir Oltean 		}
141856051948SVladimir Oltean 
141956051948SVladimir Oltean 		ocelot->targets[i] = target;
142056051948SVladimir Oltean 	}
142156051948SVladimir Oltean 
142256051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
142356051948SVladimir Oltean 	if (err) {
142456051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
1425bdeced75SVladimir Oltean 		kfree(port_phy_modes);
142656051948SVladimir Oltean 		return err;
142756051948SVladimir Oltean 	}
142856051948SVladimir Oltean 
142956051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
143056051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
143156051948SVladimir Oltean 
143256051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
143356051948SVladimir Oltean 					   sizeof(struct ocelot_port),
143456051948SVladimir Oltean 					   GFP_KERNEL);
143556051948SVladimir Oltean 		if (!ocelot_port) {
143656051948SVladimir Oltean 			dev_err(ocelot->dev,
143756051948SVladimir Oltean 				"failed to allocate port memory\n");
1438bdeced75SVladimir Oltean 			kfree(port_phy_modes);
143956051948SVladimir Oltean 			return -ENOMEM;
144056051948SVladimir Oltean 		}
144156051948SVladimir Oltean 
14421109b97bSVladimir Oltean 		target = felix_request_port_regmap(felix, port);
144391c724cfSVladimir Oltean 		if (IS_ERR(target)) {
144456051948SVladimir Oltean 			dev_err(ocelot->dev,
14451109b97bSVladimir Oltean 				"Failed to map memory space for port %d: %pe\n",
14461109b97bSVladimir Oltean 				port, target);
1447bdeced75SVladimir Oltean 			kfree(port_phy_modes);
144891c724cfSVladimir Oltean 			return PTR_ERR(target);
144956051948SVladimir Oltean 		}
145056051948SVladimir Oltean 
1451bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
145256051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
145391c724cfSVladimir Oltean 		ocelot_port->target = target;
14547e708760SVladimir Oltean 		ocelot_port->index = port;
145556051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
145656051948SVladimir Oltean 	}
145756051948SVladimir Oltean 
1458bdeced75SVladimir Oltean 	kfree(port_phy_modes);
1459bdeced75SVladimir Oltean 
1460bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
1461bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
1462bdeced75SVladimir Oltean 		if (err < 0)
1463bdeced75SVladimir Oltean 			return err;
1464bdeced75SVladimir Oltean 	}
1465bdeced75SVladimir Oltean 
146656051948SVladimir Oltean 	return 0;
146756051948SVladimir Oltean }
146856051948SVladimir Oltean 
ocelot_port_purge_txtstamp_skb(struct ocelot * ocelot,int port,struct sk_buff * skb)14691328a883SVladimir Oltean static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
14701328a883SVladimir Oltean 					   struct sk_buff *skb)
14711328a883SVladimir Oltean {
14721328a883SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
14731328a883SVladimir Oltean 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
14741328a883SVladimir Oltean 	struct sk_buff *skb_match = NULL, *skb_tmp;
14751328a883SVladimir Oltean 	unsigned long flags;
14761328a883SVladimir Oltean 
14771328a883SVladimir Oltean 	if (!clone)
14781328a883SVladimir Oltean 		return;
14791328a883SVladimir Oltean 
14801328a883SVladimir Oltean 	spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
14811328a883SVladimir Oltean 
14821328a883SVladimir Oltean 	skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
14831328a883SVladimir Oltean 		if (skb != clone)
14841328a883SVladimir Oltean 			continue;
14851328a883SVladimir Oltean 		__skb_unlink(skb, &ocelot_port->tx_skbs);
14861328a883SVladimir Oltean 		skb_match = skb;
14871328a883SVladimir Oltean 		break;
14881328a883SVladimir Oltean 	}
14891328a883SVladimir Oltean 
14901328a883SVladimir Oltean 	spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
14911328a883SVladimir Oltean 
14921328a883SVladimir Oltean 	WARN_ONCE(!skb_match,
14931328a883SVladimir Oltean 		  "Could not find skb clone in TX timestamping list\n");
14941328a883SVladimir Oltean }
14951328a883SVladimir Oltean 
149649f885b2SVladimir Oltean #define work_to_xmit_work(w) \
149749f885b2SVladimir Oltean 		container_of((w), struct felix_deferred_xmit_work, work)
149849f885b2SVladimir Oltean 
felix_port_deferred_xmit(struct kthread_work * work)149949f885b2SVladimir Oltean static void felix_port_deferred_xmit(struct kthread_work *work)
150049f885b2SVladimir Oltean {
150149f885b2SVladimir Oltean 	struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
150249f885b2SVladimir Oltean 	struct dsa_switch *ds = xmit_work->dp->ds;
150349f885b2SVladimir Oltean 	struct sk_buff *skb = xmit_work->skb;
150449f885b2SVladimir Oltean 	u32 rew_op = ocelot_ptp_rew_op(skb);
150549f885b2SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
150649f885b2SVladimir Oltean 	int port = xmit_work->dp->index;
150749f885b2SVladimir Oltean 	int retries = 10;
150849f885b2SVladimir Oltean 
1509*e83b49ecSVladimir Oltean 	ocelot_lock_inj_grp(ocelot, 0);
1510*e83b49ecSVladimir Oltean 
151149f885b2SVladimir Oltean 	do {
151249f885b2SVladimir Oltean 		if (ocelot_can_inject(ocelot, 0))
151349f885b2SVladimir Oltean 			break;
151449f885b2SVladimir Oltean 
151549f885b2SVladimir Oltean 		cpu_relax();
151649f885b2SVladimir Oltean 	} while (--retries);
151749f885b2SVladimir Oltean 
151849f885b2SVladimir Oltean 	if (!retries) {
1519*e83b49ecSVladimir Oltean 		ocelot_unlock_inj_grp(ocelot, 0);
152049f885b2SVladimir Oltean 		dev_err(ocelot->dev, "port %d failed to inject skb\n",
152149f885b2SVladimir Oltean 			port);
15221328a883SVladimir Oltean 		ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
152349f885b2SVladimir Oltean 		kfree_skb(skb);
152449f885b2SVladimir Oltean 		return;
152549f885b2SVladimir Oltean 	}
152649f885b2SVladimir Oltean 
152749f885b2SVladimir Oltean 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
152849f885b2SVladimir Oltean 
1529*e83b49ecSVladimir Oltean 	ocelot_unlock_inj_grp(ocelot, 0);
1530*e83b49ecSVladimir Oltean 
153149f885b2SVladimir Oltean 	consume_skb(skb);
153249f885b2SVladimir Oltean 	kfree(xmit_work);
153349f885b2SVladimir Oltean }
153449f885b2SVladimir Oltean 
felix_connect_tag_protocol(struct dsa_switch * ds,enum dsa_tag_protocol proto)153535d97680SVladimir Oltean static int felix_connect_tag_protocol(struct dsa_switch *ds,
153635d97680SVladimir Oltean 				      enum dsa_tag_protocol proto)
153749f885b2SVladimir Oltean {
153835d97680SVladimir Oltean 	struct ocelot_8021q_tagger_data *tagger_data;
153949f885b2SVladimir Oltean 
154035d97680SVladimir Oltean 	switch (proto) {
154135d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT_8021Q:
154235d97680SVladimir Oltean 		tagger_data = ocelot_8021q_tagger_data(ds);
154335d97680SVladimir Oltean 		tagger_data->xmit_work_fn = felix_port_deferred_xmit;
154449f885b2SVladimir Oltean 		return 0;
154535d97680SVladimir Oltean 	case DSA_TAG_PROTO_OCELOT:
154635d97680SVladimir Oltean 	case DSA_TAG_PROTO_SEVILLE:
154749f885b2SVladimir Oltean 		return 0;
154835d97680SVladimir Oltean 	default:
154935d97680SVladimir Oltean 		return -EPROTONOSUPPORT;
155049f885b2SVladimir Oltean 	}
155149f885b2SVladimir Oltean }
155249f885b2SVladimir Oltean 
felix_setup(struct dsa_switch * ds)155356051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
155456051948SVladimir Oltean {
155556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
155656051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
15572960bb14SVladimir Oltean 	struct dsa_port *dp;
15582960bb14SVladimir Oltean 	int err;
155956051948SVladimir Oltean 
156056051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
156156051948SVladimir Oltean 	if (err)
156256051948SVladimir Oltean 		return err;
156356051948SVladimir Oltean 
15643821fd01SColin Foster 	if (ocelot->targets[HSIO])
15653821fd01SColin Foster 		ocelot_pll5_init(ocelot);
15663821fd01SColin Foster 
1567d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
1568d1cc0e93SVladimir Oltean 	if (err)
15696b73b7c9SVladimir Oltean 		goto out_mdiobus_free;
1570d1cc0e93SVladimir Oltean 
15712b49d128SYangbo Lu 	if (ocelot->ptp) {
15722ac7c6c5SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
15732b49d128SYangbo Lu 		if (err) {
15742b49d128SYangbo Lu 			dev_err(ocelot->dev,
15752b49d128SYangbo Lu 				"Timestamp initialization failed\n");
15762b49d128SYangbo Lu 			ocelot->ptp = 0;
15772b49d128SYangbo Lu 		}
15782b49d128SYangbo Lu 	}
157956051948SVladimir Oltean 
15802960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds) {
15812960bb14SVladimir Oltean 		ocelot_init_port(ocelot, dp->index);
1582bd2b3161SXiaoliang Yang 
15836865eceeSColin Foster 		if (felix->info->configure_serdes)
15846865eceeSColin Foster 			felix->info->configure_serdes(ocelot, dp->index,
15856865eceeSColin Foster 						      dp->dn);
15866865eceeSColin Foster 
1587bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
1588bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
1589bd2b3161SXiaoliang Yang 		 */
15902960bb14SVladimir Oltean 		felix_port_qos_map_init(ocelot, dp->index);
159156051948SVladimir Oltean 	}
159256051948SVladimir Oltean 
1593f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
1594f59fd9caSVladimir Oltean 	if (err)
15956b73b7c9SVladimir Oltean 		goto out_deinit_ports;
1596f59fd9caSVladimir Oltean 
15977a29d220SVladimir Oltean 	/* The initial tag protocol is NPI which won't fail during initial
15987a29d220SVladimir Oltean 	 * setup, there's no real point in checking for errors.
15991cf3299bSVladimir Oltean 	 */
16007a29d220SVladimir Oltean 	felix_change_tag_protocol(ds, felix->tag_proto);
16011cf3299bSVladimir Oltean 
16020b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
1603c54913c1SVladimir Oltean 	ds->assisted_learning_on_cpu_port = true;
160454c31984SVladimir Oltean 	ds->fdb_isolation = true;
160554c31984SVladimir Oltean 	ds->max_num_bridges = ds->num_ports;
1606bdeced75SVladimir Oltean 
160756051948SVladimir Oltean 	return 0;
16086b73b7c9SVladimir Oltean 
16096b73b7c9SVladimir Oltean out_deinit_ports:
16102960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
16112960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
16126b73b7c9SVladimir Oltean 
16136b73b7c9SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
16146b73b7c9SVladimir Oltean 	ocelot_deinit(ocelot);
16156b73b7c9SVladimir Oltean 
16166b73b7c9SVladimir Oltean out_mdiobus_free:
16176b73b7c9SVladimir Oltean 	if (felix->info->mdio_bus_free)
16186b73b7c9SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
16196b73b7c9SVladimir Oltean 
16206b73b7c9SVladimir Oltean 	return err;
162156051948SVladimir Oltean }
162256051948SVladimir Oltean 
felix_teardown(struct dsa_switch * ds)162356051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
162456051948SVladimir Oltean {
162556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1626bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
16272960bb14SVladimir Oltean 	struct dsa_port *dp;
1628bdeced75SVladimir Oltean 
1629a94c16a2SVladimir Oltean 	rtnl_lock();
16307a29d220SVladimir Oltean 	if (felix->tag_proto_ops)
16317a29d220SVladimir Oltean 		felix->tag_proto_ops->teardown(ds);
1632a94c16a2SVladimir Oltean 	rtnl_unlock();
1633adb3dccfSVladimir Oltean 
16342960bb14SVladimir Oltean 	dsa_switch_for_each_available_port(dp, ds)
16352960bb14SVladimir Oltean 		ocelot_deinit_port(ocelot, dp->index);
1636d19741b0SVladimir Oltean 
163749f885b2SVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
163849f885b2SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
163949f885b2SVladimir Oltean 	ocelot_deinit(ocelot);
164049f885b2SVladimir Oltean 
1641d19741b0SVladimir Oltean 	if (felix->info->mdio_bus_free)
1642d19741b0SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
164356051948SVladimir Oltean }
164456051948SVladimir Oltean 
felix_hwtstamp_get(struct dsa_switch * ds,int port,struct ifreq * ifr)1645c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
1646c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1647c0bcf537SYangbo Lu {
1648c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1649c0bcf537SYangbo Lu 
1650c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
1651c0bcf537SYangbo Lu }
1652c0bcf537SYangbo Lu 
felix_hwtstamp_set(struct dsa_switch * ds,int port,struct ifreq * ifr)1653c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
1654c0bcf537SYangbo Lu 			      struct ifreq *ifr)
1655c0bcf537SYangbo Lu {
1656c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
165799348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
165899348004SVladimir Oltean 	bool using_tag_8021q;
165999348004SVladimir Oltean 	int err;
1660c0bcf537SYangbo Lu 
166199348004SVladimir Oltean 	err = ocelot_hwstamp_set(ocelot, port, ifr);
166299348004SVladimir Oltean 	if (err)
166399348004SVladimir Oltean 		return err;
166499348004SVladimir Oltean 
166599348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
166699348004SVladimir Oltean 
166799348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
1668c0bcf537SYangbo Lu }
1669c0bcf537SYangbo Lu 
felix_check_xtr_pkt(struct ocelot * ocelot)1670d219b4b6SVladimir Oltean static bool felix_check_xtr_pkt(struct ocelot *ocelot)
16710a6f17c6SVladimir Oltean {
16720a6f17c6SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
1673dbd03285SVladimir Oltean 	int err = 0, grp = 0;
16740a6f17c6SVladimir Oltean 
16750a6f17c6SVladimir Oltean 	if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
16760a6f17c6SVladimir Oltean 		return false;
16770a6f17c6SVladimir Oltean 
16780a6f17c6SVladimir Oltean 	if (!felix->info->quirk_no_xtr_irq)
16790a6f17c6SVladimir Oltean 		return false;
16800a6f17c6SVladimir Oltean 
1681*e83b49ecSVladimir Oltean 	ocelot_lock_xtr_grp(ocelot, grp);
1682*e83b49ecSVladimir Oltean 
16830a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
16840a6f17c6SVladimir Oltean 		struct sk_buff *skb;
16850a6f17c6SVladimir Oltean 		unsigned int type;
16860a6f17c6SVladimir Oltean 
16870a6f17c6SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
16880a6f17c6SVladimir Oltean 		if (err)
16890a6f17c6SVladimir Oltean 			goto out;
16900a6f17c6SVladimir Oltean 
16910a6f17c6SVladimir Oltean 		/* We trap to the CPU port module all PTP frames, but
16920a6f17c6SVladimir Oltean 		 * felix_rxtstamp() only gets called for event frames.
16930a6f17c6SVladimir Oltean 		 * So we need to avoid sending duplicate general
16940a6f17c6SVladimir Oltean 		 * message frames by running a second BPF classifier
16950a6f17c6SVladimir Oltean 		 * here and dropping those.
16960a6f17c6SVladimir Oltean 		 */
16970a6f17c6SVladimir Oltean 		__skb_push(skb, ETH_HLEN);
16980a6f17c6SVladimir Oltean 
16990a6f17c6SVladimir Oltean 		type = ptp_classify_raw(skb);
17000a6f17c6SVladimir Oltean 
17010a6f17c6SVladimir Oltean 		__skb_pull(skb, ETH_HLEN);
17020a6f17c6SVladimir Oltean 
17030a6f17c6SVladimir Oltean 		if (type == PTP_CLASS_NONE) {
17040a6f17c6SVladimir Oltean 			kfree_skb(skb);
17050a6f17c6SVladimir Oltean 			continue;
17060a6f17c6SVladimir Oltean 		}
17070a6f17c6SVladimir Oltean 
17080a6f17c6SVladimir Oltean 		netif_rx(skb);
17090a6f17c6SVladimir Oltean 	}
17100a6f17c6SVladimir Oltean 
17110a6f17c6SVladimir Oltean out:
17125d3bb7ddSVladimir Oltean 	if (err < 0) {
17135d3bb7ddSVladimir Oltean 		dev_err_ratelimited(ocelot->dev,
17145d3bb7ddSVladimir Oltean 				    "Error during packet extraction: %pe\n",
17155d3bb7ddSVladimir Oltean 				    ERR_PTR(err));
17160a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
17175d3bb7ddSVladimir Oltean 	}
17180a6f17c6SVladimir Oltean 
1719*e83b49ecSVladimir Oltean 	ocelot_unlock_xtr_grp(ocelot, grp);
1720*e83b49ecSVladimir Oltean 
17210a6f17c6SVladimir Oltean 	return true;
17220a6f17c6SVladimir Oltean }
17230a6f17c6SVladimir Oltean 
felix_rxtstamp(struct dsa_switch * ds,int port,struct sk_buff * skb,unsigned int type)1724c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
1725c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
1726c0bcf537SYangbo Lu {
172792f62485SVladimir Oltean 	u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
1728c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
1729c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1730c0bcf537SYangbo Lu 	struct timespec64 ts;
173192f62485SVladimir Oltean 	u32 tstamp_hi;
173292f62485SVladimir Oltean 	u64 tstamp;
1733c0bcf537SYangbo Lu 
17342edcfcbbSVladimir Oltean 	switch (type & PTP_CLASS_PMASK) {
17352edcfcbbSVladimir Oltean 	case PTP_CLASS_L2:
17362edcfcbbSVladimir Oltean 		if (!(ocelot->ports[port]->trap_proto & OCELOT_PROTO_PTP_L2))
17372edcfcbbSVladimir Oltean 			return false;
17382edcfcbbSVladimir Oltean 		break;
17392edcfcbbSVladimir Oltean 	case PTP_CLASS_IPV4:
17402edcfcbbSVladimir Oltean 	case PTP_CLASS_IPV6:
17412edcfcbbSVladimir Oltean 		if (!(ocelot->ports[port]->trap_proto & OCELOT_PROTO_PTP_L4))
17422edcfcbbSVladimir Oltean 			return false;
17432edcfcbbSVladimir Oltean 		break;
17442edcfcbbSVladimir Oltean 	}
17452edcfcbbSVladimir Oltean 
17460a6f17c6SVladimir Oltean 	/* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
17470a6f17c6SVladimir Oltean 	 * for RX timestamping. Then free it, and poll for its copy through
17480a6f17c6SVladimir Oltean 	 * MMIO in the CPU port module, and inject that into the stack from
17490a6f17c6SVladimir Oltean 	 * ocelot_xtr_poll().
17500a6f17c6SVladimir Oltean 	 */
1751d219b4b6SVladimir Oltean 	if (felix_check_xtr_pkt(ocelot)) {
17520a6f17c6SVladimir Oltean 		kfree_skb(skb);
17530a6f17c6SVladimir Oltean 		return true;
17540a6f17c6SVladimir Oltean 	}
17550a6f17c6SVladimir Oltean 
1756c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1757c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1758c0bcf537SYangbo Lu 
1759c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
1760c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
1761c0bcf537SYangbo Lu 		tstamp_hi--;
1762c0bcf537SYangbo Lu 
1763c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
1764c0bcf537SYangbo Lu 
1765c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
1766c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1767c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
1768c0bcf537SYangbo Lu 	return false;
1769c0bcf537SYangbo Lu }
1770c0bcf537SYangbo Lu 
felix_txtstamp(struct dsa_switch * ds,int port,struct sk_buff * skb)17715c5416f5SYangbo Lu static void felix_txtstamp(struct dsa_switch *ds, int port,
17725c5416f5SYangbo Lu 			   struct sk_buff *skb)
1773c0bcf537SYangbo Lu {
1774c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
1775682eaad9SYangbo Lu 	struct sk_buff *clone = NULL;
1776c0bcf537SYangbo Lu 
1777682eaad9SYangbo Lu 	if (!ocelot->ptp)
17785c5416f5SYangbo Lu 		return;
1779c0bcf537SYangbo Lu 
178052849bcfSVladimir Oltean 	if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
178152849bcfSVladimir Oltean 		dev_err_ratelimited(ds->dev,
178252849bcfSVladimir Oltean 				    "port %d delivering skb without TX timestamp\n",
178352849bcfSVladimir Oltean 				    port);
1784682eaad9SYangbo Lu 		return;
178552849bcfSVladimir Oltean 	}
1786682eaad9SYangbo Lu 
1787682eaad9SYangbo Lu 	if (clone)
1788c4b364ceSYangbo Lu 		OCELOT_SKB_CB(skb)->clone = clone;
17895c5416f5SYangbo Lu }
1790c0bcf537SYangbo Lu 
felix_change_mtu(struct dsa_switch * ds,int port,int new_mtu)17910b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
17920b912fc9SVladimir Oltean {
17930b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
179455a515b1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
17950b912fc9SVladimir Oltean 
17960b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
17970b912fc9SVladimir Oltean 
1798009d30f1SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
179955a515b1SVladimir Oltean 
1800c6081914SVladimir Oltean 	if (ocelot_port->taprio && ocelot->ops->tas_guard_bands_update)
1801c6081914SVladimir Oltean 		ocelot->ops->tas_guard_bands_update(ocelot, port);
180255a515b1SVladimir Oltean 
1803009d30f1SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
180455a515b1SVladimir Oltean 
18050b912fc9SVladimir Oltean 	return 0;
18060b912fc9SVladimir Oltean }
18070b912fc9SVladimir Oltean 
felix_get_max_mtu(struct dsa_switch * ds,int port)18080b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
18090b912fc9SVladimir Oltean {
18100b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
18110b912fc9SVladimir Oltean 
18120b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
18130b912fc9SVladimir Oltean }
18140b912fc9SVladimir Oltean 
felix_cls_flower_add(struct dsa_switch * ds,int port,struct flow_cls_offload * cls,bool ingress)181507d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
181607d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
181707d985eeSVladimir Oltean {
181807d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
181999348004SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
182099348004SVladimir Oltean 	bool using_tag_8021q;
182199348004SVladimir Oltean 	int err;
182207d985eeSVladimir Oltean 
182399348004SVladimir Oltean 	err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
182499348004SVladimir Oltean 	if (err)
182599348004SVladimir Oltean 		return err;
182699348004SVladimir Oltean 
182799348004SVladimir Oltean 	using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
182899348004SVladimir Oltean 
182999348004SVladimir Oltean 	return felix_update_trapping_destinations(ds, using_tag_8021q);
183007d985eeSVladimir Oltean }
183107d985eeSVladimir Oltean 
felix_cls_flower_del(struct dsa_switch * ds,int port,struct flow_cls_offload * cls,bool ingress)183207d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
183307d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
183407d985eeSVladimir Oltean {
183507d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
183607d985eeSVladimir Oltean 
183707d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
183807d985eeSVladimir Oltean }
183907d985eeSVladimir Oltean 
felix_cls_flower_stats(struct dsa_switch * ds,int port,struct flow_cls_offload * cls,bool ingress)184007d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
184107d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
184207d985eeSVladimir Oltean {
184307d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
184407d985eeSVladimir Oltean 
184507d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
184607d985eeSVladimir Oltean }
184707d985eeSVladimir Oltean 
felix_port_policer_add(struct dsa_switch * ds,int port,struct dsa_mall_policer_tc_entry * policer)1848fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
1849fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
1850fc411eaaSVladimir Oltean {
1851fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1852fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
1853fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
18545f035af7SPo Liu 		.burst = policer->burst,
1855fc411eaaSVladimir Oltean 	};
1856fc411eaaSVladimir Oltean 
1857fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
1858fc411eaaSVladimir Oltean }
1859fc411eaaSVladimir Oltean 
felix_port_policer_del(struct dsa_switch * ds,int port)1860fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
1861fc411eaaSVladimir Oltean {
1862fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1863fc411eaaSVladimir Oltean 
1864fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
1865fc411eaaSVladimir Oltean }
1866fc411eaaSVladimir Oltean 
felix_port_mirror_add(struct dsa_switch * ds,int port,struct dsa_mall_mirror_tc_entry * mirror,bool ingress,struct netlink_ext_ack * extack)18675e497497SVladimir Oltean static int felix_port_mirror_add(struct dsa_switch *ds, int port,
18685e497497SVladimir Oltean 				 struct dsa_mall_mirror_tc_entry *mirror,
18695e497497SVladimir Oltean 				 bool ingress, struct netlink_ext_ack *extack)
18705e497497SVladimir Oltean {
18715e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
18725e497497SVladimir Oltean 
18735e497497SVladimir Oltean 	return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
18745e497497SVladimir Oltean 				      ingress, extack);
18755e497497SVladimir Oltean }
18765e497497SVladimir Oltean 
felix_port_mirror_del(struct dsa_switch * ds,int port,struct dsa_mall_mirror_tc_entry * mirror)18775e497497SVladimir Oltean static void felix_port_mirror_del(struct dsa_switch *ds, int port,
18785e497497SVladimir Oltean 				  struct dsa_mall_mirror_tc_entry *mirror)
18795e497497SVladimir Oltean {
18805e497497SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
18815e497497SVladimir Oltean 
18825e497497SVladimir Oltean 	ocelot_port_mirror_del(ocelot, port, mirror->ingress);
18835e497497SVladimir Oltean }
18845e497497SVladimir Oltean 
felix_port_setup_tc(struct dsa_switch * ds,int port,enum tc_setup_type type,void * type_data)1885de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
1886de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
1887de143c0eSXiaoliang Yang 			       void *type_data)
1888de143c0eSXiaoliang Yang {
1889de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
1890de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
1891de143c0eSXiaoliang Yang 
1892de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
1893de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
1894de143c0eSXiaoliang Yang 	else
1895de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
1896de143c0eSXiaoliang Yang }
1897de143c0eSXiaoliang Yang 
felix_sb_pool_get(struct dsa_switch * ds,unsigned int sb_index,u16 pool_index,struct devlink_sb_pool_info * pool_info)1898f59fd9caSVladimir Oltean static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
1899f59fd9caSVladimir Oltean 			     u16 pool_index,
1900f59fd9caSVladimir Oltean 			     struct devlink_sb_pool_info *pool_info)
1901f59fd9caSVladimir Oltean {
1902f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1903f59fd9caSVladimir Oltean 
1904f59fd9caSVladimir Oltean 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
1905f59fd9caSVladimir Oltean }
1906f59fd9caSVladimir Oltean 
felix_sb_pool_set(struct dsa_switch * ds,unsigned int sb_index,u16 pool_index,u32 size,enum devlink_sb_threshold_type threshold_type,struct netlink_ext_ack * extack)1907f59fd9caSVladimir Oltean static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
1908f59fd9caSVladimir Oltean 			     u16 pool_index, u32 size,
1909f59fd9caSVladimir Oltean 			     enum devlink_sb_threshold_type threshold_type,
1910f59fd9caSVladimir Oltean 			     struct netlink_ext_ack *extack)
1911f59fd9caSVladimir Oltean {
1912f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1913f59fd9caSVladimir Oltean 
1914f59fd9caSVladimir Oltean 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
1915f59fd9caSVladimir Oltean 				  threshold_type, extack);
1916f59fd9caSVladimir Oltean }
1917f59fd9caSVladimir Oltean 
felix_sb_port_pool_get(struct dsa_switch * ds,int port,unsigned int sb_index,u16 pool_index,u32 * p_threshold)1918f59fd9caSVladimir Oltean static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
1919f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1920f59fd9caSVladimir Oltean 				  u32 *p_threshold)
1921f59fd9caSVladimir Oltean {
1922f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1923f59fd9caSVladimir Oltean 
1924f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
1925f59fd9caSVladimir Oltean 				       p_threshold);
1926f59fd9caSVladimir Oltean }
1927f59fd9caSVladimir Oltean 
felix_sb_port_pool_set(struct dsa_switch * ds,int port,unsigned int sb_index,u16 pool_index,u32 threshold,struct netlink_ext_ack * extack)1928f59fd9caSVladimir Oltean static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
1929f59fd9caSVladimir Oltean 				  unsigned int sb_index, u16 pool_index,
1930f59fd9caSVladimir Oltean 				  u32 threshold, struct netlink_ext_ack *extack)
1931f59fd9caSVladimir Oltean {
1932f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1933f59fd9caSVladimir Oltean 
1934f59fd9caSVladimir Oltean 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
1935f59fd9caSVladimir Oltean 				       threshold, extack);
1936f59fd9caSVladimir Oltean }
1937f59fd9caSVladimir Oltean 
felix_sb_tc_pool_bind_get(struct dsa_switch * ds,int port,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u16 * p_pool_index,u32 * p_threshold)1938f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
1939f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1940f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1941f59fd9caSVladimir Oltean 				     u16 *p_pool_index, u32 *p_threshold)
1942f59fd9caSVladimir Oltean {
1943f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1944f59fd9caSVladimir Oltean 
1945f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
1946f59fd9caSVladimir Oltean 					  pool_type, p_pool_index,
1947f59fd9caSVladimir Oltean 					  p_threshold);
1948f59fd9caSVladimir Oltean }
1949f59fd9caSVladimir Oltean 
felix_sb_tc_pool_bind_set(struct dsa_switch * ds,int port,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u16 pool_index,u32 threshold,struct netlink_ext_ack * extack)1950f59fd9caSVladimir Oltean static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
1951f59fd9caSVladimir Oltean 				     unsigned int sb_index, u16 tc_index,
1952f59fd9caSVladimir Oltean 				     enum devlink_sb_pool_type pool_type,
1953f59fd9caSVladimir Oltean 				     u16 pool_index, u32 threshold,
1954f59fd9caSVladimir Oltean 				     struct netlink_ext_ack *extack)
1955f59fd9caSVladimir Oltean {
1956f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1957f59fd9caSVladimir Oltean 
1958f59fd9caSVladimir Oltean 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
1959f59fd9caSVladimir Oltean 					  pool_type, pool_index, threshold,
1960f59fd9caSVladimir Oltean 					  extack);
1961f59fd9caSVladimir Oltean }
1962f59fd9caSVladimir Oltean 
felix_sb_occ_snapshot(struct dsa_switch * ds,unsigned int sb_index)1963f59fd9caSVladimir Oltean static int felix_sb_occ_snapshot(struct dsa_switch *ds,
1964f59fd9caSVladimir Oltean 				 unsigned int sb_index)
1965f59fd9caSVladimir Oltean {
1966f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1967f59fd9caSVladimir Oltean 
1968f59fd9caSVladimir Oltean 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
1969f59fd9caSVladimir Oltean }
1970f59fd9caSVladimir Oltean 
felix_sb_occ_max_clear(struct dsa_switch * ds,unsigned int sb_index)1971f59fd9caSVladimir Oltean static int felix_sb_occ_max_clear(struct dsa_switch *ds,
1972f59fd9caSVladimir Oltean 				  unsigned int sb_index)
1973f59fd9caSVladimir Oltean {
1974f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1975f59fd9caSVladimir Oltean 
1976f59fd9caSVladimir Oltean 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
1977f59fd9caSVladimir Oltean }
1978f59fd9caSVladimir Oltean 
felix_sb_occ_port_pool_get(struct dsa_switch * ds,int port,unsigned int sb_index,u16 pool_index,u32 * p_cur,u32 * p_max)1979f59fd9caSVladimir Oltean static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
1980f59fd9caSVladimir Oltean 				      unsigned int sb_index, u16 pool_index,
1981f59fd9caSVladimir Oltean 				      u32 *p_cur, u32 *p_max)
1982f59fd9caSVladimir Oltean {
1983f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1984f59fd9caSVladimir Oltean 
1985f59fd9caSVladimir Oltean 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
1986f59fd9caSVladimir Oltean 					   p_cur, p_max);
1987f59fd9caSVladimir Oltean }
1988f59fd9caSVladimir Oltean 
felix_sb_occ_tc_port_bind_get(struct dsa_switch * ds,int port,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u32 * p_cur,u32 * p_max)1989f59fd9caSVladimir Oltean static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
1990f59fd9caSVladimir Oltean 					 unsigned int sb_index, u16 tc_index,
1991f59fd9caSVladimir Oltean 					 enum devlink_sb_pool_type pool_type,
1992f59fd9caSVladimir Oltean 					 u32 *p_cur, u32 *p_max)
1993f59fd9caSVladimir Oltean {
1994f59fd9caSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
1995f59fd9caSVladimir Oltean 
1996f59fd9caSVladimir Oltean 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
1997f59fd9caSVladimir Oltean 					      pool_type, p_cur, p_max);
1998f59fd9caSVladimir Oltean }
1999f59fd9caSVladimir Oltean 
felix_mrp_add(struct dsa_switch * ds,int port,const struct switchdev_obj_mrp * mrp)2000a026c50bSHoratiu Vultur static int felix_mrp_add(struct dsa_switch *ds, int port,
2001a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
2002a026c50bSHoratiu Vultur {
2003a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
2004a026c50bSHoratiu Vultur 
2005a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
2006a026c50bSHoratiu Vultur }
2007a026c50bSHoratiu Vultur 
felix_mrp_del(struct dsa_switch * ds,int port,const struct switchdev_obj_mrp * mrp)2008a026c50bSHoratiu Vultur static int felix_mrp_del(struct dsa_switch *ds, int port,
2009a026c50bSHoratiu Vultur 			 const struct switchdev_obj_mrp *mrp)
2010a026c50bSHoratiu Vultur {
2011a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
2012a026c50bSHoratiu Vultur 
2013a026c50bSHoratiu Vultur 	return ocelot_mrp_add(ocelot, port, mrp);
2014a026c50bSHoratiu Vultur }
2015a026c50bSHoratiu Vultur 
2016a026c50bSHoratiu Vultur static int
felix_mrp_add_ring_role(struct dsa_switch * ds,int port,const struct switchdev_obj_ring_role_mrp * mrp)2017a026c50bSHoratiu Vultur felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
2018a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
2019a026c50bSHoratiu Vultur {
2020a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
2021a026c50bSHoratiu Vultur 
2022a026c50bSHoratiu Vultur 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
2023a026c50bSHoratiu Vultur }
2024a026c50bSHoratiu Vultur 
2025a026c50bSHoratiu Vultur static int
felix_mrp_del_ring_role(struct dsa_switch * ds,int port,const struct switchdev_obj_ring_role_mrp * mrp)2026a026c50bSHoratiu Vultur felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
2027a026c50bSHoratiu Vultur 			const struct switchdev_obj_ring_role_mrp *mrp)
2028a026c50bSHoratiu Vultur {
2029a026c50bSHoratiu Vultur 	struct ocelot *ocelot = ds->priv;
2030a026c50bSHoratiu Vultur 
2031a026c50bSHoratiu Vultur 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
2032a026c50bSHoratiu Vultur }
2033a026c50bSHoratiu Vultur 
felix_port_get_default_prio(struct dsa_switch * ds,int port)2034978777d0SVladimir Oltean static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
2035978777d0SVladimir Oltean {
2036978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
2037978777d0SVladimir Oltean 
2038978777d0SVladimir Oltean 	return ocelot_port_get_default_prio(ocelot, port);
2039978777d0SVladimir Oltean }
2040978777d0SVladimir Oltean 
felix_port_set_default_prio(struct dsa_switch * ds,int port,u8 prio)2041978777d0SVladimir Oltean static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
2042978777d0SVladimir Oltean 				       u8 prio)
2043978777d0SVladimir Oltean {
2044978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
2045978777d0SVladimir Oltean 
2046978777d0SVladimir Oltean 	return ocelot_port_set_default_prio(ocelot, port, prio);
2047978777d0SVladimir Oltean }
2048978777d0SVladimir Oltean 
felix_port_get_dscp_prio(struct dsa_switch * ds,int port,u8 dscp)2049978777d0SVladimir Oltean static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
2050978777d0SVladimir Oltean {
2051978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
2052978777d0SVladimir Oltean 
2053978777d0SVladimir Oltean 	return ocelot_port_get_dscp_prio(ocelot, port, dscp);
2054978777d0SVladimir Oltean }
2055978777d0SVladimir Oltean 
felix_port_add_dscp_prio(struct dsa_switch * ds,int port,u8 dscp,u8 prio)2056978777d0SVladimir Oltean static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
2057978777d0SVladimir Oltean 				    u8 prio)
2058978777d0SVladimir Oltean {
2059978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
2060978777d0SVladimir Oltean 
2061978777d0SVladimir Oltean 	return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
2062978777d0SVladimir Oltean }
2063978777d0SVladimir Oltean 
felix_port_del_dscp_prio(struct dsa_switch * ds,int port,u8 dscp,u8 prio)2064978777d0SVladimir Oltean static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
2065978777d0SVladimir Oltean 				    u8 prio)
2066978777d0SVladimir Oltean {
2067978777d0SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
2068978777d0SVladimir Oltean 
2069978777d0SVladimir Oltean 	return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
2070978777d0SVladimir Oltean }
2071978777d0SVladimir Oltean 
felix_get_mm(struct dsa_switch * ds,int port,struct ethtool_mm_state * state)20726505b680SVladimir Oltean static int felix_get_mm(struct dsa_switch *ds, int port,
20736505b680SVladimir Oltean 			struct ethtool_mm_state *state)
20746505b680SVladimir Oltean {
20756505b680SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
20766505b680SVladimir Oltean 
20776505b680SVladimir Oltean 	return ocelot_port_get_mm(ocelot, port, state);
20786505b680SVladimir Oltean }
20796505b680SVladimir Oltean 
felix_set_mm(struct dsa_switch * ds,int port,struct ethtool_mm_cfg * cfg,struct netlink_ext_ack * extack)20806505b680SVladimir Oltean static int felix_set_mm(struct dsa_switch *ds, int port,
20816505b680SVladimir Oltean 			struct ethtool_mm_cfg *cfg,
20826505b680SVladimir Oltean 			struct netlink_ext_ack *extack)
20836505b680SVladimir Oltean {
20846505b680SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
20856505b680SVladimir Oltean 
20866505b680SVladimir Oltean 	return ocelot_port_set_mm(ocelot, port, cfg, extack);
20876505b680SVladimir Oltean }
20886505b680SVladimir Oltean 
felix_get_mm_stats(struct dsa_switch * ds,int port,struct ethtool_mm_stats * stats)2089ab3f97a9SVladimir Oltean static void felix_get_mm_stats(struct dsa_switch *ds, int port,
2090ab3f97a9SVladimir Oltean 			       struct ethtool_mm_stats *stats)
2091ab3f97a9SVladimir Oltean {
2092ab3f97a9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
2093ab3f97a9SVladimir Oltean 
2094ab3f97a9SVladimir Oltean 	ocelot_port_get_mm_stats(ocelot, port, stats);
2095ab3f97a9SVladimir Oltean }
2096ab3f97a9SVladimir Oltean 
2097375e1314SVladimir Oltean const struct dsa_switch_ops felix_switch_ops = {
209856051948SVladimir Oltean 	.get_tag_protocol		= felix_get_tag_protocol,
2099adb3dccfSVladimir Oltean 	.change_tag_protocol		= felix_change_tag_protocol,
210035d97680SVladimir Oltean 	.connect_tag_protocol		= felix_connect_tag_protocol,
210156051948SVladimir Oltean 	.setup				= felix_setup,
210256051948SVladimir Oltean 	.teardown			= felix_teardown,
210356051948SVladimir Oltean 	.set_ageing_time		= felix_set_ageing_time,
21046505b680SVladimir Oltean 	.get_mm				= felix_get_mm,
21056505b680SVladimir Oltean 	.set_mm				= felix_set_mm,
2106ab3f97a9SVladimir Oltean 	.get_mm_stats			= felix_get_mm_stats,
2107776b71e5SVladimir Oltean 	.get_stats64			= felix_get_stats64,
2108e32036e1SVladimir Oltean 	.get_pause_stats		= felix_get_pause_stats,
2109e32036e1SVladimir Oltean 	.get_rmon_stats			= felix_get_rmon_stats,
2110e32036e1SVladimir Oltean 	.get_eth_ctrl_stats		= felix_get_eth_ctrl_stats,
2111e32036e1SVladimir Oltean 	.get_eth_mac_stats		= felix_get_eth_mac_stats,
2112e32036e1SVladimir Oltean 	.get_eth_phy_stats		= felix_get_eth_phy_stats,
211356051948SVladimir Oltean 	.get_strings			= felix_get_strings,
211456051948SVladimir Oltean 	.get_ethtool_stats		= felix_get_ethtool_stats,
211556051948SVladimir Oltean 	.get_sset_count			= felix_get_sset_count,
211656051948SVladimir Oltean 	.get_ts_info			= felix_get_ts_info,
211779fda660SRussell King (Oracle) 	.phylink_get_caps		= felix_phylink_get_caps,
2118544435c9SColin Foster 	.phylink_mac_config		= felix_phylink_mac_config,
2119864ba485SRussell King (Oracle) 	.phylink_mac_select_pcs		= felix_phylink_mac_select_pcs,
2120bdeced75SVladimir Oltean 	.phylink_mac_link_down		= felix_phylink_mac_link_down,
2121bdeced75SVladimir Oltean 	.phylink_mac_link_up		= felix_phylink_mac_link_up,
2122eca70102SVladimir Oltean 	.port_enable			= felix_port_enable,
21235cad43a5SVladimir Oltean 	.port_fast_age			= felix_port_fast_age,
212456051948SVladimir Oltean 	.port_fdb_dump			= felix_fdb_dump,
212556051948SVladimir Oltean 	.port_fdb_add			= felix_fdb_add,
212656051948SVladimir Oltean 	.port_fdb_del			= felix_fdb_del,
2127961d8b69SVladimir Oltean 	.lag_fdb_add			= felix_lag_fdb_add,
2128961d8b69SVladimir Oltean 	.lag_fdb_del			= felix_lag_fdb_del,
2129209edf95SVladimir Oltean 	.port_mdb_add			= felix_mdb_add,
2130209edf95SVladimir Oltean 	.port_mdb_del			= felix_mdb_del,
2131421741eaSVladimir Oltean 	.port_pre_bridge_flags		= felix_pre_bridge_flags,
2132421741eaSVladimir Oltean 	.port_bridge_flags		= felix_bridge_flags,
213356051948SVladimir Oltean 	.port_bridge_join		= felix_bridge_join,
213456051948SVladimir Oltean 	.port_bridge_leave		= felix_bridge_leave,
21358fe6832eSVladimir Oltean 	.port_lag_join			= felix_lag_join,
21368fe6832eSVladimir Oltean 	.port_lag_leave			= felix_lag_leave,
21378fe6832eSVladimir Oltean 	.port_lag_change		= felix_lag_change,
213856051948SVladimir Oltean 	.port_stp_state_set		= felix_bridge_stp_state_set,
213956051948SVladimir Oltean 	.port_vlan_filtering		= felix_vlan_filtering,
214056051948SVladimir Oltean 	.port_vlan_add			= felix_vlan_add,
214156051948SVladimir Oltean 	.port_vlan_del			= felix_vlan_del,
2142c0bcf537SYangbo Lu 	.port_hwtstamp_get		= felix_hwtstamp_get,
2143c0bcf537SYangbo Lu 	.port_hwtstamp_set		= felix_hwtstamp_set,
2144c0bcf537SYangbo Lu 	.port_rxtstamp			= felix_rxtstamp,
2145c0bcf537SYangbo Lu 	.port_txtstamp			= felix_txtstamp,
21460b912fc9SVladimir Oltean 	.port_change_mtu		= felix_change_mtu,
21470b912fc9SVladimir Oltean 	.port_max_mtu			= felix_get_max_mtu,
2148fc411eaaSVladimir Oltean 	.port_policer_add		= felix_port_policer_add,
2149fc411eaaSVladimir Oltean 	.port_policer_del		= felix_port_policer_del,
21505e497497SVladimir Oltean 	.port_mirror_add		= felix_port_mirror_add,
21515e497497SVladimir Oltean 	.port_mirror_del		= felix_port_mirror_del,
215207d985eeSVladimir Oltean 	.cls_flower_add			= felix_cls_flower_add,
215307d985eeSVladimir Oltean 	.cls_flower_del			= felix_cls_flower_del,
215407d985eeSVladimir Oltean 	.cls_flower_stats		= felix_cls_flower_stats,
2155de143c0eSXiaoliang Yang 	.port_setup_tc			= felix_port_setup_tc,
2156f59fd9caSVladimir Oltean 	.devlink_sb_pool_get		= felix_sb_pool_get,
2157f59fd9caSVladimir Oltean 	.devlink_sb_pool_set		= felix_sb_pool_set,
2158f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_get	= felix_sb_port_pool_get,
2159f59fd9caSVladimir Oltean 	.devlink_sb_port_pool_set	= felix_sb_port_pool_set,
2160f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_get	= felix_sb_tc_pool_bind_get,
2161f59fd9caSVladimir Oltean 	.devlink_sb_tc_pool_bind_set	= felix_sb_tc_pool_bind_set,
2162f59fd9caSVladimir Oltean 	.devlink_sb_occ_snapshot	= felix_sb_occ_snapshot,
2163f59fd9caSVladimir Oltean 	.devlink_sb_occ_max_clear	= felix_sb_occ_max_clear,
2164f59fd9caSVladimir Oltean 	.devlink_sb_occ_port_pool_get	= felix_sb_occ_port_pool_get,
2165f59fd9caSVladimir Oltean 	.devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
2166a026c50bSHoratiu Vultur 	.port_mrp_add			= felix_mrp_add,
2167a026c50bSHoratiu Vultur 	.port_mrp_del			= felix_mrp_del,
2168a026c50bSHoratiu Vultur 	.port_mrp_add_ring_role		= felix_mrp_add_ring_role,
2169a026c50bSHoratiu Vultur 	.port_mrp_del_ring_role		= felix_mrp_del_ring_role,
21705da11eb4SVladimir Oltean 	.tag_8021q_vlan_add		= felix_tag_8021q_vlan_add,
21715da11eb4SVladimir Oltean 	.tag_8021q_vlan_del		= felix_tag_8021q_vlan_del,
2172978777d0SVladimir Oltean 	.port_get_default_prio		= felix_port_get_default_prio,
2173978777d0SVladimir Oltean 	.port_set_default_prio		= felix_port_set_default_prio,
2174978777d0SVladimir Oltean 	.port_get_dscp_prio		= felix_port_get_dscp_prio,
2175978777d0SVladimir Oltean 	.port_add_dscp_prio		= felix_port_add_dscp_prio,
2176978777d0SVladimir Oltean 	.port_del_dscp_prio		= felix_port_del_dscp_prio,
217772c3b0c7SVladimir Oltean 	.port_set_host_flood		= felix_port_set_host_flood,
2178eca70102SVladimir Oltean 	.port_change_master		= felix_port_change_master,
217956051948SVladimir Oltean };
2180c8005511SVladimir Oltean EXPORT_SYMBOL_GPL(felix_switch_ops);
2181319e4dd1SVladimir Oltean 
felix_port_to_netdev(struct ocelot * ocelot,int port)2182319e4dd1SVladimir Oltean struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
2183319e4dd1SVladimir Oltean {
2184319e4dd1SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
2185319e4dd1SVladimir Oltean 	struct dsa_switch *ds = felix->ds;
2186319e4dd1SVladimir Oltean 
2187319e4dd1SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2188319e4dd1SVladimir Oltean 		return NULL;
2189319e4dd1SVladimir Oltean 
2190319e4dd1SVladimir Oltean 	return dsa_to_port(ds, port)->slave;
2191319e4dd1SVladimir Oltean }
2192c8005511SVladimir Oltean EXPORT_SYMBOL_GPL(felix_port_to_netdev);
2193319e4dd1SVladimir Oltean 
felix_netdev_to_port(struct net_device * dev)2194319e4dd1SVladimir Oltean int felix_netdev_to_port(struct net_device *dev)
2195319e4dd1SVladimir Oltean {
2196319e4dd1SVladimir Oltean 	struct dsa_port *dp;
2197319e4dd1SVladimir Oltean 
2198319e4dd1SVladimir Oltean 	dp = dsa_port_from_netdev(dev);
2199319e4dd1SVladimir Oltean 	if (IS_ERR(dp))
2200319e4dd1SVladimir Oltean 		return -EINVAL;
2201319e4dd1SVladimir Oltean 
2202319e4dd1SVladimir Oltean 	return dp->index;
2203319e4dd1SVladimir Oltean }
2204c8005511SVladimir Oltean EXPORT_SYMBOL_GPL(felix_netdev_to_port);
2205c8005511SVladimir Oltean 
2206c8005511SVladimir Oltean MODULE_DESCRIPTION("Felix DSA library");
2207c8005511SVladimir Oltean MODULE_LICENSE("GPL");
2208