xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 924ee317f72459a49ac4130272c7d33063e60339)
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>
920968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10a556c76aSAlexandre Belloni #include "ocelot.h"
113c83654fSVladimir Oltean #include "ocelot_vcap.h"
12a556c76aSAlexandre Belloni 
13639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
14639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
15639c1b26SSteen Hegelund 
16a556c76aSAlexandre Belloni struct ocelot_mact_entry {
17a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
18a556c76aSAlexandre Belloni 	u16 vid;
19a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
20a556c76aSAlexandre Belloni };
21a556c76aSAlexandre Belloni 
22639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
23639c1b26SSteen Hegelund {
24639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
25639c1b26SSteen Hegelund }
26639c1b26SSteen Hegelund 
27a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
28a556c76aSAlexandre Belloni {
29639c1b26SSteen Hegelund 	u32 val;
30a556c76aSAlexandre Belloni 
31639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
32639c1b26SSteen Hegelund 		ocelot, val,
33639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
34639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
35639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
36a556c76aSAlexandre Belloni }
37a556c76aSAlexandre Belloni 
38a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
39a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
40a556c76aSAlexandre Belloni 			       unsigned int vid)
41a556c76aSAlexandre Belloni {
42a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
43a556c76aSAlexandre Belloni 
44a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
45a556c76aSAlexandre Belloni 	 * understood by the hardware.
46a556c76aSAlexandre Belloni 	 */
47a556c76aSAlexandre Belloni 	mach |= vid    << 16;
48a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
49a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
50a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
51a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
52a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
53a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
54a556c76aSAlexandre Belloni 
55a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
56a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
57a556c76aSAlexandre Belloni 
58a556c76aSAlexandre Belloni }
59a556c76aSAlexandre Belloni 
609c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
61a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
629c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
63a556c76aSAlexandre Belloni {
64584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
65584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
66584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
67584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
68584b7cfcSAlban Bedel 	unsigned int mc_ports;
69584b7cfcSAlban Bedel 
70584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
71584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
72584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
73584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
74584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
75584b7cfcSAlban Bedel 	else
76584b7cfcSAlban Bedel 		mc_ports = 0;
77584b7cfcSAlban Bedel 
78584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
79584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
80584b7cfcSAlban Bedel 
81a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
82a556c76aSAlexandre Belloni 
83a556c76aSAlexandre Belloni 	/* Issue a write command */
84584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
85a556c76aSAlexandre Belloni 
86a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
87a556c76aSAlexandre Belloni }
889c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
89a556c76aSAlexandre Belloni 
909c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
919c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
92a556c76aSAlexandre Belloni {
93a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
94a556c76aSAlexandre Belloni 
95a556c76aSAlexandre Belloni 	/* Issue a forget command */
96a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
97a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
98a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
99a556c76aSAlexandre Belloni 
100a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
101a556c76aSAlexandre Belloni }
1029c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
103a556c76aSAlexandre Belloni 
104a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
105a556c76aSAlexandre Belloni {
106a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
107a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
108a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
109a556c76aSAlexandre Belloni 	 */
110a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
111a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
112a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
113a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
114a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
115a556c76aSAlexandre Belloni 
116a556c76aSAlexandre Belloni 	/* Clear the MAC table */
117a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
118a556c76aSAlexandre Belloni }
119a556c76aSAlexandre Belloni 
120f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
121b5962294SHoratiu Vultur {
122b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
123b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
124f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
12575944fdaSXiaoliang Yang 
12675944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
12775944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1282f17c050SXiaoliang Yang 
1292f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1302f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1312f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
132b5962294SHoratiu Vultur }
133b5962294SHoratiu Vultur 
134639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
135639c1b26SSteen Hegelund {
136639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
137639c1b26SSteen Hegelund }
138639c1b26SSteen Hegelund 
139a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
140a556c76aSAlexandre Belloni {
141639c1b26SSteen Hegelund 	u32 val;
142a556c76aSAlexandre Belloni 
143639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
144639c1b26SSteen Hegelund 		ocelot,
145639c1b26SSteen Hegelund 		val,
146639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
147639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
148639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
149a556c76aSAlexandre Belloni }
150a556c76aSAlexandre Belloni 
1517142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1527142529fSAntoine Tenart {
1537142529fSAntoine Tenart 	/* Select the VID to configure */
1547142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1557142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1567142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1577142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1587142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1597142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1607142529fSAntoine Tenart 
1617142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1627142529fSAntoine Tenart }
1637142529fSAntoine Tenart 
1642f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
165c3e58a75SVladimir Oltean 					struct ocelot_vlan native_vlan)
16697bb69e1SVladimir Oltean {
16797bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
16887b0f983SVladimir Oltean 	u32 val = 0;
16997bb69e1SVladimir Oltean 
170c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
17197bb69e1SVladimir Oltean 
172c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1737142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
17497bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17597bb69e1SVladimir Oltean 
17687b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
177e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
17887b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
17987b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
18087b0f983SVladimir Oltean 		else
18187b0f983SVladimir Oltean 			/* Tag all frames */
18287b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
18387b0f983SVladimir Oltean 	} else {
18487b0f983SVladimir Oltean 		/* Port tagging disabled. */
18587b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
18687b0f983SVladimir Oltean 	}
18787b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
18887b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
18987b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
19097bb69e1SVladimir Oltean }
19197bb69e1SVladimir Oltean 
19275e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
193c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
194c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
19575e5a554SVladimir Oltean {
19675e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
197be0576feSVladimir Oltean 	u32 val = 0;
19875e5a554SVladimir Oltean 
199c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
20075e5a554SVladimir Oltean 
20175e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
202c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
20375e5a554SVladimir Oltean 
20475e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
205c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
20675e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
20775e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
208be0576feSVladimir Oltean 
209be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
210be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
211be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
212be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
213be0576feSVladimir Oltean 	 */
214be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
215be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
216be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
217be0576feSVladimir Oltean 
218be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
219be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
220be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
221be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
22275e5a554SVladimir Oltean }
22375e5a554SVladimir Oltean 
2242e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
225bae33f2bSVladimir Oltean 			       bool vlan_aware)
22687b0f983SVladimir Oltean {
22770edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
228bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
22970edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
230bae33f2bSVladimir Oltean 	u32 val;
23170edfae1SVladimir Oltean 
23270edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
23370edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
23470edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
23570edfae1SVladimir Oltean 			dev_err(ocelot->dev,
23670edfae1SVladimir Oltean 				"Cannot change VLAN state with vlan modify rules active\n");
23770edfae1SVladimir Oltean 			return -EBUSY;
23870edfae1SVladimir Oltean 		}
23970edfae1SVladimir Oltean 	}
24070edfae1SVladimir Oltean 
24187b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
24287b0f983SVladimir Oltean 
24387b0f983SVladimir Oltean 	if (vlan_aware)
24487b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24587b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
24687b0f983SVladimir Oltean 	else
24787b0f983SVladimir Oltean 		val = 0;
24887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
24987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
25087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
25187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
25287b0f983SVladimir Oltean 
253c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
254c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2552e554a7aSVladimir Oltean 
2562e554a7aSVladimir Oltean 	return 0;
25787b0f983SVladimir Oltean }
25887b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
25987b0f983SVladimir Oltean 
2602f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2612f0402feSVladimir Oltean 			bool untagged)
2622f0402feSVladimir Oltean {
2632f0402feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2642f0402feSVladimir Oltean 
2652f0402feSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
2662f0402feSVladimir Oltean 	if (untagged && ocelot_port->native_vlan.vid != vid &&
2672f0402feSVladimir Oltean 	    ocelot_port->native_vlan.valid) {
2682f0402feSVladimir Oltean 		dev_err(ocelot->dev,
2692f0402feSVladimir Oltean 			"Port already has a native VLAN: %d\n",
2702f0402feSVladimir Oltean 			ocelot_port->native_vlan.vid);
2712f0402feSVladimir Oltean 		return -EBUSY;
2722f0402feSVladimir Oltean 	}
2732f0402feSVladimir Oltean 
2742f0402feSVladimir Oltean 	return 0;
2752f0402feSVladimir Oltean }
2762f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
2772f0402feSVladimir Oltean 
2785e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2797142529fSAntoine Tenart 		    bool untagged)
2807142529fSAntoine Tenart {
2817142529fSAntoine Tenart 	int ret;
2827142529fSAntoine Tenart 
2837142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
28497bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2857142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2867142529fSAntoine Tenart 	if (ret)
2877142529fSAntoine Tenart 		return ret;
2887142529fSAntoine Tenart 
2897142529fSAntoine Tenart 	/* Default ingress vlan classification */
290c3e58a75SVladimir Oltean 	if (pvid) {
291c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
292c3e58a75SVladimir Oltean 
293c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
294e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
295c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
296c3e58a75SVladimir Oltean 	}
2977142529fSAntoine Tenart 
2987142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
29997bb69e1SVladimir Oltean 	if (untagged) {
300c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
301c3e58a75SVladimir Oltean 
302c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
303e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
3042f0402feSVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
305b9cd75e6SVladimir Oltean 	}
3067142529fSAntoine Tenart 
3077142529fSAntoine Tenart 	return 0;
3087142529fSAntoine Tenart }
3095e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
3107142529fSAntoine Tenart 
3115e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3129855934cSVladimir Oltean {
3139855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3149855934cSVladimir Oltean 	int ret;
3157142529fSAntoine Tenart 
3167142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
31797bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3187142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3197142529fSAntoine Tenart 	if (ret)
3207142529fSAntoine Tenart 		return ret;
3217142529fSAntoine Tenart 
322be0576feSVladimir Oltean 	/* Ingress */
323be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
324be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
325be0576feSVladimir Oltean 
326be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
327be0576feSVladimir Oltean 	}
328be0576feSVladimir Oltean 
3297142529fSAntoine Tenart 	/* Egress */
330c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
331e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
332c3e58a75SVladimir Oltean 
333c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
334c3e58a75SVladimir Oltean 	}
3357142529fSAntoine Tenart 
3367142529fSAntoine Tenart 	return 0;
3377142529fSAntoine Tenart }
3385e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3397142529fSAntoine Tenart 
340a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
341a556c76aSAlexandre Belloni {
3427142529fSAntoine Tenart 	u16 port, vid;
3437142529fSAntoine Tenart 
344a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
345a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
346a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
347a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3487142529fSAntoine Tenart 
3497142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3507142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3517142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3527142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3537142529fSAntoine Tenart 	}
3547142529fSAntoine Tenart 
3557142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3567142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3577142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3587142529fSAntoine Tenart 	 */
3597142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3607142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3617142529fSAntoine Tenart 
3627142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3637142529fSAntoine Tenart 	 * default.
3647142529fSAntoine Tenart 	 */
365714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
366714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3677142529fSAntoine Tenart 
3687142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3697142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3707142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3717142529fSAntoine Tenart 	}
372a556c76aSAlexandre Belloni }
373a556c76aSAlexandre Belloni 
374eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
375eb4733d7SVladimir Oltean {
376eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
377eb4733d7SVladimir Oltean }
378eb4733d7SVladimir Oltean 
379eb4733d7SVladimir Oltean int ocelot_port_flush(struct ocelot *ocelot, int port)
380eb4733d7SVladimir Oltean {
381eb4733d7SVladimir Oltean 	int err, val;
382eb4733d7SVladimir Oltean 
383eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
384eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
385eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
386eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
387eb4733d7SVladimir Oltean 
388eb4733d7SVladimir Oltean 	/* Disable flow control */
389eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
390eb4733d7SVladimir Oltean 
391eb4733d7SVladimir Oltean 	/* Disable priority flow control */
392eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
393eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
394eb4733d7SVladimir Oltean 
395eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
396eb4733d7SVladimir Oltean 	 * at the port.
397eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
398eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
399eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
400eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
401eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
402eb4733d7SVladimir Oltean 	 */
403eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
404eb4733d7SVladimir Oltean 
405eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
406eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
407eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
408eb4733d7SVladimir Oltean 
409eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
410eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
411eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
412eb4733d7SVladimir Oltean 
413eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
414eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
415eb4733d7SVladimir Oltean 		       port);
416eb4733d7SVladimir Oltean 
417eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
418eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
419eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
420eb4733d7SVladimir Oltean 
421eb4733d7SVladimir Oltean 	/* Clear flushing again. */
422eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
423eb4733d7SVladimir Oltean 
424eb4733d7SVladimir Oltean 	return err;
425eb4733d7SVladimir Oltean }
426eb4733d7SVladimir Oltean EXPORT_SYMBOL(ocelot_port_flush);
427eb4733d7SVladimir Oltean 
4285e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
42926f4dbabSVladimir Oltean 			struct phy_device *phydev)
430a556c76aSAlexandre Belloni {
43126f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
4325bc9d2e6SVladimir Oltean 	int speed, mode = 0;
433a556c76aSAlexandre Belloni 
43426f4dbabSVladimir Oltean 	switch (phydev->speed) {
435a556c76aSAlexandre Belloni 	case SPEED_10:
436a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
437a556c76aSAlexandre Belloni 		break;
438a556c76aSAlexandre Belloni 	case SPEED_100:
439a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
440a556c76aSAlexandre Belloni 		break;
441a556c76aSAlexandre Belloni 	case SPEED_1000:
442a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
443a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
444a556c76aSAlexandre Belloni 		break;
445a556c76aSAlexandre Belloni 	case SPEED_2500:
446a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
447a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
448a556c76aSAlexandre Belloni 		break;
449a556c76aSAlexandre Belloni 	default:
45026f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
45126f4dbabSVladimir Oltean 			port, phydev->speed);
452a556c76aSAlexandre Belloni 		return;
453a556c76aSAlexandre Belloni 	}
454a556c76aSAlexandre Belloni 
45526f4dbabSVladimir Oltean 	phy_print_status(phydev);
456a556c76aSAlexandre Belloni 
45726f4dbabSVladimir Oltean 	if (!phydev->link)
458a556c76aSAlexandre Belloni 		return;
459a556c76aSAlexandre Belloni 
460a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
461004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
462a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
463a556c76aSAlexandre Belloni 
4641ba8f656SVladimir Oltean 	/* Disable HDX fast control */
4651ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
4661ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
4671ba8f656SVladimir Oltean 
4681ba8f656SVladimir Oltean 	/* SGMII only for now */
4691ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4701ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
4711ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4721ba8f656SVladimir Oltean 
4731ba8f656SVladimir Oltean 	/* Enable PCS */
4741ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4751ba8f656SVladimir Oltean 
4761ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4771ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4781ba8f656SVladimir Oltean 
4791ba8f656SVladimir Oltean 	/* No loopback */
4801ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
481a556c76aSAlexandre Belloni 
482a556c76aSAlexandre Belloni 	/* Enable MAC module */
483004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
484a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
485a556c76aSAlexandre Belloni 
486a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
487a556c76aSAlexandre Belloni 	 * reset */
488004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
489a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
490a556c76aSAlexandre Belloni 
491a556c76aSAlexandre Belloni 	/* No PFC */
492a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
493004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
494a556c76aSAlexandre Belloni 
495a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
496886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
497886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
498a556c76aSAlexandre Belloni 
499a556c76aSAlexandre Belloni 	/* Flow control */
500a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
501a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
502a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
503a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
504a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
505004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
506004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
507a556c76aSAlexandre Belloni }
5085e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
509a556c76aSAlexandre Belloni 
5105e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
511889b8950SVladimir Oltean 			struct phy_device *phy)
512a556c76aSAlexandre Belloni {
513a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
514a556c76aSAlexandre Belloni 	 * MAC addresses.
515a556c76aSAlexandre Belloni 	 */
516a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
517a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
518004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
519004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
520889b8950SVladimir Oltean }
5215e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
522889b8950SVladimir Oltean 
5235e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
524889b8950SVladimir Oltean {
525889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
526889b8950SVladimir Oltean 
527889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
528886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
529889b8950SVladimir Oltean }
5305e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
531889b8950SVladimir Oltean 
532e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
533e2f9a8feSVladimir Oltean 				  struct sk_buff *clone)
534400928bfSYangbo Lu {
535e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
536400928bfSYangbo Lu 
5376565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
5386565243cSVladimir Oltean 
539e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
540b049da13SYangbo Lu 	/* Store timestamp ID in cb[0] of sk_buff */
541e2f9a8feSVladimir Oltean 	clone->cb[0] = ocelot_port->ts_id;
5426565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
543e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
5446565243cSVladimir Oltean 
5456565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
546400928bfSYangbo Lu }
547400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
548400928bfSYangbo Lu 
549e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
550e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
5514e3b0468SAntoine Tenart {
5524e3b0468SAntoine Tenart 	unsigned long flags;
5534e3b0468SAntoine Tenart 	u32 val;
5544e3b0468SAntoine Tenart 
5554e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
5564e3b0468SAntoine Tenart 
5574e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
5584e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
5594e3b0468SAntoine Tenart 
5604e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
5614e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
5624e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
5634e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
5644e3b0468SAntoine Tenart 
5654e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
5664e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
5674e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
5684e3b0468SAntoine Tenart 
5694e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
5704e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
5714e3b0468SAntoine Tenart 		ts->tv_sec--;
5724e3b0468SAntoine Tenart 
5734e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
5744e3b0468SAntoine Tenart }
575e23a7b3eSYangbo Lu 
576e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
577e23a7b3eSYangbo Lu {
578e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
579e23a7b3eSYangbo Lu 
580e23a7b3eSYangbo Lu 	while (budget--) {
581b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
582e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
583e23a7b3eSYangbo Lu 		struct ocelot_port *port;
584e23a7b3eSYangbo Lu 		struct timespec64 ts;
585b049da13SYangbo Lu 		unsigned long flags;
586e23a7b3eSYangbo Lu 		u32 val, id, txport;
587e23a7b3eSYangbo Lu 
588e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
589e23a7b3eSYangbo Lu 
590e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
591e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
592e23a7b3eSYangbo Lu 			break;
593e23a7b3eSYangbo Lu 
594e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
595e23a7b3eSYangbo Lu 
596e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
597e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
598e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
599e23a7b3eSYangbo Lu 
600e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
601e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
602e23a7b3eSYangbo Lu 
603b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
604b049da13SYangbo Lu 
605b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
606b049da13SYangbo Lu 			if (skb->cb[0] != id)
607e23a7b3eSYangbo Lu 				continue;
608b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
609b049da13SYangbo Lu 			skb_match = skb;
610fc62c094SYangbo Lu 			break;
611e23a7b3eSYangbo Lu 		}
612e23a7b3eSYangbo Lu 
613b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
614b049da13SYangbo Lu 
6155fd82200Slaurent brando 		/* Get the h/w timestamp */
6165fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
617e23a7b3eSYangbo Lu 
618b049da13SYangbo Lu 		if (unlikely(!skb_match))
619e23a7b3eSYangbo Lu 			continue;
620e23a7b3eSYangbo Lu 
621e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
622e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
623e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
624e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
6255fd82200Slaurent brando 
6265fd82200Slaurent brando 		/* Next ts */
6275fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
628e23a7b3eSYangbo Lu 	}
629e23a7b3eSYangbo Lu }
630e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
6314e3b0468SAntoine Tenart 
632*924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
633*924ee317SVladimir Oltean 				u32 *rval)
634*924ee317SVladimir Oltean {
635*924ee317SVladimir Oltean 	u32 bytes_valid, val;
636*924ee317SVladimir Oltean 
637*924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
638*924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
639*924ee317SVladimir Oltean 		if (ifh)
640*924ee317SVladimir Oltean 			return -EIO;
641*924ee317SVladimir Oltean 
642*924ee317SVladimir Oltean 		do {
643*924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
644*924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
645*924ee317SVladimir Oltean 	}
646*924ee317SVladimir Oltean 
647*924ee317SVladimir Oltean 	switch (val) {
648*924ee317SVladimir Oltean 	case XTR_ABORT:
649*924ee317SVladimir Oltean 		return -EIO;
650*924ee317SVladimir Oltean 	case XTR_EOF_0:
651*924ee317SVladimir Oltean 	case XTR_EOF_1:
652*924ee317SVladimir Oltean 	case XTR_EOF_2:
653*924ee317SVladimir Oltean 	case XTR_EOF_3:
654*924ee317SVladimir Oltean 	case XTR_PRUNED:
655*924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
656*924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
657*924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
658*924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
659*924ee317SVladimir Oltean 		else
660*924ee317SVladimir Oltean 			*rval = val;
661*924ee317SVladimir Oltean 
662*924ee317SVladimir Oltean 		return bytes_valid;
663*924ee317SVladimir Oltean 	case XTR_ESCAPE:
664*924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
665*924ee317SVladimir Oltean 
666*924ee317SVladimir Oltean 		return 4;
667*924ee317SVladimir Oltean 	default:
668*924ee317SVladimir Oltean 		*rval = val;
669*924ee317SVladimir Oltean 
670*924ee317SVladimir Oltean 		return 4;
671*924ee317SVladimir Oltean 	}
672*924ee317SVladimir Oltean }
673*924ee317SVladimir Oltean 
674*924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
675*924ee317SVladimir Oltean {
676*924ee317SVladimir Oltean 	int i, err = 0;
677*924ee317SVladimir Oltean 
678*924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
679*924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
680*924ee317SVladimir Oltean 		if (err != 4)
681*924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
682*924ee317SVladimir Oltean 	}
683*924ee317SVladimir Oltean 
684*924ee317SVladimir Oltean 	return 0;
685*924ee317SVladimir Oltean }
686*924ee317SVladimir Oltean 
687*924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
688*924ee317SVladimir Oltean {
689*924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
690*924ee317SVladimir Oltean 	u64 tod_in_ns, full_ts_in_ns;
691*924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
692*924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
693*924ee317SVladimir Oltean 	struct net_device *dev;
694*924ee317SVladimir Oltean 	struct timespec64 ts;
695*924ee317SVladimir Oltean 	struct sk_buff *skb;
696*924ee317SVladimir Oltean 	int sz, buf_len;
697*924ee317SVladimir Oltean 	u32 val, *buf;
698*924ee317SVladimir Oltean 	int err;
699*924ee317SVladimir Oltean 
700*924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
701*924ee317SVladimir Oltean 	if (err)
702*924ee317SVladimir Oltean 		return err;
703*924ee317SVladimir Oltean 
704*924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
705*924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
706*924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
707*924ee317SVladimir Oltean 
708*924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
709*924ee317SVladimir Oltean 		return -EINVAL;
710*924ee317SVladimir Oltean 
711*924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
712*924ee317SVladimir Oltean 	if (!dev)
713*924ee317SVladimir Oltean 		return -EINVAL;
714*924ee317SVladimir Oltean 
715*924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
716*924ee317SVladimir Oltean 	if (unlikely(!skb)) {
717*924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
718*924ee317SVladimir Oltean 		return -ENOMEM;
719*924ee317SVladimir Oltean 	}
720*924ee317SVladimir Oltean 
721*924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
722*924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
723*924ee317SVladimir Oltean 
724*924ee317SVladimir Oltean 	len = 0;
725*924ee317SVladimir Oltean 	do {
726*924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
727*924ee317SVladimir Oltean 		if (sz < 0) {
728*924ee317SVladimir Oltean 			err = sz;
729*924ee317SVladimir Oltean 			goto out_free_skb;
730*924ee317SVladimir Oltean 		}
731*924ee317SVladimir Oltean 		*buf++ = val;
732*924ee317SVladimir Oltean 		len += sz;
733*924ee317SVladimir Oltean 	} while (len < buf_len);
734*924ee317SVladimir Oltean 
735*924ee317SVladimir Oltean 	/* Read the FCS */
736*924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
737*924ee317SVladimir Oltean 	if (sz < 0) {
738*924ee317SVladimir Oltean 		err = sz;
739*924ee317SVladimir Oltean 		goto out_free_skb;
740*924ee317SVladimir Oltean 	}
741*924ee317SVladimir Oltean 
742*924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
743*924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
744*924ee317SVladimir Oltean 
745*924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
746*924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
747*924ee317SVladimir Oltean 		*buf = val;
748*924ee317SVladimir Oltean 	}
749*924ee317SVladimir Oltean 
750*924ee317SVladimir Oltean 	if (ocelot->ptp) {
751*924ee317SVladimir Oltean 		ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
752*924ee317SVladimir Oltean 
753*924ee317SVladimir Oltean 		tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
754*924ee317SVladimir Oltean 		if ((tod_in_ns & 0xffffffff) < timestamp)
755*924ee317SVladimir Oltean 			full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
756*924ee317SVladimir Oltean 					timestamp;
757*924ee317SVladimir Oltean 		else
758*924ee317SVladimir Oltean 			full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
759*924ee317SVladimir Oltean 					timestamp;
760*924ee317SVladimir Oltean 
761*924ee317SVladimir Oltean 		shhwtstamps = skb_hwtstamps(skb);
762*924ee317SVladimir Oltean 		memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
763*924ee317SVladimir Oltean 		shhwtstamps->hwtstamp = full_ts_in_ns;
764*924ee317SVladimir Oltean 	}
765*924ee317SVladimir Oltean 
766*924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
767*924ee317SVladimir Oltean 	 * has already been forwarded.
768*924ee317SVladimir Oltean 	 */
769*924ee317SVladimir Oltean 	if (ocelot->bridge_mask & BIT(src_port))
770*924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
771*924ee317SVladimir Oltean 
772*924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
773*924ee317SVladimir Oltean 	*nskb = skb;
774*924ee317SVladimir Oltean 
775*924ee317SVladimir Oltean 	return 0;
776*924ee317SVladimir Oltean 
777*924ee317SVladimir Oltean out_free_skb:
778*924ee317SVladimir Oltean 	kfree_skb(skb);
779*924ee317SVladimir Oltean 	return err;
780*924ee317SVladimir Oltean }
781*924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
782*924ee317SVladimir Oltean 
783137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
784137ffbc4SVladimir Oltean {
785137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
786137ffbc4SVladimir Oltean 
787137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
788137ffbc4SVladimir Oltean 		return false;
789137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
790137ffbc4SVladimir Oltean 		return false;
791137ffbc4SVladimir Oltean 
792137ffbc4SVladimir Oltean 	return true;
793137ffbc4SVladimir Oltean }
794137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
795137ffbc4SVladimir Oltean 
796137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
797137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
798137ffbc4SVladimir Oltean {
79940d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
800137ffbc4SVladimir Oltean 	unsigned int i, count, last;
801137ffbc4SVladimir Oltean 
802137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
803137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
804137ffbc4SVladimir Oltean 
80540d3f295SVladimir Oltean 	ocelot_ifh_set_bypass(ifh, 1);
80640d3f295SVladimir Oltean 	ocelot_ifh_set_dest(ifh, BIT(port));
80740d3f295SVladimir Oltean 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
80840d3f295SVladimir Oltean 	ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
80940d3f295SVladimir Oltean 	ocelot_ifh_set_rew_op(ifh, rew_op);
810137ffbc4SVladimir Oltean 
811137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
81240d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
813137ffbc4SVladimir Oltean 
814137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
815137ffbc4SVladimir Oltean 	last = skb->len % 4;
816137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
817137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
818137ffbc4SVladimir Oltean 
819137ffbc4SVladimir Oltean 	/* Add padding */
820137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
821137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
822137ffbc4SVladimir Oltean 		i++;
823137ffbc4SVladimir Oltean 	}
824137ffbc4SVladimir Oltean 
825137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
826137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
827137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
828137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
829137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
830137ffbc4SVladimir Oltean 
831137ffbc4SVladimir Oltean 	/* Add dummy CRC */
832137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
833137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
834137ffbc4SVladimir Oltean 
835137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
836137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
837137ffbc4SVladimir Oltean }
838137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
839137ffbc4SVladimir Oltean 
8405e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
84187b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
842a556c76aSAlexandre Belloni {
843471beb11SVladimir Oltean 	int pgid = port;
844471beb11SVladimir Oltean 
845471beb11SVladimir Oltean 	if (port == ocelot->npi)
846471beb11SVladimir Oltean 		pgid = PGID_CPU;
847a556c76aSAlexandre Belloni 
848471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
849a556c76aSAlexandre Belloni }
8505e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
851a556c76aSAlexandre Belloni 
8525e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
853531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
854531ee1a6SVladimir Oltean {
855531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
856531ee1a6SVladimir Oltean }
8575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
858531ee1a6SVladimir Oltean 
8599c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
860531ee1a6SVladimir Oltean 			    bool is_static, void *data)
861a556c76aSAlexandre Belloni {
862531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
863a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
864a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
865a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
866a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
867a556c76aSAlexandre Belloni 
868a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
869a556c76aSAlexandre Belloni 		goto skip;
870a556c76aSAlexandre Belloni 
871a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
872a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
873a556c76aSAlexandre Belloni 	if (!nlh)
874a556c76aSAlexandre Belloni 		return -EMSGSIZE;
875a556c76aSAlexandre Belloni 
876a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
877a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
878a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
879a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
880a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
881a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
882a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
883531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
884a556c76aSAlexandre Belloni 
885531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
886a556c76aSAlexandre Belloni 		goto nla_put_failure;
887a556c76aSAlexandre Belloni 
888531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
889a556c76aSAlexandre Belloni 		goto nla_put_failure;
890a556c76aSAlexandre Belloni 
891a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
892a556c76aSAlexandre Belloni 
893a556c76aSAlexandre Belloni skip:
894a556c76aSAlexandre Belloni 	dump->idx++;
895a556c76aSAlexandre Belloni 	return 0;
896a556c76aSAlexandre Belloni 
897a556c76aSAlexandre Belloni nla_put_failure:
898a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
899a556c76aSAlexandre Belloni 	return -EMSGSIZE;
900a556c76aSAlexandre Belloni }
9019c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
902a556c76aSAlexandre Belloni 
903531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
904a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
905a556c76aSAlexandre Belloni {
906a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
907531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
908a556c76aSAlexandre Belloni 
909a556c76aSAlexandre Belloni 	/* Set row and column to read from */
910a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
911a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
912a556c76aSAlexandre Belloni 
913a556c76aSAlexandre Belloni 	/* Issue a read command */
914a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
915a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
916a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
917a556c76aSAlexandre Belloni 
918a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
919a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
920a556c76aSAlexandre Belloni 
921a556c76aSAlexandre Belloni 	/* Read the entry flags */
922a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
923a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
924a556c76aSAlexandre Belloni 		return -EINVAL;
925a556c76aSAlexandre Belloni 
926a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
927a556c76aSAlexandre Belloni 	 * do not report it.
928a556c76aSAlexandre Belloni 	 */
929a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
930531ee1a6SVladimir Oltean 	if (dst != port)
931a556c76aSAlexandre Belloni 		return -EINVAL;
932a556c76aSAlexandre Belloni 
933a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
934a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
935a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
936a556c76aSAlexandre Belloni 
937a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
938a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
939a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
940a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
941a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
942a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
943a556c76aSAlexandre Belloni 
944a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
945a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
946a556c76aSAlexandre Belloni 
947a556c76aSAlexandre Belloni 	return 0;
948a556c76aSAlexandre Belloni }
949a556c76aSAlexandre Belloni 
9505e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
951531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
952a556c76aSAlexandre Belloni {
953531ee1a6SVladimir Oltean 	int i, j;
954a556c76aSAlexandre Belloni 
95521ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
95621ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
957a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
958531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
959531ee1a6SVladimir Oltean 			bool is_static;
960531ee1a6SVladimir Oltean 			int ret;
961531ee1a6SVladimir Oltean 
962531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
963a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
964a556c76aSAlexandre Belloni 			 * skip it.
965a556c76aSAlexandre Belloni 			 */
966a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
967a556c76aSAlexandre Belloni 				continue;
968a556c76aSAlexandre Belloni 			else if (ret)
969531ee1a6SVladimir Oltean 				return ret;
970a556c76aSAlexandre Belloni 
971531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
972531ee1a6SVladimir Oltean 
973531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
974a556c76aSAlexandre Belloni 			if (ret)
975531ee1a6SVladimir Oltean 				return ret;
976a556c76aSAlexandre Belloni 		}
977a556c76aSAlexandre Belloni 	}
978a556c76aSAlexandre Belloni 
979531ee1a6SVladimir Oltean 	return 0;
980531ee1a6SVladimir Oltean }
9815e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
982531ee1a6SVladimir Oltean 
983f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
9844e3b0468SAntoine Tenart {
9854e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
9864e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
9874e3b0468SAntoine Tenart }
988f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
9894e3b0468SAntoine Tenart 
990f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
9914e3b0468SAntoine Tenart {
992306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
9934e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
9944e3b0468SAntoine Tenart 
9954e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
9964e3b0468SAntoine Tenart 		return -EFAULT;
9974e3b0468SAntoine Tenart 
9984e3b0468SAntoine Tenart 	/* reserved for future extensions */
9994e3b0468SAntoine Tenart 	if (cfg.flags)
10004e3b0468SAntoine Tenart 		return -EINVAL;
10014e3b0468SAntoine Tenart 
10024e3b0468SAntoine Tenart 	/* Tx type sanity check */
10034e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
10044e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1005306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
10064e3b0468SAntoine Tenart 		break;
10074e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
10084e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
10094e3b0468SAntoine Tenart 		 * need to update the origin time.
10104e3b0468SAntoine Tenart 		 */
1011306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
10124e3b0468SAntoine Tenart 		break;
10134e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1014306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
10154e3b0468SAntoine Tenart 		break;
10164e3b0468SAntoine Tenart 	default:
10174e3b0468SAntoine Tenart 		return -ERANGE;
10184e3b0468SAntoine Tenart 	}
10194e3b0468SAntoine Tenart 
10204e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
10214e3b0468SAntoine Tenart 
10224e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
10234e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
10244e3b0468SAntoine Tenart 		break;
10254e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
10264e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
10274e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
10284e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
10294e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
10304e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
10314e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
10324e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
10334e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
10344e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
10354e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
10364e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
10374e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
10384e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
10394e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
10404e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
10414e3b0468SAntoine Tenart 		break;
10424e3b0468SAntoine Tenart 	default:
10434e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
10444e3b0468SAntoine Tenart 		return -ERANGE;
10454e3b0468SAntoine Tenart 	}
10464e3b0468SAntoine Tenart 
10474e3b0468SAntoine Tenart 	/* Commit back the result & save it */
10484e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
10494e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
10504e3b0468SAntoine Tenart 
10514e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
10524e3b0468SAntoine Tenart }
1053f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
10544e3b0468SAntoine Tenart 
10555e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1056a556c76aSAlexandre Belloni {
1057a556c76aSAlexandre Belloni 	int i;
1058a556c76aSAlexandre Belloni 
1059a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1060a556c76aSAlexandre Belloni 		return;
1061a556c76aSAlexandre Belloni 
1062a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1063a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1064a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1065a556c76aSAlexandre Belloni }
10665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1067a556c76aSAlexandre Belloni 
10681e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1069a556c76aSAlexandre Belloni {
1070a556c76aSAlexandre Belloni 	int i, j;
1071a556c76aSAlexandre Belloni 
1072a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1073a556c76aSAlexandre Belloni 
1074a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1075a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1076a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1077a556c76aSAlexandre Belloni 
1078a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1079a556c76aSAlexandre Belloni 			u32 val;
1080a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1081a556c76aSAlexandre Belloni 
1082a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1083a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1084a556c76aSAlexandre Belloni 
1085a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1086a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1087a556c76aSAlexandre Belloni 
1088a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1089a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1090a556c76aSAlexandre Belloni 		}
1091a556c76aSAlexandre Belloni 	}
1092a556c76aSAlexandre Belloni 
10931e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
10941e1caa97SClaudiu Manoil }
10951e1caa97SClaudiu Manoil 
10961e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
10971e1caa97SClaudiu Manoil {
10981e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
10991e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
11001e1caa97SClaudiu Manoil 					     stats_work);
11011e1caa97SClaudiu Manoil 
11021e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
11031e1caa97SClaudiu Manoil 
1104a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1105a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1106a556c76aSAlexandre Belloni }
1107a556c76aSAlexandre Belloni 
11085e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1109a556c76aSAlexandre Belloni {
1110a556c76aSAlexandre Belloni 	int i;
1111a556c76aSAlexandre Belloni 
1112a556c76aSAlexandre Belloni 	/* check and update now */
11131e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1114a556c76aSAlexandre Belloni 
1115a556c76aSAlexandre Belloni 	/* Copy all counters */
1116a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1117004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1118a556c76aSAlexandre Belloni }
11195e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1120a556c76aSAlexandre Belloni 
11215e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1122c7282d38SVladimir Oltean {
1123a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1124a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1125c7282d38SVladimir Oltean 
1126a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1127a556c76aSAlexandre Belloni }
11285e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1129a556c76aSAlexandre Belloni 
11305e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1131c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1132c7282d38SVladimir Oltean {
11334e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
11344e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1135d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1136d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1137d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1138d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1139d2b09a8eSYangbo Lu 		return 0;
1140d2b09a8eSYangbo Lu 	}
11414e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
11424e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
11434e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
11444e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
11454e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
11464e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
11474e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
11484e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
11494e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
11504e3b0468SAntoine Tenart 
11514e3b0468SAntoine Tenart 	return 0;
11524e3b0468SAntoine Tenart }
11535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
11544e3b0468SAntoine Tenart 
115523ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
115623ca3b72SVladimir Oltean 				bool only_active_ports)
1157b80af659SVladimir Oltean {
1158b80af659SVladimir Oltean 	u32 mask = 0;
1159b80af659SVladimir Oltean 	int port;
1160b80af659SVladimir Oltean 
1161b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1162b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1163b80af659SVladimir Oltean 
1164b80af659SVladimir Oltean 		if (!ocelot_port)
1165b80af659SVladimir Oltean 			continue;
1166b80af659SVladimir Oltean 
116723ca3b72SVladimir Oltean 		if (ocelot_port->bond == bond) {
116823ca3b72SVladimir Oltean 			if (only_active_ports && !ocelot_port->lag_tx_active)
116923ca3b72SVladimir Oltean 				continue;
117023ca3b72SVladimir Oltean 
1171b80af659SVladimir Oltean 			mask |= BIT(port);
1172b80af659SVladimir Oltean 		}
117323ca3b72SVladimir Oltean 	}
1174b80af659SVladimir Oltean 
1175b80af659SVladimir Oltean 	return mask;
1176b80af659SVladimir Oltean }
1177b80af659SVladimir Oltean 
1178e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
11799b521250SVladimir Oltean {
1180e21268efSVladimir Oltean 	u32 mask = 0;
11819b521250SVladimir Oltean 	int port;
11829b521250SVladimir Oltean 
1183e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1184e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1185e21268efSVladimir Oltean 
1186e21268efSVladimir Oltean 		if (!ocelot_port)
1187e21268efSVladimir Oltean 			continue;
1188e21268efSVladimir Oltean 
1189e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
1190e21268efSVladimir Oltean 			mask |= BIT(port);
1191e21268efSVladimir Oltean 	}
1192e21268efSVladimir Oltean 
1193e21268efSVladimir Oltean 	return mask;
1194e21268efSVladimir Oltean }
1195e21268efSVladimir Oltean 
1196e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1197e21268efSVladimir Oltean {
1198e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
1199e21268efSVladimir Oltean 	int port;
1200e21268efSVladimir Oltean 
1201e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
1202e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
1203e21268efSVladimir Oltean 	 * those are bridged or standalone.
1204e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
1205e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
1206e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
1207e21268efSVladimir Oltean 	 */
1208e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1209e21268efSVladimir Oltean 
12109b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
12119b521250SVladimir Oltean 	 * a source for the other ports.
12129b521250SVladimir Oltean 	 */
12139b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1214e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1215e21268efSVladimir Oltean 		unsigned long mask;
1216e21268efSVladimir Oltean 
1217e21268efSVladimir Oltean 		if (!ocelot_port) {
1218e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1219e21268efSVladimir Oltean 			mask = 0;
1220e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1221e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1222e21268efSVladimir Oltean 			 * forward packets to all other ports except for
1223e21268efSVladimir Oltean 			 * themselves
1224e21268efSVladimir Oltean 			 */
1225e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1226e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
1227e21268efSVladimir Oltean 		} else if (ocelot->bridge_fwd_mask & BIT(port)) {
1228528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
12299b521250SVladimir Oltean 
1230e21268efSVladimir Oltean 			mask = ocelot->bridge_fwd_mask & ~BIT(port);
123123ca3b72SVladimir Oltean 			if (bond) {
123223ca3b72SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond,
123323ca3b72SVladimir Oltean 							      false);
123423ca3b72SVladimir Oltean 			}
12359b521250SVladimir Oltean 		} else {
1236e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1237e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1238e21268efSVladimir Oltean 			 * module otherwise.
1239e21268efSVladimir Oltean 			 */
1240e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
1241e21268efSVladimir Oltean 		}
1242e21268efSVladimir Oltean 
1243e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
12449b521250SVladimir Oltean 	}
12459b521250SVladimir Oltean }
1246e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
12479b521250SVladimir Oltean 
12485e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1249a556c76aSAlexandre Belloni {
1250421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1251a556c76aSAlexandre Belloni 	u32 port_cfg;
1252a556c76aSAlexandre Belloni 
12534bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
12544bda1415SVladimir Oltean 		return;
1255a556c76aSAlexandre Belloni 
12564bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1257a556c76aSAlexandre Belloni 
1258a556c76aSAlexandre Belloni 	switch (state) {
1259a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
12604bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
1261df561f66SGustavo A. R. Silva 		fallthrough;
1262a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1263421741eaSVladimir Oltean 		if (ocelot_port->learn_ena)
1264a556c76aSAlexandre Belloni 			port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1265a556c76aSAlexandre Belloni 		break;
1266a556c76aSAlexandre Belloni 
1267a556c76aSAlexandre Belloni 	default:
1268a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
12694bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
1270a556c76aSAlexandre Belloni 		break;
1271a556c76aSAlexandre Belloni 	}
1272a556c76aSAlexandre Belloni 
12734bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
1274a556c76aSAlexandre Belloni 
12759b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1276a556c76aSAlexandre Belloni }
12775e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1278a556c76aSAlexandre Belloni 
12795e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
12804bda1415SVladimir Oltean {
1281c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1282c0d7eccbSVladimir Oltean 
1283c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1284c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1285c0d7eccbSVladimir Oltean 	 */
1286c0d7eccbSVladimir Oltean 	if (!age_period)
1287c0d7eccbSVladimir Oltean 		age_period = 1;
1288c0d7eccbSVladimir Oltean 
1289c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1290a556c76aSAlexandre Belloni }
12915e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1292a556c76aSAlexandre Belloni 
1293a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1294a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1295a556c76aSAlexandre Belloni 						     u16 vid)
1296a556c76aSAlexandre Belloni {
1297a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1298a556c76aSAlexandre Belloni 
1299a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1300a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1301a556c76aSAlexandre Belloni 			return mc;
1302a556c76aSAlexandre Belloni 	}
1303a556c76aSAlexandre Belloni 
1304a556c76aSAlexandre Belloni 	return NULL;
1305a556c76aSAlexandre Belloni }
1306a556c76aSAlexandre Belloni 
13079403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
13089403c158SVladimir Oltean {
13099403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
13109403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
13119403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
13129403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
13137c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
13149403c158SVladimir Oltean }
13159403c158SVladimir Oltean 
1316e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1317e5d1f896SVladimir Oltean 					     unsigned long ports)
1318e5d1f896SVladimir Oltean {
1319e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1320e5d1f896SVladimir Oltean 
1321e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1322e5d1f896SVladimir Oltean 	if (!pgid)
1323e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1324e5d1f896SVladimir Oltean 
1325e5d1f896SVladimir Oltean 	pgid->ports = ports;
1326e5d1f896SVladimir Oltean 	pgid->index = index;
1327e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1328e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1329e5d1f896SVladimir Oltean 
1330e5d1f896SVladimir Oltean 	return pgid;
1331e5d1f896SVladimir Oltean }
1332e5d1f896SVladimir Oltean 
1333e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1334e5d1f896SVladimir Oltean {
1335e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1336e5d1f896SVladimir Oltean 		return;
1337e5d1f896SVladimir Oltean 
1338e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1339e5d1f896SVladimir Oltean 	kfree(pgid);
1340e5d1f896SVladimir Oltean }
1341e5d1f896SVladimir Oltean 
1342e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1343bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
13449403c158SVladimir Oltean {
1345e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1346e5d1f896SVladimir Oltean 	int index;
13479403c158SVladimir Oltean 
13489403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
13499403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
13509403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
13519403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
13529403c158SVladimir Oltean 	 */
1353bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1354bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1355e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
13569403c158SVladimir Oltean 
1357e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1358e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1359e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1360e5d1f896SVladimir Oltean 		 */
1361e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1362e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1363e5d1f896SVladimir Oltean 			return pgid;
1364e5d1f896SVladimir Oltean 		}
1365e5d1f896SVladimir Oltean 	}
1366e5d1f896SVladimir Oltean 
1367e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1368e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
13699403c158SVladimir Oltean 		bool used = false;
13709403c158SVladimir Oltean 
1371e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1372e5d1f896SVladimir Oltean 			if (pgid->index == index) {
13739403c158SVladimir Oltean 				used = true;
13749403c158SVladimir Oltean 				break;
13759403c158SVladimir Oltean 			}
13769403c158SVladimir Oltean 		}
13779403c158SVladimir Oltean 
13789403c158SVladimir Oltean 		if (!used)
1379e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
13809403c158SVladimir Oltean 	}
13819403c158SVladimir Oltean 
1382e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
13839403c158SVladimir Oltean }
13849403c158SVladimir Oltean 
13859403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1386bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
13879403c158SVladimir Oltean {
1388ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
13899403c158SVladimir Oltean 
1390bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
13919403c158SVladimir Oltean 		addr[0] = 0;
13929403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
13939403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1394bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
13959403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
13969403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
13979403c158SVladimir Oltean 	}
13989403c158SVladimir Oltean }
13999403c158SVladimir Oltean 
1400209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1401209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1402a556c76aSAlexandre Belloni {
1403a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1404004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1405e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1406a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1407a556c76aSAlexandre Belloni 
1408471beb11SVladimir Oltean 	if (port == ocelot->npi)
1409471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1410471beb11SVladimir Oltean 
1411a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1412a556c76aSAlexandre Belloni 	if (!mc) {
1413728e69aeSVladimir Oltean 		/* New entry */
1414bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1415bb8d53fdSVladimir Oltean 		if (!mc)
1416bb8d53fdSVladimir Oltean 			return -ENOMEM;
1417bb8d53fdSVladimir Oltean 
1418bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1419bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1420bb8d53fdSVladimir Oltean 		mc->vid = vid;
1421bb8d53fdSVladimir Oltean 
1422a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1423728e69aeSVladimir Oltean 	} else {
1424e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1425e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1426e5d1f896SVladimir Oltean 		 */
1427e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1428bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1429a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1430a556c76aSAlexandre Belloni 	}
1431a556c76aSAlexandre Belloni 
1432004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1433e5d1f896SVladimir Oltean 
1434e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1435e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1436e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1437e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1438e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1439e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1440e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1441e5d1f896SVladimir Oltean 	}
1442e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1443e5d1f896SVladimir Oltean 
1444bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1445a556c76aSAlexandre Belloni 
1446e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1447e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1448e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1449e5d1f896SVladimir Oltean 				 pgid->index);
1450e5d1f896SVladimir Oltean 
1451e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1452bb8d53fdSVladimir Oltean 				 mc->entry_type);
1453a556c76aSAlexandre Belloni }
1454209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1455a556c76aSAlexandre Belloni 
1456209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1457a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1458a556c76aSAlexandre Belloni {
1459a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1460004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1461e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1462a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1463a556c76aSAlexandre Belloni 
1464471beb11SVladimir Oltean 	if (port == ocelot->npi)
1465471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1466471beb11SVladimir Oltean 
1467a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1468a556c76aSAlexandre Belloni 	if (!mc)
1469a556c76aSAlexandre Belloni 		return -ENOENT;
1470a556c76aSAlexandre Belloni 
1471bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1472a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1473a556c76aSAlexandre Belloni 
1474e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1475004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1476a556c76aSAlexandre Belloni 	if (!mc->ports) {
1477a556c76aSAlexandre Belloni 		list_del(&mc->list);
1478a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1479a556c76aSAlexandre Belloni 		return 0;
1480a556c76aSAlexandre Belloni 	}
1481a556c76aSAlexandre Belloni 
1482e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1483e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1484e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1485e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1486e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1487e5d1f896SVladimir Oltean 
1488bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1489a556c76aSAlexandre Belloni 
1490e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1491e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1492e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1493e5d1f896SVladimir Oltean 				 pgid->index);
1494e5d1f896SVladimir Oltean 
1495e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1496bb8d53fdSVladimir Oltean 				 mc->entry_type);
1497a556c76aSAlexandre Belloni }
1498209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1499a556c76aSAlexandre Belloni 
15005e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1501a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1502a556c76aSAlexandre Belloni {
1503a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1504a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1505a556c76aSAlexandre Belloni 	} else {
1506a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1507a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1508a556c76aSAlexandre Belloni 			 * unsupported */
1509a556c76aSAlexandre Belloni 			return -ENODEV;
1510a556c76aSAlexandre Belloni 	}
1511a556c76aSAlexandre Belloni 
1512f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1513a556c76aSAlexandre Belloni 
1514a556c76aSAlexandre Belloni 	return 0;
1515a556c76aSAlexandre Belloni }
15165e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1517a556c76aSAlexandre Belloni 
15185e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1519a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1520a556c76aSAlexandre Belloni {
1521c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
15222e554a7aSVladimir Oltean 	int ret;
15232e554a7aSVladimir Oltean 
152497bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1525a556c76aSAlexandre Belloni 
1526a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1527a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
15287142529fSAntoine Tenart 
1529bae33f2bSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false);
15302e554a7aSVladimir Oltean 	if (ret)
15312e554a7aSVladimir Oltean 		return ret;
15322e554a7aSVladimir Oltean 
1533c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
15342f0402feSVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
15352f0402feSVladimir Oltean 
15362f0402feSVladimir Oltean 	return 0;
1537a556c76aSAlexandre Belloni }
15385e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1539a556c76aSAlexandre Belloni 
1540dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1541dc96ee37SAlexandre Belloni {
1542528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1543dc96ee37SAlexandre Belloni 	int i, port, lag;
1544dc96ee37SAlexandre Belloni 
1545dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
154696b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1547dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1548dc96ee37SAlexandre Belloni 
154996b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1550dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1551dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1552dc96ee37SAlexandre Belloni 
1553528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1554528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1555528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1556528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1557528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1558528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1559528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1560528d3f19SVladimir Oltean 	 */
1561528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1562528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1563528d3f19SVladimir Oltean 
1564528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1565528d3f19SVladimir Oltean 			continue;
1566528d3f19SVladimir Oltean 
1567528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1568528d3f19SVladimir Oltean 	}
1569528d3f19SVladimir Oltean 
1570528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1571dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1572528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
157323ca3b72SVladimir Oltean 		int num_active_ports = 0;
1574dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1575dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1576dc96ee37SAlexandre Belloni 
1577528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1578dc96ee37SAlexandre Belloni 			continue;
1579dc96ee37SAlexandre Belloni 
158023ca3b72SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1581528d3f19SVladimir Oltean 
1582dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1583dc96ee37SAlexandre Belloni 			// Destination mask
1584dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1585dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
158623ca3b72SVladimir Oltean 			aggr_idx[num_active_ports++] = port;
1587dc96ee37SAlexandre Belloni 		}
1588dc96ee37SAlexandre Belloni 
158996b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1590dc96ee37SAlexandre Belloni 			u32 ac;
1591dc96ee37SAlexandre Belloni 
1592dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1593dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
159423ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
159523ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
159623ca3b72SVladimir Oltean 			 */
159723ca3b72SVladimir Oltean 			if (num_active_ports)
159823ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1599dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1600dc96ee37SAlexandre Belloni 		}
1601528d3f19SVladimir Oltean 
1602528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1603528d3f19SVladimir Oltean 		 * the same config again.
1604528d3f19SVladimir Oltean 		 */
1605528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1606528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1607528d3f19SVladimir Oltean 
1608528d3f19SVladimir Oltean 			if (!ocelot_port)
1609528d3f19SVladimir Oltean 				continue;
1610528d3f19SVladimir Oltean 
1611528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1612528d3f19SVladimir Oltean 				visited |= BIT(port);
1613528d3f19SVladimir Oltean 		}
1614dc96ee37SAlexandre Belloni 	}
1615dc96ee37SAlexandre Belloni }
1616dc96ee37SAlexandre Belloni 
16172527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
16182527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
16192527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
16202527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
16212527f2e8SVladimir Oltean  */
16222527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1623dc96ee37SAlexandre Belloni {
16242527f2e8SVladimir Oltean 	int port;
1625dc96ee37SAlexandre Belloni 
16262527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
16272527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
16282527f2e8SVladimir Oltean 		struct net_device *bond;
1629dc96ee37SAlexandre Belloni 
16302527f2e8SVladimir Oltean 		if (!ocelot_port)
16312527f2e8SVladimir Oltean 			continue;
1632dc96ee37SAlexandre Belloni 
16332527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
16342527f2e8SVladimir Oltean 		if (bond) {
163523ca3b72SVladimir Oltean 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
163623ca3b72SVladimir Oltean 							     false));
16372527f2e8SVladimir Oltean 
16382527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
1639dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
16402527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
16412527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
16422527f2e8SVladimir Oltean 		} else {
16432527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
16442527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
16452527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
16462527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
16472527f2e8SVladimir Oltean 		}
1648dc96ee37SAlexandre Belloni 	}
1649dc96ee37SAlexandre Belloni }
1650dc96ee37SAlexandre Belloni 
16519c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1652583cbbe3SVladimir Oltean 			 struct net_device *bond,
1653583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
1654dc96ee37SAlexandre Belloni {
1655583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1656583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
1657583cbbe3SVladimir Oltean 
1658b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
1659dc96ee37SAlexandre Belloni 
16602527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
16619b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1662dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1663dc96ee37SAlexandre Belloni 
1664dc96ee37SAlexandre Belloni 	return 0;
1665dc96ee37SAlexandre Belloni }
16669c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1667dc96ee37SAlexandre Belloni 
16689c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1669dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1670dc96ee37SAlexandre Belloni {
1671b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
1672b80af659SVladimir Oltean 
16732527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
16749b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1675dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1676dc96ee37SAlexandre Belloni }
16779c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
16780e332c85SPetr Machata 
167923ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
168023ca3b72SVladimir Oltean {
168123ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
168223ca3b72SVladimir Oltean 
168323ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
168423ca3b72SVladimir Oltean 
168523ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
168623ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
168723ca3b72SVladimir Oltean }
168823ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
168923ca3b72SVladimir Oltean 
1690a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1691a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
16920b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
16930b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
16940b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1695a8015dedSVladimir Oltean  */
16960b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
169731350d7fSVladimir Oltean {
169831350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1699a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1700e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1701601e984fSVladimir Oltean 	int atop, atop_tot;
170231350d7fSVladimir Oltean 
17030b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
17040b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
17050b912fc9SVladimir Oltean 
1706cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
17070b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1708cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
17090b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
17100b912fc9SVladimir Oltean 	}
17110b912fc9SVladimir Oltean 
1712a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1713fa914e9cSVladimir Oltean 
1714e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1715e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1716e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1717541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1718541132f0SMaxim Kochetkov 			    pause_start);
1719541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1720541132f0SMaxim Kochetkov 			    pause_stop);
1721fa914e9cSVladimir Oltean 
1722601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1723f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1724a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1725601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1726601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1727601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1728fa914e9cSVladimir Oltean }
17290b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
17300b912fc9SVladimir Oltean 
17310b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
17320b912fc9SVladimir Oltean {
17330b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
17340b912fc9SVladimir Oltean 
17350b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
17360b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
17370b912fc9SVladimir Oltean 
1738cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
17390b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1740cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
17410b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
17420b912fc9SVladimir Oltean 	}
17430b912fc9SVladimir Oltean 
17440b912fc9SVladimir Oltean 	return max_mtu;
17450b912fc9SVladimir Oltean }
17460b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1747fa914e9cSVladimir Oltean 
1748421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
1749421741eaSVladimir Oltean 				     bool enabled)
1750421741eaSVladimir Oltean {
1751421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1752421741eaSVladimir Oltean 	u32 val = 0;
1753421741eaSVladimir Oltean 
1754421741eaSVladimir Oltean 	if (enabled)
1755421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
1756421741eaSVladimir Oltean 
1757421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
1758421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1759421741eaSVladimir Oltean 
1760421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
1761421741eaSVladimir Oltean }
1762421741eaSVladimir Oltean 
1763421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
1764421741eaSVladimir Oltean 					bool enabled)
1765421741eaSVladimir Oltean {
1766421741eaSVladimir Oltean 	u32 val = 0;
1767421741eaSVladimir Oltean 
1768421741eaSVladimir Oltean 	if (enabled)
1769421741eaSVladimir Oltean 		val = BIT(port);
1770421741eaSVladimir Oltean 
1771421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
1772421741eaSVladimir Oltean }
1773421741eaSVladimir Oltean 
1774421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
1775421741eaSVladimir Oltean 					bool enabled)
1776421741eaSVladimir Oltean {
1777421741eaSVladimir Oltean 	u32 val = 0;
1778421741eaSVladimir Oltean 
1779421741eaSVladimir Oltean 	if (enabled)
1780421741eaSVladimir Oltean 		val = BIT(port);
1781421741eaSVladimir Oltean 
1782421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
1783421741eaSVladimir Oltean }
1784421741eaSVladimir Oltean 
1785421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
1786421741eaSVladimir Oltean 					bool enabled)
1787421741eaSVladimir Oltean {
1788421741eaSVladimir Oltean 	u32 val = 0;
1789421741eaSVladimir Oltean 
1790421741eaSVladimir Oltean 	if (enabled)
1791421741eaSVladimir Oltean 		val = BIT(port);
1792421741eaSVladimir Oltean 
1793421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
1794421741eaSVladimir Oltean }
1795421741eaSVladimir Oltean 
1796421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
1797421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
1798421741eaSVladimir Oltean {
1799421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1800421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
1801421741eaSVladimir Oltean 		return -EINVAL;
1802421741eaSVladimir Oltean 
1803421741eaSVladimir Oltean 	return 0;
1804421741eaSVladimir Oltean }
1805421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
1806421741eaSVladimir Oltean 
1807421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
1808421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
1809421741eaSVladimir Oltean {
1810421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
1811421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
1812421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
1813421741eaSVladimir Oltean 
1814421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
1815421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
1816421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
1817421741eaSVladimir Oltean 
1818421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
1819421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
1820421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
1821421741eaSVladimir Oltean 
1822421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
1823421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
1824421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
1825421741eaSVladimir Oltean }
1826421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
1827421741eaSVladimir Oltean 
18285e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1829fa914e9cSVladimir Oltean {
1830fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1831fa914e9cSVladimir Oltean 
1832b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
18336565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
183431350d7fSVladimir Oltean 
183531350d7fSVladimir Oltean 	/* Basic L2 initialization */
183631350d7fSVladimir Oltean 
18375bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
18385bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
18395bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
18405bc9d2e6SVladimir Oltean 	 */
18415bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
18425bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
18435bc9d2e6SVladimir Oltean 
18445bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
18455bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
18465bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
18475bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
18485bc9d2e6SVladimir Oltean 	mdelay(1);
18495bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
18505bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
18515bc9d2e6SVladimir Oltean 
18525bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1853a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
18545bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
18555bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1856a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
18575bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
18585bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
18595bc9d2e6SVladimir Oltean 
18605bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
18615bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
18625bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
18635bc9d2e6SVladimir Oltean 
1864e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1865541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1866e8e6e73dSVladimir Oltean 
186731350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
186831350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
186931350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
187031350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
187131350d7fSVladimir Oltean 
187231350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
187331350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
187431350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
187531350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
187631350d7fSVladimir Oltean 
1877421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
1878421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
1879421741eaSVladimir Oltean 
188031350d7fSVladimir Oltean 	/* Enable vcap lookups */
188131350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
188231350d7fSVladimir Oltean }
18835e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
188431350d7fSVladimir Oltean 
18852d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
18862d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
18872d44b097SVladimir Oltean  * NPI mode is used).
188869df578cSVladimir Oltean  */
18892d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
189021468199SVladimir Oltean {
189169df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
189269df578cSVladimir Oltean 
189369df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
189421468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
189569df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
189669df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
189769df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
189869df578cSVladimir Oltean 	 */
189921468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
190021468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
190121468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
190221468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
190321468199SVladimir Oltean 
190469df578cSVladimir Oltean 	/* Enable CPU port module */
1905886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
190669df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1907886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
1908cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
1909886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
1910cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
191121468199SVladimir Oltean 
191221468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
191321468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
191421468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
191521468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
191621468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
191721468199SVladimir Oltean }
191821468199SVladimir Oltean 
1919f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
1920f6fe01d6SVladimir Oltean {
1921f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
1922f6fe01d6SVladimir Oltean 
1923f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
1924f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
1925f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
1926f6fe01d6SVladimir Oltean 	 */
1927f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
1928f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
1929f6fe01d6SVladimir Oltean 
1930f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
1931f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
1932f6fe01d6SVladimir Oltean }
1933f6fe01d6SVladimir Oltean 
1934a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1935a556c76aSAlexandre Belloni {
1936a556c76aSAlexandre Belloni 	char queue_name[32];
193721468199SVladimir Oltean 	int i, ret;
193821468199SVladimir Oltean 	u32 port;
1939a556c76aSAlexandre Belloni 
19403a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
19413a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
19423a77b593SVladimir Oltean 		if (ret) {
19433a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
19443a77b593SVladimir Oltean 			return ret;
19453a77b593SVladimir Oltean 		}
19463a77b593SVladimir Oltean 	}
19473a77b593SVladimir Oltean 
1948a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1949a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1950a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1951a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1952a556c76aSAlexandre Belloni 		return -ENOMEM;
1953a556c76aSAlexandre Belloni 
1954a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
19554e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
19564e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1957a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1958a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1959a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1960a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1961a556c76aSAlexandre Belloni 		return -ENOMEM;
1962a556c76aSAlexandre Belloni 
1963ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1964ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
1965ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
1966ca0b272bSVladimir Oltean 		return -ENOMEM;
1967ca0b272bSVladimir Oltean 	}
1968ca0b272bSVladimir Oltean 
19692b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1970e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
1971f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
1972a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1973a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1974aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
19752d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
1976a556c76aSAlexandre Belloni 
1977a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1978a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1979a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1980a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1981a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1982a556c76aSAlexandre Belloni 	}
1983a556c76aSAlexandre Belloni 
1984a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1985a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1986a556c76aSAlexandre Belloni 
1987a556c76aSAlexandre Belloni 	/* Aggregation mode */
1988a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1989a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1990a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1991f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
1992f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
1993f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
1994f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
1995a556c76aSAlexandre Belloni 
1996a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1997a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1998a556c76aSAlexandre Belloni 	 */
1999a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2000a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2001a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2002a556c76aSAlexandre Belloni 
2003a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2004a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2005a556c76aSAlexandre Belloni 
2006a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2007a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2008a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2009a556c76aSAlexandre Belloni 
2010a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2011edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2012a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2013b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2014a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2015edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2016a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2017a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2018a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2019a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2020a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2021a556c76aSAlexandre Belloni 
2022a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2023a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2024a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2025a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2026a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2027a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2028a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2029a556c76aSAlexandre Belloni 				 port);
2030a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2031a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2032a556c76aSAlexandre Belloni 	}
2033a556c76aSAlexandre Belloni 
203496b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2035a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2036a556c76aSAlexandre Belloni 
2037a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2038a556c76aSAlexandre Belloni 	}
2039b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2040b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2041b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2042a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2043b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2044b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2045b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2046a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2047a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2048a556c76aSAlexandre Belloni 
2049a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2050a556c76aSAlexandre Belloni 	 * registers endianness.
2051a556c76aSAlexandre Belloni 	 */
2052a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2053a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2054a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2055a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2056a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2057a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2058a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2059a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2060a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2061a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2062a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2063a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2064a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2065a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2066a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2067a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2068a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2069a556c76aSAlexandre Belloni 
20701e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2071a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2072a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
20734e3b0468SAntoine Tenart 
2074a556c76aSAlexandre Belloni 	return 0;
2075a556c76aSAlexandre Belloni }
2076a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2077a556c76aSAlexandre Belloni 
2078a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2079a556c76aSAlexandre Belloni {
2080c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2081a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2082ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2083a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2084a556c76aSAlexandre Belloni }
2085a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2086a556c76aSAlexandre Belloni 
2087e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2088e5fb512dSVladimir Oltean {
2089e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2090e5fb512dSVladimir Oltean 
2091e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2092e5fb512dSVladimir Oltean }
2093e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2094e5fb512dSVladimir Oltean 
2095a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2096