xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision b360d94f1b8647bc164e7519ec900471836be14a)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
7a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
820968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
9a556c76aSAlexandre Belloni #include "ocelot.h"
103c83654fSVladimir Oltean #include "ocelot_vcap.h"
11a556c76aSAlexandre Belloni 
12639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
13639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
14639c1b26SSteen Hegelund 
15a556c76aSAlexandre Belloni struct ocelot_mact_entry {
16a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
17a556c76aSAlexandre Belloni 	u16 vid;
18a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
19a556c76aSAlexandre Belloni };
20a556c76aSAlexandre Belloni 
21639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
22639c1b26SSteen Hegelund {
23639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
24639c1b26SSteen Hegelund }
25639c1b26SSteen Hegelund 
26a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
27a556c76aSAlexandre Belloni {
28639c1b26SSteen Hegelund 	u32 val;
29a556c76aSAlexandre Belloni 
30639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
31639c1b26SSteen Hegelund 		ocelot, val,
32639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
33639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
34639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
35a556c76aSAlexandre Belloni }
36a556c76aSAlexandre Belloni 
37a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
38a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
39a556c76aSAlexandre Belloni 			       unsigned int vid)
40a556c76aSAlexandre Belloni {
41a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
42a556c76aSAlexandre Belloni 
43a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
44a556c76aSAlexandre Belloni 	 * understood by the hardware.
45a556c76aSAlexandre Belloni 	 */
46a556c76aSAlexandre Belloni 	mach |= vid    << 16;
47a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
48a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
49a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
50a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
51a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
52a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
53a556c76aSAlexandre Belloni 
54a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
55a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
56a556c76aSAlexandre Belloni 
57a556c76aSAlexandre Belloni }
58a556c76aSAlexandre Belloni 
599c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
60a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
619c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
62a556c76aSAlexandre Belloni {
63584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
64584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
65584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
66584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
67584b7cfcSAlban Bedel 	unsigned int mc_ports;
68584b7cfcSAlban Bedel 
69584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
70584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
71584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
72584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
73584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
74584b7cfcSAlban Bedel 	else
75584b7cfcSAlban Bedel 		mc_ports = 0;
76584b7cfcSAlban Bedel 
77584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
78584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
79584b7cfcSAlban Bedel 
80a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
81a556c76aSAlexandre Belloni 
82a556c76aSAlexandre Belloni 	/* Issue a write command */
83584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
84a556c76aSAlexandre Belloni 
85a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
86a556c76aSAlexandre Belloni }
879c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
88a556c76aSAlexandre Belloni 
899c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
909c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
91a556c76aSAlexandre Belloni {
92a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
93a556c76aSAlexandre Belloni 
94a556c76aSAlexandre Belloni 	/* Issue a forget command */
95a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
96a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
97a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
98a556c76aSAlexandre Belloni 
99a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
100a556c76aSAlexandre Belloni }
1019c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
102a556c76aSAlexandre Belloni 
103a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
104a556c76aSAlexandre Belloni {
105a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
106a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
107a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
108a556c76aSAlexandre Belloni 	 */
109a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
110a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
111a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
112a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
113a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
114a556c76aSAlexandre Belloni 
115a556c76aSAlexandre Belloni 	/* Clear the MAC table */
116a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
117a556c76aSAlexandre Belloni }
118a556c76aSAlexandre Belloni 
119f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
120b5962294SHoratiu Vultur {
121b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
122b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
123f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
12475944fdaSXiaoliang Yang 
12575944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
12675944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1272f17c050SXiaoliang Yang 
1282f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1292f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1302f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
131b5962294SHoratiu Vultur }
132b5962294SHoratiu Vultur 
133639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
134639c1b26SSteen Hegelund {
135639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
136639c1b26SSteen Hegelund }
137639c1b26SSteen Hegelund 
138a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
139a556c76aSAlexandre Belloni {
140639c1b26SSteen Hegelund 	u32 val;
141a556c76aSAlexandre Belloni 
142639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
143639c1b26SSteen Hegelund 		ocelot,
144639c1b26SSteen Hegelund 		val,
145639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
146639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
147639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
148a556c76aSAlexandre Belloni }
149a556c76aSAlexandre Belloni 
1507142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1517142529fSAntoine Tenart {
1527142529fSAntoine Tenart 	/* Select the VID to configure */
1537142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1547142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1557142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1567142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1577142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1587142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1597142529fSAntoine Tenart 
1607142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1617142529fSAntoine Tenart }
1627142529fSAntoine Tenart 
1632f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
164c3e58a75SVladimir Oltean 					struct ocelot_vlan native_vlan)
16597bb69e1SVladimir Oltean {
16697bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
16787b0f983SVladimir Oltean 	u32 val = 0;
16897bb69e1SVladimir Oltean 
169c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
17097bb69e1SVladimir Oltean 
171c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1727142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
17397bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17497bb69e1SVladimir Oltean 
17587b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
176e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
17787b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
17887b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
17987b0f983SVladimir Oltean 		else
18087b0f983SVladimir Oltean 			/* Tag all frames */
18187b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
18287b0f983SVladimir Oltean 	} else {
18387b0f983SVladimir Oltean 		/* Port tagging disabled. */
18487b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
18587b0f983SVladimir Oltean 	}
18687b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
18787b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
18887b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
18997bb69e1SVladimir Oltean }
19097bb69e1SVladimir Oltean 
19175e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
192c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
193c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
19475e5a554SVladimir Oltean {
19575e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
196be0576feSVladimir Oltean 	u32 val = 0;
19775e5a554SVladimir Oltean 
198c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
19975e5a554SVladimir Oltean 
20075e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
201c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
20275e5a554SVladimir Oltean 
20375e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
204c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
20575e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
20675e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
207be0576feSVladimir Oltean 
208be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
209be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
210be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
211be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
212be0576feSVladimir Oltean 	 */
213be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
214be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
215be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
216be0576feSVladimir Oltean 
217be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
218be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
219be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
220be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
22175e5a554SVladimir Oltean }
22275e5a554SVladimir Oltean 
2232e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
224bae33f2bSVladimir Oltean 			       bool vlan_aware)
22587b0f983SVladimir Oltean {
22670edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
227bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
22870edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
229bae33f2bSVladimir Oltean 	u32 val;
23070edfae1SVladimir Oltean 
23170edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
23270edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
23370edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
23470edfae1SVladimir Oltean 			dev_err(ocelot->dev,
23570edfae1SVladimir Oltean 				"Cannot change VLAN state with vlan modify rules active\n");
23670edfae1SVladimir Oltean 			return -EBUSY;
23770edfae1SVladimir Oltean 		}
23870edfae1SVladimir Oltean 	}
23970edfae1SVladimir Oltean 
24087b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
24187b0f983SVladimir Oltean 
24287b0f983SVladimir Oltean 	if (vlan_aware)
24387b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24487b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
24587b0f983SVladimir Oltean 	else
24687b0f983SVladimir Oltean 		val = 0;
24787b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
24887b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
25087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
25187b0f983SVladimir Oltean 
252c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
253c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2542e554a7aSVladimir Oltean 
2552e554a7aSVladimir Oltean 	return 0;
25687b0f983SVladimir Oltean }
25787b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
25887b0f983SVladimir Oltean 
2592f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2602f0402feSVladimir Oltean 			bool untagged)
2612f0402feSVladimir Oltean {
2622f0402feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2632f0402feSVladimir Oltean 
2642f0402feSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
2652f0402feSVladimir Oltean 	if (untagged && ocelot_port->native_vlan.vid != vid &&
2662f0402feSVladimir Oltean 	    ocelot_port->native_vlan.valid) {
2672f0402feSVladimir Oltean 		dev_err(ocelot->dev,
2682f0402feSVladimir Oltean 			"Port already has a native VLAN: %d\n",
2692f0402feSVladimir Oltean 			ocelot_port->native_vlan.vid);
2702f0402feSVladimir Oltean 		return -EBUSY;
2712f0402feSVladimir Oltean 	}
2722f0402feSVladimir Oltean 
2732f0402feSVladimir Oltean 	return 0;
2742f0402feSVladimir Oltean }
2752f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
2762f0402feSVladimir Oltean 
2775e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2787142529fSAntoine Tenart 		    bool untagged)
2797142529fSAntoine Tenart {
2807142529fSAntoine Tenart 	int ret;
2817142529fSAntoine Tenart 
2827142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
28397bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2847142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2857142529fSAntoine Tenart 	if (ret)
2867142529fSAntoine Tenart 		return ret;
2877142529fSAntoine Tenart 
2887142529fSAntoine Tenart 	/* Default ingress vlan classification */
289c3e58a75SVladimir Oltean 	if (pvid) {
290c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
291c3e58a75SVladimir Oltean 
292c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
293e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
294c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
295c3e58a75SVladimir Oltean 	}
2967142529fSAntoine Tenart 
2977142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
29897bb69e1SVladimir Oltean 	if (untagged) {
299c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
300c3e58a75SVladimir Oltean 
301c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
302e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
3032f0402feSVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
304b9cd75e6SVladimir Oltean 	}
3057142529fSAntoine Tenart 
3067142529fSAntoine Tenart 	return 0;
3077142529fSAntoine Tenart }
3085e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
3097142529fSAntoine Tenart 
3105e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3119855934cSVladimir Oltean {
3129855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3139855934cSVladimir Oltean 	int ret;
3147142529fSAntoine Tenart 
3157142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
31697bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3177142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3187142529fSAntoine Tenart 	if (ret)
3197142529fSAntoine Tenart 		return ret;
3207142529fSAntoine Tenart 
321be0576feSVladimir Oltean 	/* Ingress */
322be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
323be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
324be0576feSVladimir Oltean 
325be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
326be0576feSVladimir Oltean 	}
327be0576feSVladimir Oltean 
3287142529fSAntoine Tenart 	/* Egress */
329c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
330e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
331c3e58a75SVladimir Oltean 
332c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
333c3e58a75SVladimir Oltean 	}
3347142529fSAntoine Tenart 
3357142529fSAntoine Tenart 	return 0;
3367142529fSAntoine Tenart }
3375e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3387142529fSAntoine Tenart 
339a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
340a556c76aSAlexandre Belloni {
3417142529fSAntoine Tenart 	u16 port, vid;
3427142529fSAntoine Tenart 
343a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
344a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
345a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
346a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3477142529fSAntoine Tenart 
3487142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3497142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3507142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3517142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3527142529fSAntoine Tenart 	}
3537142529fSAntoine Tenart 
3547142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3557142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3567142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3577142529fSAntoine Tenart 	 */
3587142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3597142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3607142529fSAntoine Tenart 
3617142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3627142529fSAntoine Tenart 	 * default.
3637142529fSAntoine Tenart 	 */
364714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
365714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3667142529fSAntoine Tenart 
3677142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3687142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3697142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3707142529fSAntoine Tenart 	}
371a556c76aSAlexandre Belloni }
372a556c76aSAlexandre Belloni 
373eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
374eb4733d7SVladimir Oltean {
375eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
376eb4733d7SVladimir Oltean }
377eb4733d7SVladimir Oltean 
378eb4733d7SVladimir Oltean int ocelot_port_flush(struct ocelot *ocelot, int port)
379eb4733d7SVladimir Oltean {
380eb4733d7SVladimir Oltean 	int err, val;
381eb4733d7SVladimir Oltean 
382eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
383eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
384eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
385eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
386eb4733d7SVladimir Oltean 
387eb4733d7SVladimir Oltean 	/* Disable flow control */
388eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
389eb4733d7SVladimir Oltean 
390eb4733d7SVladimir Oltean 	/* Disable priority flow control */
391eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
392eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
393eb4733d7SVladimir Oltean 
394eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
395eb4733d7SVladimir Oltean 	 * at the port.
396eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
397eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
398eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
399eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
400eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
401eb4733d7SVladimir Oltean 	 */
402eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
403eb4733d7SVladimir Oltean 
404eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
405eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
406eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
407eb4733d7SVladimir Oltean 
408eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
409eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
410eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
411eb4733d7SVladimir Oltean 
412eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
413eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
414eb4733d7SVladimir Oltean 		       port);
415eb4733d7SVladimir Oltean 
416eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
417eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
418eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
419eb4733d7SVladimir Oltean 
420eb4733d7SVladimir Oltean 	/* Clear flushing again. */
421eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
422eb4733d7SVladimir Oltean 
423eb4733d7SVladimir Oltean 	return err;
424eb4733d7SVladimir Oltean }
425eb4733d7SVladimir Oltean EXPORT_SYMBOL(ocelot_port_flush);
426eb4733d7SVladimir Oltean 
4275e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
42826f4dbabSVladimir Oltean 			struct phy_device *phydev)
429a556c76aSAlexandre Belloni {
43026f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
4315bc9d2e6SVladimir Oltean 	int speed, mode = 0;
432a556c76aSAlexandre Belloni 
43326f4dbabSVladimir Oltean 	switch (phydev->speed) {
434a556c76aSAlexandre Belloni 	case SPEED_10:
435a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
436a556c76aSAlexandre Belloni 		break;
437a556c76aSAlexandre Belloni 	case SPEED_100:
438a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
439a556c76aSAlexandre Belloni 		break;
440a556c76aSAlexandre Belloni 	case SPEED_1000:
441a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
442a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
443a556c76aSAlexandre Belloni 		break;
444a556c76aSAlexandre Belloni 	case SPEED_2500:
445a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
446a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
447a556c76aSAlexandre Belloni 		break;
448a556c76aSAlexandre Belloni 	default:
44926f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
45026f4dbabSVladimir Oltean 			port, phydev->speed);
451a556c76aSAlexandre Belloni 		return;
452a556c76aSAlexandre Belloni 	}
453a556c76aSAlexandre Belloni 
45426f4dbabSVladimir Oltean 	phy_print_status(phydev);
455a556c76aSAlexandre Belloni 
45626f4dbabSVladimir Oltean 	if (!phydev->link)
457a556c76aSAlexandre Belloni 		return;
458a556c76aSAlexandre Belloni 
459a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
460004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
461a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
462a556c76aSAlexandre Belloni 
4631ba8f656SVladimir Oltean 	/* Disable HDX fast control */
4641ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
4651ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
4661ba8f656SVladimir Oltean 
4671ba8f656SVladimir Oltean 	/* SGMII only for now */
4681ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4691ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
4701ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4711ba8f656SVladimir Oltean 
4721ba8f656SVladimir Oltean 	/* Enable PCS */
4731ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4741ba8f656SVladimir Oltean 
4751ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4761ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4771ba8f656SVladimir Oltean 
4781ba8f656SVladimir Oltean 	/* No loopback */
4791ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
480a556c76aSAlexandre Belloni 
481a556c76aSAlexandre Belloni 	/* Enable MAC module */
482004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
483a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
484a556c76aSAlexandre Belloni 
485a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
486a556c76aSAlexandre Belloni 	 * reset */
487004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
488a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
489a556c76aSAlexandre Belloni 
490a556c76aSAlexandre Belloni 	/* No PFC */
491a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
492004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
493a556c76aSAlexandre Belloni 
494a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
495886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
496886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
497a556c76aSAlexandre Belloni 
498a556c76aSAlexandre Belloni 	/* Flow control */
499a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
500a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
501a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
502a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
503a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
504004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
505004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
506a556c76aSAlexandre Belloni }
5075e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
508a556c76aSAlexandre Belloni 
5095e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
510889b8950SVladimir Oltean 			struct phy_device *phy)
511a556c76aSAlexandre Belloni {
512a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
513a556c76aSAlexandre Belloni 	 * MAC addresses.
514a556c76aSAlexandre Belloni 	 */
515a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
516a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
517004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
518004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
519889b8950SVladimir Oltean }
5205e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
521889b8950SVladimir Oltean 
5225e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
523889b8950SVladimir Oltean {
524889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
525889b8950SVladimir Oltean 
526889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
527886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
528889b8950SVladimir Oltean }
5295e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
530889b8950SVladimir Oltean 
531e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
532e2f9a8feSVladimir Oltean 				  struct sk_buff *clone)
533400928bfSYangbo Lu {
534e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
535400928bfSYangbo Lu 
5366565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
5376565243cSVladimir Oltean 
538e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
539b049da13SYangbo Lu 	/* Store timestamp ID in cb[0] of sk_buff */
540e2f9a8feSVladimir Oltean 	clone->cb[0] = ocelot_port->ts_id;
5416565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
542e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
5436565243cSVladimir Oltean 
5446565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
545400928bfSYangbo Lu }
546400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
547400928bfSYangbo Lu 
548e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
549e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
5504e3b0468SAntoine Tenart {
5514e3b0468SAntoine Tenart 	unsigned long flags;
5524e3b0468SAntoine Tenart 	u32 val;
5534e3b0468SAntoine Tenart 
5544e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
5554e3b0468SAntoine Tenart 
5564e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
5574e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
5584e3b0468SAntoine Tenart 
5594e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
5604e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
5614e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
5624e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
5634e3b0468SAntoine Tenart 
5644e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
5654e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
5664e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
5674e3b0468SAntoine Tenart 
5684e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
5694e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
5704e3b0468SAntoine Tenart 		ts->tv_sec--;
5714e3b0468SAntoine Tenart 
5724e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
5734e3b0468SAntoine Tenart }
574e23a7b3eSYangbo Lu 
575e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
576e23a7b3eSYangbo Lu {
577e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
578e23a7b3eSYangbo Lu 
579e23a7b3eSYangbo Lu 	while (budget--) {
580b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
581e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
582e23a7b3eSYangbo Lu 		struct ocelot_port *port;
583e23a7b3eSYangbo Lu 		struct timespec64 ts;
584b049da13SYangbo Lu 		unsigned long flags;
585e23a7b3eSYangbo Lu 		u32 val, id, txport;
586e23a7b3eSYangbo Lu 
587e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
588e23a7b3eSYangbo Lu 
589e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
590e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
591e23a7b3eSYangbo Lu 			break;
592e23a7b3eSYangbo Lu 
593e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
594e23a7b3eSYangbo Lu 
595e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
596e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
597e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
598e23a7b3eSYangbo Lu 
599e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
600e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
601e23a7b3eSYangbo Lu 
602b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
603b049da13SYangbo Lu 
604b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
605b049da13SYangbo Lu 			if (skb->cb[0] != id)
606e23a7b3eSYangbo Lu 				continue;
607b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
608b049da13SYangbo Lu 			skb_match = skb;
609fc62c094SYangbo Lu 			break;
610e23a7b3eSYangbo Lu 		}
611e23a7b3eSYangbo Lu 
612b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
613b049da13SYangbo Lu 
6145fd82200Slaurent brando 		/* Get the h/w timestamp */
6155fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
616e23a7b3eSYangbo Lu 
617b049da13SYangbo Lu 		if (unlikely(!skb_match))
618e23a7b3eSYangbo Lu 			continue;
619e23a7b3eSYangbo Lu 
620e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
621e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
622e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
623e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
6245fd82200Slaurent brando 
6255fd82200Slaurent brando 		/* Next ts */
6265fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
627e23a7b3eSYangbo Lu 	}
628e23a7b3eSYangbo Lu }
629e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
6304e3b0468SAntoine Tenart 
6315e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
63287b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
633a556c76aSAlexandre Belloni {
634471beb11SVladimir Oltean 	int pgid = port;
635471beb11SVladimir Oltean 
636471beb11SVladimir Oltean 	if (port == ocelot->npi)
637471beb11SVladimir Oltean 		pgid = PGID_CPU;
638a556c76aSAlexandre Belloni 
639471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
640a556c76aSAlexandre Belloni }
6415e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
642a556c76aSAlexandre Belloni 
6435e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
644531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
645531ee1a6SVladimir Oltean {
646531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
647531ee1a6SVladimir Oltean }
6485e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
649531ee1a6SVladimir Oltean 
6509c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
651531ee1a6SVladimir Oltean 			    bool is_static, void *data)
652a556c76aSAlexandre Belloni {
653531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
654a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
655a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
656a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
657a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
658a556c76aSAlexandre Belloni 
659a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
660a556c76aSAlexandre Belloni 		goto skip;
661a556c76aSAlexandre Belloni 
662a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
663a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
664a556c76aSAlexandre Belloni 	if (!nlh)
665a556c76aSAlexandre Belloni 		return -EMSGSIZE;
666a556c76aSAlexandre Belloni 
667a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
668a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
669a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
670a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
671a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
672a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
673a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
674531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
675a556c76aSAlexandre Belloni 
676531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
677a556c76aSAlexandre Belloni 		goto nla_put_failure;
678a556c76aSAlexandre Belloni 
679531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
680a556c76aSAlexandre Belloni 		goto nla_put_failure;
681a556c76aSAlexandre Belloni 
682a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
683a556c76aSAlexandre Belloni 
684a556c76aSAlexandre Belloni skip:
685a556c76aSAlexandre Belloni 	dump->idx++;
686a556c76aSAlexandre Belloni 	return 0;
687a556c76aSAlexandre Belloni 
688a556c76aSAlexandre Belloni nla_put_failure:
689a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
690a556c76aSAlexandre Belloni 	return -EMSGSIZE;
691a556c76aSAlexandre Belloni }
6929c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
693a556c76aSAlexandre Belloni 
694531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
695a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
696a556c76aSAlexandre Belloni {
697a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
698531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
699a556c76aSAlexandre Belloni 
700a556c76aSAlexandre Belloni 	/* Set row and column to read from */
701a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
702a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
703a556c76aSAlexandre Belloni 
704a556c76aSAlexandre Belloni 	/* Issue a read command */
705a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
706a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
707a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
708a556c76aSAlexandre Belloni 
709a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
710a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
711a556c76aSAlexandre Belloni 
712a556c76aSAlexandre Belloni 	/* Read the entry flags */
713a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
714a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
715a556c76aSAlexandre Belloni 		return -EINVAL;
716a556c76aSAlexandre Belloni 
717a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
718a556c76aSAlexandre Belloni 	 * do not report it.
719a556c76aSAlexandre Belloni 	 */
720a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
721531ee1a6SVladimir Oltean 	if (dst != port)
722a556c76aSAlexandre Belloni 		return -EINVAL;
723a556c76aSAlexandre Belloni 
724a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
725a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
726a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
727a556c76aSAlexandre Belloni 
728a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
729a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
730a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
731a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
732a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
733a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
734a556c76aSAlexandre Belloni 
735a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
736a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
737a556c76aSAlexandre Belloni 
738a556c76aSAlexandre Belloni 	return 0;
739a556c76aSAlexandre Belloni }
740a556c76aSAlexandre Belloni 
7415e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
742531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
743a556c76aSAlexandre Belloni {
744531ee1a6SVladimir Oltean 	int i, j;
745a556c76aSAlexandre Belloni 
74621ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
74721ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
748a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
749531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
750531ee1a6SVladimir Oltean 			bool is_static;
751531ee1a6SVladimir Oltean 			int ret;
752531ee1a6SVladimir Oltean 
753531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
754a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
755a556c76aSAlexandre Belloni 			 * skip it.
756a556c76aSAlexandre Belloni 			 */
757a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
758a556c76aSAlexandre Belloni 				continue;
759a556c76aSAlexandre Belloni 			else if (ret)
760531ee1a6SVladimir Oltean 				return ret;
761a556c76aSAlexandre Belloni 
762531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
763531ee1a6SVladimir Oltean 
764531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
765a556c76aSAlexandre Belloni 			if (ret)
766531ee1a6SVladimir Oltean 				return ret;
767a556c76aSAlexandre Belloni 		}
768a556c76aSAlexandre Belloni 	}
769a556c76aSAlexandre Belloni 
770531ee1a6SVladimir Oltean 	return 0;
771531ee1a6SVladimir Oltean }
7725e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
773531ee1a6SVladimir Oltean 
774f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
7754e3b0468SAntoine Tenart {
7764e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
7774e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
7784e3b0468SAntoine Tenart }
779f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
7804e3b0468SAntoine Tenart 
781f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
7824e3b0468SAntoine Tenart {
783306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
7844e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
7854e3b0468SAntoine Tenart 
7864e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
7874e3b0468SAntoine Tenart 		return -EFAULT;
7884e3b0468SAntoine Tenart 
7894e3b0468SAntoine Tenart 	/* reserved for future extensions */
7904e3b0468SAntoine Tenart 	if (cfg.flags)
7914e3b0468SAntoine Tenart 		return -EINVAL;
7924e3b0468SAntoine Tenart 
7934e3b0468SAntoine Tenart 	/* Tx type sanity check */
7944e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
7954e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
796306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
7974e3b0468SAntoine Tenart 		break;
7984e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
7994e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
8004e3b0468SAntoine Tenart 		 * need to update the origin time.
8014e3b0468SAntoine Tenart 		 */
802306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
8034e3b0468SAntoine Tenart 		break;
8044e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
805306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
8064e3b0468SAntoine Tenart 		break;
8074e3b0468SAntoine Tenart 	default:
8084e3b0468SAntoine Tenart 		return -ERANGE;
8094e3b0468SAntoine Tenart 	}
8104e3b0468SAntoine Tenart 
8114e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
8124e3b0468SAntoine Tenart 
8134e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
8144e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
8154e3b0468SAntoine Tenart 		break;
8164e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
8174e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
8184e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
8194e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
8204e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
8214e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
8224e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
8234e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
8244e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
8254e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
8264e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
8274e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
8284e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
8294e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
8304e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
8314e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
8324e3b0468SAntoine Tenart 		break;
8334e3b0468SAntoine Tenart 	default:
8344e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
8354e3b0468SAntoine Tenart 		return -ERANGE;
8364e3b0468SAntoine Tenart 	}
8374e3b0468SAntoine Tenart 
8384e3b0468SAntoine Tenart 	/* Commit back the result & save it */
8394e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
8404e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
8414e3b0468SAntoine Tenart 
8424e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
8434e3b0468SAntoine Tenart }
844f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
8454e3b0468SAntoine Tenart 
8465e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
847a556c76aSAlexandre Belloni {
848a556c76aSAlexandre Belloni 	int i;
849a556c76aSAlexandre Belloni 
850a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
851a556c76aSAlexandre Belloni 		return;
852a556c76aSAlexandre Belloni 
853a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
854a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
855a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
856a556c76aSAlexandre Belloni }
8575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
858a556c76aSAlexandre Belloni 
8591e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
860a556c76aSAlexandre Belloni {
861a556c76aSAlexandre Belloni 	int i, j;
862a556c76aSAlexandre Belloni 
863a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
864a556c76aSAlexandre Belloni 
865a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
866a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
867a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
868a556c76aSAlexandre Belloni 
869a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
870a556c76aSAlexandre Belloni 			u32 val;
871a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
872a556c76aSAlexandre Belloni 
873a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
874a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
875a556c76aSAlexandre Belloni 
876a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
877a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
878a556c76aSAlexandre Belloni 
879a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
880a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
881a556c76aSAlexandre Belloni 		}
882a556c76aSAlexandre Belloni 	}
883a556c76aSAlexandre Belloni 
8841e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
8851e1caa97SClaudiu Manoil }
8861e1caa97SClaudiu Manoil 
8871e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
8881e1caa97SClaudiu Manoil {
8891e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
8901e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
8911e1caa97SClaudiu Manoil 					     stats_work);
8921e1caa97SClaudiu Manoil 
8931e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
8941e1caa97SClaudiu Manoil 
895a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
896a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
897a556c76aSAlexandre Belloni }
898a556c76aSAlexandre Belloni 
8995e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
900a556c76aSAlexandre Belloni {
901a556c76aSAlexandre Belloni 	int i;
902a556c76aSAlexandre Belloni 
903a556c76aSAlexandre Belloni 	/* check and update now */
9041e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
905a556c76aSAlexandre Belloni 
906a556c76aSAlexandre Belloni 	/* Copy all counters */
907a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
908004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
909a556c76aSAlexandre Belloni }
9105e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
911a556c76aSAlexandre Belloni 
9125e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
913c7282d38SVladimir Oltean {
914a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
915a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
916c7282d38SVladimir Oltean 
917a556c76aSAlexandre Belloni 	return ocelot->num_stats;
918a556c76aSAlexandre Belloni }
9195e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
920a556c76aSAlexandre Belloni 
9215e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
922c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
923c7282d38SVladimir Oltean {
9244e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
9254e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
926d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
927d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
928d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
929d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
930d2b09a8eSYangbo Lu 		return 0;
931d2b09a8eSYangbo Lu 	}
9324e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
9334e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
9344e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
9354e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
9364e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
9374e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
9384e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
9394e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
9404e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
9414e3b0468SAntoine Tenart 
9424e3b0468SAntoine Tenart 	return 0;
9434e3b0468SAntoine Tenart }
9445e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
9454e3b0468SAntoine Tenart 
94623ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
94723ca3b72SVladimir Oltean 				bool only_active_ports)
948b80af659SVladimir Oltean {
949b80af659SVladimir Oltean 	u32 mask = 0;
950b80af659SVladimir Oltean 	int port;
951b80af659SVladimir Oltean 
952b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
953b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
954b80af659SVladimir Oltean 
955b80af659SVladimir Oltean 		if (!ocelot_port)
956b80af659SVladimir Oltean 			continue;
957b80af659SVladimir Oltean 
95823ca3b72SVladimir Oltean 		if (ocelot_port->bond == bond) {
95923ca3b72SVladimir Oltean 			if (only_active_ports && !ocelot_port->lag_tx_active)
96023ca3b72SVladimir Oltean 				continue;
96123ca3b72SVladimir Oltean 
962b80af659SVladimir Oltean 			mask |= BIT(port);
963b80af659SVladimir Oltean 		}
96423ca3b72SVladimir Oltean 	}
965b80af659SVladimir Oltean 
966b80af659SVladimir Oltean 	return mask;
967b80af659SVladimir Oltean }
968b80af659SVladimir Oltean 
969e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
9709b521250SVladimir Oltean {
971e21268efSVladimir Oltean 	u32 mask = 0;
9729b521250SVladimir Oltean 	int port;
9739b521250SVladimir Oltean 
974e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
975e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
976e21268efSVladimir Oltean 
977e21268efSVladimir Oltean 		if (!ocelot_port)
978e21268efSVladimir Oltean 			continue;
979e21268efSVladimir Oltean 
980e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
981e21268efSVladimir Oltean 			mask |= BIT(port);
982e21268efSVladimir Oltean 	}
983e21268efSVladimir Oltean 
984e21268efSVladimir Oltean 	return mask;
985e21268efSVladimir Oltean }
986e21268efSVladimir Oltean 
987e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
988e21268efSVladimir Oltean {
989e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
990e21268efSVladimir Oltean 	int port;
991e21268efSVladimir Oltean 
992e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
993e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
994e21268efSVladimir Oltean 	 * those are bridged or standalone.
995e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
996e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
997e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
998e21268efSVladimir Oltean 	 */
999e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1000e21268efSVladimir Oltean 
10019b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
10029b521250SVladimir Oltean 	 * a source for the other ports.
10039b521250SVladimir Oltean 	 */
10049b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1005e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1006e21268efSVladimir Oltean 		unsigned long mask;
1007e21268efSVladimir Oltean 
1008e21268efSVladimir Oltean 		if (!ocelot_port) {
1009e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1010e21268efSVladimir Oltean 			mask = 0;
1011e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1012e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1013e21268efSVladimir Oltean 			 * forward packets to all other ports except for
1014e21268efSVladimir Oltean 			 * themselves
1015e21268efSVladimir Oltean 			 */
1016e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1017e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
1018e21268efSVladimir Oltean 		} else if (ocelot->bridge_fwd_mask & BIT(port)) {
1019528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
10209b521250SVladimir Oltean 
1021e21268efSVladimir Oltean 			mask = ocelot->bridge_fwd_mask & ~BIT(port);
102223ca3b72SVladimir Oltean 			if (bond) {
102323ca3b72SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond,
102423ca3b72SVladimir Oltean 							      false);
102523ca3b72SVladimir Oltean 			}
10269b521250SVladimir Oltean 		} else {
1027e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1028e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1029e21268efSVladimir Oltean 			 * module otherwise.
1030e21268efSVladimir Oltean 			 */
1031e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
1032e21268efSVladimir Oltean 		}
1033e21268efSVladimir Oltean 
1034e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
10359b521250SVladimir Oltean 	}
10369b521250SVladimir Oltean }
1037e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
10389b521250SVladimir Oltean 
10395e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1040a556c76aSAlexandre Belloni {
1041a556c76aSAlexandre Belloni 	u32 port_cfg;
1042a556c76aSAlexandre Belloni 
10434bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
10444bda1415SVladimir Oltean 		return;
1045a556c76aSAlexandre Belloni 
10464bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1047a556c76aSAlexandre Belloni 
1048a556c76aSAlexandre Belloni 	switch (state) {
1049a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
10504bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
1051df561f66SGustavo A. R. Silva 		fallthrough;
1052a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1053a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1054a556c76aSAlexandre Belloni 		break;
1055a556c76aSAlexandre Belloni 
1056a556c76aSAlexandre Belloni 	default:
1057a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
10584bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
1059a556c76aSAlexandre Belloni 		break;
1060a556c76aSAlexandre Belloni 	}
1061a556c76aSAlexandre Belloni 
10624bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
1063a556c76aSAlexandre Belloni 
10649b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1065a556c76aSAlexandre Belloni }
10665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1067a556c76aSAlexandre Belloni 
10685e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
10694bda1415SVladimir Oltean {
1070c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1071c0d7eccbSVladimir Oltean 
1072c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1073c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1074c0d7eccbSVladimir Oltean 	 */
1075c0d7eccbSVladimir Oltean 	if (!age_period)
1076c0d7eccbSVladimir Oltean 		age_period = 1;
1077c0d7eccbSVladimir Oltean 
1078c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1079a556c76aSAlexandre Belloni }
10805e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1081a556c76aSAlexandre Belloni 
1082a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1083a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1084a556c76aSAlexandre Belloni 						     u16 vid)
1085a556c76aSAlexandre Belloni {
1086a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1087a556c76aSAlexandre Belloni 
1088a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1089a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1090a556c76aSAlexandre Belloni 			return mc;
1091a556c76aSAlexandre Belloni 	}
1092a556c76aSAlexandre Belloni 
1093a556c76aSAlexandre Belloni 	return NULL;
1094a556c76aSAlexandre Belloni }
1095a556c76aSAlexandre Belloni 
10969403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
10979403c158SVladimir Oltean {
10989403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
10999403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
11009403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
11019403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
11027c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
11039403c158SVladimir Oltean }
11049403c158SVladimir Oltean 
1105e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1106e5d1f896SVladimir Oltean 					     unsigned long ports)
1107e5d1f896SVladimir Oltean {
1108e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1109e5d1f896SVladimir Oltean 
1110e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1111e5d1f896SVladimir Oltean 	if (!pgid)
1112e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1113e5d1f896SVladimir Oltean 
1114e5d1f896SVladimir Oltean 	pgid->ports = ports;
1115e5d1f896SVladimir Oltean 	pgid->index = index;
1116e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1117e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1118e5d1f896SVladimir Oltean 
1119e5d1f896SVladimir Oltean 	return pgid;
1120e5d1f896SVladimir Oltean }
1121e5d1f896SVladimir Oltean 
1122e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1123e5d1f896SVladimir Oltean {
1124e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1125e5d1f896SVladimir Oltean 		return;
1126e5d1f896SVladimir Oltean 
1127e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1128e5d1f896SVladimir Oltean 	kfree(pgid);
1129e5d1f896SVladimir Oltean }
1130e5d1f896SVladimir Oltean 
1131e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1132bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
11339403c158SVladimir Oltean {
1134e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1135e5d1f896SVladimir Oltean 	int index;
11369403c158SVladimir Oltean 
11379403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
11389403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
11399403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
11409403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
11419403c158SVladimir Oltean 	 */
1142bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1143bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1144e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
11459403c158SVladimir Oltean 
1146e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1147e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1148e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1149e5d1f896SVladimir Oltean 		 */
1150e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1151e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1152e5d1f896SVladimir Oltean 			return pgid;
1153e5d1f896SVladimir Oltean 		}
1154e5d1f896SVladimir Oltean 	}
1155e5d1f896SVladimir Oltean 
1156e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1157e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
11589403c158SVladimir Oltean 		bool used = false;
11599403c158SVladimir Oltean 
1160e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1161e5d1f896SVladimir Oltean 			if (pgid->index == index) {
11629403c158SVladimir Oltean 				used = true;
11639403c158SVladimir Oltean 				break;
11649403c158SVladimir Oltean 			}
11659403c158SVladimir Oltean 		}
11669403c158SVladimir Oltean 
11679403c158SVladimir Oltean 		if (!used)
1168e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
11699403c158SVladimir Oltean 	}
11709403c158SVladimir Oltean 
1171e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
11729403c158SVladimir Oltean }
11739403c158SVladimir Oltean 
11749403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1175bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
11769403c158SVladimir Oltean {
1177ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
11789403c158SVladimir Oltean 
1179bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
11809403c158SVladimir Oltean 		addr[0] = 0;
11819403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
11829403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1183bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
11849403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
11859403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
11869403c158SVladimir Oltean 	}
11879403c158SVladimir Oltean }
11889403c158SVladimir Oltean 
1189209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1190209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1191a556c76aSAlexandre Belloni {
1192a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1193004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1194e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1195a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1196a556c76aSAlexandre Belloni 
1197471beb11SVladimir Oltean 	if (port == ocelot->npi)
1198471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1199471beb11SVladimir Oltean 
1200a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1201a556c76aSAlexandre Belloni 	if (!mc) {
1202728e69aeSVladimir Oltean 		/* New entry */
1203bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1204bb8d53fdSVladimir Oltean 		if (!mc)
1205bb8d53fdSVladimir Oltean 			return -ENOMEM;
1206bb8d53fdSVladimir Oltean 
1207bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1208bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1209bb8d53fdSVladimir Oltean 		mc->vid = vid;
1210bb8d53fdSVladimir Oltean 
1211a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1212728e69aeSVladimir Oltean 	} else {
1213e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1214e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1215e5d1f896SVladimir Oltean 		 */
1216e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1217bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1218a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1219a556c76aSAlexandre Belloni 	}
1220a556c76aSAlexandre Belloni 
1221004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1222e5d1f896SVladimir Oltean 
1223e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1224e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1225e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1226e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1227e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1228e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1229e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1230e5d1f896SVladimir Oltean 	}
1231e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1232e5d1f896SVladimir Oltean 
1233bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1234a556c76aSAlexandre Belloni 
1235e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1236e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1237e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1238e5d1f896SVladimir Oltean 				 pgid->index);
1239e5d1f896SVladimir Oltean 
1240e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1241bb8d53fdSVladimir Oltean 				 mc->entry_type);
1242a556c76aSAlexandre Belloni }
1243209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1244a556c76aSAlexandre Belloni 
1245209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1246a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1247a556c76aSAlexandre Belloni {
1248a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1249004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1250e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1251a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1252a556c76aSAlexandre Belloni 
1253471beb11SVladimir Oltean 	if (port == ocelot->npi)
1254471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1255471beb11SVladimir Oltean 
1256a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1257a556c76aSAlexandre Belloni 	if (!mc)
1258a556c76aSAlexandre Belloni 		return -ENOENT;
1259a556c76aSAlexandre Belloni 
1260bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1261a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1262a556c76aSAlexandre Belloni 
1263e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1264004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1265a556c76aSAlexandre Belloni 	if (!mc->ports) {
1266a556c76aSAlexandre Belloni 		list_del(&mc->list);
1267a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1268a556c76aSAlexandre Belloni 		return 0;
1269a556c76aSAlexandre Belloni 	}
1270a556c76aSAlexandre Belloni 
1271e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1272e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1273e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1274e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1275e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1276e5d1f896SVladimir Oltean 
1277bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1278a556c76aSAlexandre Belloni 
1279e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1280e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1281e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1282e5d1f896SVladimir Oltean 				 pgid->index);
1283e5d1f896SVladimir Oltean 
1284e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1285bb8d53fdSVladimir Oltean 				 mc->entry_type);
1286a556c76aSAlexandre Belloni }
1287209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1288a556c76aSAlexandre Belloni 
12895e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1290a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1291a556c76aSAlexandre Belloni {
1292a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1293a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1294a556c76aSAlexandre Belloni 	} else {
1295a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1296a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1297a556c76aSAlexandre Belloni 			 * unsupported */
1298a556c76aSAlexandre Belloni 			return -ENODEV;
1299a556c76aSAlexandre Belloni 	}
1300a556c76aSAlexandre Belloni 
1301f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1302a556c76aSAlexandre Belloni 
1303a556c76aSAlexandre Belloni 	return 0;
1304a556c76aSAlexandre Belloni }
13055e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1306a556c76aSAlexandre Belloni 
13075e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1308a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1309a556c76aSAlexandre Belloni {
1310c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
13112e554a7aSVladimir Oltean 	int ret;
13122e554a7aSVladimir Oltean 
131397bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1314a556c76aSAlexandre Belloni 
1315a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1316a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
13177142529fSAntoine Tenart 
1318bae33f2bSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false);
13192e554a7aSVladimir Oltean 	if (ret)
13202e554a7aSVladimir Oltean 		return ret;
13212e554a7aSVladimir Oltean 
1322c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
13232f0402feSVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
13242f0402feSVladimir Oltean 
13252f0402feSVladimir Oltean 	return 0;
1326a556c76aSAlexandre Belloni }
13275e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1328a556c76aSAlexandre Belloni 
1329dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1330dc96ee37SAlexandre Belloni {
1331528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1332dc96ee37SAlexandre Belloni 	int i, port, lag;
1333dc96ee37SAlexandre Belloni 
1334dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
133596b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1336dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1337dc96ee37SAlexandre Belloni 
133896b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1339dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1340dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1341dc96ee37SAlexandre Belloni 
1342528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1343528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1344528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1345528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1346528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1347528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1348528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1349528d3f19SVladimir Oltean 	 */
1350528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1351528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1352528d3f19SVladimir Oltean 
1353528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1354528d3f19SVladimir Oltean 			continue;
1355528d3f19SVladimir Oltean 
1356528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1357528d3f19SVladimir Oltean 	}
1358528d3f19SVladimir Oltean 
1359528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1360dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1361528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
136223ca3b72SVladimir Oltean 		int num_active_ports = 0;
1363dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1364dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1365dc96ee37SAlexandre Belloni 
1366528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1367dc96ee37SAlexandre Belloni 			continue;
1368dc96ee37SAlexandre Belloni 
136923ca3b72SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1370528d3f19SVladimir Oltean 
1371dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1372dc96ee37SAlexandre Belloni 			// Destination mask
1373dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1374dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
137523ca3b72SVladimir Oltean 			aggr_idx[num_active_ports++] = port;
1376dc96ee37SAlexandre Belloni 		}
1377dc96ee37SAlexandre Belloni 
137896b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1379dc96ee37SAlexandre Belloni 			u32 ac;
1380dc96ee37SAlexandre Belloni 
1381dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1382dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
138323ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
138423ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
138523ca3b72SVladimir Oltean 			 */
138623ca3b72SVladimir Oltean 			if (num_active_ports)
138723ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1388dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1389dc96ee37SAlexandre Belloni 		}
1390528d3f19SVladimir Oltean 
1391528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1392528d3f19SVladimir Oltean 		 * the same config again.
1393528d3f19SVladimir Oltean 		 */
1394528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1395528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1396528d3f19SVladimir Oltean 
1397528d3f19SVladimir Oltean 			if (!ocelot_port)
1398528d3f19SVladimir Oltean 				continue;
1399528d3f19SVladimir Oltean 
1400528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1401528d3f19SVladimir Oltean 				visited |= BIT(port);
1402528d3f19SVladimir Oltean 		}
1403dc96ee37SAlexandre Belloni 	}
1404dc96ee37SAlexandre Belloni }
1405dc96ee37SAlexandre Belloni 
14062527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
14072527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
14082527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
14092527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
14102527f2e8SVladimir Oltean  */
14112527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1412dc96ee37SAlexandre Belloni {
14132527f2e8SVladimir Oltean 	int port;
1414dc96ee37SAlexandre Belloni 
14152527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
14162527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
14172527f2e8SVladimir Oltean 		struct net_device *bond;
1418dc96ee37SAlexandre Belloni 
14192527f2e8SVladimir Oltean 		if (!ocelot_port)
14202527f2e8SVladimir Oltean 			continue;
1421dc96ee37SAlexandre Belloni 
14222527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
14232527f2e8SVladimir Oltean 		if (bond) {
142423ca3b72SVladimir Oltean 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
142523ca3b72SVladimir Oltean 							     false));
14262527f2e8SVladimir Oltean 
14272527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
1428dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
14292527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
14302527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
14312527f2e8SVladimir Oltean 		} else {
14322527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
14332527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
14342527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
14352527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
14362527f2e8SVladimir Oltean 		}
1437dc96ee37SAlexandre Belloni 	}
1438dc96ee37SAlexandre Belloni }
1439dc96ee37SAlexandre Belloni 
14409c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1441583cbbe3SVladimir Oltean 			 struct net_device *bond,
1442583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
1443dc96ee37SAlexandre Belloni {
1444583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1445583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
1446583cbbe3SVladimir Oltean 
1447b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
1448dc96ee37SAlexandre Belloni 
14492527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
14509b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1451dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1452dc96ee37SAlexandre Belloni 
1453dc96ee37SAlexandre Belloni 	return 0;
1454dc96ee37SAlexandre Belloni }
14559c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1456dc96ee37SAlexandre Belloni 
14579c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1458dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1459dc96ee37SAlexandre Belloni {
1460b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
1461b80af659SVladimir Oltean 
14622527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
14639b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1464dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1465dc96ee37SAlexandre Belloni }
14669c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
14670e332c85SPetr Machata 
146823ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
146923ca3b72SVladimir Oltean {
147023ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
147123ca3b72SVladimir Oltean 
147223ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
147323ca3b72SVladimir Oltean 
147423ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
147523ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
147623ca3b72SVladimir Oltean }
147723ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
147823ca3b72SVladimir Oltean 
1479a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1480a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
14810b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
14820b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
14830b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1484a8015dedSVladimir Oltean  */
14850b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
148631350d7fSVladimir Oltean {
148731350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1488a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1489e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1490601e984fSVladimir Oltean 	int atop, atop_tot;
149131350d7fSVladimir Oltean 
14920b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
14930b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
14940b912fc9SVladimir Oltean 
1495cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
14960b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1497cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
14980b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
14990b912fc9SVladimir Oltean 	}
15000b912fc9SVladimir Oltean 
1501a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1502fa914e9cSVladimir Oltean 
1503e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1504e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1505e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1506541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1507541132f0SMaxim Kochetkov 			    pause_start);
1508541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1509541132f0SMaxim Kochetkov 			    pause_stop);
1510fa914e9cSVladimir Oltean 
1511601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1512f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1513a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1514601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1515601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1516601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1517fa914e9cSVladimir Oltean }
15180b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
15190b912fc9SVladimir Oltean 
15200b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
15210b912fc9SVladimir Oltean {
15220b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
15230b912fc9SVladimir Oltean 
15240b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
15250b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
15260b912fc9SVladimir Oltean 
1527cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
15280b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1529cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
15300b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
15310b912fc9SVladimir Oltean 	}
15320b912fc9SVladimir Oltean 
15330b912fc9SVladimir Oltean 	return max_mtu;
15340b912fc9SVladimir Oltean }
15350b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1536fa914e9cSVladimir Oltean 
15375e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1538fa914e9cSVladimir Oltean {
1539fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1540fa914e9cSVladimir Oltean 
1541b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
15426565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
154331350d7fSVladimir Oltean 
154431350d7fSVladimir Oltean 	/* Basic L2 initialization */
154531350d7fSVladimir Oltean 
15465bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
15475bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
15485bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
15495bc9d2e6SVladimir Oltean 	 */
15505bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
15515bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
15525bc9d2e6SVladimir Oltean 
15535bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
15545bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
15555bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
15565bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
15575bc9d2e6SVladimir Oltean 	mdelay(1);
15585bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
15595bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
15605bc9d2e6SVladimir Oltean 
15615bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1562a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
15635bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
15645bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1565a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
15665bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
15675bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
15685bc9d2e6SVladimir Oltean 
15695bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
15705bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
15715bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
15725bc9d2e6SVladimir Oltean 
1573e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1574541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1575e8e6e73dSVladimir Oltean 
157631350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
157731350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
157831350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
157931350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
158031350d7fSVladimir Oltean 
158131350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
158231350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
158331350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
158431350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
158531350d7fSVladimir Oltean 
158631350d7fSVladimir Oltean 	/* Enable vcap lookups */
158731350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
158831350d7fSVladimir Oltean }
15895e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
159031350d7fSVladimir Oltean 
15912d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
15922d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
15932d44b097SVladimir Oltean  * NPI mode is used).
159469df578cSVladimir Oltean  */
15952d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
159621468199SVladimir Oltean {
159769df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
159869df578cSVladimir Oltean 
159969df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
160021468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
160169df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
160269df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
160369df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
160469df578cSVladimir Oltean 	 */
160521468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
160621468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
160721468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
160821468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
160921468199SVladimir Oltean 
161069df578cSVladimir Oltean 	/* Enable CPU port module */
1611886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
161269df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1613886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
1614cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
1615886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
1616cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
161721468199SVladimir Oltean 
161821468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
161921468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
162021468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
162121468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
162221468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
162321468199SVladimir Oltean }
162421468199SVladimir Oltean 
1625f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
1626f6fe01d6SVladimir Oltean {
1627f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
1628f6fe01d6SVladimir Oltean 
1629f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
1630f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
1631f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
1632f6fe01d6SVladimir Oltean 	 */
1633f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
1634f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
1635f6fe01d6SVladimir Oltean 
1636f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
1637f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
1638f6fe01d6SVladimir Oltean }
1639f6fe01d6SVladimir Oltean 
1640a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1641a556c76aSAlexandre Belloni {
1642a556c76aSAlexandre Belloni 	char queue_name[32];
164321468199SVladimir Oltean 	int i, ret;
164421468199SVladimir Oltean 	u32 port;
1645a556c76aSAlexandre Belloni 
16463a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
16473a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
16483a77b593SVladimir Oltean 		if (ret) {
16493a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
16503a77b593SVladimir Oltean 			return ret;
16513a77b593SVladimir Oltean 		}
16523a77b593SVladimir Oltean 	}
16533a77b593SVladimir Oltean 
1654a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1655a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1656a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1657a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1658a556c76aSAlexandre Belloni 		return -ENOMEM;
1659a556c76aSAlexandre Belloni 
1660a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
16614e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
16624e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1663a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1664a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1665a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1666a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1667a556c76aSAlexandre Belloni 		return -ENOMEM;
1668a556c76aSAlexandre Belloni 
1669ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1670ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
1671ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
1672ca0b272bSVladimir Oltean 		return -ENOMEM;
1673ca0b272bSVladimir Oltean 	}
1674ca0b272bSVladimir Oltean 
16752b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1676e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
1677f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
1678a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1679a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1680aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
16812d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
1682a556c76aSAlexandre Belloni 
1683a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1684a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1685a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1686a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1687a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1688a556c76aSAlexandre Belloni 	}
1689a556c76aSAlexandre Belloni 
1690a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1691a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1692a556c76aSAlexandre Belloni 
1693a556c76aSAlexandre Belloni 	/* Aggregation mode */
1694a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1695a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1696a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1697f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
1698f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
1699f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
1700f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
1701a556c76aSAlexandre Belloni 
1702a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1703a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1704a556c76aSAlexandre Belloni 	 */
1705a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1706a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1707a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1708a556c76aSAlexandre Belloni 
1709a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1710a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1711a556c76aSAlexandre Belloni 
1712a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1713a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1714a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1715a556c76aSAlexandre Belloni 
1716a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1717edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
1718a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1719*b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
1720a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1721edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
1722a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1723a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1724a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1725a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1726a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1727a556c76aSAlexandre Belloni 
1728a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1729a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1730a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1731a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1732a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1733a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1734a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1735a556c76aSAlexandre Belloni 				 port);
1736a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1737a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1738a556c76aSAlexandre Belloni 	}
1739a556c76aSAlexandre Belloni 
174096b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
1741a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1742a556c76aSAlexandre Belloni 
1743a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1744a556c76aSAlexandre Belloni 	}
1745*b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
1746*b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
1747*b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
1748a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
1749*b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
1750*b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
1751*b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
1752a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1753a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1754a556c76aSAlexandre Belloni 
1755a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1756a556c76aSAlexandre Belloni 	 * registers endianness.
1757a556c76aSAlexandre Belloni 	 */
1758a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1759a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1760a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1761a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1762a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1763a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1764a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1765a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1766a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1767a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1768a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1769a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1770a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1771a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1772a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1773a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1774a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1775a556c76aSAlexandre Belloni 
17761e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1777a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1778a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
17794e3b0468SAntoine Tenart 
1780a556c76aSAlexandre Belloni 	return 0;
1781a556c76aSAlexandre Belloni }
1782a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1783a556c76aSAlexandre Belloni 
1784a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1785a556c76aSAlexandre Belloni {
1786c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
1787a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1788ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
1789a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
1790a556c76aSAlexandre Belloni }
1791a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1792a556c76aSAlexandre Belloni 
1793e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
1794e5fb512dSVladimir Oltean {
1795e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1796e5fb512dSVladimir Oltean 
1797e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
1798e5fb512dSVladimir Oltean }
1799e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
1800e5fb512dSVladimir Oltean 
1801a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1802