xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 2ed2c5f0391106406ead3a74bfa571575eafe8b6)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
8a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
920968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10a556c76aSAlexandre Belloni #include "ocelot.h"
113c83654fSVladimir Oltean #include "ocelot_vcap.h"
12a556c76aSAlexandre Belloni 
13639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
14639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
15639c1b26SSteen Hegelund 
16a556c76aSAlexandre Belloni struct ocelot_mact_entry {
17a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
18a556c76aSAlexandre Belloni 	u16 vid;
19a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
20a556c76aSAlexandre Belloni };
21a556c76aSAlexandre Belloni 
22639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
23639c1b26SSteen Hegelund {
24639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
25639c1b26SSteen Hegelund }
26639c1b26SSteen Hegelund 
27a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
28a556c76aSAlexandre Belloni {
29639c1b26SSteen Hegelund 	u32 val;
30a556c76aSAlexandre Belloni 
31639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
32639c1b26SSteen Hegelund 		ocelot, val,
33639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
34639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
35639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
36a556c76aSAlexandre Belloni }
37a556c76aSAlexandre Belloni 
38a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
39a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
40a556c76aSAlexandre Belloni 			       unsigned int vid)
41a556c76aSAlexandre Belloni {
42a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
43a556c76aSAlexandre Belloni 
44a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
45a556c76aSAlexandre Belloni 	 * understood by the hardware.
46a556c76aSAlexandre Belloni 	 */
47a556c76aSAlexandre Belloni 	mach |= vid    << 16;
48a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
49a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
50a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
51a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
52a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
53a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
54a556c76aSAlexandre Belloni 
55a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
56a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
57a556c76aSAlexandre Belloni 
58a556c76aSAlexandre Belloni }
59a556c76aSAlexandre Belloni 
609c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
61a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
629c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
63a556c76aSAlexandre Belloni {
64584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
65584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
66584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
67584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
68584b7cfcSAlban Bedel 	unsigned int mc_ports;
69584b7cfcSAlban Bedel 
70584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
71584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
72584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
73584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
74584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
75584b7cfcSAlban Bedel 	else
76584b7cfcSAlban Bedel 		mc_ports = 0;
77584b7cfcSAlban Bedel 
78584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
79584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
80584b7cfcSAlban Bedel 
81a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
82a556c76aSAlexandre Belloni 
83a556c76aSAlexandre Belloni 	/* Issue a write command */
84584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
85a556c76aSAlexandre Belloni 
86a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
87a556c76aSAlexandre Belloni }
889c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
89a556c76aSAlexandre Belloni 
909c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
919c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
92a556c76aSAlexandre Belloni {
93a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
94a556c76aSAlexandre Belloni 
95a556c76aSAlexandre Belloni 	/* Issue a forget command */
96a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
97a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
98a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
99a556c76aSAlexandre Belloni 
100a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
101a556c76aSAlexandre Belloni }
1029c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
103a556c76aSAlexandre Belloni 
104a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
105a556c76aSAlexandre Belloni {
106a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
107a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
108a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
109a556c76aSAlexandre Belloni 	 */
110a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
111a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
112a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
113a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
114a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
115a556c76aSAlexandre Belloni 
116a556c76aSAlexandre Belloni 	/* Clear the MAC table */
117a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
118a556c76aSAlexandre Belloni }
119a556c76aSAlexandre Belloni 
120f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
121b5962294SHoratiu Vultur {
122b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
123b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
124f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
12575944fdaSXiaoliang Yang 
12675944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
12775944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1282f17c050SXiaoliang Yang 
1292f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1302f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1312f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
132b5962294SHoratiu Vultur }
133b5962294SHoratiu Vultur 
134639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
135639c1b26SSteen Hegelund {
136639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
137639c1b26SSteen Hegelund }
138639c1b26SSteen Hegelund 
139a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
140a556c76aSAlexandre Belloni {
141639c1b26SSteen Hegelund 	u32 val;
142a556c76aSAlexandre Belloni 
143639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
144639c1b26SSteen Hegelund 		ocelot,
145639c1b26SSteen Hegelund 		val,
146639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
147639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
148639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
149a556c76aSAlexandre Belloni }
150a556c76aSAlexandre Belloni 
1517142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1527142529fSAntoine Tenart {
1537142529fSAntoine Tenart 	/* Select the VID to configure */
1547142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1557142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1567142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1577142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1587142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1597142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1607142529fSAntoine Tenart 
1617142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1627142529fSAntoine Tenart }
1637142529fSAntoine Tenart 
1642f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
165c3e58a75SVladimir Oltean 					struct ocelot_vlan native_vlan)
16697bb69e1SVladimir Oltean {
16797bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
16887b0f983SVladimir Oltean 	u32 val = 0;
16997bb69e1SVladimir Oltean 
170c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
17197bb69e1SVladimir Oltean 
172c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1737142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
17497bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17597bb69e1SVladimir Oltean 
17687b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
177e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
17887b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
17987b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
18087b0f983SVladimir Oltean 		else
18187b0f983SVladimir Oltean 			/* Tag all frames */
18287b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
18387b0f983SVladimir Oltean 	} else {
18487b0f983SVladimir Oltean 		/* Port tagging disabled. */
18587b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
18687b0f983SVladimir Oltean 	}
18787b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
18887b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
18987b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
19097bb69e1SVladimir Oltean }
19197bb69e1SVladimir Oltean 
19275e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
193c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
194c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
19575e5a554SVladimir Oltean {
19675e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
197be0576feSVladimir Oltean 	u32 val = 0;
19875e5a554SVladimir Oltean 
199c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
20075e5a554SVladimir Oltean 
20175e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
202c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
20375e5a554SVladimir Oltean 
20475e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
205c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
20675e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
20775e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
208be0576feSVladimir Oltean 
209be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
210be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
211be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
212be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
213be0576feSVladimir Oltean 	 */
214be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
215be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
216be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
217be0576feSVladimir Oltean 
218be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
219be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
220be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
221be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
22275e5a554SVladimir Oltean }
22375e5a554SVladimir Oltean 
2242e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
225bae33f2bSVladimir Oltean 			       bool vlan_aware)
22687b0f983SVladimir Oltean {
22770edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
228bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
22970edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
230bae33f2bSVladimir Oltean 	u32 val;
23170edfae1SVladimir Oltean 
23270edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
23370edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
23470edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
23570edfae1SVladimir Oltean 			dev_err(ocelot->dev,
23670edfae1SVladimir Oltean 				"Cannot change VLAN state with vlan modify rules active\n");
23770edfae1SVladimir Oltean 			return -EBUSY;
23870edfae1SVladimir Oltean 		}
23970edfae1SVladimir Oltean 	}
24070edfae1SVladimir Oltean 
24187b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
24287b0f983SVladimir Oltean 
24387b0f983SVladimir Oltean 	if (vlan_aware)
24487b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24587b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
24687b0f983SVladimir Oltean 	else
24787b0f983SVladimir Oltean 		val = 0;
24887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
24987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
25087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
25187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
25287b0f983SVladimir Oltean 
253c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
254c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2552e554a7aSVladimir Oltean 
2562e554a7aSVladimir Oltean 	return 0;
25787b0f983SVladimir Oltean }
25887b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
25987b0f983SVladimir Oltean 
2602f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2612f0402feSVladimir Oltean 			bool untagged)
2622f0402feSVladimir Oltean {
2632f0402feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2642f0402feSVladimir Oltean 
2652f0402feSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
2662f0402feSVladimir Oltean 	if (untagged && ocelot_port->native_vlan.vid != vid &&
2672f0402feSVladimir Oltean 	    ocelot_port->native_vlan.valid) {
2682f0402feSVladimir Oltean 		dev_err(ocelot->dev,
2692f0402feSVladimir Oltean 			"Port already has a native VLAN: %d\n",
2702f0402feSVladimir Oltean 			ocelot_port->native_vlan.vid);
2712f0402feSVladimir Oltean 		return -EBUSY;
2722f0402feSVladimir Oltean 	}
2732f0402feSVladimir Oltean 
2742f0402feSVladimir Oltean 	return 0;
2752f0402feSVladimir Oltean }
2762f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
2772f0402feSVladimir Oltean 
2785e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2797142529fSAntoine Tenart 		    bool untagged)
2807142529fSAntoine Tenart {
2817142529fSAntoine Tenart 	int ret;
2827142529fSAntoine Tenart 
2837142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
28497bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2857142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2867142529fSAntoine Tenart 	if (ret)
2877142529fSAntoine Tenart 		return ret;
2887142529fSAntoine Tenart 
2897142529fSAntoine Tenart 	/* Default ingress vlan classification */
290c3e58a75SVladimir Oltean 	if (pvid) {
291c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
292c3e58a75SVladimir Oltean 
293c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
294e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
295c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
296c3e58a75SVladimir Oltean 	}
2977142529fSAntoine Tenart 
2987142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
29997bb69e1SVladimir Oltean 	if (untagged) {
300c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
301c3e58a75SVladimir Oltean 
302c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
303e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
3042f0402feSVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
305b9cd75e6SVladimir Oltean 	}
3067142529fSAntoine Tenart 
3077142529fSAntoine Tenart 	return 0;
3087142529fSAntoine Tenart }
3095e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
3107142529fSAntoine Tenart 
3115e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3129855934cSVladimir Oltean {
3139855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3149855934cSVladimir Oltean 	int ret;
3157142529fSAntoine Tenart 
3167142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
31797bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3187142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3197142529fSAntoine Tenart 	if (ret)
3207142529fSAntoine Tenart 		return ret;
3217142529fSAntoine Tenart 
322be0576feSVladimir Oltean 	/* Ingress */
323be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
324be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
325be0576feSVladimir Oltean 
326be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
327be0576feSVladimir Oltean 	}
328be0576feSVladimir Oltean 
3297142529fSAntoine Tenart 	/* Egress */
330c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
331e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
332c3e58a75SVladimir Oltean 
333c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
334c3e58a75SVladimir Oltean 	}
3357142529fSAntoine Tenart 
3367142529fSAntoine Tenart 	return 0;
3377142529fSAntoine Tenart }
3385e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3397142529fSAntoine Tenart 
340a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
341a556c76aSAlexandre Belloni {
3427142529fSAntoine Tenart 	u16 port, vid;
3437142529fSAntoine Tenart 
344a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
345a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
346a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
347a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3487142529fSAntoine Tenart 
3497142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3507142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3517142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3527142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3537142529fSAntoine Tenart 	}
3547142529fSAntoine Tenart 
3557142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3567142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3577142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3587142529fSAntoine Tenart 	 */
3597142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3607142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3617142529fSAntoine Tenart 
3627142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3637142529fSAntoine Tenart 	 * default.
3647142529fSAntoine Tenart 	 */
365714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
366714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3677142529fSAntoine Tenart 
3687142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3697142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3707142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3717142529fSAntoine Tenart 	}
372a556c76aSAlexandre Belloni }
373a556c76aSAlexandre Belloni 
374eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
375eb4733d7SVladimir Oltean {
376eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
377eb4733d7SVladimir Oltean }
378eb4733d7SVladimir Oltean 
379eb4733d7SVladimir Oltean int ocelot_port_flush(struct ocelot *ocelot, int port)
380eb4733d7SVladimir Oltean {
381eb4733d7SVladimir Oltean 	int err, val;
382eb4733d7SVladimir Oltean 
383eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
384eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
385eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
386eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
387eb4733d7SVladimir Oltean 
388eb4733d7SVladimir Oltean 	/* Disable flow control */
389eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
390eb4733d7SVladimir Oltean 
391eb4733d7SVladimir Oltean 	/* Disable priority flow control */
392eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
393eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
394eb4733d7SVladimir Oltean 
395eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
396eb4733d7SVladimir Oltean 	 * at the port.
397eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
398eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
399eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
400eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
401eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
402eb4733d7SVladimir Oltean 	 */
403eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
404eb4733d7SVladimir Oltean 
405eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
406eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
407eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
408eb4733d7SVladimir Oltean 
409eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
410eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
411eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
412eb4733d7SVladimir Oltean 
413eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
414eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
415eb4733d7SVladimir Oltean 		       port);
416eb4733d7SVladimir Oltean 
417eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
418eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
419eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
420eb4733d7SVladimir Oltean 
421eb4733d7SVladimir Oltean 	/* Clear flushing again. */
422eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
423eb4733d7SVladimir Oltean 
424eb4733d7SVladimir Oltean 	return err;
425eb4733d7SVladimir Oltean }
426eb4733d7SVladimir Oltean EXPORT_SYMBOL(ocelot_port_flush);
427eb4733d7SVladimir Oltean 
4285e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
42926f4dbabSVladimir Oltean 			struct phy_device *phydev)
430a556c76aSAlexandre Belloni {
43126f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
4325bc9d2e6SVladimir Oltean 	int speed, mode = 0;
433a556c76aSAlexandre Belloni 
43426f4dbabSVladimir Oltean 	switch (phydev->speed) {
435a556c76aSAlexandre Belloni 	case SPEED_10:
436a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
437a556c76aSAlexandre Belloni 		break;
438a556c76aSAlexandre Belloni 	case SPEED_100:
439a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
440a556c76aSAlexandre Belloni 		break;
441a556c76aSAlexandre Belloni 	case SPEED_1000:
442a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
443a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
444a556c76aSAlexandre Belloni 		break;
445a556c76aSAlexandre Belloni 	case SPEED_2500:
446a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
447a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
448a556c76aSAlexandre Belloni 		break;
449a556c76aSAlexandre Belloni 	default:
45026f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
45126f4dbabSVladimir Oltean 			port, phydev->speed);
452a556c76aSAlexandre Belloni 		return;
453a556c76aSAlexandre Belloni 	}
454a556c76aSAlexandre Belloni 
45526f4dbabSVladimir Oltean 	phy_print_status(phydev);
456a556c76aSAlexandre Belloni 
45726f4dbabSVladimir Oltean 	if (!phydev->link)
458a556c76aSAlexandre Belloni 		return;
459a556c76aSAlexandre Belloni 
460a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
461004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
462a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
463a556c76aSAlexandre Belloni 
4641ba8f656SVladimir Oltean 	/* Disable HDX fast control */
4651ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
4661ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
4671ba8f656SVladimir Oltean 
4681ba8f656SVladimir Oltean 	/* SGMII only for now */
4691ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4701ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
4711ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4721ba8f656SVladimir Oltean 
4731ba8f656SVladimir Oltean 	/* Enable PCS */
4741ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4751ba8f656SVladimir Oltean 
4761ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4771ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4781ba8f656SVladimir Oltean 
4791ba8f656SVladimir Oltean 	/* No loopback */
4801ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
481a556c76aSAlexandre Belloni 
482a556c76aSAlexandre Belloni 	/* Enable MAC module */
483004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
484a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
485a556c76aSAlexandre Belloni 
486a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
487a556c76aSAlexandre Belloni 	 * reset */
488004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
489a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
490a556c76aSAlexandre Belloni 
491a556c76aSAlexandre Belloni 	/* No PFC */
492a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
493004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
494a556c76aSAlexandre Belloni 
495a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
496886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
497886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
498a556c76aSAlexandre Belloni 
499a556c76aSAlexandre Belloni 	/* Flow control */
500a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
501a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
502a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
503a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
504a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
505004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
506004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
507a556c76aSAlexandre Belloni }
5085e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
509a556c76aSAlexandre Belloni 
5105e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
511889b8950SVladimir Oltean 			struct phy_device *phy)
512a556c76aSAlexandre Belloni {
513a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
514a556c76aSAlexandre Belloni 	 * MAC addresses.
515a556c76aSAlexandre Belloni 	 */
516a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
517a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
518004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
519004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
520889b8950SVladimir Oltean }
5215e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
522889b8950SVladimir Oltean 
5235e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
524889b8950SVladimir Oltean {
525889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
526889b8950SVladimir Oltean 
527889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
528886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
529889b8950SVladimir Oltean }
5305e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
531889b8950SVladimir Oltean 
532e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
533e2f9a8feSVladimir Oltean 				  struct sk_buff *clone)
534400928bfSYangbo Lu {
535e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
536400928bfSYangbo Lu 
5376565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
5386565243cSVladimir Oltean 
539e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
540b049da13SYangbo Lu 	/* Store timestamp ID in cb[0] of sk_buff */
541e2f9a8feSVladimir Oltean 	clone->cb[0] = ocelot_port->ts_id;
5426565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
543e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
5446565243cSVladimir Oltean 
5456565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
546400928bfSYangbo Lu }
547400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
548400928bfSYangbo Lu 
549e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
550e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
5514e3b0468SAntoine Tenart {
5524e3b0468SAntoine Tenart 	unsigned long flags;
5534e3b0468SAntoine Tenart 	u32 val;
5544e3b0468SAntoine Tenart 
5554e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
5564e3b0468SAntoine Tenart 
5574e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
5584e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
5594e3b0468SAntoine Tenart 
5604e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
5614e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
5624e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
5634e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
5644e3b0468SAntoine Tenart 
5654e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
5664e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
5674e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
5684e3b0468SAntoine Tenart 
5694e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
5704e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
5714e3b0468SAntoine Tenart 		ts->tv_sec--;
5724e3b0468SAntoine Tenart 
5734e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
5744e3b0468SAntoine Tenart }
575e23a7b3eSYangbo Lu 
576e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
577e23a7b3eSYangbo Lu {
578e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
579e23a7b3eSYangbo Lu 
580e23a7b3eSYangbo Lu 	while (budget--) {
581b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
582e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
583e23a7b3eSYangbo Lu 		struct ocelot_port *port;
584e23a7b3eSYangbo Lu 		struct timespec64 ts;
585b049da13SYangbo Lu 		unsigned long flags;
586e23a7b3eSYangbo Lu 		u32 val, id, txport;
587e23a7b3eSYangbo Lu 
588e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
589e23a7b3eSYangbo Lu 
590e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
591e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
592e23a7b3eSYangbo Lu 			break;
593e23a7b3eSYangbo Lu 
594e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
595e23a7b3eSYangbo Lu 
596e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
597e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
598e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
599e23a7b3eSYangbo Lu 
600e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
601e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
602e23a7b3eSYangbo Lu 
603b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
604b049da13SYangbo Lu 
605b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
606b049da13SYangbo Lu 			if (skb->cb[0] != id)
607e23a7b3eSYangbo Lu 				continue;
608b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
609b049da13SYangbo Lu 			skb_match = skb;
610fc62c094SYangbo Lu 			break;
611e23a7b3eSYangbo Lu 		}
612e23a7b3eSYangbo Lu 
613b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
614b049da13SYangbo Lu 
6155fd82200Slaurent brando 		/* Get the h/w timestamp */
6165fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
617e23a7b3eSYangbo Lu 
618b049da13SYangbo Lu 		if (unlikely(!skb_match))
619e23a7b3eSYangbo Lu 			continue;
620e23a7b3eSYangbo Lu 
621e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
622e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
623e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
624e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
6255fd82200Slaurent brando 
6265fd82200Slaurent brando 		/* Next ts */
6275fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
628e23a7b3eSYangbo Lu 	}
629e23a7b3eSYangbo Lu }
630e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
6314e3b0468SAntoine Tenart 
632924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
633924ee317SVladimir Oltean 				u32 *rval)
634924ee317SVladimir Oltean {
635924ee317SVladimir Oltean 	u32 bytes_valid, val;
636924ee317SVladimir Oltean 
637924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
638924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
639924ee317SVladimir Oltean 		if (ifh)
640924ee317SVladimir Oltean 			return -EIO;
641924ee317SVladimir Oltean 
642924ee317SVladimir Oltean 		do {
643924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
644924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
645924ee317SVladimir Oltean 	}
646924ee317SVladimir Oltean 
647924ee317SVladimir Oltean 	switch (val) {
648924ee317SVladimir Oltean 	case XTR_ABORT:
649924ee317SVladimir Oltean 		return -EIO;
650924ee317SVladimir Oltean 	case XTR_EOF_0:
651924ee317SVladimir Oltean 	case XTR_EOF_1:
652924ee317SVladimir Oltean 	case XTR_EOF_2:
653924ee317SVladimir Oltean 	case XTR_EOF_3:
654924ee317SVladimir Oltean 	case XTR_PRUNED:
655924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
656924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
657924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
658924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
659924ee317SVladimir Oltean 		else
660924ee317SVladimir Oltean 			*rval = val;
661924ee317SVladimir Oltean 
662924ee317SVladimir Oltean 		return bytes_valid;
663924ee317SVladimir Oltean 	case XTR_ESCAPE:
664924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
665924ee317SVladimir Oltean 
666924ee317SVladimir Oltean 		return 4;
667924ee317SVladimir Oltean 	default:
668924ee317SVladimir Oltean 		*rval = val;
669924ee317SVladimir Oltean 
670924ee317SVladimir Oltean 		return 4;
671924ee317SVladimir Oltean 	}
672924ee317SVladimir Oltean }
673924ee317SVladimir Oltean 
674924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
675924ee317SVladimir Oltean {
676924ee317SVladimir Oltean 	int i, err = 0;
677924ee317SVladimir Oltean 
678924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
679924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
680924ee317SVladimir Oltean 		if (err != 4)
681924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
682924ee317SVladimir Oltean 	}
683924ee317SVladimir Oltean 
684924ee317SVladimir Oltean 	return 0;
685924ee317SVladimir Oltean }
686924ee317SVladimir Oltean 
687924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
688924ee317SVladimir Oltean {
689924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
690*2ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
691924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
692924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
693924ee317SVladimir Oltean 	struct net_device *dev;
694924ee317SVladimir Oltean 	struct timespec64 ts;
695924ee317SVladimir Oltean 	struct sk_buff *skb;
696924ee317SVladimir Oltean 	int sz, buf_len;
697924ee317SVladimir Oltean 	u32 val, *buf;
698924ee317SVladimir Oltean 	int err;
699924ee317SVladimir Oltean 
700924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
701924ee317SVladimir Oltean 	if (err)
702924ee317SVladimir Oltean 		return err;
703924ee317SVladimir Oltean 
704924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
705924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
706924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
707924ee317SVladimir Oltean 
708924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
709924ee317SVladimir Oltean 		return -EINVAL;
710924ee317SVladimir Oltean 
711924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
712924ee317SVladimir Oltean 	if (!dev)
713924ee317SVladimir Oltean 		return -EINVAL;
714924ee317SVladimir Oltean 
715924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
716924ee317SVladimir Oltean 	if (unlikely(!skb)) {
717924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
718924ee317SVladimir Oltean 		return -ENOMEM;
719924ee317SVladimir Oltean 	}
720924ee317SVladimir Oltean 
721924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
722924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
723924ee317SVladimir Oltean 
724924ee317SVladimir Oltean 	len = 0;
725924ee317SVladimir Oltean 	do {
726924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
727924ee317SVladimir Oltean 		if (sz < 0) {
728924ee317SVladimir Oltean 			err = sz;
729924ee317SVladimir Oltean 			goto out_free_skb;
730924ee317SVladimir Oltean 		}
731924ee317SVladimir Oltean 		*buf++ = val;
732924ee317SVladimir Oltean 		len += sz;
733924ee317SVladimir Oltean 	} while (len < buf_len);
734924ee317SVladimir Oltean 
735924ee317SVladimir Oltean 	/* Read the FCS */
736924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
737924ee317SVladimir Oltean 	if (sz < 0) {
738924ee317SVladimir Oltean 		err = sz;
739924ee317SVladimir Oltean 		goto out_free_skb;
740924ee317SVladimir Oltean 	}
741924ee317SVladimir Oltean 
742924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
743924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
744924ee317SVladimir Oltean 
745924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
746924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
747924ee317SVladimir Oltean 		*buf = val;
748924ee317SVladimir Oltean 	}
749924ee317SVladimir Oltean 
750924ee317SVladimir Oltean 	if (ocelot->ptp) {
751924ee317SVladimir Oltean 		ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
752924ee317SVladimir Oltean 
753924ee317SVladimir Oltean 		tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
754924ee317SVladimir Oltean 		if ((tod_in_ns & 0xffffffff) < timestamp)
755924ee317SVladimir Oltean 			full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
756924ee317SVladimir Oltean 					timestamp;
757924ee317SVladimir Oltean 		else
758924ee317SVladimir Oltean 			full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
759924ee317SVladimir Oltean 					timestamp;
760924ee317SVladimir Oltean 
761924ee317SVladimir Oltean 		shhwtstamps = skb_hwtstamps(skb);
762924ee317SVladimir Oltean 		memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
763924ee317SVladimir Oltean 		shhwtstamps->hwtstamp = full_ts_in_ns;
764924ee317SVladimir Oltean 	}
765924ee317SVladimir Oltean 
766924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
767924ee317SVladimir Oltean 	 * has already been forwarded.
768924ee317SVladimir Oltean 	 */
769924ee317SVladimir Oltean 	if (ocelot->bridge_mask & BIT(src_port))
770924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
771924ee317SVladimir Oltean 
772924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
773d8ea7ff3SHoratiu Vultur 
774924ee317SVladimir Oltean 	*nskb = skb;
775924ee317SVladimir Oltean 
776924ee317SVladimir Oltean 	return 0;
777924ee317SVladimir Oltean 
778924ee317SVladimir Oltean out_free_skb:
779924ee317SVladimir Oltean 	kfree_skb(skb);
780924ee317SVladimir Oltean 	return err;
781924ee317SVladimir Oltean }
782924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
783924ee317SVladimir Oltean 
784137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
785137ffbc4SVladimir Oltean {
786137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
787137ffbc4SVladimir Oltean 
788137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
789137ffbc4SVladimir Oltean 		return false;
790137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
791137ffbc4SVladimir Oltean 		return false;
792137ffbc4SVladimir Oltean 
793137ffbc4SVladimir Oltean 	return true;
794137ffbc4SVladimir Oltean }
795137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
796137ffbc4SVladimir Oltean 
797137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
798137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
799137ffbc4SVladimir Oltean {
80040d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
801137ffbc4SVladimir Oltean 	unsigned int i, count, last;
802137ffbc4SVladimir Oltean 
803137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
804137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
805137ffbc4SVladimir Oltean 
80640d3f295SVladimir Oltean 	ocelot_ifh_set_bypass(ifh, 1);
8071f778d50SVladimir Oltean 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
80840d3f295SVladimir Oltean 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
80940d3f295SVladimir Oltean 	ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
81040d3f295SVladimir Oltean 	ocelot_ifh_set_rew_op(ifh, rew_op);
811137ffbc4SVladimir Oltean 
812137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
81340d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
814137ffbc4SVladimir Oltean 
815137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
816137ffbc4SVladimir Oltean 	last = skb->len % 4;
817137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
818137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
819137ffbc4SVladimir Oltean 
820137ffbc4SVladimir Oltean 	/* Add padding */
821137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
822137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
823137ffbc4SVladimir Oltean 		i++;
824137ffbc4SVladimir Oltean 	}
825137ffbc4SVladimir Oltean 
826137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
827137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
828137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
829137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
830137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
831137ffbc4SVladimir Oltean 
832137ffbc4SVladimir Oltean 	/* Add dummy CRC */
833137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
834137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
835137ffbc4SVladimir Oltean 
836137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
837137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
838137ffbc4SVladimir Oltean }
839137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
840137ffbc4SVladimir Oltean 
8410a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
8420a6f17c6SVladimir Oltean {
8430a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
8440a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
8450a6f17c6SVladimir Oltean }
8460a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
8470a6f17c6SVladimir Oltean 
8485e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
84987b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
850a556c76aSAlexandre Belloni {
851471beb11SVladimir Oltean 	int pgid = port;
852471beb11SVladimir Oltean 
853471beb11SVladimir Oltean 	if (port == ocelot->npi)
854471beb11SVladimir Oltean 		pgid = PGID_CPU;
855a556c76aSAlexandre Belloni 
856471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
857a556c76aSAlexandre Belloni }
8585e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
859a556c76aSAlexandre Belloni 
8605e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
861531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
862531ee1a6SVladimir Oltean {
863531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
864531ee1a6SVladimir Oltean }
8655e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
866531ee1a6SVladimir Oltean 
8679c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
868531ee1a6SVladimir Oltean 			    bool is_static, void *data)
869a556c76aSAlexandre Belloni {
870531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
871a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
872a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
873a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
874a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
875a556c76aSAlexandre Belloni 
876a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
877a556c76aSAlexandre Belloni 		goto skip;
878a556c76aSAlexandre Belloni 
879a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
880a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
881a556c76aSAlexandre Belloni 	if (!nlh)
882a556c76aSAlexandre Belloni 		return -EMSGSIZE;
883a556c76aSAlexandre Belloni 
884a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
885a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
886a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
887a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
888a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
889a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
890a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
891531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
892a556c76aSAlexandre Belloni 
893531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
894a556c76aSAlexandre Belloni 		goto nla_put_failure;
895a556c76aSAlexandre Belloni 
896531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
897a556c76aSAlexandre Belloni 		goto nla_put_failure;
898a556c76aSAlexandre Belloni 
899a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
900a556c76aSAlexandre Belloni 
901a556c76aSAlexandre Belloni skip:
902a556c76aSAlexandre Belloni 	dump->idx++;
903a556c76aSAlexandre Belloni 	return 0;
904a556c76aSAlexandre Belloni 
905a556c76aSAlexandre Belloni nla_put_failure:
906a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
907a556c76aSAlexandre Belloni 	return -EMSGSIZE;
908a556c76aSAlexandre Belloni }
9099c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
910a556c76aSAlexandre Belloni 
911531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
912a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
913a556c76aSAlexandre Belloni {
914a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
915531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
916a556c76aSAlexandre Belloni 
917a556c76aSAlexandre Belloni 	/* Set row and column to read from */
918a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
919a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
920a556c76aSAlexandre Belloni 
921a556c76aSAlexandre Belloni 	/* Issue a read command */
922a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
923a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
924a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
925a556c76aSAlexandre Belloni 
926a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
927a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
928a556c76aSAlexandre Belloni 
929a556c76aSAlexandre Belloni 	/* Read the entry flags */
930a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
931a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
932a556c76aSAlexandre Belloni 		return -EINVAL;
933a556c76aSAlexandre Belloni 
934a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
935a556c76aSAlexandre Belloni 	 * do not report it.
936a556c76aSAlexandre Belloni 	 */
937a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
938531ee1a6SVladimir Oltean 	if (dst != port)
939a556c76aSAlexandre Belloni 		return -EINVAL;
940a556c76aSAlexandre Belloni 
941a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
942a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
943a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
944a556c76aSAlexandre Belloni 
945a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
946a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
947a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
948a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
949a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
950a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
951a556c76aSAlexandre Belloni 
952a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
953a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
954a556c76aSAlexandre Belloni 
955a556c76aSAlexandre Belloni 	return 0;
956a556c76aSAlexandre Belloni }
957a556c76aSAlexandre Belloni 
9585e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
959531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
960a556c76aSAlexandre Belloni {
961531ee1a6SVladimir Oltean 	int i, j;
962a556c76aSAlexandre Belloni 
96321ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
96421ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
965a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
966531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
967531ee1a6SVladimir Oltean 			bool is_static;
968531ee1a6SVladimir Oltean 			int ret;
969531ee1a6SVladimir Oltean 
970531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
971a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
972a556c76aSAlexandre Belloni 			 * skip it.
973a556c76aSAlexandre Belloni 			 */
974a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
975a556c76aSAlexandre Belloni 				continue;
976a556c76aSAlexandre Belloni 			else if (ret)
977531ee1a6SVladimir Oltean 				return ret;
978a556c76aSAlexandre Belloni 
979531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
980531ee1a6SVladimir Oltean 
981531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
982a556c76aSAlexandre Belloni 			if (ret)
983531ee1a6SVladimir Oltean 				return ret;
984a556c76aSAlexandre Belloni 		}
985a556c76aSAlexandre Belloni 	}
986a556c76aSAlexandre Belloni 
987531ee1a6SVladimir Oltean 	return 0;
988531ee1a6SVladimir Oltean }
9895e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
990531ee1a6SVladimir Oltean 
991f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
9924e3b0468SAntoine Tenart {
9934e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
9944e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
9954e3b0468SAntoine Tenart }
996f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
9974e3b0468SAntoine Tenart 
998f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
9994e3b0468SAntoine Tenart {
1000306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10014e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
10024e3b0468SAntoine Tenart 
10034e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
10044e3b0468SAntoine Tenart 		return -EFAULT;
10054e3b0468SAntoine Tenart 
10064e3b0468SAntoine Tenart 	/* reserved for future extensions */
10074e3b0468SAntoine Tenart 	if (cfg.flags)
10084e3b0468SAntoine Tenart 		return -EINVAL;
10094e3b0468SAntoine Tenart 
10104e3b0468SAntoine Tenart 	/* Tx type sanity check */
10114e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
10124e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1013306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
10144e3b0468SAntoine Tenart 		break;
10154e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
10164e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
10174e3b0468SAntoine Tenart 		 * need to update the origin time.
10184e3b0468SAntoine Tenart 		 */
1019306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
10204e3b0468SAntoine Tenart 		break;
10214e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1022306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
10234e3b0468SAntoine Tenart 		break;
10244e3b0468SAntoine Tenart 	default:
10254e3b0468SAntoine Tenart 		return -ERANGE;
10264e3b0468SAntoine Tenart 	}
10274e3b0468SAntoine Tenart 
10284e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
10294e3b0468SAntoine Tenart 
10304e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
10314e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
10324e3b0468SAntoine Tenart 		break;
10334e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
10344e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
10354e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
10364e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
10374e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
10384e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
10394e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
10404e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
10414e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
10424e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
10434e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
10444e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
10454e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
10464e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
10474e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
10484e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
10494e3b0468SAntoine Tenart 		break;
10504e3b0468SAntoine Tenart 	default:
10514e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
10524e3b0468SAntoine Tenart 		return -ERANGE;
10534e3b0468SAntoine Tenart 	}
10544e3b0468SAntoine Tenart 
10554e3b0468SAntoine Tenart 	/* Commit back the result & save it */
10564e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
10574e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
10584e3b0468SAntoine Tenart 
10594e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
10604e3b0468SAntoine Tenart }
1061f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
10624e3b0468SAntoine Tenart 
10635e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1064a556c76aSAlexandre Belloni {
1065a556c76aSAlexandre Belloni 	int i;
1066a556c76aSAlexandre Belloni 
1067a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1068a556c76aSAlexandre Belloni 		return;
1069a556c76aSAlexandre Belloni 
1070a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1071a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1072a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1073a556c76aSAlexandre Belloni }
10745e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1075a556c76aSAlexandre Belloni 
10761e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1077a556c76aSAlexandre Belloni {
1078a556c76aSAlexandre Belloni 	int i, j;
1079a556c76aSAlexandre Belloni 
1080a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1081a556c76aSAlexandre Belloni 
1082a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1083a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1084a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1085a556c76aSAlexandre Belloni 
1086a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1087a556c76aSAlexandre Belloni 			u32 val;
1088a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1089a556c76aSAlexandre Belloni 
1090a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1091a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1092a556c76aSAlexandre Belloni 
1093a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1094a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1095a556c76aSAlexandre Belloni 
1096a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1097a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1098a556c76aSAlexandre Belloni 		}
1099a556c76aSAlexandre Belloni 	}
1100a556c76aSAlexandre Belloni 
11011e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
11021e1caa97SClaudiu Manoil }
11031e1caa97SClaudiu Manoil 
11041e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
11051e1caa97SClaudiu Manoil {
11061e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
11071e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
11081e1caa97SClaudiu Manoil 					     stats_work);
11091e1caa97SClaudiu Manoil 
11101e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
11111e1caa97SClaudiu Manoil 
1112a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1113a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1114a556c76aSAlexandre Belloni }
1115a556c76aSAlexandre Belloni 
11165e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1117a556c76aSAlexandre Belloni {
1118a556c76aSAlexandre Belloni 	int i;
1119a556c76aSAlexandre Belloni 
1120a556c76aSAlexandre Belloni 	/* check and update now */
11211e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1122a556c76aSAlexandre Belloni 
1123a556c76aSAlexandre Belloni 	/* Copy all counters */
1124a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1125004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1126a556c76aSAlexandre Belloni }
11275e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1128a556c76aSAlexandre Belloni 
11295e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1130c7282d38SVladimir Oltean {
1131a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1132a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1133c7282d38SVladimir Oltean 
1134a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1135a556c76aSAlexandre Belloni }
11365e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1137a556c76aSAlexandre Belloni 
11385e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1139c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1140c7282d38SVladimir Oltean {
11414e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
11424e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1143d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1144d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1145d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1146d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1147d2b09a8eSYangbo Lu 		return 0;
1148d2b09a8eSYangbo Lu 	}
11494e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
11504e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
11514e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
11524e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
11534e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
11544e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
11554e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
11564e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
11574e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
11584e3b0468SAntoine Tenart 
11594e3b0468SAntoine Tenart 	return 0;
11604e3b0468SAntoine Tenart }
11615e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
11624e3b0468SAntoine Tenart 
116323ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
116423ca3b72SVladimir Oltean 				bool only_active_ports)
1165b80af659SVladimir Oltean {
1166b80af659SVladimir Oltean 	u32 mask = 0;
1167b80af659SVladimir Oltean 	int port;
1168b80af659SVladimir Oltean 
1169b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1170b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1171b80af659SVladimir Oltean 
1172b80af659SVladimir Oltean 		if (!ocelot_port)
1173b80af659SVladimir Oltean 			continue;
1174b80af659SVladimir Oltean 
117523ca3b72SVladimir Oltean 		if (ocelot_port->bond == bond) {
117623ca3b72SVladimir Oltean 			if (only_active_ports && !ocelot_port->lag_tx_active)
117723ca3b72SVladimir Oltean 				continue;
117823ca3b72SVladimir Oltean 
1179b80af659SVladimir Oltean 			mask |= BIT(port);
1180b80af659SVladimir Oltean 		}
118123ca3b72SVladimir Oltean 	}
1182b80af659SVladimir Oltean 
1183b80af659SVladimir Oltean 	return mask;
1184b80af659SVladimir Oltean }
1185b80af659SVladimir Oltean 
1186e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
11879b521250SVladimir Oltean {
1188e21268efSVladimir Oltean 	u32 mask = 0;
11899b521250SVladimir Oltean 	int port;
11909b521250SVladimir Oltean 
1191e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1192e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1193e21268efSVladimir Oltean 
1194e21268efSVladimir Oltean 		if (!ocelot_port)
1195e21268efSVladimir Oltean 			continue;
1196e21268efSVladimir Oltean 
1197e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
1198e21268efSVladimir Oltean 			mask |= BIT(port);
1199e21268efSVladimir Oltean 	}
1200e21268efSVladimir Oltean 
1201e21268efSVladimir Oltean 	return mask;
1202e21268efSVladimir Oltean }
1203e21268efSVladimir Oltean 
1204e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1205e21268efSVladimir Oltean {
1206e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
1207e21268efSVladimir Oltean 	int port;
1208e21268efSVladimir Oltean 
1209e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
1210e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
1211e21268efSVladimir Oltean 	 * those are bridged or standalone.
1212e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
1213e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
1214e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
1215e21268efSVladimir Oltean 	 */
1216e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1217e21268efSVladimir Oltean 
12189b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
12199b521250SVladimir Oltean 	 * a source for the other ports.
12209b521250SVladimir Oltean 	 */
12219b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1222e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1223e21268efSVladimir Oltean 		unsigned long mask;
1224e21268efSVladimir Oltean 
1225e21268efSVladimir Oltean 		if (!ocelot_port) {
1226e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1227e21268efSVladimir Oltean 			mask = 0;
1228e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1229e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1230e21268efSVladimir Oltean 			 * forward packets to all other ports except for
1231e21268efSVladimir Oltean 			 * themselves
1232e21268efSVladimir Oltean 			 */
1233e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1234e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
1235e21268efSVladimir Oltean 		} else if (ocelot->bridge_fwd_mask & BIT(port)) {
1236528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
12379b521250SVladimir Oltean 
1238e21268efSVladimir Oltean 			mask = ocelot->bridge_fwd_mask & ~BIT(port);
123923ca3b72SVladimir Oltean 			if (bond) {
124023ca3b72SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond,
124123ca3b72SVladimir Oltean 							      false);
124223ca3b72SVladimir Oltean 			}
12439b521250SVladimir Oltean 		} else {
1244e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1245e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1246e21268efSVladimir Oltean 			 * module otherwise.
1247e21268efSVladimir Oltean 			 */
1248e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
1249e21268efSVladimir Oltean 		}
1250e21268efSVladimir Oltean 
1251e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
12529b521250SVladimir Oltean 	}
12539b521250SVladimir Oltean }
1254e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
12559b521250SVladimir Oltean 
12565e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1257a556c76aSAlexandre Belloni {
1258421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1259a556c76aSAlexandre Belloni 	u32 port_cfg;
1260a556c76aSAlexandre Belloni 
12614bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
12624bda1415SVladimir Oltean 		return;
1263a556c76aSAlexandre Belloni 
12644bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1265a556c76aSAlexandre Belloni 
1266a556c76aSAlexandre Belloni 	switch (state) {
1267a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
12684bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
1269df561f66SGustavo A. R. Silva 		fallthrough;
1270a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1271421741eaSVladimir Oltean 		if (ocelot_port->learn_ena)
1272a556c76aSAlexandre Belloni 			port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1273a556c76aSAlexandre Belloni 		break;
1274a556c76aSAlexandre Belloni 
1275a556c76aSAlexandre Belloni 	default:
1276a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
12774bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
1278a556c76aSAlexandre Belloni 		break;
1279a556c76aSAlexandre Belloni 	}
1280a556c76aSAlexandre Belloni 
12814bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
1282a556c76aSAlexandre Belloni 
12839b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1284a556c76aSAlexandre Belloni }
12855e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1286a556c76aSAlexandre Belloni 
12875e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
12884bda1415SVladimir Oltean {
1289c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1290c0d7eccbSVladimir Oltean 
1291c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1292c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1293c0d7eccbSVladimir Oltean 	 */
1294c0d7eccbSVladimir Oltean 	if (!age_period)
1295c0d7eccbSVladimir Oltean 		age_period = 1;
1296c0d7eccbSVladimir Oltean 
1297c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1298a556c76aSAlexandre Belloni }
12995e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1300a556c76aSAlexandre Belloni 
1301a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1302a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1303a556c76aSAlexandre Belloni 						     u16 vid)
1304a556c76aSAlexandre Belloni {
1305a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1306a556c76aSAlexandre Belloni 
1307a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1308a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1309a556c76aSAlexandre Belloni 			return mc;
1310a556c76aSAlexandre Belloni 	}
1311a556c76aSAlexandre Belloni 
1312a556c76aSAlexandre Belloni 	return NULL;
1313a556c76aSAlexandre Belloni }
1314a556c76aSAlexandre Belloni 
13159403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
13169403c158SVladimir Oltean {
13179403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
13189403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
13199403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
13209403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
13217c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
13229403c158SVladimir Oltean }
13239403c158SVladimir Oltean 
1324e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1325e5d1f896SVladimir Oltean 					     unsigned long ports)
1326e5d1f896SVladimir Oltean {
1327e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1328e5d1f896SVladimir Oltean 
1329e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1330e5d1f896SVladimir Oltean 	if (!pgid)
1331e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1332e5d1f896SVladimir Oltean 
1333e5d1f896SVladimir Oltean 	pgid->ports = ports;
1334e5d1f896SVladimir Oltean 	pgid->index = index;
1335e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1336e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1337e5d1f896SVladimir Oltean 
1338e5d1f896SVladimir Oltean 	return pgid;
1339e5d1f896SVladimir Oltean }
1340e5d1f896SVladimir Oltean 
1341e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1342e5d1f896SVladimir Oltean {
1343e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1344e5d1f896SVladimir Oltean 		return;
1345e5d1f896SVladimir Oltean 
1346e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1347e5d1f896SVladimir Oltean 	kfree(pgid);
1348e5d1f896SVladimir Oltean }
1349e5d1f896SVladimir Oltean 
1350e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1351bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
13529403c158SVladimir Oltean {
1353e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1354e5d1f896SVladimir Oltean 	int index;
13559403c158SVladimir Oltean 
13569403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
13579403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
13589403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
13599403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
13609403c158SVladimir Oltean 	 */
1361bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1362bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1363e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
13649403c158SVladimir Oltean 
1365e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1366e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1367e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1368e5d1f896SVladimir Oltean 		 */
1369e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1370e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1371e5d1f896SVladimir Oltean 			return pgid;
1372e5d1f896SVladimir Oltean 		}
1373e5d1f896SVladimir Oltean 	}
1374e5d1f896SVladimir Oltean 
1375e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1376e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
13779403c158SVladimir Oltean 		bool used = false;
13789403c158SVladimir Oltean 
1379e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1380e5d1f896SVladimir Oltean 			if (pgid->index == index) {
13819403c158SVladimir Oltean 				used = true;
13829403c158SVladimir Oltean 				break;
13839403c158SVladimir Oltean 			}
13849403c158SVladimir Oltean 		}
13859403c158SVladimir Oltean 
13869403c158SVladimir Oltean 		if (!used)
1387e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
13889403c158SVladimir Oltean 	}
13899403c158SVladimir Oltean 
1390e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
13919403c158SVladimir Oltean }
13929403c158SVladimir Oltean 
13939403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1394bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
13959403c158SVladimir Oltean {
1396ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
13979403c158SVladimir Oltean 
1398bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
13999403c158SVladimir Oltean 		addr[0] = 0;
14009403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
14019403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1402bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
14039403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
14049403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
14059403c158SVladimir Oltean 	}
14069403c158SVladimir Oltean }
14079403c158SVladimir Oltean 
1408209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1409209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1410a556c76aSAlexandre Belloni {
1411a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1412004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1413e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1414a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1415a556c76aSAlexandre Belloni 
1416471beb11SVladimir Oltean 	if (port == ocelot->npi)
1417471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1418471beb11SVladimir Oltean 
1419a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1420a556c76aSAlexandre Belloni 	if (!mc) {
1421728e69aeSVladimir Oltean 		/* New entry */
1422bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1423bb8d53fdSVladimir Oltean 		if (!mc)
1424bb8d53fdSVladimir Oltean 			return -ENOMEM;
1425bb8d53fdSVladimir Oltean 
1426bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1427bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1428bb8d53fdSVladimir Oltean 		mc->vid = vid;
1429bb8d53fdSVladimir Oltean 
1430a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1431728e69aeSVladimir Oltean 	} else {
1432e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1433e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1434e5d1f896SVladimir Oltean 		 */
1435e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1436bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1437a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1438a556c76aSAlexandre Belloni 	}
1439a556c76aSAlexandre Belloni 
1440004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1441e5d1f896SVladimir Oltean 
1442e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1443e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1444e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1445e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1446e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1447e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1448e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1449e5d1f896SVladimir Oltean 	}
1450e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1451e5d1f896SVladimir Oltean 
1452bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1453a556c76aSAlexandre Belloni 
1454e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1455e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1456e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1457e5d1f896SVladimir Oltean 				 pgid->index);
1458e5d1f896SVladimir Oltean 
1459e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1460bb8d53fdSVladimir Oltean 				 mc->entry_type);
1461a556c76aSAlexandre Belloni }
1462209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1463a556c76aSAlexandre Belloni 
1464209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1465a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1466a556c76aSAlexandre Belloni {
1467a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1468004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1469e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1470a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1471a556c76aSAlexandre Belloni 
1472471beb11SVladimir Oltean 	if (port == ocelot->npi)
1473471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1474471beb11SVladimir Oltean 
1475a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1476a556c76aSAlexandre Belloni 	if (!mc)
1477a556c76aSAlexandre Belloni 		return -ENOENT;
1478a556c76aSAlexandre Belloni 
1479bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1480a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1481a556c76aSAlexandre Belloni 
1482e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1483004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1484a556c76aSAlexandre Belloni 	if (!mc->ports) {
1485a556c76aSAlexandre Belloni 		list_del(&mc->list);
1486a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1487a556c76aSAlexandre Belloni 		return 0;
1488a556c76aSAlexandre Belloni 	}
1489a556c76aSAlexandre Belloni 
1490e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1491e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1492e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1493e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1494e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1495e5d1f896SVladimir Oltean 
1496bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1497a556c76aSAlexandre Belloni 
1498e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1499e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1500e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1501e5d1f896SVladimir Oltean 				 pgid->index);
1502e5d1f896SVladimir Oltean 
1503e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1504bb8d53fdSVladimir Oltean 				 mc->entry_type);
1505a556c76aSAlexandre Belloni }
1506209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1507a556c76aSAlexandre Belloni 
15085e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1509a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1510a556c76aSAlexandre Belloni {
1511a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1512a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1513a556c76aSAlexandre Belloni 	} else {
1514a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1515a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1516a556c76aSAlexandre Belloni 			 * unsupported */
1517a556c76aSAlexandre Belloni 			return -ENODEV;
1518a556c76aSAlexandre Belloni 	}
1519a556c76aSAlexandre Belloni 
1520f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1521a556c76aSAlexandre Belloni 
1522a556c76aSAlexandre Belloni 	return 0;
1523a556c76aSAlexandre Belloni }
15245e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1525a556c76aSAlexandre Belloni 
15265e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1527a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1528a556c76aSAlexandre Belloni {
1529c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
15302e554a7aSVladimir Oltean 	int ret;
15312e554a7aSVladimir Oltean 
153297bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1533a556c76aSAlexandre Belloni 
1534a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1535a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
15367142529fSAntoine Tenart 
1537bae33f2bSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false);
15382e554a7aSVladimir Oltean 	if (ret)
15392e554a7aSVladimir Oltean 		return ret;
15402e554a7aSVladimir Oltean 
1541c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
15422f0402feSVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
15432f0402feSVladimir Oltean 
15442f0402feSVladimir Oltean 	return 0;
1545a556c76aSAlexandre Belloni }
15465e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1547a556c76aSAlexandre Belloni 
1548dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1549dc96ee37SAlexandre Belloni {
1550528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1551dc96ee37SAlexandre Belloni 	int i, port, lag;
1552dc96ee37SAlexandre Belloni 
1553dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
155496b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1555dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1556dc96ee37SAlexandre Belloni 
155796b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1558dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1559dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1560dc96ee37SAlexandre Belloni 
1561528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1562528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1563528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1564528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1565528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1566528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1567528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1568528d3f19SVladimir Oltean 	 */
1569528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1570528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1571528d3f19SVladimir Oltean 
1572528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1573528d3f19SVladimir Oltean 			continue;
1574528d3f19SVladimir Oltean 
1575528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1576528d3f19SVladimir Oltean 	}
1577528d3f19SVladimir Oltean 
1578528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1579dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1580528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
158123ca3b72SVladimir Oltean 		int num_active_ports = 0;
1582dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1583dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1584dc96ee37SAlexandre Belloni 
1585528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1586dc96ee37SAlexandre Belloni 			continue;
1587dc96ee37SAlexandre Belloni 
158823ca3b72SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1589528d3f19SVladimir Oltean 
1590dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1591dc96ee37SAlexandre Belloni 			// Destination mask
1592dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1593dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
159423ca3b72SVladimir Oltean 			aggr_idx[num_active_ports++] = port;
1595dc96ee37SAlexandre Belloni 		}
1596dc96ee37SAlexandre Belloni 
159796b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1598dc96ee37SAlexandre Belloni 			u32 ac;
1599dc96ee37SAlexandre Belloni 
1600dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1601dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
160223ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
160323ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
160423ca3b72SVladimir Oltean 			 */
160523ca3b72SVladimir Oltean 			if (num_active_ports)
160623ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1607dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1608dc96ee37SAlexandre Belloni 		}
1609528d3f19SVladimir Oltean 
1610528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1611528d3f19SVladimir Oltean 		 * the same config again.
1612528d3f19SVladimir Oltean 		 */
1613528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1614528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1615528d3f19SVladimir Oltean 
1616528d3f19SVladimir Oltean 			if (!ocelot_port)
1617528d3f19SVladimir Oltean 				continue;
1618528d3f19SVladimir Oltean 
1619528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1620528d3f19SVladimir Oltean 				visited |= BIT(port);
1621528d3f19SVladimir Oltean 		}
1622dc96ee37SAlexandre Belloni 	}
1623dc96ee37SAlexandre Belloni }
1624dc96ee37SAlexandre Belloni 
16252527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
16262527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
16272527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
16282527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
16292527f2e8SVladimir Oltean  */
16302527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1631dc96ee37SAlexandre Belloni {
16322527f2e8SVladimir Oltean 	int port;
1633dc96ee37SAlexandre Belloni 
16342527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
16352527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
16362527f2e8SVladimir Oltean 		struct net_device *bond;
1637dc96ee37SAlexandre Belloni 
16382527f2e8SVladimir Oltean 		if (!ocelot_port)
16392527f2e8SVladimir Oltean 			continue;
1640dc96ee37SAlexandre Belloni 
16412527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
16422527f2e8SVladimir Oltean 		if (bond) {
164323ca3b72SVladimir Oltean 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
164423ca3b72SVladimir Oltean 							     false));
16452527f2e8SVladimir Oltean 
16462527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
1647dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
16482527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
16492527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
16502527f2e8SVladimir Oltean 		} else {
16512527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
16522527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
16532527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
16542527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
16552527f2e8SVladimir Oltean 		}
1656dc96ee37SAlexandre Belloni 	}
1657dc96ee37SAlexandre Belloni }
1658dc96ee37SAlexandre Belloni 
16599c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1660583cbbe3SVladimir Oltean 			 struct net_device *bond,
1661583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
1662dc96ee37SAlexandre Belloni {
1663583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1664583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
1665583cbbe3SVladimir Oltean 
1666b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
1667dc96ee37SAlexandre Belloni 
16682527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
16699b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1670dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1671dc96ee37SAlexandre Belloni 
1672dc96ee37SAlexandre Belloni 	return 0;
1673dc96ee37SAlexandre Belloni }
16749c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1675dc96ee37SAlexandre Belloni 
16769c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1677dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1678dc96ee37SAlexandre Belloni {
1679b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
1680b80af659SVladimir Oltean 
16812527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
16829b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1683dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1684dc96ee37SAlexandre Belloni }
16859c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
16860e332c85SPetr Machata 
168723ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
168823ca3b72SVladimir Oltean {
168923ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
169023ca3b72SVladimir Oltean 
169123ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
169223ca3b72SVladimir Oltean 
169323ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
169423ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
169523ca3b72SVladimir Oltean }
169623ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
169723ca3b72SVladimir Oltean 
1698a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1699a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
17000b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
17010b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
17020b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1703a8015dedSVladimir Oltean  */
17040b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
170531350d7fSVladimir Oltean {
170631350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1707a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1708e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1709601e984fSVladimir Oltean 	int atop, atop_tot;
171031350d7fSVladimir Oltean 
17110b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
17120b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
17130b912fc9SVladimir Oltean 
1714cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
17150b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1716cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
17170b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
17180b912fc9SVladimir Oltean 	}
17190b912fc9SVladimir Oltean 
1720a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1721fa914e9cSVladimir Oltean 
1722e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1723e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1724e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1725541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1726541132f0SMaxim Kochetkov 			    pause_start);
1727541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1728541132f0SMaxim Kochetkov 			    pause_stop);
1729fa914e9cSVladimir Oltean 
1730601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1731f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1732a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1733601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1734601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1735601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1736fa914e9cSVladimir Oltean }
17370b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
17380b912fc9SVladimir Oltean 
17390b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
17400b912fc9SVladimir Oltean {
17410b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
17420b912fc9SVladimir Oltean 
17430b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
17440b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
17450b912fc9SVladimir Oltean 
1746cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
17470b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1748cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
17490b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
17500b912fc9SVladimir Oltean 	}
17510b912fc9SVladimir Oltean 
17520b912fc9SVladimir Oltean 	return max_mtu;
17530b912fc9SVladimir Oltean }
17540b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1755fa914e9cSVladimir Oltean 
1756421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
1757421741eaSVladimir Oltean 				     bool enabled)
1758421741eaSVladimir Oltean {
1759421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1760421741eaSVladimir Oltean 	u32 val = 0;
1761421741eaSVladimir Oltean 
1762421741eaSVladimir Oltean 	if (enabled)
1763421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
1764421741eaSVladimir Oltean 
1765421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
1766421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1767421741eaSVladimir Oltean 
1768421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
1769421741eaSVladimir Oltean }
1770421741eaSVladimir Oltean 
1771421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
1772421741eaSVladimir Oltean 					bool enabled)
1773421741eaSVladimir Oltean {
1774421741eaSVladimir Oltean 	u32 val = 0;
1775421741eaSVladimir Oltean 
1776421741eaSVladimir Oltean 	if (enabled)
1777421741eaSVladimir Oltean 		val = BIT(port);
1778421741eaSVladimir Oltean 
1779421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
1780421741eaSVladimir Oltean }
1781421741eaSVladimir Oltean 
1782421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
1783421741eaSVladimir Oltean 					bool enabled)
1784421741eaSVladimir Oltean {
1785421741eaSVladimir Oltean 	u32 val = 0;
1786421741eaSVladimir Oltean 
1787421741eaSVladimir Oltean 	if (enabled)
1788421741eaSVladimir Oltean 		val = BIT(port);
1789421741eaSVladimir Oltean 
1790421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
1791421741eaSVladimir Oltean }
1792421741eaSVladimir Oltean 
1793421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
1794421741eaSVladimir Oltean 					bool enabled)
1795421741eaSVladimir Oltean {
1796421741eaSVladimir Oltean 	u32 val = 0;
1797421741eaSVladimir Oltean 
1798421741eaSVladimir Oltean 	if (enabled)
1799421741eaSVladimir Oltean 		val = BIT(port);
1800421741eaSVladimir Oltean 
1801421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
1802421741eaSVladimir Oltean }
1803421741eaSVladimir Oltean 
1804421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
1805421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
1806421741eaSVladimir Oltean {
1807421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1808421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
1809421741eaSVladimir Oltean 		return -EINVAL;
1810421741eaSVladimir Oltean 
1811421741eaSVladimir Oltean 	return 0;
1812421741eaSVladimir Oltean }
1813421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
1814421741eaSVladimir Oltean 
1815421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
1816421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
1817421741eaSVladimir Oltean {
1818421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
1819421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
1820421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
1821421741eaSVladimir Oltean 
1822421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
1823421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
1824421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
1825421741eaSVladimir Oltean 
1826421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
1827421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
1828421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
1829421741eaSVladimir Oltean 
1830421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
1831421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
1832421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
1833421741eaSVladimir Oltean }
1834421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
1835421741eaSVladimir Oltean 
18365e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1837fa914e9cSVladimir Oltean {
1838fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1839fa914e9cSVladimir Oltean 
1840b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
18416565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
184231350d7fSVladimir Oltean 
184331350d7fSVladimir Oltean 	/* Basic L2 initialization */
184431350d7fSVladimir Oltean 
18455bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
18465bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
18475bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
18485bc9d2e6SVladimir Oltean 	 */
18495bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
18505bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
18515bc9d2e6SVladimir Oltean 
18525bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
18535bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
18545bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
18555bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
18565bc9d2e6SVladimir Oltean 	mdelay(1);
18575bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
18585bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
18595bc9d2e6SVladimir Oltean 
18605bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1861a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
18625bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
18635bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1864a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
18655bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
18665bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
18675bc9d2e6SVladimir Oltean 
18685bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
18695bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
18705bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
18715bc9d2e6SVladimir Oltean 
1872e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1873541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1874e8e6e73dSVladimir Oltean 
187531350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
187631350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
187731350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
187831350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
187931350d7fSVladimir Oltean 
188031350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
188131350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
188231350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
188331350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
188431350d7fSVladimir Oltean 
1885421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
1886421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
1887421741eaSVladimir Oltean 
188831350d7fSVladimir Oltean 	/* Enable vcap lookups */
188931350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
189031350d7fSVladimir Oltean }
18915e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
189231350d7fSVladimir Oltean 
18932d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
18942d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
18952d44b097SVladimir Oltean  * NPI mode is used).
189669df578cSVladimir Oltean  */
18972d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
189821468199SVladimir Oltean {
189969df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
190069df578cSVladimir Oltean 
190169df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
190221468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
190369df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
190469df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
190569df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
190669df578cSVladimir Oltean 	 */
190721468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
190821468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
190921468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
191021468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
191121468199SVladimir Oltean 
191269df578cSVladimir Oltean 	/* Enable CPU port module */
1913886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
191469df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1915886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
1916cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
1917886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
1918cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
191921468199SVladimir Oltean 
192021468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
192121468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
192221468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
192321468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
192421468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
192521468199SVladimir Oltean }
192621468199SVladimir Oltean 
1927f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
1928f6fe01d6SVladimir Oltean {
1929f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
1930f6fe01d6SVladimir Oltean 
1931f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
1932f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
1933f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
1934f6fe01d6SVladimir Oltean 	 */
1935f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
1936f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
1937f6fe01d6SVladimir Oltean 
1938f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
1939f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
1940f6fe01d6SVladimir Oltean }
1941f6fe01d6SVladimir Oltean 
1942a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1943a556c76aSAlexandre Belloni {
1944a556c76aSAlexandre Belloni 	char queue_name[32];
194521468199SVladimir Oltean 	int i, ret;
194621468199SVladimir Oltean 	u32 port;
1947a556c76aSAlexandre Belloni 
19483a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
19493a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
19503a77b593SVladimir Oltean 		if (ret) {
19513a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
19523a77b593SVladimir Oltean 			return ret;
19533a77b593SVladimir Oltean 		}
19543a77b593SVladimir Oltean 	}
19553a77b593SVladimir Oltean 
1956a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1957a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1958a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1959a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1960a556c76aSAlexandre Belloni 		return -ENOMEM;
1961a556c76aSAlexandre Belloni 
1962a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
19634e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
19644e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1965a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1966a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1967a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1968a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1969a556c76aSAlexandre Belloni 		return -ENOMEM;
1970a556c76aSAlexandre Belloni 
1971ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1972ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
1973ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
1974ca0b272bSVladimir Oltean 		return -ENOMEM;
1975ca0b272bSVladimir Oltean 	}
1976ca0b272bSVladimir Oltean 
19772b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1978e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
1979f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
1980a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1981a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1982aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
19832d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
1984a556c76aSAlexandre Belloni 
1985a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1986a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1987a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1988a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1989a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1990a556c76aSAlexandre Belloni 	}
1991a556c76aSAlexandre Belloni 
1992a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1993a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1994a556c76aSAlexandre Belloni 
1995a556c76aSAlexandre Belloni 	/* Aggregation mode */
1996a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1997a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1998a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1999f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2000f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2001f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2002f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2003a556c76aSAlexandre Belloni 
2004a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2005a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2006a556c76aSAlexandre Belloni 	 */
2007a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2008a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2009a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2010a556c76aSAlexandre Belloni 
2011a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2012a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2013a556c76aSAlexandre Belloni 
2014a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2015a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2016a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2017a556c76aSAlexandre Belloni 
2018a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2019edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2020a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2021b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2022a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2023edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2024a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2025a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2026a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2027a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2028a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2029a556c76aSAlexandre Belloni 
2030a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2031a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2032a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2033a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2034a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2035a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2036a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2037a556c76aSAlexandre Belloni 				 port);
2038a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2039a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2040a556c76aSAlexandre Belloni 	}
2041a556c76aSAlexandre Belloni 
204296b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2043a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2044a556c76aSAlexandre Belloni 
2045a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2046a556c76aSAlexandre Belloni 	}
2047ebb1bb40SHoratiu Vultur 
2048ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2049ebb1bb40SHoratiu Vultur 
2050b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2051b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2052b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2053a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2054b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2055b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2056b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2057a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2058a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2059a556c76aSAlexandre Belloni 
2060a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2061a556c76aSAlexandre Belloni 	 * registers endianness.
2062a556c76aSAlexandre Belloni 	 */
2063a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2064a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2065a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2066a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2067a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2068a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2069a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2070a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2071a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2072a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2073a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2074a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2075a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2076a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2077a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2078a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2079a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2080a556c76aSAlexandre Belloni 
20811e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2082a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2083a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
20844e3b0468SAntoine Tenart 
2085a556c76aSAlexandre Belloni 	return 0;
2086a556c76aSAlexandre Belloni }
2087a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2088a556c76aSAlexandre Belloni 
2089a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2090a556c76aSAlexandre Belloni {
2091c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2092a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2093ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2094a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2095a556c76aSAlexandre Belloni }
2096a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2097a556c76aSAlexandre Belloni 
2098e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2099e5fb512dSVladimir Oltean {
2100e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2101e5fb512dSVladimir Oltean 
2102e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2103e5fb512dSVladimir Oltean }
2104e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2105e5fb512dSVladimir Oltean 
2106a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2107