xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 0da1a1c4891188f456c7790940e47c8043bc7c9b)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
8a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
939e5308bSYangbo Lu #include <linux/ptp_classify.h>
1020968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
11a556c76aSAlexandre Belloni #include "ocelot.h"
123c83654fSVladimir Oltean #include "ocelot_vcap.h"
13a556c76aSAlexandre Belloni 
14639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
15639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
16639c1b26SSteen Hegelund 
17a556c76aSAlexandre Belloni struct ocelot_mact_entry {
18a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
19a556c76aSAlexandre Belloni 	u16 vid;
20a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
21a556c76aSAlexandre Belloni };
22a556c76aSAlexandre Belloni 
23639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
24639c1b26SSteen Hegelund {
25639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
26639c1b26SSteen Hegelund }
27639c1b26SSteen Hegelund 
28a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
29a556c76aSAlexandre Belloni {
30639c1b26SSteen Hegelund 	u32 val;
31a556c76aSAlexandre Belloni 
32639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
33639c1b26SSteen Hegelund 		ocelot, val,
34639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
35639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
36639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
37a556c76aSAlexandre Belloni }
38a556c76aSAlexandre Belloni 
39a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
40a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
41a556c76aSAlexandre Belloni 			       unsigned int vid)
42a556c76aSAlexandre Belloni {
43a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
44a556c76aSAlexandre Belloni 
45a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
46a556c76aSAlexandre Belloni 	 * understood by the hardware.
47a556c76aSAlexandre Belloni 	 */
48a556c76aSAlexandre Belloni 	mach |= vid    << 16;
49a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
50a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
51a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
52a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
53a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
54a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
55a556c76aSAlexandre Belloni 
56a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
57a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
58a556c76aSAlexandre Belloni 
59a556c76aSAlexandre Belloni }
60a556c76aSAlexandre Belloni 
619c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
62a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
639c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
64a556c76aSAlexandre Belloni {
65584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
66584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
67584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
68584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
69584b7cfcSAlban Bedel 	unsigned int mc_ports;
70584b7cfcSAlban Bedel 
71584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
72584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
73584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
74584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
75584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
76584b7cfcSAlban Bedel 	else
77584b7cfcSAlban Bedel 		mc_ports = 0;
78584b7cfcSAlban Bedel 
79584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
80584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
81584b7cfcSAlban Bedel 
82a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
83a556c76aSAlexandre Belloni 
84a556c76aSAlexandre Belloni 	/* Issue a write command */
85584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
86a556c76aSAlexandre Belloni 
87a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
88a556c76aSAlexandre Belloni }
899c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
90a556c76aSAlexandre Belloni 
919c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
929c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
93a556c76aSAlexandre Belloni {
94a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
95a556c76aSAlexandre Belloni 
96a556c76aSAlexandre Belloni 	/* Issue a forget command */
97a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
98a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
99a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
100a556c76aSAlexandre Belloni 
101a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
102a556c76aSAlexandre Belloni }
1039c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
104a556c76aSAlexandre Belloni 
105a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
106a556c76aSAlexandre Belloni {
107a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
108a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
109a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
110a556c76aSAlexandre Belloni 	 */
111a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
112a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
113a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
114a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
115a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
116a556c76aSAlexandre Belloni 
117a556c76aSAlexandre Belloni 	/* Clear the MAC table */
118a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
119a556c76aSAlexandre Belloni }
120a556c76aSAlexandre Belloni 
121f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
122b5962294SHoratiu Vultur {
123b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
124b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
125f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
12675944fdaSXiaoliang Yang 
12775944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
12875944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1292f17c050SXiaoliang Yang 
1302f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1312f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1322f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
133b5962294SHoratiu Vultur }
134b5962294SHoratiu Vultur 
135639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
136639c1b26SSteen Hegelund {
137639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
138639c1b26SSteen Hegelund }
139639c1b26SSteen Hegelund 
140a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
141a556c76aSAlexandre Belloni {
142639c1b26SSteen Hegelund 	u32 val;
143a556c76aSAlexandre Belloni 
144639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
145639c1b26SSteen Hegelund 		ocelot,
146639c1b26SSteen Hegelund 		val,
147639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
148639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
149639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
150a556c76aSAlexandre Belloni }
151a556c76aSAlexandre Belloni 
1527142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1537142529fSAntoine Tenart {
1547142529fSAntoine Tenart 	/* Select the VID to configure */
1557142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1567142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1577142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1587142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1597142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1607142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1617142529fSAntoine Tenart 
1627142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1637142529fSAntoine Tenart }
1647142529fSAntoine Tenart 
165*0da1a1c4SVladimir Oltean static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
166*0da1a1c4SVladimir Oltean {
167*0da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
168*0da1a1c4SVladimir Oltean 	int num_untagged = 0;
169*0da1a1c4SVladimir Oltean 
170*0da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
171*0da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
172*0da1a1c4SVladimir Oltean 			continue;
173*0da1a1c4SVladimir Oltean 
174*0da1a1c4SVladimir Oltean 		if (vlan->untagged & BIT(port))
175*0da1a1c4SVladimir Oltean 			num_untagged++;
176*0da1a1c4SVladimir Oltean 	}
177*0da1a1c4SVladimir Oltean 
178*0da1a1c4SVladimir Oltean 	return num_untagged;
179*0da1a1c4SVladimir Oltean }
180*0da1a1c4SVladimir Oltean 
181*0da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
182*0da1a1c4SVladimir Oltean {
183*0da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
184*0da1a1c4SVladimir Oltean 	int num_tagged = 0;
185*0da1a1c4SVladimir Oltean 
186*0da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
187*0da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
188*0da1a1c4SVladimir Oltean 			continue;
189*0da1a1c4SVladimir Oltean 
190*0da1a1c4SVladimir Oltean 		if (!(vlan->untagged & BIT(port)))
191*0da1a1c4SVladimir Oltean 			num_tagged++;
192*0da1a1c4SVladimir Oltean 	}
193*0da1a1c4SVladimir Oltean 
194*0da1a1c4SVladimir Oltean 	return num_tagged;
195*0da1a1c4SVladimir Oltean }
196*0da1a1c4SVladimir Oltean 
197*0da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
198*0da1a1c4SVladimir Oltean  * _one_ egress-untagged VLAN (_the_ native VLAN)
199*0da1a1c4SVladimir Oltean  */
200*0da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
201*0da1a1c4SVladimir Oltean {
202*0da1a1c4SVladimir Oltean 	return ocelot_port_num_tagged_vlans(ocelot, port) &&
203*0da1a1c4SVladimir Oltean 	       ocelot_port_num_untagged_vlans(ocelot, port) == 1;
204*0da1a1c4SVladimir Oltean }
205*0da1a1c4SVladimir Oltean 
206*0da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan *
207*0da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
208*0da1a1c4SVladimir Oltean {
209*0da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
210*0da1a1c4SVladimir Oltean 
211*0da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
212*0da1a1c4SVladimir Oltean 		if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
213*0da1a1c4SVladimir Oltean 			return vlan;
214*0da1a1c4SVladimir Oltean 
215*0da1a1c4SVladimir Oltean 	return NULL;
216*0da1a1c4SVladimir Oltean }
217*0da1a1c4SVladimir Oltean 
218*0da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
219*0da1a1c4SVladimir Oltean  * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
220*0da1a1c4SVladimir Oltean  * state of the port.
221*0da1a1c4SVladimir Oltean  */
222*0da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
22397bb69e1SVladimir Oltean {
22497bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
22562a22bcbSVladimir Oltean 	enum ocelot_port_tag_config tag_cfg;
226*0da1a1c4SVladimir Oltean 	bool uses_native_vlan = false;
22797bb69e1SVladimir Oltean 
22887b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
229*0da1a1c4SVladimir Oltean 		uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
230*0da1a1c4SVladimir Oltean 
231*0da1a1c4SVladimir Oltean 		if (uses_native_vlan)
23262a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_NATIVE;
233*0da1a1c4SVladimir Oltean 		else if (ocelot_port_num_untagged_vlans(ocelot, port))
234*0da1a1c4SVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_DISABLED;
23587b0f983SVladimir Oltean 		else
23662a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_TRUNK;
23787b0f983SVladimir Oltean 	} else {
23862a22bcbSVladimir Oltean 		tag_cfg = OCELOT_PORT_TAG_DISABLED;
23987b0f983SVladimir Oltean 	}
240*0da1a1c4SVladimir Oltean 
24162a22bcbSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
24287b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
24387b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
244*0da1a1c4SVladimir Oltean 
245*0da1a1c4SVladimir Oltean 	if (uses_native_vlan) {
246*0da1a1c4SVladimir Oltean 		struct ocelot_bridge_vlan *native_vlan;
247*0da1a1c4SVladimir Oltean 
248*0da1a1c4SVladimir Oltean 		/* Not having a native VLAN is impossible, because
249*0da1a1c4SVladimir Oltean 		 * ocelot_port_num_untagged_vlans has returned 1.
250*0da1a1c4SVladimir Oltean 		 * So there is no use in checking for NULL here.
251*0da1a1c4SVladimir Oltean 		 */
252*0da1a1c4SVladimir Oltean 		native_vlan = ocelot_port_find_native_vlan(ocelot, port);
253*0da1a1c4SVladimir Oltean 
254*0da1a1c4SVladimir Oltean 		ocelot_rmw_gix(ocelot,
255*0da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
256*0da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID_M,
257*0da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG, port);
258*0da1a1c4SVladimir Oltean 	}
25997bb69e1SVladimir Oltean }
26097bb69e1SVladimir Oltean 
26175e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
262c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
263c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
26475e5a554SVladimir Oltean {
26575e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
266be0576feSVladimir Oltean 	u32 val = 0;
26775e5a554SVladimir Oltean 
268c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
26975e5a554SVladimir Oltean 
27075e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
271c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
27275e5a554SVladimir Oltean 
27375e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
274c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
27575e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
27675e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
277be0576feSVladimir Oltean 
278be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
279be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
280be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
281be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
282be0576feSVladimir Oltean 	 */
283be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
284be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
285be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
286be0576feSVladimir Oltean 
287be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
288be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
289be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
290be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
29175e5a554SVladimir Oltean }
29275e5a554SVladimir Oltean 
29390e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
29490e0aa8dSVladimir Oltean 							  u16 vid)
295bbf6a2d9SVladimir Oltean {
29690e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
297bbf6a2d9SVladimir Oltean 
29890e0aa8dSVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
29990e0aa8dSVladimir Oltean 		if (vlan->vid == vid)
30090e0aa8dSVladimir Oltean 			return vlan;
301bbf6a2d9SVladimir Oltean 
30290e0aa8dSVladimir Oltean 	return NULL;
303bbf6a2d9SVladimir Oltean }
304bbf6a2d9SVladimir Oltean 
305*0da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
306*0da1a1c4SVladimir Oltean 				  bool untagged)
307bbf6a2d9SVladimir Oltean {
30890e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
30990e0aa8dSVladimir Oltean 	unsigned long portmask;
31090e0aa8dSVladimir Oltean 	int err;
31190e0aa8dSVladimir Oltean 
31290e0aa8dSVladimir Oltean 	if (vlan) {
31390e0aa8dSVladimir Oltean 		portmask = vlan->portmask | BIT(port);
31490e0aa8dSVladimir Oltean 
31590e0aa8dSVladimir Oltean 		err = ocelot_vlant_set_mask(ocelot, vid, portmask);
31690e0aa8dSVladimir Oltean 		if (err)
31790e0aa8dSVladimir Oltean 			return err;
31890e0aa8dSVladimir Oltean 
31990e0aa8dSVladimir Oltean 		vlan->portmask = portmask;
320*0da1a1c4SVladimir Oltean 		/* Bridge VLANs can be overwritten with a different
321*0da1a1c4SVladimir Oltean 		 * egress-tagging setting, so make sure to override an untagged
322*0da1a1c4SVladimir Oltean 		 * with a tagged VID if that's going on.
323*0da1a1c4SVladimir Oltean 		 */
324*0da1a1c4SVladimir Oltean 		if (untagged)
325*0da1a1c4SVladimir Oltean 			vlan->untagged |= BIT(port);
326*0da1a1c4SVladimir Oltean 		else
327*0da1a1c4SVladimir Oltean 			vlan->untagged &= ~BIT(port);
32890e0aa8dSVladimir Oltean 
32990e0aa8dSVladimir Oltean 		return 0;
33090e0aa8dSVladimir Oltean 	}
33190e0aa8dSVladimir Oltean 
33290e0aa8dSVladimir Oltean 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
33390e0aa8dSVladimir Oltean 	if (!vlan)
33490e0aa8dSVladimir Oltean 		return -ENOMEM;
33590e0aa8dSVladimir Oltean 
33690e0aa8dSVladimir Oltean 	portmask = BIT(port);
33790e0aa8dSVladimir Oltean 
33890e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
33990e0aa8dSVladimir Oltean 	if (err) {
34090e0aa8dSVladimir Oltean 		kfree(vlan);
34190e0aa8dSVladimir Oltean 		return err;
34290e0aa8dSVladimir Oltean 	}
34390e0aa8dSVladimir Oltean 
34490e0aa8dSVladimir Oltean 	vlan->vid = vid;
34590e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
346*0da1a1c4SVladimir Oltean 	if (untagged)
347*0da1a1c4SVladimir Oltean 		vlan->untagged = BIT(port);
34890e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&vlan->list);
34990e0aa8dSVladimir Oltean 	list_add_tail(&vlan->list, &ocelot->vlans);
35090e0aa8dSVladimir Oltean 
35190e0aa8dSVladimir Oltean 	return 0;
352bbf6a2d9SVladimir Oltean }
353bbf6a2d9SVladimir Oltean 
354bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
355bbf6a2d9SVladimir Oltean {
35690e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
35790e0aa8dSVladimir Oltean 	unsigned long portmask;
35890e0aa8dSVladimir Oltean 	int err;
35990e0aa8dSVladimir Oltean 
36090e0aa8dSVladimir Oltean 	if (!vlan)
36190e0aa8dSVladimir Oltean 		return 0;
36290e0aa8dSVladimir Oltean 
36390e0aa8dSVladimir Oltean 	portmask = vlan->portmask & ~BIT(port);
36490e0aa8dSVladimir Oltean 
36590e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
36690e0aa8dSVladimir Oltean 	if (err)
36790e0aa8dSVladimir Oltean 		return err;
36890e0aa8dSVladimir Oltean 
36990e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
37090e0aa8dSVladimir Oltean 	if (vlan->portmask)
37190e0aa8dSVladimir Oltean 		return 0;
37290e0aa8dSVladimir Oltean 
37390e0aa8dSVladimir Oltean 	list_del(&vlan->list);
37490e0aa8dSVladimir Oltean 	kfree(vlan);
37590e0aa8dSVladimir Oltean 
37690e0aa8dSVladimir Oltean 	return 0;
377bbf6a2d9SVladimir Oltean }
378bbf6a2d9SVladimir Oltean 
3792e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
3803b95d1b2SVladimir Oltean 			       bool vlan_aware, struct netlink_ext_ack *extack)
38187b0f983SVladimir Oltean {
38270edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
383bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
38470edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
385bae33f2bSVladimir Oltean 	u32 val;
38670edfae1SVladimir Oltean 
38770edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
38870edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
38970edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
3903b95d1b2SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
3913b95d1b2SVladimir Oltean 					   "Cannot change VLAN state with vlan modify rules active");
39270edfae1SVladimir Oltean 			return -EBUSY;
39370edfae1SVladimir Oltean 		}
39470edfae1SVladimir Oltean 	}
39570edfae1SVladimir Oltean 
39687b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
39787b0f983SVladimir Oltean 
39887b0f983SVladimir Oltean 	if (vlan_aware)
39987b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
40087b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
40187b0f983SVladimir Oltean 	else
40287b0f983SVladimir Oltean 		val = 0;
40387b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
40487b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
40587b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
40687b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
40787b0f983SVladimir Oltean 
408c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
409*0da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
4102e554a7aSVladimir Oltean 
4112e554a7aSVladimir Oltean 	return 0;
41287b0f983SVladimir Oltean }
41387b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
41487b0f983SVladimir Oltean 
4152f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
41601af940eSVladimir Oltean 			bool untagged, struct netlink_ext_ack *extack)
4172f0402feSVladimir Oltean {
418*0da1a1c4SVladimir Oltean 	if (untagged) {
419*0da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
420*0da1a1c4SVladimir Oltean 		if (ocelot_port_uses_native_vlan(ocelot, port)) {
42101af940eSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
422*0da1a1c4SVladimir Oltean 					   "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
4232f0402feSVladimir Oltean 			return -EBUSY;
4242f0402feSVladimir Oltean 		}
425*0da1a1c4SVladimir Oltean 	} else {
426*0da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
427*0da1a1c4SVladimir Oltean 		if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
428*0da1a1c4SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
429*0da1a1c4SVladimir Oltean 					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
430*0da1a1c4SVladimir Oltean 			return -EBUSY;
431*0da1a1c4SVladimir Oltean 		}
432*0da1a1c4SVladimir Oltean 	}
4332f0402feSVladimir Oltean 
4342f0402feSVladimir Oltean 	return 0;
4352f0402feSVladimir Oltean }
4362f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
4372f0402feSVladimir Oltean 
4385e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
4397142529fSAntoine Tenart 		    bool untagged)
4407142529fSAntoine Tenart {
441bbf6a2d9SVladimir Oltean 	int err;
4427142529fSAntoine Tenart 
443*0da1a1c4SVladimir Oltean 	err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
444bbf6a2d9SVladimir Oltean 	if (err)
445bbf6a2d9SVladimir Oltean 		return err;
4467142529fSAntoine Tenart 
4477142529fSAntoine Tenart 	/* Default ingress vlan classification */
448c3e58a75SVladimir Oltean 	if (pvid) {
449c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
450c3e58a75SVladimir Oltean 
451c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
452e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
453c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
454c3e58a75SVladimir Oltean 	}
4557142529fSAntoine Tenart 
4567142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
457*0da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
4587142529fSAntoine Tenart 
4597142529fSAntoine Tenart 	return 0;
4607142529fSAntoine Tenart }
4615e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
4627142529fSAntoine Tenart 
4635e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
4649855934cSVladimir Oltean {
4659855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
466bbf6a2d9SVladimir Oltean 	int err;
4677142529fSAntoine Tenart 
468bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
469bbf6a2d9SVladimir Oltean 	if (err)
470bbf6a2d9SVladimir Oltean 		return err;
4717142529fSAntoine Tenart 
472be0576feSVladimir Oltean 	/* Ingress */
473be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
474be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
475be0576feSVladimir Oltean 
476be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
477be0576feSVladimir Oltean 	}
478be0576feSVladimir Oltean 
4797142529fSAntoine Tenart 	/* Egress */
480*0da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
4817142529fSAntoine Tenart 
4827142529fSAntoine Tenart 	return 0;
4837142529fSAntoine Tenart }
4845e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
4857142529fSAntoine Tenart 
486a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
487a556c76aSAlexandre Belloni {
488bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
4897142529fSAntoine Tenart 	u16 port, vid;
4907142529fSAntoine Tenart 
491a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
492a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
493a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
494a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
4957142529fSAntoine Tenart 
4967142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
497bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
49890e0aa8dSVladimir Oltean 		ocelot_vlant_set_mask(ocelot, vid, 0);
4997142529fSAntoine Tenart 
5007142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
5017142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
5027142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
5037142529fSAntoine Tenart 	 */
50490e0aa8dSVladimir Oltean 	ocelot_vlant_set_mask(ocelot, 0, all_ports);
5057142529fSAntoine Tenart 
5067142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
5077142529fSAntoine Tenart 	 * default.
5087142529fSAntoine Tenart 	 */
509bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
5107142529fSAntoine Tenart 
5117142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
5127142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
5137142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
5147142529fSAntoine Tenart 	}
515a556c76aSAlexandre Belloni }
516a556c76aSAlexandre Belloni 
517eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
518eb4733d7SVladimir Oltean {
519eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
520eb4733d7SVladimir Oltean }
521eb4733d7SVladimir Oltean 
522e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
523eb4733d7SVladimir Oltean {
5241650bdb1SVladimir Oltean 	unsigned int pause_ena;
525eb4733d7SVladimir Oltean 	int err, val;
526eb4733d7SVladimir Oltean 
527eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
528eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
529eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
530eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
531eb4733d7SVladimir Oltean 
532eb4733d7SVladimir Oltean 	/* Disable flow control */
5331650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
534eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
535eb4733d7SVladimir Oltean 
536eb4733d7SVladimir Oltean 	/* Disable priority flow control */
537eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
538eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
539eb4733d7SVladimir Oltean 
540eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
541eb4733d7SVladimir Oltean 	 * at the port.
542eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
543eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
544eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
545eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
546eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
547eb4733d7SVladimir Oltean 	 */
548eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
549eb4733d7SVladimir Oltean 
550eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
551eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
552eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
553eb4733d7SVladimir Oltean 
554eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
555eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
556eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
557eb4733d7SVladimir Oltean 
558eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
559eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
560eb4733d7SVladimir Oltean 		       port);
561eb4733d7SVladimir Oltean 
562eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
563eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
564eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
565eb4733d7SVladimir Oltean 
566eb4733d7SVladimir Oltean 	/* Clear flushing again. */
567eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
568eb4733d7SVladimir Oltean 
5691650bdb1SVladimir Oltean 	/* Re-enable flow control */
5701650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
5711650bdb1SVladimir Oltean 
572eb4733d7SVladimir Oltean 	return err;
573eb4733d7SVladimir Oltean }
574eb4733d7SVladimir Oltean 
575e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
576e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
577e6e12df6SVladimir Oltean 				  phy_interface_t interface,
578e6e12df6SVladimir Oltean 				  unsigned long quirks)
579a556c76aSAlexandre Belloni {
58026f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
581e6e12df6SVladimir Oltean 	int err;
582a556c76aSAlexandre Belloni 
583e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
584e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
585e6e12df6SVladimir Oltean 
586e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
587e6e12df6SVladimir Oltean 
588e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
589e6e12df6SVladimir Oltean 	if (err)
590e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
591e6e12df6SVladimir Oltean 			port, err);
592e6e12df6SVladimir Oltean 
593e6e12df6SVladimir Oltean 	/* Put the port in reset. */
594e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
595e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
596e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
597e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
59874a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
599e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
60074a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
601e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
602e6e12df6SVladimir Oltean }
603e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
604e6e12df6SVladimir Oltean 
605e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
606e6e12df6SVladimir Oltean 				struct phy_device *phydev,
607e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
608e6e12df6SVladimir Oltean 				phy_interface_t interface,
609e6e12df6SVladimir Oltean 				int speed, int duplex,
610e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
611e6e12df6SVladimir Oltean 				unsigned long quirks)
612e6e12df6SVladimir Oltean {
613e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
614e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
615e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
616e6e12df6SVladimir Oltean 
617e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
618e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
619e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
620e6e12df6SVladimir Oltean 	 * (which is also its default value).
621e6e12df6SVladimir Oltean 	 */
622e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
623e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
624e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
625e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
626e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
627e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
628e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
629e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
630e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
631e6e12df6SVladimir Oltean 	} else {
632e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
633e6e12df6SVladimir Oltean 	}
634e6e12df6SVladimir Oltean 
635e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
636e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
637e6e12df6SVladimir Oltean 
638e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
639e6e12df6SVladimir Oltean 
640e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
641e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
642e6e12df6SVladimir Oltean 	 */
643e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
644e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
645e6e12df6SVladimir Oltean 
646e6e12df6SVladimir Oltean 	switch (speed) {
647a556c76aSAlexandre Belloni 	case SPEED_10:
648e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
649a556c76aSAlexandre Belloni 		break;
650a556c76aSAlexandre Belloni 	case SPEED_100:
651e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
652a556c76aSAlexandre Belloni 		break;
653a556c76aSAlexandre Belloni 	case SPEED_1000:
654a556c76aSAlexandre Belloni 	case SPEED_2500:
655e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
656a556c76aSAlexandre Belloni 		break;
657a556c76aSAlexandre Belloni 	default:
658e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
659e6e12df6SVladimir Oltean 			port, speed);
660a556c76aSAlexandre Belloni 		return;
661a556c76aSAlexandre Belloni 	}
662a556c76aSAlexandre Belloni 
663e6e12df6SVladimir Oltean 	/* Handle RX pause in all cases, with 2500base-X this is used for rate
664e6e12df6SVladimir Oltean 	 * adaptation.
665e6e12df6SVladimir Oltean 	 */
666e6e12df6SVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
667a556c76aSAlexandre Belloni 
668e6e12df6SVladimir Oltean 	if (tx_pause)
669e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
670e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
671e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
672e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
673a556c76aSAlexandre Belloni 
674e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
675e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
676e6e12df6SVladimir Oltean 	 */
677e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
678a556c76aSAlexandre Belloni 
679e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
6801ba8f656SVladimir Oltean 
681e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause);
6821ba8f656SVladimir Oltean 
683e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
684e6e12df6SVladimir Oltean 	 * enable MAC module
685e6e12df6SVladimir Oltean 	 */
686004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
687a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
688a556c76aSAlexandre Belloni 
689a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
690886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
691886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
692a556c76aSAlexandre Belloni }
693e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
694889b8950SVladimir Oltean 
69552849bcfSVladimir Oltean static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
696e2f9a8feSVladimir Oltean 					struct sk_buff *clone)
697400928bfSYangbo Lu {
698e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
69952849bcfSVladimir Oltean 	unsigned long flags;
700400928bfSYangbo Lu 
70152849bcfSVladimir Oltean 	spin_lock_irqsave(&ocelot->ts_id_lock, flags);
70252849bcfSVladimir Oltean 
70352849bcfSVladimir Oltean 	if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
70452849bcfSVladimir Oltean 	    ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
70552849bcfSVladimir Oltean 		spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
70652849bcfSVladimir Oltean 		return -EBUSY;
70752849bcfSVladimir Oltean 	}
7086565243cSVladimir Oltean 
709e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
710c4b364ceSYangbo Lu 	/* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
711c4b364ceSYangbo Lu 	OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
71252849bcfSVladimir Oltean 
713c57fe003SVladimir Oltean 	ocelot_port->ts_id++;
714c57fe003SVladimir Oltean 	if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
715c57fe003SVladimir Oltean 		ocelot_port->ts_id = 0;
71652849bcfSVladimir Oltean 
71752849bcfSVladimir Oltean 	ocelot_port->ptp_skbs_in_flight++;
71852849bcfSVladimir Oltean 	ocelot->ptp_skbs_in_flight++;
71952849bcfSVladimir Oltean 
720e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
7216565243cSVladimir Oltean 
72252849bcfSVladimir Oltean 	spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
72352849bcfSVladimir Oltean 
72452849bcfSVladimir Oltean 	return 0;
725400928bfSYangbo Lu }
726682eaad9SYangbo Lu 
727fba01283SVladimir Oltean static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
728fba01283SVladimir Oltean 				       unsigned int ptp_class)
72939e5308bSYangbo Lu {
73039e5308bSYangbo Lu 	struct ptp_header *hdr;
73139e5308bSYangbo Lu 	u8 msgtype, twostep;
73239e5308bSYangbo Lu 
73339e5308bSYangbo Lu 	hdr = ptp_parse_header(skb, ptp_class);
73439e5308bSYangbo Lu 	if (!hdr)
73539e5308bSYangbo Lu 		return false;
73639e5308bSYangbo Lu 
73739e5308bSYangbo Lu 	msgtype = ptp_get_msgtype(hdr, ptp_class);
73839e5308bSYangbo Lu 	twostep = hdr->flag_field[0] & 0x2;
73939e5308bSYangbo Lu 
74039e5308bSYangbo Lu 	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
74139e5308bSYangbo Lu 		return true;
74239e5308bSYangbo Lu 
74339e5308bSYangbo Lu 	return false;
74439e5308bSYangbo Lu }
74539e5308bSYangbo Lu 
746682eaad9SYangbo Lu int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
747682eaad9SYangbo Lu 				 struct sk_buff *skb,
748682eaad9SYangbo Lu 				 struct sk_buff **clone)
749682eaad9SYangbo Lu {
750682eaad9SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
751682eaad9SYangbo Lu 	u8 ptp_cmd = ocelot_port->ptp_cmd;
752fba01283SVladimir Oltean 	unsigned int ptp_class;
75352849bcfSVladimir Oltean 	int err;
754682eaad9SYangbo Lu 
755fba01283SVladimir Oltean 	/* Don't do anything if PTP timestamping not enabled */
756fba01283SVladimir Oltean 	if (!ptp_cmd)
757fba01283SVladimir Oltean 		return 0;
758fba01283SVladimir Oltean 
759fba01283SVladimir Oltean 	ptp_class = ptp_classify_raw(skb);
760fba01283SVladimir Oltean 	if (ptp_class == PTP_CLASS_NONE)
761fba01283SVladimir Oltean 		return -EINVAL;
762682eaad9SYangbo Lu 
76339e5308bSYangbo Lu 	/* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
76439e5308bSYangbo Lu 	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
765fba01283SVladimir Oltean 		if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
76639e5308bSYangbo Lu 			OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
76739e5308bSYangbo Lu 			return 0;
76839e5308bSYangbo Lu 		}
76939e5308bSYangbo Lu 
77039e5308bSYangbo Lu 		/* Fall back to two-step timestamping */
77139e5308bSYangbo Lu 		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
77239e5308bSYangbo Lu 	}
77339e5308bSYangbo Lu 
774682eaad9SYangbo Lu 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
775682eaad9SYangbo Lu 		*clone = skb_clone_sk(skb);
776682eaad9SYangbo Lu 		if (!(*clone))
777682eaad9SYangbo Lu 			return -ENOMEM;
778682eaad9SYangbo Lu 
77952849bcfSVladimir Oltean 		err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
78052849bcfSVladimir Oltean 		if (err)
78152849bcfSVladimir Oltean 			return err;
78252849bcfSVladimir Oltean 
78339e5308bSYangbo Lu 		OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
784ebb4c6a9SVladimir Oltean 		OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
785682eaad9SYangbo Lu 	}
786682eaad9SYangbo Lu 
787682eaad9SYangbo Lu 	return 0;
788682eaad9SYangbo Lu }
789682eaad9SYangbo Lu EXPORT_SYMBOL(ocelot_port_txtstamp_request);
790400928bfSYangbo Lu 
791e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
792e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
7934e3b0468SAntoine Tenart {
7944e3b0468SAntoine Tenart 	unsigned long flags;
7954e3b0468SAntoine Tenart 	u32 val;
7964e3b0468SAntoine Tenart 
7974e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
7984e3b0468SAntoine Tenart 
7994e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
8004e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
8014e3b0468SAntoine Tenart 
8024e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
8034e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
8044e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
8054e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
8064e3b0468SAntoine Tenart 
8074e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
8084e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
8094e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
8104e3b0468SAntoine Tenart 
8114e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
8124e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
8134e3b0468SAntoine Tenart 		ts->tv_sec--;
8144e3b0468SAntoine Tenart 
8154e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
8164e3b0468SAntoine Tenart }
817e23a7b3eSYangbo Lu 
818ebb4c6a9SVladimir Oltean static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
819ebb4c6a9SVladimir Oltean {
820ebb4c6a9SVladimir Oltean 	struct ptp_header *hdr;
821ebb4c6a9SVladimir Oltean 
822ebb4c6a9SVladimir Oltean 	hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
823ebb4c6a9SVladimir Oltean 	if (WARN_ON(!hdr))
824ebb4c6a9SVladimir Oltean 		return false;
825ebb4c6a9SVladimir Oltean 
826ebb4c6a9SVladimir Oltean 	return seqid == ntohs(hdr->sequence_id);
827ebb4c6a9SVladimir Oltean }
828ebb4c6a9SVladimir Oltean 
829e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
830e23a7b3eSYangbo Lu {
831e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
832e23a7b3eSYangbo Lu 
833e23a7b3eSYangbo Lu 	while (budget--) {
834b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
835e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
836ebb4c6a9SVladimir Oltean 		u32 val, id, seqid, txport;
837e23a7b3eSYangbo Lu 		struct ocelot_port *port;
838e23a7b3eSYangbo Lu 		struct timespec64 ts;
839b049da13SYangbo Lu 		unsigned long flags;
840e23a7b3eSYangbo Lu 
841e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
842e23a7b3eSYangbo Lu 
843e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
844e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
845e23a7b3eSYangbo Lu 			break;
846e23a7b3eSYangbo Lu 
847e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
848e23a7b3eSYangbo Lu 
849e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
850e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
851e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
852ebb4c6a9SVladimir Oltean 		seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
853e23a7b3eSYangbo Lu 
854e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
855e23a7b3eSYangbo Lu 
85652849bcfSVladimir Oltean 		spin_lock(&ocelot->ts_id_lock);
85752849bcfSVladimir Oltean 		port->ptp_skbs_in_flight--;
85852849bcfSVladimir Oltean 		ocelot->ptp_skbs_in_flight--;
85952849bcfSVladimir Oltean 		spin_unlock(&ocelot->ts_id_lock);
86052849bcfSVladimir Oltean 
86152849bcfSVladimir Oltean 		/* Retrieve its associated skb */
862ebb4c6a9SVladimir Oltean try_again:
863b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
864b049da13SYangbo Lu 
865b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
866c4b364ceSYangbo Lu 			if (OCELOT_SKB_CB(skb)->ts_id != id)
867e23a7b3eSYangbo Lu 				continue;
868b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
869b049da13SYangbo Lu 			skb_match = skb;
870fc62c094SYangbo Lu 			break;
871e23a7b3eSYangbo Lu 		}
872e23a7b3eSYangbo Lu 
873b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
874b049da13SYangbo Lu 
8759fde506eSVladimir Oltean 		if (WARN_ON(!skb_match))
8769fde506eSVladimir Oltean 			continue;
8779fde506eSVladimir Oltean 
878ebb4c6a9SVladimir Oltean 		if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
879ebb4c6a9SVladimir Oltean 			dev_err_ratelimited(ocelot->dev,
880ebb4c6a9SVladimir Oltean 					    "port %d received stale TX timestamp for seqid %d, discarding\n",
881ebb4c6a9SVladimir Oltean 					    txport, seqid);
882ebb4c6a9SVladimir Oltean 			dev_kfree_skb_any(skb);
883ebb4c6a9SVladimir Oltean 			goto try_again;
884ebb4c6a9SVladimir Oltean 		}
885ebb4c6a9SVladimir Oltean 
8865fd82200Slaurent brando 		/* Get the h/w timestamp */
8875fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
888e23a7b3eSYangbo Lu 
889e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
890e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
891e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
892e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
8935fd82200Slaurent brando 
8945fd82200Slaurent brando 		/* Next ts */
8955fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
896e23a7b3eSYangbo Lu 	}
897e23a7b3eSYangbo Lu }
898e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
8994e3b0468SAntoine Tenart 
900924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
901924ee317SVladimir Oltean 				u32 *rval)
902924ee317SVladimir Oltean {
903924ee317SVladimir Oltean 	u32 bytes_valid, val;
904924ee317SVladimir Oltean 
905924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
906924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
907924ee317SVladimir Oltean 		if (ifh)
908924ee317SVladimir Oltean 			return -EIO;
909924ee317SVladimir Oltean 
910924ee317SVladimir Oltean 		do {
911924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
912924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
913924ee317SVladimir Oltean 	}
914924ee317SVladimir Oltean 
915924ee317SVladimir Oltean 	switch (val) {
916924ee317SVladimir Oltean 	case XTR_ABORT:
917924ee317SVladimir Oltean 		return -EIO;
918924ee317SVladimir Oltean 	case XTR_EOF_0:
919924ee317SVladimir Oltean 	case XTR_EOF_1:
920924ee317SVladimir Oltean 	case XTR_EOF_2:
921924ee317SVladimir Oltean 	case XTR_EOF_3:
922924ee317SVladimir Oltean 	case XTR_PRUNED:
923924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
924924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
925924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
926924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
927924ee317SVladimir Oltean 		else
928924ee317SVladimir Oltean 			*rval = val;
929924ee317SVladimir Oltean 
930924ee317SVladimir Oltean 		return bytes_valid;
931924ee317SVladimir Oltean 	case XTR_ESCAPE:
932924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
933924ee317SVladimir Oltean 
934924ee317SVladimir Oltean 		return 4;
935924ee317SVladimir Oltean 	default:
936924ee317SVladimir Oltean 		*rval = val;
937924ee317SVladimir Oltean 
938924ee317SVladimir Oltean 		return 4;
939924ee317SVladimir Oltean 	}
940924ee317SVladimir Oltean }
941924ee317SVladimir Oltean 
942924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
943924ee317SVladimir Oltean {
944924ee317SVladimir Oltean 	int i, err = 0;
945924ee317SVladimir Oltean 
946924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
947924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
948924ee317SVladimir Oltean 		if (err != 4)
949924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
950924ee317SVladimir Oltean 	}
951924ee317SVladimir Oltean 
952924ee317SVladimir Oltean 	return 0;
953924ee317SVladimir Oltean }
954924ee317SVladimir Oltean 
955924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
956924ee317SVladimir Oltean {
957924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
9582ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
959924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
960924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
961924ee317SVladimir Oltean 	struct net_device *dev;
962924ee317SVladimir Oltean 	struct timespec64 ts;
963924ee317SVladimir Oltean 	struct sk_buff *skb;
964924ee317SVladimir Oltean 	int sz, buf_len;
965924ee317SVladimir Oltean 	u32 val, *buf;
966924ee317SVladimir Oltean 	int err;
967924ee317SVladimir Oltean 
968924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
969924ee317SVladimir Oltean 	if (err)
970924ee317SVladimir Oltean 		return err;
971924ee317SVladimir Oltean 
972924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
973924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
974924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
975924ee317SVladimir Oltean 
976924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
977924ee317SVladimir Oltean 		return -EINVAL;
978924ee317SVladimir Oltean 
979924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
980924ee317SVladimir Oltean 	if (!dev)
981924ee317SVladimir Oltean 		return -EINVAL;
982924ee317SVladimir Oltean 
983924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
984924ee317SVladimir Oltean 	if (unlikely(!skb)) {
985924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
986924ee317SVladimir Oltean 		return -ENOMEM;
987924ee317SVladimir Oltean 	}
988924ee317SVladimir Oltean 
989924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
990924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
991924ee317SVladimir Oltean 
992924ee317SVladimir Oltean 	len = 0;
993924ee317SVladimir Oltean 	do {
994924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
995924ee317SVladimir Oltean 		if (sz < 0) {
996924ee317SVladimir Oltean 			err = sz;
997924ee317SVladimir Oltean 			goto out_free_skb;
998924ee317SVladimir Oltean 		}
999924ee317SVladimir Oltean 		*buf++ = val;
1000924ee317SVladimir Oltean 		len += sz;
1001924ee317SVladimir Oltean 	} while (len < buf_len);
1002924ee317SVladimir Oltean 
1003924ee317SVladimir Oltean 	/* Read the FCS */
1004924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1005924ee317SVladimir Oltean 	if (sz < 0) {
1006924ee317SVladimir Oltean 		err = sz;
1007924ee317SVladimir Oltean 		goto out_free_skb;
1008924ee317SVladimir Oltean 	}
1009924ee317SVladimir Oltean 
1010924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1011924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1012924ee317SVladimir Oltean 
1013924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1014924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1015924ee317SVladimir Oltean 		*buf = val;
1016924ee317SVladimir Oltean 	}
1017924ee317SVladimir Oltean 
1018924ee317SVladimir Oltean 	if (ocelot->ptp) {
1019924ee317SVladimir Oltean 		ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1020924ee317SVladimir Oltean 
1021924ee317SVladimir Oltean 		tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1022924ee317SVladimir Oltean 		if ((tod_in_ns & 0xffffffff) < timestamp)
1023924ee317SVladimir Oltean 			full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1024924ee317SVladimir Oltean 					timestamp;
1025924ee317SVladimir Oltean 		else
1026924ee317SVladimir Oltean 			full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1027924ee317SVladimir Oltean 					timestamp;
1028924ee317SVladimir Oltean 
1029924ee317SVladimir Oltean 		shhwtstamps = skb_hwtstamps(skb);
1030924ee317SVladimir Oltean 		memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1031924ee317SVladimir Oltean 		shhwtstamps->hwtstamp = full_ts_in_ns;
1032924ee317SVladimir Oltean 	}
1033924ee317SVladimir Oltean 
1034924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1035924ee317SVladimir Oltean 	 * has already been forwarded.
1036924ee317SVladimir Oltean 	 */
1037df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1038924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1039924ee317SVladimir Oltean 
1040924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1041d8ea7ff3SHoratiu Vultur 
1042924ee317SVladimir Oltean 	*nskb = skb;
1043924ee317SVladimir Oltean 
1044924ee317SVladimir Oltean 	return 0;
1045924ee317SVladimir Oltean 
1046924ee317SVladimir Oltean out_free_skb:
1047924ee317SVladimir Oltean 	kfree_skb(skb);
1048924ee317SVladimir Oltean 	return err;
1049924ee317SVladimir Oltean }
1050924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1051924ee317SVladimir Oltean 
1052137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1053137ffbc4SVladimir Oltean {
1054137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1055137ffbc4SVladimir Oltean 
1056137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1057137ffbc4SVladimir Oltean 		return false;
1058137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1059137ffbc4SVladimir Oltean 		return false;
1060137ffbc4SVladimir Oltean 
1061137ffbc4SVladimir Oltean 	return true;
1062137ffbc4SVladimir Oltean }
1063137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1064137ffbc4SVladimir Oltean 
1065137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1066137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1067137ffbc4SVladimir Oltean {
106840d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1069137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1070137ffbc4SVladimir Oltean 
1071137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1072137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1073137ffbc4SVladimir Oltean 
107440d3f295SVladimir Oltean 	ocelot_ifh_set_bypass(ifh, 1);
10751f778d50SVladimir Oltean 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
107640d3f295SVladimir Oltean 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1077e8c07229SVladimir Oltean 	ocelot_ifh_set_vlan_tci(ifh, skb_vlan_tag_get(skb));
107840d3f295SVladimir Oltean 	ocelot_ifh_set_rew_op(ifh, rew_op);
1079137ffbc4SVladimir Oltean 
1080137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
108140d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1082137ffbc4SVladimir Oltean 
1083137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1084137ffbc4SVladimir Oltean 	last = skb->len % 4;
1085137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1086137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1087137ffbc4SVladimir Oltean 
1088137ffbc4SVladimir Oltean 	/* Add padding */
1089137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1090137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1091137ffbc4SVladimir Oltean 		i++;
1092137ffbc4SVladimir Oltean 	}
1093137ffbc4SVladimir Oltean 
1094137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1095137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1096137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1097137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1098137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1099137ffbc4SVladimir Oltean 
1100137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1101137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1102137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1103137ffbc4SVladimir Oltean 
1104137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1105137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1106137ffbc4SVladimir Oltean }
1107137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1108137ffbc4SVladimir Oltean 
11090a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
11100a6f17c6SVladimir Oltean {
11110a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
11120a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
11130a6f17c6SVladimir Oltean }
11140a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
11150a6f17c6SVladimir Oltean 
11165e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
111787b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
1118a556c76aSAlexandre Belloni {
1119471beb11SVladimir Oltean 	int pgid = port;
1120471beb11SVladimir Oltean 
1121471beb11SVladimir Oltean 	if (port == ocelot->npi)
1122471beb11SVladimir Oltean 		pgid = PGID_CPU;
1123a556c76aSAlexandre Belloni 
1124471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
1125a556c76aSAlexandre Belloni }
11265e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1127a556c76aSAlexandre Belloni 
11285e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
1129531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
1130531ee1a6SVladimir Oltean {
1131531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1132531ee1a6SVladimir Oltean }
11335e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1134531ee1a6SVladimir Oltean 
11359c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1136531ee1a6SVladimir Oltean 			    bool is_static, void *data)
1137a556c76aSAlexandre Belloni {
1138531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
1139a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1140a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
1141a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
1142a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
1143a556c76aSAlexandre Belloni 
1144a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
1145a556c76aSAlexandre Belloni 		goto skip;
1146a556c76aSAlexandre Belloni 
1147a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1148a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
1149a556c76aSAlexandre Belloni 	if (!nlh)
1150a556c76aSAlexandre Belloni 		return -EMSGSIZE;
1151a556c76aSAlexandre Belloni 
1152a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
1153a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
1154a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
1155a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
1156a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
1157a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
1158a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
1159531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
1160a556c76aSAlexandre Belloni 
1161531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
1162a556c76aSAlexandre Belloni 		goto nla_put_failure;
1163a556c76aSAlexandre Belloni 
1164531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
1165a556c76aSAlexandre Belloni 		goto nla_put_failure;
1166a556c76aSAlexandre Belloni 
1167a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
1168a556c76aSAlexandre Belloni 
1169a556c76aSAlexandre Belloni skip:
1170a556c76aSAlexandre Belloni 	dump->idx++;
1171a556c76aSAlexandre Belloni 	return 0;
1172a556c76aSAlexandre Belloni 
1173a556c76aSAlexandre Belloni nla_put_failure:
1174a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
1175a556c76aSAlexandre Belloni 	return -EMSGSIZE;
1176a556c76aSAlexandre Belloni }
11779c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
1178a556c76aSAlexandre Belloni 
1179531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1180a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1181a556c76aSAlexandre Belloni {
1182a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1183531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1184a556c76aSAlexandre Belloni 
1185a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1186a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1187a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1188a556c76aSAlexandre Belloni 
1189a556c76aSAlexandre Belloni 	/* Issue a read command */
1190a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1191a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1192a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1193a556c76aSAlexandre Belloni 
1194a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1195a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1196a556c76aSAlexandre Belloni 
1197a556c76aSAlexandre Belloni 	/* Read the entry flags */
1198a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1199a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1200a556c76aSAlexandre Belloni 		return -EINVAL;
1201a556c76aSAlexandre Belloni 
1202a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1203a556c76aSAlexandre Belloni 	 * do not report it.
1204a556c76aSAlexandre Belloni 	 */
1205a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1206531ee1a6SVladimir Oltean 	if (dst != port)
1207a556c76aSAlexandre Belloni 		return -EINVAL;
1208a556c76aSAlexandre Belloni 
1209a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1210a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1211a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1212a556c76aSAlexandre Belloni 
1213a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1214a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1215a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1216a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1217a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1218a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1219a556c76aSAlexandre Belloni 
1220a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1221a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1222a556c76aSAlexandre Belloni 
1223a556c76aSAlexandre Belloni 	return 0;
1224a556c76aSAlexandre Belloni }
1225a556c76aSAlexandre Belloni 
12265e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1227531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1228a556c76aSAlexandre Belloni {
1229531ee1a6SVladimir Oltean 	int i, j;
1230a556c76aSAlexandre Belloni 
123121ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
123221ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1233a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1234531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1235531ee1a6SVladimir Oltean 			bool is_static;
1236531ee1a6SVladimir Oltean 			int ret;
1237531ee1a6SVladimir Oltean 
1238531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
1239a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1240a556c76aSAlexandre Belloni 			 * skip it.
1241a556c76aSAlexandre Belloni 			 */
1242a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
1243a556c76aSAlexandre Belloni 				continue;
1244a556c76aSAlexandre Belloni 			else if (ret)
1245531ee1a6SVladimir Oltean 				return ret;
1246a556c76aSAlexandre Belloni 
1247531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1248531ee1a6SVladimir Oltean 
1249531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
1250a556c76aSAlexandre Belloni 			if (ret)
1251531ee1a6SVladimir Oltean 				return ret;
1252a556c76aSAlexandre Belloni 		}
1253a556c76aSAlexandre Belloni 	}
1254a556c76aSAlexandre Belloni 
1255531ee1a6SVladimir Oltean 	return 0;
1256531ee1a6SVladimir Oltean }
12575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1258531ee1a6SVladimir Oltean 
1259f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
12604e3b0468SAntoine Tenart {
12614e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
12624e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
12634e3b0468SAntoine Tenart }
1264f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
12654e3b0468SAntoine Tenart 
1266f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
12674e3b0468SAntoine Tenart {
1268306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
12694e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
12704e3b0468SAntoine Tenart 
12714e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
12724e3b0468SAntoine Tenart 		return -EFAULT;
12734e3b0468SAntoine Tenart 
12744e3b0468SAntoine Tenart 	/* reserved for future extensions */
12754e3b0468SAntoine Tenart 	if (cfg.flags)
12764e3b0468SAntoine Tenart 		return -EINVAL;
12774e3b0468SAntoine Tenart 
12784e3b0468SAntoine Tenart 	/* Tx type sanity check */
12794e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
12804e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1281306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
12824e3b0468SAntoine Tenart 		break;
12834e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
12844e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
12854e3b0468SAntoine Tenart 		 * need to update the origin time.
12864e3b0468SAntoine Tenart 		 */
1287306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
12884e3b0468SAntoine Tenart 		break;
12894e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1290306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
12914e3b0468SAntoine Tenart 		break;
12924e3b0468SAntoine Tenart 	default:
12934e3b0468SAntoine Tenart 		return -ERANGE;
12944e3b0468SAntoine Tenart 	}
12954e3b0468SAntoine Tenart 
12964e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
12974e3b0468SAntoine Tenart 
12984e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
12994e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
13004e3b0468SAntoine Tenart 		break;
13014e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
13024e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
13034e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13044e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13054e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13064e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
13074e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13084e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
13094e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
13104e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
13114e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
13124e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
13134e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
13144e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
13154e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
13164e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
13174e3b0468SAntoine Tenart 		break;
13184e3b0468SAntoine Tenart 	default:
13194e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
13204e3b0468SAntoine Tenart 		return -ERANGE;
13214e3b0468SAntoine Tenart 	}
13224e3b0468SAntoine Tenart 
13234e3b0468SAntoine Tenart 	/* Commit back the result & save it */
13244e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
13254e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
13264e3b0468SAntoine Tenart 
13274e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
13284e3b0468SAntoine Tenart }
1329f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
13304e3b0468SAntoine Tenart 
13315e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1332a556c76aSAlexandre Belloni {
1333a556c76aSAlexandre Belloni 	int i;
1334a556c76aSAlexandre Belloni 
1335a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1336a556c76aSAlexandre Belloni 		return;
1337a556c76aSAlexandre Belloni 
1338a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1339a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1340a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1341a556c76aSAlexandre Belloni }
13425e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1343a556c76aSAlexandre Belloni 
13441e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1345a556c76aSAlexandre Belloni {
1346a556c76aSAlexandre Belloni 	int i, j;
1347a556c76aSAlexandre Belloni 
1348a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1349a556c76aSAlexandre Belloni 
1350a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1351a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1352a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1353a556c76aSAlexandre Belloni 
1354a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1355a556c76aSAlexandre Belloni 			u32 val;
1356a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1357a556c76aSAlexandre Belloni 
1358a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1359a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1360a556c76aSAlexandre Belloni 
1361a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1362a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1363a556c76aSAlexandre Belloni 
1364a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1365a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1366a556c76aSAlexandre Belloni 		}
1367a556c76aSAlexandre Belloni 	}
1368a556c76aSAlexandre Belloni 
13691e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
13701e1caa97SClaudiu Manoil }
13711e1caa97SClaudiu Manoil 
13721e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
13731e1caa97SClaudiu Manoil {
13741e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
13751e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
13761e1caa97SClaudiu Manoil 					     stats_work);
13771e1caa97SClaudiu Manoil 
13781e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
13791e1caa97SClaudiu Manoil 
1380a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1381a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1382a556c76aSAlexandre Belloni }
1383a556c76aSAlexandre Belloni 
13845e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1385a556c76aSAlexandre Belloni {
1386a556c76aSAlexandre Belloni 	int i;
1387a556c76aSAlexandre Belloni 
1388a556c76aSAlexandre Belloni 	/* check and update now */
13891e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1390a556c76aSAlexandre Belloni 
1391a556c76aSAlexandre Belloni 	/* Copy all counters */
1392a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1393004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1394a556c76aSAlexandre Belloni }
13955e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1396a556c76aSAlexandre Belloni 
13975e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1398c7282d38SVladimir Oltean {
1399a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1400a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1401c7282d38SVladimir Oltean 
1402a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1403a556c76aSAlexandre Belloni }
14045e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1405a556c76aSAlexandre Belloni 
14065e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1407c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1408c7282d38SVladimir Oltean {
14094e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
14104e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1411d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1412d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1413d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1414d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1415d2b09a8eSYangbo Lu 		return 0;
1416d2b09a8eSYangbo Lu 	}
14174e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
14184e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
14194e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
14204e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
14214e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
14224e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
14234e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
14244e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
14254e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
14264e3b0468SAntoine Tenart 
14274e3b0468SAntoine Tenart 	return 0;
14284e3b0468SAntoine Tenart }
14295e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
14304e3b0468SAntoine Tenart 
143123ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
143223ca3b72SVladimir Oltean 				bool only_active_ports)
1433b80af659SVladimir Oltean {
1434b80af659SVladimir Oltean 	u32 mask = 0;
1435b80af659SVladimir Oltean 	int port;
1436b80af659SVladimir Oltean 
1437b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1438b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1439b80af659SVladimir Oltean 
1440b80af659SVladimir Oltean 		if (!ocelot_port)
1441b80af659SVladimir Oltean 			continue;
1442b80af659SVladimir Oltean 
144323ca3b72SVladimir Oltean 		if (ocelot_port->bond == bond) {
144423ca3b72SVladimir Oltean 			if (only_active_ports && !ocelot_port->lag_tx_active)
144523ca3b72SVladimir Oltean 				continue;
144623ca3b72SVladimir Oltean 
1447b80af659SVladimir Oltean 			mask |= BIT(port);
1448b80af659SVladimir Oltean 		}
144923ca3b72SVladimir Oltean 	}
1450b80af659SVladimir Oltean 
1451b80af659SVladimir Oltean 	return mask;
1452b80af659SVladimir Oltean }
1453b80af659SVladimir Oltean 
1454acc64f52SVladimir Oltean static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port,
1455df291e54SVladimir Oltean 				      struct net_device *bridge)
1456df291e54SVladimir Oltean {
1457acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1458df291e54SVladimir Oltean 	u32 mask = 0;
1459df291e54SVladimir Oltean 	int port;
1460df291e54SVladimir Oltean 
1461acc64f52SVladimir Oltean 	if (!ocelot_port || ocelot_port->bridge != bridge ||
1462acc64f52SVladimir Oltean 	    ocelot_port->stp_state != BR_STATE_FORWARDING)
1463acc64f52SVladimir Oltean 		return 0;
1464acc64f52SVladimir Oltean 
1465df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1466acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1467df291e54SVladimir Oltean 
1468df291e54SVladimir Oltean 		if (!ocelot_port)
1469df291e54SVladimir Oltean 			continue;
1470df291e54SVladimir Oltean 
1471df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1472df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1473df291e54SVladimir Oltean 			mask |= BIT(port);
1474df291e54SVladimir Oltean 	}
1475df291e54SVladimir Oltean 
1476df291e54SVladimir Oltean 	return mask;
1477df291e54SVladimir Oltean }
1478df291e54SVladimir Oltean 
1479e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
14809b521250SVladimir Oltean {
1481e21268efSVladimir Oltean 	u32 mask = 0;
14829b521250SVladimir Oltean 	int port;
14839b521250SVladimir Oltean 
1484e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1485e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1486e21268efSVladimir Oltean 
1487e21268efSVladimir Oltean 		if (!ocelot_port)
1488e21268efSVladimir Oltean 			continue;
1489e21268efSVladimir Oltean 
1490e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
1491e21268efSVladimir Oltean 			mask |= BIT(port);
1492e21268efSVladimir Oltean 	}
1493e21268efSVladimir Oltean 
1494e21268efSVladimir Oltean 	return mask;
1495e21268efSVladimir Oltean }
1496e21268efSVladimir Oltean 
1497e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1498e21268efSVladimir Oltean {
1499e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
1500e21268efSVladimir Oltean 	int port;
1501e21268efSVladimir Oltean 
1502e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
1503e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
1504e21268efSVladimir Oltean 	 * those are bridged or standalone.
1505e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
1506e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
1507e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
1508e21268efSVladimir Oltean 	 */
1509e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1510e21268efSVladimir Oltean 
15119b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
15129b521250SVladimir Oltean 	 * a source for the other ports.
15139b521250SVladimir Oltean 	 */
15149b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1515e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1516e21268efSVladimir Oltean 		unsigned long mask;
1517e21268efSVladimir Oltean 
1518e21268efSVladimir Oltean 		if (!ocelot_port) {
1519e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1520e21268efSVladimir Oltean 			mask = 0;
1521e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1522e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1523e21268efSVladimir Oltean 			 * forward packets to all other ports except for
1524e21268efSVladimir Oltean 			 * themselves
1525e21268efSVladimir Oltean 			 */
1526e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1527e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
1528df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1529df291e54SVladimir Oltean 			struct net_device *bridge = ocelot_port->bridge;
1530528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
15319b521250SVladimir Oltean 
1532acc64f52SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port, bridge);
1533c1930148SVladimir Oltean 			mask |= cpu_fwd_mask;
1534df291e54SVladimir Oltean 			mask &= ~BIT(port);
153523ca3b72SVladimir Oltean 			if (bond) {
153623ca3b72SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond,
153723ca3b72SVladimir Oltean 							      false);
153823ca3b72SVladimir Oltean 			}
15399b521250SVladimir Oltean 		} else {
1540e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1541e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1542e21268efSVladimir Oltean 			 * module otherwise.
1543e21268efSVladimir Oltean 			 */
1544e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
1545e21268efSVladimir Oltean 		}
1546e21268efSVladimir Oltean 
1547e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
15489b521250SVladimir Oltean 	}
15499b521250SVladimir Oltean }
1550e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
15519b521250SVladimir Oltean 
15525e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1553a556c76aSAlexandre Belloni {
1554421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1555df291e54SVladimir Oltean 	u32 learn_ena = 0;
1556a556c76aSAlexandre Belloni 
1557df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1558a556c76aSAlexandre Belloni 
1559df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1560df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1561df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1562a556c76aSAlexandre Belloni 
1563df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1564df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1565a556c76aSAlexandre Belloni 
15669b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1567a556c76aSAlexandre Belloni }
15685e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1569a556c76aSAlexandre Belloni 
15705e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
15714bda1415SVladimir Oltean {
1572c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1573c0d7eccbSVladimir Oltean 
1574c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1575c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1576c0d7eccbSVladimir Oltean 	 */
1577c0d7eccbSVladimir Oltean 	if (!age_period)
1578c0d7eccbSVladimir Oltean 		age_period = 1;
1579c0d7eccbSVladimir Oltean 
1580c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1581a556c76aSAlexandre Belloni }
15825e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1583a556c76aSAlexandre Belloni 
1584a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1585a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1586a556c76aSAlexandre Belloni 						     u16 vid)
1587a556c76aSAlexandre Belloni {
1588a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1589a556c76aSAlexandre Belloni 
1590a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1591a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1592a556c76aSAlexandre Belloni 			return mc;
1593a556c76aSAlexandre Belloni 	}
1594a556c76aSAlexandre Belloni 
1595a556c76aSAlexandre Belloni 	return NULL;
1596a556c76aSAlexandre Belloni }
1597a556c76aSAlexandre Belloni 
15989403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
15999403c158SVladimir Oltean {
16009403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
16019403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
16029403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
16039403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
16047c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
16059403c158SVladimir Oltean }
16069403c158SVladimir Oltean 
1607e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1608e5d1f896SVladimir Oltean 					     unsigned long ports)
1609e5d1f896SVladimir Oltean {
1610e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1611e5d1f896SVladimir Oltean 
1612e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1613e5d1f896SVladimir Oltean 	if (!pgid)
1614e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1615e5d1f896SVladimir Oltean 
1616e5d1f896SVladimir Oltean 	pgid->ports = ports;
1617e5d1f896SVladimir Oltean 	pgid->index = index;
1618e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1619e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1620e5d1f896SVladimir Oltean 
1621e5d1f896SVladimir Oltean 	return pgid;
1622e5d1f896SVladimir Oltean }
1623e5d1f896SVladimir Oltean 
1624e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1625e5d1f896SVladimir Oltean {
1626e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1627e5d1f896SVladimir Oltean 		return;
1628e5d1f896SVladimir Oltean 
1629e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1630e5d1f896SVladimir Oltean 	kfree(pgid);
1631e5d1f896SVladimir Oltean }
1632e5d1f896SVladimir Oltean 
1633e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1634bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
16359403c158SVladimir Oltean {
1636e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1637e5d1f896SVladimir Oltean 	int index;
16389403c158SVladimir Oltean 
16399403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
16409403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
16419403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
16429403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
16439403c158SVladimir Oltean 	 */
1644bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1645bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1646e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
16479403c158SVladimir Oltean 
1648e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1649e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1650e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1651e5d1f896SVladimir Oltean 		 */
1652e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1653e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1654e5d1f896SVladimir Oltean 			return pgid;
1655e5d1f896SVladimir Oltean 		}
1656e5d1f896SVladimir Oltean 	}
1657e5d1f896SVladimir Oltean 
1658e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1659e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
16609403c158SVladimir Oltean 		bool used = false;
16619403c158SVladimir Oltean 
1662e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1663e5d1f896SVladimir Oltean 			if (pgid->index == index) {
16649403c158SVladimir Oltean 				used = true;
16659403c158SVladimir Oltean 				break;
16669403c158SVladimir Oltean 			}
16679403c158SVladimir Oltean 		}
16689403c158SVladimir Oltean 
16699403c158SVladimir Oltean 		if (!used)
1670e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
16719403c158SVladimir Oltean 	}
16729403c158SVladimir Oltean 
1673e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
16749403c158SVladimir Oltean }
16759403c158SVladimir Oltean 
16769403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1677bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
16789403c158SVladimir Oltean {
1679ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
16809403c158SVladimir Oltean 
1681bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
16829403c158SVladimir Oltean 		addr[0] = 0;
16839403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
16849403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1685bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
16869403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
16879403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
16889403c158SVladimir Oltean 	}
16899403c158SVladimir Oltean }
16909403c158SVladimir Oltean 
1691209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1692209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1693a556c76aSAlexandre Belloni {
1694a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1695004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1696e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1697a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1698a556c76aSAlexandre Belloni 
1699471beb11SVladimir Oltean 	if (port == ocelot->npi)
1700471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1701471beb11SVladimir Oltean 
1702a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1703a556c76aSAlexandre Belloni 	if (!mc) {
1704728e69aeSVladimir Oltean 		/* New entry */
1705bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1706bb8d53fdSVladimir Oltean 		if (!mc)
1707bb8d53fdSVladimir Oltean 			return -ENOMEM;
1708bb8d53fdSVladimir Oltean 
1709bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1710bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1711bb8d53fdSVladimir Oltean 		mc->vid = vid;
1712bb8d53fdSVladimir Oltean 
1713a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1714728e69aeSVladimir Oltean 	} else {
1715e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1716e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1717e5d1f896SVladimir Oltean 		 */
1718e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1719bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1720a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1721a556c76aSAlexandre Belloni 	}
1722a556c76aSAlexandre Belloni 
1723004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1724e5d1f896SVladimir Oltean 
1725e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1726e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1727e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1728e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1729e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1730e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1731e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1732e5d1f896SVladimir Oltean 	}
1733e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1734e5d1f896SVladimir Oltean 
1735bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1736a556c76aSAlexandre Belloni 
1737e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1738e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1739e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1740e5d1f896SVladimir Oltean 				 pgid->index);
1741e5d1f896SVladimir Oltean 
1742e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1743bb8d53fdSVladimir Oltean 				 mc->entry_type);
1744a556c76aSAlexandre Belloni }
1745209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1746a556c76aSAlexandre Belloni 
1747209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1748a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1749a556c76aSAlexandre Belloni {
1750a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1751004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1752e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1753a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1754a556c76aSAlexandre Belloni 
1755471beb11SVladimir Oltean 	if (port == ocelot->npi)
1756471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1757471beb11SVladimir Oltean 
1758a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1759a556c76aSAlexandre Belloni 	if (!mc)
1760a556c76aSAlexandre Belloni 		return -ENOENT;
1761a556c76aSAlexandre Belloni 
1762bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1763a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1764a556c76aSAlexandre Belloni 
1765e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1766004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1767a556c76aSAlexandre Belloni 	if (!mc->ports) {
1768a556c76aSAlexandre Belloni 		list_del(&mc->list);
1769a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1770a556c76aSAlexandre Belloni 		return 0;
1771a556c76aSAlexandre Belloni 	}
1772a556c76aSAlexandre Belloni 
1773e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1774e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1775e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1776e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1777e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1778e5d1f896SVladimir Oltean 
1779bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1780a556c76aSAlexandre Belloni 
1781e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1782e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1783e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1784e5d1f896SVladimir Oltean 				 pgid->index);
1785e5d1f896SVladimir Oltean 
1786e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1787bb8d53fdSVladimir Oltean 				 mc->entry_type);
1788a556c76aSAlexandre Belloni }
1789209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1790a556c76aSAlexandre Belloni 
1791e4bd44e8SVladimir Oltean void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1792a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1793a556c76aSAlexandre Belloni {
1794df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1795a556c76aSAlexandre Belloni 
1796df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
1797a556c76aSAlexandre Belloni 
1798e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1799a556c76aSAlexandre Belloni }
18005e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1801a556c76aSAlexandre Belloni 
1802e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1803a556c76aSAlexandre Belloni 			      struct net_device *bridge)
1804a556c76aSAlexandre Belloni {
1805df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1806*0da1a1c4SVladimir Oltean 	struct ocelot_vlan pvid = {0};
18072e554a7aSVladimir Oltean 
1808df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
18097142529fSAntoine Tenart 
1810c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
1811*0da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
1812e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1813a556c76aSAlexandre Belloni }
18145e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1815a556c76aSAlexandre Belloni 
1816dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1817dc96ee37SAlexandre Belloni {
1818528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1819dc96ee37SAlexandre Belloni 	int i, port, lag;
1820dc96ee37SAlexandre Belloni 
1821dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
182296b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1823dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1824dc96ee37SAlexandre Belloni 
182596b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1826dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1827dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1828dc96ee37SAlexandre Belloni 
1829528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1830528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1831528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1832528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1833528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1834528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1835528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1836528d3f19SVladimir Oltean 	 */
1837528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1838528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1839528d3f19SVladimir Oltean 
1840528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1841528d3f19SVladimir Oltean 			continue;
1842528d3f19SVladimir Oltean 
1843528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1844528d3f19SVladimir Oltean 	}
1845528d3f19SVladimir Oltean 
1846528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1847dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1848528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
184923ca3b72SVladimir Oltean 		int num_active_ports = 0;
1850dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1851dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1852dc96ee37SAlexandre Belloni 
1853528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1854dc96ee37SAlexandre Belloni 			continue;
1855dc96ee37SAlexandre Belloni 
185623ca3b72SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1857528d3f19SVladimir Oltean 
1858dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1859dc96ee37SAlexandre Belloni 			// Destination mask
1860dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1861dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
186223ca3b72SVladimir Oltean 			aggr_idx[num_active_ports++] = port;
1863dc96ee37SAlexandre Belloni 		}
1864dc96ee37SAlexandre Belloni 
186596b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1866dc96ee37SAlexandre Belloni 			u32 ac;
1867dc96ee37SAlexandre Belloni 
1868dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1869dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
187023ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
187123ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
187223ca3b72SVladimir Oltean 			 */
187323ca3b72SVladimir Oltean 			if (num_active_ports)
187423ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1875dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1876dc96ee37SAlexandre Belloni 		}
1877528d3f19SVladimir Oltean 
1878528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1879528d3f19SVladimir Oltean 		 * the same config again.
1880528d3f19SVladimir Oltean 		 */
1881528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1882528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1883528d3f19SVladimir Oltean 
1884528d3f19SVladimir Oltean 			if (!ocelot_port)
1885528d3f19SVladimir Oltean 				continue;
1886528d3f19SVladimir Oltean 
1887528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1888528d3f19SVladimir Oltean 				visited |= BIT(port);
1889528d3f19SVladimir Oltean 		}
1890dc96ee37SAlexandre Belloni 	}
1891dc96ee37SAlexandre Belloni }
1892dc96ee37SAlexandre Belloni 
18932527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
18942527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
18952527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
18962527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
18972527f2e8SVladimir Oltean  */
18982527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1899dc96ee37SAlexandre Belloni {
19002527f2e8SVladimir Oltean 	int port;
1901dc96ee37SAlexandre Belloni 
19022527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
19032527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
19042527f2e8SVladimir Oltean 		struct net_device *bond;
1905dc96ee37SAlexandre Belloni 
19062527f2e8SVladimir Oltean 		if (!ocelot_port)
19072527f2e8SVladimir Oltean 			continue;
1908dc96ee37SAlexandre Belloni 
19092527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
19102527f2e8SVladimir Oltean 		if (bond) {
191123ca3b72SVladimir Oltean 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
191223ca3b72SVladimir Oltean 							     false));
19132527f2e8SVladimir Oltean 
19142527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
1915dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
19162527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
19172527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
19182527f2e8SVladimir Oltean 		} else {
19192527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
19202527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
19212527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
19222527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
19232527f2e8SVladimir Oltean 		}
1924dc96ee37SAlexandre Belloni 	}
1925dc96ee37SAlexandre Belloni }
1926dc96ee37SAlexandre Belloni 
19279c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1928583cbbe3SVladimir Oltean 			 struct net_device *bond,
1929583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
1930dc96ee37SAlexandre Belloni {
1931583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1932583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
1933583cbbe3SVladimir Oltean 
1934b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
1935dc96ee37SAlexandre Belloni 
19362527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
19379b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1938dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1939dc96ee37SAlexandre Belloni 
1940dc96ee37SAlexandre Belloni 	return 0;
1941dc96ee37SAlexandre Belloni }
19429c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1943dc96ee37SAlexandre Belloni 
19449c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1945dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1946dc96ee37SAlexandre Belloni {
1947b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
1948b80af659SVladimir Oltean 
19492527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
19509b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1951dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1952dc96ee37SAlexandre Belloni }
19539c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
19540e332c85SPetr Machata 
195523ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
195623ca3b72SVladimir Oltean {
195723ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
195823ca3b72SVladimir Oltean 
195923ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
196023ca3b72SVladimir Oltean 
196123ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
196223ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
196323ca3b72SVladimir Oltean }
196423ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
196523ca3b72SVladimir Oltean 
1966a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1967a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
19680b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
19690b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
19700b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1971a8015dedSVladimir Oltean  */
19720b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
197331350d7fSVladimir Oltean {
197431350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1975a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1976e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1977601e984fSVladimir Oltean 	int atop, atop_tot;
197831350d7fSVladimir Oltean 
19790b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
19800b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
19810b912fc9SVladimir Oltean 
1982cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
19830b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1984cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
19850b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
19860b912fc9SVladimir Oltean 	}
19870b912fc9SVladimir Oltean 
1988a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1989fa914e9cSVladimir Oltean 
1990e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1991e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1992e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1993541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1994541132f0SMaxim Kochetkov 			    pause_start);
1995541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1996541132f0SMaxim Kochetkov 			    pause_stop);
1997fa914e9cSVladimir Oltean 
1998601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1999f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2000a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2001601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2002601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2003601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2004fa914e9cSVladimir Oltean }
20050b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
20060b912fc9SVladimir Oltean 
20070b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
20080b912fc9SVladimir Oltean {
20090b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
20100b912fc9SVladimir Oltean 
20110b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
20120b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
20130b912fc9SVladimir Oltean 
2014cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
20150b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2016cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
20170b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
20180b912fc9SVladimir Oltean 	}
20190b912fc9SVladimir Oltean 
20200b912fc9SVladimir Oltean 	return max_mtu;
20210b912fc9SVladimir Oltean }
20220b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2023fa914e9cSVladimir Oltean 
2024421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2025421741eaSVladimir Oltean 				     bool enabled)
2026421741eaSVladimir Oltean {
2027421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2028421741eaSVladimir Oltean 	u32 val = 0;
2029421741eaSVladimir Oltean 
2030421741eaSVladimir Oltean 	if (enabled)
2031421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2032421741eaSVladimir Oltean 
2033421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2034421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2035421741eaSVladimir Oltean 
2036421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2037421741eaSVladimir Oltean }
2038421741eaSVladimir Oltean 
2039421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2040421741eaSVladimir Oltean 					bool enabled)
2041421741eaSVladimir Oltean {
2042421741eaSVladimir Oltean 	u32 val = 0;
2043421741eaSVladimir Oltean 
2044421741eaSVladimir Oltean 	if (enabled)
2045421741eaSVladimir Oltean 		val = BIT(port);
2046421741eaSVladimir Oltean 
2047421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2048421741eaSVladimir Oltean }
2049421741eaSVladimir Oltean 
2050421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2051421741eaSVladimir Oltean 					bool enabled)
2052421741eaSVladimir Oltean {
2053421741eaSVladimir Oltean 	u32 val = 0;
2054421741eaSVladimir Oltean 
2055421741eaSVladimir Oltean 	if (enabled)
2056421741eaSVladimir Oltean 		val = BIT(port);
2057421741eaSVladimir Oltean 
2058421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
2059421741eaSVladimir Oltean }
2060421741eaSVladimir Oltean 
2061421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2062421741eaSVladimir Oltean 					bool enabled)
2063421741eaSVladimir Oltean {
2064421741eaSVladimir Oltean 	u32 val = 0;
2065421741eaSVladimir Oltean 
2066421741eaSVladimir Oltean 	if (enabled)
2067421741eaSVladimir Oltean 		val = BIT(port);
2068421741eaSVladimir Oltean 
2069421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2070421741eaSVladimir Oltean }
2071421741eaSVladimir Oltean 
2072421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2073421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2074421741eaSVladimir Oltean {
2075421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2076421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2077421741eaSVladimir Oltean 		return -EINVAL;
2078421741eaSVladimir Oltean 
2079421741eaSVladimir Oltean 	return 0;
2080421741eaSVladimir Oltean }
2081421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2082421741eaSVladimir Oltean 
2083421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2084421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2085421741eaSVladimir Oltean {
2086421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2087421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2088421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2089421741eaSVladimir Oltean 
2090421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2091421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2092421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2093421741eaSVladimir Oltean 
2094421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2095421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2096421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2097421741eaSVladimir Oltean 
2098421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2099421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2100421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2101421741eaSVladimir Oltean }
2102421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2103421741eaSVladimir Oltean 
21045e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2105fa914e9cSVladimir Oltean {
2106fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2107fa914e9cSVladimir Oltean 
2108b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
210931350d7fSVladimir Oltean 
211031350d7fSVladimir Oltean 	/* Basic L2 initialization */
211131350d7fSVladimir Oltean 
21125bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
21135bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
21145bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
21155bc9d2e6SVladimir Oltean 	 */
21165bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
21175bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
21185bc9d2e6SVladimir Oltean 
21195bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
21205bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
21215bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
21225bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
21235bc9d2e6SVladimir Oltean 	mdelay(1);
21245bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
21255bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
21265bc9d2e6SVladimir Oltean 
21275bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2128a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
21295bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
21305bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2131a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
21325bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
21335bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
21345bc9d2e6SVladimir Oltean 
21355bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
21365bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
21375bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
21385bc9d2e6SVladimir Oltean 
2139e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2140541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2141e8e6e73dSVladimir Oltean 
214231350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
214331350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
214431350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
214531350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
214631350d7fSVladimir Oltean 
214731350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
214831350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
214931350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
215031350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
215131350d7fSVladimir Oltean 
2152421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2153421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2154421741eaSVladimir Oltean 
215546efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
215646efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
215746efe4efSVladimir Oltean 	 * automatic.
215846efe4efSVladimir Oltean 	 */
215946efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
216046efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
216146efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
216246efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
216346efe4efSVladimir Oltean 
216431350d7fSVladimir Oltean 	/* Enable vcap lookups */
216531350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
216631350d7fSVladimir Oltean }
21675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
216831350d7fSVladimir Oltean 
21692d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
21702d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
21712d44b097SVladimir Oltean  * NPI mode is used).
217269df578cSVladimir Oltean  */
21732d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
217421468199SVladimir Oltean {
217569df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
217669df578cSVladimir Oltean 
217769df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
217821468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
217969df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
218069df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
218169df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
218269df578cSVladimir Oltean 	 */
218321468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
218421468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
218521468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
218621468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
218721468199SVladimir Oltean 
218869df578cSVladimir Oltean 	/* Enable CPU port module */
2189886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
219069df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2191886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2192cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2193886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2194cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
219521468199SVladimir Oltean 
219621468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
219721468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
219821468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
219921468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
220021468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
220121468199SVladimir Oltean }
220221468199SVladimir Oltean 
2203f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2204f6fe01d6SVladimir Oltean {
2205f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2206f6fe01d6SVladimir Oltean 
2207f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2208f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2209f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2210f6fe01d6SVladimir Oltean 	 */
2211f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2212f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2213f6fe01d6SVladimir Oltean 
2214f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2215f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2216f6fe01d6SVladimir Oltean }
2217f6fe01d6SVladimir Oltean 
2218a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2219a556c76aSAlexandre Belloni {
2220a556c76aSAlexandre Belloni 	char queue_name[32];
222121468199SVladimir Oltean 	int i, ret;
222221468199SVladimir Oltean 	u32 port;
2223a556c76aSAlexandre Belloni 
22243a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
22253a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
22263a77b593SVladimir Oltean 		if (ret) {
22273a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
22283a77b593SVladimir Oltean 			return ret;
22293a77b593SVladimir Oltean 		}
22303a77b593SVladimir Oltean 	}
22313a77b593SVladimir Oltean 
2232a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2233a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2234a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2235a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2236a556c76aSAlexandre Belloni 		return -ENOMEM;
2237a556c76aSAlexandre Belloni 
2238a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
22394e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
22404e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
224152849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
2242a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2243a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2244a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2245a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2246a556c76aSAlexandre Belloni 		return -ENOMEM;
2247a556c76aSAlexandre Belloni 
2248ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2249ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
2250ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
2251ca0b272bSVladimir Oltean 		return -ENOMEM;
2252ca0b272bSVladimir Oltean 	}
2253ca0b272bSVladimir Oltean 
22542b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2255e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
225690e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
2257f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2258a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2259a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2260aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
22612d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2262a556c76aSAlexandre Belloni 
2263a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2264a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2265a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2266a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2267a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2268a556c76aSAlexandre Belloni 	}
2269a556c76aSAlexandre Belloni 
2270a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2271a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2272a556c76aSAlexandre Belloni 
2273a556c76aSAlexandre Belloni 	/* Aggregation mode */
2274a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2275a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2276a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2277f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2278f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2279f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2280f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2281a556c76aSAlexandre Belloni 
2282a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2283a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2284a556c76aSAlexandre Belloni 	 */
2285a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2286a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2287a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2288a556c76aSAlexandre Belloni 
2289a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2290a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2291a556c76aSAlexandre Belloni 
2292a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2293a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2294a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2295a556c76aSAlexandre Belloni 
2296a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2297edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2298a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2299b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2300a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2301edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2302a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2303a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2304a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2305a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2306a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2307a556c76aSAlexandre Belloni 
2308a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2309a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2310a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2311a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2312a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2313a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2314a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2315a556c76aSAlexandre Belloni 				 port);
2316a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2317a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2318a556c76aSAlexandre Belloni 	}
2319a556c76aSAlexandre Belloni 
232096b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2321a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2322a556c76aSAlexandre Belloni 
2323a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2324a556c76aSAlexandre Belloni 	}
2325ebb1bb40SHoratiu Vultur 
2326ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2327ebb1bb40SHoratiu Vultur 
2328b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2329b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2330b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2331a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2332b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2333b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2334b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2335a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2336a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2337a556c76aSAlexandre Belloni 
2338a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2339a556c76aSAlexandre Belloni 	 * registers endianness.
2340a556c76aSAlexandre Belloni 	 */
2341a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2342a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2343a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2344a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2345a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2346a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2347a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2348a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2349a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2350a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2351a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2352a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2353a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2354a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2355a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2356a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2357a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2358a556c76aSAlexandre Belloni 
23591e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2360a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2361a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
23624e3b0468SAntoine Tenart 
2363a556c76aSAlexandre Belloni 	return 0;
2364a556c76aSAlexandre Belloni }
2365a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2366a556c76aSAlexandre Belloni 
2367a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2368a556c76aSAlexandre Belloni {
2369c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2370a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2371ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2372a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2373a556c76aSAlexandre Belloni }
2374a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2375a556c76aSAlexandre Belloni 
2376e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2377e5fb512dSVladimir Oltean {
2378e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2379e5fb512dSVladimir Oltean 
2380e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2381e5fb512dSVladimir Oltean }
2382e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2383e5fb512dSVladimir Oltean 
2384a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2385