xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision aac80140dc31963d818a65a522c2e2da81979857)
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>
10dfca93edSColin Foster #include <linux/phy/phy.h>
11*aac80140SVladimir Oltean #include <net/pkt_sched.h>
12fec53f44SColin Foster #include <soc/mscc/ocelot_hsio.h>
1320968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
14a556c76aSAlexandre Belloni #include "ocelot.h"
153c83654fSVladimir Oltean #include "ocelot_vcap.h"
16a556c76aSAlexandre Belloni 
17639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US	10
18639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US	100000
19b67f5502SColin Foster #define MEM_INIT_SLEEP_US	1000
20b67f5502SColin Foster #define MEM_INIT_TIMEOUT_US	100000
21b67f5502SColin Foster 
2254c31984SVladimir Oltean #define OCELOT_RSV_VLAN_RANGE_START 4000
23639c1b26SSteen Hegelund 
24a556c76aSAlexandre Belloni struct ocelot_mact_entry {
25a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
26a556c76aSAlexandre Belloni 	u16 vid;
27a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
28a556c76aSAlexandre Belloni };
29a556c76aSAlexandre Belloni 
302468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
31639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
32639c1b26SSteen Hegelund {
33639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
34639c1b26SSteen Hegelund }
35639c1b26SSteen Hegelund 
362468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
37a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
38a556c76aSAlexandre Belloni {
39639c1b26SSteen Hegelund 	u32 val;
40a556c76aSAlexandre Belloni 
41639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
42639c1b26SSteen Hegelund 		ocelot, val,
43639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
44639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
45639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
46a556c76aSAlexandre Belloni }
47a556c76aSAlexandre Belloni 
482468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
49a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
50a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
51a556c76aSAlexandre Belloni 			       unsigned int vid)
52a556c76aSAlexandre Belloni {
53a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
54a556c76aSAlexandre Belloni 
55a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
56a556c76aSAlexandre Belloni 	 * understood by the hardware.
57a556c76aSAlexandre Belloni 	 */
58a556c76aSAlexandre Belloni 	mach |= vid    << 16;
59a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
60a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
61a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
62a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
63a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
64a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
65a556c76aSAlexandre Belloni 
66a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
67a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
68a556c76aSAlexandre Belloni 
69a556c76aSAlexandre Belloni }
70a556c76aSAlexandre Belloni 
710568c3bfSXiaoliang Yang static int __ocelot_mact_learn(struct ocelot *ocelot, int port,
72a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
739c90eea3SVladimir Oltean 			       unsigned int vid, enum macaccess_entry_type type)
74a556c76aSAlexandre Belloni {
75584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
76584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
77584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
78584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
79584b7cfcSAlban Bedel 	unsigned int mc_ports;
802468346cSVladimir Oltean 	int err;
81584b7cfcSAlban Bedel 
82584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
83584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
84584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
85584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
86584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
87584b7cfcSAlban Bedel 	else
88584b7cfcSAlban Bedel 		mc_ports = 0;
89584b7cfcSAlban Bedel 
90584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
91584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
92584b7cfcSAlban Bedel 
93a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
94a556c76aSAlexandre Belloni 
95a556c76aSAlexandre Belloni 	/* Issue a write command */
96584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
97a556c76aSAlexandre Belloni 
982468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
992468346cSVladimir Oltean 
1000568c3bfSXiaoliang Yang 	return err;
1010568c3bfSXiaoliang Yang }
1020568c3bfSXiaoliang Yang 
1030568c3bfSXiaoliang Yang int ocelot_mact_learn(struct ocelot *ocelot, int port,
1040568c3bfSXiaoliang Yang 		      const unsigned char mac[ETH_ALEN],
1050568c3bfSXiaoliang Yang 		      unsigned int vid, enum macaccess_entry_type type)
1060568c3bfSXiaoliang Yang {
1070568c3bfSXiaoliang Yang 	int ret;
1080568c3bfSXiaoliang Yang 
1090568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1100568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, port, mac, vid, type);
1112468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1122468346cSVladimir Oltean 
1130568c3bfSXiaoliang Yang 	return ret;
114a556c76aSAlexandre Belloni }
1159c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
116a556c76aSAlexandre Belloni 
1179c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
1189c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
119a556c76aSAlexandre Belloni {
1202468346cSVladimir Oltean 	int err;
1212468346cSVladimir Oltean 
1222468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
1232468346cSVladimir Oltean 
124a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
125a556c76aSAlexandre Belloni 
126a556c76aSAlexandre Belloni 	/* Issue a forget command */
127a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
128a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
129a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
130a556c76aSAlexandre Belloni 
1312468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
1322468346cSVladimir Oltean 
1332468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1342468346cSVladimir Oltean 
1352468346cSVladimir Oltean 	return err;
136a556c76aSAlexandre Belloni }
1379c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
138a556c76aSAlexandre Belloni 
1390568c3bfSXiaoliang Yang int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx,
1400568c3bfSXiaoliang Yang 		       const unsigned char mac[ETH_ALEN],
1410568c3bfSXiaoliang Yang 		       unsigned int vid, enum macaccess_entry_type *type)
1420568c3bfSXiaoliang Yang {
1430568c3bfSXiaoliang Yang 	int val;
1440568c3bfSXiaoliang Yang 
1450568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1460568c3bfSXiaoliang Yang 
1470568c3bfSXiaoliang Yang 	ocelot_mact_select(ocelot, mac, vid);
1480568c3bfSXiaoliang Yang 
1490568c3bfSXiaoliang Yang 	/* Issue a read command with MACACCESS_VALID=1. */
1500568c3bfSXiaoliang Yang 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
1510568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1520568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS);
1530568c3bfSXiaoliang Yang 
1540568c3bfSXiaoliang Yang 	if (ocelot_mact_wait_for_completion(ocelot)) {
1550568c3bfSXiaoliang Yang 		mutex_unlock(&ocelot->mact_lock);
1560568c3bfSXiaoliang Yang 		return -ETIMEDOUT;
1570568c3bfSXiaoliang Yang 	}
1580568c3bfSXiaoliang Yang 
1590568c3bfSXiaoliang Yang 	/* Read back the entry flags */
1600568c3bfSXiaoliang Yang 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1610568c3bfSXiaoliang Yang 
1620568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1630568c3bfSXiaoliang Yang 
1640568c3bfSXiaoliang Yang 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1650568c3bfSXiaoliang Yang 		return -ENOENT;
1660568c3bfSXiaoliang Yang 
1670568c3bfSXiaoliang Yang 	*dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val);
1680568c3bfSXiaoliang Yang 	*type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val);
1690568c3bfSXiaoliang Yang 
1700568c3bfSXiaoliang Yang 	return 0;
1710568c3bfSXiaoliang Yang }
1720568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_lookup);
1730568c3bfSXiaoliang Yang 
1740568c3bfSXiaoliang Yang int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx,
1750568c3bfSXiaoliang Yang 				 const unsigned char mac[ETH_ALEN],
1760568c3bfSXiaoliang Yang 				 unsigned int vid,
1770568c3bfSXiaoliang Yang 				 enum macaccess_entry_type type,
1780568c3bfSXiaoliang Yang 				 int sfid, int ssid)
1790568c3bfSXiaoliang Yang {
1800568c3bfSXiaoliang Yang 	int ret;
1810568c3bfSXiaoliang Yang 
1820568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1830568c3bfSXiaoliang Yang 
1840568c3bfSXiaoliang Yang 	ocelot_write(ocelot,
1850568c3bfSXiaoliang Yang 		     (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) |
1860568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SFID(sfid) |
1870568c3bfSXiaoliang Yang 		     (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) |
1880568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SSID(ssid),
1890568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA);
1900568c3bfSXiaoliang Yang 
1910568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type);
1920568c3bfSXiaoliang Yang 
1930568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1940568c3bfSXiaoliang Yang 
1950568c3bfSXiaoliang Yang 	return ret;
1960568c3bfSXiaoliang Yang }
1970568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_learn_streamdata);
1980568c3bfSXiaoliang Yang 
199a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
200a556c76aSAlexandre Belloni {
201a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
202a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
203a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
204a556c76aSAlexandre Belloni 	 */
205a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
206a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
207a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
208a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
209a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
210a556c76aSAlexandre Belloni 
2112468346cSVladimir Oltean 	/* Clear the MAC table. We are not concurrent with anyone, so
2122468346cSVladimir Oltean 	 * holding &ocelot->mact_lock is pointless.
2132468346cSVladimir Oltean 	 */
214a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
215a556c76aSAlexandre Belloni }
216a556c76aSAlexandre Belloni 
217fec53f44SColin Foster void ocelot_pll5_init(struct ocelot *ocelot)
218fec53f44SColin Foster {
219fec53f44SColin Foster 	/* Configure PLL5. This will need a proper CCF driver
220fec53f44SColin Foster 	 * The values are coming from the VTSS API for Ocelot
221fec53f44SColin Foster 	 */
222fec53f44SColin Foster 	regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG4,
223fec53f44SColin Foster 		     HSIO_PLL5G_CFG4_IB_CTRL(0x7600) |
224fec53f44SColin Foster 		     HSIO_PLL5G_CFG4_IB_BIAS_CTRL(0x8));
225fec53f44SColin Foster 	regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG0,
226fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_CORE_CLK_DIV(0x11) |
227fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_CPU_CLK_DIV(2) |
228fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_BIAS |
229fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_VCO_BUF |
230fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_CP1 |
231fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_SELCPI(2) |
232fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_LOOP_BW_RES(0xe) |
233fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_SELBGV820(4) |
234fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_DIV4 |
235fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_CLKTREE |
236fec53f44SColin Foster 		     HSIO_PLL5G_CFG0_ENA_LANE);
237fec53f44SColin Foster 	regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG2,
238fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_EN_RESET_FRQ_DET |
239fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_EN_RESET_OVERRUN |
240fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_GAIN_TEST(0x8) |
241fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_ENA_AMPCTRL |
242fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_PWD_AMPCTRL_N |
243fec53f44SColin Foster 		     HSIO_PLL5G_CFG2_AMPC_SEL(0x10));
244fec53f44SColin Foster }
245fec53f44SColin Foster EXPORT_SYMBOL(ocelot_pll5_init);
246fec53f44SColin Foster 
247f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
248b5962294SHoratiu Vultur {
249b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
250b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
251f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
25275944fdaSXiaoliang Yang 
25375944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
25475944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
2552f17c050SXiaoliang Yang 
2562f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
2572f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
2582f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
259b5962294SHoratiu Vultur }
260b5962294SHoratiu Vultur 
26154c31984SVladimir Oltean static int ocelot_single_vlan_aware_bridge(struct ocelot *ocelot,
26254c31984SVladimir Oltean 					   struct netlink_ext_ack *extack)
26354c31984SVladimir Oltean {
26454c31984SVladimir Oltean 	struct net_device *bridge = NULL;
26554c31984SVladimir Oltean 	int port;
26654c31984SVladimir Oltean 
26754c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
26854c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
26954c31984SVladimir Oltean 
27054c31984SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bridge ||
27154c31984SVladimir Oltean 		    !br_vlan_enabled(ocelot_port->bridge))
27254c31984SVladimir Oltean 			continue;
27354c31984SVladimir Oltean 
27454c31984SVladimir Oltean 		if (!bridge) {
27554c31984SVladimir Oltean 			bridge = ocelot_port->bridge;
27654c31984SVladimir Oltean 			continue;
27754c31984SVladimir Oltean 		}
27854c31984SVladimir Oltean 
27954c31984SVladimir Oltean 		if (bridge == ocelot_port->bridge)
28054c31984SVladimir Oltean 			continue;
28154c31984SVladimir Oltean 
28254c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
28354c31984SVladimir Oltean 				   "Only one VLAN-aware bridge is supported");
28454c31984SVladimir Oltean 		return -EBUSY;
28554c31984SVladimir Oltean 	}
28654c31984SVladimir Oltean 
28754c31984SVladimir Oltean 	return 0;
28854c31984SVladimir Oltean }
28954c31984SVladimir Oltean 
290639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
291639c1b26SSteen Hegelund {
292639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
293639c1b26SSteen Hegelund }
294639c1b26SSteen Hegelund 
295a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
296a556c76aSAlexandre Belloni {
297639c1b26SSteen Hegelund 	u32 val;
298a556c76aSAlexandre Belloni 
299639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
300639c1b26SSteen Hegelund 		ocelot,
301639c1b26SSteen Hegelund 		val,
302639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
303639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
304639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
305a556c76aSAlexandre Belloni }
306a556c76aSAlexandre Belloni 
3077142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
3087142529fSAntoine Tenart {
3097142529fSAntoine Tenart 	/* Select the VID to configure */
3107142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
3117142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
3127142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
3137142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
3147142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
3157142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
3167142529fSAntoine Tenart 
3177142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
3187142529fSAntoine Tenart }
3197142529fSAntoine Tenart 
3200da1a1c4SVladimir Oltean static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
3210da1a1c4SVladimir Oltean {
3220da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3230da1a1c4SVladimir Oltean 	int num_untagged = 0;
3240da1a1c4SVladimir Oltean 
3250da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
3260da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
3270da1a1c4SVladimir Oltean 			continue;
3280da1a1c4SVladimir Oltean 
329276d37ebSVladimir Oltean 		/* Ignore the VLAN added by ocelot_add_vlan_unaware_pvid(),
330276d37ebSVladimir Oltean 		 * because this is never active in hardware at the same time as
331276d37ebSVladimir Oltean 		 * the bridge VLANs, which only matter in VLAN-aware mode.
332276d37ebSVladimir Oltean 		 */
333276d37ebSVladimir Oltean 		if (vlan->vid >= OCELOT_RSV_VLAN_RANGE_START)
334276d37ebSVladimir Oltean 			continue;
335276d37ebSVladimir Oltean 
3360da1a1c4SVladimir Oltean 		if (vlan->untagged & BIT(port))
3370da1a1c4SVladimir Oltean 			num_untagged++;
3380da1a1c4SVladimir Oltean 	}
3390da1a1c4SVladimir Oltean 
3400da1a1c4SVladimir Oltean 	return num_untagged;
3410da1a1c4SVladimir Oltean }
3420da1a1c4SVladimir Oltean 
3430da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
3440da1a1c4SVladimir Oltean {
3450da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3460da1a1c4SVladimir Oltean 	int num_tagged = 0;
3470da1a1c4SVladimir Oltean 
3480da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
3490da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
3500da1a1c4SVladimir Oltean 			continue;
3510da1a1c4SVladimir Oltean 
3520da1a1c4SVladimir Oltean 		if (!(vlan->untagged & BIT(port)))
3530da1a1c4SVladimir Oltean 			num_tagged++;
3540da1a1c4SVladimir Oltean 	}
3550da1a1c4SVladimir Oltean 
3560da1a1c4SVladimir Oltean 	return num_tagged;
3570da1a1c4SVladimir Oltean }
3580da1a1c4SVladimir Oltean 
3590da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
3600da1a1c4SVladimir Oltean  * _one_ egress-untagged VLAN (_the_ native VLAN)
3610da1a1c4SVladimir Oltean  */
3620da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
3630da1a1c4SVladimir Oltean {
3640da1a1c4SVladimir Oltean 	return ocelot_port_num_tagged_vlans(ocelot, port) &&
3650da1a1c4SVladimir Oltean 	       ocelot_port_num_untagged_vlans(ocelot, port) == 1;
3660da1a1c4SVladimir Oltean }
3670da1a1c4SVladimir Oltean 
3680da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan *
3690da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
3700da1a1c4SVladimir Oltean {
3710da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3720da1a1c4SVladimir Oltean 
3730da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
3740da1a1c4SVladimir Oltean 		if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
3750da1a1c4SVladimir Oltean 			return vlan;
3760da1a1c4SVladimir Oltean 
3770da1a1c4SVladimir Oltean 	return NULL;
3780da1a1c4SVladimir Oltean }
3790da1a1c4SVladimir Oltean 
3800da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
3810da1a1c4SVladimir Oltean  * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
3820da1a1c4SVladimir Oltean  * state of the port.
3830da1a1c4SVladimir Oltean  */
3840da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
38597bb69e1SVladimir Oltean {
38697bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
38762a22bcbSVladimir Oltean 	enum ocelot_port_tag_config tag_cfg;
3880da1a1c4SVladimir Oltean 	bool uses_native_vlan = false;
38997bb69e1SVladimir Oltean 
39087b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
3910da1a1c4SVladimir Oltean 		uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
3920da1a1c4SVladimir Oltean 
3930da1a1c4SVladimir Oltean 		if (uses_native_vlan)
39462a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_NATIVE;
3950da1a1c4SVladimir Oltean 		else if (ocelot_port_num_untagged_vlans(ocelot, port))
3960da1a1c4SVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_DISABLED;
39787b0f983SVladimir Oltean 		else
39862a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_TRUNK;
39987b0f983SVladimir Oltean 	} else {
40062a22bcbSVladimir Oltean 		tag_cfg = OCELOT_PORT_TAG_DISABLED;
40187b0f983SVladimir Oltean 	}
4020da1a1c4SVladimir Oltean 
40362a22bcbSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
40487b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
40587b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
4060da1a1c4SVladimir Oltean 
4070da1a1c4SVladimir Oltean 	if (uses_native_vlan) {
4080da1a1c4SVladimir Oltean 		struct ocelot_bridge_vlan *native_vlan;
4090da1a1c4SVladimir Oltean 
4100da1a1c4SVladimir Oltean 		/* Not having a native VLAN is impossible, because
4110da1a1c4SVladimir Oltean 		 * ocelot_port_num_untagged_vlans has returned 1.
4120da1a1c4SVladimir Oltean 		 * So there is no use in checking for NULL here.
4130da1a1c4SVladimir Oltean 		 */
4140da1a1c4SVladimir Oltean 		native_vlan = ocelot_port_find_native_vlan(ocelot, port);
4150da1a1c4SVladimir Oltean 
4160da1a1c4SVladimir Oltean 		ocelot_rmw_gix(ocelot,
4170da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
4180da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID_M,
4190da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG, port);
4200da1a1c4SVladimir Oltean 	}
42197bb69e1SVladimir Oltean }
42297bb69e1SVladimir Oltean 
42354c31984SVladimir Oltean int ocelot_bridge_num_find(struct ocelot *ocelot,
42454c31984SVladimir Oltean 			   const struct net_device *bridge)
42554c31984SVladimir Oltean {
42654c31984SVladimir Oltean 	int port;
42754c31984SVladimir Oltean 
42854c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
42954c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
43054c31984SVladimir Oltean 
43154c31984SVladimir Oltean 		if (ocelot_port && ocelot_port->bridge == bridge)
43254c31984SVladimir Oltean 			return ocelot_port->bridge_num;
43354c31984SVladimir Oltean 	}
43454c31984SVladimir Oltean 
43554c31984SVladimir Oltean 	return -1;
43654c31984SVladimir Oltean }
43754c31984SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bridge_num_find);
43854c31984SVladimir Oltean 
43954c31984SVladimir Oltean static u16 ocelot_vlan_unaware_pvid(struct ocelot *ocelot,
44054c31984SVladimir Oltean 				    const struct net_device *bridge)
44154c31984SVladimir Oltean {
44254c31984SVladimir Oltean 	int bridge_num;
44354c31984SVladimir Oltean 
44454c31984SVladimir Oltean 	/* Standalone ports use VID 0 */
44554c31984SVladimir Oltean 	if (!bridge)
44654c31984SVladimir Oltean 		return 0;
44754c31984SVladimir Oltean 
44854c31984SVladimir Oltean 	bridge_num = ocelot_bridge_num_find(ocelot, bridge);
44954c31984SVladimir Oltean 	if (WARN_ON(bridge_num < 0))
45054c31984SVladimir Oltean 		return 0;
45154c31984SVladimir Oltean 
45254c31984SVladimir Oltean 	/* VLAN-unaware bridges use a reserved VID going from 4095 downwards */
45354c31984SVladimir Oltean 	return VLAN_N_VID - bridge_num - 1;
45454c31984SVladimir Oltean }
45554c31984SVladimir Oltean 
45675e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
457c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
458d4004422SVladimir Oltean 				 const struct ocelot_bridge_vlan *pvid_vlan)
45975e5a554SVladimir Oltean {
46075e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
46154c31984SVladimir Oltean 	u16 pvid = ocelot_vlan_unaware_pvid(ocelot, ocelot_port->bridge);
462be0576feSVladimir Oltean 	u32 val = 0;
46375e5a554SVladimir Oltean 
464c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
46575e5a554SVladimir Oltean 
466d4004422SVladimir Oltean 	if (ocelot_port->vlan_aware && pvid_vlan)
467d4004422SVladimir Oltean 		pvid = pvid_vlan->vid;
46875e5a554SVladimir Oltean 
46975e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
470d4004422SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
47175e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
47275e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
473be0576feSVladimir Oltean 
474be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
475be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
476be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
477be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
478be0576feSVladimir Oltean 	 */
479d4004422SVladimir Oltean 	if (!pvid_vlan && ocelot_port->vlan_aware)
480be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
481be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
482be0576feSVladimir Oltean 
483be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
484be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
485be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
486be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
48775e5a554SVladimir Oltean }
48875e5a554SVladimir Oltean 
48990e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
49090e0aa8dSVladimir Oltean 							  u16 vid)
491bbf6a2d9SVladimir Oltean {
49290e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
493bbf6a2d9SVladimir Oltean 
49490e0aa8dSVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
49590e0aa8dSVladimir Oltean 		if (vlan->vid == vid)
49690e0aa8dSVladimir Oltean 			return vlan;
497bbf6a2d9SVladimir Oltean 
49890e0aa8dSVladimir Oltean 	return NULL;
499bbf6a2d9SVladimir Oltean }
500bbf6a2d9SVladimir Oltean 
5010da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
5020da1a1c4SVladimir Oltean 				  bool untagged)
503bbf6a2d9SVladimir Oltean {
50490e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
50590e0aa8dSVladimir Oltean 	unsigned long portmask;
50690e0aa8dSVladimir Oltean 	int err;
50790e0aa8dSVladimir Oltean 
50890e0aa8dSVladimir Oltean 	if (vlan) {
50990e0aa8dSVladimir Oltean 		portmask = vlan->portmask | BIT(port);
51090e0aa8dSVladimir Oltean 
51190e0aa8dSVladimir Oltean 		err = ocelot_vlant_set_mask(ocelot, vid, portmask);
51290e0aa8dSVladimir Oltean 		if (err)
51390e0aa8dSVladimir Oltean 			return err;
51490e0aa8dSVladimir Oltean 
51590e0aa8dSVladimir Oltean 		vlan->portmask = portmask;
5160da1a1c4SVladimir Oltean 		/* Bridge VLANs can be overwritten with a different
5170da1a1c4SVladimir Oltean 		 * egress-tagging setting, so make sure to override an untagged
5180da1a1c4SVladimir Oltean 		 * with a tagged VID if that's going on.
5190da1a1c4SVladimir Oltean 		 */
5200da1a1c4SVladimir Oltean 		if (untagged)
5210da1a1c4SVladimir Oltean 			vlan->untagged |= BIT(port);
5220da1a1c4SVladimir Oltean 		else
5230da1a1c4SVladimir Oltean 			vlan->untagged &= ~BIT(port);
52490e0aa8dSVladimir Oltean 
52590e0aa8dSVladimir Oltean 		return 0;
52690e0aa8dSVladimir Oltean 	}
52790e0aa8dSVladimir Oltean 
52890e0aa8dSVladimir Oltean 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
52990e0aa8dSVladimir Oltean 	if (!vlan)
53090e0aa8dSVladimir Oltean 		return -ENOMEM;
53190e0aa8dSVladimir Oltean 
53290e0aa8dSVladimir Oltean 	portmask = BIT(port);
53390e0aa8dSVladimir Oltean 
53490e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
53590e0aa8dSVladimir Oltean 	if (err) {
53690e0aa8dSVladimir Oltean 		kfree(vlan);
53790e0aa8dSVladimir Oltean 		return err;
53890e0aa8dSVladimir Oltean 	}
53990e0aa8dSVladimir Oltean 
54090e0aa8dSVladimir Oltean 	vlan->vid = vid;
54190e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
5420da1a1c4SVladimir Oltean 	if (untagged)
5430da1a1c4SVladimir Oltean 		vlan->untagged = BIT(port);
54490e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&vlan->list);
54590e0aa8dSVladimir Oltean 	list_add_tail(&vlan->list, &ocelot->vlans);
54690e0aa8dSVladimir Oltean 
54790e0aa8dSVladimir Oltean 	return 0;
548bbf6a2d9SVladimir Oltean }
549bbf6a2d9SVladimir Oltean 
550bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
551bbf6a2d9SVladimir Oltean {
55290e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
55390e0aa8dSVladimir Oltean 	unsigned long portmask;
55490e0aa8dSVladimir Oltean 	int err;
55590e0aa8dSVladimir Oltean 
55690e0aa8dSVladimir Oltean 	if (!vlan)
55790e0aa8dSVladimir Oltean 		return 0;
55890e0aa8dSVladimir Oltean 
55990e0aa8dSVladimir Oltean 	portmask = vlan->portmask & ~BIT(port);
56090e0aa8dSVladimir Oltean 
56190e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
56290e0aa8dSVladimir Oltean 	if (err)
56390e0aa8dSVladimir Oltean 		return err;
56490e0aa8dSVladimir Oltean 
56590e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
56690e0aa8dSVladimir Oltean 	if (vlan->portmask)
56790e0aa8dSVladimir Oltean 		return 0;
56890e0aa8dSVladimir Oltean 
56990e0aa8dSVladimir Oltean 	list_del(&vlan->list);
57090e0aa8dSVladimir Oltean 	kfree(vlan);
57190e0aa8dSVladimir Oltean 
57290e0aa8dSVladimir Oltean 	return 0;
573bbf6a2d9SVladimir Oltean }
574bbf6a2d9SVladimir Oltean 
57554c31984SVladimir Oltean static int ocelot_add_vlan_unaware_pvid(struct ocelot *ocelot, int port,
57654c31984SVladimir Oltean 					const struct net_device *bridge)
57754c31984SVladimir Oltean {
57854c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
57954c31984SVladimir Oltean 
58054c31984SVladimir Oltean 	return ocelot_vlan_member_add(ocelot, port, vid, true);
58154c31984SVladimir Oltean }
58254c31984SVladimir Oltean 
58354c31984SVladimir Oltean static int ocelot_del_vlan_unaware_pvid(struct ocelot *ocelot, int port,
58454c31984SVladimir Oltean 					const struct net_device *bridge)
58554c31984SVladimir Oltean {
58654c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
58754c31984SVladimir Oltean 
58854c31984SVladimir Oltean 	return ocelot_vlan_member_del(ocelot, port, vid);
58954c31984SVladimir Oltean }
59054c31984SVladimir Oltean 
5912e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
5923b95d1b2SVladimir Oltean 			       bool vlan_aware, struct netlink_ext_ack *extack)
59387b0f983SVladimir Oltean {
59470edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
595bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
59670edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
5971fcb8fb3SVladimir Oltean 	int err = 0;
598bae33f2bSVladimir Oltean 	u32 val;
59970edfae1SVladimir Oltean 
60070edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
60170edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
60270edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
6033b95d1b2SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6043b95d1b2SVladimir Oltean 					   "Cannot change VLAN state with vlan modify rules active");
60570edfae1SVladimir Oltean 			return -EBUSY;
60670edfae1SVladimir Oltean 		}
60770edfae1SVladimir Oltean 	}
60870edfae1SVladimir Oltean 
60954c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
61054c31984SVladimir Oltean 	if (err)
61154c31984SVladimir Oltean 		return err;
61254c31984SVladimir Oltean 
61354c31984SVladimir Oltean 	if (vlan_aware)
61454c31984SVladimir Oltean 		err = ocelot_del_vlan_unaware_pvid(ocelot, port,
61554c31984SVladimir Oltean 						   ocelot_port->bridge);
6161fcb8fb3SVladimir Oltean 	else if (ocelot_port->bridge)
61754c31984SVladimir Oltean 		err = ocelot_add_vlan_unaware_pvid(ocelot, port,
61854c31984SVladimir Oltean 						   ocelot_port->bridge);
61954c31984SVladimir Oltean 	if (err)
62054c31984SVladimir Oltean 		return err;
62154c31984SVladimir Oltean 
62287b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
62387b0f983SVladimir Oltean 
62487b0f983SVladimir Oltean 	if (vlan_aware)
62587b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
62687b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
62787b0f983SVladimir Oltean 	else
62887b0f983SVladimir Oltean 		val = 0;
62987b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
63087b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
63187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
63287b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
63387b0f983SVladimir Oltean 
634c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
6350da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6362e554a7aSVladimir Oltean 
6372e554a7aSVladimir Oltean 	return 0;
63887b0f983SVladimir Oltean }
63987b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
64087b0f983SVladimir Oltean 
6412f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
64201af940eSVladimir Oltean 			bool untagged, struct netlink_ext_ack *extack)
6432f0402feSVladimir Oltean {
6440da1a1c4SVladimir Oltean 	if (untagged) {
6450da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6460da1a1c4SVladimir Oltean 		if (ocelot_port_uses_native_vlan(ocelot, port)) {
64701af940eSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6480da1a1c4SVladimir Oltean 					   "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
6492f0402feSVladimir Oltean 			return -EBUSY;
6502f0402feSVladimir Oltean 		}
6510da1a1c4SVladimir Oltean 	} else {
6520da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6530da1a1c4SVladimir Oltean 		if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
6540da1a1c4SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6550da1a1c4SVladimir Oltean 					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
6560da1a1c4SVladimir Oltean 			return -EBUSY;
6570da1a1c4SVladimir Oltean 		}
6580da1a1c4SVladimir Oltean 	}
6592f0402feSVladimir Oltean 
66054c31984SVladimir Oltean 	if (vid > OCELOT_RSV_VLAN_RANGE_START) {
66154c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
66254c31984SVladimir Oltean 				   "VLAN range 4000-4095 reserved for VLAN-unaware bridging");
66354c31984SVladimir Oltean 		return -EBUSY;
66454c31984SVladimir Oltean 	}
66554c31984SVladimir Oltean 
6662f0402feSVladimir Oltean 	return 0;
6672f0402feSVladimir Oltean }
6682f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
6692f0402feSVladimir Oltean 
6705e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
6717142529fSAntoine Tenart 		    bool untagged)
6727142529fSAntoine Tenart {
673bbf6a2d9SVladimir Oltean 	int err;
6747142529fSAntoine Tenart 
6759323ac36SVladimir Oltean 	/* Ignore VID 0 added to our RX filter by the 8021q module, since
6769323ac36SVladimir Oltean 	 * that collides with OCELOT_STANDALONE_PVID and changes it from
6779323ac36SVladimir Oltean 	 * egress-untagged to egress-tagged.
6789323ac36SVladimir Oltean 	 */
6799323ac36SVladimir Oltean 	if (!vid)
6809323ac36SVladimir Oltean 		return 0;
6819323ac36SVladimir Oltean 
6820da1a1c4SVladimir Oltean 	err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
683bbf6a2d9SVladimir Oltean 	if (err)
684bbf6a2d9SVladimir Oltean 		return err;
6857142529fSAntoine Tenart 
6867142529fSAntoine Tenart 	/* Default ingress vlan classification */
687d4004422SVladimir Oltean 	if (pvid)
688d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port,
689d4004422SVladimir Oltean 				     ocelot_bridge_vlan_find(ocelot, vid));
6907142529fSAntoine Tenart 
6917142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
6920da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6937142529fSAntoine Tenart 
6947142529fSAntoine Tenart 	return 0;
6957142529fSAntoine Tenart }
6965e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
6977142529fSAntoine Tenart 
6985e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
6999855934cSVladimir Oltean {
7009855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
701ef576405SVladimir Oltean 	bool del_pvid = false;
702bbf6a2d9SVladimir Oltean 	int err;
7037142529fSAntoine Tenart 
7049323ac36SVladimir Oltean 	if (!vid)
7059323ac36SVladimir Oltean 		return 0;
7069323ac36SVladimir Oltean 
707ef576405SVladimir Oltean 	if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
708ef576405SVladimir Oltean 		del_pvid = true;
709ef576405SVladimir Oltean 
710bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
711bbf6a2d9SVladimir Oltean 	if (err)
712bbf6a2d9SVladimir Oltean 		return err;
7137142529fSAntoine Tenart 
714be0576feSVladimir Oltean 	/* Ingress */
715ef576405SVladimir Oltean 	if (del_pvid)
716d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, NULL);
717be0576feSVladimir Oltean 
7187142529fSAntoine Tenart 	/* Egress */
7190da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
7207142529fSAntoine Tenart 
7217142529fSAntoine Tenart 	return 0;
7227142529fSAntoine Tenart }
7235e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
7247142529fSAntoine Tenart 
725a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
726a556c76aSAlexandre Belloni {
727bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
7287142529fSAntoine Tenart 	u16 port, vid;
7297142529fSAntoine Tenart 
730a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
731a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
732a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
733a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
7347142529fSAntoine Tenart 
7357142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
736bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
73790e0aa8dSVladimir Oltean 		ocelot_vlant_set_mask(ocelot, vid, 0);
7387142529fSAntoine Tenart 
73954c31984SVladimir Oltean 	/* We need VID 0 to get traffic on standalone ports.
74054c31984SVladimir Oltean 	 * It is added automatically if the 8021q module is loaded, but we
74154c31984SVladimir Oltean 	 * can't rely on that since it might not be.
7427142529fSAntoine Tenart 	 */
74354c31984SVladimir Oltean 	ocelot_vlant_set_mask(ocelot, OCELOT_STANDALONE_PVID, all_ports);
7447142529fSAntoine Tenart 
7457142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
7467142529fSAntoine Tenart 	 * default.
7477142529fSAntoine Tenart 	 */
748bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
7497142529fSAntoine Tenart 
7507142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
7517142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
7527142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
7537142529fSAntoine Tenart 	}
754a556c76aSAlexandre Belloni }
755a556c76aSAlexandre Belloni 
756eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
757eb4733d7SVladimir Oltean {
758eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
759eb4733d7SVladimir Oltean }
760eb4733d7SVladimir Oltean 
761e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
762eb4733d7SVladimir Oltean {
7631650bdb1SVladimir Oltean 	unsigned int pause_ena;
764eb4733d7SVladimir Oltean 	int err, val;
765eb4733d7SVladimir Oltean 
766eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
767eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
768eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
769eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
770eb4733d7SVladimir Oltean 
771eb4733d7SVladimir Oltean 	/* Disable flow control */
7721650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
773eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
774eb4733d7SVladimir Oltean 
775eb4733d7SVladimir Oltean 	/* Disable priority flow control */
776eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
777eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
778eb4733d7SVladimir Oltean 
779eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
780eb4733d7SVladimir Oltean 	 * at the port.
781eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
782eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
783eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
784eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
785eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
786eb4733d7SVladimir Oltean 	 */
787eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
788eb4733d7SVladimir Oltean 
789eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
790eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
791eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
792eb4733d7SVladimir Oltean 
793eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
794eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
795eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
796eb4733d7SVladimir Oltean 
797eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
798eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
799eb4733d7SVladimir Oltean 		       port);
800eb4733d7SVladimir Oltean 
801eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
802eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
803eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
804eb4733d7SVladimir Oltean 
805eb4733d7SVladimir Oltean 	/* Clear flushing again. */
806eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
807eb4733d7SVladimir Oltean 
8081650bdb1SVladimir Oltean 	/* Re-enable flow control */
8091650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
8101650bdb1SVladimir Oltean 
811eb4733d7SVladimir Oltean 	return err;
812eb4733d7SVladimir Oltean }
813eb4733d7SVladimir Oltean 
814dfca93edSColin Foster int ocelot_port_configure_serdes(struct ocelot *ocelot, int port,
815dfca93edSColin Foster 				 struct device_node *portnp)
816dfca93edSColin Foster {
817dfca93edSColin Foster 	struct ocelot_port *ocelot_port = ocelot->ports[port];
818dfca93edSColin Foster 	struct device *dev = ocelot->dev;
819dfca93edSColin Foster 	int err;
820dfca93edSColin Foster 
821dfca93edSColin Foster 	/* Ensure clock signals and speed are set on all QSGMII links */
822dfca93edSColin Foster 	if (ocelot_port->phy_mode == PHY_INTERFACE_MODE_QSGMII)
823dfca93edSColin Foster 		ocelot_port_rmwl(ocelot_port, 0,
824dfca93edSColin Foster 				 DEV_CLOCK_CFG_MAC_TX_RST |
825dfca93edSColin Foster 				 DEV_CLOCK_CFG_MAC_RX_RST,
826dfca93edSColin Foster 				 DEV_CLOCK_CFG);
827dfca93edSColin Foster 
828dfca93edSColin Foster 	if (ocelot_port->phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
829dfca93edSColin Foster 		struct phy *serdes = of_phy_get(portnp, NULL);
830dfca93edSColin Foster 
831dfca93edSColin Foster 		if (IS_ERR(serdes)) {
832dfca93edSColin Foster 			err = PTR_ERR(serdes);
833dfca93edSColin Foster 			dev_err_probe(dev, err,
834dfca93edSColin Foster 				      "missing SerDes phys for port %d\n",
835dfca93edSColin Foster 				      port);
836dfca93edSColin Foster 			return err;
837dfca93edSColin Foster 		}
838dfca93edSColin Foster 
839dfca93edSColin Foster 		err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET,
840dfca93edSColin Foster 				       ocelot_port->phy_mode);
841dfca93edSColin Foster 		of_phy_put(serdes);
842dfca93edSColin Foster 		if (err) {
843dfca93edSColin Foster 			dev_err(dev, "Could not SerDes mode on port %d: %pe\n",
844dfca93edSColin Foster 				port, ERR_PTR(err));
845dfca93edSColin Foster 			return err;
846dfca93edSColin Foster 		}
847dfca93edSColin Foster 	}
848dfca93edSColin Foster 
849dfca93edSColin Foster 	return 0;
850dfca93edSColin Foster }
851dfca93edSColin Foster EXPORT_SYMBOL_GPL(ocelot_port_configure_serdes);
852dfca93edSColin Foster 
85369f7f89cSColin Foster void ocelot_phylink_mac_config(struct ocelot *ocelot, int port,
85469f7f89cSColin Foster 			       unsigned int link_an_mode,
85569f7f89cSColin Foster 			       const struct phylink_link_state *state)
85669f7f89cSColin Foster {
85769f7f89cSColin Foster 	struct ocelot_port *ocelot_port = ocelot->ports[port];
85869f7f89cSColin Foster 
85969f7f89cSColin Foster 	/* Disable HDX fast control */
86069f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
86169f7f89cSColin Foster 			   DEV_PORT_MISC);
86269f7f89cSColin Foster 
86369f7f89cSColin Foster 	/* SGMII only for now */
86469f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
86569f7f89cSColin Foster 			   PCS1G_MODE_CFG);
86669f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
86769f7f89cSColin Foster 
86869f7f89cSColin Foster 	/* Enable PCS */
86969f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
87069f7f89cSColin Foster 
87169f7f89cSColin Foster 	/* No aneg on SGMII */
87269f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
87369f7f89cSColin Foster 
87469f7f89cSColin Foster 	/* No loopback */
87569f7f89cSColin Foster 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
87669f7f89cSColin Foster }
87769f7f89cSColin Foster EXPORT_SYMBOL_GPL(ocelot_phylink_mac_config);
87869f7f89cSColin Foster 
879e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
880e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
881e6e12df6SVladimir Oltean 				  phy_interface_t interface,
882e6e12df6SVladimir Oltean 				  unsigned long quirks)
883a556c76aSAlexandre Belloni {
88426f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
885e6e12df6SVladimir Oltean 	int err;
886a556c76aSAlexandre Belloni 
8878abe1970SVladimir Oltean 	ocelot_port->speed = SPEED_UNKNOWN;
8888abe1970SVladimir Oltean 
889e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
890e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
891e6e12df6SVladimir Oltean 
8928abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
8938abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
8948abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
8958abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
8968abe1970SVladimir Oltean 	}
8978abe1970SVladimir Oltean 
898e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
899e6e12df6SVladimir Oltean 
900e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
901e6e12df6SVladimir Oltean 	if (err)
902e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
903e6e12df6SVladimir Oltean 			port, err);
904e6e12df6SVladimir Oltean 
905e6e12df6SVladimir Oltean 	/* Put the port in reset. */
906e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
907e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
908e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
909e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
91074a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
911e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
91274a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
913e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
914e6e12df6SVladimir Oltean }
915e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
916e6e12df6SVladimir Oltean 
917e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
918e6e12df6SVladimir Oltean 				struct phy_device *phydev,
919e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
920e6e12df6SVladimir Oltean 				phy_interface_t interface,
921e6e12df6SVladimir Oltean 				int speed, int duplex,
922e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
923e6e12df6SVladimir Oltean 				unsigned long quirks)
924e6e12df6SVladimir Oltean {
925e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
926e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
927e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
928e6e12df6SVladimir Oltean 
9298abe1970SVladimir Oltean 	ocelot_port->speed = speed;
9308abe1970SVladimir Oltean 
931e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
932e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
933e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
934e6e12df6SVladimir Oltean 	 * (which is also its default value).
935e6e12df6SVladimir Oltean 	 */
936e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
937e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
938e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
939e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
940e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
941e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
942e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
943e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
944e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
945e6e12df6SVladimir Oltean 	} else {
946e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
947e6e12df6SVladimir Oltean 	}
948e6e12df6SVladimir Oltean 
949e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
950e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
951e6e12df6SVladimir Oltean 
952e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
953e6e12df6SVladimir Oltean 
954e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
955e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
956e6e12df6SVladimir Oltean 	 */
957e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
958e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
959e6e12df6SVladimir Oltean 
960e6e12df6SVladimir Oltean 	switch (speed) {
961a556c76aSAlexandre Belloni 	case SPEED_10:
962e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
963a556c76aSAlexandre Belloni 		break;
964a556c76aSAlexandre Belloni 	case SPEED_100:
965e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
966a556c76aSAlexandre Belloni 		break;
967a556c76aSAlexandre Belloni 	case SPEED_1000:
968a556c76aSAlexandre Belloni 	case SPEED_2500:
969e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
970a556c76aSAlexandre Belloni 		break;
971a556c76aSAlexandre Belloni 	default:
972e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
973e6e12df6SVladimir Oltean 			port, speed);
974a556c76aSAlexandre Belloni 		return;
975a556c76aSAlexandre Belloni 	}
976a556c76aSAlexandre Belloni 
977de8586edSVladimir Oltean 	if (rx_pause)
978e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
979a556c76aSAlexandre Belloni 
980e6e12df6SVladimir Oltean 	if (tx_pause)
981e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
982e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
983e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
984e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
985a556c76aSAlexandre Belloni 
986e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
987e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
988e6e12df6SVladimir Oltean 	 */
989e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
990a556c76aSAlexandre Belloni 
991e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
9921ba8f656SVladimir Oltean 
99333cb0ff3SVladimir Oltean 	/* Don't attempt to send PAUSE frames on the NPI port, it's broken */
99433cb0ff3SVladimir Oltean 	if (port != ocelot->npi)
99533cb0ff3SVladimir Oltean 		ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
99633cb0ff3SVladimir Oltean 				    tx_pause);
9971ba8f656SVladimir Oltean 
998e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
999e6e12df6SVladimir Oltean 	 * enable MAC module
1000e6e12df6SVladimir Oltean 	 */
1001004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
1002a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
1003a556c76aSAlexandre Belloni 
10048abe1970SVladimir Oltean 	/* If the port supports cut-through forwarding, update the masks before
10058abe1970SVladimir Oltean 	 * enabling forwarding on the port.
10068abe1970SVladimir Oltean 	 */
10078abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
10088abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
10098abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
10108abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
10118abe1970SVladimir Oltean 	}
10128abe1970SVladimir Oltean 
1013a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
1014886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
1015886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
1016a556c76aSAlexandre Belloni }
1017e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
1018889b8950SVladimir Oltean 
1019924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
1020924ee317SVladimir Oltean 				u32 *rval)
1021924ee317SVladimir Oltean {
1022924ee317SVladimir Oltean 	u32 bytes_valid, val;
1023924ee317SVladimir Oltean 
1024924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1025924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
1026924ee317SVladimir Oltean 		if (ifh)
1027924ee317SVladimir Oltean 			return -EIO;
1028924ee317SVladimir Oltean 
1029924ee317SVladimir Oltean 		do {
1030924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1031924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
1032924ee317SVladimir Oltean 	}
1033924ee317SVladimir Oltean 
1034924ee317SVladimir Oltean 	switch (val) {
1035924ee317SVladimir Oltean 	case XTR_ABORT:
1036924ee317SVladimir Oltean 		return -EIO;
1037924ee317SVladimir Oltean 	case XTR_EOF_0:
1038924ee317SVladimir Oltean 	case XTR_EOF_1:
1039924ee317SVladimir Oltean 	case XTR_EOF_2:
1040924ee317SVladimir Oltean 	case XTR_EOF_3:
1041924ee317SVladimir Oltean 	case XTR_PRUNED:
1042924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
1043924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1044924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
1045924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1046924ee317SVladimir Oltean 		else
1047924ee317SVladimir Oltean 			*rval = val;
1048924ee317SVladimir Oltean 
1049924ee317SVladimir Oltean 		return bytes_valid;
1050924ee317SVladimir Oltean 	case XTR_ESCAPE:
1051924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1052924ee317SVladimir Oltean 
1053924ee317SVladimir Oltean 		return 4;
1054924ee317SVladimir Oltean 	default:
1055924ee317SVladimir Oltean 		*rval = val;
1056924ee317SVladimir Oltean 
1057924ee317SVladimir Oltean 		return 4;
1058924ee317SVladimir Oltean 	}
1059924ee317SVladimir Oltean }
1060924ee317SVladimir Oltean 
1061924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
1062924ee317SVladimir Oltean {
1063924ee317SVladimir Oltean 	int i, err = 0;
1064924ee317SVladimir Oltean 
1065924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
1066924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
1067924ee317SVladimir Oltean 		if (err != 4)
1068924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
1069924ee317SVladimir Oltean 	}
1070924ee317SVladimir Oltean 
1071924ee317SVladimir Oltean 	return 0;
1072924ee317SVladimir Oltean }
1073924ee317SVladimir Oltean 
1074b471a71eSClément Léger void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
1075b471a71eSClément Léger 			     u64 timestamp)
1076924ee317SVladimir Oltean {
1077924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
10782ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
1079b471a71eSClément Léger 	struct timespec64 ts;
1080b471a71eSClément Léger 
1081b471a71eSClément Léger 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1082b471a71eSClément Léger 
1083b471a71eSClément Léger 	tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1084b471a71eSClément Léger 	if ((tod_in_ns & 0xffffffff) < timestamp)
1085b471a71eSClément Léger 		full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1086b471a71eSClément Léger 				timestamp;
1087b471a71eSClément Léger 	else
1088b471a71eSClément Léger 		full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1089b471a71eSClément Léger 				timestamp;
1090b471a71eSClément Léger 
1091b471a71eSClément Léger 	shhwtstamps = skb_hwtstamps(skb);
1092b471a71eSClément Léger 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1093b471a71eSClément Léger 	shhwtstamps->hwtstamp = full_ts_in_ns;
1094b471a71eSClément Léger }
1095b471a71eSClément Léger EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
1096b471a71eSClément Léger 
1097b471a71eSClément Léger int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
1098b471a71eSClément Léger {
1099924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
1100924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
1101924ee317SVladimir Oltean 	struct net_device *dev;
1102924ee317SVladimir Oltean 	struct sk_buff *skb;
1103924ee317SVladimir Oltean 	int sz, buf_len;
1104924ee317SVladimir Oltean 	u32 val, *buf;
1105924ee317SVladimir Oltean 	int err;
1106924ee317SVladimir Oltean 
1107924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1108924ee317SVladimir Oltean 	if (err)
1109924ee317SVladimir Oltean 		return err;
1110924ee317SVladimir Oltean 
1111924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
1112924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
1113924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1114924ee317SVladimir Oltean 
1115924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1116924ee317SVladimir Oltean 		return -EINVAL;
1117924ee317SVladimir Oltean 
1118924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1119924ee317SVladimir Oltean 	if (!dev)
1120924ee317SVladimir Oltean 		return -EINVAL;
1121924ee317SVladimir Oltean 
1122924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
1123924ee317SVladimir Oltean 	if (unlikely(!skb)) {
1124924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
1125924ee317SVladimir Oltean 		return -ENOMEM;
1126924ee317SVladimir Oltean 	}
1127924ee317SVladimir Oltean 
1128924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
1129924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
1130924ee317SVladimir Oltean 
1131924ee317SVladimir Oltean 	len = 0;
1132924ee317SVladimir Oltean 	do {
1133924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1134924ee317SVladimir Oltean 		if (sz < 0) {
1135924ee317SVladimir Oltean 			err = sz;
1136924ee317SVladimir Oltean 			goto out_free_skb;
1137924ee317SVladimir Oltean 		}
1138924ee317SVladimir Oltean 		*buf++ = val;
1139924ee317SVladimir Oltean 		len += sz;
1140924ee317SVladimir Oltean 	} while (len < buf_len);
1141924ee317SVladimir Oltean 
1142924ee317SVladimir Oltean 	/* Read the FCS */
1143924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1144924ee317SVladimir Oltean 	if (sz < 0) {
1145924ee317SVladimir Oltean 		err = sz;
1146924ee317SVladimir Oltean 		goto out_free_skb;
1147924ee317SVladimir Oltean 	}
1148924ee317SVladimir Oltean 
1149924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1150924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1151924ee317SVladimir Oltean 
1152924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1153924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1154924ee317SVladimir Oltean 		*buf = val;
1155924ee317SVladimir Oltean 	}
1156924ee317SVladimir Oltean 
1157b471a71eSClément Léger 	if (ocelot->ptp)
1158b471a71eSClément Léger 		ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
1159924ee317SVladimir Oltean 
1160924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1161924ee317SVladimir Oltean 	 * has already been forwarded.
1162924ee317SVladimir Oltean 	 */
1163df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1164924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1165924ee317SVladimir Oltean 
1166924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1167d8ea7ff3SHoratiu Vultur 
1168924ee317SVladimir Oltean 	*nskb = skb;
1169924ee317SVladimir Oltean 
1170924ee317SVladimir Oltean 	return 0;
1171924ee317SVladimir Oltean 
1172924ee317SVladimir Oltean out_free_skb:
1173924ee317SVladimir Oltean 	kfree_skb(skb);
1174924ee317SVladimir Oltean 	return err;
1175924ee317SVladimir Oltean }
1176924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1177924ee317SVladimir Oltean 
1178137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1179137ffbc4SVladimir Oltean {
1180137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1181137ffbc4SVladimir Oltean 
1182137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1183137ffbc4SVladimir Oltean 		return false;
1184137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1185137ffbc4SVladimir Oltean 		return false;
1186137ffbc4SVladimir Oltean 
1187137ffbc4SVladimir Oltean 	return true;
1188137ffbc4SVladimir Oltean }
1189137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1190137ffbc4SVladimir Oltean 
1191e5150f00SClément Léger void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag)
1192e5150f00SClément Léger {
1193e5150f00SClément Léger 	ocelot_ifh_set_bypass(ifh, 1);
1194e5150f00SClément Léger 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1195e5150f00SClément Léger 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1196e5150f00SClément Léger 	if (vlan_tag)
1197e5150f00SClément Léger 		ocelot_ifh_set_vlan_tci(ifh, vlan_tag);
1198e5150f00SClément Léger 	if (rew_op)
1199e5150f00SClément Léger 		ocelot_ifh_set_rew_op(ifh, rew_op);
1200e5150f00SClément Léger }
1201e5150f00SClément Léger EXPORT_SYMBOL(ocelot_ifh_port_set);
1202e5150f00SClément Léger 
1203137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1204137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1205137ffbc4SVladimir Oltean {
120640d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1207137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1208137ffbc4SVladimir Oltean 
1209137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1210137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1211137ffbc4SVladimir Oltean 
1212e5150f00SClément Léger 	ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
1213137ffbc4SVladimir Oltean 
1214137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
121540d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1216137ffbc4SVladimir Oltean 
1217137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1218137ffbc4SVladimir Oltean 	last = skb->len % 4;
1219137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1220137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1221137ffbc4SVladimir Oltean 
1222137ffbc4SVladimir Oltean 	/* Add padding */
1223137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1224137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1225137ffbc4SVladimir Oltean 		i++;
1226137ffbc4SVladimir Oltean 	}
1227137ffbc4SVladimir Oltean 
1228137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1229137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1230137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1231137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1232137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1233137ffbc4SVladimir Oltean 
1234137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1235137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1236137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1237137ffbc4SVladimir Oltean 
1238137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1239137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1240137ffbc4SVladimir Oltean }
1241137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1242137ffbc4SVladimir Oltean 
12430a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
12440a6f17c6SVladimir Oltean {
12450a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
12460a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
12470a6f17c6SVladimir Oltean }
12480a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
12490a6f17c6SVladimir Oltean 
125054c31984SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, const unsigned char *addr,
125154c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1252a556c76aSAlexandre Belloni {
125354c31984SVladimir Oltean 	if (!vid)
125454c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
125554c31984SVladimir Oltean 
1256e9b3ba43SVladimir Oltean 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
1257a556c76aSAlexandre Belloni }
12585e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1259a556c76aSAlexandre Belloni 
126054c31984SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, const unsigned char *addr,
126154c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1262531ee1a6SVladimir Oltean {
126354c31984SVladimir Oltean 	if (!vid)
126454c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
126554c31984SVladimir Oltean 
1266531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1267531ee1a6SVladimir Oltean }
12685e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1269531ee1a6SVladimir Oltean 
12702468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
1271531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1272a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1273a556c76aSAlexandre Belloni {
1274a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1275531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1276a556c76aSAlexandre Belloni 
1277a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1278a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1279a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1280a556c76aSAlexandre Belloni 
1281a556c76aSAlexandre Belloni 	/* Issue a read command */
1282a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1283a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1284a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1285a556c76aSAlexandre Belloni 
1286a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1287a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1288a556c76aSAlexandre Belloni 
1289a556c76aSAlexandre Belloni 	/* Read the entry flags */
1290a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1291a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1292a556c76aSAlexandre Belloni 		return -EINVAL;
1293a556c76aSAlexandre Belloni 
1294a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1295a556c76aSAlexandre Belloni 	 * do not report it.
1296a556c76aSAlexandre Belloni 	 */
1297a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1298531ee1a6SVladimir Oltean 	if (dst != port)
1299a556c76aSAlexandre Belloni 		return -EINVAL;
1300a556c76aSAlexandre Belloni 
1301a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1302a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1303a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1304a556c76aSAlexandre Belloni 
1305a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1306a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1307a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1308a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1309a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1310a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1311a556c76aSAlexandre Belloni 
1312a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1313a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1314a556c76aSAlexandre Belloni 
1315a556c76aSAlexandre Belloni 	return 0;
1316a556c76aSAlexandre Belloni }
1317a556c76aSAlexandre Belloni 
13185cad43a5SVladimir Oltean int ocelot_mact_flush(struct ocelot *ocelot, int port)
13195cad43a5SVladimir Oltean {
13205cad43a5SVladimir Oltean 	int err;
13215cad43a5SVladimir Oltean 
13225cad43a5SVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
13235cad43a5SVladimir Oltean 
13245cad43a5SVladimir Oltean 	/* Program ageing filter for a single port */
13255cad43a5SVladimir Oltean 	ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
13265cad43a5SVladimir Oltean 		     ANA_ANAGEFIL);
13275cad43a5SVladimir Oltean 
13285cad43a5SVladimir Oltean 	/* Flushing dynamic FDB entries requires two successive age scans */
13295cad43a5SVladimir Oltean 	ocelot_write(ocelot,
13305cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
13315cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
13325cad43a5SVladimir Oltean 
13335cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
13345cad43a5SVladimir Oltean 	if (err) {
13355cad43a5SVladimir Oltean 		mutex_unlock(&ocelot->mact_lock);
13365cad43a5SVladimir Oltean 		return err;
13375cad43a5SVladimir Oltean 	}
13385cad43a5SVladimir Oltean 
13395cad43a5SVladimir Oltean 	/* And second... */
13405cad43a5SVladimir Oltean 	ocelot_write(ocelot,
13415cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
13425cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
13435cad43a5SVladimir Oltean 
13445cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
13455cad43a5SVladimir Oltean 
13465cad43a5SVladimir Oltean 	/* Restore ageing filter */
13475cad43a5SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_ANAGEFIL);
13485cad43a5SVladimir Oltean 
13495cad43a5SVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
13505cad43a5SVladimir Oltean 
13515cad43a5SVladimir Oltean 	return err;
13525cad43a5SVladimir Oltean }
13535cad43a5SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_mact_flush);
13545cad43a5SVladimir Oltean 
13555e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1356531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1357a556c76aSAlexandre Belloni {
13582468346cSVladimir Oltean 	int err = 0;
1359531ee1a6SVladimir Oltean 	int i, j;
1360a556c76aSAlexandre Belloni 
13612468346cSVladimir Oltean 	/* We could take the lock just around ocelot_mact_read, but doing so
13622468346cSVladimir Oltean 	 * thousands of times in a row seems rather pointless and inefficient.
13632468346cSVladimir Oltean 	 */
13642468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
13652468346cSVladimir Oltean 
136621ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
136721ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1368a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1369531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1370531ee1a6SVladimir Oltean 			bool is_static;
1371531ee1a6SVladimir Oltean 
13722468346cSVladimir Oltean 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1373a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1374a556c76aSAlexandre Belloni 			 * skip it.
1375a556c76aSAlexandre Belloni 			 */
13762468346cSVladimir Oltean 			if (err == -EINVAL)
1377a556c76aSAlexandre Belloni 				continue;
13782468346cSVladimir Oltean 			else if (err)
13792468346cSVladimir Oltean 				break;
1380a556c76aSAlexandre Belloni 
1381531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1382531ee1a6SVladimir Oltean 
138354c31984SVladimir Oltean 			/* Hide the reserved VLANs used for
138454c31984SVladimir Oltean 			 * VLAN-unaware bridging.
138554c31984SVladimir Oltean 			 */
138654c31984SVladimir Oltean 			if (entry.vid > OCELOT_RSV_VLAN_RANGE_START)
138754c31984SVladimir Oltean 				entry.vid = 0;
138854c31984SVladimir Oltean 
13892468346cSVladimir Oltean 			err = cb(entry.mac, entry.vid, is_static, data);
13902468346cSVladimir Oltean 			if (err)
13912468346cSVladimir Oltean 				break;
1392a556c76aSAlexandre Belloni 		}
1393a556c76aSAlexandre Belloni 	}
1394a556c76aSAlexandre Belloni 
13952468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
13962468346cSVladimir Oltean 
13972468346cSVladimir Oltean 	return err;
1398531ee1a6SVladimir Oltean }
13995e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1400531ee1a6SVladimir Oltean 
14019d75b881SVladimir Oltean int ocelot_trap_add(struct ocelot *ocelot, int port,
14029d75b881SVladimir Oltean 		    unsigned long cookie, bool take_ts,
140396ca08c0SVladimir Oltean 		    void (*populate)(struct ocelot_vcap_filter *f))
140496ca08c0SVladimir Oltean {
140596ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
140696ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
140796ca08c0SVladimir Oltean 	bool new = false;
140896ca08c0SVladimir Oltean 	int err;
140996ca08c0SVladimir Oltean 
141096ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
141196ca08c0SVladimir Oltean 
141296ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
141396ca08c0SVladimir Oltean 						   false);
141496ca08c0SVladimir Oltean 	if (!trap) {
141596ca08c0SVladimir Oltean 		trap = kzalloc(sizeof(*trap), GFP_KERNEL);
141696ca08c0SVladimir Oltean 		if (!trap)
141796ca08c0SVladimir Oltean 			return -ENOMEM;
141896ca08c0SVladimir Oltean 
141996ca08c0SVladimir Oltean 		populate(trap);
142096ca08c0SVladimir Oltean 		trap->prio = 1;
142196ca08c0SVladimir Oltean 		trap->id.cookie = cookie;
142296ca08c0SVladimir Oltean 		trap->id.tc_offload = false;
142396ca08c0SVladimir Oltean 		trap->block_id = VCAP_IS2;
142496ca08c0SVladimir Oltean 		trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
142596ca08c0SVladimir Oltean 		trap->lookup = 0;
142696ca08c0SVladimir Oltean 		trap->action.cpu_copy_ena = true;
142796ca08c0SVladimir Oltean 		trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
142896ca08c0SVladimir Oltean 		trap->action.port_mask = 0;
14299d75b881SVladimir Oltean 		trap->take_ts = take_ts;
1430e1846cffSVladimir Oltean 		trap->is_trap = true;
143196ca08c0SVladimir Oltean 		new = true;
143296ca08c0SVladimir Oltean 	}
143396ca08c0SVladimir Oltean 
143496ca08c0SVladimir Oltean 	trap->ingress_port_mask |= BIT(port);
143596ca08c0SVladimir Oltean 
143696ca08c0SVladimir Oltean 	if (new)
143796ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_add(ocelot, trap, NULL);
143896ca08c0SVladimir Oltean 	else
143996ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
144096ca08c0SVladimir Oltean 	if (err) {
144196ca08c0SVladimir Oltean 		trap->ingress_port_mask &= ~BIT(port);
1442e1846cffSVladimir Oltean 		if (!trap->ingress_port_mask)
144396ca08c0SVladimir Oltean 			kfree(trap);
144496ca08c0SVladimir Oltean 		return err;
144596ca08c0SVladimir Oltean 	}
144696ca08c0SVladimir Oltean 
144796ca08c0SVladimir Oltean 	return 0;
144896ca08c0SVladimir Oltean }
144996ca08c0SVladimir Oltean 
1450b9bace6eSVladimir Oltean int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie)
145196ca08c0SVladimir Oltean {
145296ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
145396ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
145496ca08c0SVladimir Oltean 
145596ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
145696ca08c0SVladimir Oltean 
145796ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
145896ca08c0SVladimir Oltean 						   false);
145996ca08c0SVladimir Oltean 	if (!trap)
146096ca08c0SVladimir Oltean 		return 0;
146196ca08c0SVladimir Oltean 
146296ca08c0SVladimir Oltean 	trap->ingress_port_mask &= ~BIT(port);
1463e1846cffSVladimir Oltean 	if (!trap->ingress_port_mask)
146496ca08c0SVladimir Oltean 		return ocelot_vcap_filter_del(ocelot, trap);
146596ca08c0SVladimir Oltean 
146696ca08c0SVladimir Oltean 	return ocelot_vcap_filter_replace(ocelot, trap);
146796ca08c0SVladimir Oltean }
146896ca08c0SVladimir Oltean 
1469a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
1470b80af659SVladimir Oltean {
1471b80af659SVladimir Oltean 	u32 mask = 0;
1472b80af659SVladimir Oltean 	int port;
1473b80af659SVladimir Oltean 
1474961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
1475961d8b69SVladimir Oltean 
1476b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1477b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1478b80af659SVladimir Oltean 
1479b80af659SVladimir Oltean 		if (!ocelot_port)
1480b80af659SVladimir Oltean 			continue;
1481b80af659SVladimir Oltean 
1482a14e6b69SVladimir Oltean 		if (ocelot_port->bond == bond)
1483b80af659SVladimir Oltean 			mask |= BIT(port);
1484b80af659SVladimir Oltean 	}
1485b80af659SVladimir Oltean 
1486b80af659SVladimir Oltean 	return mask;
1487b80af659SVladimir Oltean }
1488b80af659SVladimir Oltean 
1489961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical
1490961d8b69SVladimir Oltean  * port ID present in that LAG. It may change if that port ever leaves the LAG.
1491961d8b69SVladimir Oltean  */
1492eca70102SVladimir Oltean int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond)
1493961d8b69SVladimir Oltean {
1494961d8b69SVladimir Oltean 	int bond_mask = ocelot_get_bond_mask(ocelot, bond);
1495961d8b69SVladimir Oltean 
1496961d8b69SVladimir Oltean 	if (!bond_mask)
1497961d8b69SVladimir Oltean 		return -ENOENT;
1498961d8b69SVladimir Oltean 
1499961d8b69SVladimir Oltean 	return __ffs(bond_mask);
1500961d8b69SVladimir Oltean }
1501eca70102SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bond_get_id);
1502961d8b69SVladimir Oltean 
1503291ac151SVladimir Oltean /* Returns the mask of user ports assigned to this DSA tag_8021q CPU port.
1504291ac151SVladimir Oltean  * Note that when CPU ports are in a LAG, the user ports are assigned to the
1505291ac151SVladimir Oltean  * 'primary' CPU port, the one whose physical port number gives the logical
1506291ac151SVladimir Oltean  * port number of the LAG.
1507291ac151SVladimir Oltean  *
1508291ac151SVladimir Oltean  * We leave PGID_SRC poorly configured for the 'secondary' CPU port in the LAG
1509291ac151SVladimir Oltean  * (to which no user port is assigned), but it appears that forwarding from
1510291ac151SVladimir Oltean  * this secondary CPU port looks at the PGID_SRC associated with the logical
1511291ac151SVladimir Oltean  * port ID that it's assigned to, which *is* configured properly.
1512291ac151SVladimir Oltean  */
1513c295f983SVladimir Oltean static u32 ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot *ocelot,
1514c295f983SVladimir Oltean 					       struct ocelot_port *cpu)
1515c295f983SVladimir Oltean {
1516c295f983SVladimir Oltean 	u32 mask = 0;
1517c295f983SVladimir Oltean 	int port;
1518c295f983SVladimir Oltean 
1519c295f983SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1520c295f983SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1521c295f983SVladimir Oltean 
1522c295f983SVladimir Oltean 		if (!ocelot_port)
1523c295f983SVladimir Oltean 			continue;
1524c295f983SVladimir Oltean 
1525c295f983SVladimir Oltean 		if (ocelot_port->dsa_8021q_cpu == cpu)
1526c295f983SVladimir Oltean 			mask |= BIT(port);
1527c295f983SVladimir Oltean 	}
1528c295f983SVladimir Oltean 
1529291ac151SVladimir Oltean 	if (cpu->bond)
1530291ac151SVladimir Oltean 		mask &= ~ocelot_get_bond_mask(ocelot, cpu->bond);
1531291ac151SVladimir Oltean 
1532c295f983SVladimir Oltean 	return mask;
1533c295f983SVladimir Oltean }
1534c295f983SVladimir Oltean 
1535291ac151SVladimir Oltean /* Returns the DSA tag_8021q CPU port that the given port is assigned to,
1536291ac151SVladimir Oltean  * or the bit mask of CPU ports if said CPU port is in a LAG.
1537291ac151SVladimir Oltean  */
1538c295f983SVladimir Oltean u32 ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot *ocelot, int port)
1539c295f983SVladimir Oltean {
1540c295f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1541c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot_port->dsa_8021q_cpu;
1542c295f983SVladimir Oltean 
1543c295f983SVladimir Oltean 	if (!cpu_port)
1544c295f983SVladimir Oltean 		return 0;
1545c295f983SVladimir Oltean 
1546291ac151SVladimir Oltean 	if (cpu_port->bond)
1547291ac151SVladimir Oltean 		return ocelot_get_bond_mask(ocelot, cpu_port->bond);
1548291ac151SVladimir Oltean 
1549c295f983SVladimir Oltean 	return BIT(cpu_port->index);
1550c295f983SVladimir Oltean }
1551c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assigned_dsa_8021q_cpu_mask);
1552c295f983SVladimir Oltean 
15538abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
1554df291e54SVladimir Oltean {
1555acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1556a8bd9fa5SVladimir Oltean 	const struct net_device *bridge;
1557df291e54SVladimir Oltean 	u32 mask = 0;
1558df291e54SVladimir Oltean 	int port;
1559df291e54SVladimir Oltean 
1560a8bd9fa5SVladimir Oltean 	if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
1561a8bd9fa5SVladimir Oltean 		return 0;
1562a8bd9fa5SVladimir Oltean 
1563a8bd9fa5SVladimir Oltean 	bridge = ocelot_port->bridge;
1564a8bd9fa5SVladimir Oltean 	if (!bridge)
1565acc64f52SVladimir Oltean 		return 0;
1566acc64f52SVladimir Oltean 
1567df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1568acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1569df291e54SVladimir Oltean 
1570df291e54SVladimir Oltean 		if (!ocelot_port)
1571df291e54SVladimir Oltean 			continue;
1572df291e54SVladimir Oltean 
1573df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1574df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1575df291e54SVladimir Oltean 			mask |= BIT(port);
1576df291e54SVladimir Oltean 	}
1577df291e54SVladimir Oltean 
1578df291e54SVladimir Oltean 	return mask;
1579df291e54SVladimir Oltean }
15808abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
1581df291e54SVladimir Oltean 
1582a72e23ddSVladimir Oltean static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
1583e21268efSVladimir Oltean {
1584e21268efSVladimir Oltean 	int port;
1585e21268efSVladimir Oltean 
15868abe1970SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
15878abe1970SVladimir Oltean 
15888abe1970SVladimir Oltean 	/* If cut-through forwarding is supported, update the masks before a
15898abe1970SVladimir Oltean 	 * port joins the forwarding domain, to avoid potential underruns if it
15908abe1970SVladimir Oltean 	 * has the highest speed from the new domain.
15918abe1970SVladimir Oltean 	 */
15928abe1970SVladimir Oltean 	if (joining && ocelot->ops->cut_through_fwd)
15938abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
15948abe1970SVladimir Oltean 
15959b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
15969b521250SVladimir Oltean 	 * a source for the other ports.
15979b521250SVladimir Oltean 	 */
15989b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1599e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1600e21268efSVladimir Oltean 		unsigned long mask;
1601e21268efSVladimir Oltean 
1602e21268efSVladimir Oltean 		if (!ocelot_port) {
1603e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1604e21268efSVladimir Oltean 			mask = 0;
1605e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1606e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1607c295f983SVladimir Oltean 			 * forward packets to all ports assigned to them.
1608e21268efSVladimir Oltean 			 */
1609c295f983SVladimir Oltean 			mask = ocelot_dsa_8021q_cpu_assigned_ports(ocelot,
1610c295f983SVladimir Oltean 								   ocelot_port);
1611df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1612528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
16139b521250SVladimir Oltean 
1614a8bd9fa5SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
1615df291e54SVladimir Oltean 			mask &= ~BIT(port);
1616c295f983SVladimir Oltean 
1617c295f983SVladimir Oltean 			mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1618c295f983SVladimir Oltean 									port);
1619c295f983SVladimir Oltean 
1620a14e6b69SVladimir Oltean 			if (bond)
1621a14e6b69SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond);
16229b521250SVladimir Oltean 		} else {
1623e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1624e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1625e21268efSVladimir Oltean 			 * module otherwise.
1626e21268efSVladimir Oltean 			 */
1627c295f983SVladimir Oltean 			mask = ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1628c295f983SVladimir Oltean 								       port);
1629e21268efSVladimir Oltean 		}
1630e21268efSVladimir Oltean 
1631e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
16329b521250SVladimir Oltean 	}
16338abe1970SVladimir Oltean 
16348abe1970SVladimir Oltean 	/* If cut-through forwarding is supported and a port is leaving, there
16358abe1970SVladimir Oltean 	 * is a chance that cut-through was disabled on the other ports due to
16368abe1970SVladimir Oltean 	 * the port which is leaving (it has a higher link speed). We need to
16378abe1970SVladimir Oltean 	 * update the cut-through masks of the remaining ports no earlier than
16388abe1970SVladimir Oltean 	 * after the port has left, to prevent underruns from happening between
16398abe1970SVladimir Oltean 	 * the cut-through update and the forwarding domain update.
16408abe1970SVladimir Oltean 	 */
16418abe1970SVladimir Oltean 	if (!joining && ocelot->ops->cut_through_fwd)
16428abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
16439b521250SVladimir Oltean }
16449b521250SVladimir Oltean 
164561be79baSVladimir Oltean /* Update PGID_CPU which is the destination port mask used for whitelisting
164661be79baSVladimir Oltean  * unicast addresses filtered towards the host. In the normal and NPI modes,
164761be79baSVladimir Oltean  * this points to the analyzer entry for the CPU port module, while in DSA
164861be79baSVladimir Oltean  * tag_8021q mode, it is a bit mask of all active CPU ports.
164961be79baSVladimir Oltean  * PGID_SRC will take care of forwarding a packet from one user port to
165061be79baSVladimir Oltean  * no more than a single CPU port.
165161be79baSVladimir Oltean  */
165261be79baSVladimir Oltean static void ocelot_update_pgid_cpu(struct ocelot *ocelot)
165361be79baSVladimir Oltean {
165461be79baSVladimir Oltean 	int pgid_cpu = 0;
165561be79baSVladimir Oltean 	int port;
165661be79baSVladimir Oltean 
165761be79baSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
165861be79baSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
165961be79baSVladimir Oltean 
166061be79baSVladimir Oltean 		if (!ocelot_port || !ocelot_port->is_dsa_8021q_cpu)
166161be79baSVladimir Oltean 			continue;
166261be79baSVladimir Oltean 
166361be79baSVladimir Oltean 		pgid_cpu |= BIT(port);
166461be79baSVladimir Oltean 	}
166561be79baSVladimir Oltean 
166661be79baSVladimir Oltean 	if (!pgid_cpu)
166761be79baSVladimir Oltean 		pgid_cpu = BIT(ocelot->num_phys_ports);
166861be79baSVladimir Oltean 
166961be79baSVladimir Oltean 	ocelot_write_rix(ocelot, pgid_cpu, ANA_PGID_PGID, PGID_CPU);
167061be79baSVladimir Oltean }
167161be79baSVladimir Oltean 
167236a0bf44SVladimir Oltean void ocelot_port_setup_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
167354c31984SVladimir Oltean {
1674c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
167554c31984SVladimir Oltean 	u16 vid;
167654c31984SVladimir Oltean 
16778c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
16788c166acbSVladimir Oltean 
1679c295f983SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = true;
168054c31984SVladimir Oltean 
168154c31984SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
1682c295f983SVladimir Oltean 		ocelot_vlan_member_add(ocelot, cpu, vid, true);
168361be79baSVladimir Oltean 
168461be79baSVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
1685a72e23ddSVladimir Oltean 
168636a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
168736a0bf44SVladimir Oltean }
168836a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_setup_dsa_8021q_cpu);
168936a0bf44SVladimir Oltean 
169036a0bf44SVladimir Oltean void ocelot_port_teardown_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
169136a0bf44SVladimir Oltean {
169236a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
169336a0bf44SVladimir Oltean 	u16 vid;
169436a0bf44SVladimir Oltean 
169536a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
169636a0bf44SVladimir Oltean 
169736a0bf44SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = false;
169836a0bf44SVladimir Oltean 
169936a0bf44SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
170036a0bf44SVladimir Oltean 		ocelot_vlan_member_del(ocelot, cpu_port->index, vid);
170136a0bf44SVladimir Oltean 
170236a0bf44SVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
170336a0bf44SVladimir Oltean 
170436a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
170536a0bf44SVladimir Oltean }
170636a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_teardown_dsa_8021q_cpu);
170736a0bf44SVladimir Oltean 
170836a0bf44SVladimir Oltean void ocelot_port_assign_dsa_8021q_cpu(struct ocelot *ocelot, int port,
170936a0bf44SVladimir Oltean 				      int cpu)
171036a0bf44SVladimir Oltean {
171136a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
171236a0bf44SVladimir Oltean 
171336a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
171436a0bf44SVladimir Oltean 
171536a0bf44SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = cpu_port;
1716a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
17178c166acbSVladimir Oltean 
17188c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
171954c31984SVladimir Oltean }
1720c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assign_dsa_8021q_cpu);
172154c31984SVladimir Oltean 
1722c295f983SVladimir Oltean void ocelot_port_unassign_dsa_8021q_cpu(struct ocelot *ocelot, int port)
172354c31984SVladimir Oltean {
17248c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
17258c166acbSVladimir Oltean 
1726c295f983SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = NULL;
1727a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
17288c166acbSVladimir Oltean 
17298c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
173054c31984SVladimir Oltean }
1731c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_unassign_dsa_8021q_cpu);
173254c31984SVladimir Oltean 
17335e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1734a556c76aSAlexandre Belloni {
1735421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1736df291e54SVladimir Oltean 	u32 learn_ena = 0;
1737a556c76aSAlexandre Belloni 
17388abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
17398abe1970SVladimir Oltean 
1740df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1741a556c76aSAlexandre Belloni 
1742df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1743df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1744df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1745a556c76aSAlexandre Belloni 
1746df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1747df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1748a556c76aSAlexandre Belloni 
17498abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
17508abe1970SVladimir Oltean 
17518abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
1752a556c76aSAlexandre Belloni }
17535e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1754a556c76aSAlexandre Belloni 
17555e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
17564bda1415SVladimir Oltean {
1757c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1758c0d7eccbSVladimir Oltean 
1759c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1760c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1761c0d7eccbSVladimir Oltean 	 */
1762c0d7eccbSVladimir Oltean 	if (!age_period)
1763c0d7eccbSVladimir Oltean 		age_period = 1;
1764c0d7eccbSVladimir Oltean 
1765c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1766a556c76aSAlexandre Belloni }
17675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1768a556c76aSAlexandre Belloni 
1769a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1770a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1771a556c76aSAlexandre Belloni 						     u16 vid)
1772a556c76aSAlexandre Belloni {
1773a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1774a556c76aSAlexandre Belloni 
1775a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1776a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1777a556c76aSAlexandre Belloni 			return mc;
1778a556c76aSAlexandre Belloni 	}
1779a556c76aSAlexandre Belloni 
1780a556c76aSAlexandre Belloni 	return NULL;
1781a556c76aSAlexandre Belloni }
1782a556c76aSAlexandre Belloni 
17839403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
17849403c158SVladimir Oltean {
17859403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
17869403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
17879403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
17889403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
17897c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
17909403c158SVladimir Oltean }
17919403c158SVladimir Oltean 
1792e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1793e5d1f896SVladimir Oltean 					     unsigned long ports)
1794e5d1f896SVladimir Oltean {
1795e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1796e5d1f896SVladimir Oltean 
1797e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1798e5d1f896SVladimir Oltean 	if (!pgid)
1799e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1800e5d1f896SVladimir Oltean 
1801e5d1f896SVladimir Oltean 	pgid->ports = ports;
1802e5d1f896SVladimir Oltean 	pgid->index = index;
1803e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1804e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1805e5d1f896SVladimir Oltean 
1806e5d1f896SVladimir Oltean 	return pgid;
1807e5d1f896SVladimir Oltean }
1808e5d1f896SVladimir Oltean 
1809e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1810e5d1f896SVladimir Oltean {
1811e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1812e5d1f896SVladimir Oltean 		return;
1813e5d1f896SVladimir Oltean 
1814e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1815e5d1f896SVladimir Oltean 	kfree(pgid);
1816e5d1f896SVladimir Oltean }
1817e5d1f896SVladimir Oltean 
1818e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1819bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
18209403c158SVladimir Oltean {
1821e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1822e5d1f896SVladimir Oltean 	int index;
18239403c158SVladimir Oltean 
18249403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
18259403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
18269403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
18279403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
18289403c158SVladimir Oltean 	 */
1829bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1830bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1831e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
18329403c158SVladimir Oltean 
1833e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1834e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1835e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1836e5d1f896SVladimir Oltean 		 */
1837e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1838e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1839e5d1f896SVladimir Oltean 			return pgid;
1840e5d1f896SVladimir Oltean 		}
1841e5d1f896SVladimir Oltean 	}
1842e5d1f896SVladimir Oltean 
1843e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1844e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
18459403c158SVladimir Oltean 		bool used = false;
18469403c158SVladimir Oltean 
1847e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1848e5d1f896SVladimir Oltean 			if (pgid->index == index) {
18499403c158SVladimir Oltean 				used = true;
18509403c158SVladimir Oltean 				break;
18519403c158SVladimir Oltean 			}
18529403c158SVladimir Oltean 		}
18539403c158SVladimir Oltean 
18549403c158SVladimir Oltean 		if (!used)
1855e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
18569403c158SVladimir Oltean 	}
18579403c158SVladimir Oltean 
1858e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
18599403c158SVladimir Oltean }
18609403c158SVladimir Oltean 
18619403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1862bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
18639403c158SVladimir Oltean {
1864ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
18659403c158SVladimir Oltean 
1866bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
18679403c158SVladimir Oltean 		addr[0] = 0;
18689403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
18699403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1870bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
18719403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
18729403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
18739403c158SVladimir Oltean 	}
18749403c158SVladimir Oltean }
18759403c158SVladimir Oltean 
1876209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
187754c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
187854c31984SVladimir Oltean 			const struct net_device *bridge)
1879a556c76aSAlexandre Belloni {
1880a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1881004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1882e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1883a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1884a556c76aSAlexandre Belloni 
188554c31984SVladimir Oltean 	if (!vid)
188654c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
188754c31984SVladimir Oltean 
1888a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1889a556c76aSAlexandre Belloni 	if (!mc) {
1890728e69aeSVladimir Oltean 		/* New entry */
1891bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1892bb8d53fdSVladimir Oltean 		if (!mc)
1893bb8d53fdSVladimir Oltean 			return -ENOMEM;
1894bb8d53fdSVladimir Oltean 
1895bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1896bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1897bb8d53fdSVladimir Oltean 		mc->vid = vid;
1898bb8d53fdSVladimir Oltean 
1899a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1900728e69aeSVladimir Oltean 	} else {
1901e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1902e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1903e5d1f896SVladimir Oltean 		 */
1904e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1905bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1906a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1907a556c76aSAlexandre Belloni 	}
1908a556c76aSAlexandre Belloni 
1909004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1910e5d1f896SVladimir Oltean 
1911e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1912e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1913e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1914e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1915e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1916e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1917e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1918e5d1f896SVladimir Oltean 	}
1919e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1920e5d1f896SVladimir Oltean 
1921bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1922a556c76aSAlexandre Belloni 
1923e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1924e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1925e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1926e5d1f896SVladimir Oltean 				 pgid->index);
1927e5d1f896SVladimir Oltean 
1928e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1929bb8d53fdSVladimir Oltean 				 mc->entry_type);
1930a556c76aSAlexandre Belloni }
1931209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1932a556c76aSAlexandre Belloni 
1933209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
193454c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
193554c31984SVladimir Oltean 			const struct net_device *bridge)
1936a556c76aSAlexandre Belloni {
1937a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1938004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1939e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1940a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1941a556c76aSAlexandre Belloni 
194254c31984SVladimir Oltean 	if (!vid)
194354c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
194454c31984SVladimir Oltean 
1945a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1946a556c76aSAlexandre Belloni 	if (!mc)
1947a556c76aSAlexandre Belloni 		return -ENOENT;
1948a556c76aSAlexandre Belloni 
1949bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1950a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1951a556c76aSAlexandre Belloni 
1952e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1953004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1954a556c76aSAlexandre Belloni 	if (!mc->ports) {
1955a556c76aSAlexandre Belloni 		list_del(&mc->list);
1956a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1957a556c76aSAlexandre Belloni 		return 0;
1958a556c76aSAlexandre Belloni 	}
1959a556c76aSAlexandre Belloni 
1960e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1961e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1962e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1963e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1964e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1965e5d1f896SVladimir Oltean 
1966bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1967a556c76aSAlexandre Belloni 
1968e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1969e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1970e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1971e5d1f896SVladimir Oltean 				 pgid->index);
1972e5d1f896SVladimir Oltean 
1973e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1974bb8d53fdSVladimir Oltean 				 mc->entry_type);
1975a556c76aSAlexandre Belloni }
1976209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1977a556c76aSAlexandre Belloni 
197854c31984SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
197954c31984SVladimir Oltean 			    struct net_device *bridge, int bridge_num,
198054c31984SVladimir Oltean 			    struct netlink_ext_ack *extack)
1981a556c76aSAlexandre Belloni {
1982df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
198354c31984SVladimir Oltean 	int err;
198454c31984SVladimir Oltean 
198554c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
198654c31984SVladimir Oltean 	if (err)
198754c31984SVladimir Oltean 		return err;
1988a556c76aSAlexandre Belloni 
19898abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
19908abe1970SVladimir Oltean 
1991df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
199254c31984SVladimir Oltean 	ocelot_port->bridge_num = bridge_num;
1993a556c76aSAlexandre Belloni 
19948abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
19958abe1970SVladimir Oltean 
19968abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
199754c31984SVladimir Oltean 
199854c31984SVladimir Oltean 	if (br_vlan_enabled(bridge))
199954c31984SVladimir Oltean 		return 0;
200054c31984SVladimir Oltean 
200154c31984SVladimir Oltean 	return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge);
2002a556c76aSAlexandre Belloni }
20035e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
2004a556c76aSAlexandre Belloni 
2005e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
2006a556c76aSAlexandre Belloni 			      struct net_device *bridge)
2007a556c76aSAlexandre Belloni {
2008df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
20092e554a7aSVladimir Oltean 
20108abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
20118abe1970SVladimir Oltean 
201254c31984SVladimir Oltean 	if (!br_vlan_enabled(bridge))
201354c31984SVladimir Oltean 		ocelot_del_vlan_unaware_pvid(ocelot, port, bridge);
201454c31984SVladimir Oltean 
2015df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
201654c31984SVladimir Oltean 	ocelot_port->bridge_num = -1;
20177142529fSAntoine Tenart 
2018d4004422SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, NULL);
20190da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
20208abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
20218abe1970SVladimir Oltean 
20228abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2023a556c76aSAlexandre Belloni }
20245e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
2025a556c76aSAlexandre Belloni 
2026dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
2027dc96ee37SAlexandre Belloni {
2028528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
2029dc96ee37SAlexandre Belloni 	int i, port, lag;
2030dc96ee37SAlexandre Belloni 
2031dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
203296b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
2033dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2034dc96ee37SAlexandre Belloni 
203596b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
2036dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
2037dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
2038dc96ee37SAlexandre Belloni 
2039528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
2040528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
2041528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
2042528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
2043528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
2044528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
2045528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
2046528d3f19SVladimir Oltean 	 */
2047528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2048528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2049528d3f19SVladimir Oltean 
2050528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
2051528d3f19SVladimir Oltean 			continue;
2052528d3f19SVladimir Oltean 
2053528d3f19SVladimir Oltean 		visited &= ~BIT(port);
2054528d3f19SVladimir Oltean 	}
2055528d3f19SVladimir Oltean 
2056528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
2057dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
2058528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
205923ca3b72SVladimir Oltean 		int num_active_ports = 0;
2060dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
2061dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
2062dc96ee37SAlexandre Belloni 
2063528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
2064dc96ee37SAlexandre Belloni 			continue;
2065dc96ee37SAlexandre Belloni 
2066a14e6b69SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond);
2067528d3f19SVladimir Oltean 
2068dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
2069a14e6b69SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2070a14e6b69SVladimir Oltean 
2071dc96ee37SAlexandre Belloni 			// Destination mask
2072dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
2073dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
2074a14e6b69SVladimir Oltean 
2075a14e6b69SVladimir Oltean 			if (ocelot_port->lag_tx_active)
207623ca3b72SVladimir Oltean 				aggr_idx[num_active_ports++] = port;
2077dc96ee37SAlexandre Belloni 		}
2078dc96ee37SAlexandre Belloni 
207996b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
2080dc96ee37SAlexandre Belloni 			u32 ac;
2081dc96ee37SAlexandre Belloni 
2082dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
2083dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
208423ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
208523ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
208623ca3b72SVladimir Oltean 			 */
208723ca3b72SVladimir Oltean 			if (num_active_ports)
208823ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
2089dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
2090dc96ee37SAlexandre Belloni 		}
2091528d3f19SVladimir Oltean 
2092528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
2093528d3f19SVladimir Oltean 		 * the same config again.
2094528d3f19SVladimir Oltean 		 */
2095528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
2096528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2097528d3f19SVladimir Oltean 
2098528d3f19SVladimir Oltean 			if (!ocelot_port)
2099528d3f19SVladimir Oltean 				continue;
2100528d3f19SVladimir Oltean 
2101528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
2102528d3f19SVladimir Oltean 				visited |= BIT(port);
2103528d3f19SVladimir Oltean 		}
2104dc96ee37SAlexandre Belloni 	}
2105dc96ee37SAlexandre Belloni }
2106dc96ee37SAlexandre Belloni 
21072527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
21082527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
21092527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
21102527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
21112527f2e8SVladimir Oltean  */
21122527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
2113dc96ee37SAlexandre Belloni {
21142527f2e8SVladimir Oltean 	int port;
2115dc96ee37SAlexandre Belloni 
21162527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
21172527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
21182527f2e8SVladimir Oltean 		struct net_device *bond;
2119dc96ee37SAlexandre Belloni 
21202527f2e8SVladimir Oltean 		if (!ocelot_port)
21212527f2e8SVladimir Oltean 			continue;
2122dc96ee37SAlexandre Belloni 
21232527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
21242527f2e8SVladimir Oltean 		if (bond) {
2125961d8b69SVladimir Oltean 			int lag = ocelot_bond_get_id(ocelot, bond);
21262527f2e8SVladimir Oltean 
21272527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
2128dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
21292527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
21302527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
21312527f2e8SVladimir Oltean 		} else {
21322527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
21332527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
21342527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
21352527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
21362527f2e8SVladimir Oltean 		}
2137dc96ee37SAlexandre Belloni 	}
2138dc96ee37SAlexandre Belloni }
2139dc96ee37SAlexandre Belloni 
214028de0f9fSVladimir Oltean static int ocelot_migrate_mc(struct ocelot *ocelot, struct ocelot_multicast *mc,
214128de0f9fSVladimir Oltean 			     unsigned long from_mask, unsigned long to_mask)
214228de0f9fSVladimir Oltean {
214328de0f9fSVladimir Oltean 	unsigned char addr[ETH_ALEN];
214428de0f9fSVladimir Oltean 	struct ocelot_pgid *pgid;
214528de0f9fSVladimir Oltean 	u16 vid = mc->vid;
214628de0f9fSVladimir Oltean 
214728de0f9fSVladimir Oltean 	dev_dbg(ocelot->dev,
214828de0f9fSVladimir Oltean 		"Migrating multicast %pM vid %d from port mask 0x%lx to 0x%lx\n",
214928de0f9fSVladimir Oltean 		mc->addr, mc->vid, from_mask, to_mask);
215028de0f9fSVladimir Oltean 
215128de0f9fSVladimir Oltean 	/* First clean up the current port mask from hardware, because
215228de0f9fSVladimir Oltean 	 * we'll be modifying it.
215328de0f9fSVladimir Oltean 	 */
215428de0f9fSVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
215528de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
215628de0f9fSVladimir Oltean 	ocelot_mact_forget(ocelot, addr, vid);
215728de0f9fSVladimir Oltean 
215828de0f9fSVladimir Oltean 	mc->ports &= ~from_mask;
215928de0f9fSVladimir Oltean 	mc->ports |= to_mask;
216028de0f9fSVladimir Oltean 
216128de0f9fSVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
216228de0f9fSVladimir Oltean 	if (IS_ERR(pgid)) {
216328de0f9fSVladimir Oltean 		dev_err(ocelot->dev,
216428de0f9fSVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
216528de0f9fSVladimir Oltean 			mc->addr, mc->vid);
216628de0f9fSVladimir Oltean 		devm_kfree(ocelot->dev, mc);
216728de0f9fSVladimir Oltean 		return PTR_ERR(pgid);
216828de0f9fSVladimir Oltean 	}
216928de0f9fSVladimir Oltean 	mc->pgid = pgid;
217028de0f9fSVladimir Oltean 
217128de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
217228de0f9fSVladimir Oltean 
217328de0f9fSVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
217428de0f9fSVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
217528de0f9fSVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
217628de0f9fSVladimir Oltean 				 pgid->index);
217728de0f9fSVladimir Oltean 
217828de0f9fSVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
217928de0f9fSVladimir Oltean 				 mc->entry_type);
218028de0f9fSVladimir Oltean }
218128de0f9fSVladimir Oltean 
218228de0f9fSVladimir Oltean int ocelot_migrate_mdbs(struct ocelot *ocelot, unsigned long from_mask,
218328de0f9fSVladimir Oltean 			unsigned long to_mask)
218428de0f9fSVladimir Oltean {
218528de0f9fSVladimir Oltean 	struct ocelot_multicast *mc;
218628de0f9fSVladimir Oltean 	int err;
218728de0f9fSVladimir Oltean 
218828de0f9fSVladimir Oltean 	list_for_each_entry(mc, &ocelot->multicast, list) {
218928de0f9fSVladimir Oltean 		if (!(mc->ports & from_mask))
219028de0f9fSVladimir Oltean 			continue;
219128de0f9fSVladimir Oltean 
219228de0f9fSVladimir Oltean 		err = ocelot_migrate_mc(ocelot, mc, from_mask, to_mask);
219328de0f9fSVladimir Oltean 		if (err)
219428de0f9fSVladimir Oltean 			return err;
219528de0f9fSVladimir Oltean 	}
219628de0f9fSVladimir Oltean 
219728de0f9fSVladimir Oltean 	return 0;
219828de0f9fSVladimir Oltean }
219928de0f9fSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_migrate_mdbs);
220028de0f9fSVladimir Oltean 
2201961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says:
2202961d8b69SVladimir Oltean  *     Logical port number for front port. If port is not a member of a LLAG,
2203961d8b69SVladimir Oltean  *     then PORTID must be set to the physical port number.
2204961d8b69SVladimir Oltean  *     If port is a member of a LLAG, then PORTID must be set to the common
2205961d8b69SVladimir Oltean  *     PORTID_VAL used for all member ports of the LLAG.
2206961d8b69SVladimir Oltean  *     The value must not exceed the number of physical ports on the device.
2207961d8b69SVladimir Oltean  *
2208961d8b69SVladimir Oltean  * This means we have little choice but to migrate FDB entries pointing towards
2209961d8b69SVladimir Oltean  * a logical port when that changes.
2210961d8b69SVladimir Oltean  */
2211961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot,
2212961d8b69SVladimir Oltean 				    struct net_device *bond,
2213961d8b69SVladimir Oltean 				    int lag)
2214961d8b69SVladimir Oltean {
2215961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2216961d8b69SVladimir Oltean 	int err;
2217961d8b69SVladimir Oltean 
2218961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2219961d8b69SVladimir Oltean 
2220961d8b69SVladimir Oltean 	list_for_each_entry(fdb, &ocelot->lag_fdbs, list) {
2221961d8b69SVladimir Oltean 		if (fdb->bond != bond)
2222961d8b69SVladimir Oltean 			continue;
2223961d8b69SVladimir Oltean 
2224961d8b69SVladimir Oltean 		err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid);
2225961d8b69SVladimir Oltean 		if (err) {
2226961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2227961d8b69SVladimir Oltean 				"failed to delete LAG %s FDB %pM vid %d: %pe\n",
2228961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2229961d8b69SVladimir Oltean 		}
2230961d8b69SVladimir Oltean 
2231961d8b69SVladimir Oltean 		err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid,
2232961d8b69SVladimir Oltean 					ENTRYTYPE_LOCKED);
2233961d8b69SVladimir Oltean 		if (err) {
2234961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2235961d8b69SVladimir Oltean 				"failed to migrate LAG %s FDB %pM vid %d: %pe\n",
2236961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2237961d8b69SVladimir Oltean 		}
2238961d8b69SVladimir Oltean 	}
2239961d8b69SVladimir Oltean }
2240961d8b69SVladimir Oltean 
22419c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2242583cbbe3SVladimir Oltean 			 struct net_device *bond,
22432e359b00SVladimir Oltean 			 struct netdev_lag_upper_info *info,
22442e359b00SVladimir Oltean 			 struct netlink_ext_ack *extack)
2245dc96ee37SAlexandre Belloni {
22462e359b00SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
22472e359b00SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
22482e359b00SVladimir Oltean 				   "Can only offload LAG using hash TX type");
2249583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
22502e359b00SVladimir Oltean 	}
2251583cbbe3SVladimir Oltean 
22528abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
22538abe1970SVladimir Oltean 
2254b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
2255dc96ee37SAlexandre Belloni 
22562527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
22578abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2258dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2259dc96ee37SAlexandre Belloni 
22608abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
22618abe1970SVladimir Oltean 
2262dc96ee37SAlexandre Belloni 	return 0;
2263dc96ee37SAlexandre Belloni }
22649c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
2265dc96ee37SAlexandre Belloni 
22669c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2267dc96ee37SAlexandre Belloni 			   struct net_device *bond)
2268dc96ee37SAlexandre Belloni {
2269961d8b69SVladimir Oltean 	int old_lag_id, new_lag_id;
2270961d8b69SVladimir Oltean 
22718abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
22728abe1970SVladimir Oltean 
2273961d8b69SVladimir Oltean 	old_lag_id = ocelot_bond_get_id(ocelot, bond);
2274961d8b69SVladimir Oltean 
2275b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
2276b80af659SVladimir Oltean 
22772527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
22788abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2279dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
22808abe1970SVladimir Oltean 
2281961d8b69SVladimir Oltean 	new_lag_id = ocelot_bond_get_id(ocelot, bond);
2282961d8b69SVladimir Oltean 
2283961d8b69SVladimir Oltean 	if (new_lag_id >= 0 && old_lag_id != new_lag_id)
2284961d8b69SVladimir Oltean 		ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id);
2285961d8b69SVladimir Oltean 
22868abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2287dc96ee37SAlexandre Belloni }
22889c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
22890e332c85SPetr Machata 
229023ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
229123ca3b72SVladimir Oltean {
229223ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
229323ca3b72SVladimir Oltean 
2294961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2295961d8b69SVladimir Oltean 
229623ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
229723ca3b72SVladimir Oltean 
229823ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
229923ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
2300961d8b69SVladimir Oltean 
2301961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
230223ca3b72SVladimir Oltean }
230323ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
230423ca3b72SVladimir Oltean 
2305961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond,
230654c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
230754c31984SVladimir Oltean 		       const struct net_device *bridge)
2308961d8b69SVladimir Oltean {
2309961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2310961d8b69SVladimir Oltean 	int lag, err;
2311961d8b69SVladimir Oltean 
2312961d8b69SVladimir Oltean 	fdb = kzalloc(sizeof(*fdb), GFP_KERNEL);
2313961d8b69SVladimir Oltean 	if (!fdb)
2314961d8b69SVladimir Oltean 		return -ENOMEM;
2315961d8b69SVladimir Oltean 
231654c31984SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
231754c31984SVladimir Oltean 
231854c31984SVladimir Oltean 	if (!vid)
231954c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
232054c31984SVladimir Oltean 
2321961d8b69SVladimir Oltean 	ether_addr_copy(fdb->addr, addr);
2322961d8b69SVladimir Oltean 	fdb->vid = vid;
2323961d8b69SVladimir Oltean 	fdb->bond = bond;
2324961d8b69SVladimir Oltean 
2325961d8b69SVladimir Oltean 	lag = ocelot_bond_get_id(ocelot, bond);
2326961d8b69SVladimir Oltean 
2327961d8b69SVladimir Oltean 	err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED);
2328961d8b69SVladimir Oltean 	if (err) {
2329961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2330961d8b69SVladimir Oltean 		kfree(fdb);
2331961d8b69SVladimir Oltean 		return err;
2332961d8b69SVladimir Oltean 	}
2333961d8b69SVladimir Oltean 
2334961d8b69SVladimir Oltean 	list_add_tail(&fdb->list, &ocelot->lag_fdbs);
2335961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2336961d8b69SVladimir Oltean 
2337961d8b69SVladimir Oltean 	return 0;
2338961d8b69SVladimir Oltean }
2339961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add);
2340961d8b69SVladimir Oltean 
2341961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond,
234254c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
234354c31984SVladimir Oltean 		       const struct net_device *bridge)
2344961d8b69SVladimir Oltean {
2345961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb, *tmp;
2346961d8b69SVladimir Oltean 
2347961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2348961d8b69SVladimir Oltean 
234954c31984SVladimir Oltean 	if (!vid)
235054c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
235154c31984SVladimir Oltean 
2352961d8b69SVladimir Oltean 	list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) {
2353961d8b69SVladimir Oltean 		if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid ||
2354961d8b69SVladimir Oltean 		    fdb->bond != bond)
2355961d8b69SVladimir Oltean 			continue;
2356961d8b69SVladimir Oltean 
2357961d8b69SVladimir Oltean 		ocelot_mact_forget(ocelot, addr, vid);
2358961d8b69SVladimir Oltean 		list_del(&fdb->list);
2359961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2360961d8b69SVladimir Oltean 		kfree(fdb);
2361961d8b69SVladimir Oltean 
2362961d8b69SVladimir Oltean 		return 0;
2363961d8b69SVladimir Oltean 	}
2364961d8b69SVladimir Oltean 
2365961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2366961d8b69SVladimir Oltean 
2367961d8b69SVladimir Oltean 	return -ENOENT;
2368961d8b69SVladimir Oltean }
2369961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del);
2370961d8b69SVladimir Oltean 
2371a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2372a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
23730b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
23740b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
23750b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2376a8015dedSVladimir Oltean  */
23770b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
237831350d7fSVladimir Oltean {
237931350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2380a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2381e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
2382601e984fSVladimir Oltean 	int atop, atop_tot;
238331350d7fSVladimir Oltean 
23840b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
23850b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
23860b912fc9SVladimir Oltean 
2387cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
23880b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2389cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
23900b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
23910b912fc9SVladimir Oltean 	}
23920b912fc9SVladimir Oltean 
2393a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2394fa914e9cSVladimir Oltean 
2395e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
2396e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2397e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2398541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2399541132f0SMaxim Kochetkov 			    pause_start);
2400541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2401541132f0SMaxim Kochetkov 			    pause_stop);
2402fa914e9cSVladimir Oltean 
2403601e984fSVladimir Oltean 	/* Tail dropping watermarks */
2404f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2405a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2406601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2407601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2408601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2409fa914e9cSVladimir Oltean }
24100b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
24110b912fc9SVladimir Oltean 
24120b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
24130b912fc9SVladimir Oltean {
24140b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
24150b912fc9SVladimir Oltean 
24160b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
24170b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
24180b912fc9SVladimir Oltean 
2419cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
24200b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2421cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
24220b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
24230b912fc9SVladimir Oltean 	}
24240b912fc9SVladimir Oltean 
24250b912fc9SVladimir Oltean 	return max_mtu;
24260b912fc9SVladimir Oltean }
24270b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2428fa914e9cSVladimir Oltean 
2429421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2430421741eaSVladimir Oltean 				     bool enabled)
2431421741eaSVladimir Oltean {
2432421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2433421741eaSVladimir Oltean 	u32 val = 0;
2434421741eaSVladimir Oltean 
2435421741eaSVladimir Oltean 	if (enabled)
2436421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2437421741eaSVladimir Oltean 
2438421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2439421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2440421741eaSVladimir Oltean 
2441421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2442421741eaSVladimir Oltean }
2443421741eaSVladimir Oltean 
2444421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2445421741eaSVladimir Oltean 					bool enabled)
2446421741eaSVladimir Oltean {
2447421741eaSVladimir Oltean 	u32 val = 0;
2448421741eaSVladimir Oltean 
2449421741eaSVladimir Oltean 	if (enabled)
2450421741eaSVladimir Oltean 		val = BIT(port);
2451421741eaSVladimir Oltean 
2452421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2453421741eaSVladimir Oltean }
2454421741eaSVladimir Oltean 
2455421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2456421741eaSVladimir Oltean 					bool enabled)
2457421741eaSVladimir Oltean {
2458421741eaSVladimir Oltean 	u32 val = 0;
2459421741eaSVladimir Oltean 
2460421741eaSVladimir Oltean 	if (enabled)
2461421741eaSVladimir Oltean 		val = BIT(port);
2462421741eaSVladimir Oltean 
2463421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
24644cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4);
24654cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6);
2466421741eaSVladimir Oltean }
2467421741eaSVladimir Oltean 
2468421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2469421741eaSVladimir Oltean 					bool enabled)
2470421741eaSVladimir Oltean {
2471421741eaSVladimir Oltean 	u32 val = 0;
2472421741eaSVladimir Oltean 
2473421741eaSVladimir Oltean 	if (enabled)
2474421741eaSVladimir Oltean 		val = BIT(port);
2475421741eaSVladimir Oltean 
2476421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2477421741eaSVladimir Oltean }
2478421741eaSVladimir Oltean 
2479421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2480421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2481421741eaSVladimir Oltean {
2482421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2483421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2484421741eaSVladimir Oltean 		return -EINVAL;
2485421741eaSVladimir Oltean 
2486421741eaSVladimir Oltean 	return 0;
2487421741eaSVladimir Oltean }
2488421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2489421741eaSVladimir Oltean 
2490421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2491421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2492421741eaSVladimir Oltean {
2493421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2494421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2495421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2496421741eaSVladimir Oltean 
2497421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2498421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2499421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2500421741eaSVladimir Oltean 
2501421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2502421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2503421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2504421741eaSVladimir Oltean 
2505421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2506421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2507421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2508421741eaSVladimir Oltean }
2509421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2510421741eaSVladimir Oltean 
2511978777d0SVladimir Oltean int ocelot_port_get_default_prio(struct ocelot *ocelot, int port)
2512978777d0SVladimir Oltean {
2513978777d0SVladimir Oltean 	int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2514978777d0SVladimir Oltean 
2515978777d0SVladimir Oltean 	return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val);
2516978777d0SVladimir Oltean }
2517978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio);
2518978777d0SVladimir Oltean 
2519978777d0SVladimir Oltean int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio)
2520978777d0SVladimir Oltean {
252172f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2522978777d0SVladimir Oltean 		return -ERANGE;
2523978777d0SVladimir Oltean 
2524978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot,
2525978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio),
2526978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M,
2527978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG,
2528978777d0SVladimir Oltean 		       port);
2529978777d0SVladimir Oltean 
2530978777d0SVladimir Oltean 	return 0;
2531978777d0SVladimir Oltean }
2532978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio);
2533978777d0SVladimir Oltean 
2534978777d0SVladimir Oltean int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp)
2535978777d0SVladimir Oltean {
2536978777d0SVladimir Oltean 	int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2537978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2538978777d0SVladimir Oltean 
2539978777d0SVladimir Oltean 	/* Return error if DSCP prioritization isn't enabled */
2540978777d0SVladimir Oltean 	if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA))
2541978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2542978777d0SVladimir Oltean 
2543978777d0SVladimir Oltean 	if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) {
2544978777d0SVladimir Oltean 		dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg);
2545978777d0SVladimir Oltean 		/* Re-read ANA_DSCP_CFG for the translated DSCP */
2546978777d0SVladimir Oltean 		dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2547978777d0SVladimir Oltean 	}
2548978777d0SVladimir Oltean 
2549978777d0SVladimir Oltean 	/* If the DSCP value is not trusted, the QoS classification falls back
2550978777d0SVladimir Oltean 	 * to VLAN PCP or port-based default.
2551978777d0SVladimir Oltean 	 */
2552978777d0SVladimir Oltean 	if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA))
2553978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2554978777d0SVladimir Oltean 
2555978777d0SVladimir Oltean 	return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg);
2556978777d0SVladimir Oltean }
2557978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio);
2558978777d0SVladimir Oltean 
2559978777d0SVladimir Oltean int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2560978777d0SVladimir Oltean {
2561978777d0SVladimir Oltean 	int mask, val;
2562978777d0SVladimir Oltean 
256372f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2564978777d0SVladimir Oltean 		return -ERANGE;
2565978777d0SVladimir Oltean 
2566978777d0SVladimir Oltean 	/* There is at least one app table priority (this one), so we need to
2567978777d0SVladimir Oltean 	 * make sure DSCP prioritization is enabled on the port.
2568978777d0SVladimir Oltean 	 * Also make sure DSCP translation is disabled
2569978777d0SVladimir Oltean 	 * (dcbnl doesn't support it).
2570978777d0SVladimir Oltean 	 */
2571978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2572978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2573978777d0SVladimir Oltean 
2574978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask,
2575978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG, port);
2576978777d0SVladimir Oltean 
2577978777d0SVladimir Oltean 	/* Trust this DSCP value and map it to the given QoS class */
2578978777d0SVladimir Oltean 	val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio);
2579978777d0SVladimir Oltean 
2580978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp);
2581978777d0SVladimir Oltean 
2582978777d0SVladimir Oltean 	return 0;
2583978777d0SVladimir Oltean }
2584978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio);
2585978777d0SVladimir Oltean 
2586978777d0SVladimir Oltean int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2587978777d0SVladimir Oltean {
2588978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2589978777d0SVladimir Oltean 	int mask, i;
2590978777d0SVladimir Oltean 
2591978777d0SVladimir Oltean 	/* During a "dcb app replace" command, the new app table entry will be
2592978777d0SVladimir Oltean 	 * added first, then the old one will be deleted. But the hardware only
2593978777d0SVladimir Oltean 	 * supports one QoS class per DSCP value (duh), so if we blindly delete
2594978777d0SVladimir Oltean 	 * the app table entry for this DSCP value, we end up deleting the
2595978777d0SVladimir Oltean 	 * entry with the new priority. Avoid that by checking whether user
2596978777d0SVladimir Oltean 	 * space wants to delete the priority which is currently configured, or
2597978777d0SVladimir Oltean 	 * something else which is no longer current.
2598978777d0SVladimir Oltean 	 */
2599978777d0SVladimir Oltean 	if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio)
2600978777d0SVladimir Oltean 		return 0;
2601978777d0SVladimir Oltean 
2602978777d0SVladimir Oltean 	/* Untrust this DSCP value */
2603978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp);
2604978777d0SVladimir Oltean 
2605978777d0SVladimir Oltean 	for (i = 0; i < 64; i++) {
2606978777d0SVladimir Oltean 		int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i);
2607978777d0SVladimir Oltean 
2608978777d0SVladimir Oltean 		/* There are still app table entries on the port, so we need to
2609978777d0SVladimir Oltean 		 * keep DSCP enabled, nothing to do.
2610978777d0SVladimir Oltean 		 */
2611978777d0SVladimir Oltean 		if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)
2612978777d0SVladimir Oltean 			return 0;
2613978777d0SVladimir Oltean 	}
2614978777d0SVladimir Oltean 
2615978777d0SVladimir Oltean 	/* Disable DSCP QoS classification if there isn't any trusted
2616978777d0SVladimir Oltean 	 * DSCP value left.
2617978777d0SVladimir Oltean 	 */
2618978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2619978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2620978777d0SVladimir Oltean 
2621978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port);
2622978777d0SVladimir Oltean 
2623978777d0SVladimir Oltean 	return 0;
2624978777d0SVladimir Oltean }
2625978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio);
2626978777d0SVladimir Oltean 
2627f2a0e216SVladimir Oltean struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to,
2628ccb6ed42SVladimir Oltean 					struct netlink_ext_ack *extack)
2629ccb6ed42SVladimir Oltean {
2630ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2631ccb6ed42SVladimir Oltean 
2632ccb6ed42SVladimir Oltean 	if (m) {
2633ccb6ed42SVladimir Oltean 		if (m->to != to) {
2634ccb6ed42SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
2635ccb6ed42SVladimir Oltean 					   "Mirroring already configured towards different egress port");
2636ccb6ed42SVladimir Oltean 			return ERR_PTR(-EBUSY);
2637ccb6ed42SVladimir Oltean 		}
2638ccb6ed42SVladimir Oltean 
2639ccb6ed42SVladimir Oltean 		refcount_inc(&m->refcount);
2640ccb6ed42SVladimir Oltean 		return m;
2641ccb6ed42SVladimir Oltean 	}
2642ccb6ed42SVladimir Oltean 
2643ccb6ed42SVladimir Oltean 	m = kzalloc(sizeof(*m), GFP_KERNEL);
2644ccb6ed42SVladimir Oltean 	if (!m)
2645ccb6ed42SVladimir Oltean 		return ERR_PTR(-ENOMEM);
2646ccb6ed42SVladimir Oltean 
2647ccb6ed42SVladimir Oltean 	m->to = to;
2648ccb6ed42SVladimir Oltean 	refcount_set(&m->refcount, 1);
2649ccb6ed42SVladimir Oltean 	ocelot->mirror = m;
2650ccb6ed42SVladimir Oltean 
2651ccb6ed42SVladimir Oltean 	/* Program the mirror port to hardware */
2652ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS);
2653ccb6ed42SVladimir Oltean 
2654ccb6ed42SVladimir Oltean 	return m;
2655ccb6ed42SVladimir Oltean }
2656ccb6ed42SVladimir Oltean 
2657f2a0e216SVladimir Oltean void ocelot_mirror_put(struct ocelot *ocelot)
2658ccb6ed42SVladimir Oltean {
2659ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2660ccb6ed42SVladimir Oltean 
2661ccb6ed42SVladimir Oltean 	if (!refcount_dec_and_test(&m->refcount))
2662ccb6ed42SVladimir Oltean 		return;
2663ccb6ed42SVladimir Oltean 
2664ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_MIRRORPORTS);
2665ccb6ed42SVladimir Oltean 	ocelot->mirror = NULL;
2666ccb6ed42SVladimir Oltean 	kfree(m);
2667ccb6ed42SVladimir Oltean }
2668ccb6ed42SVladimir Oltean 
2669ccb6ed42SVladimir Oltean int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to,
2670ccb6ed42SVladimir Oltean 			   bool ingress, struct netlink_ext_ack *extack)
2671ccb6ed42SVladimir Oltean {
2672ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack);
2673ccb6ed42SVladimir Oltean 
2674ccb6ed42SVladimir Oltean 	if (IS_ERR(m))
2675ccb6ed42SVladimir Oltean 		return PTR_ERR(m);
2676ccb6ed42SVladimir Oltean 
2677ccb6ed42SVladimir Oltean 	if (ingress) {
2678ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2679ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2680ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2681ccb6ed42SVladimir Oltean 	} else {
2682ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, BIT(from), BIT(from),
2683ccb6ed42SVladimir Oltean 			   ANA_EMIRRORPORTS);
2684ccb6ed42SVladimir Oltean 	}
2685ccb6ed42SVladimir Oltean 
2686ccb6ed42SVladimir Oltean 	return 0;
2687ccb6ed42SVladimir Oltean }
2688ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_add);
2689ccb6ed42SVladimir Oltean 
2690ccb6ed42SVladimir Oltean void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress)
2691ccb6ed42SVladimir Oltean {
2692ccb6ed42SVladimir Oltean 	if (ingress) {
2693ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2694ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2695ccb6ed42SVladimir Oltean 	} else {
2696ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS);
2697ccb6ed42SVladimir Oltean 	}
2698ccb6ed42SVladimir Oltean 
2699ccb6ed42SVladimir Oltean 	ocelot_mirror_put(ocelot);
2700ccb6ed42SVladimir Oltean }
2701ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_del);
2702ccb6ed42SVladimir Oltean 
2703*aac80140SVladimir Oltean static void ocelot_port_reset_mqprio(struct ocelot *ocelot, int port)
2704*aac80140SVladimir Oltean {
2705*aac80140SVladimir Oltean 	struct net_device *dev = ocelot->ops->port_to_netdev(ocelot, port);
2706*aac80140SVladimir Oltean 
2707*aac80140SVladimir Oltean 	netdev_reset_tc(dev);
2708*aac80140SVladimir Oltean }
2709*aac80140SVladimir Oltean 
2710*aac80140SVladimir Oltean int ocelot_port_mqprio(struct ocelot *ocelot, int port,
2711*aac80140SVladimir Oltean 		       struct tc_mqprio_qopt_offload *mqprio)
2712*aac80140SVladimir Oltean {
2713*aac80140SVladimir Oltean 	struct net_device *dev = ocelot->ops->port_to_netdev(ocelot, port);
2714*aac80140SVladimir Oltean 	struct netlink_ext_ack *extack = mqprio->extack;
2715*aac80140SVladimir Oltean 	struct tc_mqprio_qopt *qopt = &mqprio->qopt;
2716*aac80140SVladimir Oltean 	int num_tc = qopt->num_tc;
2717*aac80140SVladimir Oltean 	int tc, err;
2718*aac80140SVladimir Oltean 
2719*aac80140SVladimir Oltean 	if (!num_tc) {
2720*aac80140SVladimir Oltean 		ocelot_port_reset_mqprio(ocelot, port);
2721*aac80140SVladimir Oltean 		return 0;
2722*aac80140SVladimir Oltean 	}
2723*aac80140SVladimir Oltean 
2724*aac80140SVladimir Oltean 	err = netdev_set_num_tc(dev, num_tc);
2725*aac80140SVladimir Oltean 	if (err)
2726*aac80140SVladimir Oltean 		return err;
2727*aac80140SVladimir Oltean 
2728*aac80140SVladimir Oltean 	for (tc = 0; tc < num_tc; tc++) {
2729*aac80140SVladimir Oltean 		if (qopt->count[tc] != 1) {
2730*aac80140SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
2731*aac80140SVladimir Oltean 					   "Only one TXQ per TC supported");
2732*aac80140SVladimir Oltean 			return -EINVAL;
2733*aac80140SVladimir Oltean 		}
2734*aac80140SVladimir Oltean 
2735*aac80140SVladimir Oltean 		err = netdev_set_tc_queue(dev, tc, 1, qopt->offset[tc]);
2736*aac80140SVladimir Oltean 		if (err)
2737*aac80140SVladimir Oltean 			goto err_reset_tc;
2738*aac80140SVladimir Oltean 	}
2739*aac80140SVladimir Oltean 
2740*aac80140SVladimir Oltean 	err = netif_set_real_num_tx_queues(dev, num_tc);
2741*aac80140SVladimir Oltean 	if (err)
2742*aac80140SVladimir Oltean 		goto err_reset_tc;
2743*aac80140SVladimir Oltean 
2744*aac80140SVladimir Oltean 	return 0;
2745*aac80140SVladimir Oltean 
2746*aac80140SVladimir Oltean err_reset_tc:
2747*aac80140SVladimir Oltean 	ocelot_port_reset_mqprio(ocelot, port);
2748*aac80140SVladimir Oltean 	return err;
2749*aac80140SVladimir Oltean }
2750*aac80140SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mqprio);
2751*aac80140SVladimir Oltean 
27525e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2753fa914e9cSVladimir Oltean {
2754fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2755fa914e9cSVladimir Oltean 
2756b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
275731350d7fSVladimir Oltean 
275831350d7fSVladimir Oltean 	/* Basic L2 initialization */
275931350d7fSVladimir Oltean 
27605bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
27615bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
27625bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
27635bc9d2e6SVladimir Oltean 	 */
27645bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
27655bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
27665bc9d2e6SVladimir Oltean 
27675bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
27685bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
27695bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
27705bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
27715bc9d2e6SVladimir Oltean 	mdelay(1);
27725bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
27735bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
27745bc9d2e6SVladimir Oltean 
27755bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2776a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
27775bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
27785bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2779a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
27805bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
27815bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
27825bc9d2e6SVladimir Oltean 
27835bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
27845bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
27855bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
27865bc9d2e6SVladimir Oltean 
2787e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2788541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2789e8e6e73dSVladimir Oltean 
279031350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
279131350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
279231350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
279331350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
279431350d7fSVladimir Oltean 
279531350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
279631350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
279731350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
279831350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
279931350d7fSVladimir Oltean 
2800421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2801421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2802421741eaSVladimir Oltean 
280346efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
280446efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
280546efe4efSVladimir Oltean 	 * automatic.
280646efe4efSVladimir Oltean 	 */
280746efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
280846efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
280946efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
281046efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
281146efe4efSVladimir Oltean 
281231350d7fSVladimir Oltean 	/* Enable vcap lookups */
281331350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
281431350d7fSVladimir Oltean }
28155e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
281631350d7fSVladimir Oltean 
28172d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
28182d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
28192d44b097SVladimir Oltean  * NPI mode is used).
282069df578cSVladimir Oltean  */
28212d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
282221468199SVladimir Oltean {
282369df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
282469df578cSVladimir Oltean 
282569df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
282621468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
282769df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
282869df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
282969df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
283069df578cSVladimir Oltean 	 */
283121468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
283221468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
283321468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
283421468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
283521468199SVladimir Oltean 
283669df578cSVladimir Oltean 	/* Enable CPU port module */
2837886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
283869df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2839886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2840cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2841886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2842cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
284321468199SVladimir Oltean 
284421468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
2845bfbab310SVladimir Oltean 	ocelot_write_gix(ocelot,
284654c31984SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) |
284721468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
284821468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
284921468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
285021468199SVladimir Oltean }
285121468199SVladimir Oltean 
2852f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2853f6fe01d6SVladimir Oltean {
2854f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2855f6fe01d6SVladimir Oltean 
2856f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2857f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2858f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2859f6fe01d6SVladimir Oltean 	 */
2860f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2861f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2862f6fe01d6SVladimir Oltean 
2863f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2864f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2865f6fe01d6SVladimir Oltean }
2866f6fe01d6SVladimir Oltean 
2867b67f5502SColin Foster static int ocelot_mem_init_status(struct ocelot *ocelot)
2868b67f5502SColin Foster {
2869b67f5502SColin Foster 	unsigned int val;
2870b67f5502SColin Foster 	int err;
2871b67f5502SColin Foster 
2872b67f5502SColin Foster 	err = regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
2873b67f5502SColin Foster 				&val);
2874b67f5502SColin Foster 
2875b67f5502SColin Foster 	return err ?: val;
2876b67f5502SColin Foster }
2877b67f5502SColin Foster 
2878b67f5502SColin Foster int ocelot_reset(struct ocelot *ocelot)
2879b67f5502SColin Foster {
2880b67f5502SColin Foster 	int err;
2881b67f5502SColin Foster 	u32 val;
2882b67f5502SColin Foster 
2883b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
2884b67f5502SColin Foster 	if (err)
2885b67f5502SColin Foster 		return err;
2886b67f5502SColin Foster 
2887b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
2888b67f5502SColin Foster 	if (err)
2889b67f5502SColin Foster 		return err;
2890b67f5502SColin Foster 
2891b67f5502SColin Foster 	/* MEM_INIT is a self-clearing bit. Wait for it to be cleared (should be
2892b67f5502SColin Foster 	 * 100us) before enabling the switch core.
2893b67f5502SColin Foster 	 */
2894b67f5502SColin Foster 	err = readx_poll_timeout(ocelot_mem_init_status, ocelot, val, !val,
2895b67f5502SColin Foster 				 MEM_INIT_SLEEP_US, MEM_INIT_TIMEOUT_US);
2896b67f5502SColin Foster 	if (err)
2897b67f5502SColin Foster 		return err;
2898b67f5502SColin Foster 
2899b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
2900b67f5502SColin Foster 	if (err)
2901b67f5502SColin Foster 		return err;
2902b67f5502SColin Foster 
2903b67f5502SColin Foster 	return regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
2904b67f5502SColin Foster }
2905b67f5502SColin Foster EXPORT_SYMBOL(ocelot_reset);
2906b67f5502SColin Foster 
2907a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2908a556c76aSAlexandre Belloni {
290921468199SVladimir Oltean 	int i, ret;
291021468199SVladimir Oltean 	u32 port;
2911a556c76aSAlexandre Belloni 
29123a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
29133a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
29143a77b593SVladimir Oltean 		if (ret) {
29153a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
29163a77b593SVladimir Oltean 			return ret;
29173a77b593SVladimir Oltean 		}
29183a77b593SVladimir Oltean 	}
29193a77b593SVladimir Oltean 
29204e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
29212468346cSVladimir Oltean 	mutex_init(&ocelot->mact_lock);
29228abe1970SVladimir Oltean 	mutex_init(&ocelot->fwd_domain_lock);
29238670dc33SXiaoliang Yang 	mutex_init(&ocelot->tas_lock);
29244e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
292552849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
2926a556c76aSAlexandre Belloni 
2927ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2928fe90104cSVladimir Oltean 	if (!ocelot->owq)
2929ca0b272bSVladimir Oltean 		return -ENOMEM;
2930fe90104cSVladimir Oltean 
2931fe90104cSVladimir Oltean 	ret = ocelot_stats_init(ocelot);
29326505b680SVladimir Oltean 	if (ret)
29336505b680SVladimir Oltean 		goto err_stats_init;
2934ca0b272bSVladimir Oltean 
29352b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2936e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
293790e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
2938961d8b69SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->lag_fdbs);
2939f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2940a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2941a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2942aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
29432d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2944a556c76aSAlexandre Belloni 
294523e2c506SXiaoliang Yang 	if (ocelot->ops->psfp_init)
294623e2c506SXiaoliang Yang 		ocelot->ops->psfp_init(ocelot);
294723e2c506SXiaoliang Yang 
29486505b680SVladimir Oltean 	if (ocelot->mm_supported) {
29496505b680SVladimir Oltean 		ret = ocelot_mm_init(ocelot);
29506505b680SVladimir Oltean 		if (ret)
29516505b680SVladimir Oltean 			goto err_mm_init;
29526505b680SVladimir Oltean 	}
29536505b680SVladimir Oltean 
2954a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2955a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2956a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2957a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2958a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2959a556c76aSAlexandre Belloni 	}
2960a556c76aSAlexandre Belloni 
2961a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2962a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2963a556c76aSAlexandre Belloni 
2964a556c76aSAlexandre Belloni 	/* Aggregation mode */
2965a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2966a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2967a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2968f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2969f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2970f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2971f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2972a556c76aSAlexandre Belloni 
2973a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2974a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2975a556c76aSAlexandre Belloni 	 */
2976a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2977a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2978a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2979a556c76aSAlexandre Belloni 
2980a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2981a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2982a556c76aSAlexandre Belloni 
2983a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2984a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2985a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2986a556c76aSAlexandre Belloni 
2987a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2988edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2989a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2990b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2991a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2992edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2993a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2994a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2995a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2996a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2997a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2998a556c76aSAlexandre Belloni 
2999a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3000a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
3001a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
3002a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
3003a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
3004a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
3005a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
3006a556c76aSAlexandre Belloni 				 port);
3007a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
3008a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
3009a556c76aSAlexandre Belloni 	}
3010a556c76aSAlexandre Belloni 
301196b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
3012a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
3013a556c76aSAlexandre Belloni 
3014a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
3015a556c76aSAlexandre Belloni 	}
3016ebb1bb40SHoratiu Vultur 
3017ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
3018ebb1bb40SHoratiu Vultur 
3019b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
3020b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3021b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3022a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
3023b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3024b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3025b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
3026a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
3027a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
3028a556c76aSAlexandre Belloni 
3029a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
3030a556c76aSAlexandre Belloni 	 * registers endianness.
3031a556c76aSAlexandre Belloni 	 */
3032a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
3033a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
3034a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
3035a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
3036a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
3037a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
3038a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
3039a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
3040a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
3041a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
3042a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
3043a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
3044a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
3045a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
3046a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
3047a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
3048a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
3049a556c76aSAlexandre Belloni 
3050a556c76aSAlexandre Belloni 	return 0;
30516505b680SVladimir Oltean 
30526505b680SVladimir Oltean err_mm_init:
30536505b680SVladimir Oltean 	ocelot_stats_deinit(ocelot);
30546505b680SVladimir Oltean err_stats_init:
30556505b680SVladimir Oltean 	destroy_workqueue(ocelot->owq);
30566505b680SVladimir Oltean 	return ret;
3057a556c76aSAlexandre Belloni }
3058a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
3059a556c76aSAlexandre Belloni 
3060a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
3061a556c76aSAlexandre Belloni {
3062fe90104cSVladimir Oltean 	ocelot_stats_deinit(ocelot);
3063ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
3064a556c76aSAlexandre Belloni }
3065a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
3066a556c76aSAlexandre Belloni 
3067e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
3068e5fb512dSVladimir Oltean {
3069e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3070e5fb512dSVladimir Oltean 
3071e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
3072e5fb512dSVladimir Oltean }
3073e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
3074e5fb512dSVladimir Oltean 
3075a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
3076