xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision dfca93ed51a7cf8bfda876705816a5e55381ac4a)
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>
9b67f5502SColin Foster #include <linux/iopoll.h>
10*dfca93edSColin Foster #include <linux/phy/phy.h>
11fec53f44SColin Foster #include <soc/mscc/ocelot_hsio.h>
1220968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
13a556c76aSAlexandre Belloni #include "ocelot.h"
143c83654fSVladimir Oltean #include "ocelot_vcap.h"
15a556c76aSAlexandre Belloni 
16639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US	10
17639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US	100000
18b67f5502SColin Foster #define MEM_INIT_SLEEP_US	1000
19b67f5502SColin Foster #define MEM_INIT_TIMEOUT_US	100000
20b67f5502SColin Foster 
2154c31984SVladimir Oltean #define OCELOT_RSV_VLAN_RANGE_START 4000
22639c1b26SSteen Hegelund 
23a556c76aSAlexandre Belloni struct ocelot_mact_entry {
24a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
25a556c76aSAlexandre Belloni 	u16 vid;
26a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
27a556c76aSAlexandre Belloni };
28a556c76aSAlexandre Belloni 
292468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
30639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
31639c1b26SSteen Hegelund {
32639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
33639c1b26SSteen Hegelund }
34639c1b26SSteen Hegelund 
352468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
36a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
37a556c76aSAlexandre Belloni {
38639c1b26SSteen Hegelund 	u32 val;
39a556c76aSAlexandre Belloni 
40639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
41639c1b26SSteen Hegelund 		ocelot, val,
42639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
43639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
44639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
45a556c76aSAlexandre Belloni }
46a556c76aSAlexandre Belloni 
472468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
48a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
49a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
50a556c76aSAlexandre Belloni 			       unsigned int vid)
51a556c76aSAlexandre Belloni {
52a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
53a556c76aSAlexandre Belloni 
54a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
55a556c76aSAlexandre Belloni 	 * understood by the hardware.
56a556c76aSAlexandre Belloni 	 */
57a556c76aSAlexandre Belloni 	mach |= vid    << 16;
58a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
59a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
60a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
61a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
62a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
63a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
64a556c76aSAlexandre Belloni 
65a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
66a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
67a556c76aSAlexandre Belloni 
68a556c76aSAlexandre Belloni }
69a556c76aSAlexandre Belloni 
700568c3bfSXiaoliang Yang static int __ocelot_mact_learn(struct ocelot *ocelot, int port,
71a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
729c90eea3SVladimir Oltean 			       unsigned int vid, enum macaccess_entry_type type)
73a556c76aSAlexandre Belloni {
74584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
75584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
76584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
77584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
78584b7cfcSAlban Bedel 	unsigned int mc_ports;
792468346cSVladimir Oltean 	int err;
80584b7cfcSAlban Bedel 
81584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
82584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
83584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
84584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
85584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
86584b7cfcSAlban Bedel 	else
87584b7cfcSAlban Bedel 		mc_ports = 0;
88584b7cfcSAlban Bedel 
89584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
90584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
91584b7cfcSAlban Bedel 
92a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
93a556c76aSAlexandre Belloni 
94a556c76aSAlexandre Belloni 	/* Issue a write command */
95584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
96a556c76aSAlexandre Belloni 
972468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
982468346cSVladimir Oltean 
990568c3bfSXiaoliang Yang 	return err;
1000568c3bfSXiaoliang Yang }
1010568c3bfSXiaoliang Yang 
1020568c3bfSXiaoliang Yang int ocelot_mact_learn(struct ocelot *ocelot, int port,
1030568c3bfSXiaoliang Yang 		      const unsigned char mac[ETH_ALEN],
1040568c3bfSXiaoliang Yang 		      unsigned int vid, enum macaccess_entry_type type)
1050568c3bfSXiaoliang Yang {
1060568c3bfSXiaoliang Yang 	int ret;
1070568c3bfSXiaoliang Yang 
1080568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1090568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, port, mac, vid, type);
1102468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1112468346cSVladimir Oltean 
1120568c3bfSXiaoliang Yang 	return ret;
113a556c76aSAlexandre Belloni }
1149c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
115a556c76aSAlexandre Belloni 
1169c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
1179c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
118a556c76aSAlexandre Belloni {
1192468346cSVladimir Oltean 	int err;
1202468346cSVladimir Oltean 
1212468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
1222468346cSVladimir Oltean 
123a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
124a556c76aSAlexandre Belloni 
125a556c76aSAlexandre Belloni 	/* Issue a forget command */
126a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
127a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
128a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
129a556c76aSAlexandre Belloni 
1302468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
1312468346cSVladimir Oltean 
1322468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1332468346cSVladimir Oltean 
1342468346cSVladimir Oltean 	return err;
135a556c76aSAlexandre Belloni }
1369c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
137a556c76aSAlexandre Belloni 
1380568c3bfSXiaoliang Yang int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx,
1390568c3bfSXiaoliang Yang 		       const unsigned char mac[ETH_ALEN],
1400568c3bfSXiaoliang Yang 		       unsigned int vid, enum macaccess_entry_type *type)
1410568c3bfSXiaoliang Yang {
1420568c3bfSXiaoliang Yang 	int val;
1430568c3bfSXiaoliang Yang 
1440568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1450568c3bfSXiaoliang Yang 
1460568c3bfSXiaoliang Yang 	ocelot_mact_select(ocelot, mac, vid);
1470568c3bfSXiaoliang Yang 
1480568c3bfSXiaoliang Yang 	/* Issue a read command with MACACCESS_VALID=1. */
1490568c3bfSXiaoliang Yang 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
1500568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1510568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS);
1520568c3bfSXiaoliang Yang 
1530568c3bfSXiaoliang Yang 	if (ocelot_mact_wait_for_completion(ocelot)) {
1540568c3bfSXiaoliang Yang 		mutex_unlock(&ocelot->mact_lock);
1550568c3bfSXiaoliang Yang 		return -ETIMEDOUT;
1560568c3bfSXiaoliang Yang 	}
1570568c3bfSXiaoliang Yang 
1580568c3bfSXiaoliang Yang 	/* Read back the entry flags */
1590568c3bfSXiaoliang Yang 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1600568c3bfSXiaoliang Yang 
1610568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1620568c3bfSXiaoliang Yang 
1630568c3bfSXiaoliang Yang 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1640568c3bfSXiaoliang Yang 		return -ENOENT;
1650568c3bfSXiaoliang Yang 
1660568c3bfSXiaoliang Yang 	*dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val);
1670568c3bfSXiaoliang Yang 	*type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val);
1680568c3bfSXiaoliang Yang 
1690568c3bfSXiaoliang Yang 	return 0;
1700568c3bfSXiaoliang Yang }
1710568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_lookup);
1720568c3bfSXiaoliang Yang 
1730568c3bfSXiaoliang Yang int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx,
1740568c3bfSXiaoliang Yang 				 const unsigned char mac[ETH_ALEN],
1750568c3bfSXiaoliang Yang 				 unsigned int vid,
1760568c3bfSXiaoliang Yang 				 enum macaccess_entry_type type,
1770568c3bfSXiaoliang Yang 				 int sfid, int ssid)
1780568c3bfSXiaoliang Yang {
1790568c3bfSXiaoliang Yang 	int ret;
1800568c3bfSXiaoliang Yang 
1810568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1820568c3bfSXiaoliang Yang 
1830568c3bfSXiaoliang Yang 	ocelot_write(ocelot,
1840568c3bfSXiaoliang Yang 		     (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) |
1850568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SFID(sfid) |
1860568c3bfSXiaoliang Yang 		     (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) |
1870568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SSID(ssid),
1880568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA);
1890568c3bfSXiaoliang Yang 
1900568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type);
1910568c3bfSXiaoliang Yang 
1920568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1930568c3bfSXiaoliang Yang 
1940568c3bfSXiaoliang Yang 	return ret;
1950568c3bfSXiaoliang Yang }
1960568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_learn_streamdata);
1970568c3bfSXiaoliang Yang 
198a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
199a556c76aSAlexandre Belloni {
200a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
201a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
202a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
203a556c76aSAlexandre Belloni 	 */
204a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
205a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
206a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
207a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
208a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
209a556c76aSAlexandre Belloni 
2102468346cSVladimir Oltean 	/* Clear the MAC table. We are not concurrent with anyone, so
2112468346cSVladimir Oltean 	 * holding &ocelot->mact_lock is pointless.
2122468346cSVladimir Oltean 	 */
213a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
214a556c76aSAlexandre Belloni }
215a556c76aSAlexandre Belloni 
216fec53f44SColin Foster void ocelot_pll5_init(struct ocelot *ocelot)
217fec53f44SColin Foster {
218fec53f44SColin Foster 	/* Configure PLL5. This will need a proper CCF driver
219fec53f44SColin Foster 	 * The values are coming from the VTSS API for Ocelot
220fec53f44SColin Foster 	 */
221fec53f44SColin Foster 	regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG4,
222fec53f44SColin Foster 		     HSIO_PLL5G_CFG4_IB_CTRL(0x7600) |
223fec53f44SColin Foster 		     HSIO_PLL5G_CFG4_IB_BIAS_CTRL(0x8));
224fec53f44SColin Foster 	regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG0,
225fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_CORE_CLK_DIV(0x11) |
226fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_CPU_CLK_DIV(2) |
227fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_BIAS |
228fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_VCO_BUF |
229fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_CP1 |
230fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_SELCPI(2) |
231fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_LOOP_BW_RES(0xe) |
232fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_SELBGV820(4) |
233fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_DIV4 |
234fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_CLKTREE |
235fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_LANE);
236fec53f44SColin Foster 	regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG2,
237fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_EN_RESET_FRQ_DET |
238fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_EN_RESET_OVERRUN |
239fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_GAIN_TEST(0x8) |
240fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_ENA_AMPCTRL |
241fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_PWD_AMPCTRL_N |
242fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_AMPC_SEL(0x10));
243fec53f44SColin Foster }
244fec53f44SColin Foster EXPORT_SYMBOL(ocelot_pll5_init);
245fec53f44SColin Foster 
246f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
247b5962294SHoratiu Vultur {
248b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
249b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
250f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
25175944fdaSXiaoliang Yang 
25275944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
25375944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
2542f17c050SXiaoliang Yang 
2552f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
2562f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
2572f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
258b5962294SHoratiu Vultur }
259b5962294SHoratiu Vultur 
26054c31984SVladimir Oltean static int ocelot_single_vlan_aware_bridge(struct ocelot *ocelot,
26154c31984SVladimir Oltean 					   struct netlink_ext_ack *extack)
26254c31984SVladimir Oltean {
26354c31984SVladimir Oltean 	struct net_device *bridge = NULL;
26454c31984SVladimir Oltean 	int port;
26554c31984SVladimir Oltean 
26654c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
26754c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
26854c31984SVladimir Oltean 
26954c31984SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bridge ||
27054c31984SVladimir Oltean 		    !br_vlan_enabled(ocelot_port->bridge))
27154c31984SVladimir Oltean 			continue;
27254c31984SVladimir Oltean 
27354c31984SVladimir Oltean 		if (!bridge) {
27454c31984SVladimir Oltean 			bridge = ocelot_port->bridge;
27554c31984SVladimir Oltean 			continue;
27654c31984SVladimir Oltean 		}
27754c31984SVladimir Oltean 
27854c31984SVladimir Oltean 		if (bridge == ocelot_port->bridge)
27954c31984SVladimir Oltean 			continue;
28054c31984SVladimir Oltean 
28154c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
28254c31984SVladimir Oltean 				   "Only one VLAN-aware bridge is supported");
28354c31984SVladimir Oltean 		return -EBUSY;
28454c31984SVladimir Oltean 	}
28554c31984SVladimir Oltean 
28654c31984SVladimir Oltean 	return 0;
28754c31984SVladimir Oltean }
28854c31984SVladimir Oltean 
289639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
290639c1b26SSteen Hegelund {
291639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
292639c1b26SSteen Hegelund }
293639c1b26SSteen Hegelund 
294a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
295a556c76aSAlexandre Belloni {
296639c1b26SSteen Hegelund 	u32 val;
297a556c76aSAlexandre Belloni 
298639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
299639c1b26SSteen Hegelund 		ocelot,
300639c1b26SSteen Hegelund 		val,
301639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
302639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
303639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
304a556c76aSAlexandre Belloni }
305a556c76aSAlexandre Belloni 
3067142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
3077142529fSAntoine Tenart {
3087142529fSAntoine Tenart 	/* Select the VID to configure */
3097142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
3107142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
3117142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
3127142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
3137142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
3147142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
3157142529fSAntoine Tenart 
3167142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
3177142529fSAntoine Tenart }
3187142529fSAntoine Tenart 
3190da1a1c4SVladimir Oltean static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
3200da1a1c4SVladimir Oltean {
3210da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3220da1a1c4SVladimir Oltean 	int num_untagged = 0;
3230da1a1c4SVladimir Oltean 
3240da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
3250da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
3260da1a1c4SVladimir Oltean 			continue;
3270da1a1c4SVladimir Oltean 
328276d37ebSVladimir Oltean 		/* Ignore the VLAN added by ocelot_add_vlan_unaware_pvid(),
329276d37ebSVladimir Oltean 		 * because this is never active in hardware at the same time as
330276d37ebSVladimir Oltean 		 * the bridge VLANs, which only matter in VLAN-aware mode.
331276d37ebSVladimir Oltean 		 */
332276d37ebSVladimir Oltean 		if (vlan->vid >= OCELOT_RSV_VLAN_RANGE_START)
333276d37ebSVladimir Oltean 			continue;
334276d37ebSVladimir Oltean 
3350da1a1c4SVladimir Oltean 		if (vlan->untagged & BIT(port))
3360da1a1c4SVladimir Oltean 			num_untagged++;
3370da1a1c4SVladimir Oltean 	}
3380da1a1c4SVladimir Oltean 
3390da1a1c4SVladimir Oltean 	return num_untagged;
3400da1a1c4SVladimir Oltean }
3410da1a1c4SVladimir Oltean 
3420da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
3430da1a1c4SVladimir Oltean {
3440da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3450da1a1c4SVladimir Oltean 	int num_tagged = 0;
3460da1a1c4SVladimir Oltean 
3470da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
3480da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
3490da1a1c4SVladimir Oltean 			continue;
3500da1a1c4SVladimir Oltean 
3510da1a1c4SVladimir Oltean 		if (!(vlan->untagged & BIT(port)))
3520da1a1c4SVladimir Oltean 			num_tagged++;
3530da1a1c4SVladimir Oltean 	}
3540da1a1c4SVladimir Oltean 
3550da1a1c4SVladimir Oltean 	return num_tagged;
3560da1a1c4SVladimir Oltean }
3570da1a1c4SVladimir Oltean 
3580da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
3590da1a1c4SVladimir Oltean  * _one_ egress-untagged VLAN (_the_ native VLAN)
3600da1a1c4SVladimir Oltean  */
3610da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
3620da1a1c4SVladimir Oltean {
3630da1a1c4SVladimir Oltean 	return ocelot_port_num_tagged_vlans(ocelot, port) &&
3640da1a1c4SVladimir Oltean 	       ocelot_port_num_untagged_vlans(ocelot, port) == 1;
3650da1a1c4SVladimir Oltean }
3660da1a1c4SVladimir Oltean 
3670da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan *
3680da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
3690da1a1c4SVladimir Oltean {
3700da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3710da1a1c4SVladimir Oltean 
3720da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
3730da1a1c4SVladimir Oltean 		if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
3740da1a1c4SVladimir Oltean 			return vlan;
3750da1a1c4SVladimir Oltean 
3760da1a1c4SVladimir Oltean 	return NULL;
3770da1a1c4SVladimir Oltean }
3780da1a1c4SVladimir Oltean 
3790da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
3800da1a1c4SVladimir Oltean  * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
3810da1a1c4SVladimir Oltean  * state of the port.
3820da1a1c4SVladimir Oltean  */
3830da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
38497bb69e1SVladimir Oltean {
38597bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
38662a22bcbSVladimir Oltean 	enum ocelot_port_tag_config tag_cfg;
3870da1a1c4SVladimir Oltean 	bool uses_native_vlan = false;
38897bb69e1SVladimir Oltean 
38987b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
3900da1a1c4SVladimir Oltean 		uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
3910da1a1c4SVladimir Oltean 
3920da1a1c4SVladimir Oltean 		if (uses_native_vlan)
39362a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_NATIVE;
3940da1a1c4SVladimir Oltean 		else if (ocelot_port_num_untagged_vlans(ocelot, port))
3950da1a1c4SVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_DISABLED;
39687b0f983SVladimir Oltean 		else
39762a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_TRUNK;
39887b0f983SVladimir Oltean 	} else {
39962a22bcbSVladimir Oltean 		tag_cfg = OCELOT_PORT_TAG_DISABLED;
40087b0f983SVladimir Oltean 	}
4010da1a1c4SVladimir Oltean 
40262a22bcbSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
40387b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
40487b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
4050da1a1c4SVladimir Oltean 
4060da1a1c4SVladimir Oltean 	if (uses_native_vlan) {
4070da1a1c4SVladimir Oltean 		struct ocelot_bridge_vlan *native_vlan;
4080da1a1c4SVladimir Oltean 
4090da1a1c4SVladimir Oltean 		/* Not having a native VLAN is impossible, because
4100da1a1c4SVladimir Oltean 		 * ocelot_port_num_untagged_vlans has returned 1.
4110da1a1c4SVladimir Oltean 		 * So there is no use in checking for NULL here.
4120da1a1c4SVladimir Oltean 		 */
4130da1a1c4SVladimir Oltean 		native_vlan = ocelot_port_find_native_vlan(ocelot, port);
4140da1a1c4SVladimir Oltean 
4150da1a1c4SVladimir Oltean 		ocelot_rmw_gix(ocelot,
4160da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
4170da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID_M,
4180da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG, port);
4190da1a1c4SVladimir Oltean 	}
42097bb69e1SVladimir Oltean }
42197bb69e1SVladimir Oltean 
42254c31984SVladimir Oltean int ocelot_bridge_num_find(struct ocelot *ocelot,
42354c31984SVladimir Oltean 			   const struct net_device *bridge)
42454c31984SVladimir Oltean {
42554c31984SVladimir Oltean 	int port;
42654c31984SVladimir Oltean 
42754c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
42854c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
42954c31984SVladimir Oltean 
43054c31984SVladimir Oltean 		if (ocelot_port && ocelot_port->bridge == bridge)
43154c31984SVladimir Oltean 			return ocelot_port->bridge_num;
43254c31984SVladimir Oltean 	}
43354c31984SVladimir Oltean 
43454c31984SVladimir Oltean 	return -1;
43554c31984SVladimir Oltean }
43654c31984SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bridge_num_find);
43754c31984SVladimir Oltean 
43854c31984SVladimir Oltean static u16 ocelot_vlan_unaware_pvid(struct ocelot *ocelot,
43954c31984SVladimir Oltean 				    const struct net_device *bridge)
44054c31984SVladimir Oltean {
44154c31984SVladimir Oltean 	int bridge_num;
44254c31984SVladimir Oltean 
44354c31984SVladimir Oltean 	/* Standalone ports use VID 0 */
44454c31984SVladimir Oltean 	if (!bridge)
44554c31984SVladimir Oltean 		return 0;
44654c31984SVladimir Oltean 
44754c31984SVladimir Oltean 	bridge_num = ocelot_bridge_num_find(ocelot, bridge);
44854c31984SVladimir Oltean 	if (WARN_ON(bridge_num < 0))
44954c31984SVladimir Oltean 		return 0;
45054c31984SVladimir Oltean 
45154c31984SVladimir Oltean 	/* VLAN-unaware bridges use a reserved VID going from 4095 downwards */
45254c31984SVladimir Oltean 	return VLAN_N_VID - bridge_num - 1;
45354c31984SVladimir Oltean }
45454c31984SVladimir Oltean 
45575e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
456c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
457d4004422SVladimir Oltean 				 const struct ocelot_bridge_vlan *pvid_vlan)
45875e5a554SVladimir Oltean {
45975e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
46054c31984SVladimir Oltean 	u16 pvid = ocelot_vlan_unaware_pvid(ocelot, ocelot_port->bridge);
461be0576feSVladimir Oltean 	u32 val = 0;
46275e5a554SVladimir Oltean 
463c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
46475e5a554SVladimir Oltean 
465d4004422SVladimir Oltean 	if (ocelot_port->vlan_aware && pvid_vlan)
466d4004422SVladimir Oltean 		pvid = pvid_vlan->vid;
46775e5a554SVladimir Oltean 
46875e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
469d4004422SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
47075e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
47175e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
472be0576feSVladimir Oltean 
473be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
474be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
475be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
476be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
477be0576feSVladimir Oltean 	 */
478d4004422SVladimir Oltean 	if (!pvid_vlan && ocelot_port->vlan_aware)
479be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
480be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
481be0576feSVladimir Oltean 
482be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
483be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
484be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
485be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
48675e5a554SVladimir Oltean }
48775e5a554SVladimir Oltean 
48890e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
48990e0aa8dSVladimir Oltean 							  u16 vid)
490bbf6a2d9SVladimir Oltean {
49190e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
492bbf6a2d9SVladimir Oltean 
49390e0aa8dSVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
49490e0aa8dSVladimir Oltean 		if (vlan->vid == vid)
49590e0aa8dSVladimir Oltean 			return vlan;
496bbf6a2d9SVladimir Oltean 
49790e0aa8dSVladimir Oltean 	return NULL;
498bbf6a2d9SVladimir Oltean }
499bbf6a2d9SVladimir Oltean 
5000da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
5010da1a1c4SVladimir Oltean 				  bool untagged)
502bbf6a2d9SVladimir Oltean {
50390e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
50490e0aa8dSVladimir Oltean 	unsigned long portmask;
50590e0aa8dSVladimir Oltean 	int err;
50690e0aa8dSVladimir Oltean 
50790e0aa8dSVladimir Oltean 	if (vlan) {
50890e0aa8dSVladimir Oltean 		portmask = vlan->portmask | BIT(port);
50990e0aa8dSVladimir Oltean 
51090e0aa8dSVladimir Oltean 		err = ocelot_vlant_set_mask(ocelot, vid, portmask);
51190e0aa8dSVladimir Oltean 		if (err)
51290e0aa8dSVladimir Oltean 			return err;
51390e0aa8dSVladimir Oltean 
51490e0aa8dSVladimir Oltean 		vlan->portmask = portmask;
5150da1a1c4SVladimir Oltean 		/* Bridge VLANs can be overwritten with a different
5160da1a1c4SVladimir Oltean 		 * egress-tagging setting, so make sure to override an untagged
5170da1a1c4SVladimir Oltean 		 * with a tagged VID if that's going on.
5180da1a1c4SVladimir Oltean 		 */
5190da1a1c4SVladimir Oltean 		if (untagged)
5200da1a1c4SVladimir Oltean 			vlan->untagged |= BIT(port);
5210da1a1c4SVladimir Oltean 		else
5220da1a1c4SVladimir Oltean 			vlan->untagged &= ~BIT(port);
52390e0aa8dSVladimir Oltean 
52490e0aa8dSVladimir Oltean 		return 0;
52590e0aa8dSVladimir Oltean 	}
52690e0aa8dSVladimir Oltean 
52790e0aa8dSVladimir Oltean 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
52890e0aa8dSVladimir Oltean 	if (!vlan)
52990e0aa8dSVladimir Oltean 		return -ENOMEM;
53090e0aa8dSVladimir Oltean 
53190e0aa8dSVladimir Oltean 	portmask = BIT(port);
53290e0aa8dSVladimir Oltean 
53390e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
53490e0aa8dSVladimir Oltean 	if (err) {
53590e0aa8dSVladimir Oltean 		kfree(vlan);
53690e0aa8dSVladimir Oltean 		return err;
53790e0aa8dSVladimir Oltean 	}
53890e0aa8dSVladimir Oltean 
53990e0aa8dSVladimir Oltean 	vlan->vid = vid;
54090e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
5410da1a1c4SVladimir Oltean 	if (untagged)
5420da1a1c4SVladimir Oltean 		vlan->untagged = BIT(port);
54390e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&vlan->list);
54490e0aa8dSVladimir Oltean 	list_add_tail(&vlan->list, &ocelot->vlans);
54590e0aa8dSVladimir Oltean 
54690e0aa8dSVladimir Oltean 	return 0;
547bbf6a2d9SVladimir Oltean }
548bbf6a2d9SVladimir Oltean 
549bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
550bbf6a2d9SVladimir Oltean {
55190e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
55290e0aa8dSVladimir Oltean 	unsigned long portmask;
55390e0aa8dSVladimir Oltean 	int err;
55490e0aa8dSVladimir Oltean 
55590e0aa8dSVladimir Oltean 	if (!vlan)
55690e0aa8dSVladimir Oltean 		return 0;
55790e0aa8dSVladimir Oltean 
55890e0aa8dSVladimir Oltean 	portmask = vlan->portmask & ~BIT(port);
55990e0aa8dSVladimir Oltean 
56090e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
56190e0aa8dSVladimir Oltean 	if (err)
56290e0aa8dSVladimir Oltean 		return err;
56390e0aa8dSVladimir Oltean 
56490e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
56590e0aa8dSVladimir Oltean 	if (vlan->portmask)
56690e0aa8dSVladimir Oltean 		return 0;
56790e0aa8dSVladimir Oltean 
56890e0aa8dSVladimir Oltean 	list_del(&vlan->list);
56990e0aa8dSVladimir Oltean 	kfree(vlan);
57090e0aa8dSVladimir Oltean 
57190e0aa8dSVladimir Oltean 	return 0;
572bbf6a2d9SVladimir Oltean }
573bbf6a2d9SVladimir Oltean 
57454c31984SVladimir Oltean static int ocelot_add_vlan_unaware_pvid(struct ocelot *ocelot, int port,
57554c31984SVladimir Oltean 					const struct net_device *bridge)
57654c31984SVladimir Oltean {
57754c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
57854c31984SVladimir Oltean 
57954c31984SVladimir Oltean 	return ocelot_vlan_member_add(ocelot, port, vid, true);
58054c31984SVladimir Oltean }
58154c31984SVladimir Oltean 
58254c31984SVladimir Oltean static int ocelot_del_vlan_unaware_pvid(struct ocelot *ocelot, int port,
58354c31984SVladimir Oltean 					const struct net_device *bridge)
58454c31984SVladimir Oltean {
58554c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
58654c31984SVladimir Oltean 
58754c31984SVladimir Oltean 	return ocelot_vlan_member_del(ocelot, port, vid);
58854c31984SVladimir Oltean }
58954c31984SVladimir Oltean 
5902e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
5913b95d1b2SVladimir Oltean 			       bool vlan_aware, struct netlink_ext_ack *extack)
59287b0f983SVladimir Oltean {
59370edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
594bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
59570edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
5961fcb8fb3SVladimir Oltean 	int err = 0;
597bae33f2bSVladimir Oltean 	u32 val;
59870edfae1SVladimir Oltean 
59970edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
60070edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
60170edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
6023b95d1b2SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6033b95d1b2SVladimir Oltean 					   "Cannot change VLAN state with vlan modify rules active");
60470edfae1SVladimir Oltean 			return -EBUSY;
60570edfae1SVladimir Oltean 		}
60670edfae1SVladimir Oltean 	}
60770edfae1SVladimir Oltean 
60854c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
60954c31984SVladimir Oltean 	if (err)
61054c31984SVladimir Oltean 		return err;
61154c31984SVladimir Oltean 
61254c31984SVladimir Oltean 	if (vlan_aware)
61354c31984SVladimir Oltean 		err = ocelot_del_vlan_unaware_pvid(ocelot, port,
61454c31984SVladimir Oltean 						   ocelot_port->bridge);
6151fcb8fb3SVladimir Oltean 	else if (ocelot_port->bridge)
61654c31984SVladimir Oltean 		err = ocelot_add_vlan_unaware_pvid(ocelot, port,
61754c31984SVladimir Oltean 						   ocelot_port->bridge);
61854c31984SVladimir Oltean 	if (err)
61954c31984SVladimir Oltean 		return err;
62054c31984SVladimir Oltean 
62187b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
62287b0f983SVladimir Oltean 
62387b0f983SVladimir Oltean 	if (vlan_aware)
62487b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
62587b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
62687b0f983SVladimir Oltean 	else
62787b0f983SVladimir Oltean 		val = 0;
62887b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
62987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
63087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
63187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
63287b0f983SVladimir Oltean 
633c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
6340da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6352e554a7aSVladimir Oltean 
6362e554a7aSVladimir Oltean 	return 0;
63787b0f983SVladimir Oltean }
63887b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
63987b0f983SVladimir Oltean 
6402f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
64101af940eSVladimir Oltean 			bool untagged, struct netlink_ext_ack *extack)
6422f0402feSVladimir Oltean {
6430da1a1c4SVladimir Oltean 	if (untagged) {
6440da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6450da1a1c4SVladimir Oltean 		if (ocelot_port_uses_native_vlan(ocelot, port)) {
64601af940eSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6470da1a1c4SVladimir Oltean 					   "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
6482f0402feSVladimir Oltean 			return -EBUSY;
6492f0402feSVladimir Oltean 		}
6500da1a1c4SVladimir Oltean 	} else {
6510da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6520da1a1c4SVladimir Oltean 		if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
6530da1a1c4SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6540da1a1c4SVladimir Oltean 					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
6550da1a1c4SVladimir Oltean 			return -EBUSY;
6560da1a1c4SVladimir Oltean 		}
6570da1a1c4SVladimir Oltean 	}
6582f0402feSVladimir Oltean 
65954c31984SVladimir Oltean 	if (vid > OCELOT_RSV_VLAN_RANGE_START) {
66054c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
66154c31984SVladimir Oltean 				   "VLAN range 4000-4095 reserved for VLAN-unaware bridging");
66254c31984SVladimir Oltean 		return -EBUSY;
66354c31984SVladimir Oltean 	}
66454c31984SVladimir Oltean 
6652f0402feSVladimir Oltean 	return 0;
6662f0402feSVladimir Oltean }
6672f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
6682f0402feSVladimir Oltean 
6695e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
6707142529fSAntoine Tenart 		    bool untagged)
6717142529fSAntoine Tenart {
672bbf6a2d9SVladimir Oltean 	int err;
6737142529fSAntoine Tenart 
6749323ac36SVladimir Oltean 	/* Ignore VID 0 added to our RX filter by the 8021q module, since
6759323ac36SVladimir Oltean 	 * that collides with OCELOT_STANDALONE_PVID and changes it from
6769323ac36SVladimir Oltean 	 * egress-untagged to egress-tagged.
6779323ac36SVladimir Oltean 	 */
6789323ac36SVladimir Oltean 	if (!vid)
6799323ac36SVladimir Oltean 		return 0;
6809323ac36SVladimir Oltean 
6810da1a1c4SVladimir Oltean 	err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
682bbf6a2d9SVladimir Oltean 	if (err)
683bbf6a2d9SVladimir Oltean 		return err;
6847142529fSAntoine Tenart 
6857142529fSAntoine Tenart 	/* Default ingress vlan classification */
686d4004422SVladimir Oltean 	if (pvid)
687d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port,
688d4004422SVladimir Oltean 				     ocelot_bridge_vlan_find(ocelot, vid));
6897142529fSAntoine Tenart 
6907142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
6910da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6927142529fSAntoine Tenart 
6937142529fSAntoine Tenart 	return 0;
6947142529fSAntoine Tenart }
6955e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
6967142529fSAntoine Tenart 
6975e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
6989855934cSVladimir Oltean {
6999855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
700ef576405SVladimir Oltean 	bool del_pvid = false;
701bbf6a2d9SVladimir Oltean 	int err;
7027142529fSAntoine Tenart 
7039323ac36SVladimir Oltean 	if (!vid)
7049323ac36SVladimir Oltean 		return 0;
7059323ac36SVladimir Oltean 
706ef576405SVladimir Oltean 	if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
707ef576405SVladimir Oltean 		del_pvid = true;
708ef576405SVladimir Oltean 
709bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
710bbf6a2d9SVladimir Oltean 	if (err)
711bbf6a2d9SVladimir Oltean 		return err;
7127142529fSAntoine Tenart 
713be0576feSVladimir Oltean 	/* Ingress */
714ef576405SVladimir Oltean 	if (del_pvid)
715d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, NULL);
716be0576feSVladimir Oltean 
7177142529fSAntoine Tenart 	/* Egress */
7180da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
7197142529fSAntoine Tenart 
7207142529fSAntoine Tenart 	return 0;
7217142529fSAntoine Tenart }
7225e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
7237142529fSAntoine Tenart 
724a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
725a556c76aSAlexandre Belloni {
726bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
7277142529fSAntoine Tenart 	u16 port, vid;
7287142529fSAntoine Tenart 
729a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
730a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
731a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
732a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
7337142529fSAntoine Tenart 
7347142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
735bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
73690e0aa8dSVladimir Oltean 		ocelot_vlant_set_mask(ocelot, vid, 0);
7377142529fSAntoine Tenart 
73854c31984SVladimir Oltean 	/* We need VID 0 to get traffic on standalone ports.
73954c31984SVladimir Oltean 	 * It is added automatically if the 8021q module is loaded, but we
74054c31984SVladimir Oltean 	 * can't rely on that since it might not be.
7417142529fSAntoine Tenart 	 */
74254c31984SVladimir Oltean 	ocelot_vlant_set_mask(ocelot, OCELOT_STANDALONE_PVID, all_ports);
7437142529fSAntoine Tenart 
7447142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
7457142529fSAntoine Tenart 	 * default.
7467142529fSAntoine Tenart 	 */
747bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
7487142529fSAntoine Tenart 
7497142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
7507142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
7517142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
7527142529fSAntoine Tenart 	}
753a556c76aSAlexandre Belloni }
754a556c76aSAlexandre Belloni 
755eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
756eb4733d7SVladimir Oltean {
757eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
758eb4733d7SVladimir Oltean }
759eb4733d7SVladimir Oltean 
760e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
761eb4733d7SVladimir Oltean {
7621650bdb1SVladimir Oltean 	unsigned int pause_ena;
763eb4733d7SVladimir Oltean 	int err, val;
764eb4733d7SVladimir Oltean 
765eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
766eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
767eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
768eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
769eb4733d7SVladimir Oltean 
770eb4733d7SVladimir Oltean 	/* Disable flow control */
7711650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
772eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
773eb4733d7SVladimir Oltean 
774eb4733d7SVladimir Oltean 	/* Disable priority flow control */
775eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
776eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
777eb4733d7SVladimir Oltean 
778eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
779eb4733d7SVladimir Oltean 	 * at the port.
780eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
781eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
782eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
783eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
784eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
785eb4733d7SVladimir Oltean 	 */
786eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
787eb4733d7SVladimir Oltean 
788eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
789eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
790eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
791eb4733d7SVladimir Oltean 
792eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
793eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
794eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
795eb4733d7SVladimir Oltean 
796eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
797eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
798eb4733d7SVladimir Oltean 		       port);
799eb4733d7SVladimir Oltean 
800eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
801eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
802eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
803eb4733d7SVladimir Oltean 
804eb4733d7SVladimir Oltean 	/* Clear flushing again. */
805eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
806eb4733d7SVladimir Oltean 
8071650bdb1SVladimir Oltean 	/* Re-enable flow control */
8081650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
8091650bdb1SVladimir Oltean 
810eb4733d7SVladimir Oltean 	return err;
811eb4733d7SVladimir Oltean }
812eb4733d7SVladimir Oltean 
813*dfca93edSColin Foster int ocelot_port_configure_serdes(struct ocelot *ocelot, int port,
814*dfca93edSColin Foster 				 struct device_node *portnp)
815*dfca93edSColin Foster {
816*dfca93edSColin Foster 	struct ocelot_port *ocelot_port = ocelot->ports[port];
817*dfca93edSColin Foster 	struct device *dev = ocelot->dev;
818*dfca93edSColin Foster 	int err;
819*dfca93edSColin Foster 
820*dfca93edSColin Foster 	/* Ensure clock signals and speed are set on all QSGMII links */
821*dfca93edSColin Foster 	if (ocelot_port->phy_mode == PHY_INTERFACE_MODE_QSGMII)
822*dfca93edSColin Foster 		ocelot_port_rmwl(ocelot_port, 0,
823*dfca93edSColin Foster 				 DEV_CLOCK_CFG_MAC_TX_RST |
824*dfca93edSColin Foster 				 DEV_CLOCK_CFG_MAC_RX_RST,
825*dfca93edSColin Foster 				 DEV_CLOCK_CFG);
826*dfca93edSColin Foster 
827*dfca93edSColin Foster 	if (ocelot_port->phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
828*dfca93edSColin Foster 		struct phy *serdes = of_phy_get(portnp, NULL);
829*dfca93edSColin Foster 
830*dfca93edSColin Foster 		if (IS_ERR(serdes)) {
831*dfca93edSColin Foster 			err = PTR_ERR(serdes);
832*dfca93edSColin Foster 			dev_err_probe(dev, err,
833*dfca93edSColin Foster 				      "missing SerDes phys for port %d\n",
834*dfca93edSColin Foster 				      port);
835*dfca93edSColin Foster 			return err;
836*dfca93edSColin Foster 		}
837*dfca93edSColin Foster 
838*dfca93edSColin Foster 		err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET,
839*dfca93edSColin Foster 				       ocelot_port->phy_mode);
840*dfca93edSColin Foster 		of_phy_put(serdes);
841*dfca93edSColin Foster 		if (err) {
842*dfca93edSColin Foster 			dev_err(dev, "Could not SerDes mode on port %d: %pe\n",
843*dfca93edSColin Foster 				port, ERR_PTR(err));
844*dfca93edSColin Foster 			return err;
845*dfca93edSColin Foster 		}
846*dfca93edSColin Foster 	}
847*dfca93edSColin Foster 
848*dfca93edSColin Foster 	return 0;
849*dfca93edSColin Foster }
850*dfca93edSColin Foster EXPORT_SYMBOL_GPL(ocelot_port_configure_serdes);
851*dfca93edSColin Foster 
85269f7f89cSColin Foster void ocelot_phylink_mac_config(struct ocelot *ocelot, int port,
85369f7f89cSColin Foster 			       unsigned int link_an_mode,
85469f7f89cSColin Foster 			       const struct phylink_link_state *state)
85569f7f89cSColin Foster {
85669f7f89cSColin Foster 	struct ocelot_port *ocelot_port = ocelot->ports[port];
85769f7f89cSColin Foster 
85869f7f89cSColin Foster 	/* Disable HDX fast control */
85969f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
86069f7f89cSColin Foster 			   DEV_PORT_MISC);
86169f7f89cSColin Foster 
86269f7f89cSColin Foster 	/* SGMII only for now */
86369f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
86469f7f89cSColin Foster 			   PCS1G_MODE_CFG);
86569f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
86669f7f89cSColin Foster 
86769f7f89cSColin Foster 	/* Enable PCS */
86869f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
86969f7f89cSColin Foster 
87069f7f89cSColin Foster 	/* No aneg on SGMII */
87169f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
87269f7f89cSColin Foster 
87369f7f89cSColin Foster 	/* No loopback */
87469f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
87569f7f89cSColin Foster }
87669f7f89cSColin Foster EXPORT_SYMBOL_GPL(ocelot_phylink_mac_config);
87769f7f89cSColin Foster 
878e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
879e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
880e6e12df6SVladimir Oltean 				  phy_interface_t interface,
881e6e12df6SVladimir Oltean 				  unsigned long quirks)
882a556c76aSAlexandre Belloni {
88326f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
884e6e12df6SVladimir Oltean 	int err;
885a556c76aSAlexandre Belloni 
8868abe1970SVladimir Oltean 	ocelot_port->speed = SPEED_UNKNOWN;
8878abe1970SVladimir Oltean 
888e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
889e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
890e6e12df6SVladimir Oltean 
8918abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
8928abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
8938abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
8948abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
8958abe1970SVladimir Oltean 	}
8968abe1970SVladimir Oltean 
897e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
898e6e12df6SVladimir Oltean 
899e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
900e6e12df6SVladimir Oltean 	if (err)
901e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
902e6e12df6SVladimir Oltean 			port, err);
903e6e12df6SVladimir Oltean 
904e6e12df6SVladimir Oltean 	/* Put the port in reset. */
905e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
906e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
907e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
908e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
90974a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
910e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
91174a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
912e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
913e6e12df6SVladimir Oltean }
914e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
915e6e12df6SVladimir Oltean 
916e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
917e6e12df6SVladimir Oltean 				struct phy_device *phydev,
918e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
919e6e12df6SVladimir Oltean 				phy_interface_t interface,
920e6e12df6SVladimir Oltean 				int speed, int duplex,
921e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
922e6e12df6SVladimir Oltean 				unsigned long quirks)
923e6e12df6SVladimir Oltean {
924e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
925e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
926e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
927e6e12df6SVladimir Oltean 
9288abe1970SVladimir Oltean 	ocelot_port->speed = speed;
9298abe1970SVladimir Oltean 
930e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
931e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
932e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
933e6e12df6SVladimir Oltean 	 * (which is also its default value).
934e6e12df6SVladimir Oltean 	 */
935e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
936e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
937e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
938e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
939e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
940e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
941e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
942e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
943e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
944e6e12df6SVladimir Oltean 	} else {
945e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
946e6e12df6SVladimir Oltean 	}
947e6e12df6SVladimir Oltean 
948e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
949e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
950e6e12df6SVladimir Oltean 
951e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
952e6e12df6SVladimir Oltean 
953e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
954e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
955e6e12df6SVladimir Oltean 	 */
956e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
957e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
958e6e12df6SVladimir Oltean 
959e6e12df6SVladimir Oltean 	switch (speed) {
960a556c76aSAlexandre Belloni 	case SPEED_10:
961e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
962a556c76aSAlexandre Belloni 		break;
963a556c76aSAlexandre Belloni 	case SPEED_100:
964e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
965a556c76aSAlexandre Belloni 		break;
966a556c76aSAlexandre Belloni 	case SPEED_1000:
967a556c76aSAlexandre Belloni 	case SPEED_2500:
968e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
969a556c76aSAlexandre Belloni 		break;
970a556c76aSAlexandre Belloni 	default:
971e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
972e6e12df6SVladimir Oltean 			port, speed);
973a556c76aSAlexandre Belloni 		return;
974a556c76aSAlexandre Belloni 	}
975a556c76aSAlexandre Belloni 
976de8586edSVladimir Oltean 	if (rx_pause)
977e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
978a556c76aSAlexandre Belloni 
979e6e12df6SVladimir Oltean 	if (tx_pause)
980e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
981e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
982e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
983e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
984a556c76aSAlexandre Belloni 
985e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
986e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
987e6e12df6SVladimir Oltean 	 */
988e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
989a556c76aSAlexandre Belloni 
990e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
9911ba8f656SVladimir Oltean 
99233cb0ff3SVladimir Oltean 	/* Don't attempt to send PAUSE frames on the NPI port, it's broken */
99333cb0ff3SVladimir Oltean 	if (port != ocelot->npi)
99433cb0ff3SVladimir Oltean 		ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
99533cb0ff3SVladimir Oltean 				    tx_pause);
9961ba8f656SVladimir Oltean 
997e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
998e6e12df6SVladimir Oltean 	 * enable MAC module
999e6e12df6SVladimir Oltean 	 */
1000004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
1001a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
1002a556c76aSAlexandre Belloni 
10038abe1970SVladimir Oltean 	/* If the port supports cut-through forwarding, update the masks before
10048abe1970SVladimir Oltean 	 * enabling forwarding on the port.
10058abe1970SVladimir Oltean 	 */
10068abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
10078abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
10088abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
10098abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
10108abe1970SVladimir Oltean 	}
10118abe1970SVladimir Oltean 
1012a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
1013886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
1014886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
1015a556c76aSAlexandre Belloni }
1016e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
1017889b8950SVladimir Oltean 
1018924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
1019924ee317SVladimir Oltean 				u32 *rval)
1020924ee317SVladimir Oltean {
1021924ee317SVladimir Oltean 	u32 bytes_valid, val;
1022924ee317SVladimir Oltean 
1023924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1024924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
1025924ee317SVladimir Oltean 		if (ifh)
1026924ee317SVladimir Oltean 			return -EIO;
1027924ee317SVladimir Oltean 
1028924ee317SVladimir Oltean 		do {
1029924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1030924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
1031924ee317SVladimir Oltean 	}
1032924ee317SVladimir Oltean 
1033924ee317SVladimir Oltean 	switch (val) {
1034924ee317SVladimir Oltean 	case XTR_ABORT:
1035924ee317SVladimir Oltean 		return -EIO;
1036924ee317SVladimir Oltean 	case XTR_EOF_0:
1037924ee317SVladimir Oltean 	case XTR_EOF_1:
1038924ee317SVladimir Oltean 	case XTR_EOF_2:
1039924ee317SVladimir Oltean 	case XTR_EOF_3:
1040924ee317SVladimir Oltean 	case XTR_PRUNED:
1041924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
1042924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1043924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
1044924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1045924ee317SVladimir Oltean 		else
1046924ee317SVladimir Oltean 			*rval = val;
1047924ee317SVladimir Oltean 
1048924ee317SVladimir Oltean 		return bytes_valid;
1049924ee317SVladimir Oltean 	case XTR_ESCAPE:
1050924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1051924ee317SVladimir Oltean 
1052924ee317SVladimir Oltean 		return 4;
1053924ee317SVladimir Oltean 	default:
1054924ee317SVladimir Oltean 		*rval = val;
1055924ee317SVladimir Oltean 
1056924ee317SVladimir Oltean 		return 4;
1057924ee317SVladimir Oltean 	}
1058924ee317SVladimir Oltean }
1059924ee317SVladimir Oltean 
1060924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
1061924ee317SVladimir Oltean {
1062924ee317SVladimir Oltean 	int i, err = 0;
1063924ee317SVladimir Oltean 
1064924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
1065924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
1066924ee317SVladimir Oltean 		if (err != 4)
1067924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
1068924ee317SVladimir Oltean 	}
1069924ee317SVladimir Oltean 
1070924ee317SVladimir Oltean 	return 0;
1071924ee317SVladimir Oltean }
1072924ee317SVladimir Oltean 
1073b471a71eSClément Léger void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
1074b471a71eSClément Léger 			     u64 timestamp)
1075924ee317SVladimir Oltean {
1076924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
10772ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
1078b471a71eSClément Léger 	struct timespec64 ts;
1079b471a71eSClément Léger 
1080b471a71eSClément Léger 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1081b471a71eSClément Léger 
1082b471a71eSClément Léger 	tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1083b471a71eSClément Léger 	if ((tod_in_ns & 0xffffffff) < timestamp)
1084b471a71eSClément Léger 		full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1085b471a71eSClément Léger 				timestamp;
1086b471a71eSClément Léger 	else
1087b471a71eSClément Léger 		full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1088b471a71eSClément Léger 				timestamp;
1089b471a71eSClément Léger 
1090b471a71eSClément Léger 	shhwtstamps = skb_hwtstamps(skb);
1091b471a71eSClément Léger 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1092b471a71eSClément Léger 	shhwtstamps->hwtstamp = full_ts_in_ns;
1093b471a71eSClément Léger }
1094b471a71eSClément Léger EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
1095b471a71eSClément Léger 
1096b471a71eSClément Léger int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
1097b471a71eSClément Léger {
1098924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
1099924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
1100924ee317SVladimir Oltean 	struct net_device *dev;
1101924ee317SVladimir Oltean 	struct sk_buff *skb;
1102924ee317SVladimir Oltean 	int sz, buf_len;
1103924ee317SVladimir Oltean 	u32 val, *buf;
1104924ee317SVladimir Oltean 	int err;
1105924ee317SVladimir Oltean 
1106924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1107924ee317SVladimir Oltean 	if (err)
1108924ee317SVladimir Oltean 		return err;
1109924ee317SVladimir Oltean 
1110924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
1111924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
1112924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1113924ee317SVladimir Oltean 
1114924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1115924ee317SVladimir Oltean 		return -EINVAL;
1116924ee317SVladimir Oltean 
1117924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1118924ee317SVladimir Oltean 	if (!dev)
1119924ee317SVladimir Oltean 		return -EINVAL;
1120924ee317SVladimir Oltean 
1121924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
1122924ee317SVladimir Oltean 	if (unlikely(!skb)) {
1123924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
1124924ee317SVladimir Oltean 		return -ENOMEM;
1125924ee317SVladimir Oltean 	}
1126924ee317SVladimir Oltean 
1127924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
1128924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
1129924ee317SVladimir Oltean 
1130924ee317SVladimir Oltean 	len = 0;
1131924ee317SVladimir Oltean 	do {
1132924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1133924ee317SVladimir Oltean 		if (sz < 0) {
1134924ee317SVladimir Oltean 			err = sz;
1135924ee317SVladimir Oltean 			goto out_free_skb;
1136924ee317SVladimir Oltean 		}
1137924ee317SVladimir Oltean 		*buf++ = val;
1138924ee317SVladimir Oltean 		len += sz;
1139924ee317SVladimir Oltean 	} while (len < buf_len);
1140924ee317SVladimir Oltean 
1141924ee317SVladimir Oltean 	/* Read the FCS */
1142924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1143924ee317SVladimir Oltean 	if (sz < 0) {
1144924ee317SVladimir Oltean 		err = sz;
1145924ee317SVladimir Oltean 		goto out_free_skb;
1146924ee317SVladimir Oltean 	}
1147924ee317SVladimir Oltean 
1148924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1149924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1150924ee317SVladimir Oltean 
1151924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1152924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1153924ee317SVladimir Oltean 		*buf = val;
1154924ee317SVladimir Oltean 	}
1155924ee317SVladimir Oltean 
1156b471a71eSClément Léger 	if (ocelot->ptp)
1157b471a71eSClément Léger 		ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
1158924ee317SVladimir Oltean 
1159924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1160924ee317SVladimir Oltean 	 * has already been forwarded.
1161924ee317SVladimir Oltean 	 */
1162df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1163924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1164924ee317SVladimir Oltean 
1165924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1166d8ea7ff3SHoratiu Vultur 
1167924ee317SVladimir Oltean 	*nskb = skb;
1168924ee317SVladimir Oltean 
1169924ee317SVladimir Oltean 	return 0;
1170924ee317SVladimir Oltean 
1171924ee317SVladimir Oltean out_free_skb:
1172924ee317SVladimir Oltean 	kfree_skb(skb);
1173924ee317SVladimir Oltean 	return err;
1174924ee317SVladimir Oltean }
1175924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1176924ee317SVladimir Oltean 
1177137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1178137ffbc4SVladimir Oltean {
1179137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1180137ffbc4SVladimir Oltean 
1181137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1182137ffbc4SVladimir Oltean 		return false;
1183137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1184137ffbc4SVladimir Oltean 		return false;
1185137ffbc4SVladimir Oltean 
1186137ffbc4SVladimir Oltean 	return true;
1187137ffbc4SVladimir Oltean }
1188137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1189137ffbc4SVladimir Oltean 
1190e5150f00SClément Léger void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag)
1191e5150f00SClément Léger {
1192e5150f00SClément Léger 	ocelot_ifh_set_bypass(ifh, 1);
1193e5150f00SClément Léger 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1194e5150f00SClément Léger 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1195e5150f00SClément Léger 	if (vlan_tag)
1196e5150f00SClément Léger 		ocelot_ifh_set_vlan_tci(ifh, vlan_tag);
1197e5150f00SClément Léger 	if (rew_op)
1198e5150f00SClément Léger 		ocelot_ifh_set_rew_op(ifh, rew_op);
1199e5150f00SClément Léger }
1200e5150f00SClément Léger EXPORT_SYMBOL(ocelot_ifh_port_set);
1201e5150f00SClément Léger 
1202137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1203137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1204137ffbc4SVladimir Oltean {
120540d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1206137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1207137ffbc4SVladimir Oltean 
1208137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1209137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1210137ffbc4SVladimir Oltean 
1211e5150f00SClément Léger 	ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
1212137ffbc4SVladimir Oltean 
1213137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
121440d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1215137ffbc4SVladimir Oltean 
1216137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1217137ffbc4SVladimir Oltean 	last = skb->len % 4;
1218137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1219137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1220137ffbc4SVladimir Oltean 
1221137ffbc4SVladimir Oltean 	/* Add padding */
1222137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1223137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1224137ffbc4SVladimir Oltean 		i++;
1225137ffbc4SVladimir Oltean 	}
1226137ffbc4SVladimir Oltean 
1227137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1228137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1229137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1230137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1231137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1232137ffbc4SVladimir Oltean 
1233137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1234137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1235137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1236137ffbc4SVladimir Oltean 
1237137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1238137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1239137ffbc4SVladimir Oltean }
1240137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1241137ffbc4SVladimir Oltean 
12420a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
12430a6f17c6SVladimir Oltean {
12440a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
12450a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
12460a6f17c6SVladimir Oltean }
12470a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
12480a6f17c6SVladimir Oltean 
124954c31984SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, const unsigned char *addr,
125054c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1251a556c76aSAlexandre Belloni {
125254c31984SVladimir Oltean 	if (!vid)
125354c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
125454c31984SVladimir Oltean 
1255e9b3ba43SVladimir Oltean 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
1256a556c76aSAlexandre Belloni }
12575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1258a556c76aSAlexandre Belloni 
125954c31984SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, const unsigned char *addr,
126054c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1261531ee1a6SVladimir Oltean {
126254c31984SVladimir Oltean 	if (!vid)
126354c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
126454c31984SVladimir Oltean 
1265531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1266531ee1a6SVladimir Oltean }
12675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1268531ee1a6SVladimir Oltean 
12692468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
1270531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1271a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1272a556c76aSAlexandre Belloni {
1273a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1274531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1275a556c76aSAlexandre Belloni 
1276a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1277a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1278a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1279a556c76aSAlexandre Belloni 
1280a556c76aSAlexandre Belloni 	/* Issue a read command */
1281a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1282a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1283a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1284a556c76aSAlexandre Belloni 
1285a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1286a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1287a556c76aSAlexandre Belloni 
1288a556c76aSAlexandre Belloni 	/* Read the entry flags */
1289a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1290a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1291a556c76aSAlexandre Belloni 		return -EINVAL;
1292a556c76aSAlexandre Belloni 
1293a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1294a556c76aSAlexandre Belloni 	 * do not report it.
1295a556c76aSAlexandre Belloni 	 */
1296a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1297531ee1a6SVladimir Oltean 	if (dst != port)
1298a556c76aSAlexandre Belloni 		return -EINVAL;
1299a556c76aSAlexandre Belloni 
1300a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1301a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1302a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1303a556c76aSAlexandre Belloni 
1304a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1305a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1306a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1307a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1308a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1309a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1310a556c76aSAlexandre Belloni 
1311a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1312a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1313a556c76aSAlexandre Belloni 
1314a556c76aSAlexandre Belloni 	return 0;
1315a556c76aSAlexandre Belloni }
1316a556c76aSAlexandre Belloni 
13175cad43a5SVladimir Oltean int ocelot_mact_flush(struct ocelot *ocelot, int port)
13185cad43a5SVladimir Oltean {
13195cad43a5SVladimir Oltean 	int err;
13205cad43a5SVladimir Oltean 
13215cad43a5SVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
13225cad43a5SVladimir Oltean 
13235cad43a5SVladimir Oltean 	/* Program ageing filter for a single port */
13245cad43a5SVladimir Oltean 	ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
13255cad43a5SVladimir Oltean 		     ANA_ANAGEFIL);
13265cad43a5SVladimir Oltean 
13275cad43a5SVladimir Oltean 	/* Flushing dynamic FDB entries requires two successive age scans */
13285cad43a5SVladimir Oltean 	ocelot_write(ocelot,
13295cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
13305cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
13315cad43a5SVladimir Oltean 
13325cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
13335cad43a5SVladimir Oltean 	if (err) {
13345cad43a5SVladimir Oltean 		mutex_unlock(&ocelot->mact_lock);
13355cad43a5SVladimir Oltean 		return err;
13365cad43a5SVladimir Oltean 	}
13375cad43a5SVladimir Oltean 
13385cad43a5SVladimir Oltean 	/* And second... */
13395cad43a5SVladimir Oltean 	ocelot_write(ocelot,
13405cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
13415cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
13425cad43a5SVladimir Oltean 
13435cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
13445cad43a5SVladimir Oltean 
13455cad43a5SVladimir Oltean 	/* Restore ageing filter */
13465cad43a5SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_ANAGEFIL);
13475cad43a5SVladimir Oltean 
13485cad43a5SVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
13495cad43a5SVladimir Oltean 
13505cad43a5SVladimir Oltean 	return err;
13515cad43a5SVladimir Oltean }
13525cad43a5SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_mact_flush);
13535cad43a5SVladimir Oltean 
13545e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1355531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1356a556c76aSAlexandre Belloni {
13572468346cSVladimir Oltean 	int err = 0;
1358531ee1a6SVladimir Oltean 	int i, j;
1359a556c76aSAlexandre Belloni 
13602468346cSVladimir Oltean 	/* We could take the lock just around ocelot_mact_read, but doing so
13612468346cSVladimir Oltean 	 * thousands of times in a row seems rather pointless and inefficient.
13622468346cSVladimir Oltean 	 */
13632468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
13642468346cSVladimir Oltean 
136521ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
136621ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1367a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1368531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1369531ee1a6SVladimir Oltean 			bool is_static;
1370531ee1a6SVladimir Oltean 
13712468346cSVladimir Oltean 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1372a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1373a556c76aSAlexandre Belloni 			 * skip it.
1374a556c76aSAlexandre Belloni 			 */
13752468346cSVladimir Oltean 			if (err == -EINVAL)
1376a556c76aSAlexandre Belloni 				continue;
13772468346cSVladimir Oltean 			else if (err)
13782468346cSVladimir Oltean 				break;
1379a556c76aSAlexandre Belloni 
1380531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1381531ee1a6SVladimir Oltean 
138254c31984SVladimir Oltean 			/* Hide the reserved VLANs used for
138354c31984SVladimir Oltean 			 * VLAN-unaware bridging.
138454c31984SVladimir Oltean 			 */
138554c31984SVladimir Oltean 			if (entry.vid > OCELOT_RSV_VLAN_RANGE_START)
138654c31984SVladimir Oltean 				entry.vid = 0;
138754c31984SVladimir Oltean 
13882468346cSVladimir Oltean 			err = cb(entry.mac, entry.vid, is_static, data);
13892468346cSVladimir Oltean 			if (err)
13902468346cSVladimir Oltean 				break;
1391a556c76aSAlexandre Belloni 		}
1392a556c76aSAlexandre Belloni 	}
1393a556c76aSAlexandre Belloni 
13942468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
13952468346cSVladimir Oltean 
13962468346cSVladimir Oltean 	return err;
1397531ee1a6SVladimir Oltean }
13985e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1399531ee1a6SVladimir Oltean 
14009d75b881SVladimir Oltean int ocelot_trap_add(struct ocelot *ocelot, int port,
14019d75b881SVladimir Oltean 		    unsigned long cookie, bool take_ts,
140296ca08c0SVladimir Oltean 		    void (*populate)(struct ocelot_vcap_filter *f))
140396ca08c0SVladimir Oltean {
140496ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
140596ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
140696ca08c0SVladimir Oltean 	bool new = false;
140796ca08c0SVladimir Oltean 	int err;
140896ca08c0SVladimir Oltean 
140996ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
141096ca08c0SVladimir Oltean 
141196ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
141296ca08c0SVladimir Oltean 						   false);
141396ca08c0SVladimir Oltean 	if (!trap) {
141496ca08c0SVladimir Oltean 		trap = kzalloc(sizeof(*trap), GFP_KERNEL);
141596ca08c0SVladimir Oltean 		if (!trap)
141696ca08c0SVladimir Oltean 			return -ENOMEM;
141796ca08c0SVladimir Oltean 
141896ca08c0SVladimir Oltean 		populate(trap);
141996ca08c0SVladimir Oltean 		trap->prio = 1;
142096ca08c0SVladimir Oltean 		trap->id.cookie = cookie;
142196ca08c0SVladimir Oltean 		trap->id.tc_offload = false;
142296ca08c0SVladimir Oltean 		trap->block_id = VCAP_IS2;
142396ca08c0SVladimir Oltean 		trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
142496ca08c0SVladimir Oltean 		trap->lookup = 0;
142596ca08c0SVladimir Oltean 		trap->action.cpu_copy_ena = true;
142696ca08c0SVladimir Oltean 		trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
142796ca08c0SVladimir Oltean 		trap->action.port_mask = 0;
14289d75b881SVladimir Oltean 		trap->take_ts = take_ts;
1429e1846cffSVladimir Oltean 		trap->is_trap = true;
143096ca08c0SVladimir Oltean 		new = true;
143196ca08c0SVladimir Oltean 	}
143296ca08c0SVladimir Oltean 
143396ca08c0SVladimir Oltean 	trap->ingress_port_mask |= BIT(port);
143496ca08c0SVladimir Oltean 
143596ca08c0SVladimir Oltean 	if (new)
143696ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_add(ocelot, trap, NULL);
143796ca08c0SVladimir Oltean 	else
143896ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
143996ca08c0SVladimir Oltean 	if (err) {
144096ca08c0SVladimir Oltean 		trap->ingress_port_mask &= ~BIT(port);
1441e1846cffSVladimir Oltean 		if (!trap->ingress_port_mask)
144296ca08c0SVladimir Oltean 			kfree(trap);
144396ca08c0SVladimir Oltean 		return err;
144496ca08c0SVladimir Oltean 	}
144596ca08c0SVladimir Oltean 
144696ca08c0SVladimir Oltean 	return 0;
144796ca08c0SVladimir Oltean }
144896ca08c0SVladimir Oltean 
1449b9bace6eSVladimir Oltean int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie)
145096ca08c0SVladimir Oltean {
145196ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
145296ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
145396ca08c0SVladimir Oltean 
145496ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
145596ca08c0SVladimir Oltean 
145696ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
145796ca08c0SVladimir Oltean 						   false);
145896ca08c0SVladimir Oltean 	if (!trap)
145996ca08c0SVladimir Oltean 		return 0;
146096ca08c0SVladimir Oltean 
146196ca08c0SVladimir Oltean 	trap->ingress_port_mask &= ~BIT(port);
1462e1846cffSVladimir Oltean 	if (!trap->ingress_port_mask)
146396ca08c0SVladimir Oltean 		return ocelot_vcap_filter_del(ocelot, trap);
146496ca08c0SVladimir Oltean 
146596ca08c0SVladimir Oltean 	return ocelot_vcap_filter_replace(ocelot, trap);
146696ca08c0SVladimir Oltean }
146796ca08c0SVladimir Oltean 
1468a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
1469b80af659SVladimir Oltean {
1470b80af659SVladimir Oltean 	u32 mask = 0;
1471b80af659SVladimir Oltean 	int port;
1472b80af659SVladimir Oltean 
1473961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
1474961d8b69SVladimir Oltean 
1475b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1476b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1477b80af659SVladimir Oltean 
1478b80af659SVladimir Oltean 		if (!ocelot_port)
1479b80af659SVladimir Oltean 			continue;
1480b80af659SVladimir Oltean 
1481a14e6b69SVladimir Oltean 		if (ocelot_port->bond == bond)
1482b80af659SVladimir Oltean 			mask |= BIT(port);
1483b80af659SVladimir Oltean 	}
1484b80af659SVladimir Oltean 
1485b80af659SVladimir Oltean 	return mask;
1486b80af659SVladimir Oltean }
1487b80af659SVladimir Oltean 
1488961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical
1489961d8b69SVladimir Oltean  * port ID present in that LAG. It may change if that port ever leaves the LAG.
1490961d8b69SVladimir Oltean  */
1491eca70102SVladimir Oltean int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond)
1492961d8b69SVladimir Oltean {
1493961d8b69SVladimir Oltean 	int bond_mask = ocelot_get_bond_mask(ocelot, bond);
1494961d8b69SVladimir Oltean 
1495961d8b69SVladimir Oltean 	if (!bond_mask)
1496961d8b69SVladimir Oltean 		return -ENOENT;
1497961d8b69SVladimir Oltean 
1498961d8b69SVladimir Oltean 	return __ffs(bond_mask);
1499961d8b69SVladimir Oltean }
1500eca70102SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bond_get_id);
1501961d8b69SVladimir Oltean 
1502291ac151SVladimir Oltean /* Returns the mask of user ports assigned to this DSA tag_8021q CPU port.
1503291ac151SVladimir Oltean  * Note that when CPU ports are in a LAG, the user ports are assigned to the
1504291ac151SVladimir Oltean  * 'primary' CPU port, the one whose physical port number gives the logical
1505291ac151SVladimir Oltean  * port number of the LAG.
1506291ac151SVladimir Oltean  *
1507291ac151SVladimir Oltean  * We leave PGID_SRC poorly configured for the 'secondary' CPU port in the LAG
1508291ac151SVladimir Oltean  * (to which no user port is assigned), but it appears that forwarding from
1509291ac151SVladimir Oltean  * this secondary CPU port looks at the PGID_SRC associated with the logical
1510291ac151SVladimir Oltean  * port ID that it's assigned to, which *is* configured properly.
1511291ac151SVladimir Oltean  */
1512c295f983SVladimir Oltean static u32 ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot *ocelot,
1513c295f983SVladimir Oltean 					       struct ocelot_port *cpu)
1514c295f983SVladimir Oltean {
1515c295f983SVladimir Oltean 	u32 mask = 0;
1516c295f983SVladimir Oltean 	int port;
1517c295f983SVladimir Oltean 
1518c295f983SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1519c295f983SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1520c295f983SVladimir Oltean 
1521c295f983SVladimir Oltean 		if (!ocelot_port)
1522c295f983SVladimir Oltean 			continue;
1523c295f983SVladimir Oltean 
1524c295f983SVladimir Oltean 		if (ocelot_port->dsa_8021q_cpu == cpu)
1525c295f983SVladimir Oltean 			mask |= BIT(port);
1526c295f983SVladimir Oltean 	}
1527c295f983SVladimir Oltean 
1528291ac151SVladimir Oltean 	if (cpu->bond)
1529291ac151SVladimir Oltean 		mask &= ~ocelot_get_bond_mask(ocelot, cpu->bond);
1530291ac151SVladimir Oltean 
1531c295f983SVladimir Oltean 	return mask;
1532c295f983SVladimir Oltean }
1533c295f983SVladimir Oltean 
1534291ac151SVladimir Oltean /* Returns the DSA tag_8021q CPU port that the given port is assigned to,
1535291ac151SVladimir Oltean  * or the bit mask of CPU ports if said CPU port is in a LAG.
1536291ac151SVladimir Oltean  */
1537c295f983SVladimir Oltean u32 ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot *ocelot, int port)
1538c295f983SVladimir Oltean {
1539c295f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1540c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot_port->dsa_8021q_cpu;
1541c295f983SVladimir Oltean 
1542c295f983SVladimir Oltean 	if (!cpu_port)
1543c295f983SVladimir Oltean 		return 0;
1544c295f983SVladimir Oltean 
1545291ac151SVladimir Oltean 	if (cpu_port->bond)
1546291ac151SVladimir Oltean 		return ocelot_get_bond_mask(ocelot, cpu_port->bond);
1547291ac151SVladimir Oltean 
1548c295f983SVladimir Oltean 	return BIT(cpu_port->index);
1549c295f983SVladimir Oltean }
1550c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assigned_dsa_8021q_cpu_mask);
1551c295f983SVladimir Oltean 
15528abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
1553df291e54SVladimir Oltean {
1554acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1555a8bd9fa5SVladimir Oltean 	const struct net_device *bridge;
1556df291e54SVladimir Oltean 	u32 mask = 0;
1557df291e54SVladimir Oltean 	int port;
1558df291e54SVladimir Oltean 
1559a8bd9fa5SVladimir Oltean 	if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
1560a8bd9fa5SVladimir Oltean 		return 0;
1561a8bd9fa5SVladimir Oltean 
1562a8bd9fa5SVladimir Oltean 	bridge = ocelot_port->bridge;
1563a8bd9fa5SVladimir Oltean 	if (!bridge)
1564acc64f52SVladimir Oltean 		return 0;
1565acc64f52SVladimir Oltean 
1566df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1567acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1568df291e54SVladimir Oltean 
1569df291e54SVladimir Oltean 		if (!ocelot_port)
1570df291e54SVladimir Oltean 			continue;
1571df291e54SVladimir Oltean 
1572df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1573df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1574df291e54SVladimir Oltean 			mask |= BIT(port);
1575df291e54SVladimir Oltean 	}
1576df291e54SVladimir Oltean 
1577df291e54SVladimir Oltean 	return mask;
1578df291e54SVladimir Oltean }
15798abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
1580df291e54SVladimir Oltean 
1581a72e23ddSVladimir Oltean static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
1582e21268efSVladimir Oltean {
1583e21268efSVladimir Oltean 	int port;
1584e21268efSVladimir Oltean 
15858abe1970SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
15868abe1970SVladimir Oltean 
15878abe1970SVladimir Oltean 	/* If cut-through forwarding is supported, update the masks before a
15888abe1970SVladimir Oltean 	 * port joins the forwarding domain, to avoid potential underruns if it
15898abe1970SVladimir Oltean 	 * has the highest speed from the new domain.
15908abe1970SVladimir Oltean 	 */
15918abe1970SVladimir Oltean 	if (joining && ocelot->ops->cut_through_fwd)
15928abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
15938abe1970SVladimir Oltean 
15949b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
15959b521250SVladimir Oltean 	 * a source for the other ports.
15969b521250SVladimir Oltean 	 */
15979b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1598e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1599e21268efSVladimir Oltean 		unsigned long mask;
1600e21268efSVladimir Oltean 
1601e21268efSVladimir Oltean 		if (!ocelot_port) {
1602e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1603e21268efSVladimir Oltean 			mask = 0;
1604e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1605e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1606c295f983SVladimir Oltean 			 * forward packets to all ports assigned to them.
1607e21268efSVladimir Oltean 			 */
1608c295f983SVladimir Oltean 			mask = ocelot_dsa_8021q_cpu_assigned_ports(ocelot,
1609c295f983SVladimir Oltean 								   ocelot_port);
1610df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1611528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
16129b521250SVladimir Oltean 
1613a8bd9fa5SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
1614df291e54SVladimir Oltean 			mask &= ~BIT(port);
1615c295f983SVladimir Oltean 
1616c295f983SVladimir Oltean 			mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1617c295f983SVladimir Oltean 									port);
1618c295f983SVladimir Oltean 
1619a14e6b69SVladimir Oltean 			if (bond)
1620a14e6b69SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond);
16219b521250SVladimir Oltean 		} else {
1622e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1623e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1624e21268efSVladimir Oltean 			 * module otherwise.
1625e21268efSVladimir Oltean 			 */
1626c295f983SVladimir Oltean 			mask = ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1627c295f983SVladimir Oltean 								       port);
1628e21268efSVladimir Oltean 		}
1629e21268efSVladimir Oltean 
1630e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
16319b521250SVladimir Oltean 	}
16328abe1970SVladimir Oltean 
16338abe1970SVladimir Oltean 	/* If cut-through forwarding is supported and a port is leaving, there
16348abe1970SVladimir Oltean 	 * is a chance that cut-through was disabled on the other ports due to
16358abe1970SVladimir Oltean 	 * the port which is leaving (it has a higher link speed). We need to
16368abe1970SVladimir Oltean 	 * update the cut-through masks of the remaining ports no earlier than
16378abe1970SVladimir Oltean 	 * after the port has left, to prevent underruns from happening between
16388abe1970SVladimir Oltean 	 * the cut-through update and the forwarding domain update.
16398abe1970SVladimir Oltean 	 */
16408abe1970SVladimir Oltean 	if (!joining && ocelot->ops->cut_through_fwd)
16418abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
16429b521250SVladimir Oltean }
16439b521250SVladimir Oltean 
164461be79baSVladimir Oltean /* Update PGID_CPU which is the destination port mask used for whitelisting
164561be79baSVladimir Oltean  * unicast addresses filtered towards the host. In the normal and NPI modes,
164661be79baSVladimir Oltean  * this points to the analyzer entry for the CPU port module, while in DSA
164761be79baSVladimir Oltean  * tag_8021q mode, it is a bit mask of all active CPU ports.
164861be79baSVladimir Oltean  * PGID_SRC will take care of forwarding a packet from one user port to
164961be79baSVladimir Oltean  * no more than a single CPU port.
165061be79baSVladimir Oltean  */
165161be79baSVladimir Oltean static void ocelot_update_pgid_cpu(struct ocelot *ocelot)
165261be79baSVladimir Oltean {
165361be79baSVladimir Oltean 	int pgid_cpu = 0;
165461be79baSVladimir Oltean 	int port;
165561be79baSVladimir Oltean 
165661be79baSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
165761be79baSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
165861be79baSVladimir Oltean 
165961be79baSVladimir Oltean 		if (!ocelot_port || !ocelot_port->is_dsa_8021q_cpu)
166061be79baSVladimir Oltean 			continue;
166161be79baSVladimir Oltean 
166261be79baSVladimir Oltean 		pgid_cpu |= BIT(port);
166361be79baSVladimir Oltean 	}
166461be79baSVladimir Oltean 
166561be79baSVladimir Oltean 	if (!pgid_cpu)
166661be79baSVladimir Oltean 		pgid_cpu = BIT(ocelot->num_phys_ports);
166761be79baSVladimir Oltean 
166861be79baSVladimir Oltean 	ocelot_write_rix(ocelot, pgid_cpu, ANA_PGID_PGID, PGID_CPU);
166961be79baSVladimir Oltean }
167061be79baSVladimir Oltean 
167136a0bf44SVladimir Oltean void ocelot_port_setup_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
167254c31984SVladimir Oltean {
1673c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
167454c31984SVladimir Oltean 	u16 vid;
167554c31984SVladimir Oltean 
16768c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
16778c166acbSVladimir Oltean 
1678c295f983SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = true;
167954c31984SVladimir Oltean 
168054c31984SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
1681c295f983SVladimir Oltean 		ocelot_vlan_member_add(ocelot, cpu, vid, true);
168261be79baSVladimir Oltean 
168361be79baSVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
1684a72e23ddSVladimir Oltean 
168536a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
168636a0bf44SVladimir Oltean }
168736a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_setup_dsa_8021q_cpu);
168836a0bf44SVladimir Oltean 
168936a0bf44SVladimir Oltean void ocelot_port_teardown_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
169036a0bf44SVladimir Oltean {
169136a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
169236a0bf44SVladimir Oltean 	u16 vid;
169336a0bf44SVladimir Oltean 
169436a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
169536a0bf44SVladimir Oltean 
169636a0bf44SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = false;
169736a0bf44SVladimir Oltean 
169836a0bf44SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
169936a0bf44SVladimir Oltean 		ocelot_vlan_member_del(ocelot, cpu_port->index, vid);
170036a0bf44SVladimir Oltean 
170136a0bf44SVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
170236a0bf44SVladimir Oltean 
170336a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
170436a0bf44SVladimir Oltean }
170536a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_teardown_dsa_8021q_cpu);
170636a0bf44SVladimir Oltean 
170736a0bf44SVladimir Oltean void ocelot_port_assign_dsa_8021q_cpu(struct ocelot *ocelot, int port,
170836a0bf44SVladimir Oltean 				      int cpu)
170936a0bf44SVladimir Oltean {
171036a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
171136a0bf44SVladimir Oltean 
171236a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
171336a0bf44SVladimir Oltean 
171436a0bf44SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = cpu_port;
1715a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
17168c166acbSVladimir Oltean 
17178c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
171854c31984SVladimir Oltean }
1719c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assign_dsa_8021q_cpu);
172054c31984SVladimir Oltean 
1721c295f983SVladimir Oltean void ocelot_port_unassign_dsa_8021q_cpu(struct ocelot *ocelot, int port)
172254c31984SVladimir Oltean {
17238c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
17248c166acbSVladimir Oltean 
1725c295f983SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = NULL;
1726a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
17278c166acbSVladimir Oltean 
17288c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
172954c31984SVladimir Oltean }
1730c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_unassign_dsa_8021q_cpu);
173154c31984SVladimir Oltean 
17325e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1733a556c76aSAlexandre Belloni {
1734421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1735df291e54SVladimir Oltean 	u32 learn_ena = 0;
1736a556c76aSAlexandre Belloni 
17378abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
17388abe1970SVladimir Oltean 
1739df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1740a556c76aSAlexandre Belloni 
1741df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1742df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1743df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1744a556c76aSAlexandre Belloni 
1745df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1746df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1747a556c76aSAlexandre Belloni 
17488abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
17498abe1970SVladimir Oltean 
17508abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
1751a556c76aSAlexandre Belloni }
17525e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1753a556c76aSAlexandre Belloni 
17545e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
17554bda1415SVladimir Oltean {
1756c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1757c0d7eccbSVladimir Oltean 
1758c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1759c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1760c0d7eccbSVladimir Oltean 	 */
1761c0d7eccbSVladimir Oltean 	if (!age_period)
1762c0d7eccbSVladimir Oltean 		age_period = 1;
1763c0d7eccbSVladimir Oltean 
1764c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1765a556c76aSAlexandre Belloni }
17665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1767a556c76aSAlexandre Belloni 
1768a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1769a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1770a556c76aSAlexandre Belloni 						     u16 vid)
1771a556c76aSAlexandre Belloni {
1772a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1773a556c76aSAlexandre Belloni 
1774a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1775a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1776a556c76aSAlexandre Belloni 			return mc;
1777a556c76aSAlexandre Belloni 	}
1778a556c76aSAlexandre Belloni 
1779a556c76aSAlexandre Belloni 	return NULL;
1780a556c76aSAlexandre Belloni }
1781a556c76aSAlexandre Belloni 
17829403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
17839403c158SVladimir Oltean {
17849403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
17859403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
17869403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
17879403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
17887c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
17899403c158SVladimir Oltean }
17909403c158SVladimir Oltean 
1791e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1792e5d1f896SVladimir Oltean 					     unsigned long ports)
1793e5d1f896SVladimir Oltean {
1794e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1795e5d1f896SVladimir Oltean 
1796e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1797e5d1f896SVladimir Oltean 	if (!pgid)
1798e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1799e5d1f896SVladimir Oltean 
1800e5d1f896SVladimir Oltean 	pgid->ports = ports;
1801e5d1f896SVladimir Oltean 	pgid->index = index;
1802e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1803e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1804e5d1f896SVladimir Oltean 
1805e5d1f896SVladimir Oltean 	return pgid;
1806e5d1f896SVladimir Oltean }
1807e5d1f896SVladimir Oltean 
1808e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1809e5d1f896SVladimir Oltean {
1810e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1811e5d1f896SVladimir Oltean 		return;
1812e5d1f896SVladimir Oltean 
1813e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1814e5d1f896SVladimir Oltean 	kfree(pgid);
1815e5d1f896SVladimir Oltean }
1816e5d1f896SVladimir Oltean 
1817e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1818bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
18199403c158SVladimir Oltean {
1820e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1821e5d1f896SVladimir Oltean 	int index;
18229403c158SVladimir Oltean 
18239403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
18249403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
18259403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
18269403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
18279403c158SVladimir Oltean 	 */
1828bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1829bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1830e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
18319403c158SVladimir Oltean 
1832e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1833e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1834e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1835e5d1f896SVladimir Oltean 		 */
1836e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1837e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1838e5d1f896SVladimir Oltean 			return pgid;
1839e5d1f896SVladimir Oltean 		}
1840e5d1f896SVladimir Oltean 	}
1841e5d1f896SVladimir Oltean 
1842e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1843e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
18449403c158SVladimir Oltean 		bool used = false;
18459403c158SVladimir Oltean 
1846e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1847e5d1f896SVladimir Oltean 			if (pgid->index == index) {
18489403c158SVladimir Oltean 				used = true;
18499403c158SVladimir Oltean 				break;
18509403c158SVladimir Oltean 			}
18519403c158SVladimir Oltean 		}
18529403c158SVladimir Oltean 
18539403c158SVladimir Oltean 		if (!used)
1854e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
18559403c158SVladimir Oltean 	}
18569403c158SVladimir Oltean 
1857e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
18589403c158SVladimir Oltean }
18599403c158SVladimir Oltean 
18609403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1861bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
18629403c158SVladimir Oltean {
1863ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
18649403c158SVladimir Oltean 
1865bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
18669403c158SVladimir Oltean 		addr[0] = 0;
18679403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
18689403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1869bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
18709403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
18719403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
18729403c158SVladimir Oltean 	}
18739403c158SVladimir Oltean }
18749403c158SVladimir Oltean 
1875209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
187654c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
187754c31984SVladimir Oltean 			const struct net_device *bridge)
1878a556c76aSAlexandre Belloni {
1879a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1880004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1881e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1882a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1883a556c76aSAlexandre Belloni 
188454c31984SVladimir Oltean 	if (!vid)
188554c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
188654c31984SVladimir Oltean 
1887a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1888a556c76aSAlexandre Belloni 	if (!mc) {
1889728e69aeSVladimir Oltean 		/* New entry */
1890bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1891bb8d53fdSVladimir Oltean 		if (!mc)
1892bb8d53fdSVladimir Oltean 			return -ENOMEM;
1893bb8d53fdSVladimir Oltean 
1894bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1895bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1896bb8d53fdSVladimir Oltean 		mc->vid = vid;
1897bb8d53fdSVladimir Oltean 
1898a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1899728e69aeSVladimir Oltean 	} else {
1900e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1901e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1902e5d1f896SVladimir Oltean 		 */
1903e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1904bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1905a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1906a556c76aSAlexandre Belloni 	}
1907a556c76aSAlexandre Belloni 
1908004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1909e5d1f896SVladimir Oltean 
1910e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1911e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1912e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1913e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1914e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1915e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1916e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1917e5d1f896SVladimir Oltean 	}
1918e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1919e5d1f896SVladimir Oltean 
1920bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1921a556c76aSAlexandre Belloni 
1922e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1923e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1924e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1925e5d1f896SVladimir Oltean 				 pgid->index);
1926e5d1f896SVladimir Oltean 
1927e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1928bb8d53fdSVladimir Oltean 				 mc->entry_type);
1929a556c76aSAlexandre Belloni }
1930209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1931a556c76aSAlexandre Belloni 
1932209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
193354c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
193454c31984SVladimir Oltean 			const struct net_device *bridge)
1935a556c76aSAlexandre Belloni {
1936a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1937004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1938e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1939a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1940a556c76aSAlexandre Belloni 
194154c31984SVladimir Oltean 	if (!vid)
194254c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
194354c31984SVladimir Oltean 
1944a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1945a556c76aSAlexandre Belloni 	if (!mc)
1946a556c76aSAlexandre Belloni 		return -ENOENT;
1947a556c76aSAlexandre Belloni 
1948bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1949a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1950a556c76aSAlexandre Belloni 
1951e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1952004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1953a556c76aSAlexandre Belloni 	if (!mc->ports) {
1954a556c76aSAlexandre Belloni 		list_del(&mc->list);
1955a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1956a556c76aSAlexandre Belloni 		return 0;
1957a556c76aSAlexandre Belloni 	}
1958a556c76aSAlexandre Belloni 
1959e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1960e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1961e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1962e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1963e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1964e5d1f896SVladimir Oltean 
1965bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1966a556c76aSAlexandre Belloni 
1967e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1968e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1969e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1970e5d1f896SVladimir Oltean 				 pgid->index);
1971e5d1f896SVladimir Oltean 
1972e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1973bb8d53fdSVladimir Oltean 				 mc->entry_type);
1974a556c76aSAlexandre Belloni }
1975209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1976a556c76aSAlexandre Belloni 
197754c31984SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
197854c31984SVladimir Oltean 			    struct net_device *bridge, int bridge_num,
197954c31984SVladimir Oltean 			    struct netlink_ext_ack *extack)
1980a556c76aSAlexandre Belloni {
1981df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
198254c31984SVladimir Oltean 	int err;
198354c31984SVladimir Oltean 
198454c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
198554c31984SVladimir Oltean 	if (err)
198654c31984SVladimir Oltean 		return err;
1987a556c76aSAlexandre Belloni 
19888abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
19898abe1970SVladimir Oltean 
1990df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
199154c31984SVladimir Oltean 	ocelot_port->bridge_num = bridge_num;
1992a556c76aSAlexandre Belloni 
19938abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
19948abe1970SVladimir Oltean 
19958abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
199654c31984SVladimir Oltean 
199754c31984SVladimir Oltean 	if (br_vlan_enabled(bridge))
199854c31984SVladimir Oltean 		return 0;
199954c31984SVladimir Oltean 
200054c31984SVladimir Oltean 	return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge);
2001a556c76aSAlexandre Belloni }
20025e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
2003a556c76aSAlexandre Belloni 
2004e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
2005a556c76aSAlexandre Belloni 			      struct net_device *bridge)
2006a556c76aSAlexandre Belloni {
2007df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
20082e554a7aSVladimir Oltean 
20098abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
20108abe1970SVladimir Oltean 
201154c31984SVladimir Oltean 	if (!br_vlan_enabled(bridge))
201254c31984SVladimir Oltean 		ocelot_del_vlan_unaware_pvid(ocelot, port, bridge);
201354c31984SVladimir Oltean 
2014df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
201554c31984SVladimir Oltean 	ocelot_port->bridge_num = -1;
20167142529fSAntoine Tenart 
2017d4004422SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, NULL);
20180da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
20198abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
20208abe1970SVladimir Oltean 
20218abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2022a556c76aSAlexandre Belloni }
20235e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
2024a556c76aSAlexandre Belloni 
2025dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
2026dc96ee37SAlexandre Belloni {
2027528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
2028dc96ee37SAlexandre Belloni 	int i, port, lag;
2029dc96ee37SAlexandre Belloni 
2030dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
203196b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
2032dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2033dc96ee37SAlexandre Belloni 
203496b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
2035dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
2036dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
2037dc96ee37SAlexandre Belloni 
2038528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
2039528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
2040528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
2041528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
2042528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
2043528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
2044528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
2045528d3f19SVladimir Oltean 	 */
2046528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2047528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2048528d3f19SVladimir Oltean 
2049528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
2050528d3f19SVladimir Oltean 			continue;
2051528d3f19SVladimir Oltean 
2052528d3f19SVladimir Oltean 		visited &= ~BIT(port);
2053528d3f19SVladimir Oltean 	}
2054528d3f19SVladimir Oltean 
2055528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
2056dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
2057528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
205823ca3b72SVladimir Oltean 		int num_active_ports = 0;
2059dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
2060dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
2061dc96ee37SAlexandre Belloni 
2062528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
2063dc96ee37SAlexandre Belloni 			continue;
2064dc96ee37SAlexandre Belloni 
2065a14e6b69SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond);
2066528d3f19SVladimir Oltean 
2067dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
2068a14e6b69SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2069a14e6b69SVladimir Oltean 
2070dc96ee37SAlexandre Belloni 			// Destination mask
2071dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
2072dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
2073a14e6b69SVladimir Oltean 
2074a14e6b69SVladimir Oltean 			if (ocelot_port->lag_tx_active)
207523ca3b72SVladimir Oltean 				aggr_idx[num_active_ports++] = port;
2076dc96ee37SAlexandre Belloni 		}
2077dc96ee37SAlexandre Belloni 
207896b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
2079dc96ee37SAlexandre Belloni 			u32 ac;
2080dc96ee37SAlexandre Belloni 
2081dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
2082dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
208323ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
208423ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
208523ca3b72SVladimir Oltean 			 */
208623ca3b72SVladimir Oltean 			if (num_active_ports)
208723ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
2088dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
2089dc96ee37SAlexandre Belloni 		}
2090528d3f19SVladimir Oltean 
2091528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
2092528d3f19SVladimir Oltean 		 * the same config again.
2093528d3f19SVladimir Oltean 		 */
2094528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
2095528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2096528d3f19SVladimir Oltean 
2097528d3f19SVladimir Oltean 			if (!ocelot_port)
2098528d3f19SVladimir Oltean 				continue;
2099528d3f19SVladimir Oltean 
2100528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
2101528d3f19SVladimir Oltean 				visited |= BIT(port);
2102528d3f19SVladimir Oltean 		}
2103dc96ee37SAlexandre Belloni 	}
2104dc96ee37SAlexandre Belloni }
2105dc96ee37SAlexandre Belloni 
21062527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
21072527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
21082527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
21092527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
21102527f2e8SVladimir Oltean  */
21112527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
2112dc96ee37SAlexandre Belloni {
21132527f2e8SVladimir Oltean 	int port;
2114dc96ee37SAlexandre Belloni 
21152527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
21162527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
21172527f2e8SVladimir Oltean 		struct net_device *bond;
2118dc96ee37SAlexandre Belloni 
21192527f2e8SVladimir Oltean 		if (!ocelot_port)
21202527f2e8SVladimir Oltean 			continue;
2121dc96ee37SAlexandre Belloni 
21222527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
21232527f2e8SVladimir Oltean 		if (bond) {
2124961d8b69SVladimir Oltean 			int lag = ocelot_bond_get_id(ocelot, bond);
21252527f2e8SVladimir Oltean 
21262527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
2127dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
21282527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
21292527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
21302527f2e8SVladimir Oltean 		} else {
21312527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
21322527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
21332527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
21342527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
21352527f2e8SVladimir Oltean 		}
2136dc96ee37SAlexandre Belloni 	}
2137dc96ee37SAlexandre Belloni }
2138dc96ee37SAlexandre Belloni 
213928de0f9fSVladimir Oltean static int ocelot_migrate_mc(struct ocelot *ocelot, struct ocelot_multicast *mc,
214028de0f9fSVladimir Oltean 			     unsigned long from_mask, unsigned long to_mask)
214128de0f9fSVladimir Oltean {
214228de0f9fSVladimir Oltean 	unsigned char addr[ETH_ALEN];
214328de0f9fSVladimir Oltean 	struct ocelot_pgid *pgid;
214428de0f9fSVladimir Oltean 	u16 vid = mc->vid;
214528de0f9fSVladimir Oltean 
214628de0f9fSVladimir Oltean 	dev_dbg(ocelot->dev,
214728de0f9fSVladimir Oltean 		"Migrating multicast %pM vid %d from port mask 0x%lx to 0x%lx\n",
214828de0f9fSVladimir Oltean 		mc->addr, mc->vid, from_mask, to_mask);
214928de0f9fSVladimir Oltean 
215028de0f9fSVladimir Oltean 	/* First clean up the current port mask from hardware, because
215128de0f9fSVladimir Oltean 	 * we'll be modifying it.
215228de0f9fSVladimir Oltean 	 */
215328de0f9fSVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
215428de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
215528de0f9fSVladimir Oltean 	ocelot_mact_forget(ocelot, addr, vid);
215628de0f9fSVladimir Oltean 
215728de0f9fSVladimir Oltean 	mc->ports &= ~from_mask;
215828de0f9fSVladimir Oltean 	mc->ports |= to_mask;
215928de0f9fSVladimir Oltean 
216028de0f9fSVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
216128de0f9fSVladimir Oltean 	if (IS_ERR(pgid)) {
216228de0f9fSVladimir Oltean 		dev_err(ocelot->dev,
216328de0f9fSVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
216428de0f9fSVladimir Oltean 			mc->addr, mc->vid);
216528de0f9fSVladimir Oltean 		devm_kfree(ocelot->dev, mc);
216628de0f9fSVladimir Oltean 		return PTR_ERR(pgid);
216728de0f9fSVladimir Oltean 	}
216828de0f9fSVladimir Oltean 	mc->pgid = pgid;
216928de0f9fSVladimir Oltean 
217028de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
217128de0f9fSVladimir Oltean 
217228de0f9fSVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
217328de0f9fSVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
217428de0f9fSVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
217528de0f9fSVladimir Oltean 				 pgid->index);
217628de0f9fSVladimir Oltean 
217728de0f9fSVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
217828de0f9fSVladimir Oltean 				 mc->entry_type);
217928de0f9fSVladimir Oltean }
218028de0f9fSVladimir Oltean 
218128de0f9fSVladimir Oltean int ocelot_migrate_mdbs(struct ocelot *ocelot, unsigned long from_mask,
218228de0f9fSVladimir Oltean 			unsigned long to_mask)
218328de0f9fSVladimir Oltean {
218428de0f9fSVladimir Oltean 	struct ocelot_multicast *mc;
218528de0f9fSVladimir Oltean 	int err;
218628de0f9fSVladimir Oltean 
218728de0f9fSVladimir Oltean 	list_for_each_entry(mc, &ocelot->multicast, list) {
218828de0f9fSVladimir Oltean 		if (!(mc->ports & from_mask))
218928de0f9fSVladimir Oltean 			continue;
219028de0f9fSVladimir Oltean 
219128de0f9fSVladimir Oltean 		err = ocelot_migrate_mc(ocelot, mc, from_mask, to_mask);
219228de0f9fSVladimir Oltean 		if (err)
219328de0f9fSVladimir Oltean 			return err;
219428de0f9fSVladimir Oltean 	}
219528de0f9fSVladimir Oltean 
219628de0f9fSVladimir Oltean 	return 0;
219728de0f9fSVladimir Oltean }
219828de0f9fSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_migrate_mdbs);
219928de0f9fSVladimir Oltean 
2200961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says:
2201961d8b69SVladimir Oltean  *     Logical port number for front port. If port is not a member of a LLAG,
2202961d8b69SVladimir Oltean  *     then PORTID must be set to the physical port number.
2203961d8b69SVladimir Oltean  *     If port is a member of a LLAG, then PORTID must be set to the common
2204961d8b69SVladimir Oltean  *     PORTID_VAL used for all member ports of the LLAG.
2205961d8b69SVladimir Oltean  *     The value must not exceed the number of physical ports on the device.
2206961d8b69SVladimir Oltean  *
2207961d8b69SVladimir Oltean  * This means we have little choice but to migrate FDB entries pointing towards
2208961d8b69SVladimir Oltean  * a logical port when that changes.
2209961d8b69SVladimir Oltean  */
2210961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot,
2211961d8b69SVladimir Oltean 				    struct net_device *bond,
2212961d8b69SVladimir Oltean 				    int lag)
2213961d8b69SVladimir Oltean {
2214961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2215961d8b69SVladimir Oltean 	int err;
2216961d8b69SVladimir Oltean 
2217961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2218961d8b69SVladimir Oltean 
2219961d8b69SVladimir Oltean 	list_for_each_entry(fdb, &ocelot->lag_fdbs, list) {
2220961d8b69SVladimir Oltean 		if (fdb->bond != bond)
2221961d8b69SVladimir Oltean 			continue;
2222961d8b69SVladimir Oltean 
2223961d8b69SVladimir Oltean 		err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid);
2224961d8b69SVladimir Oltean 		if (err) {
2225961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2226961d8b69SVladimir Oltean 				"failed to delete LAG %s FDB %pM vid %d: %pe\n",
2227961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2228961d8b69SVladimir Oltean 		}
2229961d8b69SVladimir Oltean 
2230961d8b69SVladimir Oltean 		err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid,
2231961d8b69SVladimir Oltean 					ENTRYTYPE_LOCKED);
2232961d8b69SVladimir Oltean 		if (err) {
2233961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2234961d8b69SVladimir Oltean 				"failed to migrate LAG %s FDB %pM vid %d: %pe\n",
2235961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2236961d8b69SVladimir Oltean 		}
2237961d8b69SVladimir Oltean 	}
2238961d8b69SVladimir Oltean }
2239961d8b69SVladimir Oltean 
22409c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2241583cbbe3SVladimir Oltean 			 struct net_device *bond,
22422e359b00SVladimir Oltean 			 struct netdev_lag_upper_info *info,
22432e359b00SVladimir Oltean 			 struct netlink_ext_ack *extack)
2244dc96ee37SAlexandre Belloni {
22452e359b00SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
22462e359b00SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
22472e359b00SVladimir Oltean 				   "Can only offload LAG using hash TX type");
2248583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
22492e359b00SVladimir Oltean 	}
2250583cbbe3SVladimir Oltean 
22518abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
22528abe1970SVladimir Oltean 
2253b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
2254dc96ee37SAlexandre Belloni 
22552527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
22568abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2257dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2258dc96ee37SAlexandre Belloni 
22598abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
22608abe1970SVladimir Oltean 
2261dc96ee37SAlexandre Belloni 	return 0;
2262dc96ee37SAlexandre Belloni }
22639c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
2264dc96ee37SAlexandre Belloni 
22659c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2266dc96ee37SAlexandre Belloni 			   struct net_device *bond)
2267dc96ee37SAlexandre Belloni {
2268961d8b69SVladimir Oltean 	int old_lag_id, new_lag_id;
2269961d8b69SVladimir Oltean 
22708abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
22718abe1970SVladimir Oltean 
2272961d8b69SVladimir Oltean 	old_lag_id = ocelot_bond_get_id(ocelot, bond);
2273961d8b69SVladimir Oltean 
2274b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
2275b80af659SVladimir Oltean 
22762527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
22778abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2278dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
22798abe1970SVladimir Oltean 
2280961d8b69SVladimir Oltean 	new_lag_id = ocelot_bond_get_id(ocelot, bond);
2281961d8b69SVladimir Oltean 
2282961d8b69SVladimir Oltean 	if (new_lag_id >= 0 && old_lag_id != new_lag_id)
2283961d8b69SVladimir Oltean 		ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id);
2284961d8b69SVladimir Oltean 
22858abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2286dc96ee37SAlexandre Belloni }
22879c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
22880e332c85SPetr Machata 
228923ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
229023ca3b72SVladimir Oltean {
229123ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
229223ca3b72SVladimir Oltean 
2293961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2294961d8b69SVladimir Oltean 
229523ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
229623ca3b72SVladimir Oltean 
229723ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
229823ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
2299961d8b69SVladimir Oltean 
2300961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
230123ca3b72SVladimir Oltean }
230223ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
230323ca3b72SVladimir Oltean 
2304961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond,
230554c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
230654c31984SVladimir Oltean 		       const struct net_device *bridge)
2307961d8b69SVladimir Oltean {
2308961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2309961d8b69SVladimir Oltean 	int lag, err;
2310961d8b69SVladimir Oltean 
2311961d8b69SVladimir Oltean 	fdb = kzalloc(sizeof(*fdb), GFP_KERNEL);
2312961d8b69SVladimir Oltean 	if (!fdb)
2313961d8b69SVladimir Oltean 		return -ENOMEM;
2314961d8b69SVladimir Oltean 
231554c31984SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
231654c31984SVladimir Oltean 
231754c31984SVladimir Oltean 	if (!vid)
231854c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
231954c31984SVladimir Oltean 
2320961d8b69SVladimir Oltean 	ether_addr_copy(fdb->addr, addr);
2321961d8b69SVladimir Oltean 	fdb->vid = vid;
2322961d8b69SVladimir Oltean 	fdb->bond = bond;
2323961d8b69SVladimir Oltean 
2324961d8b69SVladimir Oltean 	lag = ocelot_bond_get_id(ocelot, bond);
2325961d8b69SVladimir Oltean 
2326961d8b69SVladimir Oltean 	err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED);
2327961d8b69SVladimir Oltean 	if (err) {
2328961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2329961d8b69SVladimir Oltean 		kfree(fdb);
2330961d8b69SVladimir Oltean 		return err;
2331961d8b69SVladimir Oltean 	}
2332961d8b69SVladimir Oltean 
2333961d8b69SVladimir Oltean 	list_add_tail(&fdb->list, &ocelot->lag_fdbs);
2334961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2335961d8b69SVladimir Oltean 
2336961d8b69SVladimir Oltean 	return 0;
2337961d8b69SVladimir Oltean }
2338961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add);
2339961d8b69SVladimir Oltean 
2340961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond,
234154c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
234254c31984SVladimir Oltean 		       const struct net_device *bridge)
2343961d8b69SVladimir Oltean {
2344961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb, *tmp;
2345961d8b69SVladimir Oltean 
2346961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2347961d8b69SVladimir Oltean 
234854c31984SVladimir Oltean 	if (!vid)
234954c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
235054c31984SVladimir Oltean 
2351961d8b69SVladimir Oltean 	list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) {
2352961d8b69SVladimir Oltean 		if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid ||
2353961d8b69SVladimir Oltean 		    fdb->bond != bond)
2354961d8b69SVladimir Oltean 			continue;
2355961d8b69SVladimir Oltean 
2356961d8b69SVladimir Oltean 		ocelot_mact_forget(ocelot, addr, vid);
2357961d8b69SVladimir Oltean 		list_del(&fdb->list);
2358961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2359961d8b69SVladimir Oltean 		kfree(fdb);
2360961d8b69SVladimir Oltean 
2361961d8b69SVladimir Oltean 		return 0;
2362961d8b69SVladimir Oltean 	}
2363961d8b69SVladimir Oltean 
2364961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2365961d8b69SVladimir Oltean 
2366961d8b69SVladimir Oltean 	return -ENOENT;
2367961d8b69SVladimir Oltean }
2368961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del);
2369961d8b69SVladimir Oltean 
2370a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2371a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
23720b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
23730b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
23740b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2375a8015dedSVladimir Oltean  */
23760b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
237731350d7fSVladimir Oltean {
237831350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2379a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2380e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
2381601e984fSVladimir Oltean 	int atop, atop_tot;
238231350d7fSVladimir Oltean 
23830b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
23840b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
23850b912fc9SVladimir Oltean 
2386cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
23870b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2388cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
23890b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
23900b912fc9SVladimir Oltean 	}
23910b912fc9SVladimir Oltean 
2392a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2393fa914e9cSVladimir Oltean 
2394e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
2395e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2396e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2397541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2398541132f0SMaxim Kochetkov 			    pause_start);
2399541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2400541132f0SMaxim Kochetkov 			    pause_stop);
2401fa914e9cSVladimir Oltean 
2402601e984fSVladimir Oltean 	/* Tail dropping watermarks */
2403f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2404a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2405601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2406601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2407601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2408fa914e9cSVladimir Oltean }
24090b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
24100b912fc9SVladimir Oltean 
24110b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
24120b912fc9SVladimir Oltean {
24130b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
24140b912fc9SVladimir Oltean 
24150b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
24160b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
24170b912fc9SVladimir Oltean 
2418cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
24190b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2420cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
24210b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
24220b912fc9SVladimir Oltean 	}
24230b912fc9SVladimir Oltean 
24240b912fc9SVladimir Oltean 	return max_mtu;
24250b912fc9SVladimir Oltean }
24260b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2427fa914e9cSVladimir Oltean 
2428421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2429421741eaSVladimir Oltean 				     bool enabled)
2430421741eaSVladimir Oltean {
2431421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2432421741eaSVladimir Oltean 	u32 val = 0;
2433421741eaSVladimir Oltean 
2434421741eaSVladimir Oltean 	if (enabled)
2435421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2436421741eaSVladimir Oltean 
2437421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2438421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2439421741eaSVladimir Oltean 
2440421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2441421741eaSVladimir Oltean }
2442421741eaSVladimir Oltean 
2443421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2444421741eaSVladimir Oltean 					bool enabled)
2445421741eaSVladimir Oltean {
2446421741eaSVladimir Oltean 	u32 val = 0;
2447421741eaSVladimir Oltean 
2448421741eaSVladimir Oltean 	if (enabled)
2449421741eaSVladimir Oltean 		val = BIT(port);
2450421741eaSVladimir Oltean 
2451421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2452421741eaSVladimir Oltean }
2453421741eaSVladimir Oltean 
2454421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2455421741eaSVladimir Oltean 					bool enabled)
2456421741eaSVladimir Oltean {
2457421741eaSVladimir Oltean 	u32 val = 0;
2458421741eaSVladimir Oltean 
2459421741eaSVladimir Oltean 	if (enabled)
2460421741eaSVladimir Oltean 		val = BIT(port);
2461421741eaSVladimir Oltean 
2462421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
24634cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4);
24644cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6);
2465421741eaSVladimir Oltean }
2466421741eaSVladimir Oltean 
2467421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2468421741eaSVladimir Oltean 					bool enabled)
2469421741eaSVladimir Oltean {
2470421741eaSVladimir Oltean 	u32 val = 0;
2471421741eaSVladimir Oltean 
2472421741eaSVladimir Oltean 	if (enabled)
2473421741eaSVladimir Oltean 		val = BIT(port);
2474421741eaSVladimir Oltean 
2475421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2476421741eaSVladimir Oltean }
2477421741eaSVladimir Oltean 
2478421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2479421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2480421741eaSVladimir Oltean {
2481421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2482421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2483421741eaSVladimir Oltean 		return -EINVAL;
2484421741eaSVladimir Oltean 
2485421741eaSVladimir Oltean 	return 0;
2486421741eaSVladimir Oltean }
2487421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2488421741eaSVladimir Oltean 
2489421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2490421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2491421741eaSVladimir Oltean {
2492421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2493421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2494421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2495421741eaSVladimir Oltean 
2496421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2497421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2498421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2499421741eaSVladimir Oltean 
2500421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2501421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2502421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2503421741eaSVladimir Oltean 
2504421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2505421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2506421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2507421741eaSVladimir Oltean }
2508421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2509421741eaSVladimir Oltean 
2510978777d0SVladimir Oltean int ocelot_port_get_default_prio(struct ocelot *ocelot, int port)
2511978777d0SVladimir Oltean {
2512978777d0SVladimir Oltean 	int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2513978777d0SVladimir Oltean 
2514978777d0SVladimir Oltean 	return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val);
2515978777d0SVladimir Oltean }
2516978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio);
2517978777d0SVladimir Oltean 
2518978777d0SVladimir Oltean int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio)
2519978777d0SVladimir Oltean {
252072f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2521978777d0SVladimir Oltean 		return -ERANGE;
2522978777d0SVladimir Oltean 
2523978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot,
2524978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio),
2525978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M,
2526978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG,
2527978777d0SVladimir Oltean 		       port);
2528978777d0SVladimir Oltean 
2529978777d0SVladimir Oltean 	return 0;
2530978777d0SVladimir Oltean }
2531978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio);
2532978777d0SVladimir Oltean 
2533978777d0SVladimir Oltean int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp)
2534978777d0SVladimir Oltean {
2535978777d0SVladimir Oltean 	int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2536978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2537978777d0SVladimir Oltean 
2538978777d0SVladimir Oltean 	/* Return error if DSCP prioritization isn't enabled */
2539978777d0SVladimir Oltean 	if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA))
2540978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2541978777d0SVladimir Oltean 
2542978777d0SVladimir Oltean 	if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) {
2543978777d0SVladimir Oltean 		dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg);
2544978777d0SVladimir Oltean 		/* Re-read ANA_DSCP_CFG for the translated DSCP */
2545978777d0SVladimir Oltean 		dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2546978777d0SVladimir Oltean 	}
2547978777d0SVladimir Oltean 
2548978777d0SVladimir Oltean 	/* If the DSCP value is not trusted, the QoS classification falls back
2549978777d0SVladimir Oltean 	 * to VLAN PCP or port-based default.
2550978777d0SVladimir Oltean 	 */
2551978777d0SVladimir Oltean 	if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA))
2552978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2553978777d0SVladimir Oltean 
2554978777d0SVladimir Oltean 	return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg);
2555978777d0SVladimir Oltean }
2556978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio);
2557978777d0SVladimir Oltean 
2558978777d0SVladimir Oltean int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2559978777d0SVladimir Oltean {
2560978777d0SVladimir Oltean 	int mask, val;
2561978777d0SVladimir Oltean 
256272f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2563978777d0SVladimir Oltean 		return -ERANGE;
2564978777d0SVladimir Oltean 
2565978777d0SVladimir Oltean 	/* There is at least one app table priority (this one), so we need to
2566978777d0SVladimir Oltean 	 * make sure DSCP prioritization is enabled on the port.
2567978777d0SVladimir Oltean 	 * Also make sure DSCP translation is disabled
2568978777d0SVladimir Oltean 	 * (dcbnl doesn't support it).
2569978777d0SVladimir Oltean 	 */
2570978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2571978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2572978777d0SVladimir Oltean 
2573978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask,
2574978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG, port);
2575978777d0SVladimir Oltean 
2576978777d0SVladimir Oltean 	/* Trust this DSCP value and map it to the given QoS class */
2577978777d0SVladimir Oltean 	val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio);
2578978777d0SVladimir Oltean 
2579978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp);
2580978777d0SVladimir Oltean 
2581978777d0SVladimir Oltean 	return 0;
2582978777d0SVladimir Oltean }
2583978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio);
2584978777d0SVladimir Oltean 
2585978777d0SVladimir Oltean int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2586978777d0SVladimir Oltean {
2587978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2588978777d0SVladimir Oltean 	int mask, i;
2589978777d0SVladimir Oltean 
2590978777d0SVladimir Oltean 	/* During a "dcb app replace" command, the new app table entry will be
2591978777d0SVladimir Oltean 	 * added first, then the old one will be deleted. But the hardware only
2592978777d0SVladimir Oltean 	 * supports one QoS class per DSCP value (duh), so if we blindly delete
2593978777d0SVladimir Oltean 	 * the app table entry for this DSCP value, we end up deleting the
2594978777d0SVladimir Oltean 	 * entry with the new priority. Avoid that by checking whether user
2595978777d0SVladimir Oltean 	 * space wants to delete the priority which is currently configured, or
2596978777d0SVladimir Oltean 	 * something else which is no longer current.
2597978777d0SVladimir Oltean 	 */
2598978777d0SVladimir Oltean 	if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio)
2599978777d0SVladimir Oltean 		return 0;
2600978777d0SVladimir Oltean 
2601978777d0SVladimir Oltean 	/* Untrust this DSCP value */
2602978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp);
2603978777d0SVladimir Oltean 
2604978777d0SVladimir Oltean 	for (i = 0; i < 64; i++) {
2605978777d0SVladimir Oltean 		int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i);
2606978777d0SVladimir Oltean 
2607978777d0SVladimir Oltean 		/* There are still app table entries on the port, so we need to
2608978777d0SVladimir Oltean 		 * keep DSCP enabled, nothing to do.
2609978777d0SVladimir Oltean 		 */
2610978777d0SVladimir Oltean 		if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)
2611978777d0SVladimir Oltean 			return 0;
2612978777d0SVladimir Oltean 	}
2613978777d0SVladimir Oltean 
2614978777d0SVladimir Oltean 	/* Disable DSCP QoS classification if there isn't any trusted
2615978777d0SVladimir Oltean 	 * DSCP value left.
2616978777d0SVladimir Oltean 	 */
2617978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2618978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2619978777d0SVladimir Oltean 
2620978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port);
2621978777d0SVladimir Oltean 
2622978777d0SVladimir Oltean 	return 0;
2623978777d0SVladimir Oltean }
2624978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio);
2625978777d0SVladimir Oltean 
2626f2a0e216SVladimir Oltean struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to,
2627ccb6ed42SVladimir Oltean 					struct netlink_ext_ack *extack)
2628ccb6ed42SVladimir Oltean {
2629ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2630ccb6ed42SVladimir Oltean 
2631ccb6ed42SVladimir Oltean 	if (m) {
2632ccb6ed42SVladimir Oltean 		if (m->to != to) {
2633ccb6ed42SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
2634ccb6ed42SVladimir Oltean 					   "Mirroring already configured towards different egress port");
2635ccb6ed42SVladimir Oltean 			return ERR_PTR(-EBUSY);
2636ccb6ed42SVladimir Oltean 		}
2637ccb6ed42SVladimir Oltean 
2638ccb6ed42SVladimir Oltean 		refcount_inc(&m->refcount);
2639ccb6ed42SVladimir Oltean 		return m;
2640ccb6ed42SVladimir Oltean 	}
2641ccb6ed42SVladimir Oltean 
2642ccb6ed42SVladimir Oltean 	m = kzalloc(sizeof(*m), GFP_KERNEL);
2643ccb6ed42SVladimir Oltean 	if (!m)
2644ccb6ed42SVladimir Oltean 		return ERR_PTR(-ENOMEM);
2645ccb6ed42SVladimir Oltean 
2646ccb6ed42SVladimir Oltean 	m->to = to;
2647ccb6ed42SVladimir Oltean 	refcount_set(&m->refcount, 1);
2648ccb6ed42SVladimir Oltean 	ocelot->mirror = m;
2649ccb6ed42SVladimir Oltean 
2650ccb6ed42SVladimir Oltean 	/* Program the mirror port to hardware */
2651ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS);
2652ccb6ed42SVladimir Oltean 
2653ccb6ed42SVladimir Oltean 	return m;
2654ccb6ed42SVladimir Oltean }
2655ccb6ed42SVladimir Oltean 
2656f2a0e216SVladimir Oltean void ocelot_mirror_put(struct ocelot *ocelot)
2657ccb6ed42SVladimir Oltean {
2658ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2659ccb6ed42SVladimir Oltean 
2660ccb6ed42SVladimir Oltean 	if (!refcount_dec_and_test(&m->refcount))
2661ccb6ed42SVladimir Oltean 		return;
2662ccb6ed42SVladimir Oltean 
2663ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_MIRRORPORTS);
2664ccb6ed42SVladimir Oltean 	ocelot->mirror = NULL;
2665ccb6ed42SVladimir Oltean 	kfree(m);
2666ccb6ed42SVladimir Oltean }
2667ccb6ed42SVladimir Oltean 
2668ccb6ed42SVladimir Oltean int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to,
2669ccb6ed42SVladimir Oltean 			   bool ingress, struct netlink_ext_ack *extack)
2670ccb6ed42SVladimir Oltean {
2671ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack);
2672ccb6ed42SVladimir Oltean 
2673ccb6ed42SVladimir Oltean 	if (IS_ERR(m))
2674ccb6ed42SVladimir Oltean 		return PTR_ERR(m);
2675ccb6ed42SVladimir Oltean 
2676ccb6ed42SVladimir Oltean 	if (ingress) {
2677ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2678ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2679ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2680ccb6ed42SVladimir Oltean 	} else {
2681ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, BIT(from), BIT(from),
2682ccb6ed42SVladimir Oltean 			   ANA_EMIRRORPORTS);
2683ccb6ed42SVladimir Oltean 	}
2684ccb6ed42SVladimir Oltean 
2685ccb6ed42SVladimir Oltean 	return 0;
2686ccb6ed42SVladimir Oltean }
2687ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_add);
2688ccb6ed42SVladimir Oltean 
2689ccb6ed42SVladimir Oltean void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress)
2690ccb6ed42SVladimir Oltean {
2691ccb6ed42SVladimir Oltean 	if (ingress) {
2692ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2693ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2694ccb6ed42SVladimir Oltean 	} else {
2695ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS);
2696ccb6ed42SVladimir Oltean 	}
2697ccb6ed42SVladimir Oltean 
2698ccb6ed42SVladimir Oltean 	ocelot_mirror_put(ocelot);
2699ccb6ed42SVladimir Oltean }
2700ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_del);
2701ccb6ed42SVladimir Oltean 
27025e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2703fa914e9cSVladimir Oltean {
2704fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2705fa914e9cSVladimir Oltean 
2706b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
270731350d7fSVladimir Oltean 
270831350d7fSVladimir Oltean 	/* Basic L2 initialization */
270931350d7fSVladimir Oltean 
27105bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
27115bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
27125bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
27135bc9d2e6SVladimir Oltean 	 */
27145bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
27155bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
27165bc9d2e6SVladimir Oltean 
27175bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
27185bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
27195bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
27205bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
27215bc9d2e6SVladimir Oltean 	mdelay(1);
27225bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
27235bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
27245bc9d2e6SVladimir Oltean 
27255bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2726a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
27275bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
27285bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2729a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
27305bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
27315bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
27325bc9d2e6SVladimir Oltean 
27335bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
27345bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
27355bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
27365bc9d2e6SVladimir Oltean 
2737e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2738541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2739e8e6e73dSVladimir Oltean 
274031350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
274131350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
274231350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
274331350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
274431350d7fSVladimir Oltean 
274531350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
274631350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
274731350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
274831350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
274931350d7fSVladimir Oltean 
2750421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2751421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2752421741eaSVladimir Oltean 
275346efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
275446efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
275546efe4efSVladimir Oltean 	 * automatic.
275646efe4efSVladimir Oltean 	 */
275746efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
275846efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
275946efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
276046efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
276146efe4efSVladimir Oltean 
276231350d7fSVladimir Oltean 	/* Enable vcap lookups */
276331350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
276431350d7fSVladimir Oltean }
27655e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
276631350d7fSVladimir Oltean 
27672d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
27682d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
27692d44b097SVladimir Oltean  * NPI mode is used).
277069df578cSVladimir Oltean  */
27712d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
277221468199SVladimir Oltean {
277369df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
277469df578cSVladimir Oltean 
277569df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
277621468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
277769df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
277869df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
277969df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
278069df578cSVladimir Oltean 	 */
278121468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
278221468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
278321468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
278421468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
278521468199SVladimir Oltean 
278669df578cSVladimir Oltean 	/* Enable CPU port module */
2787886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
278869df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2789886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2790cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2791886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2792cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
279321468199SVladimir Oltean 
279421468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
2795bfbab310SVladimir Oltean 	ocelot_write_gix(ocelot,
279654c31984SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) |
279721468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
279821468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
279921468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
280021468199SVladimir Oltean }
280121468199SVladimir Oltean 
2802f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2803f6fe01d6SVladimir Oltean {
2804f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2805f6fe01d6SVladimir Oltean 
2806f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2807f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2808f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2809f6fe01d6SVladimir Oltean 	 */
2810f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2811f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2812f6fe01d6SVladimir Oltean 
2813f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2814f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2815f6fe01d6SVladimir Oltean }
2816f6fe01d6SVladimir Oltean 
2817b67f5502SColin Foster static int ocelot_mem_init_status(struct ocelot *ocelot)
2818b67f5502SColin Foster {
2819b67f5502SColin Foster 	unsigned int val;
2820b67f5502SColin Foster 	int err;
2821b67f5502SColin Foster 
2822b67f5502SColin Foster 	err = regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
2823b67f5502SColin Foster 				&val);
2824b67f5502SColin Foster 
2825b67f5502SColin Foster 	return err ?: val;
2826b67f5502SColin Foster }
2827b67f5502SColin Foster 
2828b67f5502SColin Foster int ocelot_reset(struct ocelot *ocelot)
2829b67f5502SColin Foster {
2830b67f5502SColin Foster 	int err;
2831b67f5502SColin Foster 	u32 val;
2832b67f5502SColin Foster 
2833b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
2834b67f5502SColin Foster 	if (err)
2835b67f5502SColin Foster 		return err;
2836b67f5502SColin Foster 
2837b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
2838b67f5502SColin Foster 	if (err)
2839b67f5502SColin Foster 		return err;
2840b67f5502SColin Foster 
2841b67f5502SColin Foster 	/* MEM_INIT is a self-clearing bit. Wait for it to be cleared (should be
2842b67f5502SColin Foster 	 * 100us) before enabling the switch core.
2843b67f5502SColin Foster 	 */
2844b67f5502SColin Foster 	err = readx_poll_timeout(ocelot_mem_init_status, ocelot, val, !val,
2845b67f5502SColin Foster 				 MEM_INIT_SLEEP_US, MEM_INIT_TIMEOUT_US);
2846b67f5502SColin Foster 	if (err)
2847b67f5502SColin Foster 		return err;
2848b67f5502SColin Foster 
2849b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
2850b67f5502SColin Foster 	if (err)
2851b67f5502SColin Foster 		return err;
2852b67f5502SColin Foster 
2853b67f5502SColin Foster 	return regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
2854b67f5502SColin Foster }
2855b67f5502SColin Foster EXPORT_SYMBOL(ocelot_reset);
2856b67f5502SColin Foster 
2857a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2858a556c76aSAlexandre Belloni {
285921468199SVladimir Oltean 	int i, ret;
286021468199SVladimir Oltean 	u32 port;
2861a556c76aSAlexandre Belloni 
28623a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
28633a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
28643a77b593SVladimir Oltean 		if (ret) {
28653a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
28663a77b593SVladimir Oltean 			return ret;
28673a77b593SVladimir Oltean 		}
28683a77b593SVladimir Oltean 	}
28693a77b593SVladimir Oltean 
28704e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
28712468346cSVladimir Oltean 	mutex_init(&ocelot->mact_lock);
28728abe1970SVladimir Oltean 	mutex_init(&ocelot->fwd_domain_lock);
28738670dc33SXiaoliang Yang 	mutex_init(&ocelot->tas_lock);
28744e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
287552849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
2876a556c76aSAlexandre Belloni 
2877ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2878fe90104cSVladimir Oltean 	if (!ocelot->owq)
2879ca0b272bSVladimir Oltean 		return -ENOMEM;
2880fe90104cSVladimir Oltean 
2881fe90104cSVladimir Oltean 	ret = ocelot_stats_init(ocelot);
28826505b680SVladimir Oltean 	if (ret)
28836505b680SVladimir Oltean 		goto err_stats_init;
2884ca0b272bSVladimir Oltean 
28852b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2886e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
288790e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
2888961d8b69SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->lag_fdbs);
2889f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2890a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2891a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2892aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
28932d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2894a556c76aSAlexandre Belloni 
289523e2c506SXiaoliang Yang 	if (ocelot->ops->psfp_init)
289623e2c506SXiaoliang Yang 		ocelot->ops->psfp_init(ocelot);
289723e2c506SXiaoliang Yang 
28986505b680SVladimir Oltean 	if (ocelot->mm_supported) {
28996505b680SVladimir Oltean 		ret = ocelot_mm_init(ocelot);
29006505b680SVladimir Oltean 		if (ret)
29016505b680SVladimir Oltean 			goto err_mm_init;
29026505b680SVladimir Oltean 	}
29036505b680SVladimir Oltean 
2904a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2905a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2906a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2907a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2908a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2909a556c76aSAlexandre Belloni 	}
2910a556c76aSAlexandre Belloni 
2911a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2912a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2913a556c76aSAlexandre Belloni 
2914a556c76aSAlexandre Belloni 	/* Aggregation mode */
2915a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2916a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2917a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2918f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2919f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2920f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2921f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2922a556c76aSAlexandre Belloni 
2923a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2924a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2925a556c76aSAlexandre Belloni 	 */
2926a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2927a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2928a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2929a556c76aSAlexandre Belloni 
2930a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2931a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2932a556c76aSAlexandre Belloni 
2933a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2934a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2935a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2936a556c76aSAlexandre Belloni 
2937a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2938edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2939a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2940b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2941a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2942edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2943a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2944a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2945a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2946a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2947a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2948a556c76aSAlexandre Belloni 
2949a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2950a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2951a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2952a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2953a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2954a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2955a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2956a556c76aSAlexandre Belloni 				 port);
2957a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2958a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2959a556c76aSAlexandre Belloni 	}
2960a556c76aSAlexandre Belloni 
296196b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2962a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2963a556c76aSAlexandre Belloni 
2964a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2965a556c76aSAlexandre Belloni 	}
2966ebb1bb40SHoratiu Vultur 
2967ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2968ebb1bb40SHoratiu Vultur 
2969b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2970b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2971b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2972a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2973b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2974b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2975b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2976a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2977a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2978a556c76aSAlexandre Belloni 
2979a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2980a556c76aSAlexandre Belloni 	 * registers endianness.
2981a556c76aSAlexandre Belloni 	 */
2982a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2983a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2984a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2985a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2986a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2987a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2988a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2989a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2990a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2991a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2992a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2993a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2994a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2995a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2996a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2997a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2998a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2999a556c76aSAlexandre Belloni 
3000a556c76aSAlexandre Belloni 	return 0;
30016505b680SVladimir Oltean 
30026505b680SVladimir Oltean err_mm_init:
30036505b680SVladimir Oltean 	ocelot_stats_deinit(ocelot);
30046505b680SVladimir Oltean err_stats_init:
30056505b680SVladimir Oltean 	destroy_workqueue(ocelot->owq);
30066505b680SVladimir Oltean 	return ret;
3007a556c76aSAlexandre Belloni }
3008a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
3009a556c76aSAlexandre Belloni 
3010a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
3011a556c76aSAlexandre Belloni {
3012fe90104cSVladimir Oltean 	ocelot_stats_deinit(ocelot);
3013ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
3014a556c76aSAlexandre Belloni }
3015a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
3016a556c76aSAlexandre Belloni 
3017e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
3018e5fb512dSVladimir Oltean {
3019e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3020e5fb512dSVladimir Oltean 
3021e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
3022e5fb512dSVladimir Oltean }
3023e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
3024e5fb512dSVladimir Oltean 
3025a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
3026