xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 1f78ff4ff7089b8265278d0bbf937fd8e5958dcf)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
8a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
920968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10a556c76aSAlexandre Belloni #include "ocelot.h"
113c83654fSVladimir Oltean #include "ocelot_vcap.h"
12a556c76aSAlexandre Belloni 
13639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
14639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
15639c1b26SSteen Hegelund 
16a556c76aSAlexandre Belloni struct ocelot_mact_entry {
17a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
18a556c76aSAlexandre Belloni 	u16 vid;
19a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
20a556c76aSAlexandre Belloni };
21a556c76aSAlexandre Belloni 
22639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
23639c1b26SSteen Hegelund {
24639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
25639c1b26SSteen Hegelund }
26639c1b26SSteen Hegelund 
27a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
28a556c76aSAlexandre Belloni {
29639c1b26SSteen Hegelund 	u32 val;
30a556c76aSAlexandre Belloni 
31639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
32639c1b26SSteen Hegelund 		ocelot, val,
33639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
34639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
35639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
36a556c76aSAlexandre Belloni }
37a556c76aSAlexandre Belloni 
38a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
39a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
40a556c76aSAlexandre Belloni 			       unsigned int vid)
41a556c76aSAlexandre Belloni {
42a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
43a556c76aSAlexandre Belloni 
44a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
45a556c76aSAlexandre Belloni 	 * understood by the hardware.
46a556c76aSAlexandre Belloni 	 */
47a556c76aSAlexandre Belloni 	mach |= vid    << 16;
48a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
49a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
50a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
51a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
52a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
53a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
54a556c76aSAlexandre Belloni 
55a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
56a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
57a556c76aSAlexandre Belloni 
58a556c76aSAlexandre Belloni }
59a556c76aSAlexandre Belloni 
609c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
61a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
629c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
63a556c76aSAlexandre Belloni {
64584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
65584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
66584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
67584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
68584b7cfcSAlban Bedel 	unsigned int mc_ports;
69584b7cfcSAlban Bedel 
70584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
71584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
72584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
73584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
74584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
75584b7cfcSAlban Bedel 	else
76584b7cfcSAlban Bedel 		mc_ports = 0;
77584b7cfcSAlban Bedel 
78584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
79584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
80584b7cfcSAlban Bedel 
81a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
82a556c76aSAlexandre Belloni 
83a556c76aSAlexandre Belloni 	/* Issue a write command */
84584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
85a556c76aSAlexandre Belloni 
86a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
87a556c76aSAlexandre Belloni }
889c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
89a556c76aSAlexandre Belloni 
909c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
919c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
92a556c76aSAlexandre Belloni {
93a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
94a556c76aSAlexandre Belloni 
95a556c76aSAlexandre Belloni 	/* Issue a forget command */
96a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
97a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
98a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
99a556c76aSAlexandre Belloni 
100a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
101a556c76aSAlexandre Belloni }
1029c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
103a556c76aSAlexandre Belloni 
104a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
105a556c76aSAlexandre Belloni {
106a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
107a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
108a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
109a556c76aSAlexandre Belloni 	 */
110a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
111a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
112a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
113a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
114a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
115a556c76aSAlexandre Belloni 
116a556c76aSAlexandre Belloni 	/* Clear the MAC table */
117a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
118a556c76aSAlexandre Belloni }
119a556c76aSAlexandre Belloni 
120f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
121b5962294SHoratiu Vultur {
122b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
123b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
124f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
12575944fdaSXiaoliang Yang 
12675944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
12775944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1282f17c050SXiaoliang Yang 
1292f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1302f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1312f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
132b5962294SHoratiu Vultur }
133b5962294SHoratiu Vultur 
134639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
135639c1b26SSteen Hegelund {
136639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
137639c1b26SSteen Hegelund }
138639c1b26SSteen Hegelund 
139a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
140a556c76aSAlexandre Belloni {
141639c1b26SSteen Hegelund 	u32 val;
142a556c76aSAlexandre Belloni 
143639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
144639c1b26SSteen Hegelund 		ocelot,
145639c1b26SSteen Hegelund 		val,
146639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
147639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
148639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
149a556c76aSAlexandre Belloni }
150a556c76aSAlexandre Belloni 
1517142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1527142529fSAntoine Tenart {
1537142529fSAntoine Tenart 	/* Select the VID to configure */
1547142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1557142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1567142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1577142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1587142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1597142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1607142529fSAntoine Tenart 
1617142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1627142529fSAntoine Tenart }
1637142529fSAntoine Tenart 
1642f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
165c3e58a75SVladimir Oltean 					struct ocelot_vlan native_vlan)
16697bb69e1SVladimir Oltean {
16797bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
16887b0f983SVladimir Oltean 	u32 val = 0;
16997bb69e1SVladimir Oltean 
170c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
17197bb69e1SVladimir Oltean 
172c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1737142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
17497bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17597bb69e1SVladimir Oltean 
17687b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
177e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
17887b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
17987b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
18087b0f983SVladimir Oltean 		else
18187b0f983SVladimir Oltean 			/* Tag all frames */
18287b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
18387b0f983SVladimir Oltean 	} else {
18487b0f983SVladimir Oltean 		/* Port tagging disabled. */
18587b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
18687b0f983SVladimir Oltean 	}
18787b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
18887b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
18987b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
19097bb69e1SVladimir Oltean }
19197bb69e1SVladimir Oltean 
19275e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
193c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
194c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
19575e5a554SVladimir Oltean {
19675e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
197be0576feSVladimir Oltean 	u32 val = 0;
19875e5a554SVladimir Oltean 
199c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
20075e5a554SVladimir Oltean 
20175e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
202c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
20375e5a554SVladimir Oltean 
20475e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
205c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
20675e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
20775e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
208be0576feSVladimir Oltean 
209be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
210be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
211be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
212be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
213be0576feSVladimir Oltean 	 */
214be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
215be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
216be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
217be0576feSVladimir Oltean 
218be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
219be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
220be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
221be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
22275e5a554SVladimir Oltean }
22375e5a554SVladimir Oltean 
2242e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
225bae33f2bSVladimir Oltean 			       bool vlan_aware)
22687b0f983SVladimir Oltean {
22770edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
228bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
22970edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
230bae33f2bSVladimir Oltean 	u32 val;
23170edfae1SVladimir Oltean 
23270edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
23370edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
23470edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
23570edfae1SVladimir Oltean 			dev_err(ocelot->dev,
23670edfae1SVladimir Oltean 				"Cannot change VLAN state with vlan modify rules active\n");
23770edfae1SVladimir Oltean 			return -EBUSY;
23870edfae1SVladimir Oltean 		}
23970edfae1SVladimir Oltean 	}
24070edfae1SVladimir Oltean 
24187b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
24287b0f983SVladimir Oltean 
24387b0f983SVladimir Oltean 	if (vlan_aware)
24487b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24587b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
24687b0f983SVladimir Oltean 	else
24787b0f983SVladimir Oltean 		val = 0;
24887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
24987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
25087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
25187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
25287b0f983SVladimir Oltean 
253c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
254c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2552e554a7aSVladimir Oltean 
2562e554a7aSVladimir Oltean 	return 0;
25787b0f983SVladimir Oltean }
25887b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
25987b0f983SVladimir Oltean 
2602f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2612f0402feSVladimir Oltean 			bool untagged)
2622f0402feSVladimir Oltean {
2632f0402feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2642f0402feSVladimir Oltean 
2652f0402feSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
2662f0402feSVladimir Oltean 	if (untagged && ocelot_port->native_vlan.vid != vid &&
2672f0402feSVladimir Oltean 	    ocelot_port->native_vlan.valid) {
2682f0402feSVladimir Oltean 		dev_err(ocelot->dev,
2692f0402feSVladimir Oltean 			"Port already has a native VLAN: %d\n",
2702f0402feSVladimir Oltean 			ocelot_port->native_vlan.vid);
2712f0402feSVladimir Oltean 		return -EBUSY;
2722f0402feSVladimir Oltean 	}
2732f0402feSVladimir Oltean 
2742f0402feSVladimir Oltean 	return 0;
2752f0402feSVladimir Oltean }
2762f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
2772f0402feSVladimir Oltean 
2785e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2797142529fSAntoine Tenart 		    bool untagged)
2807142529fSAntoine Tenart {
2817142529fSAntoine Tenart 	int ret;
2827142529fSAntoine Tenart 
2837142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
28497bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2857142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2867142529fSAntoine Tenart 	if (ret)
2877142529fSAntoine Tenart 		return ret;
2887142529fSAntoine Tenart 
2897142529fSAntoine Tenart 	/* Default ingress vlan classification */
290c3e58a75SVladimir Oltean 	if (pvid) {
291c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
292c3e58a75SVladimir Oltean 
293c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
294e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
295c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
296c3e58a75SVladimir Oltean 	}
2977142529fSAntoine Tenart 
2987142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
29997bb69e1SVladimir Oltean 	if (untagged) {
300c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
301c3e58a75SVladimir Oltean 
302c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
303e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
3042f0402feSVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
305b9cd75e6SVladimir Oltean 	}
3067142529fSAntoine Tenart 
3077142529fSAntoine Tenart 	return 0;
3087142529fSAntoine Tenart }
3095e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
3107142529fSAntoine Tenart 
3115e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3129855934cSVladimir Oltean {
3139855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3149855934cSVladimir Oltean 	int ret;
3157142529fSAntoine Tenart 
3167142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
31797bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3187142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3197142529fSAntoine Tenart 	if (ret)
3207142529fSAntoine Tenart 		return ret;
3217142529fSAntoine Tenart 
322be0576feSVladimir Oltean 	/* Ingress */
323be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
324be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
325be0576feSVladimir Oltean 
326be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
327be0576feSVladimir Oltean 	}
328be0576feSVladimir Oltean 
3297142529fSAntoine Tenart 	/* Egress */
330c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
331e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
332c3e58a75SVladimir Oltean 
333c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
334c3e58a75SVladimir Oltean 	}
3357142529fSAntoine Tenart 
3367142529fSAntoine Tenart 	return 0;
3377142529fSAntoine Tenart }
3385e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3397142529fSAntoine Tenart 
340a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
341a556c76aSAlexandre Belloni {
3427142529fSAntoine Tenart 	u16 port, vid;
3437142529fSAntoine Tenart 
344a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
345a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
346a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
347a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3487142529fSAntoine Tenart 
3497142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3507142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3517142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3527142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3537142529fSAntoine Tenart 	}
3547142529fSAntoine Tenart 
3557142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3567142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3577142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3587142529fSAntoine Tenart 	 */
3597142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3607142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3617142529fSAntoine Tenart 
3627142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3637142529fSAntoine Tenart 	 * default.
3647142529fSAntoine Tenart 	 */
365714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
366714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3677142529fSAntoine Tenart 
3687142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3697142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3707142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3717142529fSAntoine Tenart 	}
372a556c76aSAlexandre Belloni }
373a556c76aSAlexandre Belloni 
374eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
375eb4733d7SVladimir Oltean {
376eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
377eb4733d7SVladimir Oltean }
378eb4733d7SVladimir Oltean 
379eb4733d7SVladimir Oltean int ocelot_port_flush(struct ocelot *ocelot, int port)
380eb4733d7SVladimir Oltean {
381eb4733d7SVladimir Oltean 	int err, val;
382eb4733d7SVladimir Oltean 
383eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
384eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
385eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
386eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
387eb4733d7SVladimir Oltean 
388eb4733d7SVladimir Oltean 	/* Disable flow control */
389eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
390eb4733d7SVladimir Oltean 
391eb4733d7SVladimir Oltean 	/* Disable priority flow control */
392eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
393eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
394eb4733d7SVladimir Oltean 
395eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
396eb4733d7SVladimir Oltean 	 * at the port.
397eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
398eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
399eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
400eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
401eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
402eb4733d7SVladimir Oltean 	 */
403eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
404eb4733d7SVladimir Oltean 
405eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
406eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
407eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
408eb4733d7SVladimir Oltean 
409eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
410eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
411eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
412eb4733d7SVladimir Oltean 
413eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
414eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
415eb4733d7SVladimir Oltean 		       port);
416eb4733d7SVladimir Oltean 
417eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
418eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
419eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
420eb4733d7SVladimir Oltean 
421eb4733d7SVladimir Oltean 	/* Clear flushing again. */
422eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
423eb4733d7SVladimir Oltean 
424eb4733d7SVladimir Oltean 	return err;
425eb4733d7SVladimir Oltean }
426eb4733d7SVladimir Oltean EXPORT_SYMBOL(ocelot_port_flush);
427eb4733d7SVladimir Oltean 
4285e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
42926f4dbabSVladimir Oltean 			struct phy_device *phydev)
430a556c76aSAlexandre Belloni {
43126f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
4325bc9d2e6SVladimir Oltean 	int speed, mode = 0;
433a556c76aSAlexandre Belloni 
43426f4dbabSVladimir Oltean 	switch (phydev->speed) {
435a556c76aSAlexandre Belloni 	case SPEED_10:
436a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
437a556c76aSAlexandre Belloni 		break;
438a556c76aSAlexandre Belloni 	case SPEED_100:
439a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
440a556c76aSAlexandre Belloni 		break;
441a556c76aSAlexandre Belloni 	case SPEED_1000:
442a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
443a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
444a556c76aSAlexandre Belloni 		break;
445a556c76aSAlexandre Belloni 	case SPEED_2500:
446a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
447a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
448a556c76aSAlexandre Belloni 		break;
449a556c76aSAlexandre Belloni 	default:
45026f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
45126f4dbabSVladimir Oltean 			port, phydev->speed);
452a556c76aSAlexandre Belloni 		return;
453a556c76aSAlexandre Belloni 	}
454a556c76aSAlexandre Belloni 
45526f4dbabSVladimir Oltean 	phy_print_status(phydev);
456a556c76aSAlexandre Belloni 
45726f4dbabSVladimir Oltean 	if (!phydev->link)
458a556c76aSAlexandre Belloni 		return;
459a556c76aSAlexandre Belloni 
460a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
461004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
462a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
463a556c76aSAlexandre Belloni 
4641ba8f656SVladimir Oltean 	/* Disable HDX fast control */
4651ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
4661ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
4671ba8f656SVladimir Oltean 
4681ba8f656SVladimir Oltean 	/* SGMII only for now */
4691ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4701ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
4711ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4721ba8f656SVladimir Oltean 
4731ba8f656SVladimir Oltean 	/* Enable PCS */
4741ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4751ba8f656SVladimir Oltean 
4761ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4771ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4781ba8f656SVladimir Oltean 
4791ba8f656SVladimir Oltean 	/* No loopback */
4801ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
481a556c76aSAlexandre Belloni 
482a556c76aSAlexandre Belloni 	/* Enable MAC module */
483004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
484a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
485a556c76aSAlexandre Belloni 
486a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
487*1f78ff4fSYixing Liu 	 * reset
488*1f78ff4fSYixing Liu 	 */
489004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
490a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
491a556c76aSAlexandre Belloni 
492a556c76aSAlexandre Belloni 	/* No PFC */
493a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
494004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
495a556c76aSAlexandre Belloni 
496a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
497886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
498886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
499a556c76aSAlexandre Belloni 
500a556c76aSAlexandre Belloni 	/* Flow control */
501a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
502a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
503a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
504a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
505a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
506004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
507004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
508a556c76aSAlexandre Belloni }
5095e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
510a556c76aSAlexandre Belloni 
5115e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
512889b8950SVladimir Oltean 			struct phy_device *phy)
513a556c76aSAlexandre Belloni {
514a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
515a556c76aSAlexandre Belloni 	 * MAC addresses.
516a556c76aSAlexandre Belloni 	 */
517a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
518a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
519004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
520004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
521889b8950SVladimir Oltean }
5225e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
523889b8950SVladimir Oltean 
5245e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
525889b8950SVladimir Oltean {
526889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
527889b8950SVladimir Oltean 
528889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
529886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
530889b8950SVladimir Oltean }
5315e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
532889b8950SVladimir Oltean 
533e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
534e2f9a8feSVladimir Oltean 				  struct sk_buff *clone)
535400928bfSYangbo Lu {
536e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
537400928bfSYangbo Lu 
5386565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
5396565243cSVladimir Oltean 
540e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
541b049da13SYangbo Lu 	/* Store timestamp ID in cb[0] of sk_buff */
542e2f9a8feSVladimir Oltean 	clone->cb[0] = ocelot_port->ts_id;
5436565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
544e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
5456565243cSVladimir Oltean 
5466565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
547400928bfSYangbo Lu }
548400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
549400928bfSYangbo Lu 
550e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
551e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
5524e3b0468SAntoine Tenart {
5534e3b0468SAntoine Tenart 	unsigned long flags;
5544e3b0468SAntoine Tenart 	u32 val;
5554e3b0468SAntoine Tenart 
5564e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
5574e3b0468SAntoine Tenart 
5584e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
5594e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
5604e3b0468SAntoine Tenart 
5614e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
5624e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
5634e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
5644e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
5654e3b0468SAntoine Tenart 
5664e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
5674e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
5684e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
5694e3b0468SAntoine Tenart 
5704e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
5714e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
5724e3b0468SAntoine Tenart 		ts->tv_sec--;
5734e3b0468SAntoine Tenart 
5744e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
5754e3b0468SAntoine Tenart }
576e23a7b3eSYangbo Lu 
577e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
578e23a7b3eSYangbo Lu {
579e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
580e23a7b3eSYangbo Lu 
581e23a7b3eSYangbo Lu 	while (budget--) {
582b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
583e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
584e23a7b3eSYangbo Lu 		struct ocelot_port *port;
585e23a7b3eSYangbo Lu 		struct timespec64 ts;
586b049da13SYangbo Lu 		unsigned long flags;
587e23a7b3eSYangbo Lu 		u32 val, id, txport;
588e23a7b3eSYangbo Lu 
589e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
590e23a7b3eSYangbo Lu 
591e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
592e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
593e23a7b3eSYangbo Lu 			break;
594e23a7b3eSYangbo Lu 
595e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
596e23a7b3eSYangbo Lu 
597e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
598e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
599e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
600e23a7b3eSYangbo Lu 
601e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
602e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
603e23a7b3eSYangbo Lu 
604b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
605b049da13SYangbo Lu 
606b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
607b049da13SYangbo Lu 			if (skb->cb[0] != id)
608e23a7b3eSYangbo Lu 				continue;
609b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
610b049da13SYangbo Lu 			skb_match = skb;
611fc62c094SYangbo Lu 			break;
612e23a7b3eSYangbo Lu 		}
613e23a7b3eSYangbo Lu 
614b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
615b049da13SYangbo Lu 
6165fd82200Slaurent brando 		/* Get the h/w timestamp */
6175fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
618e23a7b3eSYangbo Lu 
619b049da13SYangbo Lu 		if (unlikely(!skb_match))
620e23a7b3eSYangbo Lu 			continue;
621e23a7b3eSYangbo Lu 
622e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
623e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
624e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
625e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
6265fd82200Slaurent brando 
6275fd82200Slaurent brando 		/* Next ts */
6285fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
629e23a7b3eSYangbo Lu 	}
630e23a7b3eSYangbo Lu }
631e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
6324e3b0468SAntoine Tenart 
633924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
634924ee317SVladimir Oltean 				u32 *rval)
635924ee317SVladimir Oltean {
636924ee317SVladimir Oltean 	u32 bytes_valid, val;
637924ee317SVladimir Oltean 
638924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
639924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
640924ee317SVladimir Oltean 		if (ifh)
641924ee317SVladimir Oltean 			return -EIO;
642924ee317SVladimir Oltean 
643924ee317SVladimir Oltean 		do {
644924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
645924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
646924ee317SVladimir Oltean 	}
647924ee317SVladimir Oltean 
648924ee317SVladimir Oltean 	switch (val) {
649924ee317SVladimir Oltean 	case XTR_ABORT:
650924ee317SVladimir Oltean 		return -EIO;
651924ee317SVladimir Oltean 	case XTR_EOF_0:
652924ee317SVladimir Oltean 	case XTR_EOF_1:
653924ee317SVladimir Oltean 	case XTR_EOF_2:
654924ee317SVladimir Oltean 	case XTR_EOF_3:
655924ee317SVladimir Oltean 	case XTR_PRUNED:
656924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
657924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
658924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
659924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
660924ee317SVladimir Oltean 		else
661924ee317SVladimir Oltean 			*rval = val;
662924ee317SVladimir Oltean 
663924ee317SVladimir Oltean 		return bytes_valid;
664924ee317SVladimir Oltean 	case XTR_ESCAPE:
665924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
666924ee317SVladimir Oltean 
667924ee317SVladimir Oltean 		return 4;
668924ee317SVladimir Oltean 	default:
669924ee317SVladimir Oltean 		*rval = val;
670924ee317SVladimir Oltean 
671924ee317SVladimir Oltean 		return 4;
672924ee317SVladimir Oltean 	}
673924ee317SVladimir Oltean }
674924ee317SVladimir Oltean 
675924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
676924ee317SVladimir Oltean {
677924ee317SVladimir Oltean 	int i, err = 0;
678924ee317SVladimir Oltean 
679924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
680924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
681924ee317SVladimir Oltean 		if (err != 4)
682924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
683924ee317SVladimir Oltean 	}
684924ee317SVladimir Oltean 
685924ee317SVladimir Oltean 	return 0;
686924ee317SVladimir Oltean }
687924ee317SVladimir Oltean 
688924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
689924ee317SVladimir Oltean {
690924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
6912ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
692924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
693924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
694924ee317SVladimir Oltean 	struct net_device *dev;
695924ee317SVladimir Oltean 	struct timespec64 ts;
696924ee317SVladimir Oltean 	struct sk_buff *skb;
697924ee317SVladimir Oltean 	int sz, buf_len;
698924ee317SVladimir Oltean 	u32 val, *buf;
699924ee317SVladimir Oltean 	int err;
700924ee317SVladimir Oltean 
701924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
702924ee317SVladimir Oltean 	if (err)
703924ee317SVladimir Oltean 		return err;
704924ee317SVladimir Oltean 
705924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
706924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
707924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
708924ee317SVladimir Oltean 
709924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
710924ee317SVladimir Oltean 		return -EINVAL;
711924ee317SVladimir Oltean 
712924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
713924ee317SVladimir Oltean 	if (!dev)
714924ee317SVladimir Oltean 		return -EINVAL;
715924ee317SVladimir Oltean 
716924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
717924ee317SVladimir Oltean 	if (unlikely(!skb)) {
718924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
719924ee317SVladimir Oltean 		return -ENOMEM;
720924ee317SVladimir Oltean 	}
721924ee317SVladimir Oltean 
722924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
723924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
724924ee317SVladimir Oltean 
725924ee317SVladimir Oltean 	len = 0;
726924ee317SVladimir Oltean 	do {
727924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
728924ee317SVladimir Oltean 		if (sz < 0) {
729924ee317SVladimir Oltean 			err = sz;
730924ee317SVladimir Oltean 			goto out_free_skb;
731924ee317SVladimir Oltean 		}
732924ee317SVladimir Oltean 		*buf++ = val;
733924ee317SVladimir Oltean 		len += sz;
734924ee317SVladimir Oltean 	} while (len < buf_len);
735924ee317SVladimir Oltean 
736924ee317SVladimir Oltean 	/* Read the FCS */
737924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
738924ee317SVladimir Oltean 	if (sz < 0) {
739924ee317SVladimir Oltean 		err = sz;
740924ee317SVladimir Oltean 		goto out_free_skb;
741924ee317SVladimir Oltean 	}
742924ee317SVladimir Oltean 
743924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
744924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
745924ee317SVladimir Oltean 
746924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
747924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
748924ee317SVladimir Oltean 		*buf = val;
749924ee317SVladimir Oltean 	}
750924ee317SVladimir Oltean 
751924ee317SVladimir Oltean 	if (ocelot->ptp) {
752924ee317SVladimir Oltean 		ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
753924ee317SVladimir Oltean 
754924ee317SVladimir Oltean 		tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
755924ee317SVladimir Oltean 		if ((tod_in_ns & 0xffffffff) < timestamp)
756924ee317SVladimir Oltean 			full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
757924ee317SVladimir Oltean 					timestamp;
758924ee317SVladimir Oltean 		else
759924ee317SVladimir Oltean 			full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
760924ee317SVladimir Oltean 					timestamp;
761924ee317SVladimir Oltean 
762924ee317SVladimir Oltean 		shhwtstamps = skb_hwtstamps(skb);
763924ee317SVladimir Oltean 		memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
764924ee317SVladimir Oltean 		shhwtstamps->hwtstamp = full_ts_in_ns;
765924ee317SVladimir Oltean 	}
766924ee317SVladimir Oltean 
767924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
768924ee317SVladimir Oltean 	 * has already been forwarded.
769924ee317SVladimir Oltean 	 */
770df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
771924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
772924ee317SVladimir Oltean 
773924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
774d8ea7ff3SHoratiu Vultur 
775924ee317SVladimir Oltean 	*nskb = skb;
776924ee317SVladimir Oltean 
777924ee317SVladimir Oltean 	return 0;
778924ee317SVladimir Oltean 
779924ee317SVladimir Oltean out_free_skb:
780924ee317SVladimir Oltean 	kfree_skb(skb);
781924ee317SVladimir Oltean 	return err;
782924ee317SVladimir Oltean }
783924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
784924ee317SVladimir Oltean 
785137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
786137ffbc4SVladimir Oltean {
787137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
788137ffbc4SVladimir Oltean 
789137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
790137ffbc4SVladimir Oltean 		return false;
791137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
792137ffbc4SVladimir Oltean 		return false;
793137ffbc4SVladimir Oltean 
794137ffbc4SVladimir Oltean 	return true;
795137ffbc4SVladimir Oltean }
796137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
797137ffbc4SVladimir Oltean 
798137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
799137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
800137ffbc4SVladimir Oltean {
80140d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
802137ffbc4SVladimir Oltean 	unsigned int i, count, last;
803137ffbc4SVladimir Oltean 
804137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
805137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
806137ffbc4SVladimir Oltean 
80740d3f295SVladimir Oltean 	ocelot_ifh_set_bypass(ifh, 1);
8081f778d50SVladimir Oltean 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
80940d3f295SVladimir Oltean 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
81040d3f295SVladimir Oltean 	ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
81140d3f295SVladimir Oltean 	ocelot_ifh_set_rew_op(ifh, rew_op);
812137ffbc4SVladimir Oltean 
813137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
81440d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
815137ffbc4SVladimir Oltean 
816137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
817137ffbc4SVladimir Oltean 	last = skb->len % 4;
818137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
819137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
820137ffbc4SVladimir Oltean 
821137ffbc4SVladimir Oltean 	/* Add padding */
822137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
823137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
824137ffbc4SVladimir Oltean 		i++;
825137ffbc4SVladimir Oltean 	}
826137ffbc4SVladimir Oltean 
827137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
828137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
829137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
830137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
831137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
832137ffbc4SVladimir Oltean 
833137ffbc4SVladimir Oltean 	/* Add dummy CRC */
834137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
835137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
836137ffbc4SVladimir Oltean 
837137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
838137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
839137ffbc4SVladimir Oltean }
840137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
841137ffbc4SVladimir Oltean 
8420a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
8430a6f17c6SVladimir Oltean {
8440a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
8450a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
8460a6f17c6SVladimir Oltean }
8470a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
8480a6f17c6SVladimir Oltean 
8495e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
85087b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
851a556c76aSAlexandre Belloni {
852471beb11SVladimir Oltean 	int pgid = port;
853471beb11SVladimir Oltean 
854471beb11SVladimir Oltean 	if (port == ocelot->npi)
855471beb11SVladimir Oltean 		pgid = PGID_CPU;
856a556c76aSAlexandre Belloni 
857471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
858a556c76aSAlexandre Belloni }
8595e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
860a556c76aSAlexandre Belloni 
8615e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
862531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
863531ee1a6SVladimir Oltean {
864531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
865531ee1a6SVladimir Oltean }
8665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
867531ee1a6SVladimir Oltean 
8689c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
869531ee1a6SVladimir Oltean 			    bool is_static, void *data)
870a556c76aSAlexandre Belloni {
871531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
872a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
873a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
874a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
875a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
876a556c76aSAlexandre Belloni 
877a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
878a556c76aSAlexandre Belloni 		goto skip;
879a556c76aSAlexandre Belloni 
880a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
881a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
882a556c76aSAlexandre Belloni 	if (!nlh)
883a556c76aSAlexandre Belloni 		return -EMSGSIZE;
884a556c76aSAlexandre Belloni 
885a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
886a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
887a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
888a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
889a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
890a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
891a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
892531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
893a556c76aSAlexandre Belloni 
894531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
895a556c76aSAlexandre Belloni 		goto nla_put_failure;
896a556c76aSAlexandre Belloni 
897531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
898a556c76aSAlexandre Belloni 		goto nla_put_failure;
899a556c76aSAlexandre Belloni 
900a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
901a556c76aSAlexandre Belloni 
902a556c76aSAlexandre Belloni skip:
903a556c76aSAlexandre Belloni 	dump->idx++;
904a556c76aSAlexandre Belloni 	return 0;
905a556c76aSAlexandre Belloni 
906a556c76aSAlexandre Belloni nla_put_failure:
907a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
908a556c76aSAlexandre Belloni 	return -EMSGSIZE;
909a556c76aSAlexandre Belloni }
9109c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
911a556c76aSAlexandre Belloni 
912531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
913a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
914a556c76aSAlexandre Belloni {
915a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
916531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
917a556c76aSAlexandre Belloni 
918a556c76aSAlexandre Belloni 	/* Set row and column to read from */
919a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
920a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
921a556c76aSAlexandre Belloni 
922a556c76aSAlexandre Belloni 	/* Issue a read command */
923a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
924a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
925a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
926a556c76aSAlexandre Belloni 
927a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
928a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
929a556c76aSAlexandre Belloni 
930a556c76aSAlexandre Belloni 	/* Read the entry flags */
931a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
932a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
933a556c76aSAlexandre Belloni 		return -EINVAL;
934a556c76aSAlexandre Belloni 
935a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
936a556c76aSAlexandre Belloni 	 * do not report it.
937a556c76aSAlexandre Belloni 	 */
938a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
939531ee1a6SVladimir Oltean 	if (dst != port)
940a556c76aSAlexandre Belloni 		return -EINVAL;
941a556c76aSAlexandre Belloni 
942a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
943a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
944a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
945a556c76aSAlexandre Belloni 
946a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
947a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
948a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
949a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
950a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
951a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
952a556c76aSAlexandre Belloni 
953a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
954a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
955a556c76aSAlexandre Belloni 
956a556c76aSAlexandre Belloni 	return 0;
957a556c76aSAlexandre Belloni }
958a556c76aSAlexandre Belloni 
9595e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
960531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
961a556c76aSAlexandre Belloni {
962531ee1a6SVladimir Oltean 	int i, j;
963a556c76aSAlexandre Belloni 
96421ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
96521ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
966a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
967531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
968531ee1a6SVladimir Oltean 			bool is_static;
969531ee1a6SVladimir Oltean 			int ret;
970531ee1a6SVladimir Oltean 
971531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
972a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
973a556c76aSAlexandre Belloni 			 * skip it.
974a556c76aSAlexandre Belloni 			 */
975a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
976a556c76aSAlexandre Belloni 				continue;
977a556c76aSAlexandre Belloni 			else if (ret)
978531ee1a6SVladimir Oltean 				return ret;
979a556c76aSAlexandre Belloni 
980531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
981531ee1a6SVladimir Oltean 
982531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
983a556c76aSAlexandre Belloni 			if (ret)
984531ee1a6SVladimir Oltean 				return ret;
985a556c76aSAlexandre Belloni 		}
986a556c76aSAlexandre Belloni 	}
987a556c76aSAlexandre Belloni 
988531ee1a6SVladimir Oltean 	return 0;
989531ee1a6SVladimir Oltean }
9905e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
991531ee1a6SVladimir Oltean 
992f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
9934e3b0468SAntoine Tenart {
9944e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
9954e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
9964e3b0468SAntoine Tenart }
997f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
9984e3b0468SAntoine Tenart 
999f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
10004e3b0468SAntoine Tenart {
1001306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10024e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
10034e3b0468SAntoine Tenart 
10044e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
10054e3b0468SAntoine Tenart 		return -EFAULT;
10064e3b0468SAntoine Tenart 
10074e3b0468SAntoine Tenart 	/* reserved for future extensions */
10084e3b0468SAntoine Tenart 	if (cfg.flags)
10094e3b0468SAntoine Tenart 		return -EINVAL;
10104e3b0468SAntoine Tenart 
10114e3b0468SAntoine Tenart 	/* Tx type sanity check */
10124e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
10134e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1014306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
10154e3b0468SAntoine Tenart 		break;
10164e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
10174e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
10184e3b0468SAntoine Tenart 		 * need to update the origin time.
10194e3b0468SAntoine Tenart 		 */
1020306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
10214e3b0468SAntoine Tenart 		break;
10224e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1023306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
10244e3b0468SAntoine Tenart 		break;
10254e3b0468SAntoine Tenart 	default:
10264e3b0468SAntoine Tenart 		return -ERANGE;
10274e3b0468SAntoine Tenart 	}
10284e3b0468SAntoine Tenart 
10294e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
10304e3b0468SAntoine Tenart 
10314e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
10324e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
10334e3b0468SAntoine Tenart 		break;
10344e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
10354e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
10364e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
10374e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
10384e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
10394e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
10404e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
10414e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
10424e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
10434e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
10444e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
10454e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
10464e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
10474e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
10484e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
10494e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
10504e3b0468SAntoine Tenart 		break;
10514e3b0468SAntoine Tenart 	default:
10524e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
10534e3b0468SAntoine Tenart 		return -ERANGE;
10544e3b0468SAntoine Tenart 	}
10554e3b0468SAntoine Tenart 
10564e3b0468SAntoine Tenart 	/* Commit back the result & save it */
10574e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
10584e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
10594e3b0468SAntoine Tenart 
10604e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
10614e3b0468SAntoine Tenart }
1062f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
10634e3b0468SAntoine Tenart 
10645e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1065a556c76aSAlexandre Belloni {
1066a556c76aSAlexandre Belloni 	int i;
1067a556c76aSAlexandre Belloni 
1068a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1069a556c76aSAlexandre Belloni 		return;
1070a556c76aSAlexandre Belloni 
1071a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1072a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1073a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1074a556c76aSAlexandre Belloni }
10755e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1076a556c76aSAlexandre Belloni 
10771e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1078a556c76aSAlexandre Belloni {
1079a556c76aSAlexandre Belloni 	int i, j;
1080a556c76aSAlexandre Belloni 
1081a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1082a556c76aSAlexandre Belloni 
1083a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1084a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1085a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1086a556c76aSAlexandre Belloni 
1087a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1088a556c76aSAlexandre Belloni 			u32 val;
1089a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1090a556c76aSAlexandre Belloni 
1091a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1092a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1093a556c76aSAlexandre Belloni 
1094a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1095a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1096a556c76aSAlexandre Belloni 
1097a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1098a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1099a556c76aSAlexandre Belloni 		}
1100a556c76aSAlexandre Belloni 	}
1101a556c76aSAlexandre Belloni 
11021e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
11031e1caa97SClaudiu Manoil }
11041e1caa97SClaudiu Manoil 
11051e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
11061e1caa97SClaudiu Manoil {
11071e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
11081e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
11091e1caa97SClaudiu Manoil 					     stats_work);
11101e1caa97SClaudiu Manoil 
11111e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
11121e1caa97SClaudiu Manoil 
1113a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1114a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1115a556c76aSAlexandre Belloni }
1116a556c76aSAlexandre Belloni 
11175e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1118a556c76aSAlexandre Belloni {
1119a556c76aSAlexandre Belloni 	int i;
1120a556c76aSAlexandre Belloni 
1121a556c76aSAlexandre Belloni 	/* check and update now */
11221e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1123a556c76aSAlexandre Belloni 
1124a556c76aSAlexandre Belloni 	/* Copy all counters */
1125a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1126004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1127a556c76aSAlexandre Belloni }
11285e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1129a556c76aSAlexandre Belloni 
11305e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1131c7282d38SVladimir Oltean {
1132a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1133a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1134c7282d38SVladimir Oltean 
1135a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1136a556c76aSAlexandre Belloni }
11375e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1138a556c76aSAlexandre Belloni 
11395e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1140c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1141c7282d38SVladimir Oltean {
11424e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
11434e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1144d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1145d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1146d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1147d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1148d2b09a8eSYangbo Lu 		return 0;
1149d2b09a8eSYangbo Lu 	}
11504e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
11514e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
11524e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
11534e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
11544e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
11554e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
11564e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
11574e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
11584e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
11594e3b0468SAntoine Tenart 
11604e3b0468SAntoine Tenart 	return 0;
11614e3b0468SAntoine Tenart }
11625e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
11634e3b0468SAntoine Tenart 
116423ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
116523ca3b72SVladimir Oltean 				bool only_active_ports)
1166b80af659SVladimir Oltean {
1167b80af659SVladimir Oltean 	u32 mask = 0;
1168b80af659SVladimir Oltean 	int port;
1169b80af659SVladimir Oltean 
1170b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1171b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1172b80af659SVladimir Oltean 
1173b80af659SVladimir Oltean 		if (!ocelot_port)
1174b80af659SVladimir Oltean 			continue;
1175b80af659SVladimir Oltean 
117623ca3b72SVladimir Oltean 		if (ocelot_port->bond == bond) {
117723ca3b72SVladimir Oltean 			if (only_active_ports && !ocelot_port->lag_tx_active)
117823ca3b72SVladimir Oltean 				continue;
117923ca3b72SVladimir Oltean 
1180b80af659SVladimir Oltean 			mask |= BIT(port);
1181b80af659SVladimir Oltean 		}
118223ca3b72SVladimir Oltean 	}
1183b80af659SVladimir Oltean 
1184b80af659SVladimir Oltean 	return mask;
1185b80af659SVladimir Oltean }
1186b80af659SVladimir Oltean 
1187df291e54SVladimir Oltean static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot,
1188df291e54SVladimir Oltean 				      struct net_device *bridge)
1189df291e54SVladimir Oltean {
1190df291e54SVladimir Oltean 	u32 mask = 0;
1191df291e54SVladimir Oltean 	int port;
1192df291e54SVladimir Oltean 
1193df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1194df291e54SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1195df291e54SVladimir Oltean 
1196df291e54SVladimir Oltean 		if (!ocelot_port)
1197df291e54SVladimir Oltean 			continue;
1198df291e54SVladimir Oltean 
1199df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1200df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1201df291e54SVladimir Oltean 			mask |= BIT(port);
1202df291e54SVladimir Oltean 	}
1203df291e54SVladimir Oltean 
1204df291e54SVladimir Oltean 	return mask;
1205df291e54SVladimir Oltean }
1206df291e54SVladimir Oltean 
1207e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
12089b521250SVladimir Oltean {
1209e21268efSVladimir Oltean 	u32 mask = 0;
12109b521250SVladimir Oltean 	int port;
12119b521250SVladimir Oltean 
1212e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1213e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1214e21268efSVladimir Oltean 
1215e21268efSVladimir Oltean 		if (!ocelot_port)
1216e21268efSVladimir Oltean 			continue;
1217e21268efSVladimir Oltean 
1218e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
1219e21268efSVladimir Oltean 			mask |= BIT(port);
1220e21268efSVladimir Oltean 	}
1221e21268efSVladimir Oltean 
1222e21268efSVladimir Oltean 	return mask;
1223e21268efSVladimir Oltean }
1224e21268efSVladimir Oltean 
1225e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1226e21268efSVladimir Oltean {
1227e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
1228e21268efSVladimir Oltean 	int port;
1229e21268efSVladimir Oltean 
1230e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
1231e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
1232e21268efSVladimir Oltean 	 * those are bridged or standalone.
1233e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
1234e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
1235e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
1236e21268efSVladimir Oltean 	 */
1237e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1238e21268efSVladimir Oltean 
12399b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
12409b521250SVladimir Oltean 	 * a source for the other ports.
12419b521250SVladimir Oltean 	 */
12429b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1243e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1244e21268efSVladimir Oltean 		unsigned long mask;
1245e21268efSVladimir Oltean 
1246e21268efSVladimir Oltean 		if (!ocelot_port) {
1247e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1248e21268efSVladimir Oltean 			mask = 0;
1249e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1250e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1251e21268efSVladimir Oltean 			 * forward packets to all other ports except for
1252e21268efSVladimir Oltean 			 * themselves
1253e21268efSVladimir Oltean 			 */
1254e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1255e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
1256df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1257df291e54SVladimir Oltean 			struct net_device *bridge = ocelot_port->bridge;
1258528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
12599b521250SVladimir Oltean 
1260df291e54SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, bridge);
1261df291e54SVladimir Oltean 			mask &= ~BIT(port);
126223ca3b72SVladimir Oltean 			if (bond) {
126323ca3b72SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond,
126423ca3b72SVladimir Oltean 							      false);
126523ca3b72SVladimir Oltean 			}
12669b521250SVladimir Oltean 		} else {
1267e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1268e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1269e21268efSVladimir Oltean 			 * module otherwise.
1270e21268efSVladimir Oltean 			 */
1271e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
1272e21268efSVladimir Oltean 		}
1273e21268efSVladimir Oltean 
1274e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
12759b521250SVladimir Oltean 	}
12769b521250SVladimir Oltean }
1277e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
12789b521250SVladimir Oltean 
12795e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1280a556c76aSAlexandre Belloni {
1281421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1282df291e54SVladimir Oltean 	u32 learn_ena = 0;
1283a556c76aSAlexandre Belloni 
1284df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1285a556c76aSAlexandre Belloni 
1286df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1287df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1288df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1289a556c76aSAlexandre Belloni 
1290df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1291df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1292a556c76aSAlexandre Belloni 
12939b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1294a556c76aSAlexandre Belloni }
12955e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1296a556c76aSAlexandre Belloni 
12975e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
12984bda1415SVladimir Oltean {
1299c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1300c0d7eccbSVladimir Oltean 
1301c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1302c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1303c0d7eccbSVladimir Oltean 	 */
1304c0d7eccbSVladimir Oltean 	if (!age_period)
1305c0d7eccbSVladimir Oltean 		age_period = 1;
1306c0d7eccbSVladimir Oltean 
1307c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1308a556c76aSAlexandre Belloni }
13095e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1310a556c76aSAlexandre Belloni 
1311a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1312a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1313a556c76aSAlexandre Belloni 						     u16 vid)
1314a556c76aSAlexandre Belloni {
1315a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1316a556c76aSAlexandre Belloni 
1317a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1318a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1319a556c76aSAlexandre Belloni 			return mc;
1320a556c76aSAlexandre Belloni 	}
1321a556c76aSAlexandre Belloni 
1322a556c76aSAlexandre Belloni 	return NULL;
1323a556c76aSAlexandre Belloni }
1324a556c76aSAlexandre Belloni 
13259403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
13269403c158SVladimir Oltean {
13279403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
13289403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
13299403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
13309403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
13317c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
13329403c158SVladimir Oltean }
13339403c158SVladimir Oltean 
1334e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1335e5d1f896SVladimir Oltean 					     unsigned long ports)
1336e5d1f896SVladimir Oltean {
1337e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1338e5d1f896SVladimir Oltean 
1339e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1340e5d1f896SVladimir Oltean 	if (!pgid)
1341e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1342e5d1f896SVladimir Oltean 
1343e5d1f896SVladimir Oltean 	pgid->ports = ports;
1344e5d1f896SVladimir Oltean 	pgid->index = index;
1345e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1346e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1347e5d1f896SVladimir Oltean 
1348e5d1f896SVladimir Oltean 	return pgid;
1349e5d1f896SVladimir Oltean }
1350e5d1f896SVladimir Oltean 
1351e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1352e5d1f896SVladimir Oltean {
1353e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1354e5d1f896SVladimir Oltean 		return;
1355e5d1f896SVladimir Oltean 
1356e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1357e5d1f896SVladimir Oltean 	kfree(pgid);
1358e5d1f896SVladimir Oltean }
1359e5d1f896SVladimir Oltean 
1360e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1361bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
13629403c158SVladimir Oltean {
1363e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1364e5d1f896SVladimir Oltean 	int index;
13659403c158SVladimir Oltean 
13669403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
13679403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
13689403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
13699403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
13709403c158SVladimir Oltean 	 */
1371bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1372bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1373e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
13749403c158SVladimir Oltean 
1375e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1376e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1377e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1378e5d1f896SVladimir Oltean 		 */
1379e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1380e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1381e5d1f896SVladimir Oltean 			return pgid;
1382e5d1f896SVladimir Oltean 		}
1383e5d1f896SVladimir Oltean 	}
1384e5d1f896SVladimir Oltean 
1385e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1386e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
13879403c158SVladimir Oltean 		bool used = false;
13889403c158SVladimir Oltean 
1389e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1390e5d1f896SVladimir Oltean 			if (pgid->index == index) {
13919403c158SVladimir Oltean 				used = true;
13929403c158SVladimir Oltean 				break;
13939403c158SVladimir Oltean 			}
13949403c158SVladimir Oltean 		}
13959403c158SVladimir Oltean 
13969403c158SVladimir Oltean 		if (!used)
1397e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
13989403c158SVladimir Oltean 	}
13999403c158SVladimir Oltean 
1400e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
14019403c158SVladimir Oltean }
14029403c158SVladimir Oltean 
14039403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1404bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
14059403c158SVladimir Oltean {
1406ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
14079403c158SVladimir Oltean 
1408bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
14099403c158SVladimir Oltean 		addr[0] = 0;
14109403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
14119403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1412bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
14139403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
14149403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
14159403c158SVladimir Oltean 	}
14169403c158SVladimir Oltean }
14179403c158SVladimir Oltean 
1418209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1419209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1420a556c76aSAlexandre Belloni {
1421a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1422004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1423e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1424a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1425a556c76aSAlexandre Belloni 
1426471beb11SVladimir Oltean 	if (port == ocelot->npi)
1427471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1428471beb11SVladimir Oltean 
1429a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1430a556c76aSAlexandre Belloni 	if (!mc) {
1431728e69aeSVladimir Oltean 		/* New entry */
1432bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1433bb8d53fdSVladimir Oltean 		if (!mc)
1434bb8d53fdSVladimir Oltean 			return -ENOMEM;
1435bb8d53fdSVladimir Oltean 
1436bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1437bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1438bb8d53fdSVladimir Oltean 		mc->vid = vid;
1439bb8d53fdSVladimir Oltean 
1440a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1441728e69aeSVladimir Oltean 	} else {
1442e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1443e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1444e5d1f896SVladimir Oltean 		 */
1445e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1446bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1447a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1448a556c76aSAlexandre Belloni 	}
1449a556c76aSAlexandre Belloni 
1450004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1451e5d1f896SVladimir Oltean 
1452e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1453e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1454e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1455e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1456e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1457e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1458e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1459e5d1f896SVladimir Oltean 	}
1460e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1461e5d1f896SVladimir Oltean 
1462bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1463a556c76aSAlexandre Belloni 
1464e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1465e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1466e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1467e5d1f896SVladimir Oltean 				 pgid->index);
1468e5d1f896SVladimir Oltean 
1469e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1470bb8d53fdSVladimir Oltean 				 mc->entry_type);
1471a556c76aSAlexandre Belloni }
1472209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1473a556c76aSAlexandre Belloni 
1474209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1475a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1476a556c76aSAlexandre Belloni {
1477a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1478004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1479e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1480a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1481a556c76aSAlexandre Belloni 
1482471beb11SVladimir Oltean 	if (port == ocelot->npi)
1483471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1484471beb11SVladimir Oltean 
1485a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1486a556c76aSAlexandre Belloni 	if (!mc)
1487a556c76aSAlexandre Belloni 		return -ENOENT;
1488a556c76aSAlexandre Belloni 
1489bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1490a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1491a556c76aSAlexandre Belloni 
1492e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1493004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1494a556c76aSAlexandre Belloni 	if (!mc->ports) {
1495a556c76aSAlexandre Belloni 		list_del(&mc->list);
1496a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1497a556c76aSAlexandre Belloni 		return 0;
1498a556c76aSAlexandre Belloni 	}
1499a556c76aSAlexandre Belloni 
1500e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1501e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1502e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1503e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1504e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1505e5d1f896SVladimir Oltean 
1506bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1507a556c76aSAlexandre Belloni 
1508e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1509e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1510e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1511e5d1f896SVladimir Oltean 				 pgid->index);
1512e5d1f896SVladimir Oltean 
1513e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1514bb8d53fdSVladimir Oltean 				 mc->entry_type);
1515a556c76aSAlexandre Belloni }
1516209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1517a556c76aSAlexandre Belloni 
1518e4bd44e8SVladimir Oltean void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1519a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1520a556c76aSAlexandre Belloni {
1521df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1522a556c76aSAlexandre Belloni 
1523df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
1524a556c76aSAlexandre Belloni 
1525e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1526a556c76aSAlexandre Belloni }
15275e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1528a556c76aSAlexandre Belloni 
1529e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1530a556c76aSAlexandre Belloni 			      struct net_device *bridge)
1531a556c76aSAlexandre Belloni {
1532df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1533c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
15342e554a7aSVladimir Oltean 
1535df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
15367142529fSAntoine Tenart 
1537c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
15382f0402feSVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1539e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1540a556c76aSAlexandre Belloni }
15415e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1542a556c76aSAlexandre Belloni 
1543dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1544dc96ee37SAlexandre Belloni {
1545528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1546dc96ee37SAlexandre Belloni 	int i, port, lag;
1547dc96ee37SAlexandre Belloni 
1548dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
154996b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1550dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1551dc96ee37SAlexandre Belloni 
155296b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1553dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1554dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1555dc96ee37SAlexandre Belloni 
1556528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1557528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1558528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1559528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1560528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1561528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1562528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1563528d3f19SVladimir Oltean 	 */
1564528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1565528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1566528d3f19SVladimir Oltean 
1567528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1568528d3f19SVladimir Oltean 			continue;
1569528d3f19SVladimir Oltean 
1570528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1571528d3f19SVladimir Oltean 	}
1572528d3f19SVladimir Oltean 
1573528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1574dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1575528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
157623ca3b72SVladimir Oltean 		int num_active_ports = 0;
1577dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1578dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1579dc96ee37SAlexandre Belloni 
1580528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1581dc96ee37SAlexandre Belloni 			continue;
1582dc96ee37SAlexandre Belloni 
158323ca3b72SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1584528d3f19SVladimir Oltean 
1585dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1586dc96ee37SAlexandre Belloni 			// Destination mask
1587dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1588dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
158923ca3b72SVladimir Oltean 			aggr_idx[num_active_ports++] = port;
1590dc96ee37SAlexandre Belloni 		}
1591dc96ee37SAlexandre Belloni 
159296b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1593dc96ee37SAlexandre Belloni 			u32 ac;
1594dc96ee37SAlexandre Belloni 
1595dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1596dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
159723ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
159823ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
159923ca3b72SVladimir Oltean 			 */
160023ca3b72SVladimir Oltean 			if (num_active_ports)
160123ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1602dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1603dc96ee37SAlexandre Belloni 		}
1604528d3f19SVladimir Oltean 
1605528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1606528d3f19SVladimir Oltean 		 * the same config again.
1607528d3f19SVladimir Oltean 		 */
1608528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1609528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1610528d3f19SVladimir Oltean 
1611528d3f19SVladimir Oltean 			if (!ocelot_port)
1612528d3f19SVladimir Oltean 				continue;
1613528d3f19SVladimir Oltean 
1614528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1615528d3f19SVladimir Oltean 				visited |= BIT(port);
1616528d3f19SVladimir Oltean 		}
1617dc96ee37SAlexandre Belloni 	}
1618dc96ee37SAlexandre Belloni }
1619dc96ee37SAlexandre Belloni 
16202527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
16212527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
16222527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
16232527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
16242527f2e8SVladimir Oltean  */
16252527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1626dc96ee37SAlexandre Belloni {
16272527f2e8SVladimir Oltean 	int port;
1628dc96ee37SAlexandre Belloni 
16292527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
16302527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
16312527f2e8SVladimir Oltean 		struct net_device *bond;
1632dc96ee37SAlexandre Belloni 
16332527f2e8SVladimir Oltean 		if (!ocelot_port)
16342527f2e8SVladimir Oltean 			continue;
1635dc96ee37SAlexandre Belloni 
16362527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
16372527f2e8SVladimir Oltean 		if (bond) {
163823ca3b72SVladimir Oltean 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
163923ca3b72SVladimir Oltean 							     false));
16402527f2e8SVladimir Oltean 
16412527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
1642dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
16432527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
16442527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
16452527f2e8SVladimir Oltean 		} else {
16462527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
16472527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
16482527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
16492527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
16502527f2e8SVladimir Oltean 		}
1651dc96ee37SAlexandre Belloni 	}
1652dc96ee37SAlexandre Belloni }
1653dc96ee37SAlexandre Belloni 
16549c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1655583cbbe3SVladimir Oltean 			 struct net_device *bond,
1656583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
1657dc96ee37SAlexandre Belloni {
1658583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1659583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
1660583cbbe3SVladimir Oltean 
1661b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
1662dc96ee37SAlexandre Belloni 
16632527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
16649b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1665dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1666dc96ee37SAlexandre Belloni 
1667dc96ee37SAlexandre Belloni 	return 0;
1668dc96ee37SAlexandre Belloni }
16699c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1670dc96ee37SAlexandre Belloni 
16719c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1672dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1673dc96ee37SAlexandre Belloni {
1674b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
1675b80af659SVladimir Oltean 
16762527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
16779b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1678dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1679dc96ee37SAlexandre Belloni }
16809c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
16810e332c85SPetr Machata 
168223ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
168323ca3b72SVladimir Oltean {
168423ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
168523ca3b72SVladimir Oltean 
168623ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
168723ca3b72SVladimir Oltean 
168823ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
168923ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
169023ca3b72SVladimir Oltean }
169123ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
169223ca3b72SVladimir Oltean 
1693a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1694a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
16950b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
16960b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
16970b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1698a8015dedSVladimir Oltean  */
16990b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
170031350d7fSVladimir Oltean {
170131350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1702a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1703e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1704601e984fSVladimir Oltean 	int atop, atop_tot;
170531350d7fSVladimir Oltean 
17060b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
17070b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
17080b912fc9SVladimir Oltean 
1709cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
17100b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1711cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
17120b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
17130b912fc9SVladimir Oltean 	}
17140b912fc9SVladimir Oltean 
1715a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1716fa914e9cSVladimir Oltean 
1717e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1718e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1719e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1720541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1721541132f0SMaxim Kochetkov 			    pause_start);
1722541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1723541132f0SMaxim Kochetkov 			    pause_stop);
1724fa914e9cSVladimir Oltean 
1725601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1726f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1727a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1728601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1729601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1730601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1731fa914e9cSVladimir Oltean }
17320b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
17330b912fc9SVladimir Oltean 
17340b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
17350b912fc9SVladimir Oltean {
17360b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
17370b912fc9SVladimir Oltean 
17380b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
17390b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
17400b912fc9SVladimir Oltean 
1741cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
17420b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1743cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
17440b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
17450b912fc9SVladimir Oltean 	}
17460b912fc9SVladimir Oltean 
17470b912fc9SVladimir Oltean 	return max_mtu;
17480b912fc9SVladimir Oltean }
17490b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1750fa914e9cSVladimir Oltean 
1751421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
1752421741eaSVladimir Oltean 				     bool enabled)
1753421741eaSVladimir Oltean {
1754421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1755421741eaSVladimir Oltean 	u32 val = 0;
1756421741eaSVladimir Oltean 
1757421741eaSVladimir Oltean 	if (enabled)
1758421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
1759421741eaSVladimir Oltean 
1760421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
1761421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1762421741eaSVladimir Oltean 
1763421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
1764421741eaSVladimir Oltean }
1765421741eaSVladimir Oltean 
1766421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
1767421741eaSVladimir Oltean 					bool enabled)
1768421741eaSVladimir Oltean {
1769421741eaSVladimir Oltean 	u32 val = 0;
1770421741eaSVladimir Oltean 
1771421741eaSVladimir Oltean 	if (enabled)
1772421741eaSVladimir Oltean 		val = BIT(port);
1773421741eaSVladimir Oltean 
1774421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
1775421741eaSVladimir Oltean }
1776421741eaSVladimir Oltean 
1777421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
1778421741eaSVladimir Oltean 					bool enabled)
1779421741eaSVladimir Oltean {
1780421741eaSVladimir Oltean 	u32 val = 0;
1781421741eaSVladimir Oltean 
1782421741eaSVladimir Oltean 	if (enabled)
1783421741eaSVladimir Oltean 		val = BIT(port);
1784421741eaSVladimir Oltean 
1785421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
1786421741eaSVladimir Oltean }
1787421741eaSVladimir Oltean 
1788421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
1789421741eaSVladimir Oltean 					bool enabled)
1790421741eaSVladimir Oltean {
1791421741eaSVladimir Oltean 	u32 val = 0;
1792421741eaSVladimir Oltean 
1793421741eaSVladimir Oltean 	if (enabled)
1794421741eaSVladimir Oltean 		val = BIT(port);
1795421741eaSVladimir Oltean 
1796421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
1797421741eaSVladimir Oltean }
1798421741eaSVladimir Oltean 
1799421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
1800421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
1801421741eaSVladimir Oltean {
1802421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1803421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
1804421741eaSVladimir Oltean 		return -EINVAL;
1805421741eaSVladimir Oltean 
1806421741eaSVladimir Oltean 	return 0;
1807421741eaSVladimir Oltean }
1808421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
1809421741eaSVladimir Oltean 
1810421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
1811421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
1812421741eaSVladimir Oltean {
1813421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
1814421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
1815421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
1816421741eaSVladimir Oltean 
1817421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
1818421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
1819421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
1820421741eaSVladimir Oltean 
1821421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
1822421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
1823421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
1824421741eaSVladimir Oltean 
1825421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
1826421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
1827421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
1828421741eaSVladimir Oltean }
1829421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
1830421741eaSVladimir Oltean 
18315e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1832fa914e9cSVladimir Oltean {
1833fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1834fa914e9cSVladimir Oltean 
1835b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
18366565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
183731350d7fSVladimir Oltean 
183831350d7fSVladimir Oltean 	/* Basic L2 initialization */
183931350d7fSVladimir Oltean 
18405bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
18415bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
18425bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
18435bc9d2e6SVladimir Oltean 	 */
18445bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
18455bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
18465bc9d2e6SVladimir Oltean 
18475bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
18485bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
18495bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
18505bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
18515bc9d2e6SVladimir Oltean 	mdelay(1);
18525bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
18535bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
18545bc9d2e6SVladimir Oltean 
18555bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1856a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
18575bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
18585bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1859a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
18605bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
18615bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
18625bc9d2e6SVladimir Oltean 
18635bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
18645bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
18655bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
18665bc9d2e6SVladimir Oltean 
1867e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1868541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1869e8e6e73dSVladimir Oltean 
187031350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
187131350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
187231350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
187331350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
187431350d7fSVladimir Oltean 
187531350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
187631350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
187731350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
187831350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
187931350d7fSVladimir Oltean 
1880421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
1881421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
1882421741eaSVladimir Oltean 
188331350d7fSVladimir Oltean 	/* Enable vcap lookups */
188431350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
188531350d7fSVladimir Oltean }
18865e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
188731350d7fSVladimir Oltean 
18882d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
18892d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
18902d44b097SVladimir Oltean  * NPI mode is used).
189169df578cSVladimir Oltean  */
18922d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
189321468199SVladimir Oltean {
189469df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
189569df578cSVladimir Oltean 
189669df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
189721468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
189869df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
189969df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
190069df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
190169df578cSVladimir Oltean 	 */
190221468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
190321468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
190421468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
190521468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
190621468199SVladimir Oltean 
190769df578cSVladimir Oltean 	/* Enable CPU port module */
1908886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
190969df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1910886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
1911cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
1912886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
1913cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
191421468199SVladimir Oltean 
191521468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
191621468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
191721468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
191821468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
191921468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
192021468199SVladimir Oltean }
192121468199SVladimir Oltean 
1922f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
1923f6fe01d6SVladimir Oltean {
1924f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
1925f6fe01d6SVladimir Oltean 
1926f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
1927f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
1928f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
1929f6fe01d6SVladimir Oltean 	 */
1930f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
1931f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
1932f6fe01d6SVladimir Oltean 
1933f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
1934f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
1935f6fe01d6SVladimir Oltean }
1936f6fe01d6SVladimir Oltean 
1937a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1938a556c76aSAlexandre Belloni {
1939a556c76aSAlexandre Belloni 	char queue_name[32];
194021468199SVladimir Oltean 	int i, ret;
194121468199SVladimir Oltean 	u32 port;
1942a556c76aSAlexandre Belloni 
19433a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
19443a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
19453a77b593SVladimir Oltean 		if (ret) {
19463a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
19473a77b593SVladimir Oltean 			return ret;
19483a77b593SVladimir Oltean 		}
19493a77b593SVladimir Oltean 	}
19503a77b593SVladimir Oltean 
1951a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1952a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1953a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1954a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1955a556c76aSAlexandre Belloni 		return -ENOMEM;
1956a556c76aSAlexandre Belloni 
1957a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
19584e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
19594e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1960a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1961a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1962a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1963a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1964a556c76aSAlexandre Belloni 		return -ENOMEM;
1965a556c76aSAlexandre Belloni 
1966ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1967ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
1968ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
1969ca0b272bSVladimir Oltean 		return -ENOMEM;
1970ca0b272bSVladimir Oltean 	}
1971ca0b272bSVladimir Oltean 
19722b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1973e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
1974f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
1975a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1976a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1977aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
19782d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
1979a556c76aSAlexandre Belloni 
1980a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1981a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1982a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1983a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1984a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1985a556c76aSAlexandre Belloni 	}
1986a556c76aSAlexandre Belloni 
1987a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1988a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1989a556c76aSAlexandre Belloni 
1990a556c76aSAlexandre Belloni 	/* Aggregation mode */
1991a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1992a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1993a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1994f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
1995f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
1996f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
1997f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
1998a556c76aSAlexandre Belloni 
1999a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2000a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2001a556c76aSAlexandre Belloni 	 */
2002a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2003a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2004a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2005a556c76aSAlexandre Belloni 
2006a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2007a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2008a556c76aSAlexandre Belloni 
2009a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2010a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2011a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2012a556c76aSAlexandre Belloni 
2013a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2014edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2015a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2016b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2017a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2018edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2019a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2020a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2021a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2022a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2023a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2024a556c76aSAlexandre Belloni 
2025a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2026a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2027a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2028a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2029a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2030a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2031a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2032a556c76aSAlexandre Belloni 				 port);
2033a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2034a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2035a556c76aSAlexandre Belloni 	}
2036a556c76aSAlexandre Belloni 
203796b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2038a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2039a556c76aSAlexandre Belloni 
2040a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2041a556c76aSAlexandre Belloni 	}
2042ebb1bb40SHoratiu Vultur 
2043ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2044ebb1bb40SHoratiu Vultur 
2045b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2046b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2047b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2048a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2049b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2050b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2051b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2052a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2053a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2054a556c76aSAlexandre Belloni 
2055a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2056a556c76aSAlexandre Belloni 	 * registers endianness.
2057a556c76aSAlexandre Belloni 	 */
2058a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2059a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2060a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2061a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2062a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2063a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2064a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2065a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2066a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2067a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2068a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2069a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2070a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2071a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2072a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2073a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2074a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2075a556c76aSAlexandre Belloni 
20761e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2077a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2078a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
20794e3b0468SAntoine Tenart 
2080a556c76aSAlexandre Belloni 	return 0;
2081a556c76aSAlexandre Belloni }
2082a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2083a556c76aSAlexandre Belloni 
2084a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2085a556c76aSAlexandre Belloni {
2086c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2087a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2088ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2089a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2090a556c76aSAlexandre Belloni }
2091a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2092a556c76aSAlexandre Belloni 
2093e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2094e5fb512dSVladimir Oltean {
2095e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2096e5fb512dSVladimir Oltean 
2097e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2098e5fb512dSVladimir Oltean }
2099e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2100e5fb512dSVladimir Oltean 
2101a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2102