xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision e2b2e83e52f756decbaacd8202f28745bab49e07)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
7a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
820968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
9a556c76aSAlexandre Belloni #include "ocelot.h"
103c83654fSVladimir Oltean #include "ocelot_vcap.h"
11a556c76aSAlexandre Belloni 
12639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
13639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
14639c1b26SSteen Hegelund 
15a556c76aSAlexandre Belloni struct ocelot_mact_entry {
16a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
17a556c76aSAlexandre Belloni 	u16 vid;
18a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
19a556c76aSAlexandre Belloni };
20a556c76aSAlexandre Belloni 
21639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
22639c1b26SSteen Hegelund {
23639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
24639c1b26SSteen Hegelund }
25639c1b26SSteen Hegelund 
26a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
27a556c76aSAlexandre Belloni {
28639c1b26SSteen Hegelund 	u32 val;
29a556c76aSAlexandre Belloni 
30639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
31639c1b26SSteen Hegelund 		ocelot, val,
32639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
33639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
34639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
35a556c76aSAlexandre Belloni }
36a556c76aSAlexandre Belloni 
37a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
38a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
39a556c76aSAlexandre Belloni 			       unsigned int vid)
40a556c76aSAlexandre Belloni {
41a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
42a556c76aSAlexandre Belloni 
43a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
44a556c76aSAlexandre Belloni 	 * understood by the hardware.
45a556c76aSAlexandre Belloni 	 */
46a556c76aSAlexandre Belloni 	mach |= vid    << 16;
47a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
48a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
49a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
50a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
51a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
52a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
53a556c76aSAlexandre Belloni 
54a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
55a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
56a556c76aSAlexandre Belloni 
57a556c76aSAlexandre Belloni }
58a556c76aSAlexandre Belloni 
599c90eea3SVladimir Oltean int ocelot_mact_learn(struct ocelot *ocelot, int port,
60a556c76aSAlexandre Belloni 		      const unsigned char mac[ETH_ALEN],
619c90eea3SVladimir Oltean 		      unsigned int vid, enum macaccess_entry_type type)
62a556c76aSAlexandre Belloni {
63a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
64a556c76aSAlexandre Belloni 
65a556c76aSAlexandre Belloni 	/* Issue a write command */
66a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
67a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
68a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
69a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
70a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
71a556c76aSAlexandre Belloni 
72a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
73a556c76aSAlexandre Belloni }
749c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
75a556c76aSAlexandre Belloni 
769c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
779c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
78a556c76aSAlexandre Belloni {
79a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
80a556c76aSAlexandre Belloni 
81a556c76aSAlexandre Belloni 	/* Issue a forget command */
82a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
83a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
84a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
85a556c76aSAlexandre Belloni 
86a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
87a556c76aSAlexandre Belloni }
889c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
89a556c76aSAlexandre Belloni 
90a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
91a556c76aSAlexandre Belloni {
92a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
93a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
94a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
95a556c76aSAlexandre Belloni 	 */
96a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
97a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
98a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
99a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
100a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
101a556c76aSAlexandre Belloni 
102a556c76aSAlexandre Belloni 	/* Clear the MAC table */
103a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
104a556c76aSAlexandre Belloni }
105a556c76aSAlexandre Belloni 
106f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
107b5962294SHoratiu Vultur {
108b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
109b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
110f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
11175944fdaSXiaoliang Yang 
11275944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
11375944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
1142f17c050SXiaoliang Yang 
1152f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
1162f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
1172f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
118b5962294SHoratiu Vultur }
119b5962294SHoratiu Vultur 
120639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
121639c1b26SSteen Hegelund {
122639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
123639c1b26SSteen Hegelund }
124639c1b26SSteen Hegelund 
125a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
126a556c76aSAlexandre Belloni {
127639c1b26SSteen Hegelund 	u32 val;
128a556c76aSAlexandre Belloni 
129639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
130639c1b26SSteen Hegelund 		ocelot,
131639c1b26SSteen Hegelund 		val,
132639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
133639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
134639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
135a556c76aSAlexandre Belloni }
136a556c76aSAlexandre Belloni 
1377142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1387142529fSAntoine Tenart {
1397142529fSAntoine Tenart 	/* Select the VID to configure */
1407142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1417142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1427142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1437142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1447142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1457142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1467142529fSAntoine Tenart 
1477142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1487142529fSAntoine Tenart }
1497142529fSAntoine Tenart 
15097bb69e1SVladimir Oltean static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
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 
156*e2b2e83eSVladimir Oltean 	/* Deny changing the native VLAN, but always permit deleting it */
157*e2b2e83eSVladimir Oltean 	if (ocelot_port->native_vlan.vid != native_vlan.vid &&
158*e2b2e83eSVladimir Oltean 	    ocelot_port->native_vlan.valid && native_vlan.valid) {
15997bb69e1SVladimir Oltean 		dev_err(ocelot->dev,
16097bb69e1SVladimir Oltean 			"Port already has a native VLAN: %d\n",
161c3e58a75SVladimir Oltean 			ocelot_port->native_vlan.vid);
16297bb69e1SVladimir Oltean 		return -EBUSY;
16397bb69e1SVladimir Oltean 	}
164*e2b2e83eSVladimir Oltean 
165c3e58a75SVladimir Oltean 	ocelot_port->native_vlan = native_vlan;
16697bb69e1SVladimir Oltean 
167c3e58a75SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
1687142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
16997bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
17097bb69e1SVladimir Oltean 
171*e2b2e83eSVladimir Oltean 	if (ocelot_port->vlan_aware && !ocelot_port->native_vlan.valid)
17287b0f983SVladimir Oltean 		/* If port is vlan-aware and tagged, drop untagged and priority
17387b0f983SVladimir Oltean 		 * tagged frames.
17487b0f983SVladimir Oltean 		 */
17587b0f983SVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
17687b0f983SVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
17787b0f983SVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
17887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
17987b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
18087b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
18187b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
18287b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
18387b0f983SVladimir Oltean 
18487b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
185*e2b2e83eSVladimir Oltean 		if (native_vlan.valid)
18687b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
18787b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
18887b0f983SVladimir Oltean 		else
18987b0f983SVladimir Oltean 			/* Tag all frames */
19087b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
19187b0f983SVladimir Oltean 	} else {
19287b0f983SVladimir Oltean 		/* Port tagging disabled. */
19387b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
19487b0f983SVladimir Oltean 	}
19587b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
19687b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
19787b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
19887b0f983SVladimir Oltean 
19997bb69e1SVladimir Oltean 	return 0;
20097bb69e1SVladimir Oltean }
20197bb69e1SVladimir Oltean 
20275e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
203c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
204c3e58a75SVladimir Oltean 				 struct ocelot_vlan pvid_vlan)
20575e5a554SVladimir Oltean {
20675e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
20775e5a554SVladimir Oltean 
208c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
20975e5a554SVladimir Oltean 
21075e5a554SVladimir Oltean 	if (!ocelot_port->vlan_aware)
211c3e58a75SVladimir Oltean 		pvid_vlan.vid = 0;
21275e5a554SVladimir Oltean 
21375e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
214c3e58a75SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
21575e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
21675e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
21775e5a554SVladimir Oltean }
21875e5a554SVladimir Oltean 
2192e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
2202e554a7aSVladimir Oltean 			       bool vlan_aware, struct switchdev_trans *trans)
22187b0f983SVladimir Oltean {
22287b0f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
22387b0f983SVladimir Oltean 	u32 val;
22487b0f983SVladimir Oltean 
22570edfae1SVladimir Oltean 	if (switchdev_trans_ph_prepare(trans)) {
22670edfae1SVladimir Oltean 		struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
22770edfae1SVladimir Oltean 		struct ocelot_vcap_filter *filter;
22870edfae1SVladimir Oltean 
22970edfae1SVladimir Oltean 		list_for_each_entry(filter, &block->rules, list) {
23070edfae1SVladimir Oltean 			if (filter->ingress_port_mask & BIT(port) &&
23170edfae1SVladimir Oltean 			    filter->action.vid_replace_ena) {
23270edfae1SVladimir Oltean 				dev_err(ocelot->dev,
23370edfae1SVladimir Oltean 					"Cannot change VLAN state with vlan modify rules active\n");
23470edfae1SVladimir Oltean 				return -EBUSY;
23570edfae1SVladimir Oltean 			}
23670edfae1SVladimir Oltean 		}
23770edfae1SVladimir Oltean 
2382e554a7aSVladimir Oltean 		return 0;
23970edfae1SVladimir Oltean 	}
2402e554a7aSVladimir Oltean 
24187b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
24287b0f983SVladimir Oltean 
24387b0f983SVladimir Oltean 	if (vlan_aware)
24487b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24587b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
24687b0f983SVladimir Oltean 	else
24787b0f983SVladimir Oltean 		val = 0;
24887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
24987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
25087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
25187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
25287b0f983SVladimir Oltean 
253c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
254c3e58a75SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
2552e554a7aSVladimir Oltean 
2562e554a7aSVladimir Oltean 	return 0;
25787b0f983SVladimir Oltean }
25887b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
25987b0f983SVladimir Oltean 
2605e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2617142529fSAntoine Tenart 		    bool untagged)
2627142529fSAntoine Tenart {
2637142529fSAntoine Tenart 	int ret;
2647142529fSAntoine Tenart 
2657142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
26697bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2677142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2687142529fSAntoine Tenart 	if (ret)
2697142529fSAntoine Tenart 		return ret;
2707142529fSAntoine Tenart 
2717142529fSAntoine Tenart 	/* Default ingress vlan classification */
272c3e58a75SVladimir Oltean 	if (pvid) {
273c3e58a75SVladimir Oltean 		struct ocelot_vlan pvid_vlan;
274c3e58a75SVladimir Oltean 
275c3e58a75SVladimir Oltean 		pvid_vlan.vid = vid;
276*e2b2e83eSVladimir Oltean 		pvid_vlan.valid = true;
277c3e58a75SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
278c3e58a75SVladimir Oltean 	}
2797142529fSAntoine Tenart 
2807142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
28197bb69e1SVladimir Oltean 	if (untagged) {
282c3e58a75SVladimir Oltean 		struct ocelot_vlan native_vlan;
283c3e58a75SVladimir Oltean 
284c3e58a75SVladimir Oltean 		native_vlan.vid = vid;
285*e2b2e83eSVladimir Oltean 		native_vlan.valid = true;
286c3e58a75SVladimir Oltean 		ret = ocelot_port_set_native_vlan(ocelot, port, native_vlan);
28797bb69e1SVladimir Oltean 		if (ret)
28897bb69e1SVladimir Oltean 			return ret;
289b9cd75e6SVladimir Oltean 	}
2907142529fSAntoine Tenart 
2917142529fSAntoine Tenart 	return 0;
2927142529fSAntoine Tenart }
2935e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
2947142529fSAntoine Tenart 
2955e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
2969855934cSVladimir Oltean {
2979855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2989855934cSVladimir Oltean 	int ret;
2997142529fSAntoine Tenart 
3007142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
30197bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3027142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3037142529fSAntoine Tenart 	if (ret)
3047142529fSAntoine Tenart 		return ret;
3057142529fSAntoine Tenart 
3067142529fSAntoine Tenart 	/* Egress */
307c3e58a75SVladimir Oltean 	if (ocelot_port->native_vlan.vid == vid) {
308*e2b2e83eSVladimir Oltean 		struct ocelot_vlan native_vlan = {0};
309c3e58a75SVladimir Oltean 
310c3e58a75SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
311c3e58a75SVladimir Oltean 	}
3127142529fSAntoine Tenart 
3137142529fSAntoine Tenart 	return 0;
3147142529fSAntoine Tenart }
3155e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3167142529fSAntoine Tenart 
317a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
318a556c76aSAlexandre Belloni {
3197142529fSAntoine Tenart 	u16 port, vid;
3207142529fSAntoine Tenart 
321a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
322a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
323a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
324a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3257142529fSAntoine Tenart 
3267142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3277142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3287142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3297142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3307142529fSAntoine Tenart 	}
3317142529fSAntoine Tenart 
3327142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3337142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3347142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3357142529fSAntoine Tenart 	 */
3367142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3377142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3387142529fSAntoine Tenart 
3397142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3407142529fSAntoine Tenart 	 * default.
3417142529fSAntoine Tenart 	 */
342714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
343714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3447142529fSAntoine Tenart 
3457142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3467142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3477142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3487142529fSAntoine Tenart 	}
349a556c76aSAlexandre Belloni }
350a556c76aSAlexandre Belloni 
3515e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
35226f4dbabSVladimir Oltean 			struct phy_device *phydev)
353a556c76aSAlexandre Belloni {
35426f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3555bc9d2e6SVladimir Oltean 	int speed, mode = 0;
356a556c76aSAlexandre Belloni 
35726f4dbabSVladimir Oltean 	switch (phydev->speed) {
358a556c76aSAlexandre Belloni 	case SPEED_10:
359a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
360a556c76aSAlexandre Belloni 		break;
361a556c76aSAlexandre Belloni 	case SPEED_100:
362a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
363a556c76aSAlexandre Belloni 		break;
364a556c76aSAlexandre Belloni 	case SPEED_1000:
365a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
366a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
367a556c76aSAlexandre Belloni 		break;
368a556c76aSAlexandre Belloni 	case SPEED_2500:
369a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
370a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
371a556c76aSAlexandre Belloni 		break;
372a556c76aSAlexandre Belloni 	default:
37326f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
37426f4dbabSVladimir Oltean 			port, phydev->speed);
375a556c76aSAlexandre Belloni 		return;
376a556c76aSAlexandre Belloni 	}
377a556c76aSAlexandre Belloni 
37826f4dbabSVladimir Oltean 	phy_print_status(phydev);
379a556c76aSAlexandre Belloni 
38026f4dbabSVladimir Oltean 	if (!phydev->link)
381a556c76aSAlexandre Belloni 		return;
382a556c76aSAlexandre Belloni 
383a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
384004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
385a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
386a556c76aSAlexandre Belloni 
3871ba8f656SVladimir Oltean 	/* Disable HDX fast control */
3881ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
3891ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
3901ba8f656SVladimir Oltean 
3911ba8f656SVladimir Oltean 	/* SGMII only for now */
3921ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
3931ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
3941ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
3951ba8f656SVladimir Oltean 
3961ba8f656SVladimir Oltean 	/* Enable PCS */
3971ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
3981ba8f656SVladimir Oltean 
3991ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4001ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4011ba8f656SVladimir Oltean 
4021ba8f656SVladimir Oltean 	/* No loopback */
4031ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
404a556c76aSAlexandre Belloni 
405a556c76aSAlexandre Belloni 	/* Enable MAC module */
406004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
407a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
408a556c76aSAlexandre Belloni 
409a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
410a556c76aSAlexandre Belloni 	 * reset */
411004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
412a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
413a556c76aSAlexandre Belloni 
414a556c76aSAlexandre Belloni 	/* No PFC */
415a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
416004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
417a556c76aSAlexandre Belloni 
418a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
419886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
420886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
421a556c76aSAlexandre Belloni 
422a556c76aSAlexandre Belloni 	/* Flow control */
423a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
424a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
425a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
426a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
427a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
428004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
429004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
430a556c76aSAlexandre Belloni }
4315e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
432a556c76aSAlexandre Belloni 
4335e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
434889b8950SVladimir Oltean 			struct phy_device *phy)
435a556c76aSAlexandre Belloni {
436a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
437a556c76aSAlexandre Belloni 	 * MAC addresses.
438a556c76aSAlexandre Belloni 	 */
439a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
440a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
441004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
442004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
443889b8950SVladimir Oltean }
4445e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
445889b8950SVladimir Oltean 
4465e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
447889b8950SVladimir Oltean {
448889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
449889b8950SVladimir Oltean 
450889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
451886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
452889b8950SVladimir Oltean }
4535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
454889b8950SVladimir Oltean 
455e2f9a8feSVladimir Oltean void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
456e2f9a8feSVladimir Oltean 				  struct sk_buff *clone)
457400928bfSYangbo Lu {
458e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
459400928bfSYangbo Lu 
4606565243cSVladimir Oltean 	spin_lock(&ocelot_port->ts_id_lock);
4616565243cSVladimir Oltean 
462e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
463b049da13SYangbo Lu 	/* Store timestamp ID in cb[0] of sk_buff */
464e2f9a8feSVladimir Oltean 	clone->cb[0] = ocelot_port->ts_id;
4656565243cSVladimir Oltean 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
466e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
4676565243cSVladimir Oltean 
4686565243cSVladimir Oltean 	spin_unlock(&ocelot_port->ts_id_lock);
469400928bfSYangbo Lu }
470400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
471400928bfSYangbo Lu 
472e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
473e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
4744e3b0468SAntoine Tenart {
4754e3b0468SAntoine Tenart 	unsigned long flags;
4764e3b0468SAntoine Tenart 	u32 val;
4774e3b0468SAntoine Tenart 
4784e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
4794e3b0468SAntoine Tenart 
4804e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
4814e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
4824e3b0468SAntoine Tenart 
4834e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
4844e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
4854e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
4864e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
4874e3b0468SAntoine Tenart 
4884e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
4894e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
4904e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
4914e3b0468SAntoine Tenart 
4924e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
4934e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
4944e3b0468SAntoine Tenart 		ts->tv_sec--;
4954e3b0468SAntoine Tenart 
4964e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
4974e3b0468SAntoine Tenart }
498e23a7b3eSYangbo Lu 
499e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
500e23a7b3eSYangbo Lu {
501e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
502e23a7b3eSYangbo Lu 
503e23a7b3eSYangbo Lu 	while (budget--) {
504b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
505e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
506e23a7b3eSYangbo Lu 		struct ocelot_port *port;
507e23a7b3eSYangbo Lu 		struct timespec64 ts;
508b049da13SYangbo Lu 		unsigned long flags;
509e23a7b3eSYangbo Lu 		u32 val, id, txport;
510e23a7b3eSYangbo Lu 
511e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
512e23a7b3eSYangbo Lu 
513e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
514e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
515e23a7b3eSYangbo Lu 			break;
516e23a7b3eSYangbo Lu 
517e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
518e23a7b3eSYangbo Lu 
519e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
520e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
521e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
522e23a7b3eSYangbo Lu 
523e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
524e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
525e23a7b3eSYangbo Lu 
526b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
527b049da13SYangbo Lu 
528b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
529b049da13SYangbo Lu 			if (skb->cb[0] != id)
530e23a7b3eSYangbo Lu 				continue;
531b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
532b049da13SYangbo Lu 			skb_match = skb;
533fc62c094SYangbo Lu 			break;
534e23a7b3eSYangbo Lu 		}
535e23a7b3eSYangbo Lu 
536b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
537b049da13SYangbo Lu 
5385fd82200Slaurent brando 		/* Get the h/w timestamp */
5395fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
540e23a7b3eSYangbo Lu 
541b049da13SYangbo Lu 		if (unlikely(!skb_match))
542e23a7b3eSYangbo Lu 			continue;
543e23a7b3eSYangbo Lu 
544e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
545e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
546e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
547e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
5485fd82200Slaurent brando 
5495fd82200Slaurent brando 		/* Next ts */
5505fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
551e23a7b3eSYangbo Lu 	}
552e23a7b3eSYangbo Lu }
553e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
5544e3b0468SAntoine Tenart 
5555e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
55687b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
557a556c76aSAlexandre Belloni {
558471beb11SVladimir Oltean 	int pgid = port;
559471beb11SVladimir Oltean 
560471beb11SVladimir Oltean 	if (port == ocelot->npi)
561471beb11SVladimir Oltean 		pgid = PGID_CPU;
562a556c76aSAlexandre Belloni 
563471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
564a556c76aSAlexandre Belloni }
5655e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
566a556c76aSAlexandre Belloni 
5675e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
568531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
569531ee1a6SVladimir Oltean {
570531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
571531ee1a6SVladimir Oltean }
5725e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
573531ee1a6SVladimir Oltean 
5749c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
575531ee1a6SVladimir Oltean 			    bool is_static, void *data)
576a556c76aSAlexandre Belloni {
577531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
578a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
579a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
580a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
581a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
582a556c76aSAlexandre Belloni 
583a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
584a556c76aSAlexandre Belloni 		goto skip;
585a556c76aSAlexandre Belloni 
586a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
587a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
588a556c76aSAlexandre Belloni 	if (!nlh)
589a556c76aSAlexandre Belloni 		return -EMSGSIZE;
590a556c76aSAlexandre Belloni 
591a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
592a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
593a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
594a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
595a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
596a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
597a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
598531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
599a556c76aSAlexandre Belloni 
600531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
601a556c76aSAlexandre Belloni 		goto nla_put_failure;
602a556c76aSAlexandre Belloni 
603531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
604a556c76aSAlexandre Belloni 		goto nla_put_failure;
605a556c76aSAlexandre Belloni 
606a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
607a556c76aSAlexandre Belloni 
608a556c76aSAlexandre Belloni skip:
609a556c76aSAlexandre Belloni 	dump->idx++;
610a556c76aSAlexandre Belloni 	return 0;
611a556c76aSAlexandre Belloni 
612a556c76aSAlexandre Belloni nla_put_failure:
613a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
614a556c76aSAlexandre Belloni 	return -EMSGSIZE;
615a556c76aSAlexandre Belloni }
6169c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
617a556c76aSAlexandre Belloni 
618531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
619a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
620a556c76aSAlexandre Belloni {
621a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
622531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
623a556c76aSAlexandre Belloni 
624a556c76aSAlexandre Belloni 	/* Set row and column to read from */
625a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
626a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
627a556c76aSAlexandre Belloni 
628a556c76aSAlexandre Belloni 	/* Issue a read command */
629a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
630a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
631a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
632a556c76aSAlexandre Belloni 
633a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
634a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
635a556c76aSAlexandre Belloni 
636a556c76aSAlexandre Belloni 	/* Read the entry flags */
637a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
638a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
639a556c76aSAlexandre Belloni 		return -EINVAL;
640a556c76aSAlexandre Belloni 
641a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
642a556c76aSAlexandre Belloni 	 * do not report it.
643a556c76aSAlexandre Belloni 	 */
644a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
645531ee1a6SVladimir Oltean 	if (dst != port)
646a556c76aSAlexandre Belloni 		return -EINVAL;
647a556c76aSAlexandre Belloni 
648a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
649a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
650a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
651a556c76aSAlexandre Belloni 
652a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
653a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
654a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
655a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
656a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
657a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
658a556c76aSAlexandre Belloni 
659a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
660a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
661a556c76aSAlexandre Belloni 
662a556c76aSAlexandre Belloni 	return 0;
663a556c76aSAlexandre Belloni }
664a556c76aSAlexandre Belloni 
6655e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
666531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
667a556c76aSAlexandre Belloni {
668531ee1a6SVladimir Oltean 	int i, j;
669a556c76aSAlexandre Belloni 
67021ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
67121ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
672a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
673531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
674531ee1a6SVladimir Oltean 			bool is_static;
675531ee1a6SVladimir Oltean 			int ret;
676531ee1a6SVladimir Oltean 
677531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
678a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
679a556c76aSAlexandre Belloni 			 * skip it.
680a556c76aSAlexandre Belloni 			 */
681a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
682a556c76aSAlexandre Belloni 				continue;
683a556c76aSAlexandre Belloni 			else if (ret)
684531ee1a6SVladimir Oltean 				return ret;
685a556c76aSAlexandre Belloni 
686531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
687531ee1a6SVladimir Oltean 
688531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
689a556c76aSAlexandre Belloni 			if (ret)
690531ee1a6SVladimir Oltean 				return ret;
691a556c76aSAlexandre Belloni 		}
692a556c76aSAlexandre Belloni 	}
693a556c76aSAlexandre Belloni 
694531ee1a6SVladimir Oltean 	return 0;
695531ee1a6SVladimir Oltean }
6965e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
697531ee1a6SVladimir Oltean 
698f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
6994e3b0468SAntoine Tenart {
7004e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
7014e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
7024e3b0468SAntoine Tenart }
703f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
7044e3b0468SAntoine Tenart 
705f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
7064e3b0468SAntoine Tenart {
707306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
7084e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
7094e3b0468SAntoine Tenart 
7104e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
7114e3b0468SAntoine Tenart 		return -EFAULT;
7124e3b0468SAntoine Tenart 
7134e3b0468SAntoine Tenart 	/* reserved for future extensions */
7144e3b0468SAntoine Tenart 	if (cfg.flags)
7154e3b0468SAntoine Tenart 		return -EINVAL;
7164e3b0468SAntoine Tenart 
7174e3b0468SAntoine Tenart 	/* Tx type sanity check */
7184e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
7194e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
720306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
7214e3b0468SAntoine Tenart 		break;
7224e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
7234e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
7244e3b0468SAntoine Tenart 		 * need to update the origin time.
7254e3b0468SAntoine Tenart 		 */
726306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
7274e3b0468SAntoine Tenart 		break;
7284e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
729306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
7304e3b0468SAntoine Tenart 		break;
7314e3b0468SAntoine Tenart 	default:
7324e3b0468SAntoine Tenart 		return -ERANGE;
7334e3b0468SAntoine Tenart 	}
7344e3b0468SAntoine Tenart 
7354e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
7364e3b0468SAntoine Tenart 
7374e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
7384e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
7394e3b0468SAntoine Tenart 		break;
7404e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
7414e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
7424e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
7434e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
7444e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
7454e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
7464e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
7474e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
7484e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
7494e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
7504e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
7514e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
7524e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
7534e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
7544e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
7554e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
7564e3b0468SAntoine Tenart 		break;
7574e3b0468SAntoine Tenart 	default:
7584e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
7594e3b0468SAntoine Tenart 		return -ERANGE;
7604e3b0468SAntoine Tenart 	}
7614e3b0468SAntoine Tenart 
7624e3b0468SAntoine Tenart 	/* Commit back the result & save it */
7634e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
7644e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
7654e3b0468SAntoine Tenart 
7664e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
7674e3b0468SAntoine Tenart }
768f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
7694e3b0468SAntoine Tenart 
7705e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
771a556c76aSAlexandre Belloni {
772a556c76aSAlexandre Belloni 	int i;
773a556c76aSAlexandre Belloni 
774a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
775a556c76aSAlexandre Belloni 		return;
776a556c76aSAlexandre Belloni 
777a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
778a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
779a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
780a556c76aSAlexandre Belloni }
7815e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
782a556c76aSAlexandre Belloni 
7831e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
784a556c76aSAlexandre Belloni {
785a556c76aSAlexandre Belloni 	int i, j;
786a556c76aSAlexandre Belloni 
787a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
788a556c76aSAlexandre Belloni 
789a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
790a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
791a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
792a556c76aSAlexandre Belloni 
793a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
794a556c76aSAlexandre Belloni 			u32 val;
795a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
796a556c76aSAlexandre Belloni 
797a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
798a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
799a556c76aSAlexandre Belloni 
800a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
801a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
802a556c76aSAlexandre Belloni 
803a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
804a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
805a556c76aSAlexandre Belloni 		}
806a556c76aSAlexandre Belloni 	}
807a556c76aSAlexandre Belloni 
8081e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
8091e1caa97SClaudiu Manoil }
8101e1caa97SClaudiu Manoil 
8111e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
8121e1caa97SClaudiu Manoil {
8131e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
8141e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
8151e1caa97SClaudiu Manoil 					     stats_work);
8161e1caa97SClaudiu Manoil 
8171e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
8181e1caa97SClaudiu Manoil 
819a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
820a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
821a556c76aSAlexandre Belloni }
822a556c76aSAlexandre Belloni 
8235e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
824a556c76aSAlexandre Belloni {
825a556c76aSAlexandre Belloni 	int i;
826a556c76aSAlexandre Belloni 
827a556c76aSAlexandre Belloni 	/* check and update now */
8281e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
829a556c76aSAlexandre Belloni 
830a556c76aSAlexandre Belloni 	/* Copy all counters */
831a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
832004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
833a556c76aSAlexandre Belloni }
8345e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
835a556c76aSAlexandre Belloni 
8365e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
837c7282d38SVladimir Oltean {
838a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
839a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
840c7282d38SVladimir Oltean 
841a556c76aSAlexandre Belloni 	return ocelot->num_stats;
842a556c76aSAlexandre Belloni }
8435e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
844a556c76aSAlexandre Belloni 
8455e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
846c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
847c7282d38SVladimir Oltean {
8484e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
8494e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
850d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
851d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
852d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
853d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
854d2b09a8eSYangbo Lu 		return 0;
855d2b09a8eSYangbo Lu 	}
8564e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
8574e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
8584e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
8594e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
8604e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
8614e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
8624e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
8634e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
8644e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
8654e3b0468SAntoine Tenart 
8664e3b0468SAntoine Tenart 	return 0;
8674e3b0468SAntoine Tenart }
8685e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
8694e3b0468SAntoine Tenart 
8705e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
871a556c76aSAlexandre Belloni {
872a556c76aSAlexandre Belloni 	u32 port_cfg;
8734bda1415SVladimir Oltean 	int p, i;
874a556c76aSAlexandre Belloni 
8754bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
8764bda1415SVladimir Oltean 		return;
877a556c76aSAlexandre Belloni 
8784bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
879a556c76aSAlexandre Belloni 
880a556c76aSAlexandre Belloni 	switch (state) {
881a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
8824bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
883df561f66SGustavo A. R. Silva 		fallthrough;
884a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
885a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
886a556c76aSAlexandre Belloni 		break;
887a556c76aSAlexandre Belloni 
888a556c76aSAlexandre Belloni 	default:
889a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
8904bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
891a556c76aSAlexandre Belloni 		break;
892a556c76aSAlexandre Belloni 	}
893a556c76aSAlexandre Belloni 
8944bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
895a556c76aSAlexandre Belloni 
896a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
897a556c76aSAlexandre Belloni 	 * a source for the other ports.
898a556c76aSAlexandre Belloni 	 */
8994bda1415SVladimir Oltean 	for (p = 0; p < ocelot->num_phys_ports; p++) {
90069df578cSVladimir Oltean 		if (ocelot->bridge_fwd_mask & BIT(p)) {
9014bda1415SVladimir Oltean 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
902a556c76aSAlexandre Belloni 
903a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
904a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
905a556c76aSAlexandre Belloni 
906a556c76aSAlexandre Belloni 				if (!bond_mask)
907a556c76aSAlexandre Belloni 					continue;
908a556c76aSAlexandre Belloni 
9094bda1415SVladimir Oltean 				if (bond_mask & BIT(p)) {
910a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
911a556c76aSAlexandre Belloni 					break;
912a556c76aSAlexandre Belloni 				}
913a556c76aSAlexandre Belloni 			}
914a556c76aSAlexandre Belloni 
915c9d2203bSVladimir Oltean 			ocelot_write_rix(ocelot, mask,
9164bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
917a556c76aSAlexandre Belloni 		} else {
91869df578cSVladimir Oltean 			ocelot_write_rix(ocelot, 0,
9194bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
9204bda1415SVladimir Oltean 		}
921a556c76aSAlexandre Belloni 	}
922a556c76aSAlexandre Belloni }
9235e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
924a556c76aSAlexandre Belloni 
9255e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
9264bda1415SVladimir Oltean {
927c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
928c0d7eccbSVladimir Oltean 
929c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
930c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
931c0d7eccbSVladimir Oltean 	 */
932c0d7eccbSVladimir Oltean 	if (!age_period)
933c0d7eccbSVladimir Oltean 		age_period = 1;
934c0d7eccbSVladimir Oltean 
935c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
936a556c76aSAlexandre Belloni }
9375e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
938a556c76aSAlexandre Belloni 
939a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
940a556c76aSAlexandre Belloni 						     const unsigned char *addr,
941a556c76aSAlexandre Belloni 						     u16 vid)
942a556c76aSAlexandre Belloni {
943a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
944a556c76aSAlexandre Belloni 
945a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
946a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
947a556c76aSAlexandre Belloni 			return mc;
948a556c76aSAlexandre Belloni 	}
949a556c76aSAlexandre Belloni 
950a556c76aSAlexandre Belloni 	return NULL;
951a556c76aSAlexandre Belloni }
952a556c76aSAlexandre Belloni 
9539403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
9549403c158SVladimir Oltean {
9559403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
9569403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
9579403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
9589403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
9597c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
9609403c158SVladimir Oltean }
9619403c158SVladimir Oltean 
962e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
963e5d1f896SVladimir Oltean 					     unsigned long ports)
964e5d1f896SVladimir Oltean {
965e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
966e5d1f896SVladimir Oltean 
967e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
968e5d1f896SVladimir Oltean 	if (!pgid)
969e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
970e5d1f896SVladimir Oltean 
971e5d1f896SVladimir Oltean 	pgid->ports = ports;
972e5d1f896SVladimir Oltean 	pgid->index = index;
973e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
974e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
975e5d1f896SVladimir Oltean 
976e5d1f896SVladimir Oltean 	return pgid;
977e5d1f896SVladimir Oltean }
978e5d1f896SVladimir Oltean 
979e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
980e5d1f896SVladimir Oltean {
981e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
982e5d1f896SVladimir Oltean 		return;
983e5d1f896SVladimir Oltean 
984e5d1f896SVladimir Oltean 	list_del(&pgid->list);
985e5d1f896SVladimir Oltean 	kfree(pgid);
986e5d1f896SVladimir Oltean }
987e5d1f896SVladimir Oltean 
988e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
989bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
9909403c158SVladimir Oltean {
991e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
992e5d1f896SVladimir Oltean 	int index;
9939403c158SVladimir Oltean 
9949403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
9959403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
9969403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
9979403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
9989403c158SVladimir Oltean 	 */
999bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1000bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1001e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
10029403c158SVladimir Oltean 
1003e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1004e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1005e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1006e5d1f896SVladimir Oltean 		 */
1007e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1008e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1009e5d1f896SVladimir Oltean 			return pgid;
1010e5d1f896SVladimir Oltean 		}
1011e5d1f896SVladimir Oltean 	}
1012e5d1f896SVladimir Oltean 
1013e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1014e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
10159403c158SVladimir Oltean 		bool used = false;
10169403c158SVladimir Oltean 
1017e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1018e5d1f896SVladimir Oltean 			if (pgid->index == index) {
10199403c158SVladimir Oltean 				used = true;
10209403c158SVladimir Oltean 				break;
10219403c158SVladimir Oltean 			}
10229403c158SVladimir Oltean 		}
10239403c158SVladimir Oltean 
10249403c158SVladimir Oltean 		if (!used)
1025e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
10269403c158SVladimir Oltean 	}
10279403c158SVladimir Oltean 
1028e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
10299403c158SVladimir Oltean }
10309403c158SVladimir Oltean 
10319403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1032bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
10339403c158SVladimir Oltean {
1034ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
10359403c158SVladimir Oltean 
1036bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
10379403c158SVladimir Oltean 		addr[0] = 0;
10389403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
10399403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1040bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
10419403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
10429403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
10439403c158SVladimir Oltean 	}
10449403c158SVladimir Oltean }
10459403c158SVladimir Oltean 
1046209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1047209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1048a556c76aSAlexandre Belloni {
1049a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1050004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1051e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1052a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1053a556c76aSAlexandre Belloni 
1054471beb11SVladimir Oltean 	if (port == ocelot->npi)
1055471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1056471beb11SVladimir Oltean 
1057a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1058a556c76aSAlexandre Belloni 	if (!mc) {
1059728e69aeSVladimir Oltean 		/* New entry */
1060bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1061bb8d53fdSVladimir Oltean 		if (!mc)
1062bb8d53fdSVladimir Oltean 			return -ENOMEM;
1063bb8d53fdSVladimir Oltean 
1064bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1065bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1066bb8d53fdSVladimir Oltean 		mc->vid = vid;
1067bb8d53fdSVladimir Oltean 
1068a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1069728e69aeSVladimir Oltean 	} else {
1070e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1071e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1072e5d1f896SVladimir Oltean 		 */
1073e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1074bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1075a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1076a556c76aSAlexandre Belloni 	}
1077a556c76aSAlexandre Belloni 
1078004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1079e5d1f896SVladimir Oltean 
1080e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1081e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1082e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1083e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1084e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1085e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1086e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1087e5d1f896SVladimir Oltean 	}
1088e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1089e5d1f896SVladimir Oltean 
1090bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1091a556c76aSAlexandre Belloni 
1092e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1093e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1094e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1095e5d1f896SVladimir Oltean 				 pgid->index);
1096e5d1f896SVladimir Oltean 
1097e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1098bb8d53fdSVladimir Oltean 				 mc->entry_type);
1099a556c76aSAlexandre Belloni }
1100209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1101a556c76aSAlexandre Belloni 
1102209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1103a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1104a556c76aSAlexandre Belloni {
1105a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1106004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1107e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1108a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1109a556c76aSAlexandre Belloni 
1110471beb11SVladimir Oltean 	if (port == ocelot->npi)
1111471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1112471beb11SVladimir Oltean 
1113a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1114a556c76aSAlexandre Belloni 	if (!mc)
1115a556c76aSAlexandre Belloni 		return -ENOENT;
1116a556c76aSAlexandre Belloni 
1117bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1118a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1119a556c76aSAlexandre Belloni 
1120e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1121004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1122a556c76aSAlexandre Belloni 	if (!mc->ports) {
1123a556c76aSAlexandre Belloni 		list_del(&mc->list);
1124a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1125a556c76aSAlexandre Belloni 		return 0;
1126a556c76aSAlexandre Belloni 	}
1127a556c76aSAlexandre Belloni 
1128e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1129e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1130e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1131e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1132e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1133e5d1f896SVladimir Oltean 
1134bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1135a556c76aSAlexandre Belloni 
1136e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1137e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1138e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1139e5d1f896SVladimir Oltean 				 pgid->index);
1140e5d1f896SVladimir Oltean 
1141e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1142bb8d53fdSVladimir Oltean 				 mc->entry_type);
1143a556c76aSAlexandre Belloni }
1144209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1145a556c76aSAlexandre Belloni 
11465e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1147a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1148a556c76aSAlexandre Belloni {
1149a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1150a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1151a556c76aSAlexandre Belloni 	} else {
1152a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1153a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1154a556c76aSAlexandre Belloni 			 * unsupported */
1155a556c76aSAlexandre Belloni 			return -ENODEV;
1156a556c76aSAlexandre Belloni 	}
1157a556c76aSAlexandre Belloni 
1158f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1159a556c76aSAlexandre Belloni 
1160a556c76aSAlexandre Belloni 	return 0;
1161a556c76aSAlexandre Belloni }
11625e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1163a556c76aSAlexandre Belloni 
11645e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1165a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1166a556c76aSAlexandre Belloni {
1167c3e58a75SVladimir Oltean 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
11682e554a7aSVladimir Oltean 	struct switchdev_trans trans;
11692e554a7aSVladimir Oltean 	int ret;
11702e554a7aSVladimir Oltean 
117197bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1172a556c76aSAlexandre Belloni 
1173a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1174a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
11757142529fSAntoine Tenart 
11762e554a7aSVladimir Oltean 	trans.ph_prepare = true;
11772e554a7aSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
11782e554a7aSVladimir Oltean 	if (ret)
11792e554a7aSVladimir Oltean 		return ret;
11802e554a7aSVladimir Oltean 
11812e554a7aSVladimir Oltean 	trans.ph_prepare = false;
11822e554a7aSVladimir Oltean 	ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
11832e554a7aSVladimir Oltean 	if (ret)
11842e554a7aSVladimir Oltean 		return ret;
11852e554a7aSVladimir Oltean 
1186c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, pvid);
1187c3e58a75SVladimir Oltean 	return ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1188a556c76aSAlexandre Belloni }
11895e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1190a556c76aSAlexandre Belloni 
1191dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1192dc96ee37SAlexandre Belloni {
1193dc96ee37SAlexandre Belloni 	int i, port, lag;
1194dc96ee37SAlexandre Belloni 
1195dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
119696b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1197dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1198dc96ee37SAlexandre Belloni 
119996b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1200dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1201dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1202dc96ee37SAlexandre Belloni 
1203dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1204dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1205dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1206dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1207dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1208dc96ee37SAlexandre Belloni 
1209dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1210dc96ee37SAlexandre Belloni 		if (!bond_mask)
1211dc96ee37SAlexandre Belloni 			continue;
1212dc96ee37SAlexandre Belloni 
1213dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1214dc96ee37SAlexandre Belloni 			// Destination mask
1215dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1216dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1217dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1218dc96ee37SAlexandre Belloni 			aggr_count++;
1219dc96ee37SAlexandre Belloni 		}
1220dc96ee37SAlexandre Belloni 
122196b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1222dc96ee37SAlexandre Belloni 			u32 ac;
1223dc96ee37SAlexandre Belloni 
1224dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1225dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1226dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1227dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1228dc96ee37SAlexandre Belloni 		}
1229dc96ee37SAlexandre Belloni 	}
1230dc96ee37SAlexandre Belloni }
1231dc96ee37SAlexandre Belloni 
1232dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1233dc96ee37SAlexandre Belloni {
1234dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1235dc96ee37SAlexandre Belloni 	unsigned int p;
1236dc96ee37SAlexandre Belloni 
1237dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1238dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1239dc96ee37SAlexandre Belloni 
1240dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1241dc96ee37SAlexandre Belloni 
1242dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1243dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1244dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1245dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1246dc96ee37SAlexandre Belloni 	}
1247dc96ee37SAlexandre Belloni }
1248dc96ee37SAlexandre Belloni 
12499c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1250dc96ee37SAlexandre Belloni 			 struct net_device *bond)
1251dc96ee37SAlexandre Belloni {
1252dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1253dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1254f270dbfaSVladimir Oltean 	int lag, lp;
1255dc96ee37SAlexandre Belloni 
1256dc96ee37SAlexandre Belloni 	rcu_read_lock();
1257dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1258004d44f6SVladimir Oltean 		struct ocelot_port_private *priv = netdev_priv(ndev);
1259dc96ee37SAlexandre Belloni 
1260004d44f6SVladimir Oltean 		bond_mask |= BIT(priv->chip_port);
1261dc96ee37SAlexandre Belloni 	}
1262dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1263dc96ee37SAlexandre Belloni 
1264dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1265dc96ee37SAlexandre Belloni 
1266dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1267dc96ee37SAlexandre Belloni 	 * now on
1268dc96ee37SAlexandre Belloni 	 */
1269f270dbfaSVladimir Oltean 	if (port == lp) {
1270f270dbfaSVladimir Oltean 		lag = port;
1271f270dbfaSVladimir Oltean 		ocelot->lags[port] = bond_mask;
1272f270dbfaSVladimir Oltean 		bond_mask &= ~BIT(port);
1273dc96ee37SAlexandre Belloni 		if (bond_mask) {
1274dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1275dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1276dc96ee37SAlexandre Belloni 		}
1277dc96ee37SAlexandre Belloni 	} else {
1278dc96ee37SAlexandre Belloni 		lag = lp;
1279f270dbfaSVladimir Oltean 		ocelot->lags[lp] |= BIT(port);
1280dc96ee37SAlexandre Belloni 	}
1281dc96ee37SAlexandre Belloni 
1282dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1283dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1284dc96ee37SAlexandre Belloni 
1285dc96ee37SAlexandre Belloni 	return 0;
1286dc96ee37SAlexandre Belloni }
12879c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
1288dc96ee37SAlexandre Belloni 
12899c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1290dc96ee37SAlexandre Belloni 			   struct net_device *bond)
1291dc96ee37SAlexandre Belloni {
1292dc96ee37SAlexandre Belloni 	u32 port_cfg;
1293dc96ee37SAlexandre Belloni 	int i;
1294dc96ee37SAlexandre Belloni 
1295dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1296dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1297f270dbfaSVladimir Oltean 		ocelot->lags[i] &= ~BIT(port);
1298dc96ee37SAlexandre Belloni 
1299dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1300dc96ee37SAlexandre Belloni 	 * next port
1301dc96ee37SAlexandre Belloni 	 */
1302f270dbfaSVladimir Oltean 	if (ocelot->lags[port]) {
1303f270dbfaSVladimir Oltean 		int n = __ffs(ocelot->lags[port]);
1304dc96ee37SAlexandre Belloni 
1305f270dbfaSVladimir Oltean 		ocelot->lags[n] = ocelot->lags[port];
1306f270dbfaSVladimir Oltean 		ocelot->lags[port] = 0;
1307dc96ee37SAlexandre Belloni 
1308dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1309dc96ee37SAlexandre Belloni 	}
1310dc96ee37SAlexandre Belloni 
1311f270dbfaSVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1312dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1313f270dbfaSVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1314f270dbfaSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
1315dc96ee37SAlexandre Belloni 
1316dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1317dc96ee37SAlexandre Belloni }
13189c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
13190e332c85SPetr Machata 
1320a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1321a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
13220b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
13230b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
13240b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
1325a8015dedSVladimir Oltean  */
13260b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
132731350d7fSVladimir Oltean {
132831350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1329a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1330e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
1331601e984fSVladimir Oltean 	int atop, atop_tot;
133231350d7fSVladimir Oltean 
13330b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
13340b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
13350b912fc9SVladimir Oltean 
13360b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13370b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
13380b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
13390b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
13400b912fc9SVladimir Oltean 	}
13410b912fc9SVladimir Oltean 
1342a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1343fa914e9cSVladimir Oltean 
1344e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
1345e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1346e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1347541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1348541132f0SMaxim Kochetkov 			    pause_start);
1349541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1350541132f0SMaxim Kochetkov 			    pause_stop);
1351fa914e9cSVladimir Oltean 
1352601e984fSVladimir Oltean 	/* Tail dropping watermarks */
1353601e984fSVladimir Oltean 	atop_tot = (ocelot->shared_queue_sz - 9 * maxlen) /
1354a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
1355601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1356601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1357601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1358fa914e9cSVladimir Oltean }
13590b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
13600b912fc9SVladimir Oltean 
13610b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
13620b912fc9SVladimir Oltean {
13630b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
13640b912fc9SVladimir Oltean 
13650b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
13660b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
13670b912fc9SVladimir Oltean 
13680b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
13690b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
13700b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
13710b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
13720b912fc9SVladimir Oltean 	}
13730b912fc9SVladimir Oltean 
13740b912fc9SVladimir Oltean 	return max_mtu;
13750b912fc9SVladimir Oltean }
13760b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
1377fa914e9cSVladimir Oltean 
13785e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
1379fa914e9cSVladimir Oltean {
1380fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1381fa914e9cSVladimir Oltean 
1382b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
13836565243cSVladimir Oltean 	spin_lock_init(&ocelot_port->ts_id_lock);
138431350d7fSVladimir Oltean 
138531350d7fSVladimir Oltean 	/* Basic L2 initialization */
138631350d7fSVladimir Oltean 
13875bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
13885bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
13895bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
13905bc9d2e6SVladimir Oltean 	 */
13915bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
13925bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
13935bc9d2e6SVladimir Oltean 
13945bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
13955bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
13965bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
13975bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
13985bc9d2e6SVladimir Oltean 	mdelay(1);
13995bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
14005bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
14015bc9d2e6SVladimir Oltean 
14025bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
1403a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
14045bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
14055bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1406a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
14075bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
14085bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
14095bc9d2e6SVladimir Oltean 
14105bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
14115bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
14125bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
14135bc9d2e6SVladimir Oltean 
1414e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
1415541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1416e8e6e73dSVladimir Oltean 
141731350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
141831350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
141931350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
142031350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
142131350d7fSVladimir Oltean 
142231350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
142331350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
142431350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
142531350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
142631350d7fSVladimir Oltean 
142731350d7fSVladimir Oltean 	/* Enable vcap lookups */
142831350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
142931350d7fSVladimir Oltean }
14305e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
143131350d7fSVladimir Oltean 
14322d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
14332d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
14342d44b097SVladimir Oltean  * NPI mode is used).
143569df578cSVladimir Oltean  */
14362d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
143721468199SVladimir Oltean {
143869df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
143969df578cSVladimir Oltean 
144069df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
144121468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
144269df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
144369df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
144469df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
144569df578cSVladimir Oltean 	 */
144621468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
144721468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
144821468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
144921468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
145021468199SVladimir Oltean 
145169df578cSVladimir Oltean 	/* Enable CPU port module */
1452886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
145369df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
1454886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
14552d44b097SVladimir Oltean 			    ocelot->xtr_prefix);
1456886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
14572d44b097SVladimir Oltean 			    ocelot->inj_prefix);
145821468199SVladimir Oltean 
145921468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
146021468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
146121468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
146221468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
146321468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
146421468199SVladimir Oltean }
146521468199SVladimir Oltean 
1466a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1467a556c76aSAlexandre Belloni {
1468a556c76aSAlexandre Belloni 	char queue_name[32];
146921468199SVladimir Oltean 	int i, ret;
147021468199SVladimir Oltean 	u32 port;
1471a556c76aSAlexandre Belloni 
14723a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
14733a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
14743a77b593SVladimir Oltean 		if (ret) {
14753a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
14763a77b593SVladimir Oltean 			return ret;
14773a77b593SVladimir Oltean 		}
14783a77b593SVladimir Oltean 	}
14793a77b593SVladimir Oltean 
1480dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1481dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
1482dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
1483dc96ee37SAlexandre Belloni 		return -ENOMEM;
1484dc96ee37SAlexandre Belloni 
1485a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1486a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1487a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1488a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1489a556c76aSAlexandre Belloni 		return -ENOMEM;
1490a556c76aSAlexandre Belloni 
1491a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
14924e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
14934e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
1494a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1495a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1496a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1497a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1498a556c76aSAlexandre Belloni 		return -ENOMEM;
1499a556c76aSAlexandre Belloni 
15002b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
1501e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
1502a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1503a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1504aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
15052d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
1506a556c76aSAlexandre Belloni 
1507a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1508a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1509a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1510a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1511a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1512a556c76aSAlexandre Belloni 	}
1513a556c76aSAlexandre Belloni 
1514a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1515a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1516a556c76aSAlexandre Belloni 
1517a556c76aSAlexandre Belloni 	/* Aggregation mode */
1518a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1519a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1520a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1521a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1522a556c76aSAlexandre Belloni 
1523a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1524a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1525a556c76aSAlexandre Belloni 	 */
1526a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1527a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1528a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1529a556c76aSAlexandre Belloni 
1530a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1531a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1532a556c76aSAlexandre Belloni 
1533a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1534a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1535a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1536a556c76aSAlexandre Belloni 
1537a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1538a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1539a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1540a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1541a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
1542a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1543a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1544a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1545a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1546a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1547a556c76aSAlexandre Belloni 
1548a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1549a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1550a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1551a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1552a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1553a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1554a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1555a556c76aSAlexandre Belloni 				 port);
1556a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1557a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1558a556c76aSAlexandre Belloni 	}
1559a556c76aSAlexandre Belloni 
1560a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
156196b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
1562a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1563a556c76aSAlexandre Belloni 
1564a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1565a556c76aSAlexandre Belloni 	}
1566a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
1567a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1568a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
1569a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1570a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1571a556c76aSAlexandre Belloni 
1572a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1573a556c76aSAlexandre Belloni 	 * registers endianness.
1574a556c76aSAlexandre Belloni 	 */
1575a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1576a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1577a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1578a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1579a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1580a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1581a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1582a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1583a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1584a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1585a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1586a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1587a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1588a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1589a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1590a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1591a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1592a556c76aSAlexandre Belloni 
15931e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1594a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1595a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
15964e3b0468SAntoine Tenart 
1597a556c76aSAlexandre Belloni 	return 0;
1598a556c76aSAlexandre Belloni }
1599a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1600a556c76aSAlexandre Belloni 
1601a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1602a556c76aSAlexandre Belloni {
1603c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
1604a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1605a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
1606a556c76aSAlexandre Belloni }
1607a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1608a556c76aSAlexandre Belloni 
1609e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
1610e5fb512dSVladimir Oltean {
1611e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1612e5fb512dSVladimir Oltean 
1613e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
1614e5fb512dSVladimir Oltean }
1615e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
1616e5fb512dSVladimir Oltean 
1617a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1618