xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision cacea62fcdda5656cb5b8104e73a00e043b61730)
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  */
7a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
820968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
9a556c76aSAlexandre Belloni #include "ocelot.h"
103c83654fSVladimir Oltean #include "ocelot_vcap.h"
11a556c76aSAlexandre Belloni 
12639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
13639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
14639c1b26SSteen Hegelund 
15a556c76aSAlexandre Belloni struct ocelot_mact_entry {
16a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
17a556c76aSAlexandre Belloni 	u16 vid;
18a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
19a556c76aSAlexandre Belloni };
20a556c76aSAlexandre Belloni 
21639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
22639c1b26SSteen Hegelund {
23639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
24639c1b26SSteen Hegelund }
25639c1b26SSteen Hegelund 
26a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
27a556c76aSAlexandre Belloni {
28639c1b26SSteen Hegelund 	u32 val;
29a556c76aSAlexandre Belloni 
30639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
31639c1b26SSteen Hegelund 		ocelot, val,
32639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
33639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
34639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
35a556c76aSAlexandre Belloni }
36a556c76aSAlexandre Belloni 
37a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
38a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
39a556c76aSAlexandre Belloni 			       unsigned int vid)
40a556c76aSAlexandre Belloni {
41a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
42a556c76aSAlexandre Belloni 
43a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
44a556c76aSAlexandre Belloni 	 * understood by the hardware.
45a556c76aSAlexandre Belloni 	 */
46a556c76aSAlexandre Belloni 	mach |= vid    << 16;
47a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
48a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
49a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
50a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
51a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
52a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
53a556c76aSAlexandre Belloni 
54a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
55a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
56a556c76aSAlexandre Belloni 
57a556c76aSAlexandre Belloni }
58a556c76aSAlexandre Belloni 
599c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
60a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
619c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
62a556c76aSAlexandre Belloni {
63584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
64584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
65584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
66584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
67584b7cfcSAlban Bedel 	unsigned int mc_ports;
68584b7cfcSAlban Bedel 
69584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
70584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
71584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
72584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
73584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
74584b7cfcSAlban Bedel 	else
75584b7cfcSAlban Bedel 		mc_ports = 0;
76584b7cfcSAlban Bedel 
77584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
78584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
79584b7cfcSAlban Bedel 
80a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
81a556c76aSAlexandre Belloni 
82a556c76aSAlexandre Belloni 	/* Issue a write command */
83584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
84a556c76aSAlexandre Belloni 
85a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
86a556c76aSAlexandre Belloni }
879c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
88a556c76aSAlexandre Belloni 
899c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
909c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
91a556c76aSAlexandre Belloni {
92a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
93a556c76aSAlexandre Belloni 
94a556c76aSAlexandre Belloni 	/* Issue a forget command */
95a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
96a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
97a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
98a556c76aSAlexandre Belloni 
99a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
100a556c76aSAlexandre Belloni }
1019c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
102a556c76aSAlexandre Belloni 
103a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
104a556c76aSAlexandre Belloni {
105a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
106a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
107a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
108a556c76aSAlexandre Belloni 	 */
109a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
110a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
111a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
112a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
113a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
114a556c76aSAlexandre Belloni 
115a556c76aSAlexandre Belloni 	/* Clear the MAC table */
116a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
117a556c76aSAlexandre Belloni }
118a556c76aSAlexandre Belloni 
119f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
120b5962294SHoratiu Vultur {
121b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
122b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
123f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
12475944fdaSXiaoliang Yang 
12575944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
12675944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1272f17c050SXiaoliang Yang 
1282f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1292f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1302f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
131b5962294SHoratiu Vultur }
132b5962294SHoratiu Vultur 
133639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
134639c1b26SSteen Hegelund {
135639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
136639c1b26SSteen Hegelund }
137639c1b26SSteen Hegelund 
138a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
139a556c76aSAlexandre Belloni {
140639c1b26SSteen Hegelund 	u32 val;
141a556c76aSAlexandre Belloni 
142639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
143639c1b26SSteen Hegelund 		ocelot,
144639c1b26SSteen Hegelund 		val,
145639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
146639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
147639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
148a556c76aSAlexandre Belloni }
149a556c76aSAlexandre Belloni 
1507142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1517142529fSAntoine Tenart {
1527142529fSAntoine Tenart 	/* Select the VID to configure */
1537142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1547142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1557142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1567142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1577142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1587142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1597142529fSAntoine Tenart 
1607142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1617142529fSAntoine Tenart }
1627142529fSAntoine Tenart 
1632f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
164c3e58a75SVladimir Oltean 					struct ocelot_vlan native_vlan)
16597bb69e1SVladimir Oltean {
16697bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
16787b0f983SVladimir Oltean 	u32 val = 0;
16897bb69e1SVladimir Oltean 
169c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
17097bb69e1SVladimir Oltean 
171c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1727142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
17397bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17497bb69e1SVladimir Oltean 
17587b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
176e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
17787b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
17887b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
17987b0f983SVladimir Oltean 		else
18087b0f983SVladimir Oltean 			/* Tag all frames */
18187b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
18287b0f983SVladimir Oltean 	} else {
18387b0f983SVladimir Oltean 		/* Port tagging disabled. */
18487b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
18587b0f983SVladimir Oltean 	}
18687b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
18787b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
18887b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
18997bb69e1SVladimir Oltean }
19097bb69e1SVladimir Oltean 
19175e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
192c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
193c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
19475e5a554SVladimir Oltean {
19575e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
196be0576feSVladimir Oltean 	u32 val = 0;
19775e5a554SVladimir Oltean 
198c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
19975e5a554SVladimir Oltean 
20075e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
201c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
20275e5a554SVladimir Oltean 
20375e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
204c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
20575e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
20675e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
207be0576feSVladimir Oltean 
208be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
209be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
210be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
211be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
212be0576feSVladimir Oltean 	 */
213be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
214be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
215be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
216be0576feSVladimir Oltean 
217be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
218be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
219be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
220be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
22175e5a554SVladimir Oltean }
22275e5a554SVladimir Oltean 
2232e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
224bae33f2bSVladimir Oltean 			       bool vlan_aware)
22587b0f983SVladimir Oltean {
22670edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
227bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
22870edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
229bae33f2bSVladimir Oltean 	u32 val;
23070edfae1SVladimir Oltean 
23170edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
23270edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
23370edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
23470edfae1SVladimir Oltean 			dev_err(ocelot->dev,
23570edfae1SVladimir Oltean 				"Cannot change VLAN state with vlan modify rules active\n");
23670edfae1SVladimir Oltean 			return -EBUSY;
23770edfae1SVladimir Oltean 		}
23870edfae1SVladimir Oltean 	}
23970edfae1SVladimir Oltean 
24087b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
24187b0f983SVladimir Oltean 
24287b0f983SVladimir Oltean 	if (vlan_aware)
24387b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24487b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
24587b0f983SVladimir Oltean 	else
24687b0f983SVladimir Oltean 		val = 0;
24787b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
24887b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
25087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
25187b0f983SVladimir Oltean 
252c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
253c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2542e554a7aSVladimir Oltean 
2552e554a7aSVladimir Oltean 	return 0;
25687b0f983SVladimir Oltean }
25787b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
25887b0f983SVladimir Oltean 
2592f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2602f0402feSVladimir Oltean 			bool untagged)
2612f0402feSVladimir Oltean {
2622f0402feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2632f0402feSVladimir Oltean 
2642f0402feSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
2652f0402feSVladimir Oltean 	if (untagged && ocelot_port->native_vlan.vid != vid &&
2662f0402feSVladimir Oltean 	    ocelot_port->native_vlan.valid) {
2672f0402feSVladimir Oltean 		dev_err(ocelot->dev,
2682f0402feSVladimir Oltean 			"Port already has a native VLAN: %d\n",
2692f0402feSVladimir Oltean 			ocelot_port->native_vlan.vid);
2702f0402feSVladimir Oltean 		return -EBUSY;
2712f0402feSVladimir Oltean 	}
2722f0402feSVladimir Oltean 
2732f0402feSVladimir Oltean 	return 0;
2742f0402feSVladimir Oltean }
2752f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
2762f0402feSVladimir Oltean 
2775e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2787142529fSAntoine Tenart 		    bool untagged)
2797142529fSAntoine Tenart {
2807142529fSAntoine Tenart 	int ret;
2817142529fSAntoine Tenart 
2827142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
28397bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2847142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2857142529fSAntoine Tenart 	if (ret)
2867142529fSAntoine Tenart 		return ret;
2877142529fSAntoine Tenart 
2887142529fSAntoine Tenart 	/* Default ingress vlan classification */
289c3e58a75SVladimir Oltean 	if (pvid) {
290c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
291c3e58a75SVladimir Oltean 
292c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
293e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
294c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
295c3e58a75SVladimir Oltean 	}
2967142529fSAntoine Tenart 
2977142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
29897bb69e1SVladimir Oltean 	if (untagged) {
299c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
300c3e58a75SVladimir Oltean 
301c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
302e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
3032f0402feSVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
304b9cd75e6SVladimir Oltean 	}
3057142529fSAntoine Tenart 
3067142529fSAntoine Tenart 	return 0;
3077142529fSAntoine Tenart }
3085e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
3097142529fSAntoine Tenart 
3105e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3119855934cSVladimir Oltean {
3129855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3139855934cSVladimir Oltean 	int ret;
3147142529fSAntoine Tenart 
3157142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
31697bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3177142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3187142529fSAntoine Tenart 	if (ret)
3197142529fSAntoine Tenart 		return ret;
3207142529fSAntoine Tenart 
321be0576feSVladimir Oltean 	/* Ingress */
322be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
323be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
324be0576feSVladimir Oltean 
325be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
326be0576feSVladimir Oltean 	}
327be0576feSVladimir Oltean 
3287142529fSAntoine Tenart 	/* Egress */
329c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
330e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
331c3e58a75SVladimir Oltean 
332c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
333c3e58a75SVladimir Oltean 	}
3347142529fSAntoine Tenart 
3357142529fSAntoine Tenart 	return 0;
3367142529fSAntoine Tenart }
3375e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3387142529fSAntoine Tenart 
339a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
340a556c76aSAlexandre Belloni {
3417142529fSAntoine Tenart 	u16 port, vid;
3427142529fSAntoine Tenart 
343a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
344a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
345a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
346a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3477142529fSAntoine Tenart 
3487142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3497142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3507142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3517142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3527142529fSAntoine Tenart 	}
3537142529fSAntoine Tenart 
3547142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3557142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3567142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3577142529fSAntoine Tenart 	 */
3587142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3597142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3607142529fSAntoine Tenart 
3617142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3627142529fSAntoine Tenart 	 * default.
3637142529fSAntoine Tenart 	 */
364714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
365714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3667142529fSAntoine Tenart 
3677142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3687142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3697142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3707142529fSAntoine Tenart 	}
371a556c76aSAlexandre Belloni }
372a556c76aSAlexandre Belloni 
3735e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
37426f4dbabSVladimir Oltean 			struct phy_device *phydev)
375a556c76aSAlexandre Belloni {
37626f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3775bc9d2e6SVladimir Oltean 	int speed, mode = 0;
378a556c76aSAlexandre Belloni 
37926f4dbabSVladimir Oltean 	switch (phydev->speed) {
380a556c76aSAlexandre Belloni 	case SPEED_10:
381a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
382a556c76aSAlexandre Belloni 		break;
383a556c76aSAlexandre Belloni 	case SPEED_100:
384a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
385a556c76aSAlexandre Belloni 		break;
386a556c76aSAlexandre Belloni 	case SPEED_1000:
387a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
388a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
389a556c76aSAlexandre Belloni 		break;
390a556c76aSAlexandre Belloni 	case SPEED_2500:
391a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
392a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
393a556c76aSAlexandre Belloni 		break;
394a556c76aSAlexandre Belloni 	default:
39526f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
39626f4dbabSVladimir Oltean 			port, phydev->speed);
397a556c76aSAlexandre Belloni 		return;
398a556c76aSAlexandre Belloni 	}
399a556c76aSAlexandre Belloni 
40026f4dbabSVladimir Oltean 	phy_print_status(phydev);
401a556c76aSAlexandre Belloni 
40226f4dbabSVladimir Oltean 	if (!phydev->link)
403a556c76aSAlexandre Belloni 		return;
404a556c76aSAlexandre Belloni 
405a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
406004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
407a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
408a556c76aSAlexandre Belloni 
4091ba8f656SVladimir Oltean 	/* Disable HDX fast control */
4101ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
4111ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
4121ba8f656SVladimir Oltean 
4131ba8f656SVladimir Oltean 	/* SGMII only for now */
4141ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4151ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
4161ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4171ba8f656SVladimir Oltean 
4181ba8f656SVladimir Oltean 	/* Enable PCS */
4191ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4201ba8f656SVladimir Oltean 
4211ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4221ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4231ba8f656SVladimir Oltean 
4241ba8f656SVladimir Oltean 	/* No loopback */
4251ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
426a556c76aSAlexandre Belloni 
427a556c76aSAlexandre Belloni 	/* Enable MAC module */
428004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
429a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
430a556c76aSAlexandre Belloni 
431a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
432a556c76aSAlexandre Belloni 	 * reset */
433004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
434a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
435a556c76aSAlexandre Belloni 
436a556c76aSAlexandre Belloni 	/* No PFC */
437a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
438004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
439a556c76aSAlexandre Belloni 
440a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
441886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
442886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
443a556c76aSAlexandre Belloni 
444a556c76aSAlexandre Belloni 	/* Flow control */
445a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
446a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
447a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
448a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
449a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
450004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
451004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
452a556c76aSAlexandre Belloni }
4535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
454a556c76aSAlexandre Belloni 
4555e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
456889b8950SVladimir Oltean 			struct phy_device *phy)
457a556c76aSAlexandre Belloni {
458a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
459a556c76aSAlexandre Belloni 	 * MAC addresses.
460a556c76aSAlexandre Belloni 	 */
461a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
462a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
463004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
464004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
465889b8950SVladimir Oltean }
4665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
467889b8950SVladimir Oltean 
4685e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
469889b8950SVladimir Oltean {
470889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
471889b8950SVladimir Oltean 
472889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
473886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
474889b8950SVladimir Oltean }
4755e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
476889b8950SVladimir Oltean 
477e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
478e2f9a8feSVladimir Oltean 				  struct sk_buff *clone)
479400928bfSYangbo Lu {
480e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
481400928bfSYangbo Lu 
4826565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
4836565243cSVladimir Oltean 
484e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
485b049da13SYangbo Lu 	/* Store timestamp ID in cb[0] of sk_buff */
486e2f9a8feSVladimir Oltean 	clone->cb[0] = ocelot_port->ts_id;
4876565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
488e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
4896565243cSVladimir Oltean 
4906565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
491400928bfSYangbo Lu }
492400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
493400928bfSYangbo Lu 
494e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
495e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
4964e3b0468SAntoine Tenart {
4974e3b0468SAntoine Tenart 	unsigned long flags;
4984e3b0468SAntoine Tenart 	u32 val;
4994e3b0468SAntoine Tenart 
5004e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
5014e3b0468SAntoine Tenart 
5024e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
5034e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
5044e3b0468SAntoine Tenart 
5054e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
5064e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
5074e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
5084e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
5094e3b0468SAntoine Tenart 
5104e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
5114e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
5124e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
5134e3b0468SAntoine Tenart 
5144e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
5154e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
5164e3b0468SAntoine Tenart 		ts->tv_sec--;
5174e3b0468SAntoine Tenart 
5184e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
5194e3b0468SAntoine Tenart }
520e23a7b3eSYangbo Lu 
521e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
522e23a7b3eSYangbo Lu {
523e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
524e23a7b3eSYangbo Lu 
525e23a7b3eSYangbo Lu 	while (budget--) {
526b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
527e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
528e23a7b3eSYangbo Lu 		struct ocelot_port *port;
529e23a7b3eSYangbo Lu 		struct timespec64 ts;
530b049da13SYangbo Lu 		unsigned long flags;
531e23a7b3eSYangbo Lu 		u32 val, id, txport;
532e23a7b3eSYangbo Lu 
533e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
534e23a7b3eSYangbo Lu 
535e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
536e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
537e23a7b3eSYangbo Lu 			break;
538e23a7b3eSYangbo Lu 
539e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
540e23a7b3eSYangbo Lu 
541e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
542e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
543e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
544e23a7b3eSYangbo Lu 
545e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
546e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
547e23a7b3eSYangbo Lu 
548b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
549b049da13SYangbo Lu 
550b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
551b049da13SYangbo Lu 			if (skb->cb[0] != id)
552e23a7b3eSYangbo Lu 				continue;
553b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
554b049da13SYangbo Lu 			skb_match = skb;
555fc62c094SYangbo Lu 			break;
556e23a7b3eSYangbo Lu 		}
557e23a7b3eSYangbo Lu 
558b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
559b049da13SYangbo Lu 
5605fd82200Slaurent brando 		/* Get the h/w timestamp */
5615fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
562e23a7b3eSYangbo Lu 
563b049da13SYangbo Lu 		if (unlikely(!skb_match))
564e23a7b3eSYangbo Lu 			continue;
565e23a7b3eSYangbo Lu 
566e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
567e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
568e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
569e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
5705fd82200Slaurent brando 
5715fd82200Slaurent brando 		/* Next ts */
5725fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
573e23a7b3eSYangbo Lu 	}
574e23a7b3eSYangbo Lu }
575e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
5764e3b0468SAntoine Tenart 
5775e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
57887b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
579a556c76aSAlexandre Belloni {
580471beb11SVladimir Oltean 	int pgid = port;
581471beb11SVladimir Oltean 
582471beb11SVladimir Oltean 	if (port == ocelot->npi)
583471beb11SVladimir Oltean 		pgid = PGID_CPU;
584a556c76aSAlexandre Belloni 
585471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
586a556c76aSAlexandre Belloni }
5875e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
588a556c76aSAlexandre Belloni 
5895e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
590531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
591531ee1a6SVladimir Oltean {
592531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
593531ee1a6SVladimir Oltean }
5945e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
595531ee1a6SVladimir Oltean 
5969c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
597531ee1a6SVladimir Oltean 			    bool is_static, void *data)
598a556c76aSAlexandre Belloni {
599531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
600a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
601a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
602a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
603a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
604a556c76aSAlexandre Belloni 
605a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
606a556c76aSAlexandre Belloni 		goto skip;
607a556c76aSAlexandre Belloni 
608a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
609a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
610a556c76aSAlexandre Belloni 	if (!nlh)
611a556c76aSAlexandre Belloni 		return -EMSGSIZE;
612a556c76aSAlexandre Belloni 
613a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
614a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
615a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
616a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
617a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
618a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
619a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
620531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
621a556c76aSAlexandre Belloni 
622531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
623a556c76aSAlexandre Belloni 		goto nla_put_failure;
624a556c76aSAlexandre Belloni 
625531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
626a556c76aSAlexandre Belloni 		goto nla_put_failure;
627a556c76aSAlexandre Belloni 
628a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
629a556c76aSAlexandre Belloni 
630a556c76aSAlexandre Belloni skip:
631a556c76aSAlexandre Belloni 	dump->idx++;
632a556c76aSAlexandre Belloni 	return 0;
633a556c76aSAlexandre Belloni 
634a556c76aSAlexandre Belloni nla_put_failure:
635a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
636a556c76aSAlexandre Belloni 	return -EMSGSIZE;
637a556c76aSAlexandre Belloni }
6389c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
639a556c76aSAlexandre Belloni 
640531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
641a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
642a556c76aSAlexandre Belloni {
643a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
644531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
645a556c76aSAlexandre Belloni 
646a556c76aSAlexandre Belloni 	/* Set row and column to read from */
647a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
648a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
649a556c76aSAlexandre Belloni 
650a556c76aSAlexandre Belloni 	/* Issue a read command */
651a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
652a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
653a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
654a556c76aSAlexandre Belloni 
655a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
656a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
657a556c76aSAlexandre Belloni 
658a556c76aSAlexandre Belloni 	/* Read the entry flags */
659a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
660a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
661a556c76aSAlexandre Belloni 		return -EINVAL;
662a556c76aSAlexandre Belloni 
663a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
664a556c76aSAlexandre Belloni 	 * do not report it.
665a556c76aSAlexandre Belloni 	 */
666a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
667531ee1a6SVladimir Oltean 	if (dst != port)
668a556c76aSAlexandre Belloni 		return -EINVAL;
669a556c76aSAlexandre Belloni 
670a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
671a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
672a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
673a556c76aSAlexandre Belloni 
674a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
675a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
676a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
677a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
678a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
679a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
680a556c76aSAlexandre Belloni 
681a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
682a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
683a556c76aSAlexandre Belloni 
684a556c76aSAlexandre Belloni 	return 0;
685a556c76aSAlexandre Belloni }
686a556c76aSAlexandre Belloni 
6875e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
688531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
689a556c76aSAlexandre Belloni {
690531ee1a6SVladimir Oltean 	int i, j;
691a556c76aSAlexandre Belloni 
69221ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
69321ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
694a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
695531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
696531ee1a6SVladimir Oltean 			bool is_static;
697531ee1a6SVladimir Oltean 			int ret;
698531ee1a6SVladimir Oltean 
699531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
700a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
701a556c76aSAlexandre Belloni 			 * skip it.
702a556c76aSAlexandre Belloni 			 */
703a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
704a556c76aSAlexandre Belloni 				continue;
705a556c76aSAlexandre Belloni 			else if (ret)
706531ee1a6SVladimir Oltean 				return ret;
707a556c76aSAlexandre Belloni 
708531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
709531ee1a6SVladimir Oltean 
710531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
711a556c76aSAlexandre Belloni 			if (ret)
712531ee1a6SVladimir Oltean 				return ret;
713a556c76aSAlexandre Belloni 		}
714a556c76aSAlexandre Belloni 	}
715a556c76aSAlexandre Belloni 
716531ee1a6SVladimir Oltean 	return 0;
717531ee1a6SVladimir Oltean }
7185e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
719531ee1a6SVladimir Oltean 
720f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
7214e3b0468SAntoine Tenart {
7224e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
7234e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
7244e3b0468SAntoine Tenart }
725f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
7264e3b0468SAntoine Tenart 
727f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
7284e3b0468SAntoine Tenart {
729306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
7304e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
7314e3b0468SAntoine Tenart 
7324e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
7334e3b0468SAntoine Tenart 		return -EFAULT;
7344e3b0468SAntoine Tenart 
7354e3b0468SAntoine Tenart 	/* reserved for future extensions */
7364e3b0468SAntoine Tenart 	if (cfg.flags)
7374e3b0468SAntoine Tenart 		return -EINVAL;
7384e3b0468SAntoine Tenart 
7394e3b0468SAntoine Tenart 	/* Tx type sanity check */
7404e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
7414e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
742306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
7434e3b0468SAntoine Tenart 		break;
7444e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
7454e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
7464e3b0468SAntoine Tenart 		 * need to update the origin time.
7474e3b0468SAntoine Tenart 		 */
748306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
7494e3b0468SAntoine Tenart 		break;
7504e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
751306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
7524e3b0468SAntoine Tenart 		break;
7534e3b0468SAntoine Tenart 	default:
7544e3b0468SAntoine Tenart 		return -ERANGE;
7554e3b0468SAntoine Tenart 	}
7564e3b0468SAntoine Tenart 
7574e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
7584e3b0468SAntoine Tenart 
7594e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
7604e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
7614e3b0468SAntoine Tenart 		break;
7624e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
7634e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
7644e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
7654e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
7664e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
7674e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
7684e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
7694e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
7704e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
7714e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
7724e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
7734e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
7744e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
7754e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
7764e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
7774e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
7784e3b0468SAntoine Tenart 		break;
7794e3b0468SAntoine Tenart 	default:
7804e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
7814e3b0468SAntoine Tenart 		return -ERANGE;
7824e3b0468SAntoine Tenart 	}
7834e3b0468SAntoine Tenart 
7844e3b0468SAntoine Tenart 	/* Commit back the result & save it */
7854e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
7864e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
7874e3b0468SAntoine Tenart 
7884e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
7894e3b0468SAntoine Tenart }
790f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
7914e3b0468SAntoine Tenart 
7925e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
793a556c76aSAlexandre Belloni {
794a556c76aSAlexandre Belloni 	int i;
795a556c76aSAlexandre Belloni 
796a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
797a556c76aSAlexandre Belloni 		return;
798a556c76aSAlexandre Belloni 
799a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
800a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
801a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
802a556c76aSAlexandre Belloni }
8035e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
804a556c76aSAlexandre Belloni 
8051e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
806a556c76aSAlexandre Belloni {
807a556c76aSAlexandre Belloni 	int i, j;
808a556c76aSAlexandre Belloni 
809a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
810a556c76aSAlexandre Belloni 
811a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
812a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
813a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
814a556c76aSAlexandre Belloni 
815a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
816a556c76aSAlexandre Belloni 			u32 val;
817a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
818a556c76aSAlexandre Belloni 
819a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
820a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
821a556c76aSAlexandre Belloni 
822a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
823a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
824a556c76aSAlexandre Belloni 
825a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
826a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
827a556c76aSAlexandre Belloni 		}
828a556c76aSAlexandre Belloni 	}
829a556c76aSAlexandre Belloni 
8301e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
8311e1caa97SClaudiu Manoil }
8321e1caa97SClaudiu Manoil 
8331e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
8341e1caa97SClaudiu Manoil {
8351e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
8361e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
8371e1caa97SClaudiu Manoil 					     stats_work);
8381e1caa97SClaudiu Manoil 
8391e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
8401e1caa97SClaudiu Manoil 
841a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
842a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
843a556c76aSAlexandre Belloni }
844a556c76aSAlexandre Belloni 
8455e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
846a556c76aSAlexandre Belloni {
847a556c76aSAlexandre Belloni 	int i;
848a556c76aSAlexandre Belloni 
849a556c76aSAlexandre Belloni 	/* check and update now */
8501e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
851a556c76aSAlexandre Belloni 
852a556c76aSAlexandre Belloni 	/* Copy all counters */
853a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
854004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
855a556c76aSAlexandre Belloni }
8565e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
857a556c76aSAlexandre Belloni 
8585e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
859c7282d38SVladimir Oltean {
860a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
861a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
862c7282d38SVladimir Oltean 
863a556c76aSAlexandre Belloni 	return ocelot->num_stats;
864a556c76aSAlexandre Belloni }
8655e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
866a556c76aSAlexandre Belloni 
8675e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
868c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
869c7282d38SVladimir Oltean {
8704e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
8714e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
872d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
873d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
874d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
875d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
876d2b09a8eSYangbo Lu 		return 0;
877d2b09a8eSYangbo Lu 	}
8784e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
8794e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
8804e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
8814e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
8824e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
8834e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
8844e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
8854e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
8864e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
8874e3b0468SAntoine Tenart 
8884e3b0468SAntoine Tenart 	return 0;
8894e3b0468SAntoine Tenart }
8905e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
8914e3b0468SAntoine Tenart 
8929b521250SVladimir Oltean static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
8939b521250SVladimir Oltean {
8949b521250SVladimir Oltean 	int port;
8959b521250SVladimir Oltean 
8969b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
8979b521250SVladimir Oltean 	 * a source for the other ports.
8989b521250SVladimir Oltean 	 */
8999b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
9009b521250SVladimir Oltean 		if (ocelot->bridge_fwd_mask & BIT(port)) {
9019b521250SVladimir Oltean 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
9029b521250SVladimir Oltean 			int lag;
9039b521250SVladimir Oltean 
9049b521250SVladimir Oltean 			for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
9059b521250SVladimir Oltean 				unsigned long bond_mask = ocelot->lags[lag];
9069b521250SVladimir Oltean 
9079b521250SVladimir Oltean 				if (!bond_mask)
9089b521250SVladimir Oltean 					continue;
9099b521250SVladimir Oltean 
9109b521250SVladimir Oltean 				if (bond_mask & BIT(port)) {
9119b521250SVladimir Oltean 					mask &= ~bond_mask;
9129b521250SVladimir Oltean 					break;
9139b521250SVladimir Oltean 				}
9149b521250SVladimir Oltean 			}
9159b521250SVladimir Oltean 
9169b521250SVladimir Oltean 			ocelot_write_rix(ocelot, mask,
9179b521250SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + port);
9189b521250SVladimir Oltean 		} else {
9199b521250SVladimir Oltean 			ocelot_write_rix(ocelot, 0,
9209b521250SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + port);
9219b521250SVladimir Oltean 		}
9229b521250SVladimir Oltean 	}
9239b521250SVladimir Oltean }
9249b521250SVladimir Oltean 
9255e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
926a556c76aSAlexandre Belloni {
927a556c76aSAlexandre Belloni 	u32 port_cfg;
928a556c76aSAlexandre Belloni 
9294bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
9304bda1415SVladimir Oltean 		return;
931a556c76aSAlexandre Belloni 
9324bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
933a556c76aSAlexandre Belloni 
934a556c76aSAlexandre Belloni 	switch (state) {
935a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
9364bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
937df561f66SGustavo A. R. Silva 		fallthrough;
938a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
939a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
940a556c76aSAlexandre Belloni 		break;
941a556c76aSAlexandre Belloni 
942a556c76aSAlexandre Belloni 	default:
943a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
9444bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
945a556c76aSAlexandre Belloni 		break;
946a556c76aSAlexandre Belloni 	}
947a556c76aSAlexandre Belloni 
9484bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
949a556c76aSAlexandre Belloni 
9509b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
951a556c76aSAlexandre Belloni }
9525e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
953a556c76aSAlexandre Belloni 
9545e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
9554bda1415SVladimir Oltean {
956c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
957c0d7eccbSVladimir Oltean 
958c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
959c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
960c0d7eccbSVladimir Oltean 	 */
961c0d7eccbSVladimir Oltean 	if (!age_period)
962c0d7eccbSVladimir Oltean 		age_period = 1;
963c0d7eccbSVladimir Oltean 
964c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
965a556c76aSAlexandre Belloni }
9665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
967a556c76aSAlexandre Belloni 
968a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
969a556c76aSAlexandre Belloni 						     const unsigned char *addr,
970a556c76aSAlexandre Belloni 						     u16 vid)
971a556c76aSAlexandre Belloni {
972a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
973a556c76aSAlexandre Belloni 
974a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
975a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
976a556c76aSAlexandre Belloni 			return mc;
977a556c76aSAlexandre Belloni 	}
978a556c76aSAlexandre Belloni 
979a556c76aSAlexandre Belloni 	return NULL;
980a556c76aSAlexandre Belloni }
981a556c76aSAlexandre Belloni 
9829403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
9839403c158SVladimir Oltean {
9849403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
9859403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
9869403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
9879403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
9887c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
9899403c158SVladimir Oltean }
9909403c158SVladimir Oltean 
991e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
992e5d1f896SVladimir Oltean 					     unsigned long ports)
993e5d1f896SVladimir Oltean {
994e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
995e5d1f896SVladimir Oltean 
996e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
997e5d1f896SVladimir Oltean 	if (!pgid)
998e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
999e5d1f896SVladimir Oltean 
1000e5d1f896SVladimir Oltean 	pgid->ports = ports;
1001e5d1f896SVladimir Oltean 	pgid->index = index;
1002e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1003e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1004e5d1f896SVladimir Oltean 
1005e5d1f896SVladimir Oltean 	return pgid;
1006e5d1f896SVladimir Oltean }
1007e5d1f896SVladimir Oltean 
1008e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1009e5d1f896SVladimir Oltean {
1010e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1011e5d1f896SVladimir Oltean 		return;
1012e5d1f896SVladimir Oltean 
1013e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1014e5d1f896SVladimir Oltean 	kfree(pgid);
1015e5d1f896SVladimir Oltean }
1016e5d1f896SVladimir Oltean 
1017e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1018bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
10199403c158SVladimir Oltean {
1020e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1021e5d1f896SVladimir Oltean 	int index;
10229403c158SVladimir Oltean 
10239403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
10249403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
10259403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
10269403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
10279403c158SVladimir Oltean 	 */
1028bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1029bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1030e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
10319403c158SVladimir Oltean 
1032e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1033e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1034e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1035e5d1f896SVladimir Oltean 		 */
1036e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1037e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1038e5d1f896SVladimir Oltean 			return pgid;
1039e5d1f896SVladimir Oltean 		}
1040e5d1f896SVladimir Oltean 	}
1041e5d1f896SVladimir Oltean 
1042e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1043e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
10449403c158SVladimir Oltean 		bool used = false;
10459403c158SVladimir Oltean 
1046e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1047e5d1f896SVladimir Oltean 			if (pgid->index == index) {
10489403c158SVladimir Oltean 				used = true;
10499403c158SVladimir Oltean 				break;
10509403c158SVladimir Oltean 			}
10519403c158SVladimir Oltean 		}
10529403c158SVladimir Oltean 
10539403c158SVladimir Oltean 		if (!used)
1054e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
10559403c158SVladimir Oltean 	}
10569403c158SVladimir Oltean 
1057e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
10589403c158SVladimir Oltean }
10599403c158SVladimir Oltean 
10609403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1061bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
10629403c158SVladimir Oltean {
1063ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
10649403c158SVladimir Oltean 
1065bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
10669403c158SVladimir Oltean 		addr[0] = 0;
10679403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
10689403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1069bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
10709403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
10719403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
10729403c158SVladimir Oltean 	}
10739403c158SVladimir Oltean }
10749403c158SVladimir Oltean 
1075209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1076209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1077a556c76aSAlexandre Belloni {
1078a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1079004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1080e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1081a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1082a556c76aSAlexandre Belloni 
1083471beb11SVladimir Oltean 	if (port == ocelot->npi)
1084471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1085471beb11SVladimir Oltean 
1086a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1087a556c76aSAlexandre Belloni 	if (!mc) {
1088728e69aeSVladimir Oltean 		/* New entry */
1089bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1090bb8d53fdSVladimir Oltean 		if (!mc)
1091bb8d53fdSVladimir Oltean 			return -ENOMEM;
1092bb8d53fdSVladimir Oltean 
1093bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1094bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1095bb8d53fdSVladimir Oltean 		mc->vid = vid;
1096bb8d53fdSVladimir Oltean 
1097a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1098728e69aeSVladimir Oltean 	} else {
1099e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1100e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1101e5d1f896SVladimir Oltean 		 */
1102e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1103bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1104a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1105a556c76aSAlexandre Belloni 	}
1106a556c76aSAlexandre Belloni 
1107004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1108e5d1f896SVladimir Oltean 
1109e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1110e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1111e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1112e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1113e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1114e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1115e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1116e5d1f896SVladimir Oltean 	}
1117e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1118e5d1f896SVladimir Oltean 
1119bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1120a556c76aSAlexandre Belloni 
1121e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1122e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1123e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1124e5d1f896SVladimir Oltean 				 pgid->index);
1125e5d1f896SVladimir Oltean 
1126e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1127bb8d53fdSVladimir Oltean 				 mc->entry_type);
1128a556c76aSAlexandre Belloni }
1129209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1130a556c76aSAlexandre Belloni 
1131209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1132a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1133a556c76aSAlexandre Belloni {
1134a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1135004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1136e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1137a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1138a556c76aSAlexandre Belloni 
1139471beb11SVladimir Oltean 	if (port == ocelot->npi)
1140471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1141471beb11SVladimir Oltean 
1142a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1143a556c76aSAlexandre Belloni 	if (!mc)
1144a556c76aSAlexandre Belloni 		return -ENOENT;
1145a556c76aSAlexandre Belloni 
1146bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1147a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1148a556c76aSAlexandre Belloni 
1149e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1150004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1151a556c76aSAlexandre Belloni 	if (!mc->ports) {
1152a556c76aSAlexandre Belloni 		list_del(&mc->list);
1153a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1154a556c76aSAlexandre Belloni 		return 0;
1155a556c76aSAlexandre Belloni 	}
1156a556c76aSAlexandre Belloni 
1157e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1158e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1159e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1160e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1161e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1162e5d1f896SVladimir Oltean 
1163bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1164a556c76aSAlexandre Belloni 
1165e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1166e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1167e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1168e5d1f896SVladimir Oltean 				 pgid->index);
1169e5d1f896SVladimir Oltean 
1170e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1171bb8d53fdSVladimir Oltean 				 mc->entry_type);
1172a556c76aSAlexandre Belloni }
1173209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1174a556c76aSAlexandre Belloni 
11755e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1176a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1177a556c76aSAlexandre Belloni {
1178a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1179a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1180a556c76aSAlexandre Belloni 	} else {
1181a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1182a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1183a556c76aSAlexandre Belloni 			 * unsupported */
1184a556c76aSAlexandre Belloni 			return -ENODEV;
1185a556c76aSAlexandre Belloni 	}
1186a556c76aSAlexandre Belloni 
1187f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1188a556c76aSAlexandre Belloni 
1189a556c76aSAlexandre Belloni 	return 0;
1190a556c76aSAlexandre Belloni }
11915e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1192a556c76aSAlexandre Belloni 
11935e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1194a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1195a556c76aSAlexandre Belloni {
1196c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
11972e554a7aSVladimir Oltean 	int ret;
11982e554a7aSVladimir Oltean 
119997bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1200a556c76aSAlexandre Belloni 
1201a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1202a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
12037142529fSAntoine Tenart 
1204bae33f2bSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false);
12052e554a7aSVladimir Oltean 	if (ret)
12062e554a7aSVladimir Oltean 		return ret;
12072e554a7aSVladimir Oltean 
1208c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
12092f0402feSVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
12102f0402feSVladimir Oltean 
12112f0402feSVladimir Oltean 	return 0;
1212a556c76aSAlexandre Belloni }
12135e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1214a556c76aSAlexandre Belloni 
1215dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1216dc96ee37SAlexandre Belloni {
1217dc96ee37SAlexandre Belloni 	int i, port, lag;
1218dc96ee37SAlexandre Belloni 
1219dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
122096b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1221dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1222dc96ee37SAlexandre Belloni 
122396b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1224dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1225dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1226dc96ee37SAlexandre Belloni 
1227dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1228dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1229dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1230dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1231dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1232dc96ee37SAlexandre Belloni 
1233dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1234dc96ee37SAlexandre Belloni 		if (!bond_mask)
1235dc96ee37SAlexandre Belloni 			continue;
1236dc96ee37SAlexandre Belloni 
1237dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1238dc96ee37SAlexandre Belloni 			// Destination mask
1239dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1240dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1241dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1242dc96ee37SAlexandre Belloni 			aggr_count++;
1243dc96ee37SAlexandre Belloni 		}
1244dc96ee37SAlexandre Belloni 
124596b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1246dc96ee37SAlexandre Belloni 			u32 ac;
1247dc96ee37SAlexandre Belloni 
1248dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1249dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1250dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1251dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1252dc96ee37SAlexandre Belloni 		}
1253dc96ee37SAlexandre Belloni 	}
1254dc96ee37SAlexandre Belloni }
1255dc96ee37SAlexandre Belloni 
1256dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1257dc96ee37SAlexandre Belloni {
1258dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1259dc96ee37SAlexandre Belloni 	unsigned int p;
1260dc96ee37SAlexandre Belloni 
1261dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1262dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1263dc96ee37SAlexandre Belloni 
1264dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1265dc96ee37SAlexandre Belloni 
1266dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1267dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1268dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1269dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1270dc96ee37SAlexandre Belloni 	}
1271dc96ee37SAlexandre Belloni }
1272dc96ee37SAlexandre Belloni 
12739c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1274dc96ee37SAlexandre Belloni 			 struct net_device *bond)
1275dc96ee37SAlexandre Belloni {
1276dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1277dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1278f270dbfaSVladimir Oltean 	int lag, lp;
1279dc96ee37SAlexandre Belloni 
1280dc96ee37SAlexandre Belloni 	rcu_read_lock();
1281dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1282004d44f6SVladimir Oltean 		struct ocelot_port_private *priv = netdev_priv(ndev);
1283dc96ee37SAlexandre Belloni 
1284004d44f6SVladimir Oltean 		bond_mask |= BIT(priv->chip_port);
1285dc96ee37SAlexandre Belloni 	}
1286dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1287dc96ee37SAlexandre Belloni 
1288dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1289dc96ee37SAlexandre Belloni 
1290dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1291dc96ee37SAlexandre Belloni 	 * now on
1292dc96ee37SAlexandre Belloni 	 */
1293f270dbfaSVladimir Oltean 	if (port == lp) {
1294f270dbfaSVladimir Oltean 		lag = port;
1295f270dbfaSVladimir Oltean 		ocelot->lags[port] = bond_mask;
1296f270dbfaSVladimir Oltean 		bond_mask &= ~BIT(port);
1297dc96ee37SAlexandre Belloni 		if (bond_mask) {
1298dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1299dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1300dc96ee37SAlexandre Belloni 		}
1301dc96ee37SAlexandre Belloni 	} else {
1302dc96ee37SAlexandre Belloni 		lag = lp;
1303f270dbfaSVladimir Oltean 		ocelot->lags[lp] |= BIT(port);
1304dc96ee37SAlexandre Belloni 	}
1305dc96ee37SAlexandre Belloni 
1306dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
13079b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1308dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1309dc96ee37SAlexandre Belloni 
1310dc96ee37SAlexandre Belloni 	return 0;
1311dc96ee37SAlexandre Belloni }
13129c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1313dc96ee37SAlexandre Belloni 
13149c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1315dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1316dc96ee37SAlexandre Belloni {
1317dc96ee37SAlexandre Belloni 	u32 port_cfg;
1318dc96ee37SAlexandre Belloni 	int i;
1319dc96ee37SAlexandre Belloni 
1320dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1321dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1322f270dbfaSVladimir Oltean 		ocelot->lags[i] &= ~BIT(port);
1323dc96ee37SAlexandre Belloni 
1324dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1325dc96ee37SAlexandre Belloni 	 * next port
1326dc96ee37SAlexandre Belloni 	 */
1327f270dbfaSVladimir Oltean 	if (ocelot->lags[port]) {
1328f270dbfaSVladimir Oltean 		int n = __ffs(ocelot->lags[port]);
1329dc96ee37SAlexandre Belloni 
1330f270dbfaSVladimir Oltean 		ocelot->lags[n] = ocelot->lags[port];
1331f270dbfaSVladimir Oltean 		ocelot->lags[port] = 0;
1332dc96ee37SAlexandre Belloni 
1333dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1334dc96ee37SAlexandre Belloni 	}
1335dc96ee37SAlexandre Belloni 
1336f270dbfaSVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1337dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1338f270dbfaSVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1339f270dbfaSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
1340dc96ee37SAlexandre Belloni 
13419b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1342dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1343dc96ee37SAlexandre Belloni }
13449c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
13450e332c85SPetr Machata 
1346a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1347a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
13480b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
13490b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
13500b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1351a8015dedSVladimir Oltean  */
13520b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
135331350d7fSVladimir Oltean {
135431350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1355a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1356e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1357601e984fSVladimir Oltean 	int atop, atop_tot;
135831350d7fSVladimir Oltean 
13590b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
13600b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
13610b912fc9SVladimir Oltean 
1362*cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13630b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1364*cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
13650b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
13660b912fc9SVladimir Oltean 	}
13670b912fc9SVladimir Oltean 
1368a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1369fa914e9cSVladimir Oltean 
1370e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1371e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1372e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1373541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1374541132f0SMaxim Kochetkov 			    pause_start);
1375541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1376541132f0SMaxim Kochetkov 			    pause_stop);
1377fa914e9cSVladimir Oltean 
1378601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1379f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1380a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1381601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1382601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1383601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1384fa914e9cSVladimir Oltean }
13850b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
13860b912fc9SVladimir Oltean 
13870b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
13880b912fc9SVladimir Oltean {
13890b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
13900b912fc9SVladimir Oltean 
13910b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
13920b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
13930b912fc9SVladimir Oltean 
1394*cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13950b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1396*cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
13970b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
13980b912fc9SVladimir Oltean 	}
13990b912fc9SVladimir Oltean 
14000b912fc9SVladimir Oltean 	return max_mtu;
14010b912fc9SVladimir Oltean }
14020b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1403fa914e9cSVladimir Oltean 
14045e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1405fa914e9cSVladimir Oltean {
1406fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1407fa914e9cSVladimir Oltean 
1408b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
14096565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
141031350d7fSVladimir Oltean 
141131350d7fSVladimir Oltean 	/* Basic L2 initialization */
141231350d7fSVladimir Oltean 
14135bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
14145bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
14155bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
14165bc9d2e6SVladimir Oltean 	 */
14175bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
14185bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
14195bc9d2e6SVladimir Oltean 
14205bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
14215bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
14225bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
14235bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
14245bc9d2e6SVladimir Oltean 	mdelay(1);
14255bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
14265bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
14275bc9d2e6SVladimir Oltean 
14285bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1429a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
14305bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
14315bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1432a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
14335bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
14345bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
14355bc9d2e6SVladimir Oltean 
14365bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
14375bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
14385bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
14395bc9d2e6SVladimir Oltean 
1440e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1441541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1442e8e6e73dSVladimir Oltean 
144331350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
144431350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
144531350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
144631350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
144731350d7fSVladimir Oltean 
144831350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
144931350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
145031350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
145131350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
145231350d7fSVladimir Oltean 
145331350d7fSVladimir Oltean 	/* Enable vcap lookups */
145431350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
145531350d7fSVladimir Oltean }
14565e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
145731350d7fSVladimir Oltean 
14582d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
14592d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
14602d44b097SVladimir Oltean  * NPI mode is used).
146169df578cSVladimir Oltean  */
14622d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
146321468199SVladimir Oltean {
146469df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
146569df578cSVladimir Oltean 
146669df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
146721468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
146869df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
146969df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
147069df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
147169df578cSVladimir Oltean 	 */
147221468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
147321468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
147421468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
147521468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
147621468199SVladimir Oltean 
147769df578cSVladimir Oltean 	/* Enable CPU port module */
1478886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
147969df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1480886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
1481*cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
1482886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
1483*cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
148421468199SVladimir Oltean 
148521468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
148621468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
148721468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
148821468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
148921468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
149021468199SVladimir Oltean }
149121468199SVladimir Oltean 
1492f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
1493f6fe01d6SVladimir Oltean {
1494f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
1495f6fe01d6SVladimir Oltean 
1496f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
1497f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
1498f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
1499f6fe01d6SVladimir Oltean 	 */
1500f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
1501f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
1502f6fe01d6SVladimir Oltean 
1503f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
1504f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
1505f6fe01d6SVladimir Oltean }
1506f6fe01d6SVladimir Oltean 
1507a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1508a556c76aSAlexandre Belloni {
1509a556c76aSAlexandre Belloni 	char queue_name[32];
151021468199SVladimir Oltean 	int i, ret;
151121468199SVladimir Oltean 	u32 port;
1512a556c76aSAlexandre Belloni 
15133a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
15143a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
15153a77b593SVladimir Oltean 		if (ret) {
15163a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
15173a77b593SVladimir Oltean 			return ret;
15183a77b593SVladimir Oltean 		}
15193a77b593SVladimir Oltean 	}
15203a77b593SVladimir Oltean 
1521dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1522dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
1523dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
1524dc96ee37SAlexandre Belloni 		return -ENOMEM;
1525dc96ee37SAlexandre Belloni 
1526a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1527a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1528a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1529a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1530a556c76aSAlexandre Belloni 		return -ENOMEM;
1531a556c76aSAlexandre Belloni 
1532a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
15334e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
15344e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1535a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1536a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1537a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1538a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1539a556c76aSAlexandre Belloni 		return -ENOMEM;
1540a556c76aSAlexandre Belloni 
1541ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1542ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
1543ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
1544ca0b272bSVladimir Oltean 		return -ENOMEM;
1545ca0b272bSVladimir Oltean 	}
1546ca0b272bSVladimir Oltean 
15472b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1548e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
1549f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
1550a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1551a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1552aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
15532d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
1554a556c76aSAlexandre Belloni 
1555a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1556a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1557a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1558a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1559a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1560a556c76aSAlexandre Belloni 	}
1561a556c76aSAlexandre Belloni 
1562a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1563a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1564a556c76aSAlexandre Belloni 
1565a556c76aSAlexandre Belloni 	/* Aggregation mode */
1566a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1567a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1568a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1569a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1570a556c76aSAlexandre Belloni 
1571a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1572a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1573a556c76aSAlexandre Belloni 	 */
1574a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1575a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1576a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1577a556c76aSAlexandre Belloni 
1578a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1579a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1580a556c76aSAlexandre Belloni 
1581a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1582a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1583a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1584a556c76aSAlexandre Belloni 
1585a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1586edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
1587a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1588a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1589a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1590edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
1591a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1592a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1593a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1594a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1595a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1596a556c76aSAlexandre Belloni 
1597a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1598a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1599a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1600a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1601a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1602a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1603a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1604a556c76aSAlexandre Belloni 				 port);
1605a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1606a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1607a556c76aSAlexandre Belloni 	}
1608a556c76aSAlexandre Belloni 
1609a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
161096b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
1611a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1612a556c76aSAlexandre Belloni 
1613a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1614a556c76aSAlexandre Belloni 	}
1615a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
1616a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1617a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
1618a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1619a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1620a556c76aSAlexandre Belloni 
1621a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1622a556c76aSAlexandre Belloni 	 * registers endianness.
1623a556c76aSAlexandre Belloni 	 */
1624a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1625a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1626a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1627a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1628a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1629a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1630a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1631a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1632a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1633a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1634a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1635a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1636a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1637a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1638a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1639a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1640a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1641a556c76aSAlexandre Belloni 
16421e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1643a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1644a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
16454e3b0468SAntoine Tenart 
1646a556c76aSAlexandre Belloni 	return 0;
1647a556c76aSAlexandre Belloni }
1648a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1649a556c76aSAlexandre Belloni 
1650a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1651a556c76aSAlexandre Belloni {
1652c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
1653a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1654ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
1655a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
1656a556c76aSAlexandre Belloni }
1657a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1658a556c76aSAlexandre Belloni 
1659e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
1660e5fb512dSVladimir Oltean {
1661e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1662e5fb512dSVladimir Oltean 
1663e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
1664e5fb512dSVladimir Oltean }
1665e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
1666e5fb512dSVladimir Oltean 
1667a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1668