xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 1650bdb1c516c248fb06f6d076559ff6437a5853)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
8a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
939e5308bSYangbo Lu #include <linux/ptp_classify.h>
1020968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
11a556c76aSAlexandre Belloni #include "ocelot.h"
123c83654fSVladimir Oltean #include "ocelot_vcap.h"
13a556c76aSAlexandre Belloni 
14639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
15639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
16639c1b26SSteen Hegelund 
17a556c76aSAlexandre Belloni struct ocelot_mact_entry {
18a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
19a556c76aSAlexandre Belloni 	u16 vid;
20a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
21a556c76aSAlexandre Belloni };
22a556c76aSAlexandre Belloni 
23639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
24639c1b26SSteen Hegelund {
25639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
26639c1b26SSteen Hegelund }
27639c1b26SSteen Hegelund 
28a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
29a556c76aSAlexandre Belloni {
30639c1b26SSteen Hegelund 	u32 val;
31a556c76aSAlexandre Belloni 
32639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
33639c1b26SSteen Hegelund 		ocelot, val,
34639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
35639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
36639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
37a556c76aSAlexandre Belloni }
38a556c76aSAlexandre Belloni 
39a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
40a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
41a556c76aSAlexandre Belloni 			       unsigned int vid)
42a556c76aSAlexandre Belloni {
43a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
44a556c76aSAlexandre Belloni 
45a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
46a556c76aSAlexandre Belloni 	 * understood by the hardware.
47a556c76aSAlexandre Belloni 	 */
48a556c76aSAlexandre Belloni 	mach |= vid    << 16;
49a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
50a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
51a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
52a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
53a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
54a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
55a556c76aSAlexandre Belloni 
56a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
57a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
58a556c76aSAlexandre Belloni 
59a556c76aSAlexandre Belloni }
60a556c76aSAlexandre Belloni 
619c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
62a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
639c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
64a556c76aSAlexandre Belloni {
65584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
66584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
67584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
68584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
69584b7cfcSAlban Bedel 	unsigned int mc_ports;
70584b7cfcSAlban Bedel 
71584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
72584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
73584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
74584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
75584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
76584b7cfcSAlban Bedel 	else
77584b7cfcSAlban Bedel 		mc_ports = 0;
78584b7cfcSAlban Bedel 
79584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
80584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
81584b7cfcSAlban Bedel 
82a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
83a556c76aSAlexandre Belloni 
84a556c76aSAlexandre Belloni 	/* Issue a write command */
85584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
86a556c76aSAlexandre Belloni 
87a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
88a556c76aSAlexandre Belloni }
899c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
90a556c76aSAlexandre Belloni 
919c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
929c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
93a556c76aSAlexandre Belloni {
94a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
95a556c76aSAlexandre Belloni 
96a556c76aSAlexandre Belloni 	/* Issue a forget command */
97a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
98a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
99a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
100a556c76aSAlexandre Belloni 
101a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
102a556c76aSAlexandre Belloni }
1039c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
104a556c76aSAlexandre Belloni 
105a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
106a556c76aSAlexandre Belloni {
107a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
108a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
109a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
110a556c76aSAlexandre Belloni 	 */
111a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
112a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
113a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
114a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
115a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
116a556c76aSAlexandre Belloni 
117a556c76aSAlexandre Belloni 	/* Clear the MAC table */
118a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
119a556c76aSAlexandre Belloni }
120a556c76aSAlexandre Belloni 
121f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
122b5962294SHoratiu Vultur {
123b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
124b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
125f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
12675944fdaSXiaoliang Yang 
12775944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
12875944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1292f17c050SXiaoliang Yang 
1302f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1312f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1322f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
133b5962294SHoratiu Vultur }
134b5962294SHoratiu Vultur 
135639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
136639c1b26SSteen Hegelund {
137639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
138639c1b26SSteen Hegelund }
139639c1b26SSteen Hegelund 
140a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
141a556c76aSAlexandre Belloni {
142639c1b26SSteen Hegelund 	u32 val;
143a556c76aSAlexandre Belloni 
144639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
145639c1b26SSteen Hegelund 		ocelot,
146639c1b26SSteen Hegelund 		val,
147639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
148639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
149639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
150a556c76aSAlexandre Belloni }
151a556c76aSAlexandre Belloni 
1527142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1537142529fSAntoine Tenart {
1547142529fSAntoine Tenart 	/* Select the VID to configure */
1557142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1567142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1577142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1587142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1597142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1607142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1617142529fSAntoine Tenart 
1627142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1637142529fSAntoine Tenart }
1647142529fSAntoine Tenart 
1652f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
166c3e58a75SVladimir Oltean 					struct ocelot_vlan native_vlan)
16797bb69e1SVladimir Oltean {
16897bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
16987b0f983SVladimir Oltean 	u32 val = 0;
17097bb69e1SVladimir Oltean 
171c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
17297bb69e1SVladimir Oltean 
173c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1747142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
17597bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17697bb69e1SVladimir Oltean 
17787b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
178e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
17987b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
18087b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
18187b0f983SVladimir Oltean 		else
18287b0f983SVladimir Oltean 			/* Tag all frames */
18387b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
18487b0f983SVladimir Oltean 	} else {
18587b0f983SVladimir Oltean 		/* Port tagging disabled. */
18687b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
18787b0f983SVladimir Oltean 	}
18887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
18987b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
19087b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
19197bb69e1SVladimir Oltean }
19297bb69e1SVladimir Oltean 
19375e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
194c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
195c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
19675e5a554SVladimir Oltean {
19775e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
198be0576feSVladimir Oltean 	u32 val = 0;
19975e5a554SVladimir Oltean 
200c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
20175e5a554SVladimir Oltean 
20275e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
203c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
20475e5a554SVladimir Oltean 
20575e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
206c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
20775e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
20875e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
209be0576feSVladimir Oltean 
210be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
211be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
212be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
213be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
214be0576feSVladimir Oltean 	 */
215be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
216be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
217be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
218be0576feSVladimir Oltean 
219be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
220be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
221be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
222be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
22375e5a554SVladimir Oltean }
22475e5a554SVladimir Oltean 
2252e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
226bae33f2bSVladimir Oltean 			       bool vlan_aware)
22787b0f983SVladimir Oltean {
22870edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
229bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
23070edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
231bae33f2bSVladimir Oltean 	u32 val;
23270edfae1SVladimir Oltean 
23370edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
23470edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
23570edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
23670edfae1SVladimir Oltean 			dev_err(ocelot->dev,
23770edfae1SVladimir Oltean 				"Cannot change VLAN state with vlan modify rules active\n");
23870edfae1SVladimir Oltean 			return -EBUSY;
23970edfae1SVladimir Oltean 		}
24070edfae1SVladimir Oltean 	}
24170edfae1SVladimir Oltean 
24287b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
24387b0f983SVladimir Oltean 
24487b0f983SVladimir Oltean 	if (vlan_aware)
24587b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24687b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
24787b0f983SVladimir Oltean 	else
24887b0f983SVladimir Oltean 		val = 0;
24987b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
25087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
25187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
25287b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
25387b0f983SVladimir Oltean 
254c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
255c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2562e554a7aSVladimir Oltean 
2572e554a7aSVladimir Oltean 	return 0;
25887b0f983SVladimir Oltean }
25987b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
26087b0f983SVladimir Oltean 
2612f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2622f0402feSVladimir Oltean 			bool untagged)
2632f0402feSVladimir Oltean {
2642f0402feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2652f0402feSVladimir Oltean 
2662f0402feSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
2672f0402feSVladimir Oltean 	if (untagged && ocelot_port->native_vlan.vid != vid &&
2682f0402feSVladimir Oltean 	    ocelot_port->native_vlan.valid) {
2692f0402feSVladimir Oltean 		dev_err(ocelot->dev,
2702f0402feSVladimir Oltean 			"Port already has a native VLAN: %d\n",
2712f0402feSVladimir Oltean 			ocelot_port->native_vlan.vid);
2722f0402feSVladimir Oltean 		return -EBUSY;
2732f0402feSVladimir Oltean 	}
2742f0402feSVladimir Oltean 
2752f0402feSVladimir Oltean 	return 0;
2762f0402feSVladimir Oltean }
2772f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
2782f0402feSVladimir Oltean 
2795e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2807142529fSAntoine Tenart 		    bool untagged)
2817142529fSAntoine Tenart {
2827142529fSAntoine Tenart 	int ret;
2837142529fSAntoine Tenart 
2847142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
28597bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2867142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2877142529fSAntoine Tenart 	if (ret)
2887142529fSAntoine Tenart 		return ret;
2897142529fSAntoine Tenart 
2907142529fSAntoine Tenart 	/* Default ingress vlan classification */
291c3e58a75SVladimir Oltean 	if (pvid) {
292c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
293c3e58a75SVladimir Oltean 
294c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
295e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
296c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
297c3e58a75SVladimir Oltean 	}
2987142529fSAntoine Tenart 
2997142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
30097bb69e1SVladimir Oltean 	if (untagged) {
301c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
302c3e58a75SVladimir Oltean 
303c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
304e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
3052f0402feSVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
306b9cd75e6SVladimir Oltean 	}
3077142529fSAntoine Tenart 
3087142529fSAntoine Tenart 	return 0;
3097142529fSAntoine Tenart }
3105e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
3117142529fSAntoine Tenart 
3125e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3139855934cSVladimir Oltean {
3149855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3159855934cSVladimir Oltean 	int ret;
3167142529fSAntoine Tenart 
3177142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
31897bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3197142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3207142529fSAntoine Tenart 	if (ret)
3217142529fSAntoine Tenart 		return ret;
3227142529fSAntoine Tenart 
323be0576feSVladimir Oltean 	/* Ingress */
324be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
325be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
326be0576feSVladimir Oltean 
327be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
328be0576feSVladimir Oltean 	}
329be0576feSVladimir Oltean 
3307142529fSAntoine Tenart 	/* Egress */
331c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
332e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
333c3e58a75SVladimir Oltean 
334c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
335c3e58a75SVladimir Oltean 	}
3367142529fSAntoine Tenart 
3377142529fSAntoine Tenart 	return 0;
3387142529fSAntoine Tenart }
3395e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3407142529fSAntoine Tenart 
341a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
342a556c76aSAlexandre Belloni {
3437142529fSAntoine Tenart 	u16 port, vid;
3447142529fSAntoine Tenart 
345a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
346a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
347a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
348a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3497142529fSAntoine Tenart 
3507142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3517142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3527142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3537142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3547142529fSAntoine Tenart 	}
3557142529fSAntoine Tenart 
3567142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3577142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3587142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3597142529fSAntoine Tenart 	 */
3607142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3617142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3627142529fSAntoine Tenart 
3637142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3647142529fSAntoine Tenart 	 * default.
3657142529fSAntoine Tenart 	 */
366714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
367714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3687142529fSAntoine Tenart 
3697142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3707142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3717142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3727142529fSAntoine Tenart 	}
373a556c76aSAlexandre Belloni }
374a556c76aSAlexandre Belloni 
375eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
376eb4733d7SVladimir Oltean {
377eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
378eb4733d7SVladimir Oltean }
379eb4733d7SVladimir Oltean 
380eb4733d7SVladimir Oltean int ocelot_port_flush(struct ocelot *ocelot, int port)
381eb4733d7SVladimir Oltean {
382*1650bdb1SVladimir Oltean 	unsigned int pause_ena;
383eb4733d7SVladimir Oltean 	int err, val;
384eb4733d7SVladimir Oltean 
385eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
386eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
387eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
388eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
389eb4733d7SVladimir Oltean 
390eb4733d7SVladimir Oltean 	/* Disable flow control */
391*1650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
392eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
393eb4733d7SVladimir Oltean 
394eb4733d7SVladimir Oltean 	/* Disable priority flow control */
395eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
396eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
397eb4733d7SVladimir Oltean 
398eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
399eb4733d7SVladimir Oltean 	 * at the port.
400eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
401eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
402eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
403eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
404eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
405eb4733d7SVladimir Oltean 	 */
406eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
407eb4733d7SVladimir Oltean 
408eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
409eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
410eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
411eb4733d7SVladimir Oltean 
412eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
413eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
414eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
415eb4733d7SVladimir Oltean 
416eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
417eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
418eb4733d7SVladimir Oltean 		       port);
419eb4733d7SVladimir Oltean 
420eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
421eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
422eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
423eb4733d7SVladimir Oltean 
424eb4733d7SVladimir Oltean 	/* Clear flushing again. */
425eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
426eb4733d7SVladimir Oltean 
427*1650bdb1SVladimir Oltean 	/* Re-enable flow control */
428*1650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
429*1650bdb1SVladimir Oltean 
430eb4733d7SVladimir Oltean 	return err;
431eb4733d7SVladimir Oltean }
432eb4733d7SVladimir Oltean EXPORT_SYMBOL(ocelot_port_flush);
433eb4733d7SVladimir Oltean 
4345e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
43526f4dbabSVladimir Oltean 			struct phy_device *phydev)
436a556c76aSAlexandre Belloni {
43726f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
4385bc9d2e6SVladimir Oltean 	int speed, mode = 0;
439a556c76aSAlexandre Belloni 
44026f4dbabSVladimir Oltean 	switch (phydev->speed) {
441a556c76aSAlexandre Belloni 	case SPEED_10:
442a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
443a556c76aSAlexandre Belloni 		break;
444a556c76aSAlexandre Belloni 	case SPEED_100:
445a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
446a556c76aSAlexandre Belloni 		break;
447a556c76aSAlexandre Belloni 	case SPEED_1000:
448a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
449a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
450a556c76aSAlexandre Belloni 		break;
451a556c76aSAlexandre Belloni 	case SPEED_2500:
452a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
453a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
454a556c76aSAlexandre Belloni 		break;
455a556c76aSAlexandre Belloni 	default:
45626f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
45726f4dbabSVladimir Oltean 			port, phydev->speed);
458a556c76aSAlexandre Belloni 		return;
459a556c76aSAlexandre Belloni 	}
460a556c76aSAlexandre Belloni 
46126f4dbabSVladimir Oltean 	phy_print_status(phydev);
462a556c76aSAlexandre Belloni 
46326f4dbabSVladimir Oltean 	if (!phydev->link)
464a556c76aSAlexandre Belloni 		return;
465a556c76aSAlexandre Belloni 
466a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
467004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
468a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
469a556c76aSAlexandre Belloni 
4701ba8f656SVladimir Oltean 	/* Disable HDX fast control */
4711ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
4721ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
4731ba8f656SVladimir Oltean 
4741ba8f656SVladimir Oltean 	/* SGMII only for now */
4751ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4761ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
4771ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4781ba8f656SVladimir Oltean 
4791ba8f656SVladimir Oltean 	/* Enable PCS */
4801ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4811ba8f656SVladimir Oltean 
4821ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4831ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4841ba8f656SVladimir Oltean 
4851ba8f656SVladimir Oltean 	/* No loopback */
4861ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
487a556c76aSAlexandre Belloni 
488a556c76aSAlexandre Belloni 	/* Enable MAC module */
489004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
490a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
491a556c76aSAlexandre Belloni 
492a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
4931f78ff4fSYixing Liu 	 * reset
4941f78ff4fSYixing Liu 	 */
495004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
496a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
497a556c76aSAlexandre Belloni 
498a556c76aSAlexandre Belloni 	/* No PFC */
499a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
500004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
501a556c76aSAlexandre Belloni 
502a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
503886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
504886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
505a556c76aSAlexandre Belloni 
506a556c76aSAlexandre Belloni 	/* Flow control */
507a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
508a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
509a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
510a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
511a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
512004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
513004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
514a556c76aSAlexandre Belloni }
5155e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
516a556c76aSAlexandre Belloni 
5175e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
518889b8950SVladimir Oltean 			struct phy_device *phy)
519a556c76aSAlexandre Belloni {
520a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
521a556c76aSAlexandre Belloni 	 * MAC addresses.
522a556c76aSAlexandre Belloni 	 */
523a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
524a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
525004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
526004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
527889b8950SVladimir Oltean }
5285e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
529889b8950SVladimir Oltean 
5305e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
531889b8950SVladimir Oltean {
532889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
533889b8950SVladimir Oltean 
534889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
535886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
536889b8950SVladimir Oltean }
5375e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
538889b8950SVladimir Oltean 
539682eaad9SYangbo Lu static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
540e2f9a8feSVladimir Oltean 					 struct sk_buff *clone)
541400928bfSYangbo Lu {
542e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
543400928bfSYangbo Lu 
5446565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
5456565243cSVladimir Oltean 
546e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
547c4b364ceSYangbo Lu 	/* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
548c4b364ceSYangbo Lu 	OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
5496565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
550e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
5516565243cSVladimir Oltean 
5526565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
553400928bfSYangbo Lu }
554682eaad9SYangbo Lu 
55539e5308bSYangbo Lu u32 ocelot_ptp_rew_op(struct sk_buff *skb)
55639e5308bSYangbo Lu {
55739e5308bSYangbo Lu 	struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
55839e5308bSYangbo Lu 	u8 ptp_cmd = OCELOT_SKB_CB(skb)->ptp_cmd;
55939e5308bSYangbo Lu 	u32 rew_op = 0;
56039e5308bSYangbo Lu 
56139e5308bSYangbo Lu 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP && clone) {
56239e5308bSYangbo Lu 		rew_op = ptp_cmd;
56339e5308bSYangbo Lu 		rew_op |= OCELOT_SKB_CB(clone)->ts_id << 3;
56439e5308bSYangbo Lu 	} else if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
56539e5308bSYangbo Lu 		rew_op = ptp_cmd;
56639e5308bSYangbo Lu 	}
56739e5308bSYangbo Lu 
56839e5308bSYangbo Lu 	return rew_op;
56939e5308bSYangbo Lu }
57039e5308bSYangbo Lu EXPORT_SYMBOL(ocelot_ptp_rew_op);
57139e5308bSYangbo Lu 
57239e5308bSYangbo Lu static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb)
57339e5308bSYangbo Lu {
57439e5308bSYangbo Lu 	struct ptp_header *hdr;
57539e5308bSYangbo Lu 	unsigned int ptp_class;
57639e5308bSYangbo Lu 	u8 msgtype, twostep;
57739e5308bSYangbo Lu 
57839e5308bSYangbo Lu 	ptp_class = ptp_classify_raw(skb);
57939e5308bSYangbo Lu 	if (ptp_class == PTP_CLASS_NONE)
58039e5308bSYangbo Lu 		return false;
58139e5308bSYangbo Lu 
58239e5308bSYangbo Lu 	hdr = ptp_parse_header(skb, ptp_class);
58339e5308bSYangbo Lu 	if (!hdr)
58439e5308bSYangbo Lu 		return false;
58539e5308bSYangbo Lu 
58639e5308bSYangbo Lu 	msgtype = ptp_get_msgtype(hdr, ptp_class);
58739e5308bSYangbo Lu 	twostep = hdr->flag_field[0] & 0x2;
58839e5308bSYangbo Lu 
58939e5308bSYangbo Lu 	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
59039e5308bSYangbo Lu 		return true;
59139e5308bSYangbo Lu 
59239e5308bSYangbo Lu 	return false;
59339e5308bSYangbo Lu }
59439e5308bSYangbo Lu 
595682eaad9SYangbo Lu int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
596682eaad9SYangbo Lu 				 struct sk_buff *skb,
597682eaad9SYangbo Lu 				 struct sk_buff **clone)
598682eaad9SYangbo Lu {
599682eaad9SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
600682eaad9SYangbo Lu 	u8 ptp_cmd = ocelot_port->ptp_cmd;
601682eaad9SYangbo Lu 
60239e5308bSYangbo Lu 	/* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
60339e5308bSYangbo Lu 	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
60439e5308bSYangbo Lu 		if (ocelot_ptp_is_onestep_sync(skb)) {
60539e5308bSYangbo Lu 			OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
60639e5308bSYangbo Lu 			return 0;
60739e5308bSYangbo Lu 		}
60839e5308bSYangbo Lu 
60939e5308bSYangbo Lu 		/* Fall back to two-step timestamping */
61039e5308bSYangbo Lu 		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
61139e5308bSYangbo Lu 	}
61239e5308bSYangbo Lu 
613682eaad9SYangbo Lu 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
614682eaad9SYangbo Lu 		*clone = skb_clone_sk(skb);
615682eaad9SYangbo Lu 		if (!(*clone))
616682eaad9SYangbo Lu 			return -ENOMEM;
617682eaad9SYangbo Lu 
618682eaad9SYangbo Lu 		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
61939e5308bSYangbo Lu 		OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
620682eaad9SYangbo Lu 	}
621682eaad9SYangbo Lu 
622682eaad9SYangbo Lu 	return 0;
623682eaad9SYangbo Lu }
624682eaad9SYangbo Lu EXPORT_SYMBOL(ocelot_port_txtstamp_request);
625400928bfSYangbo Lu 
626e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
627e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
6284e3b0468SAntoine Tenart {
6294e3b0468SAntoine Tenart 	unsigned long flags;
6304e3b0468SAntoine Tenart 	u32 val;
6314e3b0468SAntoine Tenart 
6324e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
6334e3b0468SAntoine Tenart 
6344e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
6354e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
6364e3b0468SAntoine Tenart 
6374e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
6384e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
6394e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
6404e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
6414e3b0468SAntoine Tenart 
6424e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
6434e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
6444e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
6454e3b0468SAntoine Tenart 
6464e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
6474e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
6484e3b0468SAntoine Tenart 		ts->tv_sec--;
6494e3b0468SAntoine Tenart 
6504e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
6514e3b0468SAntoine Tenart }
652e23a7b3eSYangbo Lu 
653e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
654e23a7b3eSYangbo Lu {
655e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
656e23a7b3eSYangbo Lu 
657e23a7b3eSYangbo Lu 	while (budget--) {
658b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
659e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
660e23a7b3eSYangbo Lu 		struct ocelot_port *port;
661e23a7b3eSYangbo Lu 		struct timespec64 ts;
662b049da13SYangbo Lu 		unsigned long flags;
663e23a7b3eSYangbo Lu 		u32 val, id, txport;
664e23a7b3eSYangbo Lu 
665e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
666e23a7b3eSYangbo Lu 
667e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
668e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
669e23a7b3eSYangbo Lu 			break;
670e23a7b3eSYangbo Lu 
671e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
672e23a7b3eSYangbo Lu 
673e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
674e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
675e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
676e23a7b3eSYangbo Lu 
677e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
678e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
679e23a7b3eSYangbo Lu 
680b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
681b049da13SYangbo Lu 
682b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
683c4b364ceSYangbo Lu 			if (OCELOT_SKB_CB(skb)->ts_id != id)
684e23a7b3eSYangbo Lu 				continue;
685b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
686b049da13SYangbo Lu 			skb_match = skb;
687fc62c094SYangbo Lu 			break;
688e23a7b3eSYangbo Lu 		}
689e23a7b3eSYangbo Lu 
690b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
691b049da13SYangbo Lu 
6925fd82200Slaurent brando 		/* Get the h/w timestamp */
6935fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
694e23a7b3eSYangbo Lu 
695b049da13SYangbo Lu 		if (unlikely(!skb_match))
696e23a7b3eSYangbo Lu 			continue;
697e23a7b3eSYangbo Lu 
698e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
699e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
700e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
701e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
7025fd82200Slaurent brando 
7035fd82200Slaurent brando 		/* Next ts */
7045fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
705e23a7b3eSYangbo Lu 	}
706e23a7b3eSYangbo Lu }
707e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
7084e3b0468SAntoine Tenart 
709924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
710924ee317SVladimir Oltean 				u32 *rval)
711924ee317SVladimir Oltean {
712924ee317SVladimir Oltean 	u32 bytes_valid, val;
713924ee317SVladimir Oltean 
714924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
715924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
716924ee317SVladimir Oltean 		if (ifh)
717924ee317SVladimir Oltean 			return -EIO;
718924ee317SVladimir Oltean 
719924ee317SVladimir Oltean 		do {
720924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
721924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
722924ee317SVladimir Oltean 	}
723924ee317SVladimir Oltean 
724924ee317SVladimir Oltean 	switch (val) {
725924ee317SVladimir Oltean 	case XTR_ABORT:
726924ee317SVladimir Oltean 		return -EIO;
727924ee317SVladimir Oltean 	case XTR_EOF_0:
728924ee317SVladimir Oltean 	case XTR_EOF_1:
729924ee317SVladimir Oltean 	case XTR_EOF_2:
730924ee317SVladimir Oltean 	case XTR_EOF_3:
731924ee317SVladimir Oltean 	case XTR_PRUNED:
732924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
733924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
734924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
735924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
736924ee317SVladimir Oltean 		else
737924ee317SVladimir Oltean 			*rval = val;
738924ee317SVladimir Oltean 
739924ee317SVladimir Oltean 		return bytes_valid;
740924ee317SVladimir Oltean 	case XTR_ESCAPE:
741924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
742924ee317SVladimir Oltean 
743924ee317SVladimir Oltean 		return 4;
744924ee317SVladimir Oltean 	default:
745924ee317SVladimir Oltean 		*rval = val;
746924ee317SVladimir Oltean 
747924ee317SVladimir Oltean 		return 4;
748924ee317SVladimir Oltean 	}
749924ee317SVladimir Oltean }
750924ee317SVladimir Oltean 
751924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
752924ee317SVladimir Oltean {
753924ee317SVladimir Oltean 	int i, err = 0;
754924ee317SVladimir Oltean 
755924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
756924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
757924ee317SVladimir Oltean 		if (err != 4)
758924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
759924ee317SVladimir Oltean 	}
760924ee317SVladimir Oltean 
761924ee317SVladimir Oltean 	return 0;
762924ee317SVladimir Oltean }
763924ee317SVladimir Oltean 
764924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
765924ee317SVladimir Oltean {
766924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
7672ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
768924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
769924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
770924ee317SVladimir Oltean 	struct net_device *dev;
771924ee317SVladimir Oltean 	struct timespec64 ts;
772924ee317SVladimir Oltean 	struct sk_buff *skb;
773924ee317SVladimir Oltean 	int sz, buf_len;
774924ee317SVladimir Oltean 	u32 val, *buf;
775924ee317SVladimir Oltean 	int err;
776924ee317SVladimir Oltean 
777924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
778924ee317SVladimir Oltean 	if (err)
779924ee317SVladimir Oltean 		return err;
780924ee317SVladimir Oltean 
781924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
782924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
783924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
784924ee317SVladimir Oltean 
785924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
786924ee317SVladimir Oltean 		return -EINVAL;
787924ee317SVladimir Oltean 
788924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
789924ee317SVladimir Oltean 	if (!dev)
790924ee317SVladimir Oltean 		return -EINVAL;
791924ee317SVladimir Oltean 
792924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
793924ee317SVladimir Oltean 	if (unlikely(!skb)) {
794924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
795924ee317SVladimir Oltean 		return -ENOMEM;
796924ee317SVladimir Oltean 	}
797924ee317SVladimir Oltean 
798924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
799924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
800924ee317SVladimir Oltean 
801924ee317SVladimir Oltean 	len = 0;
802924ee317SVladimir Oltean 	do {
803924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
804924ee317SVladimir Oltean 		if (sz < 0) {
805924ee317SVladimir Oltean 			err = sz;
806924ee317SVladimir Oltean 			goto out_free_skb;
807924ee317SVladimir Oltean 		}
808924ee317SVladimir Oltean 		*buf++ = val;
809924ee317SVladimir Oltean 		len += sz;
810924ee317SVladimir Oltean 	} while (len < buf_len);
811924ee317SVladimir Oltean 
812924ee317SVladimir Oltean 	/* Read the FCS */
813924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
814924ee317SVladimir Oltean 	if (sz < 0) {
815924ee317SVladimir Oltean 		err = sz;
816924ee317SVladimir Oltean 		goto out_free_skb;
817924ee317SVladimir Oltean 	}
818924ee317SVladimir Oltean 
819924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
820924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
821924ee317SVladimir Oltean 
822924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
823924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
824924ee317SVladimir Oltean 		*buf = val;
825924ee317SVladimir Oltean 	}
826924ee317SVladimir Oltean 
827924ee317SVladimir Oltean 	if (ocelot->ptp) {
828924ee317SVladimir Oltean 		ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
829924ee317SVladimir Oltean 
830924ee317SVladimir Oltean 		tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
831924ee317SVladimir Oltean 		if ((tod_in_ns & 0xffffffff) < timestamp)
832924ee317SVladimir Oltean 			full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
833924ee317SVladimir Oltean 					timestamp;
834924ee317SVladimir Oltean 		else
835924ee317SVladimir Oltean 			full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
836924ee317SVladimir Oltean 					timestamp;
837924ee317SVladimir Oltean 
838924ee317SVladimir Oltean 		shhwtstamps = skb_hwtstamps(skb);
839924ee317SVladimir Oltean 		memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
840924ee317SVladimir Oltean 		shhwtstamps->hwtstamp = full_ts_in_ns;
841924ee317SVladimir Oltean 	}
842924ee317SVladimir Oltean 
843924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
844924ee317SVladimir Oltean 	 * has already been forwarded.
845924ee317SVladimir Oltean 	 */
846df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
847924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
848924ee317SVladimir Oltean 
849924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
850d8ea7ff3SHoratiu Vultur 
851924ee317SVladimir Oltean 	*nskb = skb;
852924ee317SVladimir Oltean 
853924ee317SVladimir Oltean 	return 0;
854924ee317SVladimir Oltean 
855924ee317SVladimir Oltean out_free_skb:
856924ee317SVladimir Oltean 	kfree_skb(skb);
857924ee317SVladimir Oltean 	return err;
858924ee317SVladimir Oltean }
859924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
860924ee317SVladimir Oltean 
861137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
862137ffbc4SVladimir Oltean {
863137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
864137ffbc4SVladimir Oltean 
865137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
866137ffbc4SVladimir Oltean 		return false;
867137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
868137ffbc4SVladimir Oltean 		return false;
869137ffbc4SVladimir Oltean 
870137ffbc4SVladimir Oltean 	return true;
871137ffbc4SVladimir Oltean }
872137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
873137ffbc4SVladimir Oltean 
874137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
875137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
876137ffbc4SVladimir Oltean {
87740d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
878137ffbc4SVladimir Oltean 	unsigned int i, count, last;
879137ffbc4SVladimir Oltean 
880137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
881137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
882137ffbc4SVladimir Oltean 
88340d3f295SVladimir Oltean 	ocelot_ifh_set_bypass(ifh, 1);
8841f778d50SVladimir Oltean 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
88540d3f295SVladimir Oltean 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
88640d3f295SVladimir Oltean 	ocelot_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
88740d3f295SVladimir Oltean 	ocelot_ifh_set_rew_op(ifh, rew_op);
888137ffbc4SVladimir Oltean 
889137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
89040d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
891137ffbc4SVladimir Oltean 
892137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
893137ffbc4SVladimir Oltean 	last = skb->len % 4;
894137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
895137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
896137ffbc4SVladimir Oltean 
897137ffbc4SVladimir Oltean 	/* Add padding */
898137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
899137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
900137ffbc4SVladimir Oltean 		i++;
901137ffbc4SVladimir Oltean 	}
902137ffbc4SVladimir Oltean 
903137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
904137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
905137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
906137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
907137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
908137ffbc4SVladimir Oltean 
909137ffbc4SVladimir Oltean 	/* Add dummy CRC */
910137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
911137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
912137ffbc4SVladimir Oltean 
913137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
914137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
915137ffbc4SVladimir Oltean }
916137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
917137ffbc4SVladimir Oltean 
9180a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
9190a6f17c6SVladimir Oltean {
9200a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
9210a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
9220a6f17c6SVladimir Oltean }
9230a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
9240a6f17c6SVladimir Oltean 
9255e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
92687b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
927a556c76aSAlexandre Belloni {
928471beb11SVladimir Oltean 	int pgid = port;
929471beb11SVladimir Oltean 
930471beb11SVladimir Oltean 	if (port == ocelot->npi)
931471beb11SVladimir Oltean 		pgid = PGID_CPU;
932a556c76aSAlexandre Belloni 
933471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
934a556c76aSAlexandre Belloni }
9355e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
936a556c76aSAlexandre Belloni 
9375e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
938531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
939531ee1a6SVladimir Oltean {
940531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
941531ee1a6SVladimir Oltean }
9425e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
943531ee1a6SVladimir Oltean 
9449c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
945531ee1a6SVladimir Oltean 			    bool is_static, void *data)
946a556c76aSAlexandre Belloni {
947531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
948a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
949a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
950a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
951a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
952a556c76aSAlexandre Belloni 
953a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
954a556c76aSAlexandre Belloni 		goto skip;
955a556c76aSAlexandre Belloni 
956a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
957a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
958a556c76aSAlexandre Belloni 	if (!nlh)
959a556c76aSAlexandre Belloni 		return -EMSGSIZE;
960a556c76aSAlexandre Belloni 
961a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
962a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
963a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
964a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
965a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
966a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
967a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
968531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
969a556c76aSAlexandre Belloni 
970531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
971a556c76aSAlexandre Belloni 		goto nla_put_failure;
972a556c76aSAlexandre Belloni 
973531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
974a556c76aSAlexandre Belloni 		goto nla_put_failure;
975a556c76aSAlexandre Belloni 
976a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
977a556c76aSAlexandre Belloni 
978a556c76aSAlexandre Belloni skip:
979a556c76aSAlexandre Belloni 	dump->idx++;
980a556c76aSAlexandre Belloni 	return 0;
981a556c76aSAlexandre Belloni 
982a556c76aSAlexandre Belloni nla_put_failure:
983a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
984a556c76aSAlexandre Belloni 	return -EMSGSIZE;
985a556c76aSAlexandre Belloni }
9869c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
987a556c76aSAlexandre Belloni 
988531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
989a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
990a556c76aSAlexandre Belloni {
991a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
992531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
993a556c76aSAlexandre Belloni 
994a556c76aSAlexandre Belloni 	/* Set row and column to read from */
995a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
996a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
997a556c76aSAlexandre Belloni 
998a556c76aSAlexandre Belloni 	/* Issue a read command */
999a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1000a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1001a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1002a556c76aSAlexandre Belloni 
1003a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1004a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1005a556c76aSAlexandre Belloni 
1006a556c76aSAlexandre Belloni 	/* Read the entry flags */
1007a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1008a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1009a556c76aSAlexandre Belloni 		return -EINVAL;
1010a556c76aSAlexandre Belloni 
1011a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1012a556c76aSAlexandre Belloni 	 * do not report it.
1013a556c76aSAlexandre Belloni 	 */
1014a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1015531ee1a6SVladimir Oltean 	if (dst != port)
1016a556c76aSAlexandre Belloni 		return -EINVAL;
1017a556c76aSAlexandre Belloni 
1018a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1019a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1020a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1021a556c76aSAlexandre Belloni 
1022a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1023a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1024a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1025a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1026a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1027a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1028a556c76aSAlexandre Belloni 
1029a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1030a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1031a556c76aSAlexandre Belloni 
1032a556c76aSAlexandre Belloni 	return 0;
1033a556c76aSAlexandre Belloni }
1034a556c76aSAlexandre Belloni 
10355e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1036531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1037a556c76aSAlexandre Belloni {
1038531ee1a6SVladimir Oltean 	int i, j;
1039a556c76aSAlexandre Belloni 
104021ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
104121ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1042a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1043531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1044531ee1a6SVladimir Oltean 			bool is_static;
1045531ee1a6SVladimir Oltean 			int ret;
1046531ee1a6SVladimir Oltean 
1047531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
1048a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1049a556c76aSAlexandre Belloni 			 * skip it.
1050a556c76aSAlexandre Belloni 			 */
1051a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
1052a556c76aSAlexandre Belloni 				continue;
1053a556c76aSAlexandre Belloni 			else if (ret)
1054531ee1a6SVladimir Oltean 				return ret;
1055a556c76aSAlexandre Belloni 
1056531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1057531ee1a6SVladimir Oltean 
1058531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
1059a556c76aSAlexandre Belloni 			if (ret)
1060531ee1a6SVladimir Oltean 				return ret;
1061a556c76aSAlexandre Belloni 		}
1062a556c76aSAlexandre Belloni 	}
1063a556c76aSAlexandre Belloni 
1064531ee1a6SVladimir Oltean 	return 0;
1065531ee1a6SVladimir Oltean }
10665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1067531ee1a6SVladimir Oltean 
1068f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
10694e3b0468SAntoine Tenart {
10704e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
10714e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
10724e3b0468SAntoine Tenart }
1073f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
10744e3b0468SAntoine Tenart 
1075f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
10764e3b0468SAntoine Tenart {
1077306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10784e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
10794e3b0468SAntoine Tenart 
10804e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
10814e3b0468SAntoine Tenart 		return -EFAULT;
10824e3b0468SAntoine Tenart 
10834e3b0468SAntoine Tenart 	/* reserved for future extensions */
10844e3b0468SAntoine Tenart 	if (cfg.flags)
10854e3b0468SAntoine Tenart 		return -EINVAL;
10864e3b0468SAntoine Tenart 
10874e3b0468SAntoine Tenart 	/* Tx type sanity check */
10884e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
10894e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1090306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
10914e3b0468SAntoine Tenart 		break;
10924e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
10934e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
10944e3b0468SAntoine Tenart 		 * need to update the origin time.
10954e3b0468SAntoine Tenart 		 */
1096306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
10974e3b0468SAntoine Tenart 		break;
10984e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1099306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
11004e3b0468SAntoine Tenart 		break;
11014e3b0468SAntoine Tenart 	default:
11024e3b0468SAntoine Tenart 		return -ERANGE;
11034e3b0468SAntoine Tenart 	}
11044e3b0468SAntoine Tenart 
11054e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
11064e3b0468SAntoine Tenart 
11074e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
11084e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
11094e3b0468SAntoine Tenart 		break;
11104e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
11114e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
11124e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
11134e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
11144e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
11154e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
11164e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
11174e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
11184e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
11194e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
11204e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
11214e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
11224e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
11234e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
11244e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
11254e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
11264e3b0468SAntoine Tenart 		break;
11274e3b0468SAntoine Tenart 	default:
11284e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
11294e3b0468SAntoine Tenart 		return -ERANGE;
11304e3b0468SAntoine Tenart 	}
11314e3b0468SAntoine Tenart 
11324e3b0468SAntoine Tenart 	/* Commit back the result & save it */
11334e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
11344e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
11354e3b0468SAntoine Tenart 
11364e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
11374e3b0468SAntoine Tenart }
1138f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
11394e3b0468SAntoine Tenart 
11405e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1141a556c76aSAlexandre Belloni {
1142a556c76aSAlexandre Belloni 	int i;
1143a556c76aSAlexandre Belloni 
1144a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1145a556c76aSAlexandre Belloni 		return;
1146a556c76aSAlexandre Belloni 
1147a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1148a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1149a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1150a556c76aSAlexandre Belloni }
11515e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1152a556c76aSAlexandre Belloni 
11531e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1154a556c76aSAlexandre Belloni {
1155a556c76aSAlexandre Belloni 	int i, j;
1156a556c76aSAlexandre Belloni 
1157a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1158a556c76aSAlexandre Belloni 
1159a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1160a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1161a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1162a556c76aSAlexandre Belloni 
1163a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1164a556c76aSAlexandre Belloni 			u32 val;
1165a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1166a556c76aSAlexandre Belloni 
1167a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1168a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1169a556c76aSAlexandre Belloni 
1170a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1171a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1172a556c76aSAlexandre Belloni 
1173a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1174a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1175a556c76aSAlexandre Belloni 		}
1176a556c76aSAlexandre Belloni 	}
1177a556c76aSAlexandre Belloni 
11781e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
11791e1caa97SClaudiu Manoil }
11801e1caa97SClaudiu Manoil 
11811e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
11821e1caa97SClaudiu Manoil {
11831e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
11841e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
11851e1caa97SClaudiu Manoil 					     stats_work);
11861e1caa97SClaudiu Manoil 
11871e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
11881e1caa97SClaudiu Manoil 
1189a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1190a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1191a556c76aSAlexandre Belloni }
1192a556c76aSAlexandre Belloni 
11935e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1194a556c76aSAlexandre Belloni {
1195a556c76aSAlexandre Belloni 	int i;
1196a556c76aSAlexandre Belloni 
1197a556c76aSAlexandre Belloni 	/* check and update now */
11981e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1199a556c76aSAlexandre Belloni 
1200a556c76aSAlexandre Belloni 	/* Copy all counters */
1201a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1202004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1203a556c76aSAlexandre Belloni }
12045e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1205a556c76aSAlexandre Belloni 
12065e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1207c7282d38SVladimir Oltean {
1208a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1209a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1210c7282d38SVladimir Oltean 
1211a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1212a556c76aSAlexandre Belloni }
12135e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1214a556c76aSAlexandre Belloni 
12155e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1216c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1217c7282d38SVladimir Oltean {
12184e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
12194e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1220d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1221d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1222d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1223d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1224d2b09a8eSYangbo Lu 		return 0;
1225d2b09a8eSYangbo Lu 	}
12264e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
12274e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
12284e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
12294e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
12304e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
12314e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
12324e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
12334e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
12344e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
12354e3b0468SAntoine Tenart 
12364e3b0468SAntoine Tenart 	return 0;
12374e3b0468SAntoine Tenart }
12385e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
12394e3b0468SAntoine Tenart 
124023ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
124123ca3b72SVladimir Oltean 				bool only_active_ports)
1242b80af659SVladimir Oltean {
1243b80af659SVladimir Oltean 	u32 mask = 0;
1244b80af659SVladimir Oltean 	int port;
1245b80af659SVladimir Oltean 
1246b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1247b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1248b80af659SVladimir Oltean 
1249b80af659SVladimir Oltean 		if (!ocelot_port)
1250b80af659SVladimir Oltean 			continue;
1251b80af659SVladimir Oltean 
125223ca3b72SVladimir Oltean 		if (ocelot_port->bond == bond) {
125323ca3b72SVladimir Oltean 			if (only_active_ports && !ocelot_port->lag_tx_active)
125423ca3b72SVladimir Oltean 				continue;
125523ca3b72SVladimir Oltean 
1256b80af659SVladimir Oltean 			mask |= BIT(port);
1257b80af659SVladimir Oltean 		}
125823ca3b72SVladimir Oltean 	}
1259b80af659SVladimir Oltean 
1260b80af659SVladimir Oltean 	return mask;
1261b80af659SVladimir Oltean }
1262b80af659SVladimir Oltean 
1263df291e54SVladimir Oltean static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot,
1264df291e54SVladimir Oltean 				      struct net_device *bridge)
1265df291e54SVladimir Oltean {
1266df291e54SVladimir Oltean 	u32 mask = 0;
1267df291e54SVladimir Oltean 	int port;
1268df291e54SVladimir Oltean 
1269df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1270df291e54SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1271df291e54SVladimir Oltean 
1272df291e54SVladimir Oltean 		if (!ocelot_port)
1273df291e54SVladimir Oltean 			continue;
1274df291e54SVladimir Oltean 
1275df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1276df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1277df291e54SVladimir Oltean 			mask |= BIT(port);
1278df291e54SVladimir Oltean 	}
1279df291e54SVladimir Oltean 
1280df291e54SVladimir Oltean 	return mask;
1281df291e54SVladimir Oltean }
1282df291e54SVladimir Oltean 
1283e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
12849b521250SVladimir Oltean {
1285e21268efSVladimir Oltean 	u32 mask = 0;
12869b521250SVladimir Oltean 	int port;
12879b521250SVladimir Oltean 
1288e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1289e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1290e21268efSVladimir Oltean 
1291e21268efSVladimir Oltean 		if (!ocelot_port)
1292e21268efSVladimir Oltean 			continue;
1293e21268efSVladimir Oltean 
1294e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
1295e21268efSVladimir Oltean 			mask |= BIT(port);
1296e21268efSVladimir Oltean 	}
1297e21268efSVladimir Oltean 
1298e21268efSVladimir Oltean 	return mask;
1299e21268efSVladimir Oltean }
1300e21268efSVladimir Oltean 
1301e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1302e21268efSVladimir Oltean {
1303e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
1304e21268efSVladimir Oltean 	int port;
1305e21268efSVladimir Oltean 
1306e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
1307e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
1308e21268efSVladimir Oltean 	 * those are bridged or standalone.
1309e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
1310e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
1311e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
1312e21268efSVladimir Oltean 	 */
1313e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1314e21268efSVladimir Oltean 
13159b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
13169b521250SVladimir Oltean 	 * a source for the other ports.
13179b521250SVladimir Oltean 	 */
13189b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1319e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1320e21268efSVladimir Oltean 		unsigned long mask;
1321e21268efSVladimir Oltean 
1322e21268efSVladimir Oltean 		if (!ocelot_port) {
1323e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1324e21268efSVladimir Oltean 			mask = 0;
1325e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1326e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1327e21268efSVladimir Oltean 			 * forward packets to all other ports except for
1328e21268efSVladimir Oltean 			 * themselves
1329e21268efSVladimir Oltean 			 */
1330e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1331e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
1332df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1333df291e54SVladimir Oltean 			struct net_device *bridge = ocelot_port->bridge;
1334528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
13359b521250SVladimir Oltean 
1336df291e54SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, bridge);
1337df291e54SVladimir Oltean 			mask &= ~BIT(port);
133823ca3b72SVladimir Oltean 			if (bond) {
133923ca3b72SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond,
134023ca3b72SVladimir Oltean 							      false);
134123ca3b72SVladimir Oltean 			}
13429b521250SVladimir Oltean 		} else {
1343e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1344e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1345e21268efSVladimir Oltean 			 * module otherwise.
1346e21268efSVladimir Oltean 			 */
1347e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
1348e21268efSVladimir Oltean 		}
1349e21268efSVladimir Oltean 
1350e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
13519b521250SVladimir Oltean 	}
13529b521250SVladimir Oltean }
1353e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
13549b521250SVladimir Oltean 
13555e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1356a556c76aSAlexandre Belloni {
1357421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1358df291e54SVladimir Oltean 	u32 learn_ena = 0;
1359a556c76aSAlexandre Belloni 
1360df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1361a556c76aSAlexandre Belloni 
1362df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1363df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1364df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1365a556c76aSAlexandre Belloni 
1366df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1367df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1368a556c76aSAlexandre Belloni 
13699b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1370a556c76aSAlexandre Belloni }
13715e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1372a556c76aSAlexandre Belloni 
13735e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
13744bda1415SVladimir Oltean {
1375c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1376c0d7eccbSVladimir Oltean 
1377c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1378c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1379c0d7eccbSVladimir Oltean 	 */
1380c0d7eccbSVladimir Oltean 	if (!age_period)
1381c0d7eccbSVladimir Oltean 		age_period = 1;
1382c0d7eccbSVladimir Oltean 
1383c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1384a556c76aSAlexandre Belloni }
13855e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1386a556c76aSAlexandre Belloni 
1387a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1388a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1389a556c76aSAlexandre Belloni 						     u16 vid)
1390a556c76aSAlexandre Belloni {
1391a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1392a556c76aSAlexandre Belloni 
1393a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1394a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1395a556c76aSAlexandre Belloni 			return mc;
1396a556c76aSAlexandre Belloni 	}
1397a556c76aSAlexandre Belloni 
1398a556c76aSAlexandre Belloni 	return NULL;
1399a556c76aSAlexandre Belloni }
1400a556c76aSAlexandre Belloni 
14019403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
14029403c158SVladimir Oltean {
14039403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
14049403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
14059403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
14069403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
14077c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
14089403c158SVladimir Oltean }
14099403c158SVladimir Oltean 
1410e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1411e5d1f896SVladimir Oltean 					     unsigned long ports)
1412e5d1f896SVladimir Oltean {
1413e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1414e5d1f896SVladimir Oltean 
1415e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1416e5d1f896SVladimir Oltean 	if (!pgid)
1417e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1418e5d1f896SVladimir Oltean 
1419e5d1f896SVladimir Oltean 	pgid->ports = ports;
1420e5d1f896SVladimir Oltean 	pgid->index = index;
1421e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1422e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1423e5d1f896SVladimir Oltean 
1424e5d1f896SVladimir Oltean 	return pgid;
1425e5d1f896SVladimir Oltean }
1426e5d1f896SVladimir Oltean 
1427e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1428e5d1f896SVladimir Oltean {
1429e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1430e5d1f896SVladimir Oltean 		return;
1431e5d1f896SVladimir Oltean 
1432e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1433e5d1f896SVladimir Oltean 	kfree(pgid);
1434e5d1f896SVladimir Oltean }
1435e5d1f896SVladimir Oltean 
1436e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1437bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
14389403c158SVladimir Oltean {
1439e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1440e5d1f896SVladimir Oltean 	int index;
14419403c158SVladimir Oltean 
14429403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
14439403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
14449403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
14459403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
14469403c158SVladimir Oltean 	 */
1447bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1448bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1449e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
14509403c158SVladimir Oltean 
1451e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1452e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1453e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1454e5d1f896SVladimir Oltean 		 */
1455e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1456e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1457e5d1f896SVladimir Oltean 			return pgid;
1458e5d1f896SVladimir Oltean 		}
1459e5d1f896SVladimir Oltean 	}
1460e5d1f896SVladimir Oltean 
1461e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1462e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
14639403c158SVladimir Oltean 		bool used = false;
14649403c158SVladimir Oltean 
1465e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1466e5d1f896SVladimir Oltean 			if (pgid->index == index) {
14679403c158SVladimir Oltean 				used = true;
14689403c158SVladimir Oltean 				break;
14699403c158SVladimir Oltean 			}
14709403c158SVladimir Oltean 		}
14719403c158SVladimir Oltean 
14729403c158SVladimir Oltean 		if (!used)
1473e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
14749403c158SVladimir Oltean 	}
14759403c158SVladimir Oltean 
1476e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
14779403c158SVladimir Oltean }
14789403c158SVladimir Oltean 
14799403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1480bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
14819403c158SVladimir Oltean {
1482ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
14839403c158SVladimir Oltean 
1484bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
14859403c158SVladimir Oltean 		addr[0] = 0;
14869403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
14879403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1488bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
14899403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
14909403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
14919403c158SVladimir Oltean 	}
14929403c158SVladimir Oltean }
14939403c158SVladimir Oltean 
1494209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1495209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1496a556c76aSAlexandre Belloni {
1497a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1498004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1499e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1500a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1501a556c76aSAlexandre Belloni 
1502471beb11SVladimir Oltean 	if (port == ocelot->npi)
1503471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1504471beb11SVladimir Oltean 
1505a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1506a556c76aSAlexandre Belloni 	if (!mc) {
1507728e69aeSVladimir Oltean 		/* New entry */
1508bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1509bb8d53fdSVladimir Oltean 		if (!mc)
1510bb8d53fdSVladimir Oltean 			return -ENOMEM;
1511bb8d53fdSVladimir Oltean 
1512bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1513bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1514bb8d53fdSVladimir Oltean 		mc->vid = vid;
1515bb8d53fdSVladimir Oltean 
1516a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1517728e69aeSVladimir Oltean 	} else {
1518e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1519e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1520e5d1f896SVladimir Oltean 		 */
1521e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1522bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1523a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1524a556c76aSAlexandre Belloni 	}
1525a556c76aSAlexandre Belloni 
1526004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1527e5d1f896SVladimir Oltean 
1528e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1529e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1530e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1531e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1532e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1533e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1534e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1535e5d1f896SVladimir Oltean 	}
1536e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1537e5d1f896SVladimir Oltean 
1538bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1539a556c76aSAlexandre Belloni 
1540e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1541e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1542e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1543e5d1f896SVladimir Oltean 				 pgid->index);
1544e5d1f896SVladimir Oltean 
1545e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1546bb8d53fdSVladimir Oltean 				 mc->entry_type);
1547a556c76aSAlexandre Belloni }
1548209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1549a556c76aSAlexandre Belloni 
1550209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1551a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1552a556c76aSAlexandre Belloni {
1553a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1554004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1555e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1556a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1557a556c76aSAlexandre Belloni 
1558471beb11SVladimir Oltean 	if (port == ocelot->npi)
1559471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1560471beb11SVladimir Oltean 
1561a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1562a556c76aSAlexandre Belloni 	if (!mc)
1563a556c76aSAlexandre Belloni 		return -ENOENT;
1564a556c76aSAlexandre Belloni 
1565bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1566a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1567a556c76aSAlexandre Belloni 
1568e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1569004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1570a556c76aSAlexandre Belloni 	if (!mc->ports) {
1571a556c76aSAlexandre Belloni 		list_del(&mc->list);
1572a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1573a556c76aSAlexandre Belloni 		return 0;
1574a556c76aSAlexandre Belloni 	}
1575a556c76aSAlexandre Belloni 
1576e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1577e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1578e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1579e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1580e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1581e5d1f896SVladimir Oltean 
1582bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1583a556c76aSAlexandre Belloni 
1584e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1585e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1586e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1587e5d1f896SVladimir Oltean 				 pgid->index);
1588e5d1f896SVladimir Oltean 
1589e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1590bb8d53fdSVladimir Oltean 				 mc->entry_type);
1591a556c76aSAlexandre Belloni }
1592209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1593a556c76aSAlexandre Belloni 
1594e4bd44e8SVladimir Oltean void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1595a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1596a556c76aSAlexandre Belloni {
1597df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1598a556c76aSAlexandre Belloni 
1599df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
1600a556c76aSAlexandre Belloni 
1601e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1602a556c76aSAlexandre Belloni }
16035e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1604a556c76aSAlexandre Belloni 
1605e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1606a556c76aSAlexandre Belloni 			      struct net_device *bridge)
1607a556c76aSAlexandre Belloni {
1608df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1609c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
16102e554a7aSVladimir Oltean 
1611df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
16127142529fSAntoine Tenart 
1613c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
16142f0402feSVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1615e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1616a556c76aSAlexandre Belloni }
16175e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1618a556c76aSAlexandre Belloni 
1619dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1620dc96ee37SAlexandre Belloni {
1621528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1622dc96ee37SAlexandre Belloni 	int i, port, lag;
1623dc96ee37SAlexandre Belloni 
1624dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
162596b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1626dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1627dc96ee37SAlexandre Belloni 
162896b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1629dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1630dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1631dc96ee37SAlexandre Belloni 
1632528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1633528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1634528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1635528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1636528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1637528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1638528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1639528d3f19SVladimir Oltean 	 */
1640528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1641528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1642528d3f19SVladimir Oltean 
1643528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1644528d3f19SVladimir Oltean 			continue;
1645528d3f19SVladimir Oltean 
1646528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1647528d3f19SVladimir Oltean 	}
1648528d3f19SVladimir Oltean 
1649528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1650dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1651528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
165223ca3b72SVladimir Oltean 		int num_active_ports = 0;
1653dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1654dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1655dc96ee37SAlexandre Belloni 
1656528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1657dc96ee37SAlexandre Belloni 			continue;
1658dc96ee37SAlexandre Belloni 
165923ca3b72SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1660528d3f19SVladimir Oltean 
1661dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1662dc96ee37SAlexandre Belloni 			// Destination mask
1663dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1664dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
166523ca3b72SVladimir Oltean 			aggr_idx[num_active_ports++] = port;
1666dc96ee37SAlexandre Belloni 		}
1667dc96ee37SAlexandre Belloni 
166896b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1669dc96ee37SAlexandre Belloni 			u32 ac;
1670dc96ee37SAlexandre Belloni 
1671dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1672dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
167323ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
167423ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
167523ca3b72SVladimir Oltean 			 */
167623ca3b72SVladimir Oltean 			if (num_active_ports)
167723ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1678dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1679dc96ee37SAlexandre Belloni 		}
1680528d3f19SVladimir Oltean 
1681528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1682528d3f19SVladimir Oltean 		 * the same config again.
1683528d3f19SVladimir Oltean 		 */
1684528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1685528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1686528d3f19SVladimir Oltean 
1687528d3f19SVladimir Oltean 			if (!ocelot_port)
1688528d3f19SVladimir Oltean 				continue;
1689528d3f19SVladimir Oltean 
1690528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1691528d3f19SVladimir Oltean 				visited |= BIT(port);
1692528d3f19SVladimir Oltean 		}
1693dc96ee37SAlexandre Belloni 	}
1694dc96ee37SAlexandre Belloni }
1695dc96ee37SAlexandre Belloni 
16962527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
16972527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
16982527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
16992527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
17002527f2e8SVladimir Oltean  */
17012527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1702dc96ee37SAlexandre Belloni {
17032527f2e8SVladimir Oltean 	int port;
1704dc96ee37SAlexandre Belloni 
17052527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
17062527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
17072527f2e8SVladimir Oltean 		struct net_device *bond;
1708dc96ee37SAlexandre Belloni 
17092527f2e8SVladimir Oltean 		if (!ocelot_port)
17102527f2e8SVladimir Oltean 			continue;
1711dc96ee37SAlexandre Belloni 
17122527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
17132527f2e8SVladimir Oltean 		if (bond) {
171423ca3b72SVladimir Oltean 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
171523ca3b72SVladimir Oltean 							     false));
17162527f2e8SVladimir Oltean 
17172527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
1718dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
17192527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
17202527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
17212527f2e8SVladimir Oltean 		} else {
17222527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
17232527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
17242527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
17252527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
17262527f2e8SVladimir Oltean 		}
1727dc96ee37SAlexandre Belloni 	}
1728dc96ee37SAlexandre Belloni }
1729dc96ee37SAlexandre Belloni 
17309c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1731583cbbe3SVladimir Oltean 			 struct net_device *bond,
1732583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
1733dc96ee37SAlexandre Belloni {
1734583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
1735583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
1736583cbbe3SVladimir Oltean 
1737b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
1738dc96ee37SAlexandre Belloni 
17392527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
17409b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1741dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1742dc96ee37SAlexandre Belloni 
1743dc96ee37SAlexandre Belloni 	return 0;
1744dc96ee37SAlexandre Belloni }
17459c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1746dc96ee37SAlexandre Belloni 
17479c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1748dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1749dc96ee37SAlexandre Belloni {
1750b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
1751b80af659SVladimir Oltean 
17522527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
17539b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1754dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1755dc96ee37SAlexandre Belloni }
17569c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
17570e332c85SPetr Machata 
175823ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
175923ca3b72SVladimir Oltean {
176023ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
176123ca3b72SVladimir Oltean 
176223ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
176323ca3b72SVladimir Oltean 
176423ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
176523ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
176623ca3b72SVladimir Oltean }
176723ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
176823ca3b72SVladimir Oltean 
1769a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1770a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
17710b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
17720b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
17730b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1774a8015dedSVladimir Oltean  */
17750b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
177631350d7fSVladimir Oltean {
177731350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1778a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1779e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1780601e984fSVladimir Oltean 	int atop, atop_tot;
178131350d7fSVladimir Oltean 
17820b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
17830b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
17840b912fc9SVladimir Oltean 
1785cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
17860b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1787cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
17880b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
17890b912fc9SVladimir Oltean 	}
17900b912fc9SVladimir Oltean 
1791a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1792fa914e9cSVladimir Oltean 
1793e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1794e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1795e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1796541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1797541132f0SMaxim Kochetkov 			    pause_start);
1798541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1799541132f0SMaxim Kochetkov 			    pause_stop);
1800fa914e9cSVladimir Oltean 
1801601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1802f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1803a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1804601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1805601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1806601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1807fa914e9cSVladimir Oltean }
18080b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
18090b912fc9SVladimir Oltean 
18100b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
18110b912fc9SVladimir Oltean {
18120b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
18130b912fc9SVladimir Oltean 
18140b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
18150b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
18160b912fc9SVladimir Oltean 
1817cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
18180b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1819cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
18200b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
18210b912fc9SVladimir Oltean 	}
18220b912fc9SVladimir Oltean 
18230b912fc9SVladimir Oltean 	return max_mtu;
18240b912fc9SVladimir Oltean }
18250b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1826fa914e9cSVladimir Oltean 
1827421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
1828421741eaSVladimir Oltean 				     bool enabled)
1829421741eaSVladimir Oltean {
1830421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1831421741eaSVladimir Oltean 	u32 val = 0;
1832421741eaSVladimir Oltean 
1833421741eaSVladimir Oltean 	if (enabled)
1834421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
1835421741eaSVladimir Oltean 
1836421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
1837421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1838421741eaSVladimir Oltean 
1839421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
1840421741eaSVladimir Oltean }
1841421741eaSVladimir Oltean 
1842421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
1843421741eaSVladimir Oltean 					bool enabled)
1844421741eaSVladimir Oltean {
1845421741eaSVladimir Oltean 	u32 val = 0;
1846421741eaSVladimir Oltean 
1847421741eaSVladimir Oltean 	if (enabled)
1848421741eaSVladimir Oltean 		val = BIT(port);
1849421741eaSVladimir Oltean 
1850421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
1851421741eaSVladimir Oltean }
1852421741eaSVladimir Oltean 
1853421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
1854421741eaSVladimir Oltean 					bool enabled)
1855421741eaSVladimir Oltean {
1856421741eaSVladimir Oltean 	u32 val = 0;
1857421741eaSVladimir Oltean 
1858421741eaSVladimir Oltean 	if (enabled)
1859421741eaSVladimir Oltean 		val = BIT(port);
1860421741eaSVladimir Oltean 
1861421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
1862421741eaSVladimir Oltean }
1863421741eaSVladimir Oltean 
1864421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
1865421741eaSVladimir Oltean 					bool enabled)
1866421741eaSVladimir Oltean {
1867421741eaSVladimir Oltean 	u32 val = 0;
1868421741eaSVladimir Oltean 
1869421741eaSVladimir Oltean 	if (enabled)
1870421741eaSVladimir Oltean 		val = BIT(port);
1871421741eaSVladimir Oltean 
1872421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
1873421741eaSVladimir Oltean }
1874421741eaSVladimir Oltean 
1875421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
1876421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
1877421741eaSVladimir Oltean {
1878421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
1879421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
1880421741eaSVladimir Oltean 		return -EINVAL;
1881421741eaSVladimir Oltean 
1882421741eaSVladimir Oltean 	return 0;
1883421741eaSVladimir Oltean }
1884421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
1885421741eaSVladimir Oltean 
1886421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
1887421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
1888421741eaSVladimir Oltean {
1889421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
1890421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
1891421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
1892421741eaSVladimir Oltean 
1893421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
1894421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
1895421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
1896421741eaSVladimir Oltean 
1897421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
1898421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
1899421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
1900421741eaSVladimir Oltean 
1901421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
1902421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
1903421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
1904421741eaSVladimir Oltean }
1905421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
1906421741eaSVladimir Oltean 
19075e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1908fa914e9cSVladimir Oltean {
1909fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1910fa914e9cSVladimir Oltean 
1911b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
19126565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
191331350d7fSVladimir Oltean 
191431350d7fSVladimir Oltean 	/* Basic L2 initialization */
191531350d7fSVladimir Oltean 
19165bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
19175bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
19185bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
19195bc9d2e6SVladimir Oltean 	 */
19205bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
19215bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
19225bc9d2e6SVladimir Oltean 
19235bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
19245bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
19255bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
19265bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
19275bc9d2e6SVladimir Oltean 	mdelay(1);
19285bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
19295bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
19305bc9d2e6SVladimir Oltean 
19315bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1932a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
19335bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
19345bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1935a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
19365bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
19375bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
19385bc9d2e6SVladimir Oltean 
19395bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
19405bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
19415bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
19425bc9d2e6SVladimir Oltean 
1943e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1944541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1945e8e6e73dSVladimir Oltean 
194631350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
194731350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
194831350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
194931350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
195031350d7fSVladimir Oltean 
195131350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
195231350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
195331350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
195431350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
195531350d7fSVladimir Oltean 
1956421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
1957421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
1958421741eaSVladimir Oltean 
195931350d7fSVladimir Oltean 	/* Enable vcap lookups */
196031350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
196131350d7fSVladimir Oltean }
19625e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
196331350d7fSVladimir Oltean 
19642d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
19652d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
19662d44b097SVladimir Oltean  * NPI mode is used).
196769df578cSVladimir Oltean  */
19682d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
196921468199SVladimir Oltean {
197069df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
197169df578cSVladimir Oltean 
197269df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
197321468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
197469df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
197569df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
197669df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
197769df578cSVladimir Oltean 	 */
197821468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
197921468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
198021468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
198121468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
198221468199SVladimir Oltean 
198369df578cSVladimir Oltean 	/* Enable CPU port module */
1984886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
198569df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1986886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
1987cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
1988886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
1989cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
199021468199SVladimir Oltean 
199121468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
199221468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
199321468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
199421468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
199521468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
199621468199SVladimir Oltean }
199721468199SVladimir Oltean 
1998f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
1999f6fe01d6SVladimir Oltean {
2000f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2001f6fe01d6SVladimir Oltean 
2002f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2003f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2004f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2005f6fe01d6SVladimir Oltean 	 */
2006f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2007f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2008f6fe01d6SVladimir Oltean 
2009f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2010f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2011f6fe01d6SVladimir Oltean }
2012f6fe01d6SVladimir Oltean 
2013a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2014a556c76aSAlexandre Belloni {
2015a556c76aSAlexandre Belloni 	char queue_name[32];
201621468199SVladimir Oltean 	int i, ret;
201721468199SVladimir Oltean 	u32 port;
2018a556c76aSAlexandre Belloni 
20193a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
20203a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
20213a77b593SVladimir Oltean 		if (ret) {
20223a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
20233a77b593SVladimir Oltean 			return ret;
20243a77b593SVladimir Oltean 		}
20253a77b593SVladimir Oltean 	}
20263a77b593SVladimir Oltean 
2027a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2028a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2029a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2030a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2031a556c76aSAlexandre Belloni 		return -ENOMEM;
2032a556c76aSAlexandre Belloni 
2033a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
20344e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
20354e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
2036a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2037a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2038a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2039a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2040a556c76aSAlexandre Belloni 		return -ENOMEM;
2041a556c76aSAlexandre Belloni 
2042ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2043ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
2044ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
2045ca0b272bSVladimir Oltean 		return -ENOMEM;
2046ca0b272bSVladimir Oltean 	}
2047ca0b272bSVladimir Oltean 
20482b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2049e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
2050f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2051a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2052a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2053aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
20542d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2055a556c76aSAlexandre Belloni 
2056a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2057a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2058a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2059a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2060a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2061a556c76aSAlexandre Belloni 	}
2062a556c76aSAlexandre Belloni 
2063a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2064a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2065a556c76aSAlexandre Belloni 
2066a556c76aSAlexandre Belloni 	/* Aggregation mode */
2067a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2068a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2069a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2070f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2071f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2072f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2073f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2074a556c76aSAlexandre Belloni 
2075a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2076a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2077a556c76aSAlexandre Belloni 	 */
2078a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2079a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2080a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2081a556c76aSAlexandre Belloni 
2082a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2083a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2084a556c76aSAlexandre Belloni 
2085a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2086a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2087a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2088a556c76aSAlexandre Belloni 
2089a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2090edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2091a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2092b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2093a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2094edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2095a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2096a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2097a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2098a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2099a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2100a556c76aSAlexandre Belloni 
2101a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2102a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2103a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2104a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2105a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2106a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2107a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2108a556c76aSAlexandre Belloni 				 port);
2109a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2110a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2111a556c76aSAlexandre Belloni 	}
2112a556c76aSAlexandre Belloni 
211396b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2114a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2115a556c76aSAlexandre Belloni 
2116a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2117a556c76aSAlexandre Belloni 	}
2118ebb1bb40SHoratiu Vultur 
2119ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2120ebb1bb40SHoratiu Vultur 
2121b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2122b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2123b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2124a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2125b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2126b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2127b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2128a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2129a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2130a556c76aSAlexandre Belloni 
2131a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2132a556c76aSAlexandre Belloni 	 * registers endianness.
2133a556c76aSAlexandre Belloni 	 */
2134a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2135a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2136a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2137a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2138a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2139a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2140a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2141a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2142a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2143a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2144a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2145a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2146a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2147a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2148a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2149a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2150a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2151a556c76aSAlexandre Belloni 
21521e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2153a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2154a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
21554e3b0468SAntoine Tenart 
2156a556c76aSAlexandre Belloni 	return 0;
2157a556c76aSAlexandre Belloni }
2158a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2159a556c76aSAlexandre Belloni 
2160a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2161a556c76aSAlexandre Belloni {
2162c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2163a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2164ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2165a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2166a556c76aSAlexandre Belloni }
2167a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2168a556c76aSAlexandre Belloni 
2169e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2170e5fb512dSVladimir Oltean {
2171e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2172e5fb512dSVladimir Oltean 
2173e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2174e5fb512dSVladimir Oltean }
2175e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2176e5fb512dSVladimir Oltean 
2177a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2178