xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 2e359b00a11715c7a89d7448e6e4cb4d84543520)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
8a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
920968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
10a556c76aSAlexandre Belloni #include "ocelot.h"
113c83654fSVladimir Oltean #include "ocelot_vcap.h"
12a556c76aSAlexandre Belloni 
13639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
14639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
1554c31984SVladimir Oltean #define OCELOT_RSV_VLAN_RANGE_START 4000
16639c1b26SSteen Hegelund 
17a556c76aSAlexandre Belloni struct ocelot_mact_entry {
18a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
19a556c76aSAlexandre Belloni 	u16 vid;
20a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
21a556c76aSAlexandre Belloni };
22a556c76aSAlexandre Belloni 
232468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
24639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
25639c1b26SSteen Hegelund {
26639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
27639c1b26SSteen Hegelund }
28639c1b26SSteen Hegelund 
292468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
30a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
31a556c76aSAlexandre Belloni {
32639c1b26SSteen Hegelund 	u32 val;
33a556c76aSAlexandre Belloni 
34639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
35639c1b26SSteen Hegelund 		ocelot, val,
36639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
37639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
38639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
39a556c76aSAlexandre Belloni }
40a556c76aSAlexandre Belloni 
412468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
42a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
43a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
44a556c76aSAlexandre Belloni 			       unsigned int vid)
45a556c76aSAlexandre Belloni {
46a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
47a556c76aSAlexandre Belloni 
48a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
49a556c76aSAlexandre Belloni 	 * understood by the hardware.
50a556c76aSAlexandre Belloni 	 */
51a556c76aSAlexandre Belloni 	mach |= vid    << 16;
52a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
53a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
54a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
55a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
56a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
57a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
58a556c76aSAlexandre Belloni 
59a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
60a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
61a556c76aSAlexandre Belloni 
62a556c76aSAlexandre Belloni }
63a556c76aSAlexandre Belloni 
640568c3bfSXiaoliang Yang static int __ocelot_mact_learn(struct ocelot *ocelot, int port,
65a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
669c90eea3SVladimir Oltean 			       unsigned int vid, enum macaccess_entry_type type)
67a556c76aSAlexandre Belloni {
68584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
69584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
70584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
71584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
72584b7cfcSAlban Bedel 	unsigned int mc_ports;
732468346cSVladimir Oltean 	int err;
74584b7cfcSAlban Bedel 
75584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
76584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
77584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
78584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
79584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
80584b7cfcSAlban Bedel 	else
81584b7cfcSAlban Bedel 		mc_ports = 0;
82584b7cfcSAlban Bedel 
83584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
84584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
85584b7cfcSAlban Bedel 
86a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
87a556c76aSAlexandre Belloni 
88a556c76aSAlexandre Belloni 	/* Issue a write command */
89584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
90a556c76aSAlexandre Belloni 
912468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
922468346cSVladimir Oltean 
930568c3bfSXiaoliang Yang 	return err;
940568c3bfSXiaoliang Yang }
950568c3bfSXiaoliang Yang 
960568c3bfSXiaoliang Yang int ocelot_mact_learn(struct ocelot *ocelot, int port,
970568c3bfSXiaoliang Yang 		      const unsigned char mac[ETH_ALEN],
980568c3bfSXiaoliang Yang 		      unsigned int vid, enum macaccess_entry_type type)
990568c3bfSXiaoliang Yang {
1000568c3bfSXiaoliang Yang 	int ret;
1010568c3bfSXiaoliang Yang 
1020568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1030568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, port, mac, vid, type);
1042468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1052468346cSVladimir Oltean 
1060568c3bfSXiaoliang Yang 	return ret;
107a556c76aSAlexandre Belloni }
1089c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
109a556c76aSAlexandre Belloni 
1109c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
1119c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
112a556c76aSAlexandre Belloni {
1132468346cSVladimir Oltean 	int err;
1142468346cSVladimir Oltean 
1152468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
1162468346cSVladimir Oltean 
117a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
118a556c76aSAlexandre Belloni 
119a556c76aSAlexandre Belloni 	/* Issue a forget command */
120a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
121a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
122a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
123a556c76aSAlexandre Belloni 
1242468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
1252468346cSVladimir Oltean 
1262468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1272468346cSVladimir Oltean 
1282468346cSVladimir Oltean 	return err;
129a556c76aSAlexandre Belloni }
1309c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
131a556c76aSAlexandre Belloni 
1320568c3bfSXiaoliang Yang int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx,
1330568c3bfSXiaoliang Yang 		       const unsigned char mac[ETH_ALEN],
1340568c3bfSXiaoliang Yang 		       unsigned int vid, enum macaccess_entry_type *type)
1350568c3bfSXiaoliang Yang {
1360568c3bfSXiaoliang Yang 	int val;
1370568c3bfSXiaoliang Yang 
1380568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1390568c3bfSXiaoliang Yang 
1400568c3bfSXiaoliang Yang 	ocelot_mact_select(ocelot, mac, vid);
1410568c3bfSXiaoliang Yang 
1420568c3bfSXiaoliang Yang 	/* Issue a read command with MACACCESS_VALID=1. */
1430568c3bfSXiaoliang Yang 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
1440568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1450568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS);
1460568c3bfSXiaoliang Yang 
1470568c3bfSXiaoliang Yang 	if (ocelot_mact_wait_for_completion(ocelot)) {
1480568c3bfSXiaoliang Yang 		mutex_unlock(&ocelot->mact_lock);
1490568c3bfSXiaoliang Yang 		return -ETIMEDOUT;
1500568c3bfSXiaoliang Yang 	}
1510568c3bfSXiaoliang Yang 
1520568c3bfSXiaoliang Yang 	/* Read back the entry flags */
1530568c3bfSXiaoliang Yang 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1540568c3bfSXiaoliang Yang 
1550568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1560568c3bfSXiaoliang Yang 
1570568c3bfSXiaoliang Yang 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1580568c3bfSXiaoliang Yang 		return -ENOENT;
1590568c3bfSXiaoliang Yang 
1600568c3bfSXiaoliang Yang 	*dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val);
1610568c3bfSXiaoliang Yang 	*type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val);
1620568c3bfSXiaoliang Yang 
1630568c3bfSXiaoliang Yang 	return 0;
1640568c3bfSXiaoliang Yang }
1650568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_lookup);
1660568c3bfSXiaoliang Yang 
1670568c3bfSXiaoliang Yang int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx,
1680568c3bfSXiaoliang Yang 				 const unsigned char mac[ETH_ALEN],
1690568c3bfSXiaoliang Yang 				 unsigned int vid,
1700568c3bfSXiaoliang Yang 				 enum macaccess_entry_type type,
1710568c3bfSXiaoliang Yang 				 int sfid, int ssid)
1720568c3bfSXiaoliang Yang {
1730568c3bfSXiaoliang Yang 	int ret;
1740568c3bfSXiaoliang Yang 
1750568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1760568c3bfSXiaoliang Yang 
1770568c3bfSXiaoliang Yang 	ocelot_write(ocelot,
1780568c3bfSXiaoliang Yang 		     (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) |
1790568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SFID(sfid) |
1800568c3bfSXiaoliang Yang 		     (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) |
1810568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SSID(ssid),
1820568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA);
1830568c3bfSXiaoliang Yang 
1840568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type);
1850568c3bfSXiaoliang Yang 
1860568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1870568c3bfSXiaoliang Yang 
1880568c3bfSXiaoliang Yang 	return ret;
1890568c3bfSXiaoliang Yang }
1900568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_learn_streamdata);
1910568c3bfSXiaoliang Yang 
192a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
193a556c76aSAlexandre Belloni {
194a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
195a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
196a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
197a556c76aSAlexandre Belloni 	 */
198a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
199a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
200a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
201a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
202a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
203a556c76aSAlexandre Belloni 
2042468346cSVladimir Oltean 	/* Clear the MAC table. We are not concurrent with anyone, so
2052468346cSVladimir Oltean 	 * holding &ocelot->mact_lock is pointless.
2062468346cSVladimir Oltean 	 */
207a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
208a556c76aSAlexandre Belloni }
209a556c76aSAlexandre Belloni 
210f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
211b5962294SHoratiu Vultur {
212b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
213b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
214f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
21575944fdaSXiaoliang Yang 
21675944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
21775944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
2182f17c050SXiaoliang Yang 
2192f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
2202f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
2212f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
222b5962294SHoratiu Vultur }
223b5962294SHoratiu Vultur 
22454c31984SVladimir Oltean static int ocelot_single_vlan_aware_bridge(struct ocelot *ocelot,
22554c31984SVladimir Oltean 					   struct netlink_ext_ack *extack)
22654c31984SVladimir Oltean {
22754c31984SVladimir Oltean 	struct net_device *bridge = NULL;
22854c31984SVladimir Oltean 	int port;
22954c31984SVladimir Oltean 
23054c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
23154c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
23254c31984SVladimir Oltean 
23354c31984SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bridge ||
23454c31984SVladimir Oltean 		    !br_vlan_enabled(ocelot_port->bridge))
23554c31984SVladimir Oltean 			continue;
23654c31984SVladimir Oltean 
23754c31984SVladimir Oltean 		if (!bridge) {
23854c31984SVladimir Oltean 			bridge = ocelot_port->bridge;
23954c31984SVladimir Oltean 			continue;
24054c31984SVladimir Oltean 		}
24154c31984SVladimir Oltean 
24254c31984SVladimir Oltean 		if (bridge == ocelot_port->bridge)
24354c31984SVladimir Oltean 			continue;
24454c31984SVladimir Oltean 
24554c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
24654c31984SVladimir Oltean 				   "Only one VLAN-aware bridge is supported");
24754c31984SVladimir Oltean 		return -EBUSY;
24854c31984SVladimir Oltean 	}
24954c31984SVladimir Oltean 
25054c31984SVladimir Oltean 	return 0;
25154c31984SVladimir Oltean }
25254c31984SVladimir Oltean 
253639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
254639c1b26SSteen Hegelund {
255639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
256639c1b26SSteen Hegelund }
257639c1b26SSteen Hegelund 
258a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
259a556c76aSAlexandre Belloni {
260639c1b26SSteen Hegelund 	u32 val;
261a556c76aSAlexandre Belloni 
262639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
263639c1b26SSteen Hegelund 		ocelot,
264639c1b26SSteen Hegelund 		val,
265639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
266639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
267639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
268a556c76aSAlexandre Belloni }
269a556c76aSAlexandre Belloni 
2707142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
2717142529fSAntoine Tenart {
2727142529fSAntoine Tenart 	/* Select the VID to configure */
2737142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
2747142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
2757142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
2767142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
2777142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
2787142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
2797142529fSAntoine Tenart 
2807142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
2817142529fSAntoine Tenart }
2827142529fSAntoine Tenart 
2830da1a1c4SVladimir Oltean static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
2840da1a1c4SVladimir Oltean {
2850da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
2860da1a1c4SVladimir Oltean 	int num_untagged = 0;
2870da1a1c4SVladimir Oltean 
2880da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
2890da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
2900da1a1c4SVladimir Oltean 			continue;
2910da1a1c4SVladimir Oltean 
2920da1a1c4SVladimir Oltean 		if (vlan->untagged & BIT(port))
2930da1a1c4SVladimir Oltean 			num_untagged++;
2940da1a1c4SVladimir Oltean 	}
2950da1a1c4SVladimir Oltean 
2960da1a1c4SVladimir Oltean 	return num_untagged;
2970da1a1c4SVladimir Oltean }
2980da1a1c4SVladimir Oltean 
2990da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
3000da1a1c4SVladimir Oltean {
3010da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3020da1a1c4SVladimir Oltean 	int num_tagged = 0;
3030da1a1c4SVladimir Oltean 
3040da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
3050da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
3060da1a1c4SVladimir Oltean 			continue;
3070da1a1c4SVladimir Oltean 
3080da1a1c4SVladimir Oltean 		if (!(vlan->untagged & BIT(port)))
3090da1a1c4SVladimir Oltean 			num_tagged++;
3100da1a1c4SVladimir Oltean 	}
3110da1a1c4SVladimir Oltean 
3120da1a1c4SVladimir Oltean 	return num_tagged;
3130da1a1c4SVladimir Oltean }
3140da1a1c4SVladimir Oltean 
3150da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
3160da1a1c4SVladimir Oltean  * _one_ egress-untagged VLAN (_the_ native VLAN)
3170da1a1c4SVladimir Oltean  */
3180da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
3190da1a1c4SVladimir Oltean {
3200da1a1c4SVladimir Oltean 	return ocelot_port_num_tagged_vlans(ocelot, port) &&
3210da1a1c4SVladimir Oltean 	       ocelot_port_num_untagged_vlans(ocelot, port) == 1;
3220da1a1c4SVladimir Oltean }
3230da1a1c4SVladimir Oltean 
3240da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan *
3250da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
3260da1a1c4SVladimir Oltean {
3270da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3280da1a1c4SVladimir Oltean 
3290da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
3300da1a1c4SVladimir Oltean 		if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
3310da1a1c4SVladimir Oltean 			return vlan;
3320da1a1c4SVladimir Oltean 
3330da1a1c4SVladimir Oltean 	return NULL;
3340da1a1c4SVladimir Oltean }
3350da1a1c4SVladimir Oltean 
3360da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
3370da1a1c4SVladimir Oltean  * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
3380da1a1c4SVladimir Oltean  * state of the port.
3390da1a1c4SVladimir Oltean  */
3400da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
34197bb69e1SVladimir Oltean {
34297bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
34362a22bcbSVladimir Oltean 	enum ocelot_port_tag_config tag_cfg;
3440da1a1c4SVladimir Oltean 	bool uses_native_vlan = false;
34597bb69e1SVladimir Oltean 
34687b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
3470da1a1c4SVladimir Oltean 		uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
3480da1a1c4SVladimir Oltean 
3490da1a1c4SVladimir Oltean 		if (uses_native_vlan)
35062a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_NATIVE;
3510da1a1c4SVladimir Oltean 		else if (ocelot_port_num_untagged_vlans(ocelot, port))
3520da1a1c4SVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_DISABLED;
35387b0f983SVladimir Oltean 		else
35462a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_TRUNK;
35587b0f983SVladimir Oltean 	} else {
35662a22bcbSVladimir Oltean 		tag_cfg = OCELOT_PORT_TAG_DISABLED;
35787b0f983SVladimir Oltean 	}
3580da1a1c4SVladimir Oltean 
35962a22bcbSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
36087b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
36187b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
3620da1a1c4SVladimir Oltean 
3630da1a1c4SVladimir Oltean 	if (uses_native_vlan) {
3640da1a1c4SVladimir Oltean 		struct ocelot_bridge_vlan *native_vlan;
3650da1a1c4SVladimir Oltean 
3660da1a1c4SVladimir Oltean 		/* Not having a native VLAN is impossible, because
3670da1a1c4SVladimir Oltean 		 * ocelot_port_num_untagged_vlans has returned 1.
3680da1a1c4SVladimir Oltean 		 * So there is no use in checking for NULL here.
3690da1a1c4SVladimir Oltean 		 */
3700da1a1c4SVladimir Oltean 		native_vlan = ocelot_port_find_native_vlan(ocelot, port);
3710da1a1c4SVladimir Oltean 
3720da1a1c4SVladimir Oltean 		ocelot_rmw_gix(ocelot,
3730da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
3740da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID_M,
3750da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG, port);
3760da1a1c4SVladimir Oltean 	}
37797bb69e1SVladimir Oltean }
37897bb69e1SVladimir Oltean 
37954c31984SVladimir Oltean int ocelot_bridge_num_find(struct ocelot *ocelot,
38054c31984SVladimir Oltean 			   const struct net_device *bridge)
38154c31984SVladimir Oltean {
38254c31984SVladimir Oltean 	int port;
38354c31984SVladimir Oltean 
38454c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
38554c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
38654c31984SVladimir Oltean 
38754c31984SVladimir Oltean 		if (ocelot_port && ocelot_port->bridge == bridge)
38854c31984SVladimir Oltean 			return ocelot_port->bridge_num;
38954c31984SVladimir Oltean 	}
39054c31984SVladimir Oltean 
39154c31984SVladimir Oltean 	return -1;
39254c31984SVladimir Oltean }
39354c31984SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bridge_num_find);
39454c31984SVladimir Oltean 
39554c31984SVladimir Oltean static u16 ocelot_vlan_unaware_pvid(struct ocelot *ocelot,
39654c31984SVladimir Oltean 				    const struct net_device *bridge)
39754c31984SVladimir Oltean {
39854c31984SVladimir Oltean 	int bridge_num;
39954c31984SVladimir Oltean 
40054c31984SVladimir Oltean 	/* Standalone ports use VID 0 */
40154c31984SVladimir Oltean 	if (!bridge)
40254c31984SVladimir Oltean 		return 0;
40354c31984SVladimir Oltean 
40454c31984SVladimir Oltean 	bridge_num = ocelot_bridge_num_find(ocelot, bridge);
40554c31984SVladimir Oltean 	if (WARN_ON(bridge_num < 0))
40654c31984SVladimir Oltean 		return 0;
40754c31984SVladimir Oltean 
40854c31984SVladimir Oltean 	/* VLAN-unaware bridges use a reserved VID going from 4095 downwards */
40954c31984SVladimir Oltean 	return VLAN_N_VID - bridge_num - 1;
41054c31984SVladimir Oltean }
41154c31984SVladimir Oltean 
41275e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
413c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
414d4004422SVladimir Oltean 				 const struct ocelot_bridge_vlan *pvid_vlan)
41575e5a554SVladimir Oltean {
41675e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
41754c31984SVladimir Oltean 	u16 pvid = ocelot_vlan_unaware_pvid(ocelot, ocelot_port->bridge);
418be0576feSVladimir Oltean 	u32 val = 0;
41975e5a554SVladimir Oltean 
420c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
42175e5a554SVladimir Oltean 
422d4004422SVladimir Oltean 	if (ocelot_port->vlan_aware && pvid_vlan)
423d4004422SVladimir Oltean 		pvid = pvid_vlan->vid;
42475e5a554SVladimir Oltean 
42575e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
426d4004422SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
42775e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
42875e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
429be0576feSVladimir Oltean 
430be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
431be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
432be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
433be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
434be0576feSVladimir Oltean 	 */
435d4004422SVladimir Oltean 	if (!pvid_vlan && ocelot_port->vlan_aware)
436be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
437be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
438be0576feSVladimir Oltean 
439be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
440be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
441be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
442be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
44375e5a554SVladimir Oltean }
44475e5a554SVladimir Oltean 
44590e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
44690e0aa8dSVladimir Oltean 							  u16 vid)
447bbf6a2d9SVladimir Oltean {
44890e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
449bbf6a2d9SVladimir Oltean 
45090e0aa8dSVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
45190e0aa8dSVladimir Oltean 		if (vlan->vid == vid)
45290e0aa8dSVladimir Oltean 			return vlan;
453bbf6a2d9SVladimir Oltean 
45490e0aa8dSVladimir Oltean 	return NULL;
455bbf6a2d9SVladimir Oltean }
456bbf6a2d9SVladimir Oltean 
4570da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
4580da1a1c4SVladimir Oltean 				  bool untagged)
459bbf6a2d9SVladimir Oltean {
46090e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
46190e0aa8dSVladimir Oltean 	unsigned long portmask;
46290e0aa8dSVladimir Oltean 	int err;
46390e0aa8dSVladimir Oltean 
46490e0aa8dSVladimir Oltean 	if (vlan) {
46590e0aa8dSVladimir Oltean 		portmask = vlan->portmask | BIT(port);
46690e0aa8dSVladimir Oltean 
46790e0aa8dSVladimir Oltean 		err = ocelot_vlant_set_mask(ocelot, vid, portmask);
46890e0aa8dSVladimir Oltean 		if (err)
46990e0aa8dSVladimir Oltean 			return err;
47090e0aa8dSVladimir Oltean 
47190e0aa8dSVladimir Oltean 		vlan->portmask = portmask;
4720da1a1c4SVladimir Oltean 		/* Bridge VLANs can be overwritten with a different
4730da1a1c4SVladimir Oltean 		 * egress-tagging setting, so make sure to override an untagged
4740da1a1c4SVladimir Oltean 		 * with a tagged VID if that's going on.
4750da1a1c4SVladimir Oltean 		 */
4760da1a1c4SVladimir Oltean 		if (untagged)
4770da1a1c4SVladimir Oltean 			vlan->untagged |= BIT(port);
4780da1a1c4SVladimir Oltean 		else
4790da1a1c4SVladimir Oltean 			vlan->untagged &= ~BIT(port);
48090e0aa8dSVladimir Oltean 
48190e0aa8dSVladimir Oltean 		return 0;
48290e0aa8dSVladimir Oltean 	}
48390e0aa8dSVladimir Oltean 
48490e0aa8dSVladimir Oltean 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
48590e0aa8dSVladimir Oltean 	if (!vlan)
48690e0aa8dSVladimir Oltean 		return -ENOMEM;
48790e0aa8dSVladimir Oltean 
48890e0aa8dSVladimir Oltean 	portmask = BIT(port);
48990e0aa8dSVladimir Oltean 
49090e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
49190e0aa8dSVladimir Oltean 	if (err) {
49290e0aa8dSVladimir Oltean 		kfree(vlan);
49390e0aa8dSVladimir Oltean 		return err;
49490e0aa8dSVladimir Oltean 	}
49590e0aa8dSVladimir Oltean 
49690e0aa8dSVladimir Oltean 	vlan->vid = vid;
49790e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
4980da1a1c4SVladimir Oltean 	if (untagged)
4990da1a1c4SVladimir Oltean 		vlan->untagged = BIT(port);
50090e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&vlan->list);
50190e0aa8dSVladimir Oltean 	list_add_tail(&vlan->list, &ocelot->vlans);
50290e0aa8dSVladimir Oltean 
50390e0aa8dSVladimir Oltean 	return 0;
504bbf6a2d9SVladimir Oltean }
505bbf6a2d9SVladimir Oltean 
506bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
507bbf6a2d9SVladimir Oltean {
50890e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
50990e0aa8dSVladimir Oltean 	unsigned long portmask;
51090e0aa8dSVladimir Oltean 	int err;
51190e0aa8dSVladimir Oltean 
51290e0aa8dSVladimir Oltean 	if (!vlan)
51390e0aa8dSVladimir Oltean 		return 0;
51490e0aa8dSVladimir Oltean 
51590e0aa8dSVladimir Oltean 	portmask = vlan->portmask & ~BIT(port);
51690e0aa8dSVladimir Oltean 
51790e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
51890e0aa8dSVladimir Oltean 	if (err)
51990e0aa8dSVladimir Oltean 		return err;
52090e0aa8dSVladimir Oltean 
52190e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
52290e0aa8dSVladimir Oltean 	if (vlan->portmask)
52390e0aa8dSVladimir Oltean 		return 0;
52490e0aa8dSVladimir Oltean 
52590e0aa8dSVladimir Oltean 	list_del(&vlan->list);
52690e0aa8dSVladimir Oltean 	kfree(vlan);
52790e0aa8dSVladimir Oltean 
52890e0aa8dSVladimir Oltean 	return 0;
529bbf6a2d9SVladimir Oltean }
530bbf6a2d9SVladimir Oltean 
53154c31984SVladimir Oltean static int ocelot_add_vlan_unaware_pvid(struct ocelot *ocelot, int port,
53254c31984SVladimir Oltean 					const struct net_device *bridge)
53354c31984SVladimir Oltean {
53454c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
53554c31984SVladimir Oltean 
53654c31984SVladimir Oltean 	return ocelot_vlan_member_add(ocelot, port, vid, true);
53754c31984SVladimir Oltean }
53854c31984SVladimir Oltean 
53954c31984SVladimir Oltean static int ocelot_del_vlan_unaware_pvid(struct ocelot *ocelot, int port,
54054c31984SVladimir Oltean 					const struct net_device *bridge)
54154c31984SVladimir Oltean {
54254c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
54354c31984SVladimir Oltean 
54454c31984SVladimir Oltean 	return ocelot_vlan_member_del(ocelot, port, vid);
54554c31984SVladimir Oltean }
54654c31984SVladimir Oltean 
5472e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
5483b95d1b2SVladimir Oltean 			       bool vlan_aware, struct netlink_ext_ack *extack)
54987b0f983SVladimir Oltean {
55070edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
551bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
55270edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
5531fcb8fb3SVladimir Oltean 	int err = 0;
554bae33f2bSVladimir Oltean 	u32 val;
55570edfae1SVladimir Oltean 
55670edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
55770edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
55870edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
5593b95d1b2SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
5603b95d1b2SVladimir Oltean 					   "Cannot change VLAN state with vlan modify rules active");
56170edfae1SVladimir Oltean 			return -EBUSY;
56270edfae1SVladimir Oltean 		}
56370edfae1SVladimir Oltean 	}
56470edfae1SVladimir Oltean 
56554c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
56654c31984SVladimir Oltean 	if (err)
56754c31984SVladimir Oltean 		return err;
56854c31984SVladimir Oltean 
56954c31984SVladimir Oltean 	if (vlan_aware)
57054c31984SVladimir Oltean 		err = ocelot_del_vlan_unaware_pvid(ocelot, port,
57154c31984SVladimir Oltean 						   ocelot_port->bridge);
5721fcb8fb3SVladimir Oltean 	else if (ocelot_port->bridge)
57354c31984SVladimir Oltean 		err = ocelot_add_vlan_unaware_pvid(ocelot, port,
57454c31984SVladimir Oltean 						   ocelot_port->bridge);
57554c31984SVladimir Oltean 	if (err)
57654c31984SVladimir Oltean 		return err;
57754c31984SVladimir Oltean 
57887b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
57987b0f983SVladimir Oltean 
58087b0f983SVladimir Oltean 	if (vlan_aware)
58187b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
58287b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
58387b0f983SVladimir Oltean 	else
58487b0f983SVladimir Oltean 		val = 0;
58587b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
58687b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
58787b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
58887b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
58987b0f983SVladimir Oltean 
590c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
5910da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
5922e554a7aSVladimir Oltean 
5932e554a7aSVladimir Oltean 	return 0;
59487b0f983SVladimir Oltean }
59587b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
59687b0f983SVladimir Oltean 
5972f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
59801af940eSVladimir Oltean 			bool untagged, struct netlink_ext_ack *extack)
5992f0402feSVladimir Oltean {
6000da1a1c4SVladimir Oltean 	if (untagged) {
6010da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6020da1a1c4SVladimir Oltean 		if (ocelot_port_uses_native_vlan(ocelot, port)) {
60301af940eSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6040da1a1c4SVladimir Oltean 					   "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
6052f0402feSVladimir Oltean 			return -EBUSY;
6062f0402feSVladimir Oltean 		}
6070da1a1c4SVladimir Oltean 	} else {
6080da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6090da1a1c4SVladimir Oltean 		if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
6100da1a1c4SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6110da1a1c4SVladimir Oltean 					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
6120da1a1c4SVladimir Oltean 			return -EBUSY;
6130da1a1c4SVladimir Oltean 		}
6140da1a1c4SVladimir Oltean 	}
6152f0402feSVladimir Oltean 
61654c31984SVladimir Oltean 	if (vid > OCELOT_RSV_VLAN_RANGE_START) {
61754c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
61854c31984SVladimir Oltean 				   "VLAN range 4000-4095 reserved for VLAN-unaware bridging");
61954c31984SVladimir Oltean 		return -EBUSY;
62054c31984SVladimir Oltean 	}
62154c31984SVladimir Oltean 
6222f0402feSVladimir Oltean 	return 0;
6232f0402feSVladimir Oltean }
6242f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
6252f0402feSVladimir Oltean 
6265e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
6277142529fSAntoine Tenart 		    bool untagged)
6287142529fSAntoine Tenart {
629bbf6a2d9SVladimir Oltean 	int err;
6307142529fSAntoine Tenart 
6319323ac36SVladimir Oltean 	/* Ignore VID 0 added to our RX filter by the 8021q module, since
6329323ac36SVladimir Oltean 	 * that collides with OCELOT_STANDALONE_PVID and changes it from
6339323ac36SVladimir Oltean 	 * egress-untagged to egress-tagged.
6349323ac36SVladimir Oltean 	 */
6359323ac36SVladimir Oltean 	if (!vid)
6369323ac36SVladimir Oltean 		return 0;
6379323ac36SVladimir Oltean 
6380da1a1c4SVladimir Oltean 	err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
639bbf6a2d9SVladimir Oltean 	if (err)
640bbf6a2d9SVladimir Oltean 		return err;
6417142529fSAntoine Tenart 
6427142529fSAntoine Tenart 	/* Default ingress vlan classification */
643d4004422SVladimir Oltean 	if (pvid)
644d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port,
645d4004422SVladimir Oltean 				     ocelot_bridge_vlan_find(ocelot, vid));
6467142529fSAntoine Tenart 
6477142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
6480da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6497142529fSAntoine Tenart 
6507142529fSAntoine Tenart 	return 0;
6517142529fSAntoine Tenart }
6525e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
6537142529fSAntoine Tenart 
6545e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
6559855934cSVladimir Oltean {
6569855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
657ef576405SVladimir Oltean 	bool del_pvid = false;
658bbf6a2d9SVladimir Oltean 	int err;
6597142529fSAntoine Tenart 
6609323ac36SVladimir Oltean 	if (!vid)
6619323ac36SVladimir Oltean 		return 0;
6629323ac36SVladimir Oltean 
663ef576405SVladimir Oltean 	if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
664ef576405SVladimir Oltean 		del_pvid = true;
665ef576405SVladimir Oltean 
666bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
667bbf6a2d9SVladimir Oltean 	if (err)
668bbf6a2d9SVladimir Oltean 		return err;
6697142529fSAntoine Tenart 
670be0576feSVladimir Oltean 	/* Ingress */
671ef576405SVladimir Oltean 	if (del_pvid)
672d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, NULL);
673be0576feSVladimir Oltean 
6747142529fSAntoine Tenart 	/* Egress */
6750da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6767142529fSAntoine Tenart 
6777142529fSAntoine Tenart 	return 0;
6787142529fSAntoine Tenart }
6795e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
6807142529fSAntoine Tenart 
681a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
682a556c76aSAlexandre Belloni {
683bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
6847142529fSAntoine Tenart 	u16 port, vid;
6857142529fSAntoine Tenart 
686a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
687a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
688a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
689a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
6907142529fSAntoine Tenart 
6917142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
692bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
69390e0aa8dSVladimir Oltean 		ocelot_vlant_set_mask(ocelot, vid, 0);
6947142529fSAntoine Tenart 
69554c31984SVladimir Oltean 	/* We need VID 0 to get traffic on standalone ports.
69654c31984SVladimir Oltean 	 * It is added automatically if the 8021q module is loaded, but we
69754c31984SVladimir Oltean 	 * can't rely on that since it might not be.
6987142529fSAntoine Tenart 	 */
69954c31984SVladimir Oltean 	ocelot_vlant_set_mask(ocelot, OCELOT_STANDALONE_PVID, all_ports);
7007142529fSAntoine Tenart 
7017142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
7027142529fSAntoine Tenart 	 * default.
7037142529fSAntoine Tenart 	 */
704bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
7057142529fSAntoine Tenart 
7067142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
7077142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
7087142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
7097142529fSAntoine Tenart 	}
710a556c76aSAlexandre Belloni }
711a556c76aSAlexandre Belloni 
712eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
713eb4733d7SVladimir Oltean {
714eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
715eb4733d7SVladimir Oltean }
716eb4733d7SVladimir Oltean 
717e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
718eb4733d7SVladimir Oltean {
7191650bdb1SVladimir Oltean 	unsigned int pause_ena;
720eb4733d7SVladimir Oltean 	int err, val;
721eb4733d7SVladimir Oltean 
722eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
723eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
724eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
725eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
726eb4733d7SVladimir Oltean 
727eb4733d7SVladimir Oltean 	/* Disable flow control */
7281650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
729eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
730eb4733d7SVladimir Oltean 
731eb4733d7SVladimir Oltean 	/* Disable priority flow control */
732eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
733eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
734eb4733d7SVladimir Oltean 
735eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
736eb4733d7SVladimir Oltean 	 * at the port.
737eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
738eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
739eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
740eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
741eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
742eb4733d7SVladimir Oltean 	 */
743eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
744eb4733d7SVladimir Oltean 
745eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
746eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
747eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
748eb4733d7SVladimir Oltean 
749eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
750eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
751eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
752eb4733d7SVladimir Oltean 
753eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
754eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
755eb4733d7SVladimir Oltean 		       port);
756eb4733d7SVladimir Oltean 
757eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
758eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
759eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
760eb4733d7SVladimir Oltean 
761eb4733d7SVladimir Oltean 	/* Clear flushing again. */
762eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
763eb4733d7SVladimir Oltean 
7641650bdb1SVladimir Oltean 	/* Re-enable flow control */
7651650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
7661650bdb1SVladimir Oltean 
767eb4733d7SVladimir Oltean 	return err;
768eb4733d7SVladimir Oltean }
769eb4733d7SVladimir Oltean 
770e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
771e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
772e6e12df6SVladimir Oltean 				  phy_interface_t interface,
773e6e12df6SVladimir Oltean 				  unsigned long quirks)
774a556c76aSAlexandre Belloni {
77526f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
776e6e12df6SVladimir Oltean 	int err;
777a556c76aSAlexandre Belloni 
7788abe1970SVladimir Oltean 	ocelot_port->speed = SPEED_UNKNOWN;
7798abe1970SVladimir Oltean 
780e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
781e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
782e6e12df6SVladimir Oltean 
7838abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
7848abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
7858abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
7868abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
7878abe1970SVladimir Oltean 	}
7888abe1970SVladimir Oltean 
789e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
790e6e12df6SVladimir Oltean 
791e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
792e6e12df6SVladimir Oltean 	if (err)
793e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
794e6e12df6SVladimir Oltean 			port, err);
795e6e12df6SVladimir Oltean 
796e6e12df6SVladimir Oltean 	/* Put the port in reset. */
797e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
798e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
799e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
800e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
80174a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
802e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
80374a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
804e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
805e6e12df6SVladimir Oltean }
806e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
807e6e12df6SVladimir Oltean 
808e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
809e6e12df6SVladimir Oltean 				struct phy_device *phydev,
810e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
811e6e12df6SVladimir Oltean 				phy_interface_t interface,
812e6e12df6SVladimir Oltean 				int speed, int duplex,
813e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
814e6e12df6SVladimir Oltean 				unsigned long quirks)
815e6e12df6SVladimir Oltean {
816e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
817e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
818e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
819e6e12df6SVladimir Oltean 
8208abe1970SVladimir Oltean 	ocelot_port->speed = speed;
8218abe1970SVladimir Oltean 
822e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
823e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
824e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
825e6e12df6SVladimir Oltean 	 * (which is also its default value).
826e6e12df6SVladimir Oltean 	 */
827e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
828e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
829e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
830e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
831e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
832e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
833e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
834e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
835e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
836e6e12df6SVladimir Oltean 	} else {
837e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
838e6e12df6SVladimir Oltean 	}
839e6e12df6SVladimir Oltean 
840e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
841e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
842e6e12df6SVladimir Oltean 
843e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
844e6e12df6SVladimir Oltean 
845e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
846e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
847e6e12df6SVladimir Oltean 	 */
848e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
849e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
850e6e12df6SVladimir Oltean 
851e6e12df6SVladimir Oltean 	switch (speed) {
852a556c76aSAlexandre Belloni 	case SPEED_10:
853e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
854a556c76aSAlexandre Belloni 		break;
855a556c76aSAlexandre Belloni 	case SPEED_100:
856e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
857a556c76aSAlexandre Belloni 		break;
858a556c76aSAlexandre Belloni 	case SPEED_1000:
859a556c76aSAlexandre Belloni 	case SPEED_2500:
860e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
861a556c76aSAlexandre Belloni 		break;
862a556c76aSAlexandre Belloni 	default:
863e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
864e6e12df6SVladimir Oltean 			port, speed);
865a556c76aSAlexandre Belloni 		return;
866a556c76aSAlexandre Belloni 	}
867a556c76aSAlexandre Belloni 
868e6e12df6SVladimir Oltean 	/* Handle RX pause in all cases, with 2500base-X this is used for rate
869e6e12df6SVladimir Oltean 	 * adaptation.
870e6e12df6SVladimir Oltean 	 */
871e6e12df6SVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
872a556c76aSAlexandre Belloni 
873e6e12df6SVladimir Oltean 	if (tx_pause)
874e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
875e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
876e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
877e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
878a556c76aSAlexandre Belloni 
879e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
880e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
881e6e12df6SVladimir Oltean 	 */
882e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
883a556c76aSAlexandre Belloni 
884e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
8851ba8f656SVladimir Oltean 
88633cb0ff3SVladimir Oltean 	/* Don't attempt to send PAUSE frames on the NPI port, it's broken */
88733cb0ff3SVladimir Oltean 	if (port != ocelot->npi)
88833cb0ff3SVladimir Oltean 		ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
88933cb0ff3SVladimir Oltean 				    tx_pause);
8901ba8f656SVladimir Oltean 
891e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
892e6e12df6SVladimir Oltean 	 * enable MAC module
893e6e12df6SVladimir Oltean 	 */
894004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
895a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
896a556c76aSAlexandre Belloni 
8978abe1970SVladimir Oltean 	/* If the port supports cut-through forwarding, update the masks before
8988abe1970SVladimir Oltean 	 * enabling forwarding on the port.
8998abe1970SVladimir Oltean 	 */
9008abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
9018abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
9028abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
9038abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
9048abe1970SVladimir Oltean 	}
9058abe1970SVladimir Oltean 
906a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
907886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
908886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
909a556c76aSAlexandre Belloni }
910e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
911889b8950SVladimir Oltean 
912924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
913924ee317SVladimir Oltean 				u32 *rval)
914924ee317SVladimir Oltean {
915924ee317SVladimir Oltean 	u32 bytes_valid, val;
916924ee317SVladimir Oltean 
917924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
918924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
919924ee317SVladimir Oltean 		if (ifh)
920924ee317SVladimir Oltean 			return -EIO;
921924ee317SVladimir Oltean 
922924ee317SVladimir Oltean 		do {
923924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
924924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
925924ee317SVladimir Oltean 	}
926924ee317SVladimir Oltean 
927924ee317SVladimir Oltean 	switch (val) {
928924ee317SVladimir Oltean 	case XTR_ABORT:
929924ee317SVladimir Oltean 		return -EIO;
930924ee317SVladimir Oltean 	case XTR_EOF_0:
931924ee317SVladimir Oltean 	case XTR_EOF_1:
932924ee317SVladimir Oltean 	case XTR_EOF_2:
933924ee317SVladimir Oltean 	case XTR_EOF_3:
934924ee317SVladimir Oltean 	case XTR_PRUNED:
935924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
936924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
937924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
938924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
939924ee317SVladimir Oltean 		else
940924ee317SVladimir Oltean 			*rval = val;
941924ee317SVladimir Oltean 
942924ee317SVladimir Oltean 		return bytes_valid;
943924ee317SVladimir Oltean 	case XTR_ESCAPE:
944924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
945924ee317SVladimir Oltean 
946924ee317SVladimir Oltean 		return 4;
947924ee317SVladimir Oltean 	default:
948924ee317SVladimir Oltean 		*rval = val;
949924ee317SVladimir Oltean 
950924ee317SVladimir Oltean 		return 4;
951924ee317SVladimir Oltean 	}
952924ee317SVladimir Oltean }
953924ee317SVladimir Oltean 
954924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
955924ee317SVladimir Oltean {
956924ee317SVladimir Oltean 	int i, err = 0;
957924ee317SVladimir Oltean 
958924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
959924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
960924ee317SVladimir Oltean 		if (err != 4)
961924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
962924ee317SVladimir Oltean 	}
963924ee317SVladimir Oltean 
964924ee317SVladimir Oltean 	return 0;
965924ee317SVladimir Oltean }
966924ee317SVladimir Oltean 
967b471a71eSClément Léger void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
968b471a71eSClément Léger 			     u64 timestamp)
969924ee317SVladimir Oltean {
970924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
9712ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
972b471a71eSClément Léger 	struct timespec64 ts;
973b471a71eSClément Léger 
974b471a71eSClément Léger 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
975b471a71eSClément Léger 
976b471a71eSClément Léger 	tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
977b471a71eSClément Léger 	if ((tod_in_ns & 0xffffffff) < timestamp)
978b471a71eSClément Léger 		full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
979b471a71eSClément Léger 				timestamp;
980b471a71eSClément Léger 	else
981b471a71eSClément Léger 		full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
982b471a71eSClément Léger 				timestamp;
983b471a71eSClément Léger 
984b471a71eSClément Léger 	shhwtstamps = skb_hwtstamps(skb);
985b471a71eSClément Léger 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
986b471a71eSClément Léger 	shhwtstamps->hwtstamp = full_ts_in_ns;
987b471a71eSClément Léger }
988b471a71eSClément Léger EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
989b471a71eSClément Léger 
990b471a71eSClément Léger int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
991b471a71eSClément Léger {
992924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
993924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
994924ee317SVladimir Oltean 	struct net_device *dev;
995924ee317SVladimir Oltean 	struct sk_buff *skb;
996924ee317SVladimir Oltean 	int sz, buf_len;
997924ee317SVladimir Oltean 	u32 val, *buf;
998924ee317SVladimir Oltean 	int err;
999924ee317SVladimir Oltean 
1000924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1001924ee317SVladimir Oltean 	if (err)
1002924ee317SVladimir Oltean 		return err;
1003924ee317SVladimir Oltean 
1004924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
1005924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
1006924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1007924ee317SVladimir Oltean 
1008924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1009924ee317SVladimir Oltean 		return -EINVAL;
1010924ee317SVladimir Oltean 
1011924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1012924ee317SVladimir Oltean 	if (!dev)
1013924ee317SVladimir Oltean 		return -EINVAL;
1014924ee317SVladimir Oltean 
1015924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
1016924ee317SVladimir Oltean 	if (unlikely(!skb)) {
1017924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
1018924ee317SVladimir Oltean 		return -ENOMEM;
1019924ee317SVladimir Oltean 	}
1020924ee317SVladimir Oltean 
1021924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
1022924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
1023924ee317SVladimir Oltean 
1024924ee317SVladimir Oltean 	len = 0;
1025924ee317SVladimir Oltean 	do {
1026924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1027924ee317SVladimir Oltean 		if (sz < 0) {
1028924ee317SVladimir Oltean 			err = sz;
1029924ee317SVladimir Oltean 			goto out_free_skb;
1030924ee317SVladimir Oltean 		}
1031924ee317SVladimir Oltean 		*buf++ = val;
1032924ee317SVladimir Oltean 		len += sz;
1033924ee317SVladimir Oltean 	} while (len < buf_len);
1034924ee317SVladimir Oltean 
1035924ee317SVladimir Oltean 	/* Read the FCS */
1036924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1037924ee317SVladimir Oltean 	if (sz < 0) {
1038924ee317SVladimir Oltean 		err = sz;
1039924ee317SVladimir Oltean 		goto out_free_skb;
1040924ee317SVladimir Oltean 	}
1041924ee317SVladimir Oltean 
1042924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1043924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1044924ee317SVladimir Oltean 
1045924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1046924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1047924ee317SVladimir Oltean 		*buf = val;
1048924ee317SVladimir Oltean 	}
1049924ee317SVladimir Oltean 
1050b471a71eSClément Léger 	if (ocelot->ptp)
1051b471a71eSClément Léger 		ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
1052924ee317SVladimir Oltean 
1053924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1054924ee317SVladimir Oltean 	 * has already been forwarded.
1055924ee317SVladimir Oltean 	 */
1056df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1057924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1058924ee317SVladimir Oltean 
1059924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1060d8ea7ff3SHoratiu Vultur 
1061924ee317SVladimir Oltean 	*nskb = skb;
1062924ee317SVladimir Oltean 
1063924ee317SVladimir Oltean 	return 0;
1064924ee317SVladimir Oltean 
1065924ee317SVladimir Oltean out_free_skb:
1066924ee317SVladimir Oltean 	kfree_skb(skb);
1067924ee317SVladimir Oltean 	return err;
1068924ee317SVladimir Oltean }
1069924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1070924ee317SVladimir Oltean 
1071137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1072137ffbc4SVladimir Oltean {
1073137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1074137ffbc4SVladimir Oltean 
1075137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1076137ffbc4SVladimir Oltean 		return false;
1077137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1078137ffbc4SVladimir Oltean 		return false;
1079137ffbc4SVladimir Oltean 
1080137ffbc4SVladimir Oltean 	return true;
1081137ffbc4SVladimir Oltean }
1082137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1083137ffbc4SVladimir Oltean 
1084e5150f00SClément Léger void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag)
1085e5150f00SClément Léger {
1086e5150f00SClément Léger 	ocelot_ifh_set_bypass(ifh, 1);
1087e5150f00SClément Léger 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1088e5150f00SClément Léger 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1089e5150f00SClément Léger 	if (vlan_tag)
1090e5150f00SClément Léger 		ocelot_ifh_set_vlan_tci(ifh, vlan_tag);
1091e5150f00SClément Léger 	if (rew_op)
1092e5150f00SClément Léger 		ocelot_ifh_set_rew_op(ifh, rew_op);
1093e5150f00SClément Léger }
1094e5150f00SClément Léger EXPORT_SYMBOL(ocelot_ifh_port_set);
1095e5150f00SClément Léger 
1096137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1097137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1098137ffbc4SVladimir Oltean {
109940d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1100137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1101137ffbc4SVladimir Oltean 
1102137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1103137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1104137ffbc4SVladimir Oltean 
1105e5150f00SClément Léger 	ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
1106137ffbc4SVladimir Oltean 
1107137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
110840d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1109137ffbc4SVladimir Oltean 
1110137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1111137ffbc4SVladimir Oltean 	last = skb->len % 4;
1112137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1113137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1114137ffbc4SVladimir Oltean 
1115137ffbc4SVladimir Oltean 	/* Add padding */
1116137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1117137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1118137ffbc4SVladimir Oltean 		i++;
1119137ffbc4SVladimir Oltean 	}
1120137ffbc4SVladimir Oltean 
1121137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1122137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1123137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1124137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1125137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1126137ffbc4SVladimir Oltean 
1127137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1128137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1129137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1130137ffbc4SVladimir Oltean 
1131137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1132137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1133137ffbc4SVladimir Oltean }
1134137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1135137ffbc4SVladimir Oltean 
11360a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
11370a6f17c6SVladimir Oltean {
11380a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
11390a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
11400a6f17c6SVladimir Oltean }
11410a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
11420a6f17c6SVladimir Oltean 
114354c31984SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, const unsigned char *addr,
114454c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1145a556c76aSAlexandre Belloni {
114654c31984SVladimir Oltean 	if (!vid)
114754c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
114854c31984SVladimir Oltean 
1149e9b3ba43SVladimir Oltean 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
1150a556c76aSAlexandre Belloni }
11515e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1152a556c76aSAlexandre Belloni 
115354c31984SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, const unsigned char *addr,
115454c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1155531ee1a6SVladimir Oltean {
115654c31984SVladimir Oltean 	if (!vid)
115754c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
115854c31984SVladimir Oltean 
1159531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1160531ee1a6SVladimir Oltean }
11615e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1162531ee1a6SVladimir Oltean 
11632468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
1164531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1165a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1166a556c76aSAlexandre Belloni {
1167a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1168531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1169a556c76aSAlexandre Belloni 
1170a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1171a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1172a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1173a556c76aSAlexandre Belloni 
1174a556c76aSAlexandre Belloni 	/* Issue a read command */
1175a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1176a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1177a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1178a556c76aSAlexandre Belloni 
1179a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1180a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1181a556c76aSAlexandre Belloni 
1182a556c76aSAlexandre Belloni 	/* Read the entry flags */
1183a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1184a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1185a556c76aSAlexandre Belloni 		return -EINVAL;
1186a556c76aSAlexandre Belloni 
1187a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1188a556c76aSAlexandre Belloni 	 * do not report it.
1189a556c76aSAlexandre Belloni 	 */
1190a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1191531ee1a6SVladimir Oltean 	if (dst != port)
1192a556c76aSAlexandre Belloni 		return -EINVAL;
1193a556c76aSAlexandre Belloni 
1194a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1195a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1196a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1197a556c76aSAlexandre Belloni 
1198a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1199a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1200a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1201a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1202a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1203a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1204a556c76aSAlexandre Belloni 
1205a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1206a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1207a556c76aSAlexandre Belloni 
1208a556c76aSAlexandre Belloni 	return 0;
1209a556c76aSAlexandre Belloni }
1210a556c76aSAlexandre Belloni 
12115cad43a5SVladimir Oltean int ocelot_mact_flush(struct ocelot *ocelot, int port)
12125cad43a5SVladimir Oltean {
12135cad43a5SVladimir Oltean 	int err;
12145cad43a5SVladimir Oltean 
12155cad43a5SVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
12165cad43a5SVladimir Oltean 
12175cad43a5SVladimir Oltean 	/* Program ageing filter for a single port */
12185cad43a5SVladimir Oltean 	ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
12195cad43a5SVladimir Oltean 		     ANA_ANAGEFIL);
12205cad43a5SVladimir Oltean 
12215cad43a5SVladimir Oltean 	/* Flushing dynamic FDB entries requires two successive age scans */
12225cad43a5SVladimir Oltean 	ocelot_write(ocelot,
12235cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
12245cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
12255cad43a5SVladimir Oltean 
12265cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
12275cad43a5SVladimir Oltean 	if (err) {
12285cad43a5SVladimir Oltean 		mutex_unlock(&ocelot->mact_lock);
12295cad43a5SVladimir Oltean 		return err;
12305cad43a5SVladimir Oltean 	}
12315cad43a5SVladimir Oltean 
12325cad43a5SVladimir Oltean 	/* And second... */
12335cad43a5SVladimir Oltean 	ocelot_write(ocelot,
12345cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
12355cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
12365cad43a5SVladimir Oltean 
12375cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
12385cad43a5SVladimir Oltean 
12395cad43a5SVladimir Oltean 	/* Restore ageing filter */
12405cad43a5SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_ANAGEFIL);
12415cad43a5SVladimir Oltean 
12425cad43a5SVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
12435cad43a5SVladimir Oltean 
12445cad43a5SVladimir Oltean 	return err;
12455cad43a5SVladimir Oltean }
12465cad43a5SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_mact_flush);
12475cad43a5SVladimir Oltean 
12485e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1249531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1250a556c76aSAlexandre Belloni {
12512468346cSVladimir Oltean 	int err = 0;
1252531ee1a6SVladimir Oltean 	int i, j;
1253a556c76aSAlexandre Belloni 
12542468346cSVladimir Oltean 	/* We could take the lock just around ocelot_mact_read, but doing so
12552468346cSVladimir Oltean 	 * thousands of times in a row seems rather pointless and inefficient.
12562468346cSVladimir Oltean 	 */
12572468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
12582468346cSVladimir Oltean 
125921ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
126021ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1261a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1262531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1263531ee1a6SVladimir Oltean 			bool is_static;
1264531ee1a6SVladimir Oltean 
12652468346cSVladimir Oltean 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1266a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1267a556c76aSAlexandre Belloni 			 * skip it.
1268a556c76aSAlexandre Belloni 			 */
12692468346cSVladimir Oltean 			if (err == -EINVAL)
1270a556c76aSAlexandre Belloni 				continue;
12712468346cSVladimir Oltean 			else if (err)
12722468346cSVladimir Oltean 				break;
1273a556c76aSAlexandre Belloni 
1274531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1275531ee1a6SVladimir Oltean 
127654c31984SVladimir Oltean 			/* Hide the reserved VLANs used for
127754c31984SVladimir Oltean 			 * VLAN-unaware bridging.
127854c31984SVladimir Oltean 			 */
127954c31984SVladimir Oltean 			if (entry.vid > OCELOT_RSV_VLAN_RANGE_START)
128054c31984SVladimir Oltean 				entry.vid = 0;
128154c31984SVladimir Oltean 
12822468346cSVladimir Oltean 			err = cb(entry.mac, entry.vid, is_static, data);
12832468346cSVladimir Oltean 			if (err)
12842468346cSVladimir Oltean 				break;
1285a556c76aSAlexandre Belloni 		}
1286a556c76aSAlexandre Belloni 	}
1287a556c76aSAlexandre Belloni 
12882468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
12892468346cSVladimir Oltean 
12902468346cSVladimir Oltean 	return err;
1291531ee1a6SVladimir Oltean }
12925e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1293531ee1a6SVladimir Oltean 
12949d75b881SVladimir Oltean int ocelot_trap_add(struct ocelot *ocelot, int port,
12959d75b881SVladimir Oltean 		    unsigned long cookie, bool take_ts,
129696ca08c0SVladimir Oltean 		    void (*populate)(struct ocelot_vcap_filter *f))
129796ca08c0SVladimir Oltean {
129896ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
129996ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
130096ca08c0SVladimir Oltean 	bool new = false;
130196ca08c0SVladimir Oltean 	int err;
130296ca08c0SVladimir Oltean 
130396ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
130496ca08c0SVladimir Oltean 
130596ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
130696ca08c0SVladimir Oltean 						   false);
130796ca08c0SVladimir Oltean 	if (!trap) {
130896ca08c0SVladimir Oltean 		trap = kzalloc(sizeof(*trap), GFP_KERNEL);
130996ca08c0SVladimir Oltean 		if (!trap)
131096ca08c0SVladimir Oltean 			return -ENOMEM;
131196ca08c0SVladimir Oltean 
131296ca08c0SVladimir Oltean 		populate(trap);
131396ca08c0SVladimir Oltean 		trap->prio = 1;
131496ca08c0SVladimir Oltean 		trap->id.cookie = cookie;
131596ca08c0SVladimir Oltean 		trap->id.tc_offload = false;
131696ca08c0SVladimir Oltean 		trap->block_id = VCAP_IS2;
131796ca08c0SVladimir Oltean 		trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
131896ca08c0SVladimir Oltean 		trap->lookup = 0;
131996ca08c0SVladimir Oltean 		trap->action.cpu_copy_ena = true;
132096ca08c0SVladimir Oltean 		trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
132196ca08c0SVladimir Oltean 		trap->action.port_mask = 0;
13229d75b881SVladimir Oltean 		trap->take_ts = take_ts;
1323e1846cffSVladimir Oltean 		trap->is_trap = true;
132496ca08c0SVladimir Oltean 		new = true;
132596ca08c0SVladimir Oltean 	}
132696ca08c0SVladimir Oltean 
132796ca08c0SVladimir Oltean 	trap->ingress_port_mask |= BIT(port);
132896ca08c0SVladimir Oltean 
132996ca08c0SVladimir Oltean 	if (new)
133096ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_add(ocelot, trap, NULL);
133196ca08c0SVladimir Oltean 	else
133296ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
133396ca08c0SVladimir Oltean 	if (err) {
133496ca08c0SVladimir Oltean 		trap->ingress_port_mask &= ~BIT(port);
1335e1846cffSVladimir Oltean 		if (!trap->ingress_port_mask)
133696ca08c0SVladimir Oltean 			kfree(trap);
133796ca08c0SVladimir Oltean 		return err;
133896ca08c0SVladimir Oltean 	}
133996ca08c0SVladimir Oltean 
134096ca08c0SVladimir Oltean 	return 0;
134196ca08c0SVladimir Oltean }
134296ca08c0SVladimir Oltean 
1343b9bace6eSVladimir Oltean int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie)
134496ca08c0SVladimir Oltean {
134596ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
134696ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
134796ca08c0SVladimir Oltean 
134896ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
134996ca08c0SVladimir Oltean 
135096ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
135196ca08c0SVladimir Oltean 						   false);
135296ca08c0SVladimir Oltean 	if (!trap)
135396ca08c0SVladimir Oltean 		return 0;
135496ca08c0SVladimir Oltean 
135596ca08c0SVladimir Oltean 	trap->ingress_port_mask &= ~BIT(port);
1356e1846cffSVladimir Oltean 	if (!trap->ingress_port_mask)
135796ca08c0SVladimir Oltean 		return ocelot_vcap_filter_del(ocelot, trap);
135896ca08c0SVladimir Oltean 
135996ca08c0SVladimir Oltean 	return ocelot_vcap_filter_replace(ocelot, trap);
136096ca08c0SVladimir Oltean }
136196ca08c0SVladimir Oltean 
1362a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
1363b80af659SVladimir Oltean {
1364b80af659SVladimir Oltean 	u32 mask = 0;
1365b80af659SVladimir Oltean 	int port;
1366b80af659SVladimir Oltean 
1367961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
1368961d8b69SVladimir Oltean 
1369b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1370b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1371b80af659SVladimir Oltean 
1372b80af659SVladimir Oltean 		if (!ocelot_port)
1373b80af659SVladimir Oltean 			continue;
1374b80af659SVladimir Oltean 
1375a14e6b69SVladimir Oltean 		if (ocelot_port->bond == bond)
1376b80af659SVladimir Oltean 			mask |= BIT(port);
1377b80af659SVladimir Oltean 	}
1378b80af659SVladimir Oltean 
1379b80af659SVladimir Oltean 	return mask;
1380b80af659SVladimir Oltean }
1381b80af659SVladimir Oltean 
1382961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical
1383961d8b69SVladimir Oltean  * port ID present in that LAG. It may change if that port ever leaves the LAG.
1384961d8b69SVladimir Oltean  */
1385961d8b69SVladimir Oltean static int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond)
1386961d8b69SVladimir Oltean {
1387961d8b69SVladimir Oltean 	int bond_mask = ocelot_get_bond_mask(ocelot, bond);
1388961d8b69SVladimir Oltean 
1389961d8b69SVladimir Oltean 	if (!bond_mask)
1390961d8b69SVladimir Oltean 		return -ENOENT;
1391961d8b69SVladimir Oltean 
1392961d8b69SVladimir Oltean 	return __ffs(bond_mask);
1393961d8b69SVladimir Oltean }
1394961d8b69SVladimir Oltean 
1395291ac151SVladimir Oltean /* Returns the mask of user ports assigned to this DSA tag_8021q CPU port.
1396291ac151SVladimir Oltean  * Note that when CPU ports are in a LAG, the user ports are assigned to the
1397291ac151SVladimir Oltean  * 'primary' CPU port, the one whose physical port number gives the logical
1398291ac151SVladimir Oltean  * port number of the LAG.
1399291ac151SVladimir Oltean  *
1400291ac151SVladimir Oltean  * We leave PGID_SRC poorly configured for the 'secondary' CPU port in the LAG
1401291ac151SVladimir Oltean  * (to which no user port is assigned), but it appears that forwarding from
1402291ac151SVladimir Oltean  * this secondary CPU port looks at the PGID_SRC associated with the logical
1403291ac151SVladimir Oltean  * port ID that it's assigned to, which *is* configured properly.
1404291ac151SVladimir Oltean  */
1405c295f983SVladimir Oltean static u32 ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot *ocelot,
1406c295f983SVladimir Oltean 					       struct ocelot_port *cpu)
1407c295f983SVladimir Oltean {
1408c295f983SVladimir Oltean 	u32 mask = 0;
1409c295f983SVladimir Oltean 	int port;
1410c295f983SVladimir Oltean 
1411c295f983SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1412c295f983SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1413c295f983SVladimir Oltean 
1414c295f983SVladimir Oltean 		if (!ocelot_port)
1415c295f983SVladimir Oltean 			continue;
1416c295f983SVladimir Oltean 
1417c295f983SVladimir Oltean 		if (ocelot_port->dsa_8021q_cpu == cpu)
1418c295f983SVladimir Oltean 			mask |= BIT(port);
1419c295f983SVladimir Oltean 	}
1420c295f983SVladimir Oltean 
1421291ac151SVladimir Oltean 	if (cpu->bond)
1422291ac151SVladimir Oltean 		mask &= ~ocelot_get_bond_mask(ocelot, cpu->bond);
1423291ac151SVladimir Oltean 
1424c295f983SVladimir Oltean 	return mask;
1425c295f983SVladimir Oltean }
1426c295f983SVladimir Oltean 
1427291ac151SVladimir Oltean /* Returns the DSA tag_8021q CPU port that the given port is assigned to,
1428291ac151SVladimir Oltean  * or the bit mask of CPU ports if said CPU port is in a LAG.
1429291ac151SVladimir Oltean  */
1430c295f983SVladimir Oltean u32 ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot *ocelot, int port)
1431c295f983SVladimir Oltean {
1432c295f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1433c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot_port->dsa_8021q_cpu;
1434c295f983SVladimir Oltean 
1435c295f983SVladimir Oltean 	if (!cpu_port)
1436c295f983SVladimir Oltean 		return 0;
1437c295f983SVladimir Oltean 
1438291ac151SVladimir Oltean 	if (cpu_port->bond)
1439291ac151SVladimir Oltean 		return ocelot_get_bond_mask(ocelot, cpu_port->bond);
1440291ac151SVladimir Oltean 
1441c295f983SVladimir Oltean 	return BIT(cpu_port->index);
1442c295f983SVladimir Oltean }
1443c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assigned_dsa_8021q_cpu_mask);
1444c295f983SVladimir Oltean 
14458abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
1446df291e54SVladimir Oltean {
1447acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1448a8bd9fa5SVladimir Oltean 	const struct net_device *bridge;
1449df291e54SVladimir Oltean 	u32 mask = 0;
1450df291e54SVladimir Oltean 	int port;
1451df291e54SVladimir Oltean 
1452a8bd9fa5SVladimir Oltean 	if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
1453a8bd9fa5SVladimir Oltean 		return 0;
1454a8bd9fa5SVladimir Oltean 
1455a8bd9fa5SVladimir Oltean 	bridge = ocelot_port->bridge;
1456a8bd9fa5SVladimir Oltean 	if (!bridge)
1457acc64f52SVladimir Oltean 		return 0;
1458acc64f52SVladimir Oltean 
1459df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1460acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1461df291e54SVladimir Oltean 
1462df291e54SVladimir Oltean 		if (!ocelot_port)
1463df291e54SVladimir Oltean 			continue;
1464df291e54SVladimir Oltean 
1465df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1466df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1467df291e54SVladimir Oltean 			mask |= BIT(port);
1468df291e54SVladimir Oltean 	}
1469df291e54SVladimir Oltean 
1470df291e54SVladimir Oltean 	return mask;
1471df291e54SVladimir Oltean }
14728abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
1473df291e54SVladimir Oltean 
1474a72e23ddSVladimir Oltean static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
1475e21268efSVladimir Oltean {
1476e21268efSVladimir Oltean 	int port;
1477e21268efSVladimir Oltean 
14788abe1970SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
14798abe1970SVladimir Oltean 
14808abe1970SVladimir Oltean 	/* If cut-through forwarding is supported, update the masks before a
14818abe1970SVladimir Oltean 	 * port joins the forwarding domain, to avoid potential underruns if it
14828abe1970SVladimir Oltean 	 * has the highest speed from the new domain.
14838abe1970SVladimir Oltean 	 */
14848abe1970SVladimir Oltean 	if (joining && ocelot->ops->cut_through_fwd)
14858abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
14868abe1970SVladimir Oltean 
14879b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
14889b521250SVladimir Oltean 	 * a source for the other ports.
14899b521250SVladimir Oltean 	 */
14909b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1491e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1492e21268efSVladimir Oltean 		unsigned long mask;
1493e21268efSVladimir Oltean 
1494e21268efSVladimir Oltean 		if (!ocelot_port) {
1495e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1496e21268efSVladimir Oltean 			mask = 0;
1497e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1498e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1499c295f983SVladimir Oltean 			 * forward packets to all ports assigned to them.
1500e21268efSVladimir Oltean 			 */
1501c295f983SVladimir Oltean 			mask = ocelot_dsa_8021q_cpu_assigned_ports(ocelot,
1502c295f983SVladimir Oltean 								   ocelot_port);
1503df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1504528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
15059b521250SVladimir Oltean 
1506a8bd9fa5SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
1507df291e54SVladimir Oltean 			mask &= ~BIT(port);
1508c295f983SVladimir Oltean 
1509c295f983SVladimir Oltean 			mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1510c295f983SVladimir Oltean 									port);
1511c295f983SVladimir Oltean 
1512a14e6b69SVladimir Oltean 			if (bond)
1513a14e6b69SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond);
15149b521250SVladimir Oltean 		} else {
1515e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1516e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1517e21268efSVladimir Oltean 			 * module otherwise.
1518e21268efSVladimir Oltean 			 */
1519c295f983SVladimir Oltean 			mask = ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1520c295f983SVladimir Oltean 								       port);
1521e21268efSVladimir Oltean 		}
1522e21268efSVladimir Oltean 
1523e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
15249b521250SVladimir Oltean 	}
15258abe1970SVladimir Oltean 
15268abe1970SVladimir Oltean 	/* If cut-through forwarding is supported and a port is leaving, there
15278abe1970SVladimir Oltean 	 * is a chance that cut-through was disabled on the other ports due to
15288abe1970SVladimir Oltean 	 * the port which is leaving (it has a higher link speed). We need to
15298abe1970SVladimir Oltean 	 * update the cut-through masks of the remaining ports no earlier than
15308abe1970SVladimir Oltean 	 * after the port has left, to prevent underruns from happening between
15318abe1970SVladimir Oltean 	 * the cut-through update and the forwarding domain update.
15328abe1970SVladimir Oltean 	 */
15338abe1970SVladimir Oltean 	if (!joining && ocelot->ops->cut_through_fwd)
15348abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
15359b521250SVladimir Oltean }
15369b521250SVladimir Oltean 
153761be79baSVladimir Oltean /* Update PGID_CPU which is the destination port mask used for whitelisting
153861be79baSVladimir Oltean  * unicast addresses filtered towards the host. In the normal and NPI modes,
153961be79baSVladimir Oltean  * this points to the analyzer entry for the CPU port module, while in DSA
154061be79baSVladimir Oltean  * tag_8021q mode, it is a bit mask of all active CPU ports.
154161be79baSVladimir Oltean  * PGID_SRC will take care of forwarding a packet from one user port to
154261be79baSVladimir Oltean  * no more than a single CPU port.
154361be79baSVladimir Oltean  */
154461be79baSVladimir Oltean static void ocelot_update_pgid_cpu(struct ocelot *ocelot)
154561be79baSVladimir Oltean {
154661be79baSVladimir Oltean 	int pgid_cpu = 0;
154761be79baSVladimir Oltean 	int port;
154861be79baSVladimir Oltean 
154961be79baSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
155061be79baSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
155161be79baSVladimir Oltean 
155261be79baSVladimir Oltean 		if (!ocelot_port || !ocelot_port->is_dsa_8021q_cpu)
155361be79baSVladimir Oltean 			continue;
155461be79baSVladimir Oltean 
155561be79baSVladimir Oltean 		pgid_cpu |= BIT(port);
155661be79baSVladimir Oltean 	}
155761be79baSVladimir Oltean 
155861be79baSVladimir Oltean 	if (!pgid_cpu)
155961be79baSVladimir Oltean 		pgid_cpu = BIT(ocelot->num_phys_ports);
156061be79baSVladimir Oltean 
156161be79baSVladimir Oltean 	ocelot_write_rix(ocelot, pgid_cpu, ANA_PGID_PGID, PGID_CPU);
156261be79baSVladimir Oltean }
156361be79baSVladimir Oltean 
156436a0bf44SVladimir Oltean void ocelot_port_setup_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
156554c31984SVladimir Oltean {
1566c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
156754c31984SVladimir Oltean 	u16 vid;
156854c31984SVladimir Oltean 
15698c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
15708c166acbSVladimir Oltean 
1571c295f983SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = true;
157254c31984SVladimir Oltean 
157354c31984SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
1574c295f983SVladimir Oltean 		ocelot_vlan_member_add(ocelot, cpu, vid, true);
157561be79baSVladimir Oltean 
157661be79baSVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
1577a72e23ddSVladimir Oltean 
157836a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
157936a0bf44SVladimir Oltean }
158036a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_setup_dsa_8021q_cpu);
158136a0bf44SVladimir Oltean 
158236a0bf44SVladimir Oltean void ocelot_port_teardown_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
158336a0bf44SVladimir Oltean {
158436a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
158536a0bf44SVladimir Oltean 	u16 vid;
158636a0bf44SVladimir Oltean 
158736a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
158836a0bf44SVladimir Oltean 
158936a0bf44SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = false;
159036a0bf44SVladimir Oltean 
159136a0bf44SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
159236a0bf44SVladimir Oltean 		ocelot_vlan_member_del(ocelot, cpu_port->index, vid);
159336a0bf44SVladimir Oltean 
159436a0bf44SVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
159536a0bf44SVladimir Oltean 
159636a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
159736a0bf44SVladimir Oltean }
159836a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_teardown_dsa_8021q_cpu);
159936a0bf44SVladimir Oltean 
160036a0bf44SVladimir Oltean void ocelot_port_assign_dsa_8021q_cpu(struct ocelot *ocelot, int port,
160136a0bf44SVladimir Oltean 				      int cpu)
160236a0bf44SVladimir Oltean {
160336a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
160436a0bf44SVladimir Oltean 
160536a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
160636a0bf44SVladimir Oltean 
160736a0bf44SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = cpu_port;
1608a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
16098c166acbSVladimir Oltean 
16108c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
161154c31984SVladimir Oltean }
1612c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assign_dsa_8021q_cpu);
161354c31984SVladimir Oltean 
1614c295f983SVladimir Oltean void ocelot_port_unassign_dsa_8021q_cpu(struct ocelot *ocelot, int port)
161554c31984SVladimir Oltean {
16168c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
16178c166acbSVladimir Oltean 
1618c295f983SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = NULL;
1619a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
16208c166acbSVladimir Oltean 
16218c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
162254c31984SVladimir Oltean }
1623c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_unassign_dsa_8021q_cpu);
162454c31984SVladimir Oltean 
16255e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1626a556c76aSAlexandre Belloni {
1627421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1628df291e54SVladimir Oltean 	u32 learn_ena = 0;
1629a556c76aSAlexandre Belloni 
16308abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
16318abe1970SVladimir Oltean 
1632df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1633a556c76aSAlexandre Belloni 
1634df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1635df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1636df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1637a556c76aSAlexandre Belloni 
1638df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1639df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1640a556c76aSAlexandre Belloni 
16418abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
16428abe1970SVladimir Oltean 
16438abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
1644a556c76aSAlexandre Belloni }
16455e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1646a556c76aSAlexandre Belloni 
16475e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
16484bda1415SVladimir Oltean {
1649c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1650c0d7eccbSVladimir Oltean 
1651c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1652c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1653c0d7eccbSVladimir Oltean 	 */
1654c0d7eccbSVladimir Oltean 	if (!age_period)
1655c0d7eccbSVladimir Oltean 		age_period = 1;
1656c0d7eccbSVladimir Oltean 
1657c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1658a556c76aSAlexandre Belloni }
16595e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1660a556c76aSAlexandre Belloni 
1661a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1662a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1663a556c76aSAlexandre Belloni 						     u16 vid)
1664a556c76aSAlexandre Belloni {
1665a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1666a556c76aSAlexandre Belloni 
1667a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1668a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1669a556c76aSAlexandre Belloni 			return mc;
1670a556c76aSAlexandre Belloni 	}
1671a556c76aSAlexandre Belloni 
1672a556c76aSAlexandre Belloni 	return NULL;
1673a556c76aSAlexandre Belloni }
1674a556c76aSAlexandre Belloni 
16759403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
16769403c158SVladimir Oltean {
16779403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
16789403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
16799403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
16809403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
16817c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
16829403c158SVladimir Oltean }
16839403c158SVladimir Oltean 
1684e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1685e5d1f896SVladimir Oltean 					     unsigned long ports)
1686e5d1f896SVladimir Oltean {
1687e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1688e5d1f896SVladimir Oltean 
1689e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1690e5d1f896SVladimir Oltean 	if (!pgid)
1691e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1692e5d1f896SVladimir Oltean 
1693e5d1f896SVladimir Oltean 	pgid->ports = ports;
1694e5d1f896SVladimir Oltean 	pgid->index = index;
1695e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1696e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1697e5d1f896SVladimir Oltean 
1698e5d1f896SVladimir Oltean 	return pgid;
1699e5d1f896SVladimir Oltean }
1700e5d1f896SVladimir Oltean 
1701e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1702e5d1f896SVladimir Oltean {
1703e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1704e5d1f896SVladimir Oltean 		return;
1705e5d1f896SVladimir Oltean 
1706e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1707e5d1f896SVladimir Oltean 	kfree(pgid);
1708e5d1f896SVladimir Oltean }
1709e5d1f896SVladimir Oltean 
1710e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1711bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
17129403c158SVladimir Oltean {
1713e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1714e5d1f896SVladimir Oltean 	int index;
17159403c158SVladimir Oltean 
17169403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
17179403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
17189403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
17199403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
17209403c158SVladimir Oltean 	 */
1721bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1722bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1723e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
17249403c158SVladimir Oltean 
1725e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1726e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1727e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1728e5d1f896SVladimir Oltean 		 */
1729e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1730e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1731e5d1f896SVladimir Oltean 			return pgid;
1732e5d1f896SVladimir Oltean 		}
1733e5d1f896SVladimir Oltean 	}
1734e5d1f896SVladimir Oltean 
1735e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1736e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
17379403c158SVladimir Oltean 		bool used = false;
17389403c158SVladimir Oltean 
1739e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1740e5d1f896SVladimir Oltean 			if (pgid->index == index) {
17419403c158SVladimir Oltean 				used = true;
17429403c158SVladimir Oltean 				break;
17439403c158SVladimir Oltean 			}
17449403c158SVladimir Oltean 		}
17459403c158SVladimir Oltean 
17469403c158SVladimir Oltean 		if (!used)
1747e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
17489403c158SVladimir Oltean 	}
17499403c158SVladimir Oltean 
1750e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
17519403c158SVladimir Oltean }
17529403c158SVladimir Oltean 
17539403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1754bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
17559403c158SVladimir Oltean {
1756ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
17579403c158SVladimir Oltean 
1758bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
17599403c158SVladimir Oltean 		addr[0] = 0;
17609403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
17619403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1762bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
17639403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
17649403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
17659403c158SVladimir Oltean 	}
17669403c158SVladimir Oltean }
17679403c158SVladimir Oltean 
1768209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
176954c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
177054c31984SVladimir Oltean 			const struct net_device *bridge)
1771a556c76aSAlexandre Belloni {
1772a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1773004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1774e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1775a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1776a556c76aSAlexandre Belloni 
177754c31984SVladimir Oltean 	if (!vid)
177854c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
177954c31984SVladimir Oltean 
1780a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1781a556c76aSAlexandre Belloni 	if (!mc) {
1782728e69aeSVladimir Oltean 		/* New entry */
1783bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1784bb8d53fdSVladimir Oltean 		if (!mc)
1785bb8d53fdSVladimir Oltean 			return -ENOMEM;
1786bb8d53fdSVladimir Oltean 
1787bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1788bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1789bb8d53fdSVladimir Oltean 		mc->vid = vid;
1790bb8d53fdSVladimir Oltean 
1791a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1792728e69aeSVladimir Oltean 	} else {
1793e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1794e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1795e5d1f896SVladimir Oltean 		 */
1796e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1797bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1798a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1799a556c76aSAlexandre Belloni 	}
1800a556c76aSAlexandre Belloni 
1801004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1802e5d1f896SVladimir Oltean 
1803e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1804e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1805e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1806e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1807e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1808e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1809e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1810e5d1f896SVladimir Oltean 	}
1811e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1812e5d1f896SVladimir Oltean 
1813bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1814a556c76aSAlexandre Belloni 
1815e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1816e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1817e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1818e5d1f896SVladimir Oltean 				 pgid->index);
1819e5d1f896SVladimir Oltean 
1820e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1821bb8d53fdSVladimir Oltean 				 mc->entry_type);
1822a556c76aSAlexandre Belloni }
1823209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1824a556c76aSAlexandre Belloni 
1825209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
182654c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
182754c31984SVladimir Oltean 			const struct net_device *bridge)
1828a556c76aSAlexandre Belloni {
1829a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1830004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1831e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1832a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1833a556c76aSAlexandre Belloni 
183454c31984SVladimir Oltean 	if (!vid)
183554c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
183654c31984SVladimir Oltean 
1837a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1838a556c76aSAlexandre Belloni 	if (!mc)
1839a556c76aSAlexandre Belloni 		return -ENOENT;
1840a556c76aSAlexandre Belloni 
1841bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1842a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1843a556c76aSAlexandre Belloni 
1844e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1845004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1846a556c76aSAlexandre Belloni 	if (!mc->ports) {
1847a556c76aSAlexandre Belloni 		list_del(&mc->list);
1848a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1849a556c76aSAlexandre Belloni 		return 0;
1850a556c76aSAlexandre Belloni 	}
1851a556c76aSAlexandre Belloni 
1852e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1853e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1854e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1855e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1856e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1857e5d1f896SVladimir Oltean 
1858bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1859a556c76aSAlexandre Belloni 
1860e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1861e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1862e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1863e5d1f896SVladimir Oltean 				 pgid->index);
1864e5d1f896SVladimir Oltean 
1865e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1866bb8d53fdSVladimir Oltean 				 mc->entry_type);
1867a556c76aSAlexandre Belloni }
1868209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1869a556c76aSAlexandre Belloni 
187054c31984SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
187154c31984SVladimir Oltean 			    struct net_device *bridge, int bridge_num,
187254c31984SVladimir Oltean 			    struct netlink_ext_ack *extack)
1873a556c76aSAlexandre Belloni {
1874df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
187554c31984SVladimir Oltean 	int err;
187654c31984SVladimir Oltean 
187754c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
187854c31984SVladimir Oltean 	if (err)
187954c31984SVladimir Oltean 		return err;
1880a556c76aSAlexandre Belloni 
18818abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
18828abe1970SVladimir Oltean 
1883df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
188454c31984SVladimir Oltean 	ocelot_port->bridge_num = bridge_num;
1885a556c76aSAlexandre Belloni 
18868abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
18878abe1970SVladimir Oltean 
18888abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
188954c31984SVladimir Oltean 
189054c31984SVladimir Oltean 	if (br_vlan_enabled(bridge))
189154c31984SVladimir Oltean 		return 0;
189254c31984SVladimir Oltean 
189354c31984SVladimir Oltean 	return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge);
1894a556c76aSAlexandre Belloni }
18955e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1896a556c76aSAlexandre Belloni 
1897e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1898a556c76aSAlexandre Belloni 			      struct net_device *bridge)
1899a556c76aSAlexandre Belloni {
1900df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
19012e554a7aSVladimir Oltean 
19028abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
19038abe1970SVladimir Oltean 
190454c31984SVladimir Oltean 	if (!br_vlan_enabled(bridge))
190554c31984SVladimir Oltean 		ocelot_del_vlan_unaware_pvid(ocelot, port, bridge);
190654c31984SVladimir Oltean 
1907df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
190854c31984SVladimir Oltean 	ocelot_port->bridge_num = -1;
19097142529fSAntoine Tenart 
1910d4004422SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, NULL);
19110da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
19128abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
19138abe1970SVladimir Oltean 
19148abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
1915a556c76aSAlexandre Belloni }
19165e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1917a556c76aSAlexandre Belloni 
1918dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1919dc96ee37SAlexandre Belloni {
1920528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1921dc96ee37SAlexandre Belloni 	int i, port, lag;
1922dc96ee37SAlexandre Belloni 
1923dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
192496b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1925dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1926dc96ee37SAlexandre Belloni 
192796b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1928dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1929dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1930dc96ee37SAlexandre Belloni 
1931528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1932528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1933528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1934528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1935528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1936528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1937528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1938528d3f19SVladimir Oltean 	 */
1939528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1940528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1941528d3f19SVladimir Oltean 
1942528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1943528d3f19SVladimir Oltean 			continue;
1944528d3f19SVladimir Oltean 
1945528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1946528d3f19SVladimir Oltean 	}
1947528d3f19SVladimir Oltean 
1948528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1949dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1950528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
195123ca3b72SVladimir Oltean 		int num_active_ports = 0;
1952dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1953dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1954dc96ee37SAlexandre Belloni 
1955528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1956dc96ee37SAlexandre Belloni 			continue;
1957dc96ee37SAlexandre Belloni 
1958a14e6b69SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond);
1959528d3f19SVladimir Oltean 
1960dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1961a14e6b69SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1962a14e6b69SVladimir Oltean 
1963dc96ee37SAlexandre Belloni 			// Destination mask
1964dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1965dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1966a14e6b69SVladimir Oltean 
1967a14e6b69SVladimir Oltean 			if (ocelot_port->lag_tx_active)
196823ca3b72SVladimir Oltean 				aggr_idx[num_active_ports++] = port;
1969dc96ee37SAlexandre Belloni 		}
1970dc96ee37SAlexandre Belloni 
197196b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1972dc96ee37SAlexandre Belloni 			u32 ac;
1973dc96ee37SAlexandre Belloni 
1974dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1975dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
197623ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
197723ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
197823ca3b72SVladimir Oltean 			 */
197923ca3b72SVladimir Oltean 			if (num_active_ports)
198023ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1981dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1982dc96ee37SAlexandre Belloni 		}
1983528d3f19SVladimir Oltean 
1984528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1985528d3f19SVladimir Oltean 		 * the same config again.
1986528d3f19SVladimir Oltean 		 */
1987528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1988528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1989528d3f19SVladimir Oltean 
1990528d3f19SVladimir Oltean 			if (!ocelot_port)
1991528d3f19SVladimir Oltean 				continue;
1992528d3f19SVladimir Oltean 
1993528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1994528d3f19SVladimir Oltean 				visited |= BIT(port);
1995528d3f19SVladimir Oltean 		}
1996dc96ee37SAlexandre Belloni 	}
1997dc96ee37SAlexandre Belloni }
1998dc96ee37SAlexandre Belloni 
19992527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
20002527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
20012527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
20022527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
20032527f2e8SVladimir Oltean  */
20042527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
2005dc96ee37SAlexandre Belloni {
20062527f2e8SVladimir Oltean 	int port;
2007dc96ee37SAlexandre Belloni 
20082527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
20092527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
20102527f2e8SVladimir Oltean 		struct net_device *bond;
2011dc96ee37SAlexandre Belloni 
20122527f2e8SVladimir Oltean 		if (!ocelot_port)
20132527f2e8SVladimir Oltean 			continue;
2014dc96ee37SAlexandre Belloni 
20152527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
20162527f2e8SVladimir Oltean 		if (bond) {
2017961d8b69SVladimir Oltean 			int lag = ocelot_bond_get_id(ocelot, bond);
20182527f2e8SVladimir Oltean 
20192527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
2020dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
20212527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
20222527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
20232527f2e8SVladimir Oltean 		} else {
20242527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
20252527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
20262527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
20272527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
20282527f2e8SVladimir Oltean 		}
2029dc96ee37SAlexandre Belloni 	}
2030dc96ee37SAlexandre Belloni }
2031dc96ee37SAlexandre Belloni 
203228de0f9fSVladimir Oltean static int ocelot_migrate_mc(struct ocelot *ocelot, struct ocelot_multicast *mc,
203328de0f9fSVladimir Oltean 			     unsigned long from_mask, unsigned long to_mask)
203428de0f9fSVladimir Oltean {
203528de0f9fSVladimir Oltean 	unsigned char addr[ETH_ALEN];
203628de0f9fSVladimir Oltean 	struct ocelot_pgid *pgid;
203728de0f9fSVladimir Oltean 	u16 vid = mc->vid;
203828de0f9fSVladimir Oltean 
203928de0f9fSVladimir Oltean 	dev_dbg(ocelot->dev,
204028de0f9fSVladimir Oltean 		"Migrating multicast %pM vid %d from port mask 0x%lx to 0x%lx\n",
204128de0f9fSVladimir Oltean 		mc->addr, mc->vid, from_mask, to_mask);
204228de0f9fSVladimir Oltean 
204328de0f9fSVladimir Oltean 	/* First clean up the current port mask from hardware, because
204428de0f9fSVladimir Oltean 	 * we'll be modifying it.
204528de0f9fSVladimir Oltean 	 */
204628de0f9fSVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
204728de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
204828de0f9fSVladimir Oltean 	ocelot_mact_forget(ocelot, addr, vid);
204928de0f9fSVladimir Oltean 
205028de0f9fSVladimir Oltean 	mc->ports &= ~from_mask;
205128de0f9fSVladimir Oltean 	mc->ports |= to_mask;
205228de0f9fSVladimir Oltean 
205328de0f9fSVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
205428de0f9fSVladimir Oltean 	if (IS_ERR(pgid)) {
205528de0f9fSVladimir Oltean 		dev_err(ocelot->dev,
205628de0f9fSVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
205728de0f9fSVladimir Oltean 			mc->addr, mc->vid);
205828de0f9fSVladimir Oltean 		devm_kfree(ocelot->dev, mc);
205928de0f9fSVladimir Oltean 		return PTR_ERR(pgid);
206028de0f9fSVladimir Oltean 	}
206128de0f9fSVladimir Oltean 	mc->pgid = pgid;
206228de0f9fSVladimir Oltean 
206328de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
206428de0f9fSVladimir Oltean 
206528de0f9fSVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
206628de0f9fSVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
206728de0f9fSVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
206828de0f9fSVladimir Oltean 				 pgid->index);
206928de0f9fSVladimir Oltean 
207028de0f9fSVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
207128de0f9fSVladimir Oltean 				 mc->entry_type);
207228de0f9fSVladimir Oltean }
207328de0f9fSVladimir Oltean 
207428de0f9fSVladimir Oltean int ocelot_migrate_mdbs(struct ocelot *ocelot, unsigned long from_mask,
207528de0f9fSVladimir Oltean 			unsigned long to_mask)
207628de0f9fSVladimir Oltean {
207728de0f9fSVladimir Oltean 	struct ocelot_multicast *mc;
207828de0f9fSVladimir Oltean 	int err;
207928de0f9fSVladimir Oltean 
208028de0f9fSVladimir Oltean 	list_for_each_entry(mc, &ocelot->multicast, list) {
208128de0f9fSVladimir Oltean 		if (!(mc->ports & from_mask))
208228de0f9fSVladimir Oltean 			continue;
208328de0f9fSVladimir Oltean 
208428de0f9fSVladimir Oltean 		err = ocelot_migrate_mc(ocelot, mc, from_mask, to_mask);
208528de0f9fSVladimir Oltean 		if (err)
208628de0f9fSVladimir Oltean 			return err;
208728de0f9fSVladimir Oltean 	}
208828de0f9fSVladimir Oltean 
208928de0f9fSVladimir Oltean 	return 0;
209028de0f9fSVladimir Oltean }
209128de0f9fSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_migrate_mdbs);
209228de0f9fSVladimir Oltean 
2093961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says:
2094961d8b69SVladimir Oltean  *     Logical port number for front port. If port is not a member of a LLAG,
2095961d8b69SVladimir Oltean  *     then PORTID must be set to the physical port number.
2096961d8b69SVladimir Oltean  *     If port is a member of a LLAG, then PORTID must be set to the common
2097961d8b69SVladimir Oltean  *     PORTID_VAL used for all member ports of the LLAG.
2098961d8b69SVladimir Oltean  *     The value must not exceed the number of physical ports on the device.
2099961d8b69SVladimir Oltean  *
2100961d8b69SVladimir Oltean  * This means we have little choice but to migrate FDB entries pointing towards
2101961d8b69SVladimir Oltean  * a logical port when that changes.
2102961d8b69SVladimir Oltean  */
2103961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot,
2104961d8b69SVladimir Oltean 				    struct net_device *bond,
2105961d8b69SVladimir Oltean 				    int lag)
2106961d8b69SVladimir Oltean {
2107961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2108961d8b69SVladimir Oltean 	int err;
2109961d8b69SVladimir Oltean 
2110961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2111961d8b69SVladimir Oltean 
2112961d8b69SVladimir Oltean 	list_for_each_entry(fdb, &ocelot->lag_fdbs, list) {
2113961d8b69SVladimir Oltean 		if (fdb->bond != bond)
2114961d8b69SVladimir Oltean 			continue;
2115961d8b69SVladimir Oltean 
2116961d8b69SVladimir Oltean 		err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid);
2117961d8b69SVladimir Oltean 		if (err) {
2118961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2119961d8b69SVladimir Oltean 				"failed to delete LAG %s FDB %pM vid %d: %pe\n",
2120961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2121961d8b69SVladimir Oltean 		}
2122961d8b69SVladimir Oltean 
2123961d8b69SVladimir Oltean 		err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid,
2124961d8b69SVladimir Oltean 					ENTRYTYPE_LOCKED);
2125961d8b69SVladimir Oltean 		if (err) {
2126961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2127961d8b69SVladimir Oltean 				"failed to migrate LAG %s FDB %pM vid %d: %pe\n",
2128961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2129961d8b69SVladimir Oltean 		}
2130961d8b69SVladimir Oltean 	}
2131961d8b69SVladimir Oltean }
2132961d8b69SVladimir Oltean 
21339c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2134583cbbe3SVladimir Oltean 			 struct net_device *bond,
2135*2e359b00SVladimir Oltean 			 struct netdev_lag_upper_info *info,
2136*2e359b00SVladimir Oltean 			 struct netlink_ext_ack *extack)
2137dc96ee37SAlexandre Belloni {
2138*2e359b00SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
2139*2e359b00SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
2140*2e359b00SVladimir Oltean 				   "Can only offload LAG using hash TX type");
2141583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
2142*2e359b00SVladimir Oltean 	}
2143583cbbe3SVladimir Oltean 
21448abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
21458abe1970SVladimir Oltean 
2146b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
2147dc96ee37SAlexandre Belloni 
21482527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
21498abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2150dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2151dc96ee37SAlexandre Belloni 
21528abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
21538abe1970SVladimir Oltean 
2154dc96ee37SAlexandre Belloni 	return 0;
2155dc96ee37SAlexandre Belloni }
21569c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
2157dc96ee37SAlexandre Belloni 
21589c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2159dc96ee37SAlexandre Belloni 			   struct net_device *bond)
2160dc96ee37SAlexandre Belloni {
2161961d8b69SVladimir Oltean 	int old_lag_id, new_lag_id;
2162961d8b69SVladimir Oltean 
21638abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
21648abe1970SVladimir Oltean 
2165961d8b69SVladimir Oltean 	old_lag_id = ocelot_bond_get_id(ocelot, bond);
2166961d8b69SVladimir Oltean 
2167b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
2168b80af659SVladimir Oltean 
21692527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
21708abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2171dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
21728abe1970SVladimir Oltean 
2173961d8b69SVladimir Oltean 	new_lag_id = ocelot_bond_get_id(ocelot, bond);
2174961d8b69SVladimir Oltean 
2175961d8b69SVladimir Oltean 	if (new_lag_id >= 0 && old_lag_id != new_lag_id)
2176961d8b69SVladimir Oltean 		ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id);
2177961d8b69SVladimir Oltean 
21788abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2179dc96ee37SAlexandre Belloni }
21809c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
21810e332c85SPetr Machata 
218223ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
218323ca3b72SVladimir Oltean {
218423ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
218523ca3b72SVladimir Oltean 
2186961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2187961d8b69SVladimir Oltean 
218823ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
218923ca3b72SVladimir Oltean 
219023ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
219123ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
2192961d8b69SVladimir Oltean 
2193961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
219423ca3b72SVladimir Oltean }
219523ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
219623ca3b72SVladimir Oltean 
2197961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond,
219854c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
219954c31984SVladimir Oltean 		       const struct net_device *bridge)
2200961d8b69SVladimir Oltean {
2201961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2202961d8b69SVladimir Oltean 	int lag, err;
2203961d8b69SVladimir Oltean 
2204961d8b69SVladimir Oltean 	fdb = kzalloc(sizeof(*fdb), GFP_KERNEL);
2205961d8b69SVladimir Oltean 	if (!fdb)
2206961d8b69SVladimir Oltean 		return -ENOMEM;
2207961d8b69SVladimir Oltean 
220854c31984SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
220954c31984SVladimir Oltean 
221054c31984SVladimir Oltean 	if (!vid)
221154c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
221254c31984SVladimir Oltean 
2213961d8b69SVladimir Oltean 	ether_addr_copy(fdb->addr, addr);
2214961d8b69SVladimir Oltean 	fdb->vid = vid;
2215961d8b69SVladimir Oltean 	fdb->bond = bond;
2216961d8b69SVladimir Oltean 
2217961d8b69SVladimir Oltean 	lag = ocelot_bond_get_id(ocelot, bond);
2218961d8b69SVladimir Oltean 
2219961d8b69SVladimir Oltean 	err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED);
2220961d8b69SVladimir Oltean 	if (err) {
2221961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2222961d8b69SVladimir Oltean 		kfree(fdb);
2223961d8b69SVladimir Oltean 		return err;
2224961d8b69SVladimir Oltean 	}
2225961d8b69SVladimir Oltean 
2226961d8b69SVladimir Oltean 	list_add_tail(&fdb->list, &ocelot->lag_fdbs);
2227961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2228961d8b69SVladimir Oltean 
2229961d8b69SVladimir Oltean 	return 0;
2230961d8b69SVladimir Oltean }
2231961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add);
2232961d8b69SVladimir Oltean 
2233961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond,
223454c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
223554c31984SVladimir Oltean 		       const struct net_device *bridge)
2236961d8b69SVladimir Oltean {
2237961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb, *tmp;
2238961d8b69SVladimir Oltean 
2239961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2240961d8b69SVladimir Oltean 
224154c31984SVladimir Oltean 	if (!vid)
224254c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
224354c31984SVladimir Oltean 
2244961d8b69SVladimir Oltean 	list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) {
2245961d8b69SVladimir Oltean 		if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid ||
2246961d8b69SVladimir Oltean 		    fdb->bond != bond)
2247961d8b69SVladimir Oltean 			continue;
2248961d8b69SVladimir Oltean 
2249961d8b69SVladimir Oltean 		ocelot_mact_forget(ocelot, addr, vid);
2250961d8b69SVladimir Oltean 		list_del(&fdb->list);
2251961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2252961d8b69SVladimir Oltean 		kfree(fdb);
2253961d8b69SVladimir Oltean 
2254961d8b69SVladimir Oltean 		return 0;
2255961d8b69SVladimir Oltean 	}
2256961d8b69SVladimir Oltean 
2257961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2258961d8b69SVladimir Oltean 
2259961d8b69SVladimir Oltean 	return -ENOENT;
2260961d8b69SVladimir Oltean }
2261961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del);
2262961d8b69SVladimir Oltean 
2263a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2264a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
22650b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
22660b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
22670b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2268a8015dedSVladimir Oltean  */
22690b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
227031350d7fSVladimir Oltean {
227131350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2272a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2273e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
2274601e984fSVladimir Oltean 	int atop, atop_tot;
227531350d7fSVladimir Oltean 
22760b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
22770b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
22780b912fc9SVladimir Oltean 
2279cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
22800b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2281cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
22820b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
22830b912fc9SVladimir Oltean 	}
22840b912fc9SVladimir Oltean 
2285a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2286fa914e9cSVladimir Oltean 
2287e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
2288e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2289e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2290541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2291541132f0SMaxim Kochetkov 			    pause_start);
2292541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2293541132f0SMaxim Kochetkov 			    pause_stop);
2294fa914e9cSVladimir Oltean 
2295601e984fSVladimir Oltean 	/* Tail dropping watermarks */
2296f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2297a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2298601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2299601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2300601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2301fa914e9cSVladimir Oltean }
23020b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
23030b912fc9SVladimir Oltean 
23040b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
23050b912fc9SVladimir Oltean {
23060b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
23070b912fc9SVladimir Oltean 
23080b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
23090b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
23100b912fc9SVladimir Oltean 
2311cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
23120b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2313cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
23140b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
23150b912fc9SVladimir Oltean 	}
23160b912fc9SVladimir Oltean 
23170b912fc9SVladimir Oltean 	return max_mtu;
23180b912fc9SVladimir Oltean }
23190b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2320fa914e9cSVladimir Oltean 
2321421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2322421741eaSVladimir Oltean 				     bool enabled)
2323421741eaSVladimir Oltean {
2324421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2325421741eaSVladimir Oltean 	u32 val = 0;
2326421741eaSVladimir Oltean 
2327421741eaSVladimir Oltean 	if (enabled)
2328421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2329421741eaSVladimir Oltean 
2330421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2331421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2332421741eaSVladimir Oltean 
2333421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2334421741eaSVladimir Oltean }
2335421741eaSVladimir Oltean 
2336421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2337421741eaSVladimir Oltean 					bool enabled)
2338421741eaSVladimir Oltean {
2339421741eaSVladimir Oltean 	u32 val = 0;
2340421741eaSVladimir Oltean 
2341421741eaSVladimir Oltean 	if (enabled)
2342421741eaSVladimir Oltean 		val = BIT(port);
2343421741eaSVladimir Oltean 
2344421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2345421741eaSVladimir Oltean }
2346421741eaSVladimir Oltean 
2347421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2348421741eaSVladimir Oltean 					bool enabled)
2349421741eaSVladimir Oltean {
2350421741eaSVladimir Oltean 	u32 val = 0;
2351421741eaSVladimir Oltean 
2352421741eaSVladimir Oltean 	if (enabled)
2353421741eaSVladimir Oltean 		val = BIT(port);
2354421741eaSVladimir Oltean 
2355421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
23564cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4);
23574cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6);
2358421741eaSVladimir Oltean }
2359421741eaSVladimir Oltean 
2360421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2361421741eaSVladimir Oltean 					bool enabled)
2362421741eaSVladimir Oltean {
2363421741eaSVladimir Oltean 	u32 val = 0;
2364421741eaSVladimir Oltean 
2365421741eaSVladimir Oltean 	if (enabled)
2366421741eaSVladimir Oltean 		val = BIT(port);
2367421741eaSVladimir Oltean 
2368421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2369421741eaSVladimir Oltean }
2370421741eaSVladimir Oltean 
2371421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2372421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2373421741eaSVladimir Oltean {
2374421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2375421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2376421741eaSVladimir Oltean 		return -EINVAL;
2377421741eaSVladimir Oltean 
2378421741eaSVladimir Oltean 	return 0;
2379421741eaSVladimir Oltean }
2380421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2381421741eaSVladimir Oltean 
2382421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2383421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2384421741eaSVladimir Oltean {
2385421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2386421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2387421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2388421741eaSVladimir Oltean 
2389421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2390421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2391421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2392421741eaSVladimir Oltean 
2393421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2394421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2395421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2396421741eaSVladimir Oltean 
2397421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2398421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2399421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2400421741eaSVladimir Oltean }
2401421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2402421741eaSVladimir Oltean 
2403978777d0SVladimir Oltean int ocelot_port_get_default_prio(struct ocelot *ocelot, int port)
2404978777d0SVladimir Oltean {
2405978777d0SVladimir Oltean 	int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2406978777d0SVladimir Oltean 
2407978777d0SVladimir Oltean 	return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val);
2408978777d0SVladimir Oltean }
2409978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio);
2410978777d0SVladimir Oltean 
2411978777d0SVladimir Oltean int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio)
2412978777d0SVladimir Oltean {
241372f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2414978777d0SVladimir Oltean 		return -ERANGE;
2415978777d0SVladimir Oltean 
2416978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot,
2417978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio),
2418978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M,
2419978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG,
2420978777d0SVladimir Oltean 		       port);
2421978777d0SVladimir Oltean 
2422978777d0SVladimir Oltean 	return 0;
2423978777d0SVladimir Oltean }
2424978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio);
2425978777d0SVladimir Oltean 
2426978777d0SVladimir Oltean int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp)
2427978777d0SVladimir Oltean {
2428978777d0SVladimir Oltean 	int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2429978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2430978777d0SVladimir Oltean 
2431978777d0SVladimir Oltean 	/* Return error if DSCP prioritization isn't enabled */
2432978777d0SVladimir Oltean 	if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA))
2433978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2434978777d0SVladimir Oltean 
2435978777d0SVladimir Oltean 	if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) {
2436978777d0SVladimir Oltean 		dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg);
2437978777d0SVladimir Oltean 		/* Re-read ANA_DSCP_CFG for the translated DSCP */
2438978777d0SVladimir Oltean 		dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2439978777d0SVladimir Oltean 	}
2440978777d0SVladimir Oltean 
2441978777d0SVladimir Oltean 	/* If the DSCP value is not trusted, the QoS classification falls back
2442978777d0SVladimir Oltean 	 * to VLAN PCP or port-based default.
2443978777d0SVladimir Oltean 	 */
2444978777d0SVladimir Oltean 	if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA))
2445978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2446978777d0SVladimir Oltean 
2447978777d0SVladimir Oltean 	return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg);
2448978777d0SVladimir Oltean }
2449978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio);
2450978777d0SVladimir Oltean 
2451978777d0SVladimir Oltean int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2452978777d0SVladimir Oltean {
2453978777d0SVladimir Oltean 	int mask, val;
2454978777d0SVladimir Oltean 
245572f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2456978777d0SVladimir Oltean 		return -ERANGE;
2457978777d0SVladimir Oltean 
2458978777d0SVladimir Oltean 	/* There is at least one app table priority (this one), so we need to
2459978777d0SVladimir Oltean 	 * make sure DSCP prioritization is enabled on the port.
2460978777d0SVladimir Oltean 	 * Also make sure DSCP translation is disabled
2461978777d0SVladimir Oltean 	 * (dcbnl doesn't support it).
2462978777d0SVladimir Oltean 	 */
2463978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2464978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2465978777d0SVladimir Oltean 
2466978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask,
2467978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG, port);
2468978777d0SVladimir Oltean 
2469978777d0SVladimir Oltean 	/* Trust this DSCP value and map it to the given QoS class */
2470978777d0SVladimir Oltean 	val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio);
2471978777d0SVladimir Oltean 
2472978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp);
2473978777d0SVladimir Oltean 
2474978777d0SVladimir Oltean 	return 0;
2475978777d0SVladimir Oltean }
2476978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio);
2477978777d0SVladimir Oltean 
2478978777d0SVladimir Oltean int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2479978777d0SVladimir Oltean {
2480978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2481978777d0SVladimir Oltean 	int mask, i;
2482978777d0SVladimir Oltean 
2483978777d0SVladimir Oltean 	/* During a "dcb app replace" command, the new app table entry will be
2484978777d0SVladimir Oltean 	 * added first, then the old one will be deleted. But the hardware only
2485978777d0SVladimir Oltean 	 * supports one QoS class per DSCP value (duh), so if we blindly delete
2486978777d0SVladimir Oltean 	 * the app table entry for this DSCP value, we end up deleting the
2487978777d0SVladimir Oltean 	 * entry with the new priority. Avoid that by checking whether user
2488978777d0SVladimir Oltean 	 * space wants to delete the priority which is currently configured, or
2489978777d0SVladimir Oltean 	 * something else which is no longer current.
2490978777d0SVladimir Oltean 	 */
2491978777d0SVladimir Oltean 	if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio)
2492978777d0SVladimir Oltean 		return 0;
2493978777d0SVladimir Oltean 
2494978777d0SVladimir Oltean 	/* Untrust this DSCP value */
2495978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp);
2496978777d0SVladimir Oltean 
2497978777d0SVladimir Oltean 	for (i = 0; i < 64; i++) {
2498978777d0SVladimir Oltean 		int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i);
2499978777d0SVladimir Oltean 
2500978777d0SVladimir Oltean 		/* There are still app table entries on the port, so we need to
2501978777d0SVladimir Oltean 		 * keep DSCP enabled, nothing to do.
2502978777d0SVladimir Oltean 		 */
2503978777d0SVladimir Oltean 		if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)
2504978777d0SVladimir Oltean 			return 0;
2505978777d0SVladimir Oltean 	}
2506978777d0SVladimir Oltean 
2507978777d0SVladimir Oltean 	/* Disable DSCP QoS classification if there isn't any trusted
2508978777d0SVladimir Oltean 	 * DSCP value left.
2509978777d0SVladimir Oltean 	 */
2510978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2511978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2512978777d0SVladimir Oltean 
2513978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port);
2514978777d0SVladimir Oltean 
2515978777d0SVladimir Oltean 	return 0;
2516978777d0SVladimir Oltean }
2517978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio);
2518978777d0SVladimir Oltean 
2519f2a0e216SVladimir Oltean struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to,
2520ccb6ed42SVladimir Oltean 					struct netlink_ext_ack *extack)
2521ccb6ed42SVladimir Oltean {
2522ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2523ccb6ed42SVladimir Oltean 
2524ccb6ed42SVladimir Oltean 	if (m) {
2525ccb6ed42SVladimir Oltean 		if (m->to != to) {
2526ccb6ed42SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
2527ccb6ed42SVladimir Oltean 					   "Mirroring already configured towards different egress port");
2528ccb6ed42SVladimir Oltean 			return ERR_PTR(-EBUSY);
2529ccb6ed42SVladimir Oltean 		}
2530ccb6ed42SVladimir Oltean 
2531ccb6ed42SVladimir Oltean 		refcount_inc(&m->refcount);
2532ccb6ed42SVladimir Oltean 		return m;
2533ccb6ed42SVladimir Oltean 	}
2534ccb6ed42SVladimir Oltean 
2535ccb6ed42SVladimir Oltean 	m = kzalloc(sizeof(*m), GFP_KERNEL);
2536ccb6ed42SVladimir Oltean 	if (!m)
2537ccb6ed42SVladimir Oltean 		return ERR_PTR(-ENOMEM);
2538ccb6ed42SVladimir Oltean 
2539ccb6ed42SVladimir Oltean 	m->to = to;
2540ccb6ed42SVladimir Oltean 	refcount_set(&m->refcount, 1);
2541ccb6ed42SVladimir Oltean 	ocelot->mirror = m;
2542ccb6ed42SVladimir Oltean 
2543ccb6ed42SVladimir Oltean 	/* Program the mirror port to hardware */
2544ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS);
2545ccb6ed42SVladimir Oltean 
2546ccb6ed42SVladimir Oltean 	return m;
2547ccb6ed42SVladimir Oltean }
2548ccb6ed42SVladimir Oltean 
2549f2a0e216SVladimir Oltean void ocelot_mirror_put(struct ocelot *ocelot)
2550ccb6ed42SVladimir Oltean {
2551ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2552ccb6ed42SVladimir Oltean 
2553ccb6ed42SVladimir Oltean 	if (!refcount_dec_and_test(&m->refcount))
2554ccb6ed42SVladimir Oltean 		return;
2555ccb6ed42SVladimir Oltean 
2556ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_MIRRORPORTS);
2557ccb6ed42SVladimir Oltean 	ocelot->mirror = NULL;
2558ccb6ed42SVladimir Oltean 	kfree(m);
2559ccb6ed42SVladimir Oltean }
2560ccb6ed42SVladimir Oltean 
2561ccb6ed42SVladimir Oltean int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to,
2562ccb6ed42SVladimir Oltean 			   bool ingress, struct netlink_ext_ack *extack)
2563ccb6ed42SVladimir Oltean {
2564ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack);
2565ccb6ed42SVladimir Oltean 
2566ccb6ed42SVladimir Oltean 	if (IS_ERR(m))
2567ccb6ed42SVladimir Oltean 		return PTR_ERR(m);
2568ccb6ed42SVladimir Oltean 
2569ccb6ed42SVladimir Oltean 	if (ingress) {
2570ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2571ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2572ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2573ccb6ed42SVladimir Oltean 	} else {
2574ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, BIT(from), BIT(from),
2575ccb6ed42SVladimir Oltean 			   ANA_EMIRRORPORTS);
2576ccb6ed42SVladimir Oltean 	}
2577ccb6ed42SVladimir Oltean 
2578ccb6ed42SVladimir Oltean 	return 0;
2579ccb6ed42SVladimir Oltean }
2580ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_add);
2581ccb6ed42SVladimir Oltean 
2582ccb6ed42SVladimir Oltean void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress)
2583ccb6ed42SVladimir Oltean {
2584ccb6ed42SVladimir Oltean 	if (ingress) {
2585ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2586ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2587ccb6ed42SVladimir Oltean 	} else {
2588ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS);
2589ccb6ed42SVladimir Oltean 	}
2590ccb6ed42SVladimir Oltean 
2591ccb6ed42SVladimir Oltean 	ocelot_mirror_put(ocelot);
2592ccb6ed42SVladimir Oltean }
2593ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_del);
2594ccb6ed42SVladimir Oltean 
25955e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2596fa914e9cSVladimir Oltean {
2597fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2598fa914e9cSVladimir Oltean 
2599b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
260031350d7fSVladimir Oltean 
260131350d7fSVladimir Oltean 	/* Basic L2 initialization */
260231350d7fSVladimir Oltean 
26035bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
26045bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
26055bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
26065bc9d2e6SVladimir Oltean 	 */
26075bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
26085bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
26095bc9d2e6SVladimir Oltean 
26105bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
26115bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
26125bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
26135bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
26145bc9d2e6SVladimir Oltean 	mdelay(1);
26155bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
26165bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
26175bc9d2e6SVladimir Oltean 
26185bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2619a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
26205bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
26215bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2622a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
26235bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
26245bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
26255bc9d2e6SVladimir Oltean 
26265bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
26275bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
26285bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
26295bc9d2e6SVladimir Oltean 
2630e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2631541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2632e8e6e73dSVladimir Oltean 
263331350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
263431350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
263531350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
263631350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
263731350d7fSVladimir Oltean 
263831350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
263931350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
264031350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
264131350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
264231350d7fSVladimir Oltean 
2643421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2644421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2645421741eaSVladimir Oltean 
264646efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
264746efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
264846efe4efSVladimir Oltean 	 * automatic.
264946efe4efSVladimir Oltean 	 */
265046efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
265146efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
265246efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
265346efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
265446efe4efSVladimir Oltean 
265531350d7fSVladimir Oltean 	/* Enable vcap lookups */
265631350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
265731350d7fSVladimir Oltean }
26585e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
265931350d7fSVladimir Oltean 
26602d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
26612d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
26622d44b097SVladimir Oltean  * NPI mode is used).
266369df578cSVladimir Oltean  */
26642d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
266521468199SVladimir Oltean {
266669df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
266769df578cSVladimir Oltean 
266869df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
266921468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
267069df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
267169df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
267269df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
267369df578cSVladimir Oltean 	 */
267421468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
267521468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
267621468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
267721468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
267821468199SVladimir Oltean 
267969df578cSVladimir Oltean 	/* Enable CPU port module */
2680886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
268169df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2682886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2683cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2684886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2685cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
268621468199SVladimir Oltean 
268721468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
2688bfbab310SVladimir Oltean 	ocelot_write_gix(ocelot,
268954c31984SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) |
269021468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
269121468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
269221468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
269321468199SVladimir Oltean }
269421468199SVladimir Oltean 
2695f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2696f6fe01d6SVladimir Oltean {
2697f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2698f6fe01d6SVladimir Oltean 
2699f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2700f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2701f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2702f6fe01d6SVladimir Oltean 	 */
2703f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2704f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2705f6fe01d6SVladimir Oltean 
2706f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2707f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2708f6fe01d6SVladimir Oltean }
2709f6fe01d6SVladimir Oltean 
2710a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2711a556c76aSAlexandre Belloni {
271221468199SVladimir Oltean 	int i, ret;
271321468199SVladimir Oltean 	u32 port;
2714a556c76aSAlexandre Belloni 
27153a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
27163a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
27173a77b593SVladimir Oltean 		if (ret) {
27183a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
27193a77b593SVladimir Oltean 			return ret;
27203a77b593SVladimir Oltean 		}
27213a77b593SVladimir Oltean 	}
27223a77b593SVladimir Oltean 
27234e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
27242468346cSVladimir Oltean 	mutex_init(&ocelot->mact_lock);
27258abe1970SVladimir Oltean 	mutex_init(&ocelot->fwd_domain_lock);
27268670dc33SXiaoliang Yang 	mutex_init(&ocelot->tas_lock);
27274e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
272852849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
2729a556c76aSAlexandre Belloni 
2730ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2731fe90104cSVladimir Oltean 	if (!ocelot->owq)
2732ca0b272bSVladimir Oltean 		return -ENOMEM;
2733fe90104cSVladimir Oltean 
2734fe90104cSVladimir Oltean 	ret = ocelot_stats_init(ocelot);
2735fe90104cSVladimir Oltean 	if (ret) {
2736fe90104cSVladimir Oltean 		destroy_workqueue(ocelot->owq);
2737fe90104cSVladimir Oltean 		return ret;
2738ca0b272bSVladimir Oltean 	}
2739ca0b272bSVladimir Oltean 
27402b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2741e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
274290e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
2743961d8b69SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->lag_fdbs);
2744f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2745a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2746a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2747aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
27482d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2749a556c76aSAlexandre Belloni 
275023e2c506SXiaoliang Yang 	if (ocelot->ops->psfp_init)
275123e2c506SXiaoliang Yang 		ocelot->ops->psfp_init(ocelot);
275223e2c506SXiaoliang Yang 
2753a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2754a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2755a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2756a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2757a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2758a556c76aSAlexandre Belloni 	}
2759a556c76aSAlexandre Belloni 
2760a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2761a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2762a556c76aSAlexandre Belloni 
2763a556c76aSAlexandre Belloni 	/* Aggregation mode */
2764a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2765a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2766a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2767f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2768f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2769f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2770f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2771a556c76aSAlexandre Belloni 
2772a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2773a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2774a556c76aSAlexandre Belloni 	 */
2775a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2776a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2777a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2778a556c76aSAlexandre Belloni 
2779a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2780a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2781a556c76aSAlexandre Belloni 
2782a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2783a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2784a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2785a556c76aSAlexandre Belloni 
2786a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2787edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2788a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2789b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2790a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2791edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2792a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2793a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2794a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2795a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2796a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2797a556c76aSAlexandre Belloni 
2798a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2799a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2800a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2801a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2802a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2803a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2804a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2805a556c76aSAlexandre Belloni 				 port);
2806a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2807a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2808a556c76aSAlexandre Belloni 	}
2809a556c76aSAlexandre Belloni 
281096b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2811a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2812a556c76aSAlexandre Belloni 
2813a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2814a556c76aSAlexandre Belloni 	}
2815ebb1bb40SHoratiu Vultur 
2816ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2817ebb1bb40SHoratiu Vultur 
2818b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2819b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2820b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2821a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2822b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2823b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2824b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2825a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2826a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2827a556c76aSAlexandre Belloni 
2828a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2829a556c76aSAlexandre Belloni 	 * registers endianness.
2830a556c76aSAlexandre Belloni 	 */
2831a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2832a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2833a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2834a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2835a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2836a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2837a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2838a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2839a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2840a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2841a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2842a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2843a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2844a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2845a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2846a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2847a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2848a556c76aSAlexandre Belloni 
2849a556c76aSAlexandre Belloni 	return 0;
2850a556c76aSAlexandre Belloni }
2851a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2852a556c76aSAlexandre Belloni 
2853a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2854a556c76aSAlexandre Belloni {
2855fe90104cSVladimir Oltean 	ocelot_stats_deinit(ocelot);
2856ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2857a556c76aSAlexandre Belloni }
2858a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2859a556c76aSAlexandre Belloni 
2860e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2861e5fb512dSVladimir Oltean {
2862e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2863e5fb512dSVladimir Oltean 
2864e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2865e5fb512dSVladimir Oltean }
2866e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2867e5fb512dSVladimir Oltean 
2868a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2869