xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 2e554a7a5d8a8092ecb20c547734bb33fddd5046)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
7a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
820968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
9a556c76aSAlexandre Belloni #include "ocelot.h"
103c83654fSVladimir Oltean #include "ocelot_vcap.h"
11a556c76aSAlexandre Belloni 
12639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
13639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
14639c1b26SSteen Hegelund 
15a556c76aSAlexandre Belloni struct ocelot_mact_entry {
16a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
17a556c76aSAlexandre Belloni 	u16 vid;
18a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
19a556c76aSAlexandre Belloni };
20a556c76aSAlexandre Belloni 
21639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
22639c1b26SSteen Hegelund {
23639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
24639c1b26SSteen Hegelund }
25639c1b26SSteen Hegelund 
26a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
27a556c76aSAlexandre Belloni {
28639c1b26SSteen Hegelund 	u32 val;
29a556c76aSAlexandre Belloni 
30639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
31639c1b26SSteen Hegelund 		ocelot, val,
32639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
33639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
34639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
35a556c76aSAlexandre Belloni }
36a556c76aSAlexandre Belloni 
37a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
38a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
39a556c76aSAlexandre Belloni 			       unsigned int vid)
40a556c76aSAlexandre Belloni {
41a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
42a556c76aSAlexandre Belloni 
43a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
44a556c76aSAlexandre Belloni 	 * understood by the hardware.
45a556c76aSAlexandre Belloni 	 */
46a556c76aSAlexandre Belloni 	mach |= vid    << 16;
47a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
48a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
49a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
50a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
51a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
52a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
53a556c76aSAlexandre Belloni 
54a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
55a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
56a556c76aSAlexandre Belloni 
57a556c76aSAlexandre Belloni }
58a556c76aSAlexandre Belloni 
599c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
60a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
619c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
62a556c76aSAlexandre Belloni {
63a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
64a556c76aSAlexandre Belloni 
65a556c76aSAlexandre Belloni 	/* Issue a write command */
66a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
67a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
68a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
69a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
70a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
71a556c76aSAlexandre Belloni 
72a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
73a556c76aSAlexandre Belloni }
749c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
75a556c76aSAlexandre Belloni 
769c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
779c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
78a556c76aSAlexandre Belloni {
79a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
80a556c76aSAlexandre Belloni 
81a556c76aSAlexandre Belloni 	/* Issue a forget command */
82a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
83a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
84a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
85a556c76aSAlexandre Belloni 
86a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
87a556c76aSAlexandre Belloni }
889c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
89a556c76aSAlexandre Belloni 
90a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
91a556c76aSAlexandre Belloni {
92a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
93a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
94a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
95a556c76aSAlexandre Belloni 	 */
96a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
97a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
98a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
99a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
100a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
101a556c76aSAlexandre Belloni 
102a556c76aSAlexandre Belloni 	/* Clear the MAC table */
103a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
104a556c76aSAlexandre Belloni }
105a556c76aSAlexandre Belloni 
106f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
107b5962294SHoratiu Vultur {
108b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
109b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
110f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
11175944fdaSXiaoliang Yang 
11275944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
11375944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1142f17c050SXiaoliang Yang 
1152f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1162f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1172f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
118b5962294SHoratiu Vultur }
119b5962294SHoratiu Vultur 
120639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
121639c1b26SSteen Hegelund {
122639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
123639c1b26SSteen Hegelund }
124639c1b26SSteen Hegelund 
125a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
126a556c76aSAlexandre Belloni {
127639c1b26SSteen Hegelund 	u32 val;
128a556c76aSAlexandre Belloni 
129639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
130639c1b26SSteen Hegelund 		ocelot,
131639c1b26SSteen Hegelund 		val,
132639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
133639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
134639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
135a556c76aSAlexandre Belloni }
136a556c76aSAlexandre Belloni 
1377142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1387142529fSAntoine Tenart {
1397142529fSAntoine Tenart 	/* Select the VID to configure */
1407142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1417142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1427142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1437142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1447142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1457142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1467142529fSAntoine Tenart 
1477142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1487142529fSAntoine Tenart }
1497142529fSAntoine Tenart 
15097bb69e1SVladimir Oltean static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
15197bb69e1SVladimir Oltean 				       u16 vid)
15297bb69e1SVladimir Oltean {
15397bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
15487b0f983SVladimir Oltean 	u32 val = 0;
15597bb69e1SVladimir Oltean 
15697bb69e1SVladimir Oltean 	if (ocelot_port->vid != vid) {
15797bb69e1SVladimir Oltean 		/* Always permit deleting the native VLAN (vid = 0) */
15897bb69e1SVladimir Oltean 		if (ocelot_port->vid && vid) {
15997bb69e1SVladimir Oltean 			dev_err(ocelot->dev,
16097bb69e1SVladimir Oltean 				"Port already has a native VLAN: %d\n",
16197bb69e1SVladimir Oltean 				ocelot_port->vid);
16297bb69e1SVladimir Oltean 			return -EBUSY;
16397bb69e1SVladimir Oltean 		}
16497bb69e1SVladimir Oltean 		ocelot_port->vid = vid;
16597bb69e1SVladimir Oltean 	}
16697bb69e1SVladimir Oltean 
16797bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
1687142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
16997bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17097bb69e1SVladimir Oltean 
17187b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware && !ocelot_port->vid)
17287b0f983SVladimir Oltean 		/* If port is vlan-aware and tagged, drop untagged and priority
17387b0f983SVladimir Oltean 		 * tagged frames.
17487b0f983SVladimir Oltean 		 */
17587b0f983SVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
17687b0f983SVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
17787b0f983SVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
17887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
17987b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
18087b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
18187b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
18287b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
18387b0f983SVladimir Oltean 
18487b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
18587b0f983SVladimir Oltean 		if (ocelot_port->vid)
18687b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
18787b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
18887b0f983SVladimir Oltean 		else
18987b0f983SVladimir Oltean 			/* Tag all frames */
19087b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
19187b0f983SVladimir Oltean 	} else {
19287b0f983SVladimir Oltean 		/* Port tagging disabled. */
19387b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
19487b0f983SVladimir Oltean 	}
19587b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
19687b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
19787b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
19887b0f983SVladimir Oltean 
19997bb69e1SVladimir Oltean 	return 0;
20097bb69e1SVladimir Oltean }
20197bb69e1SVladimir Oltean 
202*2e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
203*2e554a7aSVladimir Oltean 			       bool vlan_aware, struct switchdev_trans *trans)
20487b0f983SVladimir Oltean {
20587b0f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
20687b0f983SVladimir Oltean 	u32 val;
20787b0f983SVladimir Oltean 
208*2e554a7aSVladimir Oltean 	if (switchdev_trans_ph_prepare(trans))
209*2e554a7aSVladimir Oltean 		return 0;
210*2e554a7aSVladimir Oltean 
21187b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
21287b0f983SVladimir Oltean 
21387b0f983SVladimir Oltean 	if (vlan_aware)
21487b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
21587b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
21687b0f983SVladimir Oltean 	else
21787b0f983SVladimir Oltean 		val = 0;
21887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
21987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
22087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
22187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
22287b0f983SVladimir Oltean 
22387b0f983SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid);
224*2e554a7aSVladimir Oltean 
225*2e554a7aSVladimir Oltean 	return 0;
22687b0f983SVladimir Oltean }
22787b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
22887b0f983SVladimir Oltean 
22997bb69e1SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
23097bb69e1SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
23197bb69e1SVladimir Oltean {
23297bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
23397bb69e1SVladimir Oltean 
23497bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot,
23597bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
23697bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
23797bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
23897bb69e1SVladimir Oltean 
23997bb69e1SVladimir Oltean 	ocelot_port->pvid = pvid;
2407142529fSAntoine Tenart }
2417142529fSAntoine Tenart 
2425e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2437142529fSAntoine Tenart 		    bool untagged)
2447142529fSAntoine Tenart {
2457142529fSAntoine Tenart 	int ret;
2467142529fSAntoine Tenart 
2477142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
24897bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2497142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2507142529fSAntoine Tenart 	if (ret)
2517142529fSAntoine Tenart 		return ret;
2527142529fSAntoine Tenart 
2537142529fSAntoine Tenart 	/* Default ingress vlan classification */
2547142529fSAntoine Tenart 	if (pvid)
25597bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, vid);
2567142529fSAntoine Tenart 
2577142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
25897bb69e1SVladimir Oltean 	if (untagged) {
25997bb69e1SVladimir Oltean 		ret = ocelot_port_set_native_vlan(ocelot, port, vid);
26097bb69e1SVladimir Oltean 		if (ret)
26197bb69e1SVladimir Oltean 			return ret;
262b9cd75e6SVladimir Oltean 	}
2637142529fSAntoine Tenart 
2647142529fSAntoine Tenart 	return 0;
2657142529fSAntoine Tenart }
2665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
2677142529fSAntoine Tenart 
2685e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
2699855934cSVladimir Oltean {
2709855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2719855934cSVladimir Oltean 	int ret;
2727142529fSAntoine Tenart 
2737142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
27497bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
2757142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2767142529fSAntoine Tenart 	if (ret)
2777142529fSAntoine Tenart 		return ret;
2787142529fSAntoine Tenart 
2797142529fSAntoine Tenart 	/* Ingress */
28097bb69e1SVladimir Oltean 	if (ocelot_port->pvid == vid)
28197bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, 0);
2827142529fSAntoine Tenart 
2837142529fSAntoine Tenart 	/* Egress */
28497bb69e1SVladimir Oltean 	if (ocelot_port->vid == vid)
28597bb69e1SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, 0);
2867142529fSAntoine Tenart 
2877142529fSAntoine Tenart 	return 0;
2887142529fSAntoine Tenart }
2895e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
2907142529fSAntoine Tenart 
291a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
292a556c76aSAlexandre Belloni {
2937142529fSAntoine Tenart 	u16 port, vid;
2947142529fSAntoine Tenart 
295a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
296a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
297a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
298a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
2997142529fSAntoine Tenart 
3007142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3017142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3027142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3037142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3047142529fSAntoine Tenart 	}
3057142529fSAntoine Tenart 
3067142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3077142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3087142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3097142529fSAntoine Tenart 	 */
3107142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3117142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3127142529fSAntoine Tenart 
3137142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3147142529fSAntoine Tenart 	 * default.
3157142529fSAntoine Tenart 	 */
316714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
317714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3187142529fSAntoine Tenart 
3197142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3207142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3217142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3227142529fSAntoine Tenart 	}
323a556c76aSAlexandre Belloni }
324a556c76aSAlexandre Belloni 
3255e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
32626f4dbabSVladimir Oltean 			struct phy_device *phydev)
327a556c76aSAlexandre Belloni {
32826f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3295bc9d2e6SVladimir Oltean 	int speed, mode = 0;
330a556c76aSAlexandre Belloni 
33126f4dbabSVladimir Oltean 	switch (phydev->speed) {
332a556c76aSAlexandre Belloni 	case SPEED_10:
333a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
334a556c76aSAlexandre Belloni 		break;
335a556c76aSAlexandre Belloni 	case SPEED_100:
336a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
337a556c76aSAlexandre Belloni 		break;
338a556c76aSAlexandre Belloni 	case SPEED_1000:
339a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
340a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
341a556c76aSAlexandre Belloni 		break;
342a556c76aSAlexandre Belloni 	case SPEED_2500:
343a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
344a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
345a556c76aSAlexandre Belloni 		break;
346a556c76aSAlexandre Belloni 	default:
34726f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
34826f4dbabSVladimir Oltean 			port, phydev->speed);
349a556c76aSAlexandre Belloni 		return;
350a556c76aSAlexandre Belloni 	}
351a556c76aSAlexandre Belloni 
35226f4dbabSVladimir Oltean 	phy_print_status(phydev);
353a556c76aSAlexandre Belloni 
35426f4dbabSVladimir Oltean 	if (!phydev->link)
355a556c76aSAlexandre Belloni 		return;
356a556c76aSAlexandre Belloni 
357a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
358004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
359a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
360a556c76aSAlexandre Belloni 
3611ba8f656SVladimir Oltean 	/* Disable HDX fast control */
3621ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
3631ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
3641ba8f656SVladimir Oltean 
3651ba8f656SVladimir Oltean 	/* SGMII only for now */
3661ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
3671ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
3681ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
3691ba8f656SVladimir Oltean 
3701ba8f656SVladimir Oltean 	/* Enable PCS */
3711ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
3721ba8f656SVladimir Oltean 
3731ba8f656SVladimir Oltean 	/* No aneg on SGMII */
3741ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
3751ba8f656SVladimir Oltean 
3761ba8f656SVladimir Oltean 	/* No loopback */
3771ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
378a556c76aSAlexandre Belloni 
379a556c76aSAlexandre Belloni 	/* Enable MAC module */
380004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
381a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
382a556c76aSAlexandre Belloni 
383a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
384a556c76aSAlexandre Belloni 	 * reset */
385004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
386a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
387a556c76aSAlexandre Belloni 
388a556c76aSAlexandre Belloni 	/* No PFC */
389a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
390004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
391a556c76aSAlexandre Belloni 
392a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
393886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
394886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
395a556c76aSAlexandre Belloni 
396a556c76aSAlexandre Belloni 	/* Flow control */
397a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
398a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
399a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
400a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
401a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
402004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
403004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
404a556c76aSAlexandre Belloni }
4055e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
406a556c76aSAlexandre Belloni 
4075e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
408889b8950SVladimir Oltean 			struct phy_device *phy)
409a556c76aSAlexandre Belloni {
410a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
411a556c76aSAlexandre Belloni 	 * MAC addresses.
412a556c76aSAlexandre Belloni 	 */
413a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
414a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
415004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
416004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
417889b8950SVladimir Oltean }
4185e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
419889b8950SVladimir Oltean 
4205e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
421889b8950SVladimir Oltean {
422889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
423889b8950SVladimir Oltean 
424889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
425886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
426889b8950SVladimir Oltean }
4275e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
428889b8950SVladimir Oltean 
429e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
430e2f9a8feSVladimir Oltean 				  struct sk_buff *clone)
431400928bfSYangbo Lu {
432e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
433400928bfSYangbo Lu 
4346565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
4356565243cSVladimir Oltean 
436e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
437b049da13SYangbo Lu 	/* Store timestamp ID in cb[0] of sk_buff */
438e2f9a8feSVladimir Oltean 	clone->cb[0] = ocelot_port->ts_id;
4396565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
440e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
4416565243cSVladimir Oltean 
4426565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
443400928bfSYangbo Lu }
444400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
445400928bfSYangbo Lu 
446e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
447e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
4484e3b0468SAntoine Tenart {
4494e3b0468SAntoine Tenart 	unsigned long flags;
4504e3b0468SAntoine Tenart 	u32 val;
4514e3b0468SAntoine Tenart 
4524e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
4534e3b0468SAntoine Tenart 
4544e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
4554e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
4564e3b0468SAntoine Tenart 
4574e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
4584e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
4594e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
4604e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
4614e3b0468SAntoine Tenart 
4624e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
4634e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
4644e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
4654e3b0468SAntoine Tenart 
4664e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
4674e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
4684e3b0468SAntoine Tenart 		ts->tv_sec--;
4694e3b0468SAntoine Tenart 
4704e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
4714e3b0468SAntoine Tenart }
472e23a7b3eSYangbo Lu 
473e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
474e23a7b3eSYangbo Lu {
475e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
476e23a7b3eSYangbo Lu 
477e23a7b3eSYangbo Lu 	while (budget--) {
478b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
479e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
480e23a7b3eSYangbo Lu 		struct ocelot_port *port;
481e23a7b3eSYangbo Lu 		struct timespec64 ts;
482b049da13SYangbo Lu 		unsigned long flags;
483e23a7b3eSYangbo Lu 		u32 val, id, txport;
484e23a7b3eSYangbo Lu 
485e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
486e23a7b3eSYangbo Lu 
487e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
488e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
489e23a7b3eSYangbo Lu 			break;
490e23a7b3eSYangbo Lu 
491e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
492e23a7b3eSYangbo Lu 
493e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
494e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
495e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
496e23a7b3eSYangbo Lu 
497e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
498e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
499e23a7b3eSYangbo Lu 
500b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
501b049da13SYangbo Lu 
502b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
503b049da13SYangbo Lu 			if (skb->cb[0] != id)
504e23a7b3eSYangbo Lu 				continue;
505b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
506b049da13SYangbo Lu 			skb_match = skb;
507fc62c094SYangbo Lu 			break;
508e23a7b3eSYangbo Lu 		}
509e23a7b3eSYangbo Lu 
510b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
511b049da13SYangbo Lu 
5125fd82200Slaurent brando 		/* Get the h/w timestamp */
5135fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
514e23a7b3eSYangbo Lu 
515b049da13SYangbo Lu 		if (unlikely(!skb_match))
516e23a7b3eSYangbo Lu 			continue;
517e23a7b3eSYangbo Lu 
518e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
519e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
520e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
521e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
5225fd82200Slaurent brando 
5235fd82200Slaurent brando 		/* Next ts */
5245fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
525e23a7b3eSYangbo Lu 	}
526e23a7b3eSYangbo Lu }
527e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
5284e3b0468SAntoine Tenart 
5295e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
53087b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
531a556c76aSAlexandre Belloni {
532531ee1a6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
533471beb11SVladimir Oltean 	int pgid = port;
534471beb11SVladimir Oltean 
535471beb11SVladimir Oltean 	if (port == ocelot->npi)
536471beb11SVladimir Oltean 		pgid = PGID_CPU;
537a556c76aSAlexandre Belloni 
5387142529fSAntoine Tenart 	if (!vid) {
53987b0f983SVladimir Oltean 		if (!ocelot_port->vlan_aware)
5407142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
5417142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
5427142529fSAntoine Tenart 			 * matches incoming untagged packets
5437142529fSAntoine Tenart 			 */
544531ee1a6SVladimir Oltean 			vid = ocelot_port->pvid;
5457142529fSAntoine Tenart 		else
5467142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
5477142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
5487142529fSAntoine Tenart 			 */
5497142529fSAntoine Tenart 			return -EINVAL;
5507142529fSAntoine Tenart 	}
5517142529fSAntoine Tenart 
552471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
553a556c76aSAlexandre Belloni }
5545e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
555a556c76aSAlexandre Belloni 
5565e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
557531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
558531ee1a6SVladimir Oltean {
559531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
560531ee1a6SVladimir Oltean }
5615e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
562531ee1a6SVladimir Oltean 
5639c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
564531ee1a6SVladimir Oltean 			    bool is_static, void *data)
565a556c76aSAlexandre Belloni {
566531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
567a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
568a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
569a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
570a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
571a556c76aSAlexandre Belloni 
572a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
573a556c76aSAlexandre Belloni 		goto skip;
574a556c76aSAlexandre Belloni 
575a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
576a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
577a556c76aSAlexandre Belloni 	if (!nlh)
578a556c76aSAlexandre Belloni 		return -EMSGSIZE;
579a556c76aSAlexandre Belloni 
580a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
581a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
582a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
583a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
584a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
585a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
586a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
587531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
588a556c76aSAlexandre Belloni 
589531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
590a556c76aSAlexandre Belloni 		goto nla_put_failure;
591a556c76aSAlexandre Belloni 
592531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
593a556c76aSAlexandre Belloni 		goto nla_put_failure;
594a556c76aSAlexandre Belloni 
595a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
596a556c76aSAlexandre Belloni 
597a556c76aSAlexandre Belloni skip:
598a556c76aSAlexandre Belloni 	dump->idx++;
599a556c76aSAlexandre Belloni 	return 0;
600a556c76aSAlexandre Belloni 
601a556c76aSAlexandre Belloni nla_put_failure:
602a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
603a556c76aSAlexandre Belloni 	return -EMSGSIZE;
604a556c76aSAlexandre Belloni }
6059c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
606a556c76aSAlexandre Belloni 
607531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
608a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
609a556c76aSAlexandre Belloni {
610a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
611531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
612a556c76aSAlexandre Belloni 
613a556c76aSAlexandre Belloni 	/* Set row and column to read from */
614a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
615a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
616a556c76aSAlexandre Belloni 
617a556c76aSAlexandre Belloni 	/* Issue a read command */
618a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
619a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
620a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
621a556c76aSAlexandre Belloni 
622a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
623a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
624a556c76aSAlexandre Belloni 
625a556c76aSAlexandre Belloni 	/* Read the entry flags */
626a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
627a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
628a556c76aSAlexandre Belloni 		return -EINVAL;
629a556c76aSAlexandre Belloni 
630a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
631a556c76aSAlexandre Belloni 	 * do not report it.
632a556c76aSAlexandre Belloni 	 */
633a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
634531ee1a6SVladimir Oltean 	if (dst != port)
635a556c76aSAlexandre Belloni 		return -EINVAL;
636a556c76aSAlexandre Belloni 
637a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
638a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
639a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
640a556c76aSAlexandre Belloni 
641a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
642a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
643a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
644a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
645a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
646a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
647a556c76aSAlexandre Belloni 
648a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
649a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
650a556c76aSAlexandre Belloni 
651a556c76aSAlexandre Belloni 	return 0;
652a556c76aSAlexandre Belloni }
653a556c76aSAlexandre Belloni 
6545e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
655531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
656a556c76aSAlexandre Belloni {
657531ee1a6SVladimir Oltean 	int i, j;
658a556c76aSAlexandre Belloni 
65921ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
66021ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
661a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
662531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
663531ee1a6SVladimir Oltean 			bool is_static;
664531ee1a6SVladimir Oltean 			int ret;
665531ee1a6SVladimir Oltean 
666531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
667a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
668a556c76aSAlexandre Belloni 			 * skip it.
669a556c76aSAlexandre Belloni 			 */
670a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
671a556c76aSAlexandre Belloni 				continue;
672a556c76aSAlexandre Belloni 			else if (ret)
673531ee1a6SVladimir Oltean 				return ret;
674a556c76aSAlexandre Belloni 
675531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
676531ee1a6SVladimir Oltean 
677531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
678a556c76aSAlexandre Belloni 			if (ret)
679531ee1a6SVladimir Oltean 				return ret;
680a556c76aSAlexandre Belloni 		}
681a556c76aSAlexandre Belloni 	}
682a556c76aSAlexandre Belloni 
683531ee1a6SVladimir Oltean 	return 0;
684531ee1a6SVladimir Oltean }
6855e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
686531ee1a6SVladimir Oltean 
687f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
6884e3b0468SAntoine Tenart {
6894e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
6904e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
6914e3b0468SAntoine Tenart }
692f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
6934e3b0468SAntoine Tenart 
694f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
6954e3b0468SAntoine Tenart {
696306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
6974e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
6984e3b0468SAntoine Tenart 
6994e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
7004e3b0468SAntoine Tenart 		return -EFAULT;
7014e3b0468SAntoine Tenart 
7024e3b0468SAntoine Tenart 	/* reserved for future extensions */
7034e3b0468SAntoine Tenart 	if (cfg.flags)
7044e3b0468SAntoine Tenart 		return -EINVAL;
7054e3b0468SAntoine Tenart 
7064e3b0468SAntoine Tenart 	/* Tx type sanity check */
7074e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
7084e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
709306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
7104e3b0468SAntoine Tenart 		break;
7114e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
7124e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
7134e3b0468SAntoine Tenart 		 * need to update the origin time.
7144e3b0468SAntoine Tenart 		 */
715306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
7164e3b0468SAntoine Tenart 		break;
7174e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
718306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
7194e3b0468SAntoine Tenart 		break;
7204e3b0468SAntoine Tenart 	default:
7214e3b0468SAntoine Tenart 		return -ERANGE;
7224e3b0468SAntoine Tenart 	}
7234e3b0468SAntoine Tenart 
7244e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
7254e3b0468SAntoine Tenart 
7264e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
7274e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
7284e3b0468SAntoine Tenart 		break;
7294e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
7304e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
7314e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
7324e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
7334e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
7344e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
7354e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
7364e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
7374e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
7384e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
7394e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
7404e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
7414e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
7424e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
7434e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
7444e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
7454e3b0468SAntoine Tenart 		break;
7464e3b0468SAntoine Tenart 	default:
7474e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
7484e3b0468SAntoine Tenart 		return -ERANGE;
7494e3b0468SAntoine Tenart 	}
7504e3b0468SAntoine Tenart 
7514e3b0468SAntoine Tenart 	/* Commit back the result & save it */
7524e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
7534e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
7544e3b0468SAntoine Tenart 
7554e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
7564e3b0468SAntoine Tenart }
757f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
7584e3b0468SAntoine Tenart 
7595e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
760a556c76aSAlexandre Belloni {
761a556c76aSAlexandre Belloni 	int i;
762a556c76aSAlexandre Belloni 
763a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
764a556c76aSAlexandre Belloni 		return;
765a556c76aSAlexandre Belloni 
766a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
767a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
768a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
769a556c76aSAlexandre Belloni }
7705e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
771a556c76aSAlexandre Belloni 
7721e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
773a556c76aSAlexandre Belloni {
774a556c76aSAlexandre Belloni 	int i, j;
775a556c76aSAlexandre Belloni 
776a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
777a556c76aSAlexandre Belloni 
778a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
779a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
780a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
781a556c76aSAlexandre Belloni 
782a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
783a556c76aSAlexandre Belloni 			u32 val;
784a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
785a556c76aSAlexandre Belloni 
786a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
787a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
788a556c76aSAlexandre Belloni 
789a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
790a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
791a556c76aSAlexandre Belloni 
792a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
793a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
794a556c76aSAlexandre Belloni 		}
795a556c76aSAlexandre Belloni 	}
796a556c76aSAlexandre Belloni 
7971e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
7981e1caa97SClaudiu Manoil }
7991e1caa97SClaudiu Manoil 
8001e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
8011e1caa97SClaudiu Manoil {
8021e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
8031e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
8041e1caa97SClaudiu Manoil 					     stats_work);
8051e1caa97SClaudiu Manoil 
8061e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
8071e1caa97SClaudiu Manoil 
808a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
809a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
810a556c76aSAlexandre Belloni }
811a556c76aSAlexandre Belloni 
8125e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
813a556c76aSAlexandre Belloni {
814a556c76aSAlexandre Belloni 	int i;
815a556c76aSAlexandre Belloni 
816a556c76aSAlexandre Belloni 	/* check and update now */
8171e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
818a556c76aSAlexandre Belloni 
819a556c76aSAlexandre Belloni 	/* Copy all counters */
820a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
821004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
822a556c76aSAlexandre Belloni }
8235e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
824a556c76aSAlexandre Belloni 
8255e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
826c7282d38SVladimir Oltean {
827a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
828a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
829c7282d38SVladimir Oltean 
830a556c76aSAlexandre Belloni 	return ocelot->num_stats;
831a556c76aSAlexandre Belloni }
8325e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
833a556c76aSAlexandre Belloni 
8345e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
835c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
836c7282d38SVladimir Oltean {
8374e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
8384e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
839d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
840d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
841d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
842d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
843d2b09a8eSYangbo Lu 		return 0;
844d2b09a8eSYangbo Lu 	}
8454e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
8464e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
8474e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
8484e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
8494e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
8504e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
8514e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
8524e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
8534e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
8544e3b0468SAntoine Tenart 
8554e3b0468SAntoine Tenart 	return 0;
8564e3b0468SAntoine Tenart }
8575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
8584e3b0468SAntoine Tenart 
8595e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
860a556c76aSAlexandre Belloni {
861a556c76aSAlexandre Belloni 	u32 port_cfg;
8624bda1415SVladimir Oltean 	int p, i;
863a556c76aSAlexandre Belloni 
8644bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
8654bda1415SVladimir Oltean 		return;
866a556c76aSAlexandre Belloni 
8674bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
868a556c76aSAlexandre Belloni 
869a556c76aSAlexandre Belloni 	switch (state) {
870a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
8714bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
872df561f66SGustavo A. R. Silva 		fallthrough;
873a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
874a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
875a556c76aSAlexandre Belloni 		break;
876a556c76aSAlexandre Belloni 
877a556c76aSAlexandre Belloni 	default:
878a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
8794bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
880a556c76aSAlexandre Belloni 		break;
881a556c76aSAlexandre Belloni 	}
882a556c76aSAlexandre Belloni 
8834bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
884a556c76aSAlexandre Belloni 
885a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
886a556c76aSAlexandre Belloni 	 * a source for the other ports.
887a556c76aSAlexandre Belloni 	 */
8884bda1415SVladimir Oltean 	for (p = 0; p < ocelot->num_phys_ports; p++) {
88969df578cSVladimir Oltean 		if (ocelot->bridge_fwd_mask & BIT(p)) {
8904bda1415SVladimir Oltean 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
891a556c76aSAlexandre Belloni 
892a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
893a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
894a556c76aSAlexandre Belloni 
895a556c76aSAlexandre Belloni 				if (!bond_mask)
896a556c76aSAlexandre Belloni 					continue;
897a556c76aSAlexandre Belloni 
8984bda1415SVladimir Oltean 				if (bond_mask & BIT(p)) {
899a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
900a556c76aSAlexandre Belloni 					break;
901a556c76aSAlexandre Belloni 				}
902a556c76aSAlexandre Belloni 			}
903a556c76aSAlexandre Belloni 
904c9d2203bSVladimir Oltean 			ocelot_write_rix(ocelot, mask,
9054bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
906a556c76aSAlexandre Belloni 		} else {
90769df578cSVladimir Oltean 			ocelot_write_rix(ocelot, 0,
9084bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
9094bda1415SVladimir Oltean 		}
910a556c76aSAlexandre Belloni 	}
911a556c76aSAlexandre Belloni }
9125e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
913a556c76aSAlexandre Belloni 
9145e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
9154bda1415SVladimir Oltean {
916c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
917c0d7eccbSVladimir Oltean 
918c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
919c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
920c0d7eccbSVladimir Oltean 	 */
921c0d7eccbSVladimir Oltean 	if (!age_period)
922c0d7eccbSVladimir Oltean 		age_period = 1;
923c0d7eccbSVladimir Oltean 
924c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
925a556c76aSAlexandre Belloni }
9265e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
927a556c76aSAlexandre Belloni 
928a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
929a556c76aSAlexandre Belloni 						     const unsigned char *addr,
930a556c76aSAlexandre Belloni 						     u16 vid)
931a556c76aSAlexandre Belloni {
932a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
933a556c76aSAlexandre Belloni 
934a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
935a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
936a556c76aSAlexandre Belloni 			return mc;
937a556c76aSAlexandre Belloni 	}
938a556c76aSAlexandre Belloni 
939a556c76aSAlexandre Belloni 	return NULL;
940a556c76aSAlexandre Belloni }
941a556c76aSAlexandre Belloni 
9429403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
9439403c158SVladimir Oltean {
9449403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
9459403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
9469403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
9479403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
9489403c158SVladimir Oltean 	return ENTRYTYPE_NORMAL;
9499403c158SVladimir Oltean }
9509403c158SVladimir Oltean 
9519403c158SVladimir Oltean static int ocelot_mdb_get_pgid(struct ocelot *ocelot,
9529403c158SVladimir Oltean 			       enum macaccess_entry_type entry_type)
9539403c158SVladimir Oltean {
9549403c158SVladimir Oltean 	int pgid;
9559403c158SVladimir Oltean 
9569403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
9579403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
9589403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
9599403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
9609403c158SVladimir Oltean 	 */
9619403c158SVladimir Oltean 	if (entry_type == ENTRYTYPE_MACv4 ||
9629403c158SVladimir Oltean 	    entry_type == ENTRYTYPE_MACv6)
9639403c158SVladimir Oltean 		return 0;
9649403c158SVladimir Oltean 
9659403c158SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, pgid) {
9669403c158SVladimir Oltean 		struct ocelot_multicast *mc;
9679403c158SVladimir Oltean 		bool used = false;
9689403c158SVladimir Oltean 
9699403c158SVladimir Oltean 		list_for_each_entry(mc, &ocelot->multicast, list) {
9709403c158SVladimir Oltean 			if (mc->pgid == pgid) {
9719403c158SVladimir Oltean 				used = true;
9729403c158SVladimir Oltean 				break;
9739403c158SVladimir Oltean 			}
9749403c158SVladimir Oltean 		}
9759403c158SVladimir Oltean 
9769403c158SVladimir Oltean 		if (!used)
9779403c158SVladimir Oltean 			return pgid;
9789403c158SVladimir Oltean 	}
9799403c158SVladimir Oltean 
9809403c158SVladimir Oltean 	return -1;
9819403c158SVladimir Oltean }
9829403c158SVladimir Oltean 
9839403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
9849403c158SVladimir Oltean 				       struct ocelot_multicast *mc,
9859403c158SVladimir Oltean 				       enum macaccess_entry_type entry_type)
9869403c158SVladimir Oltean {
9879403c158SVladimir Oltean 	memcpy(addr, mc->addr, ETH_ALEN);
9889403c158SVladimir Oltean 
9899403c158SVladimir Oltean 	if (entry_type == ENTRYTYPE_MACv4) {
9909403c158SVladimir Oltean 		addr[0] = 0;
9919403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
9929403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
9939403c158SVladimir Oltean 	} else if (entry_type == ENTRYTYPE_MACv6) {
9949403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
9959403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
9969403c158SVladimir Oltean 	}
9979403c158SVladimir Oltean }
9989403c158SVladimir Oltean 
999209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1000209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1001a556c76aSAlexandre Belloni {
1002209edf95SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10039403c158SVladimir Oltean 	enum macaccess_entry_type entry_type;
1004a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1005004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1006a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1007a556c76aSAlexandre Belloni 	bool new = false;
1008a556c76aSAlexandre Belloni 
1009471beb11SVladimir Oltean 	if (port == ocelot->npi)
1010471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1011471beb11SVladimir Oltean 
1012a556c76aSAlexandre Belloni 	if (!vid)
1013004d44f6SVladimir Oltean 		vid = ocelot_port->pvid;
1014a556c76aSAlexandre Belloni 
10159403c158SVladimir Oltean 	entry_type = ocelot_classify_mdb(mdb->addr);
10169403c158SVladimir Oltean 
1017a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1018a556c76aSAlexandre Belloni 	if (!mc) {
10199403c158SVladimir Oltean 		int pgid = ocelot_mdb_get_pgid(ocelot, entry_type);
10209403c158SVladimir Oltean 
10219403c158SVladimir Oltean 		if (pgid < 0) {
10229403c158SVladimir Oltean 			dev_err(ocelot->dev,
10239403c158SVladimir Oltean 				"No more PGIDs available for mdb %pM vid %d\n",
10249403c158SVladimir Oltean 				mdb->addr, vid);
10259403c158SVladimir Oltean 			return -ENOSPC;
10269403c158SVladimir Oltean 		}
10279403c158SVladimir Oltean 
1028a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1029a556c76aSAlexandre Belloni 		if (!mc)
1030a556c76aSAlexandre Belloni 			return -ENOMEM;
1031a556c76aSAlexandre Belloni 
1032a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1033a556c76aSAlexandre Belloni 		mc->vid = vid;
10349403c158SVladimir Oltean 		mc->pgid = pgid;
1035a556c76aSAlexandre Belloni 
1036a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1037a556c76aSAlexandre Belloni 		new = true;
1038a556c76aSAlexandre Belloni 	}
1039a556c76aSAlexandre Belloni 
1040a556c76aSAlexandre Belloni 	if (!new) {
10419403c158SVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc, entry_type);
1042a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1043a556c76aSAlexandre Belloni 	}
1044a556c76aSAlexandre Belloni 
1045004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
10469403c158SVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc, entry_type);
1047a556c76aSAlexandre Belloni 
10489403c158SVladimir Oltean 	return ocelot_mact_learn(ocelot, mc->pgid, addr, vid, entry_type);
1049a556c76aSAlexandre Belloni }
1050209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1051a556c76aSAlexandre Belloni 
1052209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1053a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1054a556c76aSAlexandre Belloni {
1055209edf95SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10569403c158SVladimir Oltean 	enum macaccess_entry_type entry_type;
1057a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1058004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1059a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1060a556c76aSAlexandre Belloni 
1061471beb11SVladimir Oltean 	if (port == ocelot->npi)
1062471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1063471beb11SVladimir Oltean 
1064a556c76aSAlexandre Belloni 	if (!vid)
1065004d44f6SVladimir Oltean 		vid = ocelot_port->pvid;
1066a556c76aSAlexandre Belloni 
1067a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1068a556c76aSAlexandre Belloni 	if (!mc)
1069a556c76aSAlexandre Belloni 		return -ENOENT;
1070a556c76aSAlexandre Belloni 
10719403c158SVladimir Oltean 	entry_type = ocelot_classify_mdb(mdb->addr);
10729403c158SVladimir Oltean 
10739403c158SVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc, entry_type);
1074a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1075a556c76aSAlexandre Belloni 
1076004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1077a556c76aSAlexandre Belloni 	if (!mc->ports) {
1078a556c76aSAlexandre Belloni 		list_del(&mc->list);
1079a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1080a556c76aSAlexandre Belloni 		return 0;
1081a556c76aSAlexandre Belloni 	}
1082a556c76aSAlexandre Belloni 
10839403c158SVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc, entry_type);
1084a556c76aSAlexandre Belloni 
10859403c158SVladimir Oltean 	return ocelot_mact_learn(ocelot, mc->pgid, addr, vid, entry_type);
1086a556c76aSAlexandre Belloni }
1087209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1088a556c76aSAlexandre Belloni 
10895e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1090a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1091a556c76aSAlexandre Belloni {
1092a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1093a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1094a556c76aSAlexandre Belloni 	} else {
1095a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1096a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1097a556c76aSAlexandre Belloni 			 * unsupported */
1098a556c76aSAlexandre Belloni 			return -ENODEV;
1099a556c76aSAlexandre Belloni 	}
1100a556c76aSAlexandre Belloni 
1101f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1102a556c76aSAlexandre Belloni 
1103a556c76aSAlexandre Belloni 	return 0;
1104a556c76aSAlexandre Belloni }
11055e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1106a556c76aSAlexandre Belloni 
11075e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1108a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1109a556c76aSAlexandre Belloni {
1110*2e554a7aSVladimir Oltean 	struct switchdev_trans trans;
1111*2e554a7aSVladimir Oltean 	int ret;
1112*2e554a7aSVladimir Oltean 
111397bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1114a556c76aSAlexandre Belloni 
1115a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1116a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
11177142529fSAntoine Tenart 
1118*2e554a7aSVladimir Oltean 	trans.ph_prepare = true;
1119*2e554a7aSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
1120*2e554a7aSVladimir Oltean 	if (ret)
1121*2e554a7aSVladimir Oltean 		return ret;
1122*2e554a7aSVladimir Oltean 
1123*2e554a7aSVladimir Oltean 	trans.ph_prepare = false;
1124*2e554a7aSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
1125*2e554a7aSVladimir Oltean 	if (ret)
1126*2e554a7aSVladimir Oltean 		return ret;
1127*2e554a7aSVladimir Oltean 
112897bb69e1SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, 0);
112997bb69e1SVladimir Oltean 	return ocelot_port_set_native_vlan(ocelot, port, 0);
1130a556c76aSAlexandre Belloni }
11315e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1132a556c76aSAlexandre Belloni 
1133dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1134dc96ee37SAlexandre Belloni {
1135dc96ee37SAlexandre Belloni 	int i, port, lag;
1136dc96ee37SAlexandre Belloni 
1137dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
113896b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1139dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1140dc96ee37SAlexandre Belloni 
114196b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1142dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1143dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1144dc96ee37SAlexandre Belloni 
1145dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1146dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1147dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1148dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1149dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1150dc96ee37SAlexandre Belloni 
1151dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1152dc96ee37SAlexandre Belloni 		if (!bond_mask)
1153dc96ee37SAlexandre Belloni 			continue;
1154dc96ee37SAlexandre Belloni 
1155dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1156dc96ee37SAlexandre Belloni 			// Destination mask
1157dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1158dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1159dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1160dc96ee37SAlexandre Belloni 			aggr_count++;
1161dc96ee37SAlexandre Belloni 		}
1162dc96ee37SAlexandre Belloni 
116396b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1164dc96ee37SAlexandre Belloni 			u32 ac;
1165dc96ee37SAlexandre Belloni 
1166dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1167dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1168dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1169dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1170dc96ee37SAlexandre Belloni 		}
1171dc96ee37SAlexandre Belloni 	}
1172dc96ee37SAlexandre Belloni }
1173dc96ee37SAlexandre Belloni 
1174dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1175dc96ee37SAlexandre Belloni {
1176dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1177dc96ee37SAlexandre Belloni 	unsigned int p;
1178dc96ee37SAlexandre Belloni 
1179dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1180dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1181dc96ee37SAlexandre Belloni 
1182dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1183dc96ee37SAlexandre Belloni 
1184dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1185dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1186dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1187dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1188dc96ee37SAlexandre Belloni 	}
1189dc96ee37SAlexandre Belloni }
1190dc96ee37SAlexandre Belloni 
11919c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1192dc96ee37SAlexandre Belloni 			 struct net_device *bond)
1193dc96ee37SAlexandre Belloni {
1194dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1195dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1196f270dbfaSVladimir Oltean 	int lag, lp;
1197dc96ee37SAlexandre Belloni 
1198dc96ee37SAlexandre Belloni 	rcu_read_lock();
1199dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1200004d44f6SVladimir Oltean 		struct ocelot_port_private *priv = netdev_priv(ndev);
1201dc96ee37SAlexandre Belloni 
1202004d44f6SVladimir Oltean 		bond_mask |= BIT(priv->chip_port);
1203dc96ee37SAlexandre Belloni 	}
1204dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1205dc96ee37SAlexandre Belloni 
1206dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1207dc96ee37SAlexandre Belloni 
1208dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1209dc96ee37SAlexandre Belloni 	 * now on
1210dc96ee37SAlexandre Belloni 	 */
1211f270dbfaSVladimir Oltean 	if (port == lp) {
1212f270dbfaSVladimir Oltean 		lag = port;
1213f270dbfaSVladimir Oltean 		ocelot->lags[port] = bond_mask;
1214f270dbfaSVladimir Oltean 		bond_mask &= ~BIT(port);
1215dc96ee37SAlexandre Belloni 		if (bond_mask) {
1216dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1217dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1218dc96ee37SAlexandre Belloni 		}
1219dc96ee37SAlexandre Belloni 	} else {
1220dc96ee37SAlexandre Belloni 		lag = lp;
1221f270dbfaSVladimir Oltean 		ocelot->lags[lp] |= BIT(port);
1222dc96ee37SAlexandre Belloni 	}
1223dc96ee37SAlexandre Belloni 
1224dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1225dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1226dc96ee37SAlexandre Belloni 
1227dc96ee37SAlexandre Belloni 	return 0;
1228dc96ee37SAlexandre Belloni }
12299c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1230dc96ee37SAlexandre Belloni 
12319c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1232dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1233dc96ee37SAlexandre Belloni {
1234dc96ee37SAlexandre Belloni 	u32 port_cfg;
1235dc96ee37SAlexandre Belloni 	int i;
1236dc96ee37SAlexandre Belloni 
1237dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1238dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1239f270dbfaSVladimir Oltean 		ocelot->lags[i] &= ~BIT(port);
1240dc96ee37SAlexandre Belloni 
1241dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1242dc96ee37SAlexandre Belloni 	 * next port
1243dc96ee37SAlexandre Belloni 	 */
1244f270dbfaSVladimir Oltean 	if (ocelot->lags[port]) {
1245f270dbfaSVladimir Oltean 		int n = __ffs(ocelot->lags[port]);
1246dc96ee37SAlexandre Belloni 
1247f270dbfaSVladimir Oltean 		ocelot->lags[n] = ocelot->lags[port];
1248f270dbfaSVladimir Oltean 		ocelot->lags[port] = 0;
1249dc96ee37SAlexandre Belloni 
1250dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1251dc96ee37SAlexandre Belloni 	}
1252dc96ee37SAlexandre Belloni 
1253f270dbfaSVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1254dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1255f270dbfaSVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1256f270dbfaSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
1257dc96ee37SAlexandre Belloni 
1258dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1259dc96ee37SAlexandre Belloni }
12609c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
12610e332c85SPetr Machata 
1262a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1263a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
12640b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
12650b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
12660b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1267a8015dedSVladimir Oltean  */
12680b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
126931350d7fSVladimir Oltean {
127031350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1271a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1272e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
12735bc9d2e6SVladimir Oltean 	int atop_wm;
127431350d7fSVladimir Oltean 
12750b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
12760b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
12770b912fc9SVladimir Oltean 
12780b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
12790b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
12800b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
12810b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
12820b912fc9SVladimir Oltean 	}
12830b912fc9SVladimir Oltean 
1284a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1285fa914e9cSVladimir Oltean 
1286e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1287e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1288e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1289541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1290541132f0SMaxim Kochetkov 			    pause_start);
1291541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1292541132f0SMaxim Kochetkov 			    pause_stop);
1293fa914e9cSVladimir Oltean 
1294fa914e9cSVladimir Oltean 	/* Tail dropping watermark */
1295a8015dedSVladimir Oltean 	atop_wm = (ocelot->shared_queue_sz - 9 * maxlen) /
1296a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1297aa92d836SMaxim Kochetkov 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(9 * maxlen),
1298fa914e9cSVladimir Oltean 			 SYS_ATOP, port);
1299aa92d836SMaxim Kochetkov 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
1300fa914e9cSVladimir Oltean }
13010b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
13020b912fc9SVladimir Oltean 
13030b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
13040b912fc9SVladimir Oltean {
13050b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
13060b912fc9SVladimir Oltean 
13070b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
13080b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
13090b912fc9SVladimir Oltean 
13100b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13110b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
13120b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
13130b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
13140b912fc9SVladimir Oltean 	}
13150b912fc9SVladimir Oltean 
13160b912fc9SVladimir Oltean 	return max_mtu;
13170b912fc9SVladimir Oltean }
13180b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1319fa914e9cSVladimir Oltean 
13205e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1321fa914e9cSVladimir Oltean {
1322fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1323fa914e9cSVladimir Oltean 
1324b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
13256565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
132631350d7fSVladimir Oltean 
132731350d7fSVladimir Oltean 	/* Basic L2 initialization */
132831350d7fSVladimir Oltean 
13295bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
13305bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
13315bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
13325bc9d2e6SVladimir Oltean 	 */
13335bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
13345bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
13355bc9d2e6SVladimir Oltean 
13365bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
13375bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
13385bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
13395bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
13405bc9d2e6SVladimir Oltean 	mdelay(1);
13415bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
13425bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
13435bc9d2e6SVladimir Oltean 
13445bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1345a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
13465bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
13475bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1348a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
13495bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
13505bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
13515bc9d2e6SVladimir Oltean 
13525bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
13535bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
13545bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
13555bc9d2e6SVladimir Oltean 
1356e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1357541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1358e8e6e73dSVladimir Oltean 
135931350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
136031350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
136131350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
136231350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
136331350d7fSVladimir Oltean 
136431350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
136531350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
136631350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
136731350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
136831350d7fSVladimir Oltean 
136931350d7fSVladimir Oltean 	/* Enable vcap lookups */
137031350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
137131350d7fSVladimir Oltean }
13725e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
137331350d7fSVladimir Oltean 
13742d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
13752d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
13762d44b097SVladimir Oltean  * NPI mode is used).
137769df578cSVladimir Oltean  */
13782d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
137921468199SVladimir Oltean {
138069df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
138169df578cSVladimir Oltean 
138269df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
138321468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
138469df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
138569df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
138669df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
138769df578cSVladimir Oltean 	 */
138821468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
138921468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
139021468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
139121468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
139221468199SVladimir Oltean 
139369df578cSVladimir Oltean 	/* Enable CPU port module */
1394886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
139569df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1396886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
13972d44b097SVladimir Oltean 			    ocelot->xtr_prefix);
1398886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
13992d44b097SVladimir Oltean 			    ocelot->inj_prefix);
140021468199SVladimir Oltean 
140121468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
140221468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
140321468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
140421468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
140521468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
140621468199SVladimir Oltean }
140721468199SVladimir Oltean 
1408a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1409a556c76aSAlexandre Belloni {
1410a556c76aSAlexandre Belloni 	char queue_name[32];
141121468199SVladimir Oltean 	int i, ret;
141221468199SVladimir Oltean 	u32 port;
1413a556c76aSAlexandre Belloni 
14143a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
14153a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
14163a77b593SVladimir Oltean 		if (ret) {
14173a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
14183a77b593SVladimir Oltean 			return ret;
14193a77b593SVladimir Oltean 		}
14203a77b593SVladimir Oltean 	}
14213a77b593SVladimir Oltean 
1422dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1423dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
1424dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
1425dc96ee37SAlexandre Belloni 		return -ENOMEM;
1426dc96ee37SAlexandre Belloni 
1427a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1428a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1429a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1430a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1431a556c76aSAlexandre Belloni 		return -ENOMEM;
1432a556c76aSAlexandre Belloni 
1433a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
14344e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
14354e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1436a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1437a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1438a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1439a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1440a556c76aSAlexandre Belloni 		return -ENOMEM;
1441a556c76aSAlexandre Belloni 
14422b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1443a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1444a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1445aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
14462d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
1447a556c76aSAlexandre Belloni 
1448a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1449a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1450a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1451a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1452a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1453a556c76aSAlexandre Belloni 	}
1454a556c76aSAlexandre Belloni 
1455a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1456a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1457a556c76aSAlexandre Belloni 
1458a556c76aSAlexandre Belloni 	/* Aggregation mode */
1459a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1460a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1461a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1462a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1463a556c76aSAlexandre Belloni 
1464a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1465a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1466a556c76aSAlexandre Belloni 	 */
1467a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1468a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1469a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1470a556c76aSAlexandre Belloni 
1471a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1472a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1473a556c76aSAlexandre Belloni 
1474a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1475a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1476a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1477a556c76aSAlexandre Belloni 
1478a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1479a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1480a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1481a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1482a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
1483a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1484a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1485a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1486a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1487a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1488a556c76aSAlexandre Belloni 
1489a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1490a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1491a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1492a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1493a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1494a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1495a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1496a556c76aSAlexandre Belloni 				 port);
1497a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1498a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1499a556c76aSAlexandre Belloni 	}
1500a556c76aSAlexandre Belloni 
1501a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
150296b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
1503a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1504a556c76aSAlexandre Belloni 
1505a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1506a556c76aSAlexandre Belloni 	}
1507a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
1508a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1509a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
1510a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1511a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1512a556c76aSAlexandre Belloni 
1513a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1514a556c76aSAlexandre Belloni 	 * registers endianness.
1515a556c76aSAlexandre Belloni 	 */
1516a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1517a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1518a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1519a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1520a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1521a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1522a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1523a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1524a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1525a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1526a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1527a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1528a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1529a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1530a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1531a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1532a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1533a556c76aSAlexandre Belloni 
15341e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1535a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1536a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
15374e3b0468SAntoine Tenart 
1538a556c76aSAlexandre Belloni 	return 0;
1539a556c76aSAlexandre Belloni }
1540a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1541a556c76aSAlexandre Belloni 
1542a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1543a556c76aSAlexandre Belloni {
1544c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
1545a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1546a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
1547a556c76aSAlexandre Belloni }
1548a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1549a556c76aSAlexandre Belloni 
1550e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
1551e5fb512dSVladimir Oltean {
1552e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1553e5fb512dSVladimir Oltean 
1554e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
1555e5fb512dSVladimir Oltean }
1556e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
1557e5fb512dSVladimir Oltean 
1558a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1559