xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 6505b68056558357dc979d11921ed45cfd68ed69)
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 
292276d37ebSVladimir Oltean 		/* Ignore the VLAN added by ocelot_add_vlan_unaware_pvid(),
293276d37ebSVladimir Oltean 		 * because this is never active in hardware at the same time as
294276d37ebSVladimir Oltean 		 * the bridge VLANs, which only matter in VLAN-aware mode.
295276d37ebSVladimir Oltean 		 */
296276d37ebSVladimir Oltean 		if (vlan->vid >= OCELOT_RSV_VLAN_RANGE_START)
297276d37ebSVladimir Oltean 			continue;
298276d37ebSVladimir Oltean 
2990da1a1c4SVladimir Oltean 		if (vlan->untagged & BIT(port))
3000da1a1c4SVladimir Oltean 			num_untagged++;
3010da1a1c4SVladimir Oltean 	}
3020da1a1c4SVladimir Oltean 
3030da1a1c4SVladimir Oltean 	return num_untagged;
3040da1a1c4SVladimir Oltean }
3050da1a1c4SVladimir Oltean 
3060da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
3070da1a1c4SVladimir Oltean {
3080da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3090da1a1c4SVladimir Oltean 	int num_tagged = 0;
3100da1a1c4SVladimir Oltean 
3110da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
3120da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
3130da1a1c4SVladimir Oltean 			continue;
3140da1a1c4SVladimir Oltean 
3150da1a1c4SVladimir Oltean 		if (!(vlan->untagged & BIT(port)))
3160da1a1c4SVladimir Oltean 			num_tagged++;
3170da1a1c4SVladimir Oltean 	}
3180da1a1c4SVladimir Oltean 
3190da1a1c4SVladimir Oltean 	return num_tagged;
3200da1a1c4SVladimir Oltean }
3210da1a1c4SVladimir Oltean 
3220da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
3230da1a1c4SVladimir Oltean  * _one_ egress-untagged VLAN (_the_ native VLAN)
3240da1a1c4SVladimir Oltean  */
3250da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
3260da1a1c4SVladimir Oltean {
3270da1a1c4SVladimir Oltean 	return ocelot_port_num_tagged_vlans(ocelot, port) &&
3280da1a1c4SVladimir Oltean 	       ocelot_port_num_untagged_vlans(ocelot, port) == 1;
3290da1a1c4SVladimir Oltean }
3300da1a1c4SVladimir Oltean 
3310da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan *
3320da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
3330da1a1c4SVladimir Oltean {
3340da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3350da1a1c4SVladimir Oltean 
3360da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
3370da1a1c4SVladimir Oltean 		if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
3380da1a1c4SVladimir Oltean 			return vlan;
3390da1a1c4SVladimir Oltean 
3400da1a1c4SVladimir Oltean 	return NULL;
3410da1a1c4SVladimir Oltean }
3420da1a1c4SVladimir Oltean 
3430da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
3440da1a1c4SVladimir Oltean  * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
3450da1a1c4SVladimir Oltean  * state of the port.
3460da1a1c4SVladimir Oltean  */
3470da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
34897bb69e1SVladimir Oltean {
34997bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
35062a22bcbSVladimir Oltean 	enum ocelot_port_tag_config tag_cfg;
3510da1a1c4SVladimir Oltean 	bool uses_native_vlan = false;
35297bb69e1SVladimir Oltean 
35387b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
3540da1a1c4SVladimir Oltean 		uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
3550da1a1c4SVladimir Oltean 
3560da1a1c4SVladimir Oltean 		if (uses_native_vlan)
35762a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_NATIVE;
3580da1a1c4SVladimir Oltean 		else if (ocelot_port_num_untagged_vlans(ocelot, port))
3590da1a1c4SVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_DISABLED;
36087b0f983SVladimir Oltean 		else
36162a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_TRUNK;
36287b0f983SVladimir Oltean 	} else {
36362a22bcbSVladimir Oltean 		tag_cfg = OCELOT_PORT_TAG_DISABLED;
36487b0f983SVladimir Oltean 	}
3650da1a1c4SVladimir Oltean 
36662a22bcbSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
36787b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
36887b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
3690da1a1c4SVladimir Oltean 
3700da1a1c4SVladimir Oltean 	if (uses_native_vlan) {
3710da1a1c4SVladimir Oltean 		struct ocelot_bridge_vlan *native_vlan;
3720da1a1c4SVladimir Oltean 
3730da1a1c4SVladimir Oltean 		/* Not having a native VLAN is impossible, because
3740da1a1c4SVladimir Oltean 		 * ocelot_port_num_untagged_vlans has returned 1.
3750da1a1c4SVladimir Oltean 		 * So there is no use in checking for NULL here.
3760da1a1c4SVladimir Oltean 		 */
3770da1a1c4SVladimir Oltean 		native_vlan = ocelot_port_find_native_vlan(ocelot, port);
3780da1a1c4SVladimir Oltean 
3790da1a1c4SVladimir Oltean 		ocelot_rmw_gix(ocelot,
3800da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
3810da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID_M,
3820da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG, port);
3830da1a1c4SVladimir Oltean 	}
38497bb69e1SVladimir Oltean }
38597bb69e1SVladimir Oltean 
38654c31984SVladimir Oltean int ocelot_bridge_num_find(struct ocelot *ocelot,
38754c31984SVladimir Oltean 			   const struct net_device *bridge)
38854c31984SVladimir Oltean {
38954c31984SVladimir Oltean 	int port;
39054c31984SVladimir Oltean 
39154c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
39254c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
39354c31984SVladimir Oltean 
39454c31984SVladimir Oltean 		if (ocelot_port && ocelot_port->bridge == bridge)
39554c31984SVladimir Oltean 			return ocelot_port->bridge_num;
39654c31984SVladimir Oltean 	}
39754c31984SVladimir Oltean 
39854c31984SVladimir Oltean 	return -1;
39954c31984SVladimir Oltean }
40054c31984SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bridge_num_find);
40154c31984SVladimir Oltean 
40254c31984SVladimir Oltean static u16 ocelot_vlan_unaware_pvid(struct ocelot *ocelot,
40354c31984SVladimir Oltean 				    const struct net_device *bridge)
40454c31984SVladimir Oltean {
40554c31984SVladimir Oltean 	int bridge_num;
40654c31984SVladimir Oltean 
40754c31984SVladimir Oltean 	/* Standalone ports use VID 0 */
40854c31984SVladimir Oltean 	if (!bridge)
40954c31984SVladimir Oltean 		return 0;
41054c31984SVladimir Oltean 
41154c31984SVladimir Oltean 	bridge_num = ocelot_bridge_num_find(ocelot, bridge);
41254c31984SVladimir Oltean 	if (WARN_ON(bridge_num < 0))
41354c31984SVladimir Oltean 		return 0;
41454c31984SVladimir Oltean 
41554c31984SVladimir Oltean 	/* VLAN-unaware bridges use a reserved VID going from 4095 downwards */
41654c31984SVladimir Oltean 	return VLAN_N_VID - bridge_num - 1;
41754c31984SVladimir Oltean }
41854c31984SVladimir Oltean 
41975e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
420c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
421d4004422SVladimir Oltean 				 const struct ocelot_bridge_vlan *pvid_vlan)
42275e5a554SVladimir Oltean {
42375e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
42454c31984SVladimir Oltean 	u16 pvid = ocelot_vlan_unaware_pvid(ocelot, ocelot_port->bridge);
425be0576feSVladimir Oltean 	u32 val = 0;
42675e5a554SVladimir Oltean 
427c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
42875e5a554SVladimir Oltean 
429d4004422SVladimir Oltean 	if (ocelot_port->vlan_aware && pvid_vlan)
430d4004422SVladimir Oltean 		pvid = pvid_vlan->vid;
43175e5a554SVladimir Oltean 
43275e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
433d4004422SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
43475e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
43575e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
436be0576feSVladimir Oltean 
437be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
438be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
439be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
440be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
441be0576feSVladimir Oltean 	 */
442d4004422SVladimir Oltean 	if (!pvid_vlan && ocelot_port->vlan_aware)
443be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
444be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
445be0576feSVladimir Oltean 
446be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
447be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
448be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
449be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
45075e5a554SVladimir Oltean }
45175e5a554SVladimir Oltean 
45290e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
45390e0aa8dSVladimir Oltean 							  u16 vid)
454bbf6a2d9SVladimir Oltean {
45590e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
456bbf6a2d9SVladimir Oltean 
45790e0aa8dSVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
45890e0aa8dSVladimir Oltean 		if (vlan->vid == vid)
45990e0aa8dSVladimir Oltean 			return vlan;
460bbf6a2d9SVladimir Oltean 
46190e0aa8dSVladimir Oltean 	return NULL;
462bbf6a2d9SVladimir Oltean }
463bbf6a2d9SVladimir Oltean 
4640da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
4650da1a1c4SVladimir Oltean 				  bool untagged)
466bbf6a2d9SVladimir Oltean {
46790e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
46890e0aa8dSVladimir Oltean 	unsigned long portmask;
46990e0aa8dSVladimir Oltean 	int err;
47090e0aa8dSVladimir Oltean 
47190e0aa8dSVladimir Oltean 	if (vlan) {
47290e0aa8dSVladimir Oltean 		portmask = vlan->portmask | BIT(port);
47390e0aa8dSVladimir Oltean 
47490e0aa8dSVladimir Oltean 		err = ocelot_vlant_set_mask(ocelot, vid, portmask);
47590e0aa8dSVladimir Oltean 		if (err)
47690e0aa8dSVladimir Oltean 			return err;
47790e0aa8dSVladimir Oltean 
47890e0aa8dSVladimir Oltean 		vlan->portmask = portmask;
4790da1a1c4SVladimir Oltean 		/* Bridge VLANs can be overwritten with a different
4800da1a1c4SVladimir Oltean 		 * egress-tagging setting, so make sure to override an untagged
4810da1a1c4SVladimir Oltean 		 * with a tagged VID if that's going on.
4820da1a1c4SVladimir Oltean 		 */
4830da1a1c4SVladimir Oltean 		if (untagged)
4840da1a1c4SVladimir Oltean 			vlan->untagged |= BIT(port);
4850da1a1c4SVladimir Oltean 		else
4860da1a1c4SVladimir Oltean 			vlan->untagged &= ~BIT(port);
48790e0aa8dSVladimir Oltean 
48890e0aa8dSVladimir Oltean 		return 0;
48990e0aa8dSVladimir Oltean 	}
49090e0aa8dSVladimir Oltean 
49190e0aa8dSVladimir Oltean 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
49290e0aa8dSVladimir Oltean 	if (!vlan)
49390e0aa8dSVladimir Oltean 		return -ENOMEM;
49490e0aa8dSVladimir Oltean 
49590e0aa8dSVladimir Oltean 	portmask = BIT(port);
49690e0aa8dSVladimir Oltean 
49790e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
49890e0aa8dSVladimir Oltean 	if (err) {
49990e0aa8dSVladimir Oltean 		kfree(vlan);
50090e0aa8dSVladimir Oltean 		return err;
50190e0aa8dSVladimir Oltean 	}
50290e0aa8dSVladimir Oltean 
50390e0aa8dSVladimir Oltean 	vlan->vid = vid;
50490e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
5050da1a1c4SVladimir Oltean 	if (untagged)
5060da1a1c4SVladimir Oltean 		vlan->untagged = BIT(port);
50790e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&vlan->list);
50890e0aa8dSVladimir Oltean 	list_add_tail(&vlan->list, &ocelot->vlans);
50990e0aa8dSVladimir Oltean 
51090e0aa8dSVladimir Oltean 	return 0;
511bbf6a2d9SVladimir Oltean }
512bbf6a2d9SVladimir Oltean 
513bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
514bbf6a2d9SVladimir Oltean {
51590e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
51690e0aa8dSVladimir Oltean 	unsigned long portmask;
51790e0aa8dSVladimir Oltean 	int err;
51890e0aa8dSVladimir Oltean 
51990e0aa8dSVladimir Oltean 	if (!vlan)
52090e0aa8dSVladimir Oltean 		return 0;
52190e0aa8dSVladimir Oltean 
52290e0aa8dSVladimir Oltean 	portmask = vlan->portmask & ~BIT(port);
52390e0aa8dSVladimir Oltean 
52490e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
52590e0aa8dSVladimir Oltean 	if (err)
52690e0aa8dSVladimir Oltean 		return err;
52790e0aa8dSVladimir Oltean 
52890e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
52990e0aa8dSVladimir Oltean 	if (vlan->portmask)
53090e0aa8dSVladimir Oltean 		return 0;
53190e0aa8dSVladimir Oltean 
53290e0aa8dSVladimir Oltean 	list_del(&vlan->list);
53390e0aa8dSVladimir Oltean 	kfree(vlan);
53490e0aa8dSVladimir Oltean 
53590e0aa8dSVladimir Oltean 	return 0;
536bbf6a2d9SVladimir Oltean }
537bbf6a2d9SVladimir Oltean 
53854c31984SVladimir Oltean static int ocelot_add_vlan_unaware_pvid(struct ocelot *ocelot, int port,
53954c31984SVladimir Oltean 					const struct net_device *bridge)
54054c31984SVladimir Oltean {
54154c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
54254c31984SVladimir Oltean 
54354c31984SVladimir Oltean 	return ocelot_vlan_member_add(ocelot, port, vid, true);
54454c31984SVladimir Oltean }
54554c31984SVladimir Oltean 
54654c31984SVladimir Oltean static int ocelot_del_vlan_unaware_pvid(struct ocelot *ocelot, int port,
54754c31984SVladimir Oltean 					const struct net_device *bridge)
54854c31984SVladimir Oltean {
54954c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
55054c31984SVladimir Oltean 
55154c31984SVladimir Oltean 	return ocelot_vlan_member_del(ocelot, port, vid);
55254c31984SVladimir Oltean }
55354c31984SVladimir Oltean 
5542e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
5553b95d1b2SVladimir Oltean 			       bool vlan_aware, struct netlink_ext_ack *extack)
55687b0f983SVladimir Oltean {
55770edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
558bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
55970edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
5601fcb8fb3SVladimir Oltean 	int err = 0;
561bae33f2bSVladimir Oltean 	u32 val;
56270edfae1SVladimir Oltean 
56370edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
56470edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
56570edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
5663b95d1b2SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
5673b95d1b2SVladimir Oltean 					   "Cannot change VLAN state with vlan modify rules active");
56870edfae1SVladimir Oltean 			return -EBUSY;
56970edfae1SVladimir Oltean 		}
57070edfae1SVladimir Oltean 	}
57170edfae1SVladimir Oltean 
57254c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
57354c31984SVladimir Oltean 	if (err)
57454c31984SVladimir Oltean 		return err;
57554c31984SVladimir Oltean 
57654c31984SVladimir Oltean 	if (vlan_aware)
57754c31984SVladimir Oltean 		err = ocelot_del_vlan_unaware_pvid(ocelot, port,
57854c31984SVladimir Oltean 						   ocelot_port->bridge);
5791fcb8fb3SVladimir Oltean 	else if (ocelot_port->bridge)
58054c31984SVladimir Oltean 		err = ocelot_add_vlan_unaware_pvid(ocelot, port,
58154c31984SVladimir Oltean 						   ocelot_port->bridge);
58254c31984SVladimir Oltean 	if (err)
58354c31984SVladimir Oltean 		return err;
58454c31984SVladimir Oltean 
58587b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
58687b0f983SVladimir Oltean 
58787b0f983SVladimir Oltean 	if (vlan_aware)
58887b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
58987b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
59087b0f983SVladimir Oltean 	else
59187b0f983SVladimir Oltean 		val = 0;
59287b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
59387b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
59487b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
59587b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
59687b0f983SVladimir Oltean 
597c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
5980da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
5992e554a7aSVladimir Oltean 
6002e554a7aSVladimir Oltean 	return 0;
60187b0f983SVladimir Oltean }
60287b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
60387b0f983SVladimir Oltean 
6042f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
60501af940eSVladimir Oltean 			bool untagged, struct netlink_ext_ack *extack)
6062f0402feSVladimir Oltean {
6070da1a1c4SVladimir Oltean 	if (untagged) {
6080da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6090da1a1c4SVladimir Oltean 		if (ocelot_port_uses_native_vlan(ocelot, port)) {
61001af940eSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6110da1a1c4SVladimir Oltean 					   "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
6122f0402feSVladimir Oltean 			return -EBUSY;
6132f0402feSVladimir Oltean 		}
6140da1a1c4SVladimir Oltean 	} else {
6150da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6160da1a1c4SVladimir Oltean 		if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
6170da1a1c4SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6180da1a1c4SVladimir Oltean 					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
6190da1a1c4SVladimir Oltean 			return -EBUSY;
6200da1a1c4SVladimir Oltean 		}
6210da1a1c4SVladimir Oltean 	}
6222f0402feSVladimir Oltean 
62354c31984SVladimir Oltean 	if (vid > OCELOT_RSV_VLAN_RANGE_START) {
62454c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
62554c31984SVladimir Oltean 				   "VLAN range 4000-4095 reserved for VLAN-unaware bridging");
62654c31984SVladimir Oltean 		return -EBUSY;
62754c31984SVladimir Oltean 	}
62854c31984SVladimir Oltean 
6292f0402feSVladimir Oltean 	return 0;
6302f0402feSVladimir Oltean }
6312f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
6322f0402feSVladimir Oltean 
6335e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
6347142529fSAntoine Tenart 		    bool untagged)
6357142529fSAntoine Tenart {
636bbf6a2d9SVladimir Oltean 	int err;
6377142529fSAntoine Tenart 
6389323ac36SVladimir Oltean 	/* Ignore VID 0 added to our RX filter by the 8021q module, since
6399323ac36SVladimir Oltean 	 * that collides with OCELOT_STANDALONE_PVID and changes it from
6409323ac36SVladimir Oltean 	 * egress-untagged to egress-tagged.
6419323ac36SVladimir Oltean 	 */
6429323ac36SVladimir Oltean 	if (!vid)
6439323ac36SVladimir Oltean 		return 0;
6449323ac36SVladimir Oltean 
6450da1a1c4SVladimir Oltean 	err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
646bbf6a2d9SVladimir Oltean 	if (err)
647bbf6a2d9SVladimir Oltean 		return err;
6487142529fSAntoine Tenart 
6497142529fSAntoine Tenart 	/* Default ingress vlan classification */
650d4004422SVladimir Oltean 	if (pvid)
651d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port,
652d4004422SVladimir Oltean 				     ocelot_bridge_vlan_find(ocelot, vid));
6537142529fSAntoine Tenart 
6547142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
6550da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6567142529fSAntoine Tenart 
6577142529fSAntoine Tenart 	return 0;
6587142529fSAntoine Tenart }
6595e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
6607142529fSAntoine Tenart 
6615e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
6629855934cSVladimir Oltean {
6639855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
664ef576405SVladimir Oltean 	bool del_pvid = false;
665bbf6a2d9SVladimir Oltean 	int err;
6667142529fSAntoine Tenart 
6679323ac36SVladimir Oltean 	if (!vid)
6689323ac36SVladimir Oltean 		return 0;
6699323ac36SVladimir Oltean 
670ef576405SVladimir Oltean 	if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
671ef576405SVladimir Oltean 		del_pvid = true;
672ef576405SVladimir Oltean 
673bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
674bbf6a2d9SVladimir Oltean 	if (err)
675bbf6a2d9SVladimir Oltean 		return err;
6767142529fSAntoine Tenart 
677be0576feSVladimir Oltean 	/* Ingress */
678ef576405SVladimir Oltean 	if (del_pvid)
679d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, NULL);
680be0576feSVladimir Oltean 
6817142529fSAntoine Tenart 	/* Egress */
6820da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6837142529fSAntoine Tenart 
6847142529fSAntoine Tenart 	return 0;
6857142529fSAntoine Tenart }
6865e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
6877142529fSAntoine Tenart 
688a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
689a556c76aSAlexandre Belloni {
690bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
6917142529fSAntoine Tenart 	u16 port, vid;
6927142529fSAntoine Tenart 
693a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
694a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
695a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
696a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
6977142529fSAntoine Tenart 
6987142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
699bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
70090e0aa8dSVladimir Oltean 		ocelot_vlant_set_mask(ocelot, vid, 0);
7017142529fSAntoine Tenart 
70254c31984SVladimir Oltean 	/* We need VID 0 to get traffic on standalone ports.
70354c31984SVladimir Oltean 	 * It is added automatically if the 8021q module is loaded, but we
70454c31984SVladimir Oltean 	 * can't rely on that since it might not be.
7057142529fSAntoine Tenart 	 */
70654c31984SVladimir Oltean 	ocelot_vlant_set_mask(ocelot, OCELOT_STANDALONE_PVID, all_ports);
7077142529fSAntoine Tenart 
7087142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
7097142529fSAntoine Tenart 	 * default.
7107142529fSAntoine Tenart 	 */
711bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
7127142529fSAntoine Tenart 
7137142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
7147142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
7157142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
7167142529fSAntoine Tenart 	}
717a556c76aSAlexandre Belloni }
718a556c76aSAlexandre Belloni 
719eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
720eb4733d7SVladimir Oltean {
721eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
722eb4733d7SVladimir Oltean }
723eb4733d7SVladimir Oltean 
724e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
725eb4733d7SVladimir Oltean {
7261650bdb1SVladimir Oltean 	unsigned int pause_ena;
727eb4733d7SVladimir Oltean 	int err, val;
728eb4733d7SVladimir Oltean 
729eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
730eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
731eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
732eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
733eb4733d7SVladimir Oltean 
734eb4733d7SVladimir Oltean 	/* Disable flow control */
7351650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
736eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
737eb4733d7SVladimir Oltean 
738eb4733d7SVladimir Oltean 	/* Disable priority flow control */
739eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
740eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
741eb4733d7SVladimir Oltean 
742eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
743eb4733d7SVladimir Oltean 	 * at the port.
744eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
745eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
746eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
747eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
748eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
749eb4733d7SVladimir Oltean 	 */
750eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
751eb4733d7SVladimir Oltean 
752eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
753eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
754eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
755eb4733d7SVladimir Oltean 
756eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
757eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
758eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
759eb4733d7SVladimir Oltean 
760eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
761eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
762eb4733d7SVladimir Oltean 		       port);
763eb4733d7SVladimir Oltean 
764eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
765eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
766eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
767eb4733d7SVladimir Oltean 
768eb4733d7SVladimir Oltean 	/* Clear flushing again. */
769eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
770eb4733d7SVladimir Oltean 
7711650bdb1SVladimir Oltean 	/* Re-enable flow control */
7721650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
7731650bdb1SVladimir Oltean 
774eb4733d7SVladimir Oltean 	return err;
775eb4733d7SVladimir Oltean }
776eb4733d7SVladimir Oltean 
777e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
778e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
779e6e12df6SVladimir Oltean 				  phy_interface_t interface,
780e6e12df6SVladimir Oltean 				  unsigned long quirks)
781a556c76aSAlexandre Belloni {
78226f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
783e6e12df6SVladimir Oltean 	int err;
784a556c76aSAlexandre Belloni 
7858abe1970SVladimir Oltean 	ocelot_port->speed = SPEED_UNKNOWN;
7868abe1970SVladimir Oltean 
787e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
788e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
789e6e12df6SVladimir Oltean 
7908abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
7918abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
7928abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
7938abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
7948abe1970SVladimir Oltean 	}
7958abe1970SVladimir Oltean 
796e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
797e6e12df6SVladimir Oltean 
798e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
799e6e12df6SVladimir Oltean 	if (err)
800e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
801e6e12df6SVladimir Oltean 			port, err);
802e6e12df6SVladimir Oltean 
803e6e12df6SVladimir Oltean 	/* Put the port in reset. */
804e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
805e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
806e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
807e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
80874a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
809e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
81074a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
811e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
812e6e12df6SVladimir Oltean }
813e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
814e6e12df6SVladimir Oltean 
815e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
816e6e12df6SVladimir Oltean 				struct phy_device *phydev,
817e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
818e6e12df6SVladimir Oltean 				phy_interface_t interface,
819e6e12df6SVladimir Oltean 				int speed, int duplex,
820e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
821e6e12df6SVladimir Oltean 				unsigned long quirks)
822e6e12df6SVladimir Oltean {
823e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
824e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
825e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
826e6e12df6SVladimir Oltean 
8278abe1970SVladimir Oltean 	ocelot_port->speed = speed;
8288abe1970SVladimir Oltean 
829e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
830e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
831e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
832e6e12df6SVladimir Oltean 	 * (which is also its default value).
833e6e12df6SVladimir Oltean 	 */
834e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
835e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
836e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
837e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
838e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
839e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
840e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
841e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
842e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
843e6e12df6SVladimir Oltean 	} else {
844e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
845e6e12df6SVladimir Oltean 	}
846e6e12df6SVladimir Oltean 
847e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
848e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
849e6e12df6SVladimir Oltean 
850e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
851e6e12df6SVladimir Oltean 
852e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
853e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
854e6e12df6SVladimir Oltean 	 */
855e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
856e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
857e6e12df6SVladimir Oltean 
858e6e12df6SVladimir Oltean 	switch (speed) {
859a556c76aSAlexandre Belloni 	case SPEED_10:
860e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
861a556c76aSAlexandre Belloni 		break;
862a556c76aSAlexandre Belloni 	case SPEED_100:
863e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
864a556c76aSAlexandre Belloni 		break;
865a556c76aSAlexandre Belloni 	case SPEED_1000:
866a556c76aSAlexandre Belloni 	case SPEED_2500:
867e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
868a556c76aSAlexandre Belloni 		break;
869a556c76aSAlexandre Belloni 	default:
870e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
871e6e12df6SVladimir Oltean 			port, speed);
872a556c76aSAlexandre Belloni 		return;
873a556c76aSAlexandre Belloni 	}
874a556c76aSAlexandre Belloni 
875de8586edSVladimir Oltean 	if (rx_pause)
876e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
877a556c76aSAlexandre Belloni 
878e6e12df6SVladimir Oltean 	if (tx_pause)
879e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
880e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
881e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
882e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
883a556c76aSAlexandre Belloni 
884e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
885e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
886e6e12df6SVladimir Oltean 	 */
887e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
888a556c76aSAlexandre Belloni 
889e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
8901ba8f656SVladimir Oltean 
89133cb0ff3SVladimir Oltean 	/* Don't attempt to send PAUSE frames on the NPI port, it's broken */
89233cb0ff3SVladimir Oltean 	if (port != ocelot->npi)
89333cb0ff3SVladimir Oltean 		ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
89433cb0ff3SVladimir Oltean 				    tx_pause);
8951ba8f656SVladimir Oltean 
896e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
897e6e12df6SVladimir Oltean 	 * enable MAC module
898e6e12df6SVladimir Oltean 	 */
899004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
900a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
901a556c76aSAlexandre Belloni 
9028abe1970SVladimir Oltean 	/* If the port supports cut-through forwarding, update the masks before
9038abe1970SVladimir Oltean 	 * enabling forwarding on the port.
9048abe1970SVladimir Oltean 	 */
9058abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
9068abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
9078abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
9088abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
9098abe1970SVladimir Oltean 	}
9108abe1970SVladimir Oltean 
911a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
912886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
913886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
914a556c76aSAlexandre Belloni }
915e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
916889b8950SVladimir Oltean 
917924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
918924ee317SVladimir Oltean 				u32 *rval)
919924ee317SVladimir Oltean {
920924ee317SVladimir Oltean 	u32 bytes_valid, val;
921924ee317SVladimir Oltean 
922924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
923924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
924924ee317SVladimir Oltean 		if (ifh)
925924ee317SVladimir Oltean 			return -EIO;
926924ee317SVladimir Oltean 
927924ee317SVladimir Oltean 		do {
928924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
929924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
930924ee317SVladimir Oltean 	}
931924ee317SVladimir Oltean 
932924ee317SVladimir Oltean 	switch (val) {
933924ee317SVladimir Oltean 	case XTR_ABORT:
934924ee317SVladimir Oltean 		return -EIO;
935924ee317SVladimir Oltean 	case XTR_EOF_0:
936924ee317SVladimir Oltean 	case XTR_EOF_1:
937924ee317SVladimir Oltean 	case XTR_EOF_2:
938924ee317SVladimir Oltean 	case XTR_EOF_3:
939924ee317SVladimir Oltean 	case XTR_PRUNED:
940924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
941924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
942924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
943924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
944924ee317SVladimir Oltean 		else
945924ee317SVladimir Oltean 			*rval = val;
946924ee317SVladimir Oltean 
947924ee317SVladimir Oltean 		return bytes_valid;
948924ee317SVladimir Oltean 	case XTR_ESCAPE:
949924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
950924ee317SVladimir Oltean 
951924ee317SVladimir Oltean 		return 4;
952924ee317SVladimir Oltean 	default:
953924ee317SVladimir Oltean 		*rval = val;
954924ee317SVladimir Oltean 
955924ee317SVladimir Oltean 		return 4;
956924ee317SVladimir Oltean 	}
957924ee317SVladimir Oltean }
958924ee317SVladimir Oltean 
959924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
960924ee317SVladimir Oltean {
961924ee317SVladimir Oltean 	int i, err = 0;
962924ee317SVladimir Oltean 
963924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
964924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
965924ee317SVladimir Oltean 		if (err != 4)
966924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
967924ee317SVladimir Oltean 	}
968924ee317SVladimir Oltean 
969924ee317SVladimir Oltean 	return 0;
970924ee317SVladimir Oltean }
971924ee317SVladimir Oltean 
972b471a71eSClément Léger void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
973b471a71eSClément Léger 			     u64 timestamp)
974924ee317SVladimir Oltean {
975924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
9762ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
977b471a71eSClément Léger 	struct timespec64 ts;
978b471a71eSClément Léger 
979b471a71eSClément Léger 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
980b471a71eSClément Léger 
981b471a71eSClément Léger 	tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
982b471a71eSClément Léger 	if ((tod_in_ns & 0xffffffff) < timestamp)
983b471a71eSClément Léger 		full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
984b471a71eSClément Léger 				timestamp;
985b471a71eSClément Léger 	else
986b471a71eSClément Léger 		full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
987b471a71eSClément Léger 				timestamp;
988b471a71eSClément Léger 
989b471a71eSClément Léger 	shhwtstamps = skb_hwtstamps(skb);
990b471a71eSClément Léger 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
991b471a71eSClément Léger 	shhwtstamps->hwtstamp = full_ts_in_ns;
992b471a71eSClément Léger }
993b471a71eSClément Léger EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
994b471a71eSClément Léger 
995b471a71eSClément Léger int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
996b471a71eSClément Léger {
997924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
998924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
999924ee317SVladimir Oltean 	struct net_device *dev;
1000924ee317SVladimir Oltean 	struct sk_buff *skb;
1001924ee317SVladimir Oltean 	int sz, buf_len;
1002924ee317SVladimir Oltean 	u32 val, *buf;
1003924ee317SVladimir Oltean 	int err;
1004924ee317SVladimir Oltean 
1005924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1006924ee317SVladimir Oltean 	if (err)
1007924ee317SVladimir Oltean 		return err;
1008924ee317SVladimir Oltean 
1009924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
1010924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
1011924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1012924ee317SVladimir Oltean 
1013924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1014924ee317SVladimir Oltean 		return -EINVAL;
1015924ee317SVladimir Oltean 
1016924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1017924ee317SVladimir Oltean 	if (!dev)
1018924ee317SVladimir Oltean 		return -EINVAL;
1019924ee317SVladimir Oltean 
1020924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
1021924ee317SVladimir Oltean 	if (unlikely(!skb)) {
1022924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
1023924ee317SVladimir Oltean 		return -ENOMEM;
1024924ee317SVladimir Oltean 	}
1025924ee317SVladimir Oltean 
1026924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
1027924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
1028924ee317SVladimir Oltean 
1029924ee317SVladimir Oltean 	len = 0;
1030924ee317SVladimir Oltean 	do {
1031924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1032924ee317SVladimir Oltean 		if (sz < 0) {
1033924ee317SVladimir Oltean 			err = sz;
1034924ee317SVladimir Oltean 			goto out_free_skb;
1035924ee317SVladimir Oltean 		}
1036924ee317SVladimir Oltean 		*buf++ = val;
1037924ee317SVladimir Oltean 		len += sz;
1038924ee317SVladimir Oltean 	} while (len < buf_len);
1039924ee317SVladimir Oltean 
1040924ee317SVladimir Oltean 	/* Read the FCS */
1041924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1042924ee317SVladimir Oltean 	if (sz < 0) {
1043924ee317SVladimir Oltean 		err = sz;
1044924ee317SVladimir Oltean 		goto out_free_skb;
1045924ee317SVladimir Oltean 	}
1046924ee317SVladimir Oltean 
1047924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1048924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1049924ee317SVladimir Oltean 
1050924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1051924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1052924ee317SVladimir Oltean 		*buf = val;
1053924ee317SVladimir Oltean 	}
1054924ee317SVladimir Oltean 
1055b471a71eSClément Léger 	if (ocelot->ptp)
1056b471a71eSClément Léger 		ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
1057924ee317SVladimir Oltean 
1058924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1059924ee317SVladimir Oltean 	 * has already been forwarded.
1060924ee317SVladimir Oltean 	 */
1061df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1062924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1063924ee317SVladimir Oltean 
1064924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1065d8ea7ff3SHoratiu Vultur 
1066924ee317SVladimir Oltean 	*nskb = skb;
1067924ee317SVladimir Oltean 
1068924ee317SVladimir Oltean 	return 0;
1069924ee317SVladimir Oltean 
1070924ee317SVladimir Oltean out_free_skb:
1071924ee317SVladimir Oltean 	kfree_skb(skb);
1072924ee317SVladimir Oltean 	return err;
1073924ee317SVladimir Oltean }
1074924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1075924ee317SVladimir Oltean 
1076137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1077137ffbc4SVladimir Oltean {
1078137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1079137ffbc4SVladimir Oltean 
1080137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1081137ffbc4SVladimir Oltean 		return false;
1082137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1083137ffbc4SVladimir Oltean 		return false;
1084137ffbc4SVladimir Oltean 
1085137ffbc4SVladimir Oltean 	return true;
1086137ffbc4SVladimir Oltean }
1087137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1088137ffbc4SVladimir Oltean 
1089e5150f00SClément Léger void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag)
1090e5150f00SClément Léger {
1091e5150f00SClément Léger 	ocelot_ifh_set_bypass(ifh, 1);
1092e5150f00SClément Léger 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1093e5150f00SClément Léger 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1094e5150f00SClément Léger 	if (vlan_tag)
1095e5150f00SClément Léger 		ocelot_ifh_set_vlan_tci(ifh, vlan_tag);
1096e5150f00SClément Léger 	if (rew_op)
1097e5150f00SClément Léger 		ocelot_ifh_set_rew_op(ifh, rew_op);
1098e5150f00SClément Léger }
1099e5150f00SClément Léger EXPORT_SYMBOL(ocelot_ifh_port_set);
1100e5150f00SClément Léger 
1101137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1102137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1103137ffbc4SVladimir Oltean {
110440d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1105137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1106137ffbc4SVladimir Oltean 
1107137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1108137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1109137ffbc4SVladimir Oltean 
1110e5150f00SClément Léger 	ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
1111137ffbc4SVladimir Oltean 
1112137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
111340d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1114137ffbc4SVladimir Oltean 
1115137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1116137ffbc4SVladimir Oltean 	last = skb->len % 4;
1117137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1118137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1119137ffbc4SVladimir Oltean 
1120137ffbc4SVladimir Oltean 	/* Add padding */
1121137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1122137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1123137ffbc4SVladimir Oltean 		i++;
1124137ffbc4SVladimir Oltean 	}
1125137ffbc4SVladimir Oltean 
1126137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1127137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1128137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1129137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1130137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1131137ffbc4SVladimir Oltean 
1132137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1133137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1134137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1135137ffbc4SVladimir Oltean 
1136137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1137137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1138137ffbc4SVladimir Oltean }
1139137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1140137ffbc4SVladimir Oltean 
11410a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
11420a6f17c6SVladimir Oltean {
11430a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
11440a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
11450a6f17c6SVladimir Oltean }
11460a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
11470a6f17c6SVladimir Oltean 
114854c31984SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, const unsigned char *addr,
114954c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1150a556c76aSAlexandre Belloni {
115154c31984SVladimir Oltean 	if (!vid)
115254c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
115354c31984SVladimir Oltean 
1154e9b3ba43SVladimir Oltean 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
1155a556c76aSAlexandre Belloni }
11565e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1157a556c76aSAlexandre Belloni 
115854c31984SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, const unsigned char *addr,
115954c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1160531ee1a6SVladimir Oltean {
116154c31984SVladimir Oltean 	if (!vid)
116254c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
116354c31984SVladimir Oltean 
1164531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1165531ee1a6SVladimir Oltean }
11665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1167531ee1a6SVladimir Oltean 
11682468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
1169531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1170a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1171a556c76aSAlexandre Belloni {
1172a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1173531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1174a556c76aSAlexandre Belloni 
1175a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1176a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1177a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1178a556c76aSAlexandre Belloni 
1179a556c76aSAlexandre Belloni 	/* Issue a read command */
1180a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1181a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1182a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1183a556c76aSAlexandre Belloni 
1184a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1185a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1186a556c76aSAlexandre Belloni 
1187a556c76aSAlexandre Belloni 	/* Read the entry flags */
1188a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1189a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1190a556c76aSAlexandre Belloni 		return -EINVAL;
1191a556c76aSAlexandre Belloni 
1192a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1193a556c76aSAlexandre Belloni 	 * do not report it.
1194a556c76aSAlexandre Belloni 	 */
1195a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1196531ee1a6SVladimir Oltean 	if (dst != port)
1197a556c76aSAlexandre Belloni 		return -EINVAL;
1198a556c76aSAlexandre Belloni 
1199a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1200a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1201a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1202a556c76aSAlexandre Belloni 
1203a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1204a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1205a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1206a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1207a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1208a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1209a556c76aSAlexandre Belloni 
1210a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1211a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1212a556c76aSAlexandre Belloni 
1213a556c76aSAlexandre Belloni 	return 0;
1214a556c76aSAlexandre Belloni }
1215a556c76aSAlexandre Belloni 
12165cad43a5SVladimir Oltean int ocelot_mact_flush(struct ocelot *ocelot, int port)
12175cad43a5SVladimir Oltean {
12185cad43a5SVladimir Oltean 	int err;
12195cad43a5SVladimir Oltean 
12205cad43a5SVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
12215cad43a5SVladimir Oltean 
12225cad43a5SVladimir Oltean 	/* Program ageing filter for a single port */
12235cad43a5SVladimir Oltean 	ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
12245cad43a5SVladimir Oltean 		     ANA_ANAGEFIL);
12255cad43a5SVladimir Oltean 
12265cad43a5SVladimir Oltean 	/* Flushing dynamic FDB entries requires two successive age scans */
12275cad43a5SVladimir Oltean 	ocelot_write(ocelot,
12285cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
12295cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
12305cad43a5SVladimir Oltean 
12315cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
12325cad43a5SVladimir Oltean 	if (err) {
12335cad43a5SVladimir Oltean 		mutex_unlock(&ocelot->mact_lock);
12345cad43a5SVladimir Oltean 		return err;
12355cad43a5SVladimir Oltean 	}
12365cad43a5SVladimir Oltean 
12375cad43a5SVladimir Oltean 	/* And second... */
12385cad43a5SVladimir Oltean 	ocelot_write(ocelot,
12395cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
12405cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
12415cad43a5SVladimir Oltean 
12425cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
12435cad43a5SVladimir Oltean 
12445cad43a5SVladimir Oltean 	/* Restore ageing filter */
12455cad43a5SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_ANAGEFIL);
12465cad43a5SVladimir Oltean 
12475cad43a5SVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
12485cad43a5SVladimir Oltean 
12495cad43a5SVladimir Oltean 	return err;
12505cad43a5SVladimir Oltean }
12515cad43a5SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_mact_flush);
12525cad43a5SVladimir Oltean 
12535e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1254531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1255a556c76aSAlexandre Belloni {
12562468346cSVladimir Oltean 	int err = 0;
1257531ee1a6SVladimir Oltean 	int i, j;
1258a556c76aSAlexandre Belloni 
12592468346cSVladimir Oltean 	/* We could take the lock just around ocelot_mact_read, but doing so
12602468346cSVladimir Oltean 	 * thousands of times in a row seems rather pointless and inefficient.
12612468346cSVladimir Oltean 	 */
12622468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
12632468346cSVladimir Oltean 
126421ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
126521ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1266a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1267531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1268531ee1a6SVladimir Oltean 			bool is_static;
1269531ee1a6SVladimir Oltean 
12702468346cSVladimir Oltean 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1271a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1272a556c76aSAlexandre Belloni 			 * skip it.
1273a556c76aSAlexandre Belloni 			 */
12742468346cSVladimir Oltean 			if (err == -EINVAL)
1275a556c76aSAlexandre Belloni 				continue;
12762468346cSVladimir Oltean 			else if (err)
12772468346cSVladimir Oltean 				break;
1278a556c76aSAlexandre Belloni 
1279531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1280531ee1a6SVladimir Oltean 
128154c31984SVladimir Oltean 			/* Hide the reserved VLANs used for
128254c31984SVladimir Oltean 			 * VLAN-unaware bridging.
128354c31984SVladimir Oltean 			 */
128454c31984SVladimir Oltean 			if (entry.vid > OCELOT_RSV_VLAN_RANGE_START)
128554c31984SVladimir Oltean 				entry.vid = 0;
128654c31984SVladimir Oltean 
12872468346cSVladimir Oltean 			err = cb(entry.mac, entry.vid, is_static, data);
12882468346cSVladimir Oltean 			if (err)
12892468346cSVladimir Oltean 				break;
1290a556c76aSAlexandre Belloni 		}
1291a556c76aSAlexandre Belloni 	}
1292a556c76aSAlexandre Belloni 
12932468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
12942468346cSVladimir Oltean 
12952468346cSVladimir Oltean 	return err;
1296531ee1a6SVladimir Oltean }
12975e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1298531ee1a6SVladimir Oltean 
12999d75b881SVladimir Oltean int ocelot_trap_add(struct ocelot *ocelot, int port,
13009d75b881SVladimir Oltean 		    unsigned long cookie, bool take_ts,
130196ca08c0SVladimir Oltean 		    void (*populate)(struct ocelot_vcap_filter *f))
130296ca08c0SVladimir Oltean {
130396ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
130496ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
130596ca08c0SVladimir Oltean 	bool new = false;
130696ca08c0SVladimir Oltean 	int err;
130796ca08c0SVladimir Oltean 
130896ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
130996ca08c0SVladimir Oltean 
131096ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
131196ca08c0SVladimir Oltean 						   false);
131296ca08c0SVladimir Oltean 	if (!trap) {
131396ca08c0SVladimir Oltean 		trap = kzalloc(sizeof(*trap), GFP_KERNEL);
131496ca08c0SVladimir Oltean 		if (!trap)
131596ca08c0SVladimir Oltean 			return -ENOMEM;
131696ca08c0SVladimir Oltean 
131796ca08c0SVladimir Oltean 		populate(trap);
131896ca08c0SVladimir Oltean 		trap->prio = 1;
131996ca08c0SVladimir Oltean 		trap->id.cookie = cookie;
132096ca08c0SVladimir Oltean 		trap->id.tc_offload = false;
132196ca08c0SVladimir Oltean 		trap->block_id = VCAP_IS2;
132296ca08c0SVladimir Oltean 		trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
132396ca08c0SVladimir Oltean 		trap->lookup = 0;
132496ca08c0SVladimir Oltean 		trap->action.cpu_copy_ena = true;
132596ca08c0SVladimir Oltean 		trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
132696ca08c0SVladimir Oltean 		trap->action.port_mask = 0;
13279d75b881SVladimir Oltean 		trap->take_ts = take_ts;
1328e1846cffSVladimir Oltean 		trap->is_trap = true;
132996ca08c0SVladimir Oltean 		new = true;
133096ca08c0SVladimir Oltean 	}
133196ca08c0SVladimir Oltean 
133296ca08c0SVladimir Oltean 	trap->ingress_port_mask |= BIT(port);
133396ca08c0SVladimir Oltean 
133496ca08c0SVladimir Oltean 	if (new)
133596ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_add(ocelot, trap, NULL);
133696ca08c0SVladimir Oltean 	else
133796ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
133896ca08c0SVladimir Oltean 	if (err) {
133996ca08c0SVladimir Oltean 		trap->ingress_port_mask &= ~BIT(port);
1340e1846cffSVladimir Oltean 		if (!trap->ingress_port_mask)
134196ca08c0SVladimir Oltean 			kfree(trap);
134296ca08c0SVladimir Oltean 		return err;
134396ca08c0SVladimir Oltean 	}
134496ca08c0SVladimir Oltean 
134596ca08c0SVladimir Oltean 	return 0;
134696ca08c0SVladimir Oltean }
134796ca08c0SVladimir Oltean 
1348b9bace6eSVladimir Oltean int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie)
134996ca08c0SVladimir Oltean {
135096ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
135196ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
135296ca08c0SVladimir Oltean 
135396ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
135496ca08c0SVladimir Oltean 
135596ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
135696ca08c0SVladimir Oltean 						   false);
135796ca08c0SVladimir Oltean 	if (!trap)
135896ca08c0SVladimir Oltean 		return 0;
135996ca08c0SVladimir Oltean 
136096ca08c0SVladimir Oltean 	trap->ingress_port_mask &= ~BIT(port);
1361e1846cffSVladimir Oltean 	if (!trap->ingress_port_mask)
136296ca08c0SVladimir Oltean 		return ocelot_vcap_filter_del(ocelot, trap);
136396ca08c0SVladimir Oltean 
136496ca08c0SVladimir Oltean 	return ocelot_vcap_filter_replace(ocelot, trap);
136596ca08c0SVladimir Oltean }
136696ca08c0SVladimir Oltean 
1367a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
1368b80af659SVladimir Oltean {
1369b80af659SVladimir Oltean 	u32 mask = 0;
1370b80af659SVladimir Oltean 	int port;
1371b80af659SVladimir Oltean 
1372961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
1373961d8b69SVladimir Oltean 
1374b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1375b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1376b80af659SVladimir Oltean 
1377b80af659SVladimir Oltean 		if (!ocelot_port)
1378b80af659SVladimir Oltean 			continue;
1379b80af659SVladimir Oltean 
1380a14e6b69SVladimir Oltean 		if (ocelot_port->bond == bond)
1381b80af659SVladimir Oltean 			mask |= BIT(port);
1382b80af659SVladimir Oltean 	}
1383b80af659SVladimir Oltean 
1384b80af659SVladimir Oltean 	return mask;
1385b80af659SVladimir Oltean }
1386b80af659SVladimir Oltean 
1387961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical
1388961d8b69SVladimir Oltean  * port ID present in that LAG. It may change if that port ever leaves the LAG.
1389961d8b69SVladimir Oltean  */
1390eca70102SVladimir Oltean int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond)
1391961d8b69SVladimir Oltean {
1392961d8b69SVladimir Oltean 	int bond_mask = ocelot_get_bond_mask(ocelot, bond);
1393961d8b69SVladimir Oltean 
1394961d8b69SVladimir Oltean 	if (!bond_mask)
1395961d8b69SVladimir Oltean 		return -ENOENT;
1396961d8b69SVladimir Oltean 
1397961d8b69SVladimir Oltean 	return __ffs(bond_mask);
1398961d8b69SVladimir Oltean }
1399eca70102SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bond_get_id);
1400961d8b69SVladimir Oltean 
1401291ac151SVladimir Oltean /* Returns the mask of user ports assigned to this DSA tag_8021q CPU port.
1402291ac151SVladimir Oltean  * Note that when CPU ports are in a LAG, the user ports are assigned to the
1403291ac151SVladimir Oltean  * 'primary' CPU port, the one whose physical port number gives the logical
1404291ac151SVladimir Oltean  * port number of the LAG.
1405291ac151SVladimir Oltean  *
1406291ac151SVladimir Oltean  * We leave PGID_SRC poorly configured for the 'secondary' CPU port in the LAG
1407291ac151SVladimir Oltean  * (to which no user port is assigned), but it appears that forwarding from
1408291ac151SVladimir Oltean  * this secondary CPU port looks at the PGID_SRC associated with the logical
1409291ac151SVladimir Oltean  * port ID that it's assigned to, which *is* configured properly.
1410291ac151SVladimir Oltean  */
1411c295f983SVladimir Oltean static u32 ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot *ocelot,
1412c295f983SVladimir Oltean 					       struct ocelot_port *cpu)
1413c295f983SVladimir Oltean {
1414c295f983SVladimir Oltean 	u32 mask = 0;
1415c295f983SVladimir Oltean 	int port;
1416c295f983SVladimir Oltean 
1417c295f983SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1418c295f983SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1419c295f983SVladimir Oltean 
1420c295f983SVladimir Oltean 		if (!ocelot_port)
1421c295f983SVladimir Oltean 			continue;
1422c295f983SVladimir Oltean 
1423c295f983SVladimir Oltean 		if (ocelot_port->dsa_8021q_cpu == cpu)
1424c295f983SVladimir Oltean 			mask |= BIT(port);
1425c295f983SVladimir Oltean 	}
1426c295f983SVladimir Oltean 
1427291ac151SVladimir Oltean 	if (cpu->bond)
1428291ac151SVladimir Oltean 		mask &= ~ocelot_get_bond_mask(ocelot, cpu->bond);
1429291ac151SVladimir Oltean 
1430c295f983SVladimir Oltean 	return mask;
1431c295f983SVladimir Oltean }
1432c295f983SVladimir Oltean 
1433291ac151SVladimir Oltean /* Returns the DSA tag_8021q CPU port that the given port is assigned to,
1434291ac151SVladimir Oltean  * or the bit mask of CPU ports if said CPU port is in a LAG.
1435291ac151SVladimir Oltean  */
1436c295f983SVladimir Oltean u32 ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot *ocelot, int port)
1437c295f983SVladimir Oltean {
1438c295f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1439c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot_port->dsa_8021q_cpu;
1440c295f983SVladimir Oltean 
1441c295f983SVladimir Oltean 	if (!cpu_port)
1442c295f983SVladimir Oltean 		return 0;
1443c295f983SVladimir Oltean 
1444291ac151SVladimir Oltean 	if (cpu_port->bond)
1445291ac151SVladimir Oltean 		return ocelot_get_bond_mask(ocelot, cpu_port->bond);
1446291ac151SVladimir Oltean 
1447c295f983SVladimir Oltean 	return BIT(cpu_port->index);
1448c295f983SVladimir Oltean }
1449c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assigned_dsa_8021q_cpu_mask);
1450c295f983SVladimir Oltean 
14518abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
1452df291e54SVladimir Oltean {
1453acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1454a8bd9fa5SVladimir Oltean 	const struct net_device *bridge;
1455df291e54SVladimir Oltean 	u32 mask = 0;
1456df291e54SVladimir Oltean 	int port;
1457df291e54SVladimir Oltean 
1458a8bd9fa5SVladimir Oltean 	if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
1459a8bd9fa5SVladimir Oltean 		return 0;
1460a8bd9fa5SVladimir Oltean 
1461a8bd9fa5SVladimir Oltean 	bridge = ocelot_port->bridge;
1462a8bd9fa5SVladimir Oltean 	if (!bridge)
1463acc64f52SVladimir Oltean 		return 0;
1464acc64f52SVladimir Oltean 
1465df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1466acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1467df291e54SVladimir Oltean 
1468df291e54SVladimir Oltean 		if (!ocelot_port)
1469df291e54SVladimir Oltean 			continue;
1470df291e54SVladimir Oltean 
1471df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1472df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1473df291e54SVladimir Oltean 			mask |= BIT(port);
1474df291e54SVladimir Oltean 	}
1475df291e54SVladimir Oltean 
1476df291e54SVladimir Oltean 	return mask;
1477df291e54SVladimir Oltean }
14788abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
1479df291e54SVladimir Oltean 
1480a72e23ddSVladimir Oltean static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
1481e21268efSVladimir Oltean {
1482e21268efSVladimir Oltean 	int port;
1483e21268efSVladimir Oltean 
14848abe1970SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
14858abe1970SVladimir Oltean 
14868abe1970SVladimir Oltean 	/* If cut-through forwarding is supported, update the masks before a
14878abe1970SVladimir Oltean 	 * port joins the forwarding domain, to avoid potential underruns if it
14888abe1970SVladimir Oltean 	 * has the highest speed from the new domain.
14898abe1970SVladimir Oltean 	 */
14908abe1970SVladimir Oltean 	if (joining && ocelot->ops->cut_through_fwd)
14918abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
14928abe1970SVladimir Oltean 
14939b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
14949b521250SVladimir Oltean 	 * a source for the other ports.
14959b521250SVladimir Oltean 	 */
14969b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1497e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1498e21268efSVladimir Oltean 		unsigned long mask;
1499e21268efSVladimir Oltean 
1500e21268efSVladimir Oltean 		if (!ocelot_port) {
1501e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1502e21268efSVladimir Oltean 			mask = 0;
1503e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1504e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1505c295f983SVladimir Oltean 			 * forward packets to all ports assigned to them.
1506e21268efSVladimir Oltean 			 */
1507c295f983SVladimir Oltean 			mask = ocelot_dsa_8021q_cpu_assigned_ports(ocelot,
1508c295f983SVladimir Oltean 								   ocelot_port);
1509df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1510528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
15119b521250SVladimir Oltean 
1512a8bd9fa5SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
1513df291e54SVladimir Oltean 			mask &= ~BIT(port);
1514c295f983SVladimir Oltean 
1515c295f983SVladimir Oltean 			mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1516c295f983SVladimir Oltean 									port);
1517c295f983SVladimir Oltean 
1518a14e6b69SVladimir Oltean 			if (bond)
1519a14e6b69SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond);
15209b521250SVladimir Oltean 		} else {
1521e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1522e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1523e21268efSVladimir Oltean 			 * module otherwise.
1524e21268efSVladimir Oltean 			 */
1525c295f983SVladimir Oltean 			mask = ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1526c295f983SVladimir Oltean 								       port);
1527e21268efSVladimir Oltean 		}
1528e21268efSVladimir Oltean 
1529e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
15309b521250SVladimir Oltean 	}
15318abe1970SVladimir Oltean 
15328abe1970SVladimir Oltean 	/* If cut-through forwarding is supported and a port is leaving, there
15338abe1970SVladimir Oltean 	 * is a chance that cut-through was disabled on the other ports due to
15348abe1970SVladimir Oltean 	 * the port which is leaving (it has a higher link speed). We need to
15358abe1970SVladimir Oltean 	 * update the cut-through masks of the remaining ports no earlier than
15368abe1970SVladimir Oltean 	 * after the port has left, to prevent underruns from happening between
15378abe1970SVladimir Oltean 	 * the cut-through update and the forwarding domain update.
15388abe1970SVladimir Oltean 	 */
15398abe1970SVladimir Oltean 	if (!joining && ocelot->ops->cut_through_fwd)
15408abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
15419b521250SVladimir Oltean }
15429b521250SVladimir Oltean 
154361be79baSVladimir Oltean /* Update PGID_CPU which is the destination port mask used for whitelisting
154461be79baSVladimir Oltean  * unicast addresses filtered towards the host. In the normal and NPI modes,
154561be79baSVladimir Oltean  * this points to the analyzer entry for the CPU port module, while in DSA
154661be79baSVladimir Oltean  * tag_8021q mode, it is a bit mask of all active CPU ports.
154761be79baSVladimir Oltean  * PGID_SRC will take care of forwarding a packet from one user port to
154861be79baSVladimir Oltean  * no more than a single CPU port.
154961be79baSVladimir Oltean  */
155061be79baSVladimir Oltean static void ocelot_update_pgid_cpu(struct ocelot *ocelot)
155161be79baSVladimir Oltean {
155261be79baSVladimir Oltean 	int pgid_cpu = 0;
155361be79baSVladimir Oltean 	int port;
155461be79baSVladimir Oltean 
155561be79baSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
155661be79baSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
155761be79baSVladimir Oltean 
155861be79baSVladimir Oltean 		if (!ocelot_port || !ocelot_port->is_dsa_8021q_cpu)
155961be79baSVladimir Oltean 			continue;
156061be79baSVladimir Oltean 
156161be79baSVladimir Oltean 		pgid_cpu |= BIT(port);
156261be79baSVladimir Oltean 	}
156361be79baSVladimir Oltean 
156461be79baSVladimir Oltean 	if (!pgid_cpu)
156561be79baSVladimir Oltean 		pgid_cpu = BIT(ocelot->num_phys_ports);
156661be79baSVladimir Oltean 
156761be79baSVladimir Oltean 	ocelot_write_rix(ocelot, pgid_cpu, ANA_PGID_PGID, PGID_CPU);
156861be79baSVladimir Oltean }
156961be79baSVladimir Oltean 
157036a0bf44SVladimir Oltean void ocelot_port_setup_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
157154c31984SVladimir Oltean {
1572c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
157354c31984SVladimir Oltean 	u16 vid;
157454c31984SVladimir Oltean 
15758c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
15768c166acbSVladimir Oltean 
1577c295f983SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = true;
157854c31984SVladimir Oltean 
157954c31984SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
1580c295f983SVladimir Oltean 		ocelot_vlan_member_add(ocelot, cpu, vid, true);
158161be79baSVladimir Oltean 
158261be79baSVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
1583a72e23ddSVladimir Oltean 
158436a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
158536a0bf44SVladimir Oltean }
158636a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_setup_dsa_8021q_cpu);
158736a0bf44SVladimir Oltean 
158836a0bf44SVladimir Oltean void ocelot_port_teardown_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
158936a0bf44SVladimir Oltean {
159036a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
159136a0bf44SVladimir Oltean 	u16 vid;
159236a0bf44SVladimir Oltean 
159336a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
159436a0bf44SVladimir Oltean 
159536a0bf44SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = false;
159636a0bf44SVladimir Oltean 
159736a0bf44SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
159836a0bf44SVladimir Oltean 		ocelot_vlan_member_del(ocelot, cpu_port->index, vid);
159936a0bf44SVladimir Oltean 
160036a0bf44SVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
160136a0bf44SVladimir Oltean 
160236a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
160336a0bf44SVladimir Oltean }
160436a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_teardown_dsa_8021q_cpu);
160536a0bf44SVladimir Oltean 
160636a0bf44SVladimir Oltean void ocelot_port_assign_dsa_8021q_cpu(struct ocelot *ocelot, int port,
160736a0bf44SVladimir Oltean 				      int cpu)
160836a0bf44SVladimir Oltean {
160936a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
161036a0bf44SVladimir Oltean 
161136a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
161236a0bf44SVladimir Oltean 
161336a0bf44SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = cpu_port;
1614a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
16158c166acbSVladimir Oltean 
16168c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
161754c31984SVladimir Oltean }
1618c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assign_dsa_8021q_cpu);
161954c31984SVladimir Oltean 
1620c295f983SVladimir Oltean void ocelot_port_unassign_dsa_8021q_cpu(struct ocelot *ocelot, int port)
162154c31984SVladimir Oltean {
16228c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
16238c166acbSVladimir Oltean 
1624c295f983SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = NULL;
1625a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
16268c166acbSVladimir Oltean 
16278c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
162854c31984SVladimir Oltean }
1629c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_unassign_dsa_8021q_cpu);
163054c31984SVladimir Oltean 
16315e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1632a556c76aSAlexandre Belloni {
1633421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1634df291e54SVladimir Oltean 	u32 learn_ena = 0;
1635a556c76aSAlexandre Belloni 
16368abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
16378abe1970SVladimir Oltean 
1638df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1639a556c76aSAlexandre Belloni 
1640df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1641df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1642df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1643a556c76aSAlexandre Belloni 
1644df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1645df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1646a556c76aSAlexandre Belloni 
16478abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
16488abe1970SVladimir Oltean 
16498abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
1650a556c76aSAlexandre Belloni }
16515e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1652a556c76aSAlexandre Belloni 
16535e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
16544bda1415SVladimir Oltean {
1655c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1656c0d7eccbSVladimir Oltean 
1657c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1658c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1659c0d7eccbSVladimir Oltean 	 */
1660c0d7eccbSVladimir Oltean 	if (!age_period)
1661c0d7eccbSVladimir Oltean 		age_period = 1;
1662c0d7eccbSVladimir Oltean 
1663c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1664a556c76aSAlexandre Belloni }
16655e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1666a556c76aSAlexandre Belloni 
1667a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1668a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1669a556c76aSAlexandre Belloni 						     u16 vid)
1670a556c76aSAlexandre Belloni {
1671a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1672a556c76aSAlexandre Belloni 
1673a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1674a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1675a556c76aSAlexandre Belloni 			return mc;
1676a556c76aSAlexandre Belloni 	}
1677a556c76aSAlexandre Belloni 
1678a556c76aSAlexandre Belloni 	return NULL;
1679a556c76aSAlexandre Belloni }
1680a556c76aSAlexandre Belloni 
16819403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
16829403c158SVladimir Oltean {
16839403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
16849403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
16859403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
16869403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
16877c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
16889403c158SVladimir Oltean }
16899403c158SVladimir Oltean 
1690e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1691e5d1f896SVladimir Oltean 					     unsigned long ports)
1692e5d1f896SVladimir Oltean {
1693e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1694e5d1f896SVladimir Oltean 
1695e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1696e5d1f896SVladimir Oltean 	if (!pgid)
1697e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1698e5d1f896SVladimir Oltean 
1699e5d1f896SVladimir Oltean 	pgid->ports = ports;
1700e5d1f896SVladimir Oltean 	pgid->index = index;
1701e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1702e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1703e5d1f896SVladimir Oltean 
1704e5d1f896SVladimir Oltean 	return pgid;
1705e5d1f896SVladimir Oltean }
1706e5d1f896SVladimir Oltean 
1707e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1708e5d1f896SVladimir Oltean {
1709e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1710e5d1f896SVladimir Oltean 		return;
1711e5d1f896SVladimir Oltean 
1712e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1713e5d1f896SVladimir Oltean 	kfree(pgid);
1714e5d1f896SVladimir Oltean }
1715e5d1f896SVladimir Oltean 
1716e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1717bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
17189403c158SVladimir Oltean {
1719e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1720e5d1f896SVladimir Oltean 	int index;
17219403c158SVladimir Oltean 
17229403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
17239403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
17249403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
17259403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
17269403c158SVladimir Oltean 	 */
1727bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1728bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1729e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
17309403c158SVladimir Oltean 
1731e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1732e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1733e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1734e5d1f896SVladimir Oltean 		 */
1735e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1736e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1737e5d1f896SVladimir Oltean 			return pgid;
1738e5d1f896SVladimir Oltean 		}
1739e5d1f896SVladimir Oltean 	}
1740e5d1f896SVladimir Oltean 
1741e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1742e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
17439403c158SVladimir Oltean 		bool used = false;
17449403c158SVladimir Oltean 
1745e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1746e5d1f896SVladimir Oltean 			if (pgid->index == index) {
17479403c158SVladimir Oltean 				used = true;
17489403c158SVladimir Oltean 				break;
17499403c158SVladimir Oltean 			}
17509403c158SVladimir Oltean 		}
17519403c158SVladimir Oltean 
17529403c158SVladimir Oltean 		if (!used)
1753e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
17549403c158SVladimir Oltean 	}
17559403c158SVladimir Oltean 
1756e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
17579403c158SVladimir Oltean }
17589403c158SVladimir Oltean 
17599403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1760bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
17619403c158SVladimir Oltean {
1762ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
17639403c158SVladimir Oltean 
1764bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
17659403c158SVladimir Oltean 		addr[0] = 0;
17669403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
17679403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1768bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
17699403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
17709403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
17719403c158SVladimir Oltean 	}
17729403c158SVladimir Oltean }
17739403c158SVladimir Oltean 
1774209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
177554c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
177654c31984SVladimir Oltean 			const struct net_device *bridge)
1777a556c76aSAlexandre Belloni {
1778a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1779004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1780e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1781a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1782a556c76aSAlexandre Belloni 
178354c31984SVladimir Oltean 	if (!vid)
178454c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
178554c31984SVladimir Oltean 
1786a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1787a556c76aSAlexandre Belloni 	if (!mc) {
1788728e69aeSVladimir Oltean 		/* New entry */
1789bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1790bb8d53fdSVladimir Oltean 		if (!mc)
1791bb8d53fdSVladimir Oltean 			return -ENOMEM;
1792bb8d53fdSVladimir Oltean 
1793bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1794bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1795bb8d53fdSVladimir Oltean 		mc->vid = vid;
1796bb8d53fdSVladimir Oltean 
1797a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1798728e69aeSVladimir Oltean 	} else {
1799e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1800e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1801e5d1f896SVladimir Oltean 		 */
1802e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1803bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1804a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1805a556c76aSAlexandre Belloni 	}
1806a556c76aSAlexandre Belloni 
1807004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1808e5d1f896SVladimir Oltean 
1809e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1810e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1811e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1812e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1813e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1814e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1815e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1816e5d1f896SVladimir Oltean 	}
1817e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1818e5d1f896SVladimir Oltean 
1819bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1820a556c76aSAlexandre Belloni 
1821e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1822e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1823e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1824e5d1f896SVladimir Oltean 				 pgid->index);
1825e5d1f896SVladimir Oltean 
1826e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1827bb8d53fdSVladimir Oltean 				 mc->entry_type);
1828a556c76aSAlexandre Belloni }
1829209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1830a556c76aSAlexandre Belloni 
1831209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
183254c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
183354c31984SVladimir Oltean 			const struct net_device *bridge)
1834a556c76aSAlexandre Belloni {
1835a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1836004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1837e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1838a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1839a556c76aSAlexandre Belloni 
184054c31984SVladimir Oltean 	if (!vid)
184154c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
184254c31984SVladimir Oltean 
1843a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1844a556c76aSAlexandre Belloni 	if (!mc)
1845a556c76aSAlexandre Belloni 		return -ENOENT;
1846a556c76aSAlexandre Belloni 
1847bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1848a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1849a556c76aSAlexandre Belloni 
1850e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1851004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1852a556c76aSAlexandre Belloni 	if (!mc->ports) {
1853a556c76aSAlexandre Belloni 		list_del(&mc->list);
1854a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1855a556c76aSAlexandre Belloni 		return 0;
1856a556c76aSAlexandre Belloni 	}
1857a556c76aSAlexandre Belloni 
1858e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1859e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1860e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1861e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1862e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1863e5d1f896SVladimir Oltean 
1864bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1865a556c76aSAlexandre Belloni 
1866e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1867e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1868e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1869e5d1f896SVladimir Oltean 				 pgid->index);
1870e5d1f896SVladimir Oltean 
1871e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1872bb8d53fdSVladimir Oltean 				 mc->entry_type);
1873a556c76aSAlexandre Belloni }
1874209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1875a556c76aSAlexandre Belloni 
187654c31984SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
187754c31984SVladimir Oltean 			    struct net_device *bridge, int bridge_num,
187854c31984SVladimir Oltean 			    struct netlink_ext_ack *extack)
1879a556c76aSAlexandre Belloni {
1880df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
188154c31984SVladimir Oltean 	int err;
188254c31984SVladimir Oltean 
188354c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
188454c31984SVladimir Oltean 	if (err)
188554c31984SVladimir Oltean 		return err;
1886a556c76aSAlexandre Belloni 
18878abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
18888abe1970SVladimir Oltean 
1889df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
189054c31984SVladimir Oltean 	ocelot_port->bridge_num = bridge_num;
1891a556c76aSAlexandre Belloni 
18928abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
18938abe1970SVladimir Oltean 
18948abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
189554c31984SVladimir Oltean 
189654c31984SVladimir Oltean 	if (br_vlan_enabled(bridge))
189754c31984SVladimir Oltean 		return 0;
189854c31984SVladimir Oltean 
189954c31984SVladimir Oltean 	return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge);
1900a556c76aSAlexandre Belloni }
19015e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1902a556c76aSAlexandre Belloni 
1903e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1904a556c76aSAlexandre Belloni 			      struct net_device *bridge)
1905a556c76aSAlexandre Belloni {
1906df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
19072e554a7aSVladimir Oltean 
19088abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
19098abe1970SVladimir Oltean 
191054c31984SVladimir Oltean 	if (!br_vlan_enabled(bridge))
191154c31984SVladimir Oltean 		ocelot_del_vlan_unaware_pvid(ocelot, port, bridge);
191254c31984SVladimir Oltean 
1913df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
191454c31984SVladimir Oltean 	ocelot_port->bridge_num = -1;
19157142529fSAntoine Tenart 
1916d4004422SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, NULL);
19170da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
19188abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
19198abe1970SVladimir Oltean 
19208abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
1921a556c76aSAlexandre Belloni }
19225e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1923a556c76aSAlexandre Belloni 
1924dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1925dc96ee37SAlexandre Belloni {
1926528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1927dc96ee37SAlexandre Belloni 	int i, port, lag;
1928dc96ee37SAlexandre Belloni 
1929dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
193096b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1931dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1932dc96ee37SAlexandre Belloni 
193396b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1934dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1935dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1936dc96ee37SAlexandre Belloni 
1937528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1938528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1939528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1940528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1941528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1942528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1943528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1944528d3f19SVladimir Oltean 	 */
1945528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1946528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1947528d3f19SVladimir Oltean 
1948528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1949528d3f19SVladimir Oltean 			continue;
1950528d3f19SVladimir Oltean 
1951528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1952528d3f19SVladimir Oltean 	}
1953528d3f19SVladimir Oltean 
1954528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1955dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1956528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
195723ca3b72SVladimir Oltean 		int num_active_ports = 0;
1958dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1959dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1960dc96ee37SAlexandre Belloni 
1961528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1962dc96ee37SAlexandre Belloni 			continue;
1963dc96ee37SAlexandre Belloni 
1964a14e6b69SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond);
1965528d3f19SVladimir Oltean 
1966dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1967a14e6b69SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1968a14e6b69SVladimir Oltean 
1969dc96ee37SAlexandre Belloni 			// Destination mask
1970dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1971dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1972a14e6b69SVladimir Oltean 
1973a14e6b69SVladimir Oltean 			if (ocelot_port->lag_tx_active)
197423ca3b72SVladimir Oltean 				aggr_idx[num_active_ports++] = port;
1975dc96ee37SAlexandre Belloni 		}
1976dc96ee37SAlexandre Belloni 
197796b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1978dc96ee37SAlexandre Belloni 			u32 ac;
1979dc96ee37SAlexandre Belloni 
1980dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1981dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
198223ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
198323ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
198423ca3b72SVladimir Oltean 			 */
198523ca3b72SVladimir Oltean 			if (num_active_ports)
198623ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1987dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1988dc96ee37SAlexandre Belloni 		}
1989528d3f19SVladimir Oltean 
1990528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1991528d3f19SVladimir Oltean 		 * the same config again.
1992528d3f19SVladimir Oltean 		 */
1993528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1994528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1995528d3f19SVladimir Oltean 
1996528d3f19SVladimir Oltean 			if (!ocelot_port)
1997528d3f19SVladimir Oltean 				continue;
1998528d3f19SVladimir Oltean 
1999528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
2000528d3f19SVladimir Oltean 				visited |= BIT(port);
2001528d3f19SVladimir Oltean 		}
2002dc96ee37SAlexandre Belloni 	}
2003dc96ee37SAlexandre Belloni }
2004dc96ee37SAlexandre Belloni 
20052527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
20062527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
20072527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
20082527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
20092527f2e8SVladimir Oltean  */
20102527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
2011dc96ee37SAlexandre Belloni {
20122527f2e8SVladimir Oltean 	int port;
2013dc96ee37SAlexandre Belloni 
20142527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
20152527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
20162527f2e8SVladimir Oltean 		struct net_device *bond;
2017dc96ee37SAlexandre Belloni 
20182527f2e8SVladimir Oltean 		if (!ocelot_port)
20192527f2e8SVladimir Oltean 			continue;
2020dc96ee37SAlexandre Belloni 
20212527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
20222527f2e8SVladimir Oltean 		if (bond) {
2023961d8b69SVladimir Oltean 			int lag = ocelot_bond_get_id(ocelot, bond);
20242527f2e8SVladimir Oltean 
20252527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
2026dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
20272527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
20282527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
20292527f2e8SVladimir Oltean 		} else {
20302527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
20312527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
20322527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
20332527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
20342527f2e8SVladimir Oltean 		}
2035dc96ee37SAlexandre Belloni 	}
2036dc96ee37SAlexandre Belloni }
2037dc96ee37SAlexandre Belloni 
203828de0f9fSVladimir Oltean static int ocelot_migrate_mc(struct ocelot *ocelot, struct ocelot_multicast *mc,
203928de0f9fSVladimir Oltean 			     unsigned long from_mask, unsigned long to_mask)
204028de0f9fSVladimir Oltean {
204128de0f9fSVladimir Oltean 	unsigned char addr[ETH_ALEN];
204228de0f9fSVladimir Oltean 	struct ocelot_pgid *pgid;
204328de0f9fSVladimir Oltean 	u16 vid = mc->vid;
204428de0f9fSVladimir Oltean 
204528de0f9fSVladimir Oltean 	dev_dbg(ocelot->dev,
204628de0f9fSVladimir Oltean 		"Migrating multicast %pM vid %d from port mask 0x%lx to 0x%lx\n",
204728de0f9fSVladimir Oltean 		mc->addr, mc->vid, from_mask, to_mask);
204828de0f9fSVladimir Oltean 
204928de0f9fSVladimir Oltean 	/* First clean up the current port mask from hardware, because
205028de0f9fSVladimir Oltean 	 * we'll be modifying it.
205128de0f9fSVladimir Oltean 	 */
205228de0f9fSVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
205328de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
205428de0f9fSVladimir Oltean 	ocelot_mact_forget(ocelot, addr, vid);
205528de0f9fSVladimir Oltean 
205628de0f9fSVladimir Oltean 	mc->ports &= ~from_mask;
205728de0f9fSVladimir Oltean 	mc->ports |= to_mask;
205828de0f9fSVladimir Oltean 
205928de0f9fSVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
206028de0f9fSVladimir Oltean 	if (IS_ERR(pgid)) {
206128de0f9fSVladimir Oltean 		dev_err(ocelot->dev,
206228de0f9fSVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
206328de0f9fSVladimir Oltean 			mc->addr, mc->vid);
206428de0f9fSVladimir Oltean 		devm_kfree(ocelot->dev, mc);
206528de0f9fSVladimir Oltean 		return PTR_ERR(pgid);
206628de0f9fSVladimir Oltean 	}
206728de0f9fSVladimir Oltean 	mc->pgid = pgid;
206828de0f9fSVladimir Oltean 
206928de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
207028de0f9fSVladimir Oltean 
207128de0f9fSVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
207228de0f9fSVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
207328de0f9fSVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
207428de0f9fSVladimir Oltean 				 pgid->index);
207528de0f9fSVladimir Oltean 
207628de0f9fSVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
207728de0f9fSVladimir Oltean 				 mc->entry_type);
207828de0f9fSVladimir Oltean }
207928de0f9fSVladimir Oltean 
208028de0f9fSVladimir Oltean int ocelot_migrate_mdbs(struct ocelot *ocelot, unsigned long from_mask,
208128de0f9fSVladimir Oltean 			unsigned long to_mask)
208228de0f9fSVladimir Oltean {
208328de0f9fSVladimir Oltean 	struct ocelot_multicast *mc;
208428de0f9fSVladimir Oltean 	int err;
208528de0f9fSVladimir Oltean 
208628de0f9fSVladimir Oltean 	list_for_each_entry(mc, &ocelot->multicast, list) {
208728de0f9fSVladimir Oltean 		if (!(mc->ports & from_mask))
208828de0f9fSVladimir Oltean 			continue;
208928de0f9fSVladimir Oltean 
209028de0f9fSVladimir Oltean 		err = ocelot_migrate_mc(ocelot, mc, from_mask, to_mask);
209128de0f9fSVladimir Oltean 		if (err)
209228de0f9fSVladimir Oltean 			return err;
209328de0f9fSVladimir Oltean 	}
209428de0f9fSVladimir Oltean 
209528de0f9fSVladimir Oltean 	return 0;
209628de0f9fSVladimir Oltean }
209728de0f9fSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_migrate_mdbs);
209828de0f9fSVladimir Oltean 
2099961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says:
2100961d8b69SVladimir Oltean  *     Logical port number for front port. If port is not a member of a LLAG,
2101961d8b69SVladimir Oltean  *     then PORTID must be set to the physical port number.
2102961d8b69SVladimir Oltean  *     If port is a member of a LLAG, then PORTID must be set to the common
2103961d8b69SVladimir Oltean  *     PORTID_VAL used for all member ports of the LLAG.
2104961d8b69SVladimir Oltean  *     The value must not exceed the number of physical ports on the device.
2105961d8b69SVladimir Oltean  *
2106961d8b69SVladimir Oltean  * This means we have little choice but to migrate FDB entries pointing towards
2107961d8b69SVladimir Oltean  * a logical port when that changes.
2108961d8b69SVladimir Oltean  */
2109961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot,
2110961d8b69SVladimir Oltean 				    struct net_device *bond,
2111961d8b69SVladimir Oltean 				    int lag)
2112961d8b69SVladimir Oltean {
2113961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2114961d8b69SVladimir Oltean 	int err;
2115961d8b69SVladimir Oltean 
2116961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2117961d8b69SVladimir Oltean 
2118961d8b69SVladimir Oltean 	list_for_each_entry(fdb, &ocelot->lag_fdbs, list) {
2119961d8b69SVladimir Oltean 		if (fdb->bond != bond)
2120961d8b69SVladimir Oltean 			continue;
2121961d8b69SVladimir Oltean 
2122961d8b69SVladimir Oltean 		err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid);
2123961d8b69SVladimir Oltean 		if (err) {
2124961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2125961d8b69SVladimir Oltean 				"failed to delete LAG %s FDB %pM vid %d: %pe\n",
2126961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2127961d8b69SVladimir Oltean 		}
2128961d8b69SVladimir Oltean 
2129961d8b69SVladimir Oltean 		err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid,
2130961d8b69SVladimir Oltean 					ENTRYTYPE_LOCKED);
2131961d8b69SVladimir Oltean 		if (err) {
2132961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2133961d8b69SVladimir Oltean 				"failed to migrate LAG %s FDB %pM vid %d: %pe\n",
2134961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2135961d8b69SVladimir Oltean 		}
2136961d8b69SVladimir Oltean 	}
2137961d8b69SVladimir Oltean }
2138961d8b69SVladimir Oltean 
21399c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2140583cbbe3SVladimir Oltean 			 struct net_device *bond,
21412e359b00SVladimir Oltean 			 struct netdev_lag_upper_info *info,
21422e359b00SVladimir Oltean 			 struct netlink_ext_ack *extack)
2143dc96ee37SAlexandre Belloni {
21442e359b00SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
21452e359b00SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
21462e359b00SVladimir Oltean 				   "Can only offload LAG using hash TX type");
2147583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
21482e359b00SVladimir Oltean 	}
2149583cbbe3SVladimir Oltean 
21508abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
21518abe1970SVladimir Oltean 
2152b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
2153dc96ee37SAlexandre Belloni 
21542527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
21558abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2156dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2157dc96ee37SAlexandre Belloni 
21588abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
21598abe1970SVladimir Oltean 
2160dc96ee37SAlexandre Belloni 	return 0;
2161dc96ee37SAlexandre Belloni }
21629c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
2163dc96ee37SAlexandre Belloni 
21649c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2165dc96ee37SAlexandre Belloni 			   struct net_device *bond)
2166dc96ee37SAlexandre Belloni {
2167961d8b69SVladimir Oltean 	int old_lag_id, new_lag_id;
2168961d8b69SVladimir Oltean 
21698abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
21708abe1970SVladimir Oltean 
2171961d8b69SVladimir Oltean 	old_lag_id = ocelot_bond_get_id(ocelot, bond);
2172961d8b69SVladimir Oltean 
2173b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
2174b80af659SVladimir Oltean 
21752527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
21768abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2177dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
21788abe1970SVladimir Oltean 
2179961d8b69SVladimir Oltean 	new_lag_id = ocelot_bond_get_id(ocelot, bond);
2180961d8b69SVladimir Oltean 
2181961d8b69SVladimir Oltean 	if (new_lag_id >= 0 && old_lag_id != new_lag_id)
2182961d8b69SVladimir Oltean 		ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id);
2183961d8b69SVladimir Oltean 
21848abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2185dc96ee37SAlexandre Belloni }
21869c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
21870e332c85SPetr Machata 
218823ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
218923ca3b72SVladimir Oltean {
219023ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
219123ca3b72SVladimir Oltean 
2192961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2193961d8b69SVladimir Oltean 
219423ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
219523ca3b72SVladimir Oltean 
219623ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
219723ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
2198961d8b69SVladimir Oltean 
2199961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
220023ca3b72SVladimir Oltean }
220123ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
220223ca3b72SVladimir Oltean 
2203961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond,
220454c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
220554c31984SVladimir Oltean 		       const struct net_device *bridge)
2206961d8b69SVladimir Oltean {
2207961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2208961d8b69SVladimir Oltean 	int lag, err;
2209961d8b69SVladimir Oltean 
2210961d8b69SVladimir Oltean 	fdb = kzalloc(sizeof(*fdb), GFP_KERNEL);
2211961d8b69SVladimir Oltean 	if (!fdb)
2212961d8b69SVladimir Oltean 		return -ENOMEM;
2213961d8b69SVladimir Oltean 
221454c31984SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
221554c31984SVladimir Oltean 
221654c31984SVladimir Oltean 	if (!vid)
221754c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
221854c31984SVladimir Oltean 
2219961d8b69SVladimir Oltean 	ether_addr_copy(fdb->addr, addr);
2220961d8b69SVladimir Oltean 	fdb->vid = vid;
2221961d8b69SVladimir Oltean 	fdb->bond = bond;
2222961d8b69SVladimir Oltean 
2223961d8b69SVladimir Oltean 	lag = ocelot_bond_get_id(ocelot, bond);
2224961d8b69SVladimir Oltean 
2225961d8b69SVladimir Oltean 	err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED);
2226961d8b69SVladimir Oltean 	if (err) {
2227961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2228961d8b69SVladimir Oltean 		kfree(fdb);
2229961d8b69SVladimir Oltean 		return err;
2230961d8b69SVladimir Oltean 	}
2231961d8b69SVladimir Oltean 
2232961d8b69SVladimir Oltean 	list_add_tail(&fdb->list, &ocelot->lag_fdbs);
2233961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2234961d8b69SVladimir Oltean 
2235961d8b69SVladimir Oltean 	return 0;
2236961d8b69SVladimir Oltean }
2237961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add);
2238961d8b69SVladimir Oltean 
2239961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond,
224054c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
224154c31984SVladimir Oltean 		       const struct net_device *bridge)
2242961d8b69SVladimir Oltean {
2243961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb, *tmp;
2244961d8b69SVladimir Oltean 
2245961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2246961d8b69SVladimir Oltean 
224754c31984SVladimir Oltean 	if (!vid)
224854c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
224954c31984SVladimir Oltean 
2250961d8b69SVladimir Oltean 	list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) {
2251961d8b69SVladimir Oltean 		if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid ||
2252961d8b69SVladimir Oltean 		    fdb->bond != bond)
2253961d8b69SVladimir Oltean 			continue;
2254961d8b69SVladimir Oltean 
2255961d8b69SVladimir Oltean 		ocelot_mact_forget(ocelot, addr, vid);
2256961d8b69SVladimir Oltean 		list_del(&fdb->list);
2257961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2258961d8b69SVladimir Oltean 		kfree(fdb);
2259961d8b69SVladimir Oltean 
2260961d8b69SVladimir Oltean 		return 0;
2261961d8b69SVladimir Oltean 	}
2262961d8b69SVladimir Oltean 
2263961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2264961d8b69SVladimir Oltean 
2265961d8b69SVladimir Oltean 	return -ENOENT;
2266961d8b69SVladimir Oltean }
2267961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del);
2268961d8b69SVladimir Oltean 
2269a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2270a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
22710b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
22720b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
22730b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2274a8015dedSVladimir Oltean  */
22750b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
227631350d7fSVladimir Oltean {
227731350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2278a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2279e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
2280601e984fSVladimir Oltean 	int atop, atop_tot;
228131350d7fSVladimir Oltean 
22820b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
22830b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
22840b912fc9SVladimir Oltean 
2285cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
22860b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2287cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
22880b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
22890b912fc9SVladimir Oltean 	}
22900b912fc9SVladimir Oltean 
2291a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2292fa914e9cSVladimir Oltean 
2293e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
2294e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2295e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2296541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2297541132f0SMaxim Kochetkov 			    pause_start);
2298541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2299541132f0SMaxim Kochetkov 			    pause_stop);
2300fa914e9cSVladimir Oltean 
2301601e984fSVladimir Oltean 	/* Tail dropping watermarks */
2302f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2303a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2304601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2305601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2306601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2307fa914e9cSVladimir Oltean }
23080b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
23090b912fc9SVladimir Oltean 
23100b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
23110b912fc9SVladimir Oltean {
23120b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
23130b912fc9SVladimir Oltean 
23140b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
23150b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
23160b912fc9SVladimir Oltean 
2317cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
23180b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2319cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
23200b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
23210b912fc9SVladimir Oltean 	}
23220b912fc9SVladimir Oltean 
23230b912fc9SVladimir Oltean 	return max_mtu;
23240b912fc9SVladimir Oltean }
23250b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2326fa914e9cSVladimir Oltean 
2327421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2328421741eaSVladimir Oltean 				     bool enabled)
2329421741eaSVladimir Oltean {
2330421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2331421741eaSVladimir Oltean 	u32 val = 0;
2332421741eaSVladimir Oltean 
2333421741eaSVladimir Oltean 	if (enabled)
2334421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2335421741eaSVladimir Oltean 
2336421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2337421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2338421741eaSVladimir Oltean 
2339421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2340421741eaSVladimir Oltean }
2341421741eaSVladimir Oltean 
2342421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2343421741eaSVladimir Oltean 					bool enabled)
2344421741eaSVladimir Oltean {
2345421741eaSVladimir Oltean 	u32 val = 0;
2346421741eaSVladimir Oltean 
2347421741eaSVladimir Oltean 	if (enabled)
2348421741eaSVladimir Oltean 		val = BIT(port);
2349421741eaSVladimir Oltean 
2350421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2351421741eaSVladimir Oltean }
2352421741eaSVladimir Oltean 
2353421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2354421741eaSVladimir Oltean 					bool enabled)
2355421741eaSVladimir Oltean {
2356421741eaSVladimir Oltean 	u32 val = 0;
2357421741eaSVladimir Oltean 
2358421741eaSVladimir Oltean 	if (enabled)
2359421741eaSVladimir Oltean 		val = BIT(port);
2360421741eaSVladimir Oltean 
2361421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
23624cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4);
23634cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6);
2364421741eaSVladimir Oltean }
2365421741eaSVladimir Oltean 
2366421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2367421741eaSVladimir Oltean 					bool enabled)
2368421741eaSVladimir Oltean {
2369421741eaSVladimir Oltean 	u32 val = 0;
2370421741eaSVladimir Oltean 
2371421741eaSVladimir Oltean 	if (enabled)
2372421741eaSVladimir Oltean 		val = BIT(port);
2373421741eaSVladimir Oltean 
2374421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2375421741eaSVladimir Oltean }
2376421741eaSVladimir Oltean 
2377421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2378421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2379421741eaSVladimir Oltean {
2380421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2381421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2382421741eaSVladimir Oltean 		return -EINVAL;
2383421741eaSVladimir Oltean 
2384421741eaSVladimir Oltean 	return 0;
2385421741eaSVladimir Oltean }
2386421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2387421741eaSVladimir Oltean 
2388421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2389421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2390421741eaSVladimir Oltean {
2391421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2392421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2393421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2394421741eaSVladimir Oltean 
2395421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2396421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2397421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2398421741eaSVladimir Oltean 
2399421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2400421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2401421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2402421741eaSVladimir Oltean 
2403421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2404421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2405421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2406421741eaSVladimir Oltean }
2407421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2408421741eaSVladimir Oltean 
2409978777d0SVladimir Oltean int ocelot_port_get_default_prio(struct ocelot *ocelot, int port)
2410978777d0SVladimir Oltean {
2411978777d0SVladimir Oltean 	int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2412978777d0SVladimir Oltean 
2413978777d0SVladimir Oltean 	return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val);
2414978777d0SVladimir Oltean }
2415978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio);
2416978777d0SVladimir Oltean 
2417978777d0SVladimir Oltean int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio)
2418978777d0SVladimir Oltean {
241972f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2420978777d0SVladimir Oltean 		return -ERANGE;
2421978777d0SVladimir Oltean 
2422978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot,
2423978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio),
2424978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M,
2425978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG,
2426978777d0SVladimir Oltean 		       port);
2427978777d0SVladimir Oltean 
2428978777d0SVladimir Oltean 	return 0;
2429978777d0SVladimir Oltean }
2430978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio);
2431978777d0SVladimir Oltean 
2432978777d0SVladimir Oltean int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp)
2433978777d0SVladimir Oltean {
2434978777d0SVladimir Oltean 	int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2435978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2436978777d0SVladimir Oltean 
2437978777d0SVladimir Oltean 	/* Return error if DSCP prioritization isn't enabled */
2438978777d0SVladimir Oltean 	if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA))
2439978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2440978777d0SVladimir Oltean 
2441978777d0SVladimir Oltean 	if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) {
2442978777d0SVladimir Oltean 		dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg);
2443978777d0SVladimir Oltean 		/* Re-read ANA_DSCP_CFG for the translated DSCP */
2444978777d0SVladimir Oltean 		dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2445978777d0SVladimir Oltean 	}
2446978777d0SVladimir Oltean 
2447978777d0SVladimir Oltean 	/* If the DSCP value is not trusted, the QoS classification falls back
2448978777d0SVladimir Oltean 	 * to VLAN PCP or port-based default.
2449978777d0SVladimir Oltean 	 */
2450978777d0SVladimir Oltean 	if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA))
2451978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2452978777d0SVladimir Oltean 
2453978777d0SVladimir Oltean 	return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg);
2454978777d0SVladimir Oltean }
2455978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio);
2456978777d0SVladimir Oltean 
2457978777d0SVladimir Oltean int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2458978777d0SVladimir Oltean {
2459978777d0SVladimir Oltean 	int mask, val;
2460978777d0SVladimir Oltean 
246172f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2462978777d0SVladimir Oltean 		return -ERANGE;
2463978777d0SVladimir Oltean 
2464978777d0SVladimir Oltean 	/* There is at least one app table priority (this one), so we need to
2465978777d0SVladimir Oltean 	 * make sure DSCP prioritization is enabled on the port.
2466978777d0SVladimir Oltean 	 * Also make sure DSCP translation is disabled
2467978777d0SVladimir Oltean 	 * (dcbnl doesn't support it).
2468978777d0SVladimir Oltean 	 */
2469978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2470978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2471978777d0SVladimir Oltean 
2472978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask,
2473978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG, port);
2474978777d0SVladimir Oltean 
2475978777d0SVladimir Oltean 	/* Trust this DSCP value and map it to the given QoS class */
2476978777d0SVladimir Oltean 	val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio);
2477978777d0SVladimir Oltean 
2478978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp);
2479978777d0SVladimir Oltean 
2480978777d0SVladimir Oltean 	return 0;
2481978777d0SVladimir Oltean }
2482978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio);
2483978777d0SVladimir Oltean 
2484978777d0SVladimir Oltean int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2485978777d0SVladimir Oltean {
2486978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2487978777d0SVladimir Oltean 	int mask, i;
2488978777d0SVladimir Oltean 
2489978777d0SVladimir Oltean 	/* During a "dcb app replace" command, the new app table entry will be
2490978777d0SVladimir Oltean 	 * added first, then the old one will be deleted. But the hardware only
2491978777d0SVladimir Oltean 	 * supports one QoS class per DSCP value (duh), so if we blindly delete
2492978777d0SVladimir Oltean 	 * the app table entry for this DSCP value, we end up deleting the
2493978777d0SVladimir Oltean 	 * entry with the new priority. Avoid that by checking whether user
2494978777d0SVladimir Oltean 	 * space wants to delete the priority which is currently configured, or
2495978777d0SVladimir Oltean 	 * something else which is no longer current.
2496978777d0SVladimir Oltean 	 */
2497978777d0SVladimir Oltean 	if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio)
2498978777d0SVladimir Oltean 		return 0;
2499978777d0SVladimir Oltean 
2500978777d0SVladimir Oltean 	/* Untrust this DSCP value */
2501978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp);
2502978777d0SVladimir Oltean 
2503978777d0SVladimir Oltean 	for (i = 0; i < 64; i++) {
2504978777d0SVladimir Oltean 		int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i);
2505978777d0SVladimir Oltean 
2506978777d0SVladimir Oltean 		/* There are still app table entries on the port, so we need to
2507978777d0SVladimir Oltean 		 * keep DSCP enabled, nothing to do.
2508978777d0SVladimir Oltean 		 */
2509978777d0SVladimir Oltean 		if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)
2510978777d0SVladimir Oltean 			return 0;
2511978777d0SVladimir Oltean 	}
2512978777d0SVladimir Oltean 
2513978777d0SVladimir Oltean 	/* Disable DSCP QoS classification if there isn't any trusted
2514978777d0SVladimir Oltean 	 * DSCP value left.
2515978777d0SVladimir Oltean 	 */
2516978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2517978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2518978777d0SVladimir Oltean 
2519978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port);
2520978777d0SVladimir Oltean 
2521978777d0SVladimir Oltean 	return 0;
2522978777d0SVladimir Oltean }
2523978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio);
2524978777d0SVladimir Oltean 
2525f2a0e216SVladimir Oltean struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to,
2526ccb6ed42SVladimir Oltean 					struct netlink_ext_ack *extack)
2527ccb6ed42SVladimir Oltean {
2528ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2529ccb6ed42SVladimir Oltean 
2530ccb6ed42SVladimir Oltean 	if (m) {
2531ccb6ed42SVladimir Oltean 		if (m->to != to) {
2532ccb6ed42SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
2533ccb6ed42SVladimir Oltean 					   "Mirroring already configured towards different egress port");
2534ccb6ed42SVladimir Oltean 			return ERR_PTR(-EBUSY);
2535ccb6ed42SVladimir Oltean 		}
2536ccb6ed42SVladimir Oltean 
2537ccb6ed42SVladimir Oltean 		refcount_inc(&m->refcount);
2538ccb6ed42SVladimir Oltean 		return m;
2539ccb6ed42SVladimir Oltean 	}
2540ccb6ed42SVladimir Oltean 
2541ccb6ed42SVladimir Oltean 	m = kzalloc(sizeof(*m), GFP_KERNEL);
2542ccb6ed42SVladimir Oltean 	if (!m)
2543ccb6ed42SVladimir Oltean 		return ERR_PTR(-ENOMEM);
2544ccb6ed42SVladimir Oltean 
2545ccb6ed42SVladimir Oltean 	m->to = to;
2546ccb6ed42SVladimir Oltean 	refcount_set(&m->refcount, 1);
2547ccb6ed42SVladimir Oltean 	ocelot->mirror = m;
2548ccb6ed42SVladimir Oltean 
2549ccb6ed42SVladimir Oltean 	/* Program the mirror port to hardware */
2550ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS);
2551ccb6ed42SVladimir Oltean 
2552ccb6ed42SVladimir Oltean 	return m;
2553ccb6ed42SVladimir Oltean }
2554ccb6ed42SVladimir Oltean 
2555f2a0e216SVladimir Oltean void ocelot_mirror_put(struct ocelot *ocelot)
2556ccb6ed42SVladimir Oltean {
2557ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2558ccb6ed42SVladimir Oltean 
2559ccb6ed42SVladimir Oltean 	if (!refcount_dec_and_test(&m->refcount))
2560ccb6ed42SVladimir Oltean 		return;
2561ccb6ed42SVladimir Oltean 
2562ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_MIRRORPORTS);
2563ccb6ed42SVladimir Oltean 	ocelot->mirror = NULL;
2564ccb6ed42SVladimir Oltean 	kfree(m);
2565ccb6ed42SVladimir Oltean }
2566ccb6ed42SVladimir Oltean 
2567ccb6ed42SVladimir Oltean int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to,
2568ccb6ed42SVladimir Oltean 			   bool ingress, struct netlink_ext_ack *extack)
2569ccb6ed42SVladimir Oltean {
2570ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack);
2571ccb6ed42SVladimir Oltean 
2572ccb6ed42SVladimir Oltean 	if (IS_ERR(m))
2573ccb6ed42SVladimir Oltean 		return PTR_ERR(m);
2574ccb6ed42SVladimir Oltean 
2575ccb6ed42SVladimir Oltean 	if (ingress) {
2576ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2577ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2578ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2579ccb6ed42SVladimir Oltean 	} else {
2580ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, BIT(from), BIT(from),
2581ccb6ed42SVladimir Oltean 			   ANA_EMIRRORPORTS);
2582ccb6ed42SVladimir Oltean 	}
2583ccb6ed42SVladimir Oltean 
2584ccb6ed42SVladimir Oltean 	return 0;
2585ccb6ed42SVladimir Oltean }
2586ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_add);
2587ccb6ed42SVladimir Oltean 
2588ccb6ed42SVladimir Oltean void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress)
2589ccb6ed42SVladimir Oltean {
2590ccb6ed42SVladimir Oltean 	if (ingress) {
2591ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2592ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2593ccb6ed42SVladimir Oltean 	} else {
2594ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS);
2595ccb6ed42SVladimir Oltean 	}
2596ccb6ed42SVladimir Oltean 
2597ccb6ed42SVladimir Oltean 	ocelot_mirror_put(ocelot);
2598ccb6ed42SVladimir Oltean }
2599ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_del);
2600ccb6ed42SVladimir Oltean 
26015e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2602fa914e9cSVladimir Oltean {
2603fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2604fa914e9cSVladimir Oltean 
2605b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
260631350d7fSVladimir Oltean 
260731350d7fSVladimir Oltean 	/* Basic L2 initialization */
260831350d7fSVladimir Oltean 
26095bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
26105bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
26115bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
26125bc9d2e6SVladimir Oltean 	 */
26135bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
26145bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
26155bc9d2e6SVladimir Oltean 
26165bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
26175bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
26185bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
26195bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
26205bc9d2e6SVladimir Oltean 	mdelay(1);
26215bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
26225bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
26235bc9d2e6SVladimir Oltean 
26245bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2625a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
26265bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
26275bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2628a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
26295bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
26305bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
26315bc9d2e6SVladimir Oltean 
26325bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
26335bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
26345bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
26355bc9d2e6SVladimir Oltean 
2636e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2637541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2638e8e6e73dSVladimir Oltean 
263931350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
264031350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
264131350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
264231350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
264331350d7fSVladimir Oltean 
264431350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
264531350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
264631350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
264731350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
264831350d7fSVladimir Oltean 
2649421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2650421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2651421741eaSVladimir Oltean 
265246efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
265346efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
265446efe4efSVladimir Oltean 	 * automatic.
265546efe4efSVladimir Oltean 	 */
265646efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
265746efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
265846efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
265946efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
266046efe4efSVladimir Oltean 
266131350d7fSVladimir Oltean 	/* Enable vcap lookups */
266231350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
266331350d7fSVladimir Oltean }
26645e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
266531350d7fSVladimir Oltean 
26662d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
26672d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
26682d44b097SVladimir Oltean  * NPI mode is used).
266969df578cSVladimir Oltean  */
26702d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
267121468199SVladimir Oltean {
267269df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
267369df578cSVladimir Oltean 
267469df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
267521468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
267669df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
267769df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
267869df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
267969df578cSVladimir Oltean 	 */
268021468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
268121468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
268221468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
268321468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
268421468199SVladimir Oltean 
268569df578cSVladimir Oltean 	/* Enable CPU port module */
2686886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
268769df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2688886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2689cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2690886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2691cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
269221468199SVladimir Oltean 
269321468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
2694bfbab310SVladimir Oltean 	ocelot_write_gix(ocelot,
269554c31984SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) |
269621468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
269721468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
269821468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
269921468199SVladimir Oltean }
270021468199SVladimir Oltean 
2701f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2702f6fe01d6SVladimir Oltean {
2703f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2704f6fe01d6SVladimir Oltean 
2705f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2706f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2707f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2708f6fe01d6SVladimir Oltean 	 */
2709f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2710f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2711f6fe01d6SVladimir Oltean 
2712f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2713f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2714f6fe01d6SVladimir Oltean }
2715f6fe01d6SVladimir Oltean 
2716a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2717a556c76aSAlexandre Belloni {
271821468199SVladimir Oltean 	int i, ret;
271921468199SVladimir Oltean 	u32 port;
2720a556c76aSAlexandre Belloni 
27213a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
27223a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
27233a77b593SVladimir Oltean 		if (ret) {
27243a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
27253a77b593SVladimir Oltean 			return ret;
27263a77b593SVladimir Oltean 		}
27273a77b593SVladimir Oltean 	}
27283a77b593SVladimir Oltean 
27294e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
27302468346cSVladimir Oltean 	mutex_init(&ocelot->mact_lock);
27318abe1970SVladimir Oltean 	mutex_init(&ocelot->fwd_domain_lock);
27328670dc33SXiaoliang Yang 	mutex_init(&ocelot->tas_lock);
27334e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
273452849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
2735a556c76aSAlexandre Belloni 
2736ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2737fe90104cSVladimir Oltean 	if (!ocelot->owq)
2738ca0b272bSVladimir Oltean 		return -ENOMEM;
2739fe90104cSVladimir Oltean 
2740fe90104cSVladimir Oltean 	ret = ocelot_stats_init(ocelot);
2741*6505b680SVladimir Oltean 	if (ret)
2742*6505b680SVladimir Oltean 		goto err_stats_init;
2743ca0b272bSVladimir Oltean 
27442b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2745e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
274690e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
2747961d8b69SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->lag_fdbs);
2748f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2749a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2750a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2751aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
27522d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2753a556c76aSAlexandre Belloni 
275423e2c506SXiaoliang Yang 	if (ocelot->ops->psfp_init)
275523e2c506SXiaoliang Yang 		ocelot->ops->psfp_init(ocelot);
275623e2c506SXiaoliang Yang 
2757*6505b680SVladimir Oltean 	if (ocelot->mm_supported) {
2758*6505b680SVladimir Oltean 		ret = ocelot_mm_init(ocelot);
2759*6505b680SVladimir Oltean 		if (ret)
2760*6505b680SVladimir Oltean 			goto err_mm_init;
2761*6505b680SVladimir Oltean 	}
2762*6505b680SVladimir Oltean 
2763a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2764a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2765a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2766a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2767a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2768a556c76aSAlexandre Belloni 	}
2769a556c76aSAlexandre Belloni 
2770a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2771a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2772a556c76aSAlexandre Belloni 
2773a556c76aSAlexandre Belloni 	/* Aggregation mode */
2774a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2775a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2776a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2777f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2778f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2779f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2780f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2781a556c76aSAlexandre Belloni 
2782a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2783a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2784a556c76aSAlexandre Belloni 	 */
2785a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2786a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2787a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2788a556c76aSAlexandre Belloni 
2789a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2790a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2791a556c76aSAlexandre Belloni 
2792a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2793a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2794a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2795a556c76aSAlexandre Belloni 
2796a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2797edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2798a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2799b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2800a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2801edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2802a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2803a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2804a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2805a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2806a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2807a556c76aSAlexandre Belloni 
2808a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2809a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2810a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2811a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2812a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2813a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2814a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2815a556c76aSAlexandre Belloni 				 port);
2816a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2817a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2818a556c76aSAlexandre Belloni 	}
2819a556c76aSAlexandre Belloni 
282096b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2821a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2822a556c76aSAlexandre Belloni 
2823a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2824a556c76aSAlexandre Belloni 	}
2825ebb1bb40SHoratiu Vultur 
2826ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2827ebb1bb40SHoratiu Vultur 
2828b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2829b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2830b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2831a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2832b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2833b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2834b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2835a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2836a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2837a556c76aSAlexandre Belloni 
2838a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2839a556c76aSAlexandre Belloni 	 * registers endianness.
2840a556c76aSAlexandre Belloni 	 */
2841a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2842a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2843a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2844a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2845a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2846a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2847a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2848a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2849a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2850a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2851a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2852a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2853a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2854a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2855a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2856a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2857a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2858a556c76aSAlexandre Belloni 
2859a556c76aSAlexandre Belloni 	return 0;
2860*6505b680SVladimir Oltean 
2861*6505b680SVladimir Oltean err_mm_init:
2862*6505b680SVladimir Oltean 	ocelot_stats_deinit(ocelot);
2863*6505b680SVladimir Oltean err_stats_init:
2864*6505b680SVladimir Oltean 	destroy_workqueue(ocelot->owq);
2865*6505b680SVladimir Oltean 	return ret;
2866a556c76aSAlexandre Belloni }
2867a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2868a556c76aSAlexandre Belloni 
2869a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2870a556c76aSAlexandre Belloni {
2871fe90104cSVladimir Oltean 	ocelot_stats_deinit(ocelot);
2872ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2873a556c76aSAlexandre Belloni }
2874a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2875a556c76aSAlexandre Belloni 
2876e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2877e5fb512dSVladimir Oltean {
2878e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2879e5fb512dSVladimir Oltean 
2880e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2881e5fb512dSVladimir Oltean }
2882e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2883e5fb512dSVladimir Oltean 
2884a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2885