xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision f6fe01d6fa24dd3c89996ad82780872441e86bfa)
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 
1502f0402feSVladimir Oltean static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
151c3e58a75SVladimir Oltean 					struct ocelot_vlan native_vlan)
15297bb69e1SVladimir Oltean {
15397bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
15487b0f983SVladimir Oltean 	u32 val = 0;
15597bb69e1SVladimir Oltean 
156c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
15797bb69e1SVladimir Oltean 
158c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1597142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
16097bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
16197bb69e1SVladimir Oltean 
16287b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
163e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
16487b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
16587b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
16687b0f983SVladimir Oltean 		else
16787b0f983SVladimir Oltean 			/* Tag all frames */
16887b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
16987b0f983SVladimir Oltean 	} else {
17087b0f983SVladimir Oltean 		/* Port tagging disabled. */
17187b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
17287b0f983SVladimir Oltean 	}
17387b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
17487b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
17587b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
17697bb69e1SVladimir Oltean }
17797bb69e1SVladimir Oltean 
17875e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
179c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
180c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
18175e5a554SVladimir Oltean {
18275e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
183be0576feSVladimir Oltean 	u32 val = 0;
18475e5a554SVladimir Oltean 
185c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
18675e5a554SVladimir Oltean 
18775e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
188c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
18975e5a554SVladimir Oltean 
19075e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
191c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
19275e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
19375e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
194be0576feSVladimir Oltean 
195be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
196be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
197be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
198be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
199be0576feSVladimir Oltean 	 */
200be0576feSVladimir Oltean 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
201be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
202be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
203be0576feSVladimir Oltean 
204be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
205be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
206be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
207be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
20875e5a554SVladimir Oltean }
20975e5a554SVladimir Oltean 
2102e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
211bae33f2bSVladimir Oltean 			       bool vlan_aware)
21287b0f983SVladimir Oltean {
21370edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
214bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
21570edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
216bae33f2bSVladimir Oltean 	u32 val;
21770edfae1SVladimir Oltean 
21870edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
21970edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
22070edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
22170edfae1SVladimir Oltean 			dev_err(ocelot->dev,
22270edfae1SVladimir Oltean 				"Cannot change VLAN state with vlan modify rules active\n");
22370edfae1SVladimir Oltean 			return -EBUSY;
22470edfae1SVladimir Oltean 		}
22570edfae1SVladimir Oltean 	}
22670edfae1SVladimir Oltean 
22787b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
22887b0f983SVladimir Oltean 
22987b0f983SVladimir Oltean 	if (vlan_aware)
23087b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
23187b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
23287b0f983SVladimir Oltean 	else
23387b0f983SVladimir Oltean 		val = 0;
23487b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
23587b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
23687b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
23787b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
23887b0f983SVladimir Oltean 
239c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
240c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2412e554a7aSVladimir Oltean 
2422e554a7aSVladimir Oltean 	return 0;
24387b0f983SVladimir Oltean }
24487b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
24587b0f983SVladimir Oltean 
2462f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2472f0402feSVladimir Oltean 			bool untagged)
2482f0402feSVladimir Oltean {
2492f0402feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2502f0402feSVladimir Oltean 
2512f0402feSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
2522f0402feSVladimir Oltean 	if (untagged && ocelot_port->native_vlan.vid != vid &&
2532f0402feSVladimir Oltean 	    ocelot_port->native_vlan.valid) {
2542f0402feSVladimir Oltean 		dev_err(ocelot->dev,
2552f0402feSVladimir Oltean 			"Port already has a native VLAN: %d\n",
2562f0402feSVladimir Oltean 			ocelot_port->native_vlan.vid);
2572f0402feSVladimir Oltean 		return -EBUSY;
2582f0402feSVladimir Oltean 	}
2592f0402feSVladimir Oltean 
2602f0402feSVladimir Oltean 	return 0;
2612f0402feSVladimir Oltean }
2622f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
2632f0402feSVladimir Oltean 
2645e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2657142529fSAntoine Tenart 		    bool untagged)
2667142529fSAntoine Tenart {
2677142529fSAntoine Tenart 	int ret;
2687142529fSAntoine Tenart 
2697142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
27097bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2717142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2727142529fSAntoine Tenart 	if (ret)
2737142529fSAntoine Tenart 		return ret;
2747142529fSAntoine Tenart 
2757142529fSAntoine Tenart 	/* Default ingress vlan classification */
276c3e58a75SVladimir Oltean 	if (pvid) {
277c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
278c3e58a75SVladimir Oltean 
279c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
280e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
281c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
282c3e58a75SVladimir Oltean 	}
2837142529fSAntoine Tenart 
2847142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
28597bb69e1SVladimir Oltean 	if (untagged) {
286c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
287c3e58a75SVladimir Oltean 
288c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
289e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
2902f0402feSVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
291b9cd75e6SVladimir Oltean 	}
2927142529fSAntoine Tenart 
2937142529fSAntoine Tenart 	return 0;
2947142529fSAntoine Tenart }
2955e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
2967142529fSAntoine Tenart 
2975e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
2989855934cSVladimir Oltean {
2999855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3009855934cSVladimir Oltean 	int ret;
3017142529fSAntoine Tenart 
3027142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
30397bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3047142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3057142529fSAntoine Tenart 	if (ret)
3067142529fSAntoine Tenart 		return ret;
3077142529fSAntoine Tenart 
308be0576feSVladimir Oltean 	/* Ingress */
309be0576feSVladimir Oltean 	if (ocelot_port->pvid_vlan.vid == vid) {
310be0576feSVladimir Oltean 		struct ocelot_vlan pvid_vlan = {0};
311be0576feSVladimir Oltean 
312be0576feSVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
313be0576feSVladimir Oltean 	}
314be0576feSVladimir Oltean 
3157142529fSAntoine Tenart 	/* Egress */
316c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
317e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
318c3e58a75SVladimir Oltean 
319c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
320c3e58a75SVladimir Oltean 	}
3217142529fSAntoine Tenart 
3227142529fSAntoine Tenart 	return 0;
3237142529fSAntoine Tenart }
3245e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3257142529fSAntoine Tenart 
326a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
327a556c76aSAlexandre Belloni {
3287142529fSAntoine Tenart 	u16 port, vid;
3297142529fSAntoine Tenart 
330a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
331a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
332a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
333a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3347142529fSAntoine Tenart 
3357142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3367142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3377142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3387142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3397142529fSAntoine Tenart 	}
3407142529fSAntoine Tenart 
3417142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3427142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3437142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3447142529fSAntoine Tenart 	 */
3457142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3467142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3477142529fSAntoine Tenart 
3487142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3497142529fSAntoine Tenart 	 * default.
3507142529fSAntoine Tenart 	 */
351714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
352714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3537142529fSAntoine Tenart 
3547142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3557142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3567142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3577142529fSAntoine Tenart 	}
358a556c76aSAlexandre Belloni }
359a556c76aSAlexandre Belloni 
3605e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
36126f4dbabSVladimir Oltean 			struct phy_device *phydev)
362a556c76aSAlexandre Belloni {
36326f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3645bc9d2e6SVladimir Oltean 	int speed, mode = 0;
365a556c76aSAlexandre Belloni 
36626f4dbabSVladimir Oltean 	switch (phydev->speed) {
367a556c76aSAlexandre Belloni 	case SPEED_10:
368a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
369a556c76aSAlexandre Belloni 		break;
370a556c76aSAlexandre Belloni 	case SPEED_100:
371a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
372a556c76aSAlexandre Belloni 		break;
373a556c76aSAlexandre Belloni 	case SPEED_1000:
374a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
375a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
376a556c76aSAlexandre Belloni 		break;
377a556c76aSAlexandre Belloni 	case SPEED_2500:
378a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
379a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
380a556c76aSAlexandre Belloni 		break;
381a556c76aSAlexandre Belloni 	default:
38226f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
38326f4dbabSVladimir Oltean 			port, phydev->speed);
384a556c76aSAlexandre Belloni 		return;
385a556c76aSAlexandre Belloni 	}
386a556c76aSAlexandre Belloni 
38726f4dbabSVladimir Oltean 	phy_print_status(phydev);
388a556c76aSAlexandre Belloni 
38926f4dbabSVladimir Oltean 	if (!phydev->link)
390a556c76aSAlexandre Belloni 		return;
391a556c76aSAlexandre Belloni 
392a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
393004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
394a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
395a556c76aSAlexandre Belloni 
3961ba8f656SVladimir Oltean 	/* Disable HDX fast control */
3971ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
3981ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
3991ba8f656SVladimir Oltean 
4001ba8f656SVladimir Oltean 	/* SGMII only for now */
4011ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4021ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
4031ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4041ba8f656SVladimir Oltean 
4051ba8f656SVladimir Oltean 	/* Enable PCS */
4061ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4071ba8f656SVladimir Oltean 
4081ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4091ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4101ba8f656SVladimir Oltean 
4111ba8f656SVladimir Oltean 	/* No loopback */
4121ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
413a556c76aSAlexandre Belloni 
414a556c76aSAlexandre Belloni 	/* Enable MAC module */
415004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
416a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
417a556c76aSAlexandre Belloni 
418a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
419a556c76aSAlexandre Belloni 	 * reset */
420004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
421a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
422a556c76aSAlexandre Belloni 
423a556c76aSAlexandre Belloni 	/* No PFC */
424a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
425004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
426a556c76aSAlexandre Belloni 
427a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
428886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
429886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
430a556c76aSAlexandre Belloni 
431a556c76aSAlexandre Belloni 	/* Flow control */
432a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
433a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
434a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
435a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
436a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
437004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
438004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
439a556c76aSAlexandre Belloni }
4405e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
441a556c76aSAlexandre Belloni 
4425e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
443889b8950SVladimir Oltean 			struct phy_device *phy)
444a556c76aSAlexandre Belloni {
445a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
446a556c76aSAlexandre Belloni 	 * MAC addresses.
447a556c76aSAlexandre Belloni 	 */
448a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
449a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
450004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
451004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
452889b8950SVladimir Oltean }
4535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
454889b8950SVladimir Oltean 
4555e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
456889b8950SVladimir Oltean {
457889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
458889b8950SVladimir Oltean 
459889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
460886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
461889b8950SVladimir Oltean }
4625e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
463889b8950SVladimir Oltean 
464e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
465e2f9a8feSVladimir Oltean 				  struct sk_buff *clone)
466400928bfSYangbo Lu {
467e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
468400928bfSYangbo Lu 
4696565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
4706565243cSVladimir Oltean 
471e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
472b049da13SYangbo Lu 	/* Store timestamp ID in cb[0] of sk_buff */
473e2f9a8feSVladimir Oltean 	clone->cb[0] = ocelot_port->ts_id;
4746565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
475e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
4766565243cSVladimir Oltean 
4776565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
478400928bfSYangbo Lu }
479400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
480400928bfSYangbo Lu 
481e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
482e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
4834e3b0468SAntoine Tenart {
4844e3b0468SAntoine Tenart 	unsigned long flags;
4854e3b0468SAntoine Tenart 	u32 val;
4864e3b0468SAntoine Tenart 
4874e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
4884e3b0468SAntoine Tenart 
4894e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
4904e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
4914e3b0468SAntoine Tenart 
4924e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
4934e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
4944e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
4954e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
4964e3b0468SAntoine Tenart 
4974e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
4984e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
4994e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
5004e3b0468SAntoine Tenart 
5014e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
5024e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
5034e3b0468SAntoine Tenart 		ts->tv_sec--;
5044e3b0468SAntoine Tenart 
5054e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
5064e3b0468SAntoine Tenart }
507e23a7b3eSYangbo Lu 
508e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
509e23a7b3eSYangbo Lu {
510e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
511e23a7b3eSYangbo Lu 
512e23a7b3eSYangbo Lu 	while (budget--) {
513b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
514e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
515e23a7b3eSYangbo Lu 		struct ocelot_port *port;
516e23a7b3eSYangbo Lu 		struct timespec64 ts;
517b049da13SYangbo Lu 		unsigned long flags;
518e23a7b3eSYangbo Lu 		u32 val, id, txport;
519e23a7b3eSYangbo Lu 
520e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
521e23a7b3eSYangbo Lu 
522e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
523e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
524e23a7b3eSYangbo Lu 			break;
525e23a7b3eSYangbo Lu 
526e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
527e23a7b3eSYangbo Lu 
528e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
529e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
530e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
531e23a7b3eSYangbo Lu 
532e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
533e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
534e23a7b3eSYangbo Lu 
535b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
536b049da13SYangbo Lu 
537b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
538b049da13SYangbo Lu 			if (skb->cb[0] != id)
539e23a7b3eSYangbo Lu 				continue;
540b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
541b049da13SYangbo Lu 			skb_match = skb;
542fc62c094SYangbo Lu 			break;
543e23a7b3eSYangbo Lu 		}
544e23a7b3eSYangbo Lu 
545b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
546b049da13SYangbo Lu 
5475fd82200Slaurent brando 		/* Get the h/w timestamp */
5485fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
549e23a7b3eSYangbo Lu 
550b049da13SYangbo Lu 		if (unlikely(!skb_match))
551e23a7b3eSYangbo Lu 			continue;
552e23a7b3eSYangbo Lu 
553e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
554e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
555e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
556e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
5575fd82200Slaurent brando 
5585fd82200Slaurent brando 		/* Next ts */
5595fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
560e23a7b3eSYangbo Lu 	}
561e23a7b3eSYangbo Lu }
562e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
5634e3b0468SAntoine Tenart 
5645e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
56587b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
566a556c76aSAlexandre Belloni {
567471beb11SVladimir Oltean 	int pgid = port;
568471beb11SVladimir Oltean 
569471beb11SVladimir Oltean 	if (port == ocelot->npi)
570471beb11SVladimir Oltean 		pgid = PGID_CPU;
571a556c76aSAlexandre Belloni 
572471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
573a556c76aSAlexandre Belloni }
5745e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
575a556c76aSAlexandre Belloni 
5765e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
577531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
578531ee1a6SVladimir Oltean {
579531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
580531ee1a6SVladimir Oltean }
5815e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
582531ee1a6SVladimir Oltean 
5839c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
584531ee1a6SVladimir Oltean 			    bool is_static, void *data)
585a556c76aSAlexandre Belloni {
586531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
587a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
588a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
589a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
590a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
591a556c76aSAlexandre Belloni 
592a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
593a556c76aSAlexandre Belloni 		goto skip;
594a556c76aSAlexandre Belloni 
595a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
596a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
597a556c76aSAlexandre Belloni 	if (!nlh)
598a556c76aSAlexandre Belloni 		return -EMSGSIZE;
599a556c76aSAlexandre Belloni 
600a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
601a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
602a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
603a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
604a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
605a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
606a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
607531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
608a556c76aSAlexandre Belloni 
609531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
610a556c76aSAlexandre Belloni 		goto nla_put_failure;
611a556c76aSAlexandre Belloni 
612531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
613a556c76aSAlexandre Belloni 		goto nla_put_failure;
614a556c76aSAlexandre Belloni 
615a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
616a556c76aSAlexandre Belloni 
617a556c76aSAlexandre Belloni skip:
618a556c76aSAlexandre Belloni 	dump->idx++;
619a556c76aSAlexandre Belloni 	return 0;
620a556c76aSAlexandre Belloni 
621a556c76aSAlexandre Belloni nla_put_failure:
622a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
623a556c76aSAlexandre Belloni 	return -EMSGSIZE;
624a556c76aSAlexandre Belloni }
6259c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
626a556c76aSAlexandre Belloni 
627531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
628a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
629a556c76aSAlexandre Belloni {
630a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
631531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
632a556c76aSAlexandre Belloni 
633a556c76aSAlexandre Belloni 	/* Set row and column to read from */
634a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
635a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
636a556c76aSAlexandre Belloni 
637a556c76aSAlexandre Belloni 	/* Issue a read command */
638a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
639a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
640a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
641a556c76aSAlexandre Belloni 
642a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
643a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
644a556c76aSAlexandre Belloni 
645a556c76aSAlexandre Belloni 	/* Read the entry flags */
646a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
647a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
648a556c76aSAlexandre Belloni 		return -EINVAL;
649a556c76aSAlexandre Belloni 
650a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
651a556c76aSAlexandre Belloni 	 * do not report it.
652a556c76aSAlexandre Belloni 	 */
653a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
654531ee1a6SVladimir Oltean 	if (dst != port)
655a556c76aSAlexandre Belloni 		return -EINVAL;
656a556c76aSAlexandre Belloni 
657a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
658a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
659a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
660a556c76aSAlexandre Belloni 
661a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
662a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
663a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
664a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
665a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
666a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
667a556c76aSAlexandre Belloni 
668a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
669a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
670a556c76aSAlexandre Belloni 
671a556c76aSAlexandre Belloni 	return 0;
672a556c76aSAlexandre Belloni }
673a556c76aSAlexandre Belloni 
6745e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
675531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
676a556c76aSAlexandre Belloni {
677531ee1a6SVladimir Oltean 	int i, j;
678a556c76aSAlexandre Belloni 
67921ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
68021ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
681a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
682531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
683531ee1a6SVladimir Oltean 			bool is_static;
684531ee1a6SVladimir Oltean 			int ret;
685531ee1a6SVladimir Oltean 
686531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
687a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
688a556c76aSAlexandre Belloni 			 * skip it.
689a556c76aSAlexandre Belloni 			 */
690a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
691a556c76aSAlexandre Belloni 				continue;
692a556c76aSAlexandre Belloni 			else if (ret)
693531ee1a6SVladimir Oltean 				return ret;
694a556c76aSAlexandre Belloni 
695531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
696531ee1a6SVladimir Oltean 
697531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
698a556c76aSAlexandre Belloni 			if (ret)
699531ee1a6SVladimir Oltean 				return ret;
700a556c76aSAlexandre Belloni 		}
701a556c76aSAlexandre Belloni 	}
702a556c76aSAlexandre Belloni 
703531ee1a6SVladimir Oltean 	return 0;
704531ee1a6SVladimir Oltean }
7055e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
706531ee1a6SVladimir Oltean 
707f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
7084e3b0468SAntoine Tenart {
7094e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
7104e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
7114e3b0468SAntoine Tenart }
712f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
7134e3b0468SAntoine Tenart 
714f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
7154e3b0468SAntoine Tenart {
716306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
7174e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
7184e3b0468SAntoine Tenart 
7194e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
7204e3b0468SAntoine Tenart 		return -EFAULT;
7214e3b0468SAntoine Tenart 
7224e3b0468SAntoine Tenart 	/* reserved for future extensions */
7234e3b0468SAntoine Tenart 	if (cfg.flags)
7244e3b0468SAntoine Tenart 		return -EINVAL;
7254e3b0468SAntoine Tenart 
7264e3b0468SAntoine Tenart 	/* Tx type sanity check */
7274e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
7284e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
729306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
7304e3b0468SAntoine Tenart 		break;
7314e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
7324e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
7334e3b0468SAntoine Tenart 		 * need to update the origin time.
7344e3b0468SAntoine Tenart 		 */
735306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
7364e3b0468SAntoine Tenart 		break;
7374e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
738306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
7394e3b0468SAntoine Tenart 		break;
7404e3b0468SAntoine Tenart 	default:
7414e3b0468SAntoine Tenart 		return -ERANGE;
7424e3b0468SAntoine Tenart 	}
7434e3b0468SAntoine Tenart 
7444e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
7454e3b0468SAntoine Tenart 
7464e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
7474e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
7484e3b0468SAntoine Tenart 		break;
7494e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
7504e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
7514e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
7524e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
7534e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
7544e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
7554e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
7564e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
7574e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
7584e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
7594e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
7604e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
7614e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
7624e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
7634e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
7644e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
7654e3b0468SAntoine Tenart 		break;
7664e3b0468SAntoine Tenart 	default:
7674e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
7684e3b0468SAntoine Tenart 		return -ERANGE;
7694e3b0468SAntoine Tenart 	}
7704e3b0468SAntoine Tenart 
7714e3b0468SAntoine Tenart 	/* Commit back the result & save it */
7724e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
7734e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
7744e3b0468SAntoine Tenart 
7754e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
7764e3b0468SAntoine Tenart }
777f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
7784e3b0468SAntoine Tenart 
7795e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
780a556c76aSAlexandre Belloni {
781a556c76aSAlexandre Belloni 	int i;
782a556c76aSAlexandre Belloni 
783a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
784a556c76aSAlexandre Belloni 		return;
785a556c76aSAlexandre Belloni 
786a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
787a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
788a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
789a556c76aSAlexandre Belloni }
7905e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
791a556c76aSAlexandre Belloni 
7921e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
793a556c76aSAlexandre Belloni {
794a556c76aSAlexandre Belloni 	int i, j;
795a556c76aSAlexandre Belloni 
796a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
797a556c76aSAlexandre Belloni 
798a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
799a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
800a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
801a556c76aSAlexandre Belloni 
802a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
803a556c76aSAlexandre Belloni 			u32 val;
804a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
805a556c76aSAlexandre Belloni 
806a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
807a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
808a556c76aSAlexandre Belloni 
809a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
810a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
811a556c76aSAlexandre Belloni 
812a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
813a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
814a556c76aSAlexandre Belloni 		}
815a556c76aSAlexandre Belloni 	}
816a556c76aSAlexandre Belloni 
8171e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
8181e1caa97SClaudiu Manoil }
8191e1caa97SClaudiu Manoil 
8201e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
8211e1caa97SClaudiu Manoil {
8221e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
8231e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
8241e1caa97SClaudiu Manoil 					     stats_work);
8251e1caa97SClaudiu Manoil 
8261e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
8271e1caa97SClaudiu Manoil 
828a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
829a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
830a556c76aSAlexandre Belloni }
831a556c76aSAlexandre Belloni 
8325e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
833a556c76aSAlexandre Belloni {
834a556c76aSAlexandre Belloni 	int i;
835a556c76aSAlexandre Belloni 
836a556c76aSAlexandre Belloni 	/* check and update now */
8371e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
838a556c76aSAlexandre Belloni 
839a556c76aSAlexandre Belloni 	/* Copy all counters */
840a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
841004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
842a556c76aSAlexandre Belloni }
8435e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
844a556c76aSAlexandre Belloni 
8455e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
846c7282d38SVladimir Oltean {
847a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
848a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
849c7282d38SVladimir Oltean 
850a556c76aSAlexandre Belloni 	return ocelot->num_stats;
851a556c76aSAlexandre Belloni }
8525e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
853a556c76aSAlexandre Belloni 
8545e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
855c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
856c7282d38SVladimir Oltean {
8574e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
8584e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
859d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
860d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
861d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
862d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
863d2b09a8eSYangbo Lu 		return 0;
864d2b09a8eSYangbo Lu 	}
8654e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
8664e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
8674e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
8684e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
8694e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
8704e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
8714e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
8724e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
8734e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
8744e3b0468SAntoine Tenart 
8754e3b0468SAntoine Tenart 	return 0;
8764e3b0468SAntoine Tenart }
8775e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
8784e3b0468SAntoine Tenart 
8795e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
880a556c76aSAlexandre Belloni {
881a556c76aSAlexandre Belloni 	u32 port_cfg;
8824bda1415SVladimir Oltean 	int p, i;
883a556c76aSAlexandre Belloni 
8844bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
8854bda1415SVladimir Oltean 		return;
886a556c76aSAlexandre Belloni 
8874bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
888a556c76aSAlexandre Belloni 
889a556c76aSAlexandre Belloni 	switch (state) {
890a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
8914bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
892df561f66SGustavo A. R. Silva 		fallthrough;
893a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
894a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
895a556c76aSAlexandre Belloni 		break;
896a556c76aSAlexandre Belloni 
897a556c76aSAlexandre Belloni 	default:
898a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
8994bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
900a556c76aSAlexandre Belloni 		break;
901a556c76aSAlexandre Belloni 	}
902a556c76aSAlexandre Belloni 
9034bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
904a556c76aSAlexandre Belloni 
905a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
906a556c76aSAlexandre Belloni 	 * a source for the other ports.
907a556c76aSAlexandre Belloni 	 */
9084bda1415SVladimir Oltean 	for (p = 0; p < ocelot->num_phys_ports; p++) {
90969df578cSVladimir Oltean 		if (ocelot->bridge_fwd_mask & BIT(p)) {
9104bda1415SVladimir Oltean 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
911a556c76aSAlexandre Belloni 
912a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
913a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
914a556c76aSAlexandre Belloni 
915a556c76aSAlexandre Belloni 				if (!bond_mask)
916a556c76aSAlexandre Belloni 					continue;
917a556c76aSAlexandre Belloni 
9184bda1415SVladimir Oltean 				if (bond_mask & BIT(p)) {
919a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
920a556c76aSAlexandre Belloni 					break;
921a556c76aSAlexandre Belloni 				}
922a556c76aSAlexandre Belloni 			}
923a556c76aSAlexandre Belloni 
924c9d2203bSVladimir Oltean 			ocelot_write_rix(ocelot, mask,
9254bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
926a556c76aSAlexandre Belloni 		} else {
92769df578cSVladimir Oltean 			ocelot_write_rix(ocelot, 0,
9284bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
9294bda1415SVladimir Oltean 		}
930a556c76aSAlexandre Belloni 	}
931a556c76aSAlexandre Belloni }
9325e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
933a556c76aSAlexandre Belloni 
9345e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
9354bda1415SVladimir Oltean {
936c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
937c0d7eccbSVladimir Oltean 
938c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
939c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
940c0d7eccbSVladimir Oltean 	 */
941c0d7eccbSVladimir Oltean 	if (!age_period)
942c0d7eccbSVladimir Oltean 		age_period = 1;
943c0d7eccbSVladimir Oltean 
944c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
945a556c76aSAlexandre Belloni }
9465e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
947a556c76aSAlexandre Belloni 
948a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
949a556c76aSAlexandre Belloni 						     const unsigned char *addr,
950a556c76aSAlexandre Belloni 						     u16 vid)
951a556c76aSAlexandre Belloni {
952a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
953a556c76aSAlexandre Belloni 
954a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
955a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
956a556c76aSAlexandre Belloni 			return mc;
957a556c76aSAlexandre Belloni 	}
958a556c76aSAlexandre Belloni 
959a556c76aSAlexandre Belloni 	return NULL;
960a556c76aSAlexandre Belloni }
961a556c76aSAlexandre Belloni 
9629403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
9639403c158SVladimir Oltean {
9649403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
9659403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
9669403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
9679403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
9687c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
9699403c158SVladimir Oltean }
9709403c158SVladimir Oltean 
971e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
972e5d1f896SVladimir Oltean 					     unsigned long ports)
973e5d1f896SVladimir Oltean {
974e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
975e5d1f896SVladimir Oltean 
976e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
977e5d1f896SVladimir Oltean 	if (!pgid)
978e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
979e5d1f896SVladimir Oltean 
980e5d1f896SVladimir Oltean 	pgid->ports = ports;
981e5d1f896SVladimir Oltean 	pgid->index = index;
982e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
983e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
984e5d1f896SVladimir Oltean 
985e5d1f896SVladimir Oltean 	return pgid;
986e5d1f896SVladimir Oltean }
987e5d1f896SVladimir Oltean 
988e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
989e5d1f896SVladimir Oltean {
990e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
991e5d1f896SVladimir Oltean 		return;
992e5d1f896SVladimir Oltean 
993e5d1f896SVladimir Oltean 	list_del(&pgid->list);
994e5d1f896SVladimir Oltean 	kfree(pgid);
995e5d1f896SVladimir Oltean }
996e5d1f896SVladimir Oltean 
997e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
998bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
9999403c158SVladimir Oltean {
1000e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1001e5d1f896SVladimir Oltean 	int index;
10029403c158SVladimir Oltean 
10039403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
10049403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
10059403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
10069403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
10079403c158SVladimir Oltean 	 */
1008bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1009bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1010e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
10119403c158SVladimir Oltean 
1012e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1013e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1014e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1015e5d1f896SVladimir Oltean 		 */
1016e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1017e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1018e5d1f896SVladimir Oltean 			return pgid;
1019e5d1f896SVladimir Oltean 		}
1020e5d1f896SVladimir Oltean 	}
1021e5d1f896SVladimir Oltean 
1022e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1023e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
10249403c158SVladimir Oltean 		bool used = false;
10259403c158SVladimir Oltean 
1026e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1027e5d1f896SVladimir Oltean 			if (pgid->index == index) {
10289403c158SVladimir Oltean 				used = true;
10299403c158SVladimir Oltean 				break;
10309403c158SVladimir Oltean 			}
10319403c158SVladimir Oltean 		}
10329403c158SVladimir Oltean 
10339403c158SVladimir Oltean 		if (!used)
1034e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
10359403c158SVladimir Oltean 	}
10369403c158SVladimir Oltean 
1037e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
10389403c158SVladimir Oltean }
10399403c158SVladimir Oltean 
10409403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1041bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
10429403c158SVladimir Oltean {
1043ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
10449403c158SVladimir Oltean 
1045bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
10469403c158SVladimir Oltean 		addr[0] = 0;
10479403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
10489403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1049bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
10509403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
10519403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
10529403c158SVladimir Oltean 	}
10539403c158SVladimir Oltean }
10549403c158SVladimir Oltean 
1055209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1056209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1057a556c76aSAlexandre Belloni {
1058a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1059004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1060e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1061a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1062a556c76aSAlexandre Belloni 
1063471beb11SVladimir Oltean 	if (port == ocelot->npi)
1064471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1065471beb11SVladimir Oltean 
1066a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1067a556c76aSAlexandre Belloni 	if (!mc) {
1068728e69aeSVladimir Oltean 		/* New entry */
1069bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1070bb8d53fdSVladimir Oltean 		if (!mc)
1071bb8d53fdSVladimir Oltean 			return -ENOMEM;
1072bb8d53fdSVladimir Oltean 
1073bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1074bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1075bb8d53fdSVladimir Oltean 		mc->vid = vid;
1076bb8d53fdSVladimir Oltean 
1077a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1078728e69aeSVladimir Oltean 	} else {
1079e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1080e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1081e5d1f896SVladimir Oltean 		 */
1082e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1083bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1084a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1085a556c76aSAlexandre Belloni 	}
1086a556c76aSAlexandre Belloni 
1087004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1088e5d1f896SVladimir Oltean 
1089e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1090e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1091e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1092e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1093e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1094e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1095e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1096e5d1f896SVladimir Oltean 	}
1097e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1098e5d1f896SVladimir Oltean 
1099bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1100a556c76aSAlexandre Belloni 
1101e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1102e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1103e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1104e5d1f896SVladimir Oltean 				 pgid->index);
1105e5d1f896SVladimir Oltean 
1106e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1107bb8d53fdSVladimir Oltean 				 mc->entry_type);
1108a556c76aSAlexandre Belloni }
1109209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1110a556c76aSAlexandre Belloni 
1111209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1112a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1113a556c76aSAlexandre Belloni {
1114a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1115004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1116e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1117a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1118a556c76aSAlexandre Belloni 
1119471beb11SVladimir Oltean 	if (port == ocelot->npi)
1120471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1121471beb11SVladimir Oltean 
1122a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1123a556c76aSAlexandre Belloni 	if (!mc)
1124a556c76aSAlexandre Belloni 		return -ENOENT;
1125a556c76aSAlexandre Belloni 
1126bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1127a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1128a556c76aSAlexandre Belloni 
1129e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1130004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1131a556c76aSAlexandre Belloni 	if (!mc->ports) {
1132a556c76aSAlexandre Belloni 		list_del(&mc->list);
1133a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1134a556c76aSAlexandre Belloni 		return 0;
1135a556c76aSAlexandre Belloni 	}
1136a556c76aSAlexandre Belloni 
1137e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1138e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1139e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1140e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1141e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1142e5d1f896SVladimir Oltean 
1143bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1144a556c76aSAlexandre Belloni 
1145e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1146e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1147e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1148e5d1f896SVladimir Oltean 				 pgid->index);
1149e5d1f896SVladimir Oltean 
1150e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1151bb8d53fdSVladimir Oltean 				 mc->entry_type);
1152a556c76aSAlexandre Belloni }
1153209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1154a556c76aSAlexandre Belloni 
11555e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1156a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1157a556c76aSAlexandre Belloni {
1158a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1159a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1160a556c76aSAlexandre Belloni 	} else {
1161a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1162a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1163a556c76aSAlexandre Belloni 			 * unsupported */
1164a556c76aSAlexandre Belloni 			return -ENODEV;
1165a556c76aSAlexandre Belloni 	}
1166a556c76aSAlexandre Belloni 
1167f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1168a556c76aSAlexandre Belloni 
1169a556c76aSAlexandre Belloni 	return 0;
1170a556c76aSAlexandre Belloni }
11715e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1172a556c76aSAlexandre Belloni 
11735e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1174a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1175a556c76aSAlexandre Belloni {
1176c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
11772e554a7aSVladimir Oltean 	int ret;
11782e554a7aSVladimir Oltean 
117997bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1180a556c76aSAlexandre Belloni 
1181a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1182a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
11837142529fSAntoine Tenart 
1184bae33f2bSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false);
11852e554a7aSVladimir Oltean 	if (ret)
11862e554a7aSVladimir Oltean 		return ret;
11872e554a7aSVladimir Oltean 
1188c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
11892f0402feSVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
11902f0402feSVladimir Oltean 
11912f0402feSVladimir Oltean 	return 0;
1192a556c76aSAlexandre Belloni }
11935e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1194a556c76aSAlexandre Belloni 
1195dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1196dc96ee37SAlexandre Belloni {
1197dc96ee37SAlexandre Belloni 	int i, port, lag;
1198dc96ee37SAlexandre Belloni 
1199dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
120096b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1201dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1202dc96ee37SAlexandre Belloni 
120396b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1204dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1205dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1206dc96ee37SAlexandre Belloni 
1207dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1208dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1209dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1210dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1211dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1212dc96ee37SAlexandre Belloni 
1213dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1214dc96ee37SAlexandre Belloni 		if (!bond_mask)
1215dc96ee37SAlexandre Belloni 			continue;
1216dc96ee37SAlexandre Belloni 
1217dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1218dc96ee37SAlexandre Belloni 			// Destination mask
1219dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1220dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1221dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1222dc96ee37SAlexandre Belloni 			aggr_count++;
1223dc96ee37SAlexandre Belloni 		}
1224dc96ee37SAlexandre Belloni 
122596b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1226dc96ee37SAlexandre Belloni 			u32 ac;
1227dc96ee37SAlexandre Belloni 
1228dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1229dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1230dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1231dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1232dc96ee37SAlexandre Belloni 		}
1233dc96ee37SAlexandre Belloni 	}
1234dc96ee37SAlexandre Belloni }
1235dc96ee37SAlexandre Belloni 
1236dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1237dc96ee37SAlexandre Belloni {
1238dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1239dc96ee37SAlexandre Belloni 	unsigned int p;
1240dc96ee37SAlexandre Belloni 
1241dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1242dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1243dc96ee37SAlexandre Belloni 
1244dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1245dc96ee37SAlexandre Belloni 
1246dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1247dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1248dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1249dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1250dc96ee37SAlexandre Belloni 	}
1251dc96ee37SAlexandre Belloni }
1252dc96ee37SAlexandre Belloni 
12539c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1254dc96ee37SAlexandre Belloni 			 struct net_device *bond)
1255dc96ee37SAlexandre Belloni {
1256dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1257dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1258f270dbfaSVladimir Oltean 	int lag, lp;
1259dc96ee37SAlexandre Belloni 
1260dc96ee37SAlexandre Belloni 	rcu_read_lock();
1261dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1262004d44f6SVladimir Oltean 		struct ocelot_port_private *priv = netdev_priv(ndev);
1263dc96ee37SAlexandre Belloni 
1264004d44f6SVladimir Oltean 		bond_mask |= BIT(priv->chip_port);
1265dc96ee37SAlexandre Belloni 	}
1266dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1267dc96ee37SAlexandre Belloni 
1268dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1269dc96ee37SAlexandre Belloni 
1270dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1271dc96ee37SAlexandre Belloni 	 * now on
1272dc96ee37SAlexandre Belloni 	 */
1273f270dbfaSVladimir Oltean 	if (port == lp) {
1274f270dbfaSVladimir Oltean 		lag = port;
1275f270dbfaSVladimir Oltean 		ocelot->lags[port] = bond_mask;
1276f270dbfaSVladimir Oltean 		bond_mask &= ~BIT(port);
1277dc96ee37SAlexandre Belloni 		if (bond_mask) {
1278dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1279dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1280dc96ee37SAlexandre Belloni 		}
1281dc96ee37SAlexandre Belloni 	} else {
1282dc96ee37SAlexandre Belloni 		lag = lp;
1283f270dbfaSVladimir Oltean 		ocelot->lags[lp] |= BIT(port);
1284dc96ee37SAlexandre Belloni 	}
1285dc96ee37SAlexandre Belloni 
1286dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1287dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1288dc96ee37SAlexandre Belloni 
1289dc96ee37SAlexandre Belloni 	return 0;
1290dc96ee37SAlexandre Belloni }
12919c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1292dc96ee37SAlexandre Belloni 
12939c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1294dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1295dc96ee37SAlexandre Belloni {
1296dc96ee37SAlexandre Belloni 	u32 port_cfg;
1297dc96ee37SAlexandre Belloni 	int i;
1298dc96ee37SAlexandre Belloni 
1299dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1300dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1301f270dbfaSVladimir Oltean 		ocelot->lags[i] &= ~BIT(port);
1302dc96ee37SAlexandre Belloni 
1303dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1304dc96ee37SAlexandre Belloni 	 * next port
1305dc96ee37SAlexandre Belloni 	 */
1306f270dbfaSVladimir Oltean 	if (ocelot->lags[port]) {
1307f270dbfaSVladimir Oltean 		int n = __ffs(ocelot->lags[port]);
1308dc96ee37SAlexandre Belloni 
1309f270dbfaSVladimir Oltean 		ocelot->lags[n] = ocelot->lags[port];
1310f270dbfaSVladimir Oltean 		ocelot->lags[port] = 0;
1311dc96ee37SAlexandre Belloni 
1312dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1313dc96ee37SAlexandre Belloni 	}
1314dc96ee37SAlexandre Belloni 
1315f270dbfaSVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1316dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1317f270dbfaSVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1318f270dbfaSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
1319dc96ee37SAlexandre Belloni 
1320dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1321dc96ee37SAlexandre Belloni }
13229c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
13230e332c85SPetr Machata 
1324a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1325a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
13260b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
13270b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
13280b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1329a8015dedSVladimir Oltean  */
13300b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
133131350d7fSVladimir Oltean {
133231350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1333a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1334e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1335601e984fSVladimir Oltean 	int atop, atop_tot;
133631350d7fSVladimir Oltean 
13370b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
13380b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
13390b912fc9SVladimir Oltean 
13400b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13410b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
13420b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
13430b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
13440b912fc9SVladimir Oltean 	}
13450b912fc9SVladimir Oltean 
1346a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1347fa914e9cSVladimir Oltean 
1348e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1349e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1350e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1351541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1352541132f0SMaxim Kochetkov 			    pause_start);
1353541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1354541132f0SMaxim Kochetkov 			    pause_stop);
1355fa914e9cSVladimir Oltean 
1356601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1357*f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
1358a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1359601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1360601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1361601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1362fa914e9cSVladimir Oltean }
13630b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
13640b912fc9SVladimir Oltean 
13650b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
13660b912fc9SVladimir Oltean {
13670b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
13680b912fc9SVladimir Oltean 
13690b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
13700b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
13710b912fc9SVladimir Oltean 
13720b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13730b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
13740b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
13750b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
13760b912fc9SVladimir Oltean 	}
13770b912fc9SVladimir Oltean 
13780b912fc9SVladimir Oltean 	return max_mtu;
13790b912fc9SVladimir Oltean }
13800b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1381fa914e9cSVladimir Oltean 
13825e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1383fa914e9cSVladimir Oltean {
1384fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1385fa914e9cSVladimir Oltean 
1386b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
13876565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
138831350d7fSVladimir Oltean 
138931350d7fSVladimir Oltean 	/* Basic L2 initialization */
139031350d7fSVladimir Oltean 
13915bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
13925bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
13935bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
13945bc9d2e6SVladimir Oltean 	 */
13955bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
13965bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
13975bc9d2e6SVladimir Oltean 
13985bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
13995bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
14005bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
14015bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
14025bc9d2e6SVladimir Oltean 	mdelay(1);
14035bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
14045bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
14055bc9d2e6SVladimir Oltean 
14065bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1407a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
14085bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
14095bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1410a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
14115bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
14125bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
14135bc9d2e6SVladimir Oltean 
14145bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
14155bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
14165bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
14175bc9d2e6SVladimir Oltean 
1418e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1419541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1420e8e6e73dSVladimir Oltean 
142131350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
142231350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
142331350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
142431350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
142531350d7fSVladimir Oltean 
142631350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
142731350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
142831350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
142931350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
143031350d7fSVladimir Oltean 
143131350d7fSVladimir Oltean 	/* Enable vcap lookups */
143231350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
143331350d7fSVladimir Oltean }
14345e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
143531350d7fSVladimir Oltean 
14362d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
14372d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
14382d44b097SVladimir Oltean  * NPI mode is used).
143969df578cSVladimir Oltean  */
14402d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
144121468199SVladimir Oltean {
144269df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
144369df578cSVladimir Oltean 
144469df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
144521468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
144669df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
144769df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
144869df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
144969df578cSVladimir Oltean 	 */
145021468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
145121468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
145221468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
145321468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
145421468199SVladimir Oltean 
145569df578cSVladimir Oltean 	/* Enable CPU port module */
1456886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
145769df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1458886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
14592d44b097SVladimir Oltean 			    ocelot->xtr_prefix);
1460886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
14612d44b097SVladimir Oltean 			    ocelot->inj_prefix);
146221468199SVladimir Oltean 
146321468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
146421468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
146521468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
146621468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
146721468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
146821468199SVladimir Oltean }
146921468199SVladimir Oltean 
1470*f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
1471*f6fe01d6SVladimir Oltean {
1472*f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
1473*f6fe01d6SVladimir Oltean 
1474*f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
1475*f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
1476*f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
1477*f6fe01d6SVladimir Oltean 	 */
1478*f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
1479*f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
1480*f6fe01d6SVladimir Oltean 
1481*f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
1482*f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
1483*f6fe01d6SVladimir Oltean 
1484*f6fe01d6SVladimir Oltean 	dev_info(ocelot->dev,
1485*f6fe01d6SVladimir Oltean 		 "Detected %d bytes of packet buffer and %d frame references\n",
1486*f6fe01d6SVladimir Oltean 		 ocelot->packet_buffer_size, ocelot->num_frame_refs);
1487*f6fe01d6SVladimir Oltean }
1488*f6fe01d6SVladimir Oltean 
1489a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1490a556c76aSAlexandre Belloni {
1491a556c76aSAlexandre Belloni 	char queue_name[32];
149221468199SVladimir Oltean 	int i, ret;
149321468199SVladimir Oltean 	u32 port;
1494a556c76aSAlexandre Belloni 
14953a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
14963a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
14973a77b593SVladimir Oltean 		if (ret) {
14983a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
14993a77b593SVladimir Oltean 			return ret;
15003a77b593SVladimir Oltean 		}
15013a77b593SVladimir Oltean 	}
15023a77b593SVladimir Oltean 
1503dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1504dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
1505dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
1506dc96ee37SAlexandre Belloni 		return -ENOMEM;
1507dc96ee37SAlexandre Belloni 
1508a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1509a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1510a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1511a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1512a556c76aSAlexandre Belloni 		return -ENOMEM;
1513a556c76aSAlexandre Belloni 
1514a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
15154e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
15164e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1517a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1518a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1519a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1520a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1521a556c76aSAlexandre Belloni 		return -ENOMEM;
1522a556c76aSAlexandre Belloni 
1523ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1524ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
1525ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
1526ca0b272bSVladimir Oltean 		return -ENOMEM;
1527ca0b272bSVladimir Oltean 	}
1528ca0b272bSVladimir Oltean 
15292b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1530e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
1531*f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
1532a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1533a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1534aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
15352d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
1536a556c76aSAlexandre Belloni 
1537a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1538a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1539a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1540a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1541a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1542a556c76aSAlexandre Belloni 	}
1543a556c76aSAlexandre Belloni 
1544a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1545a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1546a556c76aSAlexandre Belloni 
1547a556c76aSAlexandre Belloni 	/* Aggregation mode */
1548a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1549a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1550a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1551a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1552a556c76aSAlexandre Belloni 
1553a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1554a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1555a556c76aSAlexandre Belloni 	 */
1556a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1557a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1558a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1559a556c76aSAlexandre Belloni 
1560a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1561a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1562a556c76aSAlexandre Belloni 
1563a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1564a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1565a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1566a556c76aSAlexandre Belloni 
1567a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1568edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
1569a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1570a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1571a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1572edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
1573a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1574a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1575a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1576a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1577a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1578a556c76aSAlexandre Belloni 
1579a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1580a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1581a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1582a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1583a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1584a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1585a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1586a556c76aSAlexandre Belloni 				 port);
1587a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1588a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1589a556c76aSAlexandre Belloni 	}
1590a556c76aSAlexandre Belloni 
1591a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
159296b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
1593a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1594a556c76aSAlexandre Belloni 
1595a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1596a556c76aSAlexandre Belloni 	}
1597a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
1598a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1599a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
1600a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1601a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1602a556c76aSAlexandre Belloni 
1603a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1604a556c76aSAlexandre Belloni 	 * registers endianness.
1605a556c76aSAlexandre Belloni 	 */
1606a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1607a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1608a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1609a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1610a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1611a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1612a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1613a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1614a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1615a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1616a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1617a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1618a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1619a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1620a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1621a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1622a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1623a556c76aSAlexandre Belloni 
16241e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1625a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1626a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
16274e3b0468SAntoine Tenart 
1628a556c76aSAlexandre Belloni 	return 0;
1629a556c76aSAlexandre Belloni }
1630a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1631a556c76aSAlexandre Belloni 
1632a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1633a556c76aSAlexandre Belloni {
1634c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
1635a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1636ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
1637a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
1638a556c76aSAlexandre Belloni }
1639a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1640a556c76aSAlexandre Belloni 
1641e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
1642e5fb512dSVladimir Oltean {
1643e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1644e5fb512dSVladimir Oltean 
1645e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
1646e5fb512dSVladimir Oltean }
1647e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
1648e5fb512dSVladimir Oltean 
1649a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1650