xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 471beb11c4ecdefd1d8502861c5e151fd642dc6e)
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>
8a556c76aSAlexandre Belloni #include "ocelot.h"
93c83654fSVladimir Oltean #include "ocelot_vcap.h"
10a556c76aSAlexandre Belloni 
11639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
12639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
13639c1b26SSteen Hegelund 
14a556c76aSAlexandre Belloni struct ocelot_mact_entry {
15a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
16a556c76aSAlexandre Belloni 	u16 vid;
17a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
18a556c76aSAlexandre Belloni };
19a556c76aSAlexandre Belloni 
20639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
21639c1b26SSteen Hegelund {
22639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
23639c1b26SSteen Hegelund }
24639c1b26SSteen Hegelund 
25a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
26a556c76aSAlexandre Belloni {
27639c1b26SSteen Hegelund 	u32 val;
28a556c76aSAlexandre Belloni 
29639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
30639c1b26SSteen Hegelund 		ocelot, val,
31639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
32639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
33639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
34a556c76aSAlexandre Belloni }
35a556c76aSAlexandre Belloni 
36a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
37a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
38a556c76aSAlexandre Belloni 			       unsigned int vid)
39a556c76aSAlexandre Belloni {
40a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
41a556c76aSAlexandre Belloni 
42a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
43a556c76aSAlexandre Belloni 	 * understood by the hardware.
44a556c76aSAlexandre Belloni 	 */
45a556c76aSAlexandre Belloni 	mach |= vid    << 16;
46a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
47a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
48a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
49a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
50a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
51a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
52a556c76aSAlexandre Belloni 
53a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
54a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
55a556c76aSAlexandre Belloni 
56a556c76aSAlexandre Belloni }
57a556c76aSAlexandre Belloni 
589c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
59a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
609c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
61a556c76aSAlexandre Belloni {
62a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
63a556c76aSAlexandre Belloni 
64a556c76aSAlexandre Belloni 	/* Issue a write command */
65a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
66a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
67a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
68a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
69a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
70a556c76aSAlexandre Belloni 
71a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
72a556c76aSAlexandre Belloni }
739c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
74a556c76aSAlexandre Belloni 
759c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
769c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
77a556c76aSAlexandre Belloni {
78a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
79a556c76aSAlexandre Belloni 
80a556c76aSAlexandre Belloni 	/* Issue a forget command */
81a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
82a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
83a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
84a556c76aSAlexandre Belloni 
85a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
86a556c76aSAlexandre Belloni }
879c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
88a556c76aSAlexandre Belloni 
89a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
90a556c76aSAlexandre Belloni {
91a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
92a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
93a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
94a556c76aSAlexandre Belloni 	 */
95a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
96a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
97a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
98a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
99a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
100a556c76aSAlexandre Belloni 
101a556c76aSAlexandre Belloni 	/* Clear the MAC table */
102a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
103a556c76aSAlexandre Belloni }
104a556c76aSAlexandre Belloni 
105f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
106b5962294SHoratiu Vultur {
107b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
108b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
109f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
110b5962294SHoratiu Vultur }
111b5962294SHoratiu Vultur 
112639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
113639c1b26SSteen Hegelund {
114639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
115639c1b26SSteen Hegelund }
116639c1b26SSteen Hegelund 
117a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
118a556c76aSAlexandre Belloni {
119639c1b26SSteen Hegelund 	u32 val;
120a556c76aSAlexandre Belloni 
121639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
122639c1b26SSteen Hegelund 		ocelot,
123639c1b26SSteen Hegelund 		val,
124639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
125639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
126639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
127a556c76aSAlexandre Belloni }
128a556c76aSAlexandre Belloni 
1297142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1307142529fSAntoine Tenart {
1317142529fSAntoine Tenart 	/* Select the VID to configure */
1327142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1337142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1347142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1357142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1367142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1377142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1387142529fSAntoine Tenart 
1397142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1407142529fSAntoine Tenart }
1417142529fSAntoine Tenart 
14297bb69e1SVladimir Oltean static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
14397bb69e1SVladimir Oltean 				       u16 vid)
14497bb69e1SVladimir Oltean {
14597bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
14687b0f983SVladimir Oltean 	u32 val = 0;
14797bb69e1SVladimir Oltean 
14897bb69e1SVladimir Oltean 	if (ocelot_port->vid != vid) {
14997bb69e1SVladimir Oltean 		/* Always permit deleting the native VLAN (vid = 0) */
15097bb69e1SVladimir Oltean 		if (ocelot_port->vid && vid) {
15197bb69e1SVladimir Oltean 			dev_err(ocelot->dev,
15297bb69e1SVladimir Oltean 				"Port already has a native VLAN: %d\n",
15397bb69e1SVladimir Oltean 				ocelot_port->vid);
15497bb69e1SVladimir Oltean 			return -EBUSY;
15597bb69e1SVladimir Oltean 		}
15697bb69e1SVladimir Oltean 		ocelot_port->vid = vid;
15797bb69e1SVladimir Oltean 	}
15897bb69e1SVladimir Oltean 
15997bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
1607142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
16197bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
16297bb69e1SVladimir Oltean 
16387b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware && !ocelot_port->vid)
16487b0f983SVladimir Oltean 		/* If port is vlan-aware and tagged, drop untagged and priority
16587b0f983SVladimir Oltean 		 * tagged frames.
16687b0f983SVladimir Oltean 		 */
16787b0f983SVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
16887b0f983SVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
16987b0f983SVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
17087b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
17187b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
17287b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
17387b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
17487b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
17587b0f983SVladimir Oltean 
17687b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
17787b0f983SVladimir Oltean 		if (ocelot_port->vid)
17887b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
17987b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
18087b0f983SVladimir Oltean 		else
18187b0f983SVladimir Oltean 			/* Tag all frames */
18287b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
18387b0f983SVladimir Oltean 	} else {
18487b0f983SVladimir Oltean 		/* Port tagging disabled. */
18587b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
18687b0f983SVladimir Oltean 	}
18787b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
18887b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
18987b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
19087b0f983SVladimir Oltean 
19197bb69e1SVladimir Oltean 	return 0;
19297bb69e1SVladimir Oltean }
19397bb69e1SVladimir Oltean 
19487b0f983SVladimir Oltean void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
19587b0f983SVladimir Oltean 				bool vlan_aware)
19687b0f983SVladimir Oltean {
19787b0f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
19887b0f983SVladimir Oltean 	u32 val;
19987b0f983SVladimir Oltean 
20087b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
20187b0f983SVladimir Oltean 
20287b0f983SVladimir Oltean 	if (vlan_aware)
20387b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
20487b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
20587b0f983SVladimir Oltean 	else
20687b0f983SVladimir Oltean 		val = 0;
20787b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
20887b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
20987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
21087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
21187b0f983SVladimir Oltean 
21287b0f983SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid);
21387b0f983SVladimir Oltean }
21487b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
21587b0f983SVladimir Oltean 
21697bb69e1SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
21797bb69e1SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
21897bb69e1SVladimir Oltean {
21997bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
22097bb69e1SVladimir Oltean 
22197bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot,
22297bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
22397bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
22497bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
22597bb69e1SVladimir Oltean 
22697bb69e1SVladimir Oltean 	ocelot_port->pvid = pvid;
2277142529fSAntoine Tenart }
2287142529fSAntoine Tenart 
2295e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2307142529fSAntoine Tenart 		    bool untagged)
2317142529fSAntoine Tenart {
2327142529fSAntoine Tenart 	int ret;
2337142529fSAntoine Tenart 
2347142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
23597bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2367142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2377142529fSAntoine Tenart 	if (ret)
2387142529fSAntoine Tenart 		return ret;
2397142529fSAntoine Tenart 
2407142529fSAntoine Tenart 	/* Default ingress vlan classification */
2417142529fSAntoine Tenart 	if (pvid)
24297bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, vid);
2437142529fSAntoine Tenart 
2447142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
24597bb69e1SVladimir Oltean 	if (untagged) {
24697bb69e1SVladimir Oltean 		ret = ocelot_port_set_native_vlan(ocelot, port, vid);
24797bb69e1SVladimir Oltean 		if (ret)
24897bb69e1SVladimir Oltean 			return ret;
249b9cd75e6SVladimir Oltean 	}
2507142529fSAntoine Tenart 
2517142529fSAntoine Tenart 	return 0;
2527142529fSAntoine Tenart }
2535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
2547142529fSAntoine Tenart 
2555e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
2569855934cSVladimir Oltean {
2579855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2589855934cSVladimir Oltean 	int ret;
2597142529fSAntoine Tenart 
2607142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
26197bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
2627142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2637142529fSAntoine Tenart 	if (ret)
2647142529fSAntoine Tenart 		return ret;
2657142529fSAntoine Tenart 
2667142529fSAntoine Tenart 	/* Ingress */
26797bb69e1SVladimir Oltean 	if (ocelot_port->pvid == vid)
26897bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, 0);
2697142529fSAntoine Tenart 
2707142529fSAntoine Tenart 	/* Egress */
27197bb69e1SVladimir Oltean 	if (ocelot_port->vid == vid)
27297bb69e1SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, 0);
2737142529fSAntoine Tenart 
2747142529fSAntoine Tenart 	return 0;
2757142529fSAntoine Tenart }
2765e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
2777142529fSAntoine Tenart 
278a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
279a556c76aSAlexandre Belloni {
2807142529fSAntoine Tenart 	u16 port, vid;
2817142529fSAntoine Tenart 
282a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
283a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
284a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
285a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
2867142529fSAntoine Tenart 
2877142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
2887142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
2897142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
2907142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2917142529fSAntoine Tenart 	}
2927142529fSAntoine Tenart 
2937142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
2947142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
2957142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
2967142529fSAntoine Tenart 	 */
2977142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
2987142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
2997142529fSAntoine Tenart 
3007142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3017142529fSAntoine Tenart 	 * default.
3027142529fSAntoine Tenart 	 */
303714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
304714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3057142529fSAntoine Tenart 
3067142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3077142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3087142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3097142529fSAntoine Tenart 	}
310a556c76aSAlexandre Belloni }
311a556c76aSAlexandre Belloni 
312a556c76aSAlexandre Belloni /* Watermark encode
313a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
314a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
315a556c76aSAlexandre Belloni  */
316a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
317a556c76aSAlexandre Belloni {
318a556c76aSAlexandre Belloni 	if (value >= BIT(8))
319a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
320a556c76aSAlexandre Belloni 
321a556c76aSAlexandre Belloni 	return value;
322a556c76aSAlexandre Belloni }
323a556c76aSAlexandre Belloni 
3245e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
32526f4dbabSVladimir Oltean 			struct phy_device *phydev)
326a556c76aSAlexandre Belloni {
32726f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3285bc9d2e6SVladimir Oltean 	int speed, mode = 0;
329a556c76aSAlexandre Belloni 
33026f4dbabSVladimir Oltean 	switch (phydev->speed) {
331a556c76aSAlexandre Belloni 	case SPEED_10:
332a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
333a556c76aSAlexandre Belloni 		break;
334a556c76aSAlexandre Belloni 	case SPEED_100:
335a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
336a556c76aSAlexandre Belloni 		break;
337a556c76aSAlexandre Belloni 	case SPEED_1000:
338a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
339a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
340a556c76aSAlexandre Belloni 		break;
341a556c76aSAlexandre Belloni 	case SPEED_2500:
342a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
343a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
344a556c76aSAlexandre Belloni 		break;
345a556c76aSAlexandre Belloni 	default:
34626f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
34726f4dbabSVladimir Oltean 			port, phydev->speed);
348a556c76aSAlexandre Belloni 		return;
349a556c76aSAlexandre Belloni 	}
350a556c76aSAlexandre Belloni 
35126f4dbabSVladimir Oltean 	phy_print_status(phydev);
352a556c76aSAlexandre Belloni 
35326f4dbabSVladimir Oltean 	if (!phydev->link)
354a556c76aSAlexandre Belloni 		return;
355a556c76aSAlexandre Belloni 
356a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
357004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
358a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
359a556c76aSAlexandre Belloni 
3601ba8f656SVladimir Oltean 	/* Disable HDX fast control */
3611ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
3621ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
3631ba8f656SVladimir Oltean 
3641ba8f656SVladimir Oltean 	/* SGMII only for now */
3651ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
3661ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
3671ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
3681ba8f656SVladimir Oltean 
3691ba8f656SVladimir Oltean 	/* Enable PCS */
3701ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
3711ba8f656SVladimir Oltean 
3721ba8f656SVladimir Oltean 	/* No aneg on SGMII */
3731ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
3741ba8f656SVladimir Oltean 
3751ba8f656SVladimir Oltean 	/* No loopback */
3761ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
377a556c76aSAlexandre Belloni 
378a556c76aSAlexandre Belloni 	/* Enable MAC module */
379004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
380a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
381a556c76aSAlexandre Belloni 
382a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
383a556c76aSAlexandre Belloni 	 * reset */
384004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
385a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
386a556c76aSAlexandre Belloni 
387a556c76aSAlexandre Belloni 	/* No PFC */
388a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
389004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
390a556c76aSAlexandre Belloni 
391a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
392a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
393a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
394a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
395004d44f6SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, port);
396a556c76aSAlexandre Belloni 
397a556c76aSAlexandre Belloni 	/* Flow control */
398a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
399a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
400a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
401a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
402a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
403004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
404004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
405a556c76aSAlexandre Belloni }
4065e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
407a556c76aSAlexandre Belloni 
4085e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
409889b8950SVladimir Oltean 			struct phy_device *phy)
410a556c76aSAlexandre Belloni {
411a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
412a556c76aSAlexandre Belloni 	 * MAC addresses.
413a556c76aSAlexandre Belloni 	 */
414a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
415a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
416004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
417004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
418889b8950SVladimir Oltean }
4195e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
420889b8950SVladimir Oltean 
4215e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
422889b8950SVladimir Oltean {
423889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
424889b8950SVladimir Oltean 
425889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
426889b8950SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
427889b8950SVladimir Oltean 		       QSYS_SWITCH_PORT_MODE, port);
428889b8950SVladimir Oltean }
4295e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
430889b8950SVladimir Oltean 
431400928bfSYangbo Lu int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
432400928bfSYangbo Lu 				 struct sk_buff *skb)
433400928bfSYangbo Lu {
434400928bfSYangbo Lu 	struct skb_shared_info *shinfo = skb_shinfo(skb);
435400928bfSYangbo Lu 	struct ocelot *ocelot = ocelot_port->ocelot;
436400928bfSYangbo Lu 
437400928bfSYangbo Lu 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
438400928bfSYangbo Lu 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
439400928bfSYangbo Lu 		shinfo->tx_flags |= SKBTX_IN_PROGRESS;
440b049da13SYangbo Lu 		/* Store timestamp ID in cb[0] of sk_buff */
441b049da13SYangbo Lu 		skb->cb[0] = ocelot_port->ts_id % 4;
442b049da13SYangbo Lu 		skb_queue_tail(&ocelot_port->tx_skbs, skb);
443400928bfSYangbo Lu 		return 0;
444400928bfSYangbo Lu 	}
445400928bfSYangbo Lu 	return -ENODATA;
446400928bfSYangbo Lu }
447400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
448400928bfSYangbo Lu 
449e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
450e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
4514e3b0468SAntoine Tenart {
4524e3b0468SAntoine Tenart 	unsigned long flags;
4534e3b0468SAntoine Tenart 	u32 val;
4544e3b0468SAntoine Tenart 
4554e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
4564e3b0468SAntoine Tenart 
4574e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
4584e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
4594e3b0468SAntoine Tenart 
4604e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
4614e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
4624e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
4634e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
4644e3b0468SAntoine Tenart 
4654e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
4664e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
4674e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
4684e3b0468SAntoine Tenart 
4694e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
4704e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
4714e3b0468SAntoine Tenart 		ts->tv_sec--;
4724e3b0468SAntoine Tenart 
4734e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
4744e3b0468SAntoine Tenart }
475e23a7b3eSYangbo Lu 
476e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
477e23a7b3eSYangbo Lu {
478e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
479e23a7b3eSYangbo Lu 
480e23a7b3eSYangbo Lu 	while (budget--) {
481b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
482e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
483e23a7b3eSYangbo Lu 		struct ocelot_port *port;
484e23a7b3eSYangbo Lu 		struct timespec64 ts;
485b049da13SYangbo Lu 		unsigned long flags;
486e23a7b3eSYangbo Lu 		u32 val, id, txport;
487e23a7b3eSYangbo Lu 
488e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
489e23a7b3eSYangbo Lu 
490e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
491e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
492e23a7b3eSYangbo Lu 			break;
493e23a7b3eSYangbo Lu 
494e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
495e23a7b3eSYangbo Lu 
496e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
497e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
498e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
499e23a7b3eSYangbo Lu 
500e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
501e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
502e23a7b3eSYangbo Lu 
503b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
504b049da13SYangbo Lu 
505b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
506b049da13SYangbo Lu 			if (skb->cb[0] != id)
507e23a7b3eSYangbo Lu 				continue;
508b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
509b049da13SYangbo Lu 			skb_match = skb;
510fc62c094SYangbo Lu 			break;
511e23a7b3eSYangbo Lu 		}
512e23a7b3eSYangbo Lu 
513b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
514b049da13SYangbo Lu 
515e23a7b3eSYangbo Lu 		/* Next ts */
516e23a7b3eSYangbo Lu 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
517e23a7b3eSYangbo Lu 
518b049da13SYangbo Lu 		if (unlikely(!skb_match))
519e23a7b3eSYangbo Lu 			continue;
520e23a7b3eSYangbo Lu 
521e23a7b3eSYangbo Lu 		/* Get the h/w timestamp */
522e23a7b3eSYangbo Lu 		ocelot_get_hwtimestamp(ocelot, &ts);
523e23a7b3eSYangbo Lu 
524e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
525e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
526e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
527b049da13SYangbo Lu 		skb_tstamp_tx(skb_match, &shhwtstamps);
528e23a7b3eSYangbo Lu 
529b049da13SYangbo Lu 		dev_kfree_skb_any(skb_match);
530e23a7b3eSYangbo Lu 	}
531e23a7b3eSYangbo Lu }
532e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
5334e3b0468SAntoine Tenart 
5345e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
53587b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
536a556c76aSAlexandre Belloni {
537531ee1a6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
538*471beb11SVladimir Oltean 	int pgid = port;
539*471beb11SVladimir Oltean 
540*471beb11SVladimir Oltean 	if (port == ocelot->npi)
541*471beb11SVladimir Oltean 		pgid = PGID_CPU;
542a556c76aSAlexandre Belloni 
5437142529fSAntoine Tenart 	if (!vid) {
54487b0f983SVladimir Oltean 		if (!ocelot_port->vlan_aware)
5457142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
5467142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
5477142529fSAntoine Tenart 			 * matches incoming untagged packets
5487142529fSAntoine Tenart 			 */
549531ee1a6SVladimir Oltean 			vid = ocelot_port->pvid;
5507142529fSAntoine Tenart 		else
5517142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
5527142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
5537142529fSAntoine Tenart 			 */
5547142529fSAntoine Tenart 			return -EINVAL;
5557142529fSAntoine Tenart 	}
5567142529fSAntoine Tenart 
557*471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
558a556c76aSAlexandre Belloni }
5595e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
560a556c76aSAlexandre Belloni 
5615e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
562531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
563531ee1a6SVladimir Oltean {
564531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
565531ee1a6SVladimir Oltean }
5665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
567531ee1a6SVladimir Oltean 
5689c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
569531ee1a6SVladimir Oltean 			    bool is_static, void *data)
570a556c76aSAlexandre Belloni {
571531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
572a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
573a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
574a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
575a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
576a556c76aSAlexandre Belloni 
577a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
578a556c76aSAlexandre Belloni 		goto skip;
579a556c76aSAlexandre Belloni 
580a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
581a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
582a556c76aSAlexandre Belloni 	if (!nlh)
583a556c76aSAlexandre Belloni 		return -EMSGSIZE;
584a556c76aSAlexandre Belloni 
585a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
586a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
587a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
588a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
589a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
590a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
591a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
592531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
593a556c76aSAlexandre Belloni 
594531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
595a556c76aSAlexandre Belloni 		goto nla_put_failure;
596a556c76aSAlexandre Belloni 
597531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
598a556c76aSAlexandre Belloni 		goto nla_put_failure;
599a556c76aSAlexandre Belloni 
600a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
601a556c76aSAlexandre Belloni 
602a556c76aSAlexandre Belloni skip:
603a556c76aSAlexandre Belloni 	dump->idx++;
604a556c76aSAlexandre Belloni 	return 0;
605a556c76aSAlexandre Belloni 
606a556c76aSAlexandre Belloni nla_put_failure:
607a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
608a556c76aSAlexandre Belloni 	return -EMSGSIZE;
609a556c76aSAlexandre Belloni }
6109c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
611a556c76aSAlexandre Belloni 
612531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
613a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
614a556c76aSAlexandre Belloni {
615a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
616531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
617a556c76aSAlexandre Belloni 
618a556c76aSAlexandre Belloni 	/* Set row and column to read from */
619a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
620a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
621a556c76aSAlexandre Belloni 
622a556c76aSAlexandre Belloni 	/* Issue a read command */
623a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
624a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
625a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
626a556c76aSAlexandre Belloni 
627a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
628a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
629a556c76aSAlexandre Belloni 
630a556c76aSAlexandre Belloni 	/* Read the entry flags */
631a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
632a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
633a556c76aSAlexandre Belloni 		return -EINVAL;
634a556c76aSAlexandre Belloni 
635a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
636a556c76aSAlexandre Belloni 	 * do not report it.
637a556c76aSAlexandre Belloni 	 */
638a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
639531ee1a6SVladimir Oltean 	if (dst != port)
640a556c76aSAlexandre Belloni 		return -EINVAL;
641a556c76aSAlexandre Belloni 
642a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
643a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
644a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
645a556c76aSAlexandre Belloni 
646a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
647a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
648a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
649a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
650a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
651a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
652a556c76aSAlexandre Belloni 
653a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
654a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
655a556c76aSAlexandre Belloni 
656a556c76aSAlexandre Belloni 	return 0;
657a556c76aSAlexandre Belloni }
658a556c76aSAlexandre Belloni 
6595e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
660531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
661a556c76aSAlexandre Belloni {
662531ee1a6SVladimir Oltean 	int i, j;
663a556c76aSAlexandre Belloni 
66421ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
66521ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
666a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
667531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
668531ee1a6SVladimir Oltean 			bool is_static;
669531ee1a6SVladimir Oltean 			int ret;
670531ee1a6SVladimir Oltean 
671531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
672a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
673a556c76aSAlexandre Belloni 			 * skip it.
674a556c76aSAlexandre Belloni 			 */
675a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
676a556c76aSAlexandre Belloni 				continue;
677a556c76aSAlexandre Belloni 			else if (ret)
678531ee1a6SVladimir Oltean 				return ret;
679a556c76aSAlexandre Belloni 
680531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
681531ee1a6SVladimir Oltean 
682531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
683a556c76aSAlexandre Belloni 			if (ret)
684531ee1a6SVladimir Oltean 				return ret;
685a556c76aSAlexandre Belloni 		}
686a556c76aSAlexandre Belloni 	}
687a556c76aSAlexandre Belloni 
688531ee1a6SVladimir Oltean 	return 0;
689531ee1a6SVladimir Oltean }
6905e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
691531ee1a6SVladimir Oltean 
692f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
6934e3b0468SAntoine Tenart {
6944e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
6954e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
6964e3b0468SAntoine Tenart }
697f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
6984e3b0468SAntoine Tenart 
699f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
7004e3b0468SAntoine Tenart {
701306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
7024e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
7034e3b0468SAntoine Tenart 
7044e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
7054e3b0468SAntoine Tenart 		return -EFAULT;
7064e3b0468SAntoine Tenart 
7074e3b0468SAntoine Tenart 	/* reserved for future extensions */
7084e3b0468SAntoine Tenart 	if (cfg.flags)
7094e3b0468SAntoine Tenart 		return -EINVAL;
7104e3b0468SAntoine Tenart 
7114e3b0468SAntoine Tenart 	/* Tx type sanity check */
7124e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
7134e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
714306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
7154e3b0468SAntoine Tenart 		break;
7164e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
7174e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
7184e3b0468SAntoine Tenart 		 * need to update the origin time.
7194e3b0468SAntoine Tenart 		 */
720306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
7214e3b0468SAntoine Tenart 		break;
7224e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
723306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
7244e3b0468SAntoine Tenart 		break;
7254e3b0468SAntoine Tenart 	default:
7264e3b0468SAntoine Tenart 		return -ERANGE;
7274e3b0468SAntoine Tenart 	}
7284e3b0468SAntoine Tenart 
7294e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
7304e3b0468SAntoine Tenart 
7314e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
7324e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
7334e3b0468SAntoine Tenart 		break;
7344e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
7354e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
7364e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
7374e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
7384e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
7394e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
7404e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
7414e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
7424e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
7434e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
7444e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
7454e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
7464e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
7474e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
7484e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
7494e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
7504e3b0468SAntoine Tenart 		break;
7514e3b0468SAntoine Tenart 	default:
7524e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
7534e3b0468SAntoine Tenart 		return -ERANGE;
7544e3b0468SAntoine Tenart 	}
7554e3b0468SAntoine Tenart 
7564e3b0468SAntoine Tenart 	/* Commit back the result & save it */
7574e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
7584e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
7594e3b0468SAntoine Tenart 
7604e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
7614e3b0468SAntoine Tenart }
762f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
7634e3b0468SAntoine Tenart 
7645e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
765a556c76aSAlexandre Belloni {
766a556c76aSAlexandre Belloni 	int i;
767a556c76aSAlexandre Belloni 
768a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
769a556c76aSAlexandre Belloni 		return;
770a556c76aSAlexandre Belloni 
771a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
772a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
773a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
774a556c76aSAlexandre Belloni }
7755e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
776a556c76aSAlexandre Belloni 
7771e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
778a556c76aSAlexandre Belloni {
779a556c76aSAlexandre Belloni 	int i, j;
780a556c76aSAlexandre Belloni 
781a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
782a556c76aSAlexandre Belloni 
783a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
784a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
785a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
786a556c76aSAlexandre Belloni 
787a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
788a556c76aSAlexandre Belloni 			u32 val;
789a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
790a556c76aSAlexandre Belloni 
791a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
792a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
793a556c76aSAlexandre Belloni 
794a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
795a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
796a556c76aSAlexandre Belloni 
797a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
798a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
799a556c76aSAlexandre Belloni 		}
800a556c76aSAlexandre Belloni 	}
801a556c76aSAlexandre Belloni 
8021e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
8031e1caa97SClaudiu Manoil }
8041e1caa97SClaudiu Manoil 
8051e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
8061e1caa97SClaudiu Manoil {
8071e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
8081e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
8091e1caa97SClaudiu Manoil 					     stats_work);
8101e1caa97SClaudiu Manoil 
8111e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
8121e1caa97SClaudiu Manoil 
813a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
814a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
815a556c76aSAlexandre Belloni }
816a556c76aSAlexandre Belloni 
8175e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
818a556c76aSAlexandre Belloni {
819a556c76aSAlexandre Belloni 	int i;
820a556c76aSAlexandre Belloni 
821a556c76aSAlexandre Belloni 	/* check and update now */
8221e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
823a556c76aSAlexandre Belloni 
824a556c76aSAlexandre Belloni 	/* Copy all counters */
825a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
826004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
827a556c76aSAlexandre Belloni }
8285e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
829a556c76aSAlexandre Belloni 
8305e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
831c7282d38SVladimir Oltean {
832a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
833a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
834c7282d38SVladimir Oltean 
835a556c76aSAlexandre Belloni 	return ocelot->num_stats;
836a556c76aSAlexandre Belloni }
8375e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
838a556c76aSAlexandre Belloni 
8395e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
840c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
841c7282d38SVladimir Oltean {
8424e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
8434e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
844d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
845d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
846d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
847d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
848d2b09a8eSYangbo Lu 		return 0;
849d2b09a8eSYangbo Lu 	}
8504e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
8514e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
8524e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
8534e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
8544e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
8554e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
8564e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
8574e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
8584e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
8594e3b0468SAntoine Tenart 
8604e3b0468SAntoine Tenart 	return 0;
8614e3b0468SAntoine Tenart }
8625e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
8634e3b0468SAntoine Tenart 
8645e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
865a556c76aSAlexandre Belloni {
866a556c76aSAlexandre Belloni 	u32 port_cfg;
8674bda1415SVladimir Oltean 	int p, i;
868a556c76aSAlexandre Belloni 
8694bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
8704bda1415SVladimir Oltean 		return;
871a556c76aSAlexandre Belloni 
8724bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
873a556c76aSAlexandre Belloni 
874a556c76aSAlexandre Belloni 	switch (state) {
875a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
8764bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
877a556c76aSAlexandre Belloni 		/* Fallthrough */
878a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
879a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
880a556c76aSAlexandre Belloni 		break;
881a556c76aSAlexandre Belloni 
882a556c76aSAlexandre Belloni 	default:
883a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
8844bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
885a556c76aSAlexandre Belloni 		break;
886a556c76aSAlexandre Belloni 	}
887a556c76aSAlexandre Belloni 
8884bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
889a556c76aSAlexandre Belloni 
890a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
891a556c76aSAlexandre Belloni 	 * a source for the other ports.
892a556c76aSAlexandre Belloni 	 */
8934bda1415SVladimir Oltean 	for (p = 0; p < ocelot->num_phys_ports; p++) {
89469df578cSVladimir Oltean 		if (ocelot->bridge_fwd_mask & BIT(p)) {
8954bda1415SVladimir Oltean 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
896a556c76aSAlexandre Belloni 
897a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
898a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
899a556c76aSAlexandre Belloni 
900a556c76aSAlexandre Belloni 				if (!bond_mask)
901a556c76aSAlexandre Belloni 					continue;
902a556c76aSAlexandre Belloni 
9034bda1415SVladimir Oltean 				if (bond_mask & BIT(p)) {
904a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
905a556c76aSAlexandre Belloni 					break;
906a556c76aSAlexandre Belloni 				}
907a556c76aSAlexandre Belloni 			}
908a556c76aSAlexandre Belloni 
909c9d2203bSVladimir Oltean 			ocelot_write_rix(ocelot, mask,
9104bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
911a556c76aSAlexandre Belloni 		} else {
91269df578cSVladimir Oltean 			ocelot_write_rix(ocelot, 0,
9134bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
9144bda1415SVladimir Oltean 		}
915a556c76aSAlexandre Belloni 	}
916a556c76aSAlexandre Belloni }
9175e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
918a556c76aSAlexandre Belloni 
9195e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
9204bda1415SVladimir Oltean {
921c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
922c0d7eccbSVladimir Oltean 
923c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
924c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
925c0d7eccbSVladimir Oltean 	 */
926c0d7eccbSVladimir Oltean 	if (!age_period)
927c0d7eccbSVladimir Oltean 		age_period = 1;
928c0d7eccbSVladimir Oltean 
929c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
930a556c76aSAlexandre Belloni }
9315e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
932a556c76aSAlexandre Belloni 
933a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
934a556c76aSAlexandre Belloni 						     const unsigned char *addr,
935a556c76aSAlexandre Belloni 						     u16 vid)
936a556c76aSAlexandre Belloni {
937a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
938a556c76aSAlexandre Belloni 
939a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
940a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
941a556c76aSAlexandre Belloni 			return mc;
942a556c76aSAlexandre Belloni 	}
943a556c76aSAlexandre Belloni 
944a556c76aSAlexandre Belloni 	return NULL;
945a556c76aSAlexandre Belloni }
946a556c76aSAlexandre Belloni 
9479c90eea3SVladimir Oltean int ocelot_port_obj_add_mdb(struct net_device *dev,
948a556c76aSAlexandre Belloni 			    const struct switchdev_obj_port_mdb *mdb,
949a556c76aSAlexandre Belloni 			    struct switchdev_trans *trans)
950a556c76aSAlexandre Belloni {
951004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
952004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
953004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
954a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
955004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
956004d44f6SVladimir Oltean 	int port = priv->chip_port;
957a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
958a556c76aSAlexandre Belloni 	bool new = false;
959a556c76aSAlexandre Belloni 
960*471beb11SVladimir Oltean 	if (port == ocelot->npi)
961*471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
962*471beb11SVladimir Oltean 
963a556c76aSAlexandre Belloni 	if (!vid)
964004d44f6SVladimir Oltean 		vid = ocelot_port->pvid;
965a556c76aSAlexandre Belloni 
966a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
967a556c76aSAlexandre Belloni 	if (!mc) {
968a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
969a556c76aSAlexandre Belloni 		if (!mc)
970a556c76aSAlexandre Belloni 			return -ENOMEM;
971a556c76aSAlexandre Belloni 
972a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
973a556c76aSAlexandre Belloni 		mc->vid = vid;
974a556c76aSAlexandre Belloni 
975a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
976a556c76aSAlexandre Belloni 		new = true;
977a556c76aSAlexandre Belloni 	}
978a556c76aSAlexandre Belloni 
979a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
980a556c76aSAlexandre Belloni 	addr[0] = 0;
981a556c76aSAlexandre Belloni 
982a556c76aSAlexandre Belloni 	if (!new) {
9830897ecf7SVladimir Oltean 		addr[1] = mc->ports >> 8;
9840897ecf7SVladimir Oltean 		addr[2] = mc->ports & 0xff;
985a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
986a556c76aSAlexandre Belloni 	}
987a556c76aSAlexandre Belloni 
988004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
9890897ecf7SVladimir Oltean 	addr[1] = mc->ports >> 8;
9900897ecf7SVladimir Oltean 	addr[2] = mc->ports & 0xff;
991a556c76aSAlexandre Belloni 
992a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
993a556c76aSAlexandre Belloni }
9949c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_obj_add_mdb);
995a556c76aSAlexandre Belloni 
9969c90eea3SVladimir Oltean int ocelot_port_obj_del_mdb(struct net_device *dev,
997a556c76aSAlexandre Belloni 			    const struct switchdev_obj_port_mdb *mdb)
998a556c76aSAlexandre Belloni {
999004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1000004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
1001004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1002a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1003004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1004004d44f6SVladimir Oltean 	int port = priv->chip_port;
1005a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1006a556c76aSAlexandre Belloni 
1007*471beb11SVladimir Oltean 	if (port == ocelot->npi)
1008*471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1009*471beb11SVladimir Oltean 
1010a556c76aSAlexandre Belloni 	if (!vid)
1011004d44f6SVladimir Oltean 		vid = ocelot_port->pvid;
1012a556c76aSAlexandre Belloni 
1013a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1014a556c76aSAlexandre Belloni 	if (!mc)
1015a556c76aSAlexandre Belloni 		return -ENOENT;
1016a556c76aSAlexandre Belloni 
1017a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1018a556c76aSAlexandre Belloni 	addr[0] = 0;
10190897ecf7SVladimir Oltean 	addr[1] = mc->ports >> 8;
10200897ecf7SVladimir Oltean 	addr[2] = mc->ports & 0xff;
1021a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1022a556c76aSAlexandre Belloni 
1023004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1024a556c76aSAlexandre Belloni 	if (!mc->ports) {
1025a556c76aSAlexandre Belloni 		list_del(&mc->list);
1026a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1027a556c76aSAlexandre Belloni 		return 0;
1028a556c76aSAlexandre Belloni 	}
1029a556c76aSAlexandre Belloni 
10300897ecf7SVladimir Oltean 	addr[1] = mc->ports >> 8;
10310897ecf7SVladimir Oltean 	addr[2] = mc->ports & 0xff;
1032a556c76aSAlexandre Belloni 
1033a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1034a556c76aSAlexandre Belloni }
10359c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_obj_del_mdb);
1036a556c76aSAlexandre Belloni 
10375e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1038a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1039a556c76aSAlexandre Belloni {
1040a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1041a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1042a556c76aSAlexandre Belloni 	} else {
1043a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1044a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1045a556c76aSAlexandre Belloni 			 * unsupported */
1046a556c76aSAlexandre Belloni 			return -ENODEV;
1047a556c76aSAlexandre Belloni 	}
1048a556c76aSAlexandre Belloni 
1049f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1050a556c76aSAlexandre Belloni 
1051a556c76aSAlexandre Belloni 	return 0;
1052a556c76aSAlexandre Belloni }
10535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1054a556c76aSAlexandre Belloni 
10555e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1056a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1057a556c76aSAlexandre Belloni {
105897bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1059a556c76aSAlexandre Belloni 
1060a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1061a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
10627142529fSAntoine Tenart 
106397bb69e1SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, 0);
106497bb69e1SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, 0);
106597bb69e1SVladimir Oltean 	return ocelot_port_set_native_vlan(ocelot, port, 0);
1066a556c76aSAlexandre Belloni }
10675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1068a556c76aSAlexandre Belloni 
1069dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1070dc96ee37SAlexandre Belloni {
1071dc96ee37SAlexandre Belloni 	int i, port, lag;
1072dc96ee37SAlexandre Belloni 
1073dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1074dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1075dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1076dc96ee37SAlexandre Belloni 
1077dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1078dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1079dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1080dc96ee37SAlexandre Belloni 
1081dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1082dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1083dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1084dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1085dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1086dc96ee37SAlexandre Belloni 
1087dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1088dc96ee37SAlexandre Belloni 		if (!bond_mask)
1089dc96ee37SAlexandre Belloni 			continue;
1090dc96ee37SAlexandre Belloni 
1091dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1092dc96ee37SAlexandre Belloni 			// Destination mask
1093dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1094dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1095dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1096dc96ee37SAlexandre Belloni 			aggr_count++;
1097dc96ee37SAlexandre Belloni 		}
1098dc96ee37SAlexandre Belloni 
1099dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1100dc96ee37SAlexandre Belloni 			u32 ac;
1101dc96ee37SAlexandre Belloni 
1102dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1103dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1104dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1105dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1106dc96ee37SAlexandre Belloni 		}
1107dc96ee37SAlexandre Belloni 	}
1108dc96ee37SAlexandre Belloni }
1109dc96ee37SAlexandre Belloni 
1110dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1111dc96ee37SAlexandre Belloni {
1112dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1113dc96ee37SAlexandre Belloni 	unsigned int p;
1114dc96ee37SAlexandre Belloni 
1115dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1116dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1117dc96ee37SAlexandre Belloni 
1118dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1119dc96ee37SAlexandre Belloni 
1120dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1121dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1122dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1123dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1124dc96ee37SAlexandre Belloni 	}
1125dc96ee37SAlexandre Belloni }
1126dc96ee37SAlexandre Belloni 
11279c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1128dc96ee37SAlexandre Belloni 			 struct net_device *bond)
1129dc96ee37SAlexandre Belloni {
1130dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1131dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1132f270dbfaSVladimir Oltean 	int lag, lp;
1133dc96ee37SAlexandre Belloni 
1134dc96ee37SAlexandre Belloni 	rcu_read_lock();
1135dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1136004d44f6SVladimir Oltean 		struct ocelot_port_private *priv = netdev_priv(ndev);
1137dc96ee37SAlexandre Belloni 
1138004d44f6SVladimir Oltean 		bond_mask |= BIT(priv->chip_port);
1139dc96ee37SAlexandre Belloni 	}
1140dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1141dc96ee37SAlexandre Belloni 
1142dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1143dc96ee37SAlexandre Belloni 
1144dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1145dc96ee37SAlexandre Belloni 	 * now on
1146dc96ee37SAlexandre Belloni 	 */
1147f270dbfaSVladimir Oltean 	if (port == lp) {
1148f270dbfaSVladimir Oltean 		lag = port;
1149f270dbfaSVladimir Oltean 		ocelot->lags[port] = bond_mask;
1150f270dbfaSVladimir Oltean 		bond_mask &= ~BIT(port);
1151dc96ee37SAlexandre Belloni 		if (bond_mask) {
1152dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1153dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1154dc96ee37SAlexandre Belloni 		}
1155dc96ee37SAlexandre Belloni 	} else {
1156dc96ee37SAlexandre Belloni 		lag = lp;
1157f270dbfaSVladimir Oltean 		ocelot->lags[lp] |= BIT(port);
1158dc96ee37SAlexandre Belloni 	}
1159dc96ee37SAlexandre Belloni 
1160dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1161dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1162dc96ee37SAlexandre Belloni 
1163dc96ee37SAlexandre Belloni 	return 0;
1164dc96ee37SAlexandre Belloni }
11659c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1166dc96ee37SAlexandre Belloni 
11679c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1168dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1169dc96ee37SAlexandre Belloni {
1170dc96ee37SAlexandre Belloni 	u32 port_cfg;
1171dc96ee37SAlexandre Belloni 	int i;
1172dc96ee37SAlexandre Belloni 
1173dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1174dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1175f270dbfaSVladimir Oltean 		ocelot->lags[i] &= ~BIT(port);
1176dc96ee37SAlexandre Belloni 
1177dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1178dc96ee37SAlexandre Belloni 	 * next port
1179dc96ee37SAlexandre Belloni 	 */
1180f270dbfaSVladimir Oltean 	if (ocelot->lags[port]) {
1181f270dbfaSVladimir Oltean 		int n = __ffs(ocelot->lags[port]);
1182dc96ee37SAlexandre Belloni 
1183f270dbfaSVladimir Oltean 		ocelot->lags[n] = ocelot->lags[port];
1184f270dbfaSVladimir Oltean 		ocelot->lags[port] = 0;
1185dc96ee37SAlexandre Belloni 
1186dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1187dc96ee37SAlexandre Belloni 	}
1188dc96ee37SAlexandre Belloni 
1189f270dbfaSVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1190dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1191f270dbfaSVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1192f270dbfaSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
1193dc96ee37SAlexandre Belloni 
1194dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1195dc96ee37SAlexandre Belloni }
11969c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
11970e332c85SPetr Machata 
1198a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1199a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
12000b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
12010b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
12020b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1203a8015dedSVladimir Oltean  */
12040b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
120531350d7fSVladimir Oltean {
120631350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1207a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
12085bc9d2e6SVladimir Oltean 	int atop_wm;
120931350d7fSVladimir Oltean 
12100b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
12110b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
12120b912fc9SVladimir Oltean 
12130b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
12140b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
12150b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
12160b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
12170b912fc9SVladimir Oltean 	}
12180b912fc9SVladimir Oltean 
1219a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1220fa914e9cSVladimir Oltean 
1221fa914e9cSVladimir Oltean 	/* Set Pause WM hysteresis
1222a8015dedSVladimir Oltean 	 * 152 = 6 * maxlen / OCELOT_BUFFER_CELL_SZ
1223a8015dedSVladimir Oltean 	 * 101 = 4 * maxlen / OCELOT_BUFFER_CELL_SZ
1224fa914e9cSVladimir Oltean 	 */
1225fa914e9cSVladimir Oltean 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
1226fa914e9cSVladimir Oltean 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
1227fa914e9cSVladimir Oltean 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, port);
1228fa914e9cSVladimir Oltean 
1229fa914e9cSVladimir Oltean 	/* Tail dropping watermark */
1230a8015dedSVladimir Oltean 	atop_wm = (ocelot->shared_queue_sz - 9 * maxlen) /
1231a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1232a8015dedSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * maxlen),
1233fa914e9cSVladimir Oltean 			 SYS_ATOP, port);
1234fa914e9cSVladimir Oltean 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
1235fa914e9cSVladimir Oltean }
12360b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
12370b912fc9SVladimir Oltean 
12380b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
12390b912fc9SVladimir Oltean {
12400b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
12410b912fc9SVladimir Oltean 
12420b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
12430b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
12440b912fc9SVladimir Oltean 
12450b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
12460b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
12470b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
12480b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
12490b912fc9SVladimir Oltean 	}
12500b912fc9SVladimir Oltean 
12510b912fc9SVladimir Oltean 	return max_mtu;
12520b912fc9SVladimir Oltean }
12530b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1254fa914e9cSVladimir Oltean 
12555e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1256fa914e9cSVladimir Oltean {
1257fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1258fa914e9cSVladimir Oltean 
1259b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
126031350d7fSVladimir Oltean 
126131350d7fSVladimir Oltean 	/* Basic L2 initialization */
126231350d7fSVladimir Oltean 
12635bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
12645bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
12655bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
12665bc9d2e6SVladimir Oltean 	 */
12675bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
12685bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
12695bc9d2e6SVladimir Oltean 
12705bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
12715bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
12725bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
12735bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
12745bc9d2e6SVladimir Oltean 	mdelay(1);
12755bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
12765bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
12775bc9d2e6SVladimir Oltean 
12785bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1279a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
12805bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
12815bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1282a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
12835bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
12845bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
12855bc9d2e6SVladimir Oltean 
12865bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
12875bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
12885bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
12895bc9d2e6SVladimir Oltean 
129031350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
129131350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
129231350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
129331350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
129431350d7fSVladimir Oltean 
129531350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
129631350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
129731350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
129831350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
129931350d7fSVladimir Oltean 
130031350d7fSVladimir Oltean 	/* Enable vcap lookups */
130131350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
130231350d7fSVladimir Oltean }
13035e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
130431350d7fSVladimir Oltean 
130569df578cSVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues.
130669df578cSVladimir Oltean  * If @npi contains a valid port index, the CPU port module is connected
130769df578cSVladimir Oltean  * to the Node Processor Interface (NPI). This is the mode through which
130869df578cSVladimir Oltean  * frames can be injected from and extracted to an external CPU,
130969df578cSVladimir Oltean  * over Ethernet.
131069df578cSVladimir Oltean  */
131169df578cSVladimir Oltean void ocelot_configure_cpu(struct ocelot *ocelot, int npi,
131221468199SVladimir Oltean 			  enum ocelot_tag_prefix injection,
131321468199SVladimir Oltean 			  enum ocelot_tag_prefix extraction)
131421468199SVladimir Oltean {
131569df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
131669df578cSVladimir Oltean 
13170b912fc9SVladimir Oltean 	ocelot->npi = npi;
13180b912fc9SVladimir Oltean 	ocelot->inj_prefix = injection;
13190b912fc9SVladimir Oltean 	ocelot->xtr_prefix = extraction;
13200b912fc9SVladimir Oltean 
132169df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
132221468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
132369df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
132469df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
132569df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
132669df578cSVladimir Oltean 	 */
132721468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
132821468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
132921468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
133021468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
133121468199SVladimir Oltean 
133269df578cSVladimir Oltean 	if (npi >= 0 && npi < ocelot->num_phys_ports) {
133321468199SVladimir Oltean 		ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
133469df578cSVladimir Oltean 			     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(npi),
133521468199SVladimir Oltean 			     QSYS_EXT_CPU_CFG);
1336ba551bc3SVladimir Oltean 
133769df578cSVladimir Oltean 		/* Enable NPI port */
133869df578cSVladimir Oltean 		ocelot_write_rix(ocelot,
133969df578cSVladimir Oltean 				 QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
134069df578cSVladimir Oltean 				 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
134169df578cSVladimir Oltean 				 QSYS_SWITCH_PORT_MODE_PORT_ENA,
134269df578cSVladimir Oltean 				 QSYS_SWITCH_PORT_MODE, npi);
134369df578cSVladimir Oltean 		/* NPI port Injection/Extraction configuration */
134469df578cSVladimir Oltean 		ocelot_write_rix(ocelot,
134569df578cSVladimir Oltean 				 SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
134669df578cSVladimir Oltean 				 SYS_PORT_MODE_INCL_INJ_HDR(injection),
134769df578cSVladimir Oltean 				 SYS_PORT_MODE, npi);
134821468199SVladimir Oltean 	}
134921468199SVladimir Oltean 
135069df578cSVladimir Oltean 	/* Enable CPU port module */
135121468199SVladimir Oltean 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
135221468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
135321468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
135421468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, cpu);
135569df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
135621468199SVladimir Oltean 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
135721468199SVladimir Oltean 			 SYS_PORT_MODE_INCL_INJ_HDR(injection),
135821468199SVladimir Oltean 			 SYS_PORT_MODE, cpu);
135921468199SVladimir Oltean 
136021468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
136121468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
136221468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
136321468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
136421468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
136521468199SVladimir Oltean }
136669df578cSVladimir Oltean EXPORT_SYMBOL(ocelot_configure_cpu);
136721468199SVladimir Oltean 
1368a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1369a556c76aSAlexandre Belloni {
1370a556c76aSAlexandre Belloni 	char queue_name[32];
137121468199SVladimir Oltean 	int i, ret;
137221468199SVladimir Oltean 	u32 port;
1373a556c76aSAlexandre Belloni 
13743a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
13753a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
13763a77b593SVladimir Oltean 		if (ret) {
13773a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
13783a77b593SVladimir Oltean 			return ret;
13793a77b593SVladimir Oltean 		}
13803a77b593SVladimir Oltean 	}
13813a77b593SVladimir Oltean 
1382dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1383dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
1384dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
1385dc96ee37SAlexandre Belloni 		return -ENOMEM;
1386dc96ee37SAlexandre Belloni 
1387a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1388a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1389a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1390a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1391a556c76aSAlexandre Belloni 		return -ENOMEM;
1392a556c76aSAlexandre Belloni 
1393a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
13944e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
13954e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1396a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1397a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1398a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1399a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1400a556c76aSAlexandre Belloni 		return -ENOMEM;
1401a556c76aSAlexandre Belloni 
14022b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1403a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1404a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1405aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
1406a556c76aSAlexandre Belloni 
1407a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1408a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1409a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1410a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1411a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1412a556c76aSAlexandre Belloni 	}
1413a556c76aSAlexandre Belloni 
1414a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1415a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1416a556c76aSAlexandre Belloni 
1417a556c76aSAlexandre Belloni 	/* Aggregation mode */
1418a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1419a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1420a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1421a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1422a556c76aSAlexandre Belloni 
1423a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1424a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1425a556c76aSAlexandre Belloni 	 */
1426a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1427a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1428a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1429a556c76aSAlexandre Belloni 
1430a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1431a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1432a556c76aSAlexandre Belloni 
1433a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1434a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1435a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1436a556c76aSAlexandre Belloni 
1437a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1438a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1439a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1440a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1441a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
1442a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1443a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1444a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1445a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1446a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1447a556c76aSAlexandre Belloni 
1448a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1449a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1450a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1451a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1452a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1453a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1454a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1455a556c76aSAlexandre Belloni 				 port);
1456a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1457a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1458a556c76aSAlexandre Belloni 	}
1459a556c76aSAlexandre Belloni 
1460a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
1461a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
1462a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1463a556c76aSAlexandre Belloni 
1464a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1465a556c76aSAlexandre Belloni 	}
1466a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
1467a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1468a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
1469a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1470a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1471a556c76aSAlexandre Belloni 
1472a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1473a556c76aSAlexandre Belloni 	 * registers endianness.
1474a556c76aSAlexandre Belloni 	 */
1475a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1476a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1477a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1478a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1479a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1480a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1481a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1482a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1483a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1484a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1485a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1486a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1487a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1488a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1489a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1490a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1491a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1492a556c76aSAlexandre Belloni 
14931e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1494a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1495a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
14964e3b0468SAntoine Tenart 
1497a556c76aSAlexandre Belloni 	return 0;
1498a556c76aSAlexandre Belloni }
1499a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1500a556c76aSAlexandre Belloni 
1501a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1502a556c76aSAlexandre Belloni {
15034e3b0468SAntoine Tenart 	struct ocelot_port *port;
15044e3b0468SAntoine Tenart 	int i;
15054e3b0468SAntoine Tenart 
1506c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
1507a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1508a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
15094e3b0468SAntoine Tenart 
15104e3b0468SAntoine Tenart 	for (i = 0; i < ocelot->num_phys_ports; i++) {
15114e3b0468SAntoine Tenart 		port = ocelot->ports[i];
1512b049da13SYangbo Lu 		skb_queue_purge(&port->tx_skbs);
15134e3b0468SAntoine Tenart 	}
1514a556c76aSAlexandre Belloni }
1515a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1516a556c76aSAlexandre Belloni 
1517a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1518