xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 55e43d6abd078ed6d219902ce8cb4d68e3c993ba)
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>
11aac80140SVladimir 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 */
ocelot_mact_read_macaccess(struct ocelot * ocelot)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 */
ocelot_mact_wait_for_completion(struct ocelot * ocelot)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 */
ocelot_mact_select(struct ocelot * ocelot,const unsigned char mac[ETH_ALEN],unsigned int vid)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 
__ocelot_mact_learn(struct ocelot * ocelot,int port,const unsigned char mac[ETH_ALEN],unsigned int vid,enum macaccess_entry_type type)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 
ocelot_mact_learn(struct ocelot * ocelot,int port,const unsigned char mac[ETH_ALEN],unsigned int vid,enum macaccess_entry_type type)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 
ocelot_mact_forget(struct ocelot * ocelot,const unsigned char mac[ETH_ALEN],unsigned int vid)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 
ocelot_mact_lookup(struct ocelot * ocelot,int * dst_idx,const unsigned char mac[ETH_ALEN],unsigned int vid,enum macaccess_entry_type * type)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 
ocelot_mact_learn_streamdata(struct ocelot * ocelot,int dst_idx,const unsigned char mac[ETH_ALEN],unsigned int vid,enum macaccess_entry_type type,int sfid,int ssid)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 
ocelot_mact_init(struct ocelot * ocelot)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 
ocelot_pll5_init(struct ocelot * ocelot)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 
ocelot_vcap_enable(struct ocelot * ocelot,int port)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 
ocelot_single_vlan_aware_bridge(struct ocelot * ocelot,struct netlink_ext_ack * extack)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 
ocelot_vlant_read_vlanaccess(struct ocelot * ocelot)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 
ocelot_vlant_wait_for_completion(struct ocelot * ocelot)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 
ocelot_vlant_set_mask(struct ocelot * ocelot,u16 vid,u32 mask)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 
ocelot_port_num_untagged_vlans(struct ocelot * ocelot,int port)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 
ocelot_port_num_tagged_vlans(struct ocelot * ocelot,int port)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  */
ocelot_port_uses_native_vlan(struct ocelot * ocelot,int port)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 *
ocelot_port_find_native_vlan(struct ocelot * ocelot,int port)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  */
ocelot_port_manage_port_tag(struct ocelot * ocelot,int port)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 
ocelot_bridge_num_find(struct ocelot * ocelot,const struct net_device * bridge)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 
ocelot_vlan_unaware_pvid(struct ocelot * ocelot,const struct net_device * bridge)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) */
ocelot_port_set_pvid(struct ocelot * ocelot,int port,const struct ocelot_bridge_vlan * pvid_vlan)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 
ocelot_bridge_vlan_find(struct ocelot * ocelot,u16 vid)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 
ocelot_vlan_member_add(struct ocelot * ocelot,int port,u16 vid,bool untagged)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 
ocelot_vlan_member_del(struct ocelot * ocelot,int port,u16 vid)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 
ocelot_add_vlan_unaware_pvid(struct ocelot * ocelot,int port,const struct net_device * bridge)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 
ocelot_del_vlan_unaware_pvid(struct ocelot * ocelot,int port,const struct net_device * bridge)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 
ocelot_port_vlan_filtering(struct ocelot * ocelot,int port,bool vlan_aware,struct netlink_ext_ack * extack)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 
ocelot_vlan_prepare(struct ocelot * ocelot,int port,u16 vid,bool pvid,bool untagged,struct netlink_ext_ack * extack)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 
ocelot_vlan_add(struct ocelot * ocelot,int port,u16 vid,bool pvid,bool untagged)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 
ocelot_vlan_del(struct ocelot * ocelot,int port,u16 vid)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 
ocelot_vlan_init(struct ocelot * ocelot)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 
ocelot_read_eq_avail(struct ocelot * ocelot,int port)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 
ocelot_port_flush(struct ocelot * ocelot,int port)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 
ocelot_port_configure_serdes(struct ocelot * ocelot,int port,struct device_node * portnp)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 
ocelot_phylink_mac_config(struct ocelot * ocelot,int port,unsigned int link_an_mode,const struct phylink_link_state * state)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 
ocelot_phylink_mac_link_down(struct ocelot * ocelot,int port,unsigned int link_an_mode,phy_interface_t interface,unsigned long quirks)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 
ocelot_phylink_mac_link_up(struct ocelot * ocelot,int port,struct phy_device * phydev,unsigned int link_an_mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause,unsigned long quirks)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);
1009403ffc2cSVladimir Oltean 		/* Workaround for hardware bug - FP doesn't work
1010403ffc2cSVladimir Oltean 		 * at all link speeds for all PHY modes. The function
1011403ffc2cSVladimir Oltean 		 * below also calls ocelot->ops->cut_through_fwd(),
1012403ffc2cSVladimir Oltean 		 * so we don't need to do it twice.
1013403ffc2cSVladimir Oltean 		 */
1014403ffc2cSVladimir Oltean 		ocelot_port_update_active_preemptible_tcs(ocelot, port);
10158abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
10168abe1970SVladimir Oltean 	}
10178abe1970SVladimir Oltean 
1018a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
1019886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
1020886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
1021a556c76aSAlexandre Belloni }
1022e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
1023889b8950SVladimir Oltean 
ocelot_rx_frame_word(struct ocelot * ocelot,u8 grp,bool ifh,u32 * rval)1024924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
1025924ee317SVladimir Oltean 				u32 *rval)
1026924ee317SVladimir Oltean {
1027924ee317SVladimir Oltean 	u32 bytes_valid, val;
1028924ee317SVladimir Oltean 
1029924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1030924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
1031924ee317SVladimir Oltean 		if (ifh)
1032924ee317SVladimir Oltean 			return -EIO;
1033924ee317SVladimir Oltean 
1034924ee317SVladimir Oltean 		do {
1035924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1036924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
1037924ee317SVladimir Oltean 	}
1038924ee317SVladimir Oltean 
1039924ee317SVladimir Oltean 	switch (val) {
1040924ee317SVladimir Oltean 	case XTR_ABORT:
1041924ee317SVladimir Oltean 		return -EIO;
1042924ee317SVladimir Oltean 	case XTR_EOF_0:
1043924ee317SVladimir Oltean 	case XTR_EOF_1:
1044924ee317SVladimir Oltean 	case XTR_EOF_2:
1045924ee317SVladimir Oltean 	case XTR_EOF_3:
1046924ee317SVladimir Oltean 	case XTR_PRUNED:
1047924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
1048924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1049924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
1050924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1051924ee317SVladimir Oltean 		else
1052924ee317SVladimir Oltean 			*rval = val;
1053924ee317SVladimir Oltean 
1054924ee317SVladimir Oltean 		return bytes_valid;
1055924ee317SVladimir Oltean 	case XTR_ESCAPE:
1056924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1057924ee317SVladimir Oltean 
1058924ee317SVladimir Oltean 		return 4;
1059924ee317SVladimir Oltean 	default:
1060924ee317SVladimir Oltean 		*rval = val;
1061924ee317SVladimir Oltean 
1062924ee317SVladimir Oltean 		return 4;
1063924ee317SVladimir Oltean 	}
1064924ee317SVladimir Oltean }
1065924ee317SVladimir Oltean 
ocelot_xtr_poll_xfh(struct ocelot * ocelot,int grp,u32 * xfh)1066924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
1067924ee317SVladimir Oltean {
1068924ee317SVladimir Oltean 	int i, err = 0;
1069924ee317SVladimir Oltean 
1070924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
1071924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
1072924ee317SVladimir Oltean 		if (err != 4)
1073924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
1074924ee317SVladimir Oltean 	}
1075924ee317SVladimir Oltean 
1076924ee317SVladimir Oltean 	return 0;
1077924ee317SVladimir Oltean }
1078924ee317SVladimir Oltean 
ocelot_ptp_rx_timestamp(struct ocelot * ocelot,struct sk_buff * skb,u64 timestamp)1079b471a71eSClément Léger void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
1080b471a71eSClément Léger 			     u64 timestamp)
1081924ee317SVladimir Oltean {
1082924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
10832ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
1084b471a71eSClément Léger 	struct timespec64 ts;
1085b471a71eSClément Léger 
1086b471a71eSClément Léger 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1087b471a71eSClément Léger 
1088b471a71eSClément Léger 	tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1089b471a71eSClément Léger 	if ((tod_in_ns & 0xffffffff) < timestamp)
1090b471a71eSClément Léger 		full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1091b471a71eSClément Léger 				timestamp;
1092b471a71eSClément Léger 	else
1093b471a71eSClément Léger 		full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1094b471a71eSClément Léger 				timestamp;
1095b471a71eSClément Léger 
1096b471a71eSClément Léger 	shhwtstamps = skb_hwtstamps(skb);
1097b471a71eSClément Léger 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1098b471a71eSClément Léger 	shhwtstamps->hwtstamp = full_ts_in_ns;
1099b471a71eSClément Léger }
1100b471a71eSClément Léger EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
1101b471a71eSClément Léger 
ocelot_lock_inj_grp(struct ocelot * ocelot,int grp)1102e83b49ecSVladimir Oltean void ocelot_lock_inj_grp(struct ocelot *ocelot, int grp)
1103e83b49ecSVladimir Oltean 			 __acquires(&ocelot->inj_lock)
1104e83b49ecSVladimir Oltean {
1105e83b49ecSVladimir Oltean 	spin_lock(&ocelot->inj_lock);
1106e83b49ecSVladimir Oltean }
1107e83b49ecSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lock_inj_grp);
1108e83b49ecSVladimir Oltean 
ocelot_unlock_inj_grp(struct ocelot * ocelot,int grp)1109e83b49ecSVladimir Oltean void ocelot_unlock_inj_grp(struct ocelot *ocelot, int grp)
1110e83b49ecSVladimir Oltean 			   __releases(&ocelot->inj_lock)
1111e83b49ecSVladimir Oltean {
1112e83b49ecSVladimir Oltean 	spin_unlock(&ocelot->inj_lock);
1113e83b49ecSVladimir Oltean }
1114e83b49ecSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_unlock_inj_grp);
1115e83b49ecSVladimir Oltean 
ocelot_lock_xtr_grp(struct ocelot * ocelot,int grp)1116e83b49ecSVladimir Oltean void ocelot_lock_xtr_grp(struct ocelot *ocelot, int grp)
1117e83b49ecSVladimir Oltean 			 __acquires(&ocelot->inj_lock)
1118e83b49ecSVladimir Oltean {
1119e83b49ecSVladimir Oltean 	spin_lock(&ocelot->inj_lock);
1120e83b49ecSVladimir Oltean }
1121e83b49ecSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lock_xtr_grp);
1122e83b49ecSVladimir Oltean 
ocelot_unlock_xtr_grp(struct ocelot * ocelot,int grp)1123e83b49ecSVladimir Oltean void ocelot_unlock_xtr_grp(struct ocelot *ocelot, int grp)
1124e83b49ecSVladimir Oltean 			   __releases(&ocelot->inj_lock)
1125e83b49ecSVladimir Oltean {
1126e83b49ecSVladimir Oltean 	spin_unlock(&ocelot->inj_lock);
1127e83b49ecSVladimir Oltean }
1128e83b49ecSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_unlock_xtr_grp);
1129e83b49ecSVladimir Oltean 
ocelot_lock_xtr_grp_bh(struct ocelot * ocelot,int grp)1130e83b49ecSVladimir Oltean void ocelot_lock_xtr_grp_bh(struct ocelot *ocelot, int grp)
1131e83b49ecSVladimir Oltean 			    __acquires(&ocelot->xtr_lock)
1132e83b49ecSVladimir Oltean {
1133e83b49ecSVladimir Oltean 	spin_lock_bh(&ocelot->xtr_lock);
1134e83b49ecSVladimir Oltean }
1135e83b49ecSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lock_xtr_grp_bh);
1136e83b49ecSVladimir Oltean 
ocelot_unlock_xtr_grp_bh(struct ocelot * ocelot,int grp)1137e83b49ecSVladimir Oltean void ocelot_unlock_xtr_grp_bh(struct ocelot *ocelot, int grp)
1138e83b49ecSVladimir Oltean 			      __releases(&ocelot->xtr_lock)
1139e83b49ecSVladimir Oltean {
1140e83b49ecSVladimir Oltean 	spin_unlock_bh(&ocelot->xtr_lock);
1141e83b49ecSVladimir Oltean }
1142e83b49ecSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_unlock_xtr_grp_bh);
1143e83b49ecSVladimir Oltean 
ocelot_xtr_poll_frame(struct ocelot * ocelot,int grp,struct sk_buff ** nskb)1144b471a71eSClément Léger int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
1145b471a71eSClément Léger {
1146924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
1147924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
1148924ee317SVladimir Oltean 	struct net_device *dev;
1149924ee317SVladimir Oltean 	struct sk_buff *skb;
1150924ee317SVladimir Oltean 	int sz, buf_len;
1151924ee317SVladimir Oltean 	u32 val, *buf;
1152924ee317SVladimir Oltean 	int err;
1153924ee317SVladimir Oltean 
1154e83b49ecSVladimir Oltean 	lockdep_assert_held(&ocelot->xtr_lock);
1155e83b49ecSVladimir Oltean 
1156924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1157924ee317SVladimir Oltean 	if (err)
1158924ee317SVladimir Oltean 		return err;
1159924ee317SVladimir Oltean 
1160924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
1161924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
1162924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1163924ee317SVladimir Oltean 
1164924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1165924ee317SVladimir Oltean 		return -EINVAL;
1166924ee317SVladimir Oltean 
1167924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1168924ee317SVladimir Oltean 	if (!dev)
1169924ee317SVladimir Oltean 		return -EINVAL;
1170924ee317SVladimir Oltean 
1171924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
1172924ee317SVladimir Oltean 	if (unlikely(!skb)) {
1173924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
1174924ee317SVladimir Oltean 		return -ENOMEM;
1175924ee317SVladimir Oltean 	}
1176924ee317SVladimir Oltean 
1177924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
1178924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
1179924ee317SVladimir Oltean 
1180924ee317SVladimir Oltean 	len = 0;
1181924ee317SVladimir Oltean 	do {
1182924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1183924ee317SVladimir Oltean 		if (sz < 0) {
1184924ee317SVladimir Oltean 			err = sz;
1185924ee317SVladimir Oltean 			goto out_free_skb;
1186924ee317SVladimir Oltean 		}
1187924ee317SVladimir Oltean 		*buf++ = val;
1188924ee317SVladimir Oltean 		len += sz;
1189924ee317SVladimir Oltean 	} while (len < buf_len);
1190924ee317SVladimir Oltean 
1191924ee317SVladimir Oltean 	/* Read the FCS */
1192924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1193924ee317SVladimir Oltean 	if (sz < 0) {
1194924ee317SVladimir Oltean 		err = sz;
1195924ee317SVladimir Oltean 		goto out_free_skb;
1196924ee317SVladimir Oltean 	}
1197924ee317SVladimir Oltean 
1198924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1199924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1200924ee317SVladimir Oltean 
1201924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1202924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1203924ee317SVladimir Oltean 		*buf = val;
1204924ee317SVladimir Oltean 	}
1205924ee317SVladimir Oltean 
1206b471a71eSClément Léger 	if (ocelot->ptp)
1207b471a71eSClément Léger 		ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
1208924ee317SVladimir Oltean 
1209924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1210924ee317SVladimir Oltean 	 * has already been forwarded.
1211924ee317SVladimir Oltean 	 */
1212df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1213924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1214924ee317SVladimir Oltean 
1215924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1216d8ea7ff3SHoratiu Vultur 
1217924ee317SVladimir Oltean 	*nskb = skb;
1218924ee317SVladimir Oltean 
1219924ee317SVladimir Oltean 	return 0;
1220924ee317SVladimir Oltean 
1221924ee317SVladimir Oltean out_free_skb:
1222924ee317SVladimir Oltean 	kfree_skb(skb);
1223924ee317SVladimir Oltean 	return err;
1224924ee317SVladimir Oltean }
1225924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1226924ee317SVladimir Oltean 
ocelot_can_inject(struct ocelot * ocelot,int grp)1227137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1228137ffbc4SVladimir Oltean {
1229137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1230137ffbc4SVladimir Oltean 
1231e83b49ecSVladimir Oltean 	lockdep_assert_held(&ocelot->inj_lock);
1232e83b49ecSVladimir Oltean 
1233137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1234137ffbc4SVladimir Oltean 		return false;
1235137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1236137ffbc4SVladimir Oltean 		return false;
1237137ffbc4SVladimir Oltean 
1238137ffbc4SVladimir Oltean 	return true;
1239137ffbc4SVladimir Oltean }
1240137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1241137ffbc4SVladimir Oltean 
1242dd17e1e6SVladimir Oltean /**
1243dd17e1e6SVladimir Oltean  * ocelot_ifh_set_basic - Set basic information in Injection Frame Header
1244dd17e1e6SVladimir Oltean  * @ifh: Pointer to Injection Frame Header memory
1245dd17e1e6SVladimir Oltean  * @ocelot: Switch private data structure
1246dd17e1e6SVladimir Oltean  * @port: Egress port number
1247dd17e1e6SVladimir Oltean  * @rew_op: Egress rewriter operation for PTP
1248dd17e1e6SVladimir Oltean  * @skb: Pointer to socket buffer (packet)
1249dd17e1e6SVladimir Oltean  *
1250dd17e1e6SVladimir Oltean  * Populate the Injection Frame Header with basic information for this skb: the
1251dd17e1e6SVladimir Oltean  * analyzer bypass bit, destination port, VLAN info, egress rewriter info.
1252dd17e1e6SVladimir Oltean  */
ocelot_ifh_set_basic(void * ifh,struct ocelot * ocelot,int port,u32 rew_op,struct sk_buff * skb)1253dd17e1e6SVladimir Oltean void ocelot_ifh_set_basic(void *ifh, struct ocelot *ocelot, int port,
1254dd17e1e6SVladimir Oltean 			  u32 rew_op, struct sk_buff *skb)
1255e5150f00SClément Léger {
1256dd17e1e6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1257ff7f554bSVladimir Oltean 	struct net_device *dev = skb->dev;
1258dd17e1e6SVladimir Oltean 	u64 vlan_tci, tag_type;
1259ff7f554bSVladimir Oltean 	int qos_class;
1260dd17e1e6SVladimir Oltean 
1261dd17e1e6SVladimir Oltean 	ocelot_xmit_get_vlan_info(skb, ocelot_port->bridge, &vlan_tci,
1262dd17e1e6SVladimir Oltean 				  &tag_type);
1263dd17e1e6SVladimir Oltean 
1264ff7f554bSVladimir Oltean 	qos_class = netdev_get_num_tc(dev) ?
1265ff7f554bSVladimir Oltean 		    netdev_get_prio_tc_map(dev, skb->priority) : skb->priority;
1266ff7f554bSVladimir Oltean 
1267ff7f554bSVladimir Oltean 	memset(ifh, 0, OCELOT_TAG_LEN);
1268e5150f00SClément Léger 	ocelot_ifh_set_bypass(ifh, 1);
1269*2f3c62ffSVladimir Oltean 	ocelot_ifh_set_src(ifh, ocelot->num_phys_ports);
1270e5150f00SClément Léger 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1271ff7f554bSVladimir Oltean 	ocelot_ifh_set_qos_class(ifh, qos_class);
1272dd17e1e6SVladimir Oltean 	ocelot_ifh_set_tag_type(ifh, tag_type);
1273dd17e1e6SVladimir Oltean 	ocelot_ifh_set_vlan_tci(ifh, vlan_tci);
1274e5150f00SClément Léger 	if (rew_op)
1275e5150f00SClément Léger 		ocelot_ifh_set_rew_op(ifh, rew_op);
1276e5150f00SClément Léger }
1277dd17e1e6SVladimir Oltean EXPORT_SYMBOL(ocelot_ifh_set_basic);
1278e5150f00SClément Léger 
ocelot_port_inject_frame(struct ocelot * ocelot,int port,int grp,u32 rew_op,struct sk_buff * skb)1279137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1280137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1281137ffbc4SVladimir Oltean {
1282ff7f554bSVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4];
1283137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1284137ffbc4SVladimir Oltean 
1285e83b49ecSVladimir Oltean 	lockdep_assert_held(&ocelot->inj_lock);
1286e83b49ecSVladimir Oltean 
1287137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1288137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1289137ffbc4SVladimir Oltean 
1290dd17e1e6SVladimir Oltean 	ocelot_ifh_set_basic(ifh, ocelot, port, rew_op, skb);
1291137ffbc4SVladimir Oltean 
1292137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
129340d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1294137ffbc4SVladimir Oltean 
1295137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1296137ffbc4SVladimir Oltean 	last = skb->len % 4;
1297137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1298137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1299137ffbc4SVladimir Oltean 
1300137ffbc4SVladimir Oltean 	/* Add padding */
1301137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1302137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1303137ffbc4SVladimir Oltean 		i++;
1304137ffbc4SVladimir Oltean 	}
1305137ffbc4SVladimir Oltean 
1306137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1307137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1308137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1309137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1310137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1311137ffbc4SVladimir Oltean 
1312137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1313137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1314137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1315137ffbc4SVladimir Oltean 
1316137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1317137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1318137ffbc4SVladimir Oltean }
1319137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1320137ffbc4SVladimir Oltean 
ocelot_drain_cpu_queue(struct ocelot * ocelot,int grp)13210a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
13220a6f17c6SVladimir Oltean {
1323e83b49ecSVladimir Oltean 	lockdep_assert_held(&ocelot->xtr_lock);
1324e83b49ecSVladimir Oltean 
13250a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
13260a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
13270a6f17c6SVladimir Oltean }
13280a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
13290a6f17c6SVladimir Oltean 
ocelot_fdb_add(struct ocelot * ocelot,int port,const unsigned char * addr,u16 vid,const struct net_device * bridge)133054c31984SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, const unsigned char *addr,
133154c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1332a556c76aSAlexandre Belloni {
133354c31984SVladimir Oltean 	if (!vid)
133454c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
133554c31984SVladimir Oltean 
1336e9b3ba43SVladimir Oltean 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
1337a556c76aSAlexandre Belloni }
13385e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1339a556c76aSAlexandre Belloni 
ocelot_fdb_del(struct ocelot * ocelot,int port,const unsigned char * addr,u16 vid,const struct net_device * bridge)134054c31984SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, const unsigned char *addr,
134154c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1342531ee1a6SVladimir Oltean {
134354c31984SVladimir Oltean 	if (!vid)
134454c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
134554c31984SVladimir Oltean 
1346531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1347531ee1a6SVladimir Oltean }
13485e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1349531ee1a6SVladimir Oltean 
13502468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
ocelot_mact_read(struct ocelot * ocelot,int port,int row,int col,struct ocelot_mact_entry * entry)1351531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1352a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1353a556c76aSAlexandre Belloni {
1354a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1355531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1356a556c76aSAlexandre Belloni 
1357a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1358a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1359a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1360a556c76aSAlexandre Belloni 
1361a556c76aSAlexandre Belloni 	/* Issue a read command */
1362a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1363a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1364a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1365a556c76aSAlexandre Belloni 
1366a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1367a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1368a556c76aSAlexandre Belloni 
1369a556c76aSAlexandre Belloni 	/* Read the entry flags */
1370a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1371a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1372a556c76aSAlexandre Belloni 		return -EINVAL;
1373a556c76aSAlexandre Belloni 
1374a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1375a556c76aSAlexandre Belloni 	 * do not report it.
1376a556c76aSAlexandre Belloni 	 */
1377a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1378531ee1a6SVladimir Oltean 	if (dst != port)
1379a556c76aSAlexandre Belloni 		return -EINVAL;
1380a556c76aSAlexandre Belloni 
1381a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1382a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1383a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1384a556c76aSAlexandre Belloni 
1385a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1386a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1387a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1388a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1389a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1390a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1391a556c76aSAlexandre Belloni 
1392a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1393a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1394a556c76aSAlexandre Belloni 
1395a556c76aSAlexandre Belloni 	return 0;
1396a556c76aSAlexandre Belloni }
1397a556c76aSAlexandre Belloni 
ocelot_mact_flush(struct ocelot * ocelot,int port)13985cad43a5SVladimir Oltean int ocelot_mact_flush(struct ocelot *ocelot, int port)
13995cad43a5SVladimir Oltean {
14005cad43a5SVladimir Oltean 	int err;
14015cad43a5SVladimir Oltean 
14025cad43a5SVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
14035cad43a5SVladimir Oltean 
14045cad43a5SVladimir Oltean 	/* Program ageing filter for a single port */
14055cad43a5SVladimir Oltean 	ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
14065cad43a5SVladimir Oltean 		     ANA_ANAGEFIL);
14075cad43a5SVladimir Oltean 
14085cad43a5SVladimir Oltean 	/* Flushing dynamic FDB entries requires two successive age scans */
14095cad43a5SVladimir Oltean 	ocelot_write(ocelot,
14105cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
14115cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
14125cad43a5SVladimir Oltean 
14135cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
14145cad43a5SVladimir Oltean 	if (err) {
14155cad43a5SVladimir Oltean 		mutex_unlock(&ocelot->mact_lock);
14165cad43a5SVladimir Oltean 		return err;
14175cad43a5SVladimir Oltean 	}
14185cad43a5SVladimir Oltean 
14195cad43a5SVladimir Oltean 	/* And second... */
14205cad43a5SVladimir Oltean 	ocelot_write(ocelot,
14215cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
14225cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
14235cad43a5SVladimir Oltean 
14245cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
14255cad43a5SVladimir Oltean 
14265cad43a5SVladimir Oltean 	/* Restore ageing filter */
14275cad43a5SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_ANAGEFIL);
14285cad43a5SVladimir Oltean 
14295cad43a5SVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
14305cad43a5SVladimir Oltean 
14315cad43a5SVladimir Oltean 	return err;
14325cad43a5SVladimir Oltean }
14335cad43a5SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_mact_flush);
14345cad43a5SVladimir Oltean 
ocelot_fdb_dump(struct ocelot * ocelot,int port,dsa_fdb_dump_cb_t * cb,void * data)14355e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1436531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1437a556c76aSAlexandre Belloni {
14382468346cSVladimir Oltean 	int err = 0;
1439531ee1a6SVladimir Oltean 	int i, j;
1440a556c76aSAlexandre Belloni 
14412468346cSVladimir Oltean 	/* We could take the lock just around ocelot_mact_read, but doing so
14422468346cSVladimir Oltean 	 * thousands of times in a row seems rather pointless and inefficient.
14432468346cSVladimir Oltean 	 */
14442468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
14452468346cSVladimir Oltean 
144621ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
144721ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1448a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1449531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1450531ee1a6SVladimir Oltean 			bool is_static;
1451531ee1a6SVladimir Oltean 
14522468346cSVladimir Oltean 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1453a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1454a556c76aSAlexandre Belloni 			 * skip it.
1455a556c76aSAlexandre Belloni 			 */
14562468346cSVladimir Oltean 			if (err == -EINVAL)
1457a556c76aSAlexandre Belloni 				continue;
14582468346cSVladimir Oltean 			else if (err)
14592468346cSVladimir Oltean 				break;
1460a556c76aSAlexandre Belloni 
1461531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1462531ee1a6SVladimir Oltean 
146354c31984SVladimir Oltean 			/* Hide the reserved VLANs used for
146454c31984SVladimir Oltean 			 * VLAN-unaware bridging.
146554c31984SVladimir Oltean 			 */
146654c31984SVladimir Oltean 			if (entry.vid > OCELOT_RSV_VLAN_RANGE_START)
146754c31984SVladimir Oltean 				entry.vid = 0;
146854c31984SVladimir Oltean 
14692468346cSVladimir Oltean 			err = cb(entry.mac, entry.vid, is_static, data);
14702468346cSVladimir Oltean 			if (err)
14712468346cSVladimir Oltean 				break;
1472a556c76aSAlexandre Belloni 		}
1473a556c76aSAlexandre Belloni 	}
1474a556c76aSAlexandre Belloni 
14752468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
14762468346cSVladimir Oltean 
14772468346cSVladimir Oltean 	return err;
1478531ee1a6SVladimir Oltean }
14795e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1480531ee1a6SVladimir Oltean 
ocelot_trap_add(struct ocelot * ocelot,int port,unsigned long cookie,bool take_ts,void (* populate)(struct ocelot_vcap_filter * f))14819d75b881SVladimir Oltean int ocelot_trap_add(struct ocelot *ocelot, int port,
14829d75b881SVladimir Oltean 		    unsigned long cookie, bool take_ts,
148396ca08c0SVladimir Oltean 		    void (*populate)(struct ocelot_vcap_filter *f))
148496ca08c0SVladimir Oltean {
148596ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
148696ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
148796ca08c0SVladimir Oltean 	bool new = false;
148896ca08c0SVladimir Oltean 	int err;
148996ca08c0SVladimir Oltean 
149096ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
149196ca08c0SVladimir Oltean 
149296ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
149396ca08c0SVladimir Oltean 						   false);
149496ca08c0SVladimir Oltean 	if (!trap) {
149596ca08c0SVladimir Oltean 		trap = kzalloc(sizeof(*trap), GFP_KERNEL);
149696ca08c0SVladimir Oltean 		if (!trap)
149796ca08c0SVladimir Oltean 			return -ENOMEM;
149896ca08c0SVladimir Oltean 
149996ca08c0SVladimir Oltean 		populate(trap);
150096ca08c0SVladimir Oltean 		trap->prio = 1;
150196ca08c0SVladimir Oltean 		trap->id.cookie = cookie;
150296ca08c0SVladimir Oltean 		trap->id.tc_offload = false;
150396ca08c0SVladimir Oltean 		trap->block_id = VCAP_IS2;
150496ca08c0SVladimir Oltean 		trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
150596ca08c0SVladimir Oltean 		trap->lookup = 0;
150696ca08c0SVladimir Oltean 		trap->action.cpu_copy_ena = true;
150796ca08c0SVladimir Oltean 		trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
150896ca08c0SVladimir Oltean 		trap->action.port_mask = 0;
15099d75b881SVladimir Oltean 		trap->take_ts = take_ts;
1510e1846cffSVladimir Oltean 		trap->is_trap = true;
151196ca08c0SVladimir Oltean 		new = true;
151296ca08c0SVladimir Oltean 	}
151396ca08c0SVladimir Oltean 
151496ca08c0SVladimir Oltean 	trap->ingress_port_mask |= BIT(port);
151596ca08c0SVladimir Oltean 
151696ca08c0SVladimir Oltean 	if (new)
151796ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_add(ocelot, trap, NULL);
151896ca08c0SVladimir Oltean 	else
151996ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
152096ca08c0SVladimir Oltean 	if (err) {
152196ca08c0SVladimir Oltean 		trap->ingress_port_mask &= ~BIT(port);
1522e1846cffSVladimir Oltean 		if (!trap->ingress_port_mask)
152396ca08c0SVladimir Oltean 			kfree(trap);
152496ca08c0SVladimir Oltean 		return err;
152596ca08c0SVladimir Oltean 	}
152696ca08c0SVladimir Oltean 
152796ca08c0SVladimir Oltean 	return 0;
152896ca08c0SVladimir Oltean }
152996ca08c0SVladimir Oltean 
ocelot_trap_del(struct ocelot * ocelot,int port,unsigned long cookie)1530b9bace6eSVladimir Oltean int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie)
153196ca08c0SVladimir Oltean {
153296ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
153396ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
153496ca08c0SVladimir Oltean 
153596ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
153696ca08c0SVladimir Oltean 
153796ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
153896ca08c0SVladimir Oltean 						   false);
153996ca08c0SVladimir Oltean 	if (!trap)
154096ca08c0SVladimir Oltean 		return 0;
154196ca08c0SVladimir Oltean 
154296ca08c0SVladimir Oltean 	trap->ingress_port_mask &= ~BIT(port);
1543e1846cffSVladimir Oltean 	if (!trap->ingress_port_mask)
154496ca08c0SVladimir Oltean 		return ocelot_vcap_filter_del(ocelot, trap);
154596ca08c0SVladimir Oltean 
154696ca08c0SVladimir Oltean 	return ocelot_vcap_filter_replace(ocelot, trap);
154796ca08c0SVladimir Oltean }
154896ca08c0SVladimir Oltean 
ocelot_get_bond_mask(struct ocelot * ocelot,struct net_device * bond)1549a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
1550b80af659SVladimir Oltean {
1551b80af659SVladimir Oltean 	u32 mask = 0;
1552b80af659SVladimir Oltean 	int port;
1553b80af659SVladimir Oltean 
1554961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
1555961d8b69SVladimir Oltean 
1556b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1557b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1558b80af659SVladimir Oltean 
1559b80af659SVladimir Oltean 		if (!ocelot_port)
1560b80af659SVladimir Oltean 			continue;
1561b80af659SVladimir Oltean 
1562a14e6b69SVladimir Oltean 		if (ocelot_port->bond == bond)
1563b80af659SVladimir Oltean 			mask |= BIT(port);
1564b80af659SVladimir Oltean 	}
1565b80af659SVladimir Oltean 
1566b80af659SVladimir Oltean 	return mask;
1567b80af659SVladimir Oltean }
1568b80af659SVladimir Oltean 
1569961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical
1570961d8b69SVladimir Oltean  * port ID present in that LAG. It may change if that port ever leaves the LAG.
1571961d8b69SVladimir Oltean  */
ocelot_bond_get_id(struct ocelot * ocelot,struct net_device * bond)1572eca70102SVladimir Oltean int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond)
1573961d8b69SVladimir Oltean {
1574961d8b69SVladimir Oltean 	int bond_mask = ocelot_get_bond_mask(ocelot, bond);
1575961d8b69SVladimir Oltean 
1576961d8b69SVladimir Oltean 	if (!bond_mask)
1577961d8b69SVladimir Oltean 		return -ENOENT;
1578961d8b69SVladimir Oltean 
1579961d8b69SVladimir Oltean 	return __ffs(bond_mask);
1580961d8b69SVladimir Oltean }
1581eca70102SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bond_get_id);
1582961d8b69SVladimir Oltean 
1583291ac151SVladimir Oltean /* Returns the mask of user ports assigned to this DSA tag_8021q CPU port.
1584291ac151SVladimir Oltean  * Note that when CPU ports are in a LAG, the user ports are assigned to the
1585291ac151SVladimir Oltean  * 'primary' CPU port, the one whose physical port number gives the logical
1586291ac151SVladimir Oltean  * port number of the LAG.
1587291ac151SVladimir Oltean  *
1588291ac151SVladimir Oltean  * We leave PGID_SRC poorly configured for the 'secondary' CPU port in the LAG
1589291ac151SVladimir Oltean  * (to which no user port is assigned), but it appears that forwarding from
1590291ac151SVladimir Oltean  * this secondary CPU port looks at the PGID_SRC associated with the logical
1591291ac151SVladimir Oltean  * port ID that it's assigned to, which *is* configured properly.
1592291ac151SVladimir Oltean  */
ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot * ocelot,struct ocelot_port * cpu)1593c295f983SVladimir Oltean static u32 ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot *ocelot,
1594c295f983SVladimir Oltean 					       struct ocelot_port *cpu)
1595c295f983SVladimir Oltean {
1596c295f983SVladimir Oltean 	u32 mask = 0;
1597c295f983SVladimir Oltean 	int port;
1598c295f983SVladimir Oltean 
1599c295f983SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1600c295f983SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1601c295f983SVladimir Oltean 
1602c295f983SVladimir Oltean 		if (!ocelot_port)
1603c295f983SVladimir Oltean 			continue;
1604c295f983SVladimir Oltean 
1605c295f983SVladimir Oltean 		if (ocelot_port->dsa_8021q_cpu == cpu)
1606c295f983SVladimir Oltean 			mask |= BIT(port);
1607c295f983SVladimir Oltean 	}
1608c295f983SVladimir Oltean 
1609291ac151SVladimir Oltean 	if (cpu->bond)
1610291ac151SVladimir Oltean 		mask &= ~ocelot_get_bond_mask(ocelot, cpu->bond);
1611291ac151SVladimir Oltean 
1612c295f983SVladimir Oltean 	return mask;
1613c295f983SVladimir Oltean }
1614c295f983SVladimir Oltean 
1615291ac151SVladimir Oltean /* Returns the DSA tag_8021q CPU port that the given port is assigned to,
1616291ac151SVladimir Oltean  * or the bit mask of CPU ports if said CPU port is in a LAG.
1617291ac151SVladimir Oltean  */
ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot * ocelot,int port)1618c295f983SVladimir Oltean u32 ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot *ocelot, int port)
1619c295f983SVladimir Oltean {
1620c295f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1621c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot_port->dsa_8021q_cpu;
1622c295f983SVladimir Oltean 
1623c295f983SVladimir Oltean 	if (!cpu_port)
1624c295f983SVladimir Oltean 		return 0;
1625c295f983SVladimir Oltean 
1626291ac151SVladimir Oltean 	if (cpu_port->bond)
1627291ac151SVladimir Oltean 		return ocelot_get_bond_mask(ocelot, cpu_port->bond);
1628291ac151SVladimir Oltean 
1629c295f983SVladimir Oltean 	return BIT(cpu_port->index);
1630c295f983SVladimir Oltean }
1631c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assigned_dsa_8021q_cpu_mask);
1632c295f983SVladimir Oltean 
ocelot_get_bridge_fwd_mask(struct ocelot * ocelot,int src_port)16338abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
1634df291e54SVladimir Oltean {
1635acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1636a8bd9fa5SVladimir Oltean 	const struct net_device *bridge;
1637df291e54SVladimir Oltean 	u32 mask = 0;
1638df291e54SVladimir Oltean 	int port;
1639df291e54SVladimir Oltean 
1640a8bd9fa5SVladimir Oltean 	if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
1641a8bd9fa5SVladimir Oltean 		return 0;
1642a8bd9fa5SVladimir Oltean 
1643a8bd9fa5SVladimir Oltean 	bridge = ocelot_port->bridge;
1644a8bd9fa5SVladimir Oltean 	if (!bridge)
1645acc64f52SVladimir Oltean 		return 0;
1646acc64f52SVladimir Oltean 
1647df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1648acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1649df291e54SVladimir Oltean 
1650df291e54SVladimir Oltean 		if (!ocelot_port)
1651df291e54SVladimir Oltean 			continue;
1652df291e54SVladimir Oltean 
1653df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1654df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1655df291e54SVladimir Oltean 			mask |= BIT(port);
1656df291e54SVladimir Oltean 	}
1657df291e54SVladimir Oltean 
1658df291e54SVladimir Oltean 	return mask;
1659df291e54SVladimir Oltean }
16608abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
1661df291e54SVladimir Oltean 
ocelot_apply_bridge_fwd_mask(struct ocelot * ocelot,bool joining)1662a72e23ddSVladimir Oltean static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
1663e21268efSVladimir Oltean {
1664e21268efSVladimir Oltean 	int port;
1665e21268efSVladimir Oltean 
16668abe1970SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
16678abe1970SVladimir Oltean 
16688abe1970SVladimir Oltean 	/* If cut-through forwarding is supported, update the masks before a
16698abe1970SVladimir Oltean 	 * port joins the forwarding domain, to avoid potential underruns if it
16708abe1970SVladimir Oltean 	 * has the highest speed from the new domain.
16718abe1970SVladimir Oltean 	 */
16728abe1970SVladimir Oltean 	if (joining && ocelot->ops->cut_through_fwd)
16738abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
16748abe1970SVladimir Oltean 
16759b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
16769b521250SVladimir Oltean 	 * a source for the other ports.
16779b521250SVladimir Oltean 	 */
16789b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1679e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1680e21268efSVladimir Oltean 		unsigned long mask;
1681e21268efSVladimir Oltean 
1682e21268efSVladimir Oltean 		if (!ocelot_port) {
1683e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1684e21268efSVladimir Oltean 			mask = 0;
1685e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1686e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1687c295f983SVladimir Oltean 			 * forward packets to all ports assigned to them.
1688e21268efSVladimir Oltean 			 */
1689c295f983SVladimir Oltean 			mask = ocelot_dsa_8021q_cpu_assigned_ports(ocelot,
1690c295f983SVladimir Oltean 								   ocelot_port);
1691df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1692528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
16939b521250SVladimir Oltean 
1694a8bd9fa5SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
1695df291e54SVladimir Oltean 			mask &= ~BIT(port);
1696c295f983SVladimir Oltean 
1697c295f983SVladimir Oltean 			mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1698c295f983SVladimir Oltean 									port);
1699c295f983SVladimir Oltean 
1700a14e6b69SVladimir Oltean 			if (bond)
1701a14e6b69SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond);
17029b521250SVladimir Oltean 		} else {
1703e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1704e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1705e21268efSVladimir Oltean 			 * module otherwise.
1706e21268efSVladimir Oltean 			 */
1707c295f983SVladimir Oltean 			mask = ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
1708c295f983SVladimir Oltean 								       port);
1709e21268efSVladimir Oltean 		}
1710e21268efSVladimir Oltean 
1711e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
17129b521250SVladimir Oltean 	}
17138abe1970SVladimir Oltean 
17148abe1970SVladimir Oltean 	/* If cut-through forwarding is supported and a port is leaving, there
17158abe1970SVladimir Oltean 	 * is a chance that cut-through was disabled on the other ports due to
17168abe1970SVladimir Oltean 	 * the port which is leaving (it has a higher link speed). We need to
17178abe1970SVladimir Oltean 	 * update the cut-through masks of the remaining ports no earlier than
17188abe1970SVladimir Oltean 	 * after the port has left, to prevent underruns from happening between
17198abe1970SVladimir Oltean 	 * the cut-through update and the forwarding domain update.
17208abe1970SVladimir Oltean 	 */
17218abe1970SVladimir Oltean 	if (!joining && ocelot->ops->cut_through_fwd)
17228abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
17239b521250SVladimir Oltean }
17249b521250SVladimir Oltean 
172561be79baSVladimir Oltean /* Update PGID_CPU which is the destination port mask used for whitelisting
172661be79baSVladimir Oltean  * unicast addresses filtered towards the host. In the normal and NPI modes,
172761be79baSVladimir Oltean  * this points to the analyzer entry for the CPU port module, while in DSA
172861be79baSVladimir Oltean  * tag_8021q mode, it is a bit mask of all active CPU ports.
172961be79baSVladimir Oltean  * PGID_SRC will take care of forwarding a packet from one user port to
173061be79baSVladimir Oltean  * no more than a single CPU port.
173161be79baSVladimir Oltean  */
ocelot_update_pgid_cpu(struct ocelot * ocelot)173261be79baSVladimir Oltean static void ocelot_update_pgid_cpu(struct ocelot *ocelot)
173361be79baSVladimir Oltean {
173461be79baSVladimir Oltean 	int pgid_cpu = 0;
173561be79baSVladimir Oltean 	int port;
173661be79baSVladimir Oltean 
173761be79baSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
173861be79baSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
173961be79baSVladimir Oltean 
174061be79baSVladimir Oltean 		if (!ocelot_port || !ocelot_port->is_dsa_8021q_cpu)
174161be79baSVladimir Oltean 			continue;
174261be79baSVladimir Oltean 
174361be79baSVladimir Oltean 		pgid_cpu |= BIT(port);
174461be79baSVladimir Oltean 	}
174561be79baSVladimir Oltean 
174661be79baSVladimir Oltean 	if (!pgid_cpu)
174761be79baSVladimir Oltean 		pgid_cpu = BIT(ocelot->num_phys_ports);
174861be79baSVladimir Oltean 
174961be79baSVladimir Oltean 	ocelot_write_rix(ocelot, pgid_cpu, ANA_PGID_PGID, PGID_CPU);
175061be79baSVladimir Oltean }
175161be79baSVladimir Oltean 
ocelot_port_setup_dsa_8021q_cpu(struct ocelot * ocelot,int cpu)175236a0bf44SVladimir Oltean void ocelot_port_setup_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
175354c31984SVladimir Oltean {
1754c295f983SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
175554c31984SVladimir Oltean 	u16 vid;
175654c31984SVladimir Oltean 
17578c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
17588c166acbSVladimir Oltean 
1759c295f983SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = true;
176054c31984SVladimir Oltean 
176154c31984SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
1762c295f983SVladimir Oltean 		ocelot_vlan_member_add(ocelot, cpu, vid, true);
176361be79baSVladimir Oltean 
176461be79baSVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
1765a72e23ddSVladimir Oltean 
176636a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
176736a0bf44SVladimir Oltean }
176836a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_setup_dsa_8021q_cpu);
176936a0bf44SVladimir Oltean 
ocelot_port_teardown_dsa_8021q_cpu(struct ocelot * ocelot,int cpu)177036a0bf44SVladimir Oltean void ocelot_port_teardown_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
177136a0bf44SVladimir Oltean {
177236a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
177336a0bf44SVladimir Oltean 	u16 vid;
177436a0bf44SVladimir Oltean 
177536a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
177636a0bf44SVladimir Oltean 
177736a0bf44SVladimir Oltean 	cpu_port->is_dsa_8021q_cpu = false;
177836a0bf44SVladimir Oltean 
177936a0bf44SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
178036a0bf44SVladimir Oltean 		ocelot_vlan_member_del(ocelot, cpu_port->index, vid);
178136a0bf44SVladimir Oltean 
178236a0bf44SVladimir Oltean 	ocelot_update_pgid_cpu(ocelot);
178336a0bf44SVladimir Oltean 
178436a0bf44SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
178536a0bf44SVladimir Oltean }
178636a0bf44SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_teardown_dsa_8021q_cpu);
178736a0bf44SVladimir Oltean 
ocelot_port_assign_dsa_8021q_cpu(struct ocelot * ocelot,int port,int cpu)178836a0bf44SVladimir Oltean void ocelot_port_assign_dsa_8021q_cpu(struct ocelot *ocelot, int port,
178936a0bf44SVladimir Oltean 				      int cpu)
179036a0bf44SVladimir Oltean {
179136a0bf44SVladimir Oltean 	struct ocelot_port *cpu_port = ocelot->ports[cpu];
179236a0bf44SVladimir Oltean 
179336a0bf44SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
179436a0bf44SVladimir Oltean 
179536a0bf44SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = cpu_port;
1796a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
17978c166acbSVladimir Oltean 
17988c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
179954c31984SVladimir Oltean }
1800c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_assign_dsa_8021q_cpu);
180154c31984SVladimir Oltean 
ocelot_port_unassign_dsa_8021q_cpu(struct ocelot * ocelot,int port)1802c295f983SVladimir Oltean void ocelot_port_unassign_dsa_8021q_cpu(struct ocelot *ocelot, int port)
180354c31984SVladimir Oltean {
18048c166acbSVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
18058c166acbSVladimir Oltean 
1806c295f983SVladimir Oltean 	ocelot->ports[port]->dsa_8021q_cpu = NULL;
1807a72e23ddSVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
18088c166acbSVladimir Oltean 
18098c166acbSVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
181054c31984SVladimir Oltean }
1811c295f983SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_unassign_dsa_8021q_cpu);
181254c31984SVladimir Oltean 
ocelot_bridge_stp_state_set(struct ocelot * ocelot,int port,u8 state)18135e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1814a556c76aSAlexandre Belloni {
1815421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1816df291e54SVladimir Oltean 	u32 learn_ena = 0;
1817a556c76aSAlexandre Belloni 
18188abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
18198abe1970SVladimir Oltean 
1820df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1821a556c76aSAlexandre Belloni 
1822df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1823df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1824df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1825a556c76aSAlexandre Belloni 
1826df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1827df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1828a556c76aSAlexandre Belloni 
18298abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
18308abe1970SVladimir Oltean 
18318abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
1832a556c76aSAlexandre Belloni }
18335e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1834a556c76aSAlexandre Belloni 
ocelot_set_ageing_time(struct ocelot * ocelot,unsigned int msecs)18355e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
18364bda1415SVladimir Oltean {
1837c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1838c0d7eccbSVladimir Oltean 
1839c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1840c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1841c0d7eccbSVladimir Oltean 	 */
1842c0d7eccbSVladimir Oltean 	if (!age_period)
1843c0d7eccbSVladimir Oltean 		age_period = 1;
1844c0d7eccbSVladimir Oltean 
1845c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1846a556c76aSAlexandre Belloni }
18475e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1848a556c76aSAlexandre Belloni 
ocelot_multicast_get(struct ocelot * ocelot,const unsigned char * addr,u16 vid)1849a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1850a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1851a556c76aSAlexandre Belloni 						     u16 vid)
1852a556c76aSAlexandre Belloni {
1853a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1854a556c76aSAlexandre Belloni 
1855a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1856a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1857a556c76aSAlexandre Belloni 			return mc;
1858a556c76aSAlexandre Belloni 	}
1859a556c76aSAlexandre Belloni 
1860a556c76aSAlexandre Belloni 	return NULL;
1861a556c76aSAlexandre Belloni }
1862a556c76aSAlexandre Belloni 
ocelot_classify_mdb(const unsigned char * addr)18639403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
18649403c158SVladimir Oltean {
18659403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
18669403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
18679403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
18689403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
18697c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
18709403c158SVladimir Oltean }
18719403c158SVladimir Oltean 
ocelot_pgid_alloc(struct ocelot * ocelot,int index,unsigned long ports)1872e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1873e5d1f896SVladimir Oltean 					     unsigned long ports)
1874e5d1f896SVladimir Oltean {
1875e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1876e5d1f896SVladimir Oltean 
1877e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1878e5d1f896SVladimir Oltean 	if (!pgid)
1879e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1880e5d1f896SVladimir Oltean 
1881e5d1f896SVladimir Oltean 	pgid->ports = ports;
1882e5d1f896SVladimir Oltean 	pgid->index = index;
1883e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1884e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1885e5d1f896SVladimir Oltean 
1886e5d1f896SVladimir Oltean 	return pgid;
1887e5d1f896SVladimir Oltean }
1888e5d1f896SVladimir Oltean 
ocelot_pgid_free(struct ocelot * ocelot,struct ocelot_pgid * pgid)1889e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1890e5d1f896SVladimir Oltean {
1891e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1892e5d1f896SVladimir Oltean 		return;
1893e5d1f896SVladimir Oltean 
1894e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1895e5d1f896SVladimir Oltean 	kfree(pgid);
1896e5d1f896SVladimir Oltean }
1897e5d1f896SVladimir Oltean 
ocelot_mdb_get_pgid(struct ocelot * ocelot,const struct ocelot_multicast * mc)1898e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1899bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
19009403c158SVladimir Oltean {
1901e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1902e5d1f896SVladimir Oltean 	int index;
19039403c158SVladimir Oltean 
19049403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
19059403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
19069403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
19079403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
19089403c158SVladimir Oltean 	 */
1909bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1910bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1911e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
19129403c158SVladimir Oltean 
1913e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1914e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1915e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1916e5d1f896SVladimir Oltean 		 */
1917e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1918e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1919e5d1f896SVladimir Oltean 			return pgid;
1920e5d1f896SVladimir Oltean 		}
1921e5d1f896SVladimir Oltean 	}
1922e5d1f896SVladimir Oltean 
1923e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1924e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
19259403c158SVladimir Oltean 		bool used = false;
19269403c158SVladimir Oltean 
1927e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1928e5d1f896SVladimir Oltean 			if (pgid->index == index) {
19299403c158SVladimir Oltean 				used = true;
19309403c158SVladimir Oltean 				break;
19319403c158SVladimir Oltean 			}
19329403c158SVladimir Oltean 		}
19339403c158SVladimir Oltean 
19349403c158SVladimir Oltean 		if (!used)
1935e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
19369403c158SVladimir Oltean 	}
19379403c158SVladimir Oltean 
1938e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
19399403c158SVladimir Oltean }
19409403c158SVladimir Oltean 
ocelot_encode_ports_to_mdb(unsigned char * addr,struct ocelot_multicast * mc)19419403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1942bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
19439403c158SVladimir Oltean {
1944ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
19459403c158SVladimir Oltean 
1946bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
19479403c158SVladimir Oltean 		addr[0] = 0;
19489403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
19499403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1950bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
19519403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
19529403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
19539403c158SVladimir Oltean 	}
19549403c158SVladimir Oltean }
19559403c158SVladimir Oltean 
ocelot_port_mdb_add(struct ocelot * ocelot,int port,const struct switchdev_obj_port_mdb * mdb,const struct net_device * bridge)1956209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
195754c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
195854c31984SVladimir Oltean 			const struct net_device *bridge)
1959a556c76aSAlexandre Belloni {
1960a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1961004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1962e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1963a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1964a556c76aSAlexandre Belloni 
196554c31984SVladimir Oltean 	if (!vid)
196654c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
196754c31984SVladimir Oltean 
1968a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1969a556c76aSAlexandre Belloni 	if (!mc) {
1970728e69aeSVladimir Oltean 		/* New entry */
1971bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1972bb8d53fdSVladimir Oltean 		if (!mc)
1973bb8d53fdSVladimir Oltean 			return -ENOMEM;
1974bb8d53fdSVladimir Oltean 
1975bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1976bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1977bb8d53fdSVladimir Oltean 		mc->vid = vid;
1978bb8d53fdSVladimir Oltean 
1979a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1980728e69aeSVladimir Oltean 	} else {
1981e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1982e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1983e5d1f896SVladimir Oltean 		 */
1984e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1985bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1986a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1987a556c76aSAlexandre Belloni 	}
1988a556c76aSAlexandre Belloni 
1989004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1990e5d1f896SVladimir Oltean 
1991e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1992e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1993e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1994e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1995e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1996e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1997e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1998e5d1f896SVladimir Oltean 	}
1999e5d1f896SVladimir Oltean 	mc->pgid = pgid;
2000e5d1f896SVladimir Oltean 
2001bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2002a556c76aSAlexandre Belloni 
2003e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
2004e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
2005e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2006e5d1f896SVladimir Oltean 				 pgid->index);
2007e5d1f896SVladimir Oltean 
2008e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
2009bb8d53fdSVladimir Oltean 				 mc->entry_type);
2010a556c76aSAlexandre Belloni }
2011209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
2012a556c76aSAlexandre Belloni 
ocelot_port_mdb_del(struct ocelot * ocelot,int port,const struct switchdev_obj_port_mdb * mdb,const struct net_device * bridge)2013209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
201454c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
201554c31984SVladimir Oltean 			const struct net_device *bridge)
2016a556c76aSAlexandre Belloni {
2017a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
2018004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
2019e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2020a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
2021a556c76aSAlexandre Belloni 
202254c31984SVladimir Oltean 	if (!vid)
202354c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
202454c31984SVladimir Oltean 
2025a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2026a556c76aSAlexandre Belloni 	if (!mc)
2027a556c76aSAlexandre Belloni 		return -ENOENT;
2028a556c76aSAlexandre Belloni 
2029bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2030a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
2031a556c76aSAlexandre Belloni 
2032e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
2033004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
2034a556c76aSAlexandre Belloni 	if (!mc->ports) {
2035a556c76aSAlexandre Belloni 		list_del(&mc->list);
2036a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
2037a556c76aSAlexandre Belloni 		return 0;
2038a556c76aSAlexandre Belloni 	}
2039a556c76aSAlexandre Belloni 
2040e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
2041e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
2042e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
2043e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
2044e5d1f896SVladimir Oltean 	mc->pgid = pgid;
2045e5d1f896SVladimir Oltean 
2046bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2047a556c76aSAlexandre Belloni 
2048e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
2049e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
2050e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2051e5d1f896SVladimir Oltean 				 pgid->index);
2052e5d1f896SVladimir Oltean 
2053e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
2054bb8d53fdSVladimir Oltean 				 mc->entry_type);
2055a556c76aSAlexandre Belloni }
2056209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
2057a556c76aSAlexandre Belloni 
ocelot_port_bridge_join(struct ocelot * ocelot,int port,struct net_device * bridge,int bridge_num,struct netlink_ext_ack * extack)205854c31984SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
205954c31984SVladimir Oltean 			    struct net_device *bridge, int bridge_num,
206054c31984SVladimir Oltean 			    struct netlink_ext_ack *extack)
2061a556c76aSAlexandre Belloni {
2062df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
206354c31984SVladimir Oltean 	int err;
206454c31984SVladimir Oltean 
206554c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
206654c31984SVladimir Oltean 	if (err)
206754c31984SVladimir Oltean 		return err;
2068a556c76aSAlexandre Belloni 
20698abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
20708abe1970SVladimir Oltean 
2071df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
207254c31984SVladimir Oltean 	ocelot_port->bridge_num = bridge_num;
2073a556c76aSAlexandre Belloni 
20748abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
20758abe1970SVladimir Oltean 
20768abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
207754c31984SVladimir Oltean 
207854c31984SVladimir Oltean 	if (br_vlan_enabled(bridge))
207954c31984SVladimir Oltean 		return 0;
208054c31984SVladimir Oltean 
208154c31984SVladimir Oltean 	return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge);
2082a556c76aSAlexandre Belloni }
20835e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
2084a556c76aSAlexandre Belloni 
ocelot_port_bridge_leave(struct ocelot * ocelot,int port,struct net_device * bridge)2085e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
2086a556c76aSAlexandre Belloni 			      struct net_device *bridge)
2087a556c76aSAlexandre Belloni {
2088df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
20892e554a7aSVladimir Oltean 
20908abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
20918abe1970SVladimir Oltean 
209254c31984SVladimir Oltean 	if (!br_vlan_enabled(bridge))
209354c31984SVladimir Oltean 		ocelot_del_vlan_unaware_pvid(ocelot, port, bridge);
209454c31984SVladimir Oltean 
2095df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
209654c31984SVladimir Oltean 	ocelot_port->bridge_num = -1;
20977142529fSAntoine Tenart 
2098d4004422SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, NULL);
20990da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
21008abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
21018abe1970SVladimir Oltean 
21028abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2103a556c76aSAlexandre Belloni }
21045e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
2105a556c76aSAlexandre Belloni 
ocelot_set_aggr_pgids(struct ocelot * ocelot)2106dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
2107dc96ee37SAlexandre Belloni {
2108528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
2109dc96ee37SAlexandre Belloni 	int i, port, lag;
2110dc96ee37SAlexandre Belloni 
2111dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
211296b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
2113dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2114dc96ee37SAlexandre Belloni 
211596b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
2116dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
2117dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
2118dc96ee37SAlexandre Belloni 
2119528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
2120528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
2121528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
2122528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
2123528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
2124528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
2125528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
2126528d3f19SVladimir Oltean 	 */
2127528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2128528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2129528d3f19SVladimir Oltean 
2130528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
2131528d3f19SVladimir Oltean 			continue;
2132528d3f19SVladimir Oltean 
2133528d3f19SVladimir Oltean 		visited &= ~BIT(port);
2134528d3f19SVladimir Oltean 	}
2135528d3f19SVladimir Oltean 
2136528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
2137dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
2138528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
213923ca3b72SVladimir Oltean 		int num_active_ports = 0;
2140dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
2141dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
2142dc96ee37SAlexandre Belloni 
2143528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
2144dc96ee37SAlexandre Belloni 			continue;
2145dc96ee37SAlexandre Belloni 
2146a14e6b69SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond);
2147528d3f19SVladimir Oltean 
2148dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
2149a14e6b69SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2150a14e6b69SVladimir Oltean 
2151dc96ee37SAlexandre Belloni 			// Destination mask
2152dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
2153dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
2154a14e6b69SVladimir Oltean 
2155a14e6b69SVladimir Oltean 			if (ocelot_port->lag_tx_active)
215623ca3b72SVladimir Oltean 				aggr_idx[num_active_ports++] = port;
2157dc96ee37SAlexandre Belloni 		}
2158dc96ee37SAlexandre Belloni 
215996b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
2160dc96ee37SAlexandre Belloni 			u32 ac;
2161dc96ee37SAlexandre Belloni 
2162dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
2163dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
216423ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
216523ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
216623ca3b72SVladimir Oltean 			 */
216723ca3b72SVladimir Oltean 			if (num_active_ports)
216823ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
2169dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
2170dc96ee37SAlexandre Belloni 		}
2171528d3f19SVladimir Oltean 
2172528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
2173528d3f19SVladimir Oltean 		 * the same config again.
2174528d3f19SVladimir Oltean 		 */
2175528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
2176528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2177528d3f19SVladimir Oltean 
2178528d3f19SVladimir Oltean 			if (!ocelot_port)
2179528d3f19SVladimir Oltean 				continue;
2180528d3f19SVladimir Oltean 
2181528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
2182528d3f19SVladimir Oltean 				visited |= BIT(port);
2183528d3f19SVladimir Oltean 		}
2184dc96ee37SAlexandre Belloni 	}
2185dc96ee37SAlexandre Belloni }
2186dc96ee37SAlexandre Belloni 
21872527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
21882527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
21892527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
21902527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
21912527f2e8SVladimir Oltean  */
ocelot_setup_logical_port_ids(struct ocelot * ocelot)21922527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
2193dc96ee37SAlexandre Belloni {
21942527f2e8SVladimir Oltean 	int port;
2195dc96ee37SAlexandre Belloni 
21962527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
21972527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
21982527f2e8SVladimir Oltean 		struct net_device *bond;
2199dc96ee37SAlexandre Belloni 
22002527f2e8SVladimir Oltean 		if (!ocelot_port)
22012527f2e8SVladimir Oltean 			continue;
2202dc96ee37SAlexandre Belloni 
22032527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
22042527f2e8SVladimir Oltean 		if (bond) {
2205961d8b69SVladimir Oltean 			int lag = ocelot_bond_get_id(ocelot, bond);
22062527f2e8SVladimir Oltean 
22072527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
2208dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
22092527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
22102527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
22112527f2e8SVladimir Oltean 		} else {
22122527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
22132527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
22142527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
22152527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
22162527f2e8SVladimir Oltean 		}
2217dc96ee37SAlexandre Belloni 	}
2218dc96ee37SAlexandre Belloni }
2219dc96ee37SAlexandre Belloni 
ocelot_migrate_mc(struct ocelot * ocelot,struct ocelot_multicast * mc,unsigned long from_mask,unsigned long to_mask)222028de0f9fSVladimir Oltean static int ocelot_migrate_mc(struct ocelot *ocelot, struct ocelot_multicast *mc,
222128de0f9fSVladimir Oltean 			     unsigned long from_mask, unsigned long to_mask)
222228de0f9fSVladimir Oltean {
222328de0f9fSVladimir Oltean 	unsigned char addr[ETH_ALEN];
222428de0f9fSVladimir Oltean 	struct ocelot_pgid *pgid;
222528de0f9fSVladimir Oltean 	u16 vid = mc->vid;
222628de0f9fSVladimir Oltean 
222728de0f9fSVladimir Oltean 	dev_dbg(ocelot->dev,
222828de0f9fSVladimir Oltean 		"Migrating multicast %pM vid %d from port mask 0x%lx to 0x%lx\n",
222928de0f9fSVladimir Oltean 		mc->addr, mc->vid, from_mask, to_mask);
223028de0f9fSVladimir Oltean 
223128de0f9fSVladimir Oltean 	/* First clean up the current port mask from hardware, because
223228de0f9fSVladimir Oltean 	 * we'll be modifying it.
223328de0f9fSVladimir Oltean 	 */
223428de0f9fSVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
223528de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
223628de0f9fSVladimir Oltean 	ocelot_mact_forget(ocelot, addr, vid);
223728de0f9fSVladimir Oltean 
223828de0f9fSVladimir Oltean 	mc->ports &= ~from_mask;
223928de0f9fSVladimir Oltean 	mc->ports |= to_mask;
224028de0f9fSVladimir Oltean 
224128de0f9fSVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
224228de0f9fSVladimir Oltean 	if (IS_ERR(pgid)) {
224328de0f9fSVladimir Oltean 		dev_err(ocelot->dev,
224428de0f9fSVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
224528de0f9fSVladimir Oltean 			mc->addr, mc->vid);
224628de0f9fSVladimir Oltean 		devm_kfree(ocelot->dev, mc);
224728de0f9fSVladimir Oltean 		return PTR_ERR(pgid);
224828de0f9fSVladimir Oltean 	}
224928de0f9fSVladimir Oltean 	mc->pgid = pgid;
225028de0f9fSVladimir Oltean 
225128de0f9fSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
225228de0f9fSVladimir Oltean 
225328de0f9fSVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
225428de0f9fSVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
225528de0f9fSVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
225628de0f9fSVladimir Oltean 				 pgid->index);
225728de0f9fSVladimir Oltean 
225828de0f9fSVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
225928de0f9fSVladimir Oltean 				 mc->entry_type);
226028de0f9fSVladimir Oltean }
226128de0f9fSVladimir Oltean 
ocelot_migrate_mdbs(struct ocelot * ocelot,unsigned long from_mask,unsigned long to_mask)226228de0f9fSVladimir Oltean int ocelot_migrate_mdbs(struct ocelot *ocelot, unsigned long from_mask,
226328de0f9fSVladimir Oltean 			unsigned long to_mask)
226428de0f9fSVladimir Oltean {
226528de0f9fSVladimir Oltean 	struct ocelot_multicast *mc;
226628de0f9fSVladimir Oltean 	int err;
226728de0f9fSVladimir Oltean 
226828de0f9fSVladimir Oltean 	list_for_each_entry(mc, &ocelot->multicast, list) {
226928de0f9fSVladimir Oltean 		if (!(mc->ports & from_mask))
227028de0f9fSVladimir Oltean 			continue;
227128de0f9fSVladimir Oltean 
227228de0f9fSVladimir Oltean 		err = ocelot_migrate_mc(ocelot, mc, from_mask, to_mask);
227328de0f9fSVladimir Oltean 		if (err)
227428de0f9fSVladimir Oltean 			return err;
227528de0f9fSVladimir Oltean 	}
227628de0f9fSVladimir Oltean 
227728de0f9fSVladimir Oltean 	return 0;
227828de0f9fSVladimir Oltean }
227928de0f9fSVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_migrate_mdbs);
228028de0f9fSVladimir Oltean 
2281961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says:
2282961d8b69SVladimir Oltean  *     Logical port number for front port. If port is not a member of a LLAG,
2283961d8b69SVladimir Oltean  *     then PORTID must be set to the physical port number.
2284961d8b69SVladimir Oltean  *     If port is a member of a LLAG, then PORTID must be set to the common
2285961d8b69SVladimir Oltean  *     PORTID_VAL used for all member ports of the LLAG.
2286961d8b69SVladimir Oltean  *     The value must not exceed the number of physical ports on the device.
2287961d8b69SVladimir Oltean  *
2288961d8b69SVladimir Oltean  * This means we have little choice but to migrate FDB entries pointing towards
2289961d8b69SVladimir Oltean  * a logical port when that changes.
2290961d8b69SVladimir Oltean  */
ocelot_migrate_lag_fdbs(struct ocelot * ocelot,struct net_device * bond,int lag)2291961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot,
2292961d8b69SVladimir Oltean 				    struct net_device *bond,
2293961d8b69SVladimir Oltean 				    int lag)
2294961d8b69SVladimir Oltean {
2295961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2296961d8b69SVladimir Oltean 	int err;
2297961d8b69SVladimir Oltean 
2298961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2299961d8b69SVladimir Oltean 
2300961d8b69SVladimir Oltean 	list_for_each_entry(fdb, &ocelot->lag_fdbs, list) {
2301961d8b69SVladimir Oltean 		if (fdb->bond != bond)
2302961d8b69SVladimir Oltean 			continue;
2303961d8b69SVladimir Oltean 
2304961d8b69SVladimir Oltean 		err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid);
2305961d8b69SVladimir Oltean 		if (err) {
2306961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2307961d8b69SVladimir Oltean 				"failed to delete LAG %s FDB %pM vid %d: %pe\n",
2308961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2309961d8b69SVladimir Oltean 		}
2310961d8b69SVladimir Oltean 
2311961d8b69SVladimir Oltean 		err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid,
2312961d8b69SVladimir Oltean 					ENTRYTYPE_LOCKED);
2313961d8b69SVladimir Oltean 		if (err) {
2314961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2315961d8b69SVladimir Oltean 				"failed to migrate LAG %s FDB %pM vid %d: %pe\n",
2316961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2317961d8b69SVladimir Oltean 		}
2318961d8b69SVladimir Oltean 	}
2319961d8b69SVladimir Oltean }
2320961d8b69SVladimir Oltean 
ocelot_port_lag_join(struct ocelot * ocelot,int port,struct net_device * bond,struct netdev_lag_upper_info * info,struct netlink_ext_ack * extack)23219c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2322583cbbe3SVladimir Oltean 			 struct net_device *bond,
23232e359b00SVladimir Oltean 			 struct netdev_lag_upper_info *info,
23242e359b00SVladimir Oltean 			 struct netlink_ext_ack *extack)
2325dc96ee37SAlexandre Belloni {
23262e359b00SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
23272e359b00SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
23282e359b00SVladimir Oltean 				   "Can only offload LAG using hash TX type");
2329583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
23302e359b00SVladimir Oltean 	}
2331583cbbe3SVladimir Oltean 
23328abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
23338abe1970SVladimir Oltean 
2334b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
2335dc96ee37SAlexandre Belloni 
23362527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
23378abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2338dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2339dc96ee37SAlexandre Belloni 
23408abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
23418abe1970SVladimir Oltean 
2342dc96ee37SAlexandre Belloni 	return 0;
2343dc96ee37SAlexandre Belloni }
23449c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
2345dc96ee37SAlexandre Belloni 
ocelot_port_lag_leave(struct ocelot * ocelot,int port,struct net_device * bond)23469c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2347dc96ee37SAlexandre Belloni 			   struct net_device *bond)
2348dc96ee37SAlexandre Belloni {
2349961d8b69SVladimir Oltean 	int old_lag_id, new_lag_id;
2350961d8b69SVladimir Oltean 
23518abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
23528abe1970SVladimir Oltean 
2353961d8b69SVladimir Oltean 	old_lag_id = ocelot_bond_get_id(ocelot, bond);
2354961d8b69SVladimir Oltean 
2355b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
2356b80af659SVladimir Oltean 
23572527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
23588abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2359dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
23608abe1970SVladimir Oltean 
2361961d8b69SVladimir Oltean 	new_lag_id = ocelot_bond_get_id(ocelot, bond);
2362961d8b69SVladimir Oltean 
2363961d8b69SVladimir Oltean 	if (new_lag_id >= 0 && old_lag_id != new_lag_id)
2364961d8b69SVladimir Oltean 		ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id);
2365961d8b69SVladimir Oltean 
23668abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2367dc96ee37SAlexandre Belloni }
23689c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
23690e332c85SPetr Machata 
ocelot_port_lag_change(struct ocelot * ocelot,int port,bool lag_tx_active)237023ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
237123ca3b72SVladimir Oltean {
237223ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
237323ca3b72SVladimir Oltean 
2374961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2375961d8b69SVladimir Oltean 
237623ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
237723ca3b72SVladimir Oltean 
237823ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
237923ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
2380961d8b69SVladimir Oltean 
2381961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
238223ca3b72SVladimir Oltean }
238323ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
238423ca3b72SVladimir Oltean 
ocelot_lag_fdb_add(struct ocelot * ocelot,struct net_device * bond,const unsigned char * addr,u16 vid,const struct net_device * bridge)2385961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond,
238654c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
238754c31984SVladimir Oltean 		       const struct net_device *bridge)
2388961d8b69SVladimir Oltean {
2389961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2390961d8b69SVladimir Oltean 	int lag, err;
2391961d8b69SVladimir Oltean 
2392961d8b69SVladimir Oltean 	fdb = kzalloc(sizeof(*fdb), GFP_KERNEL);
2393961d8b69SVladimir Oltean 	if (!fdb)
2394961d8b69SVladimir Oltean 		return -ENOMEM;
2395961d8b69SVladimir Oltean 
239654c31984SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
239754c31984SVladimir Oltean 
239854c31984SVladimir Oltean 	if (!vid)
239954c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
240054c31984SVladimir Oltean 
2401961d8b69SVladimir Oltean 	ether_addr_copy(fdb->addr, addr);
2402961d8b69SVladimir Oltean 	fdb->vid = vid;
2403961d8b69SVladimir Oltean 	fdb->bond = bond;
2404961d8b69SVladimir Oltean 
2405961d8b69SVladimir Oltean 	lag = ocelot_bond_get_id(ocelot, bond);
2406961d8b69SVladimir Oltean 
2407961d8b69SVladimir Oltean 	err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED);
2408961d8b69SVladimir Oltean 	if (err) {
2409961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2410961d8b69SVladimir Oltean 		kfree(fdb);
2411961d8b69SVladimir Oltean 		return err;
2412961d8b69SVladimir Oltean 	}
2413961d8b69SVladimir Oltean 
2414961d8b69SVladimir Oltean 	list_add_tail(&fdb->list, &ocelot->lag_fdbs);
2415961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2416961d8b69SVladimir Oltean 
2417961d8b69SVladimir Oltean 	return 0;
2418961d8b69SVladimir Oltean }
2419961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add);
2420961d8b69SVladimir Oltean 
ocelot_lag_fdb_del(struct ocelot * ocelot,struct net_device * bond,const unsigned char * addr,u16 vid,const struct net_device * bridge)2421961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond,
242254c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
242354c31984SVladimir Oltean 		       const struct net_device *bridge)
2424961d8b69SVladimir Oltean {
2425961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb, *tmp;
2426961d8b69SVladimir Oltean 
2427961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2428961d8b69SVladimir Oltean 
242954c31984SVladimir Oltean 	if (!vid)
243054c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
243154c31984SVladimir Oltean 
2432961d8b69SVladimir Oltean 	list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) {
2433961d8b69SVladimir Oltean 		if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid ||
2434961d8b69SVladimir Oltean 		    fdb->bond != bond)
2435961d8b69SVladimir Oltean 			continue;
2436961d8b69SVladimir Oltean 
2437961d8b69SVladimir Oltean 		ocelot_mact_forget(ocelot, addr, vid);
2438961d8b69SVladimir Oltean 		list_del(&fdb->list);
2439961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2440961d8b69SVladimir Oltean 		kfree(fdb);
2441961d8b69SVladimir Oltean 
2442961d8b69SVladimir Oltean 		return 0;
2443961d8b69SVladimir Oltean 	}
2444961d8b69SVladimir Oltean 
2445961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2446961d8b69SVladimir Oltean 
2447961d8b69SVladimir Oltean 	return -ENOENT;
2448961d8b69SVladimir Oltean }
2449961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del);
2450961d8b69SVladimir Oltean 
2451a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2452a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
24530b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
24540b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
24550b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2456a8015dedSVladimir Oltean  */
ocelot_port_set_maxlen(struct ocelot * ocelot,int port,size_t sdu)24570b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
245831350d7fSVladimir Oltean {
245931350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2460a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2461e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
2462601e984fSVladimir Oltean 	int atop, atop_tot;
246331350d7fSVladimir Oltean 
24640b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
24650b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
24660b912fc9SVladimir Oltean 
2467cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
24680b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2469cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
24700b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
24710b912fc9SVladimir Oltean 	}
24720b912fc9SVladimir Oltean 
2473a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2474fa914e9cSVladimir Oltean 
2475e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
2476e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2477e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2478541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2479541132f0SMaxim Kochetkov 			    pause_start);
2480541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2481541132f0SMaxim Kochetkov 			    pause_stop);
2482fa914e9cSVladimir Oltean 
2483601e984fSVladimir Oltean 	/* Tail dropping watermarks */
2484f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2485a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2486601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2487601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2488601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2489fa914e9cSVladimir Oltean }
24900b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
24910b912fc9SVladimir Oltean 
ocelot_get_max_mtu(struct ocelot * ocelot,int port)24920b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
24930b912fc9SVladimir Oltean {
24940b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
24950b912fc9SVladimir Oltean 
24960b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
24970b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
24980b912fc9SVladimir Oltean 
2499cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
25000b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2501cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
25020b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
25030b912fc9SVladimir Oltean 	}
25040b912fc9SVladimir Oltean 
25050b912fc9SVladimir Oltean 	return max_mtu;
25060b912fc9SVladimir Oltean }
25070b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2508fa914e9cSVladimir Oltean 
ocelot_port_set_learning(struct ocelot * ocelot,int port,bool enabled)2509421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2510421741eaSVladimir Oltean 				     bool enabled)
2511421741eaSVladimir Oltean {
2512421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2513421741eaSVladimir Oltean 	u32 val = 0;
2514421741eaSVladimir Oltean 
2515421741eaSVladimir Oltean 	if (enabled)
2516421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2517421741eaSVladimir Oltean 
2518421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2519421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2520421741eaSVladimir Oltean 
2521421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2522421741eaSVladimir Oltean }
2523421741eaSVladimir Oltean 
ocelot_port_set_ucast_flood(struct ocelot * ocelot,int port,bool enabled)2524421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2525421741eaSVladimir Oltean 					bool enabled)
2526421741eaSVladimir Oltean {
2527421741eaSVladimir Oltean 	u32 val = 0;
2528421741eaSVladimir Oltean 
2529421741eaSVladimir Oltean 	if (enabled)
2530421741eaSVladimir Oltean 		val = BIT(port);
2531421741eaSVladimir Oltean 
2532421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2533421741eaSVladimir Oltean }
2534421741eaSVladimir Oltean 
ocelot_port_set_mcast_flood(struct ocelot * ocelot,int port,bool enabled)2535421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2536421741eaSVladimir Oltean 					bool enabled)
2537421741eaSVladimir Oltean {
2538421741eaSVladimir Oltean 	u32 val = 0;
2539421741eaSVladimir Oltean 
2540421741eaSVladimir Oltean 	if (enabled)
2541421741eaSVladimir Oltean 		val = BIT(port);
2542421741eaSVladimir Oltean 
2543421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
25444cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4);
25454cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6);
2546421741eaSVladimir Oltean }
2547421741eaSVladimir Oltean 
ocelot_port_set_bcast_flood(struct ocelot * ocelot,int port,bool enabled)2548421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2549421741eaSVladimir Oltean 					bool enabled)
2550421741eaSVladimir Oltean {
2551421741eaSVladimir Oltean 	u32 val = 0;
2552421741eaSVladimir Oltean 
2553421741eaSVladimir Oltean 	if (enabled)
2554421741eaSVladimir Oltean 		val = BIT(port);
2555421741eaSVladimir Oltean 
2556421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2557421741eaSVladimir Oltean }
2558421741eaSVladimir Oltean 
ocelot_port_pre_bridge_flags(struct ocelot * ocelot,int port,struct switchdev_brport_flags flags)2559421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2560421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2561421741eaSVladimir Oltean {
2562421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2563421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2564421741eaSVladimir Oltean 		return -EINVAL;
2565421741eaSVladimir Oltean 
2566421741eaSVladimir Oltean 	return 0;
2567421741eaSVladimir Oltean }
2568421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2569421741eaSVladimir Oltean 
ocelot_port_bridge_flags(struct ocelot * ocelot,int port,struct switchdev_brport_flags flags)2570421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2571421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2572421741eaSVladimir Oltean {
2573421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2574421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2575421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2576421741eaSVladimir Oltean 
2577421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2578421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2579421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2580421741eaSVladimir Oltean 
2581421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2582421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2583421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2584421741eaSVladimir Oltean 
2585421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2586421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2587421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2588421741eaSVladimir Oltean }
2589421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2590421741eaSVladimir Oltean 
ocelot_port_get_default_prio(struct ocelot * ocelot,int port)2591978777d0SVladimir Oltean int ocelot_port_get_default_prio(struct ocelot *ocelot, int port)
2592978777d0SVladimir Oltean {
2593978777d0SVladimir Oltean 	int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2594978777d0SVladimir Oltean 
2595978777d0SVladimir Oltean 	return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val);
2596978777d0SVladimir Oltean }
2597978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio);
2598978777d0SVladimir Oltean 
ocelot_port_set_default_prio(struct ocelot * ocelot,int port,u8 prio)2599978777d0SVladimir Oltean int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio)
2600978777d0SVladimir Oltean {
260172f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2602978777d0SVladimir Oltean 		return -ERANGE;
2603978777d0SVladimir Oltean 
2604978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot,
2605978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio),
2606978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M,
2607978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG,
2608978777d0SVladimir Oltean 		       port);
2609978777d0SVladimir Oltean 
2610978777d0SVladimir Oltean 	return 0;
2611978777d0SVladimir Oltean }
2612978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio);
2613978777d0SVladimir Oltean 
ocelot_port_get_dscp_prio(struct ocelot * ocelot,int port,u8 dscp)2614978777d0SVladimir Oltean int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp)
2615978777d0SVladimir Oltean {
2616978777d0SVladimir Oltean 	int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2617978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2618978777d0SVladimir Oltean 
2619978777d0SVladimir Oltean 	/* Return error if DSCP prioritization isn't enabled */
2620978777d0SVladimir Oltean 	if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA))
2621978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2622978777d0SVladimir Oltean 
2623978777d0SVladimir Oltean 	if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) {
2624978777d0SVladimir Oltean 		dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg);
2625978777d0SVladimir Oltean 		/* Re-read ANA_DSCP_CFG for the translated DSCP */
2626978777d0SVladimir Oltean 		dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2627978777d0SVladimir Oltean 	}
2628978777d0SVladimir Oltean 
2629978777d0SVladimir Oltean 	/* If the DSCP value is not trusted, the QoS classification falls back
2630978777d0SVladimir Oltean 	 * to VLAN PCP or port-based default.
2631978777d0SVladimir Oltean 	 */
2632978777d0SVladimir Oltean 	if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA))
2633978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2634978777d0SVladimir Oltean 
2635978777d0SVladimir Oltean 	return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg);
2636978777d0SVladimir Oltean }
2637978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio);
2638978777d0SVladimir Oltean 
ocelot_port_add_dscp_prio(struct ocelot * ocelot,int port,u8 dscp,u8 prio)2639978777d0SVladimir Oltean int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2640978777d0SVladimir Oltean {
2641978777d0SVladimir Oltean 	int mask, val;
2642978777d0SVladimir Oltean 
264372f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2644978777d0SVladimir Oltean 		return -ERANGE;
2645978777d0SVladimir Oltean 
2646978777d0SVladimir Oltean 	/* There is at least one app table priority (this one), so we need to
2647978777d0SVladimir Oltean 	 * make sure DSCP prioritization is enabled on the port.
2648978777d0SVladimir Oltean 	 * Also make sure DSCP translation is disabled
2649978777d0SVladimir Oltean 	 * (dcbnl doesn't support it).
2650978777d0SVladimir Oltean 	 */
2651978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2652978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2653978777d0SVladimir Oltean 
2654978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask,
2655978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG, port);
2656978777d0SVladimir Oltean 
2657978777d0SVladimir Oltean 	/* Trust this DSCP value and map it to the given QoS class */
2658978777d0SVladimir Oltean 	val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio);
2659978777d0SVladimir Oltean 
2660978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp);
2661978777d0SVladimir Oltean 
2662978777d0SVladimir Oltean 	return 0;
2663978777d0SVladimir Oltean }
2664978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio);
2665978777d0SVladimir Oltean 
ocelot_port_del_dscp_prio(struct ocelot * ocelot,int port,u8 dscp,u8 prio)2666978777d0SVladimir Oltean int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2667978777d0SVladimir Oltean {
2668978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2669978777d0SVladimir Oltean 	int mask, i;
2670978777d0SVladimir Oltean 
2671978777d0SVladimir Oltean 	/* During a "dcb app replace" command, the new app table entry will be
2672978777d0SVladimir Oltean 	 * added first, then the old one will be deleted. But the hardware only
2673978777d0SVladimir Oltean 	 * supports one QoS class per DSCP value (duh), so if we blindly delete
2674978777d0SVladimir Oltean 	 * the app table entry for this DSCP value, we end up deleting the
2675978777d0SVladimir Oltean 	 * entry with the new priority. Avoid that by checking whether user
2676978777d0SVladimir Oltean 	 * space wants to delete the priority which is currently configured, or
2677978777d0SVladimir Oltean 	 * something else which is no longer current.
2678978777d0SVladimir Oltean 	 */
2679978777d0SVladimir Oltean 	if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio)
2680978777d0SVladimir Oltean 		return 0;
2681978777d0SVladimir Oltean 
2682978777d0SVladimir Oltean 	/* Untrust this DSCP value */
2683978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp);
2684978777d0SVladimir Oltean 
2685978777d0SVladimir Oltean 	for (i = 0; i < 64; i++) {
2686978777d0SVladimir Oltean 		int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i);
2687978777d0SVladimir Oltean 
2688978777d0SVladimir Oltean 		/* There are still app table entries on the port, so we need to
2689978777d0SVladimir Oltean 		 * keep DSCP enabled, nothing to do.
2690978777d0SVladimir Oltean 		 */
2691978777d0SVladimir Oltean 		if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)
2692978777d0SVladimir Oltean 			return 0;
2693978777d0SVladimir Oltean 	}
2694978777d0SVladimir Oltean 
2695978777d0SVladimir Oltean 	/* Disable DSCP QoS classification if there isn't any trusted
2696978777d0SVladimir Oltean 	 * DSCP value left.
2697978777d0SVladimir Oltean 	 */
2698978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2699978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2700978777d0SVladimir Oltean 
2701978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port);
2702978777d0SVladimir Oltean 
2703978777d0SVladimir Oltean 	return 0;
2704978777d0SVladimir Oltean }
2705978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio);
2706978777d0SVladimir Oltean 
ocelot_mirror_get(struct ocelot * ocelot,int to,struct netlink_ext_ack * extack)2707f2a0e216SVladimir Oltean struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to,
2708ccb6ed42SVladimir Oltean 					struct netlink_ext_ack *extack)
2709ccb6ed42SVladimir Oltean {
2710ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2711ccb6ed42SVladimir Oltean 
2712ccb6ed42SVladimir Oltean 	if (m) {
2713ccb6ed42SVladimir Oltean 		if (m->to != to) {
2714ccb6ed42SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
2715ccb6ed42SVladimir Oltean 					   "Mirroring already configured towards different egress port");
2716ccb6ed42SVladimir Oltean 			return ERR_PTR(-EBUSY);
2717ccb6ed42SVladimir Oltean 		}
2718ccb6ed42SVladimir Oltean 
2719ccb6ed42SVladimir Oltean 		refcount_inc(&m->refcount);
2720ccb6ed42SVladimir Oltean 		return m;
2721ccb6ed42SVladimir Oltean 	}
2722ccb6ed42SVladimir Oltean 
2723ccb6ed42SVladimir Oltean 	m = kzalloc(sizeof(*m), GFP_KERNEL);
2724ccb6ed42SVladimir Oltean 	if (!m)
2725ccb6ed42SVladimir Oltean 		return ERR_PTR(-ENOMEM);
2726ccb6ed42SVladimir Oltean 
2727ccb6ed42SVladimir Oltean 	m->to = to;
2728ccb6ed42SVladimir Oltean 	refcount_set(&m->refcount, 1);
2729ccb6ed42SVladimir Oltean 	ocelot->mirror = m;
2730ccb6ed42SVladimir Oltean 
2731ccb6ed42SVladimir Oltean 	/* Program the mirror port to hardware */
2732ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS);
2733ccb6ed42SVladimir Oltean 
2734ccb6ed42SVladimir Oltean 	return m;
2735ccb6ed42SVladimir Oltean }
2736ccb6ed42SVladimir Oltean 
ocelot_mirror_put(struct ocelot * ocelot)2737f2a0e216SVladimir Oltean void ocelot_mirror_put(struct ocelot *ocelot)
2738ccb6ed42SVladimir Oltean {
2739ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
2740ccb6ed42SVladimir Oltean 
2741ccb6ed42SVladimir Oltean 	if (!refcount_dec_and_test(&m->refcount))
2742ccb6ed42SVladimir Oltean 		return;
2743ccb6ed42SVladimir Oltean 
2744ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_MIRRORPORTS);
2745ccb6ed42SVladimir Oltean 	ocelot->mirror = NULL;
2746ccb6ed42SVladimir Oltean 	kfree(m);
2747ccb6ed42SVladimir Oltean }
2748ccb6ed42SVladimir Oltean 
ocelot_port_mirror_add(struct ocelot * ocelot,int from,int to,bool ingress,struct netlink_ext_ack * extack)2749ccb6ed42SVladimir Oltean int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to,
2750ccb6ed42SVladimir Oltean 			   bool ingress, struct netlink_ext_ack *extack)
2751ccb6ed42SVladimir Oltean {
2752ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack);
2753ccb6ed42SVladimir Oltean 
2754ccb6ed42SVladimir Oltean 	if (IS_ERR(m))
2755ccb6ed42SVladimir Oltean 		return PTR_ERR(m);
2756ccb6ed42SVladimir Oltean 
2757ccb6ed42SVladimir Oltean 	if (ingress) {
2758ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2759ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2760ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2761ccb6ed42SVladimir Oltean 	} else {
2762ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, BIT(from), BIT(from),
2763ccb6ed42SVladimir Oltean 			   ANA_EMIRRORPORTS);
2764ccb6ed42SVladimir Oltean 	}
2765ccb6ed42SVladimir Oltean 
2766ccb6ed42SVladimir Oltean 	return 0;
2767ccb6ed42SVladimir Oltean }
2768ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_add);
2769ccb6ed42SVladimir Oltean 
ocelot_port_mirror_del(struct ocelot * ocelot,int from,bool ingress)2770ccb6ed42SVladimir Oltean void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress)
2771ccb6ed42SVladimir Oltean {
2772ccb6ed42SVladimir Oltean 	if (ingress) {
2773ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
2774ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
2775ccb6ed42SVladimir Oltean 	} else {
2776ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS);
2777ccb6ed42SVladimir Oltean 	}
2778ccb6ed42SVladimir Oltean 
2779ccb6ed42SVladimir Oltean 	ocelot_mirror_put(ocelot);
2780ccb6ed42SVladimir Oltean }
2781ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_del);
2782ccb6ed42SVladimir Oltean 
ocelot_port_reset_mqprio(struct ocelot * ocelot,int port)2783aac80140SVladimir Oltean static void ocelot_port_reset_mqprio(struct ocelot *ocelot, int port)
2784aac80140SVladimir Oltean {
2785aac80140SVladimir Oltean 	struct net_device *dev = ocelot->ops->port_to_netdev(ocelot, port);
2786aac80140SVladimir Oltean 
2787aac80140SVladimir Oltean 	netdev_reset_tc(dev);
2788403ffc2cSVladimir Oltean 	ocelot_port_change_fp(ocelot, port, 0);
2789aac80140SVladimir Oltean }
2790aac80140SVladimir Oltean 
ocelot_port_mqprio(struct ocelot * ocelot,int port,struct tc_mqprio_qopt_offload * mqprio)2791aac80140SVladimir Oltean int ocelot_port_mqprio(struct ocelot *ocelot, int port,
2792aac80140SVladimir Oltean 		       struct tc_mqprio_qopt_offload *mqprio)
2793aac80140SVladimir Oltean {
2794aac80140SVladimir Oltean 	struct net_device *dev = ocelot->ops->port_to_netdev(ocelot, port);
2795aac80140SVladimir Oltean 	struct netlink_ext_ack *extack = mqprio->extack;
2796aac80140SVladimir Oltean 	struct tc_mqprio_qopt *qopt = &mqprio->qopt;
2797aac80140SVladimir Oltean 	int num_tc = qopt->num_tc;
2798aac80140SVladimir Oltean 	int tc, err;
2799aac80140SVladimir Oltean 
2800aac80140SVladimir Oltean 	if (!num_tc) {
2801aac80140SVladimir Oltean 		ocelot_port_reset_mqprio(ocelot, port);
2802aac80140SVladimir Oltean 		return 0;
2803aac80140SVladimir Oltean 	}
2804aac80140SVladimir Oltean 
2805aac80140SVladimir Oltean 	err = netdev_set_num_tc(dev, num_tc);
2806aac80140SVladimir Oltean 	if (err)
2807aac80140SVladimir Oltean 		return err;
2808aac80140SVladimir Oltean 
2809aac80140SVladimir Oltean 	for (tc = 0; tc < num_tc; tc++) {
2810aac80140SVladimir Oltean 		if (qopt->count[tc] != 1) {
2811aac80140SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
2812aac80140SVladimir Oltean 					   "Only one TXQ per TC supported");
2813aac80140SVladimir Oltean 			return -EINVAL;
2814aac80140SVladimir Oltean 		}
2815aac80140SVladimir Oltean 
2816aac80140SVladimir Oltean 		err = netdev_set_tc_queue(dev, tc, 1, qopt->offset[tc]);
2817aac80140SVladimir Oltean 		if (err)
2818aac80140SVladimir Oltean 			goto err_reset_tc;
2819aac80140SVladimir Oltean 	}
2820aac80140SVladimir Oltean 
2821aac80140SVladimir Oltean 	err = netif_set_real_num_tx_queues(dev, num_tc);
2822aac80140SVladimir Oltean 	if (err)
2823aac80140SVladimir Oltean 		goto err_reset_tc;
2824aac80140SVladimir Oltean 
2825403ffc2cSVladimir Oltean 	ocelot_port_change_fp(ocelot, port, mqprio->preemptible_tcs);
2826403ffc2cSVladimir Oltean 
2827aac80140SVladimir Oltean 	return 0;
2828aac80140SVladimir Oltean 
2829aac80140SVladimir Oltean err_reset_tc:
2830aac80140SVladimir Oltean 	ocelot_port_reset_mqprio(ocelot, port);
2831aac80140SVladimir Oltean 	return err;
2832aac80140SVladimir Oltean }
2833aac80140SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mqprio);
2834aac80140SVladimir Oltean 
ocelot_init_port(struct ocelot * ocelot,int port)28355e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2836fa914e9cSVladimir Oltean {
2837fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2838fa914e9cSVladimir Oltean 
2839b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
284031350d7fSVladimir Oltean 
284131350d7fSVladimir Oltean 	/* Basic L2 initialization */
284231350d7fSVladimir Oltean 
28435bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
28445bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
28455bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
28465bc9d2e6SVladimir Oltean 	 */
28475bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
28485bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
28495bc9d2e6SVladimir Oltean 
28505bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
28515bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
28525bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
28535bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
28545bc9d2e6SVladimir Oltean 	mdelay(1);
28555bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
28565bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
28575bc9d2e6SVladimir Oltean 
28585bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2859a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
28605bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
28615bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2862a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
28635bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
28645bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
28655bc9d2e6SVladimir Oltean 
28665bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
28675bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
28685bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
28695bc9d2e6SVladimir Oltean 
2870e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2871541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2872e8e6e73dSVladimir Oltean 
287331350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
287431350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
287531350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
287631350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
287731350d7fSVladimir Oltean 
287831350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
287931350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
288031350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
288131350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
288231350d7fSVladimir Oltean 
2883421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2884421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2885421741eaSVladimir Oltean 
288646efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
288746efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
288846efe4efSVladimir Oltean 	 * automatic.
288946efe4efSVladimir Oltean 	 */
289046efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
289146efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
289246efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
289346efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
289446efe4efSVladimir Oltean 
289531350d7fSVladimir Oltean 	/* Enable vcap lookups */
289631350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
289731350d7fSVladimir Oltean }
28985e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
289931350d7fSVladimir Oltean 
29002d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
29012d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
29022d44b097SVladimir Oltean  * NPI mode is used).
290369df578cSVladimir Oltean  */
ocelot_cpu_port_init(struct ocelot * ocelot)29042d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
290521468199SVladimir Oltean {
290669df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
290769df578cSVladimir Oltean 
290869df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
290921468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
291069df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
291169df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
291269df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
291369df578cSVladimir Oltean 	 */
291421468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
291521468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
291621468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
291721468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
291821468199SVladimir Oltean 
291969df578cSVladimir Oltean 	/* Enable CPU port module */
2920886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
292169df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2922886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2923cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2924886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2925cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
292621468199SVladimir Oltean 
292721468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
2928bfbab310SVladimir Oltean 	ocelot_write_gix(ocelot,
292954c31984SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) |
293021468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
293121468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
293221468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
293321468199SVladimir Oltean }
293421468199SVladimir Oltean 
ocelot_detect_features(struct ocelot * ocelot)2935f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2936f6fe01d6SVladimir Oltean {
2937f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2938f6fe01d6SVladimir Oltean 
2939f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2940f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2941f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2942f6fe01d6SVladimir Oltean 	 */
2943f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2944f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2945f6fe01d6SVladimir Oltean 
2946f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2947f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2948f6fe01d6SVladimir Oltean }
2949f6fe01d6SVladimir Oltean 
ocelot_mem_init_status(struct ocelot * ocelot)2950b67f5502SColin Foster static int ocelot_mem_init_status(struct ocelot *ocelot)
2951b67f5502SColin Foster {
2952b67f5502SColin Foster 	unsigned int val;
2953b67f5502SColin Foster 	int err;
2954b67f5502SColin Foster 
2955b67f5502SColin Foster 	err = regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
2956b67f5502SColin Foster 				&val);
2957b67f5502SColin Foster 
2958b67f5502SColin Foster 	return err ?: val;
2959b67f5502SColin Foster }
2960b67f5502SColin Foster 
ocelot_reset(struct ocelot * ocelot)2961b67f5502SColin Foster int ocelot_reset(struct ocelot *ocelot)
2962b67f5502SColin Foster {
2963b67f5502SColin Foster 	int err;
2964b67f5502SColin Foster 	u32 val;
2965b67f5502SColin Foster 
2966b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
2967b67f5502SColin Foster 	if (err)
2968b67f5502SColin Foster 		return err;
2969b67f5502SColin Foster 
2970b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
2971b67f5502SColin Foster 	if (err)
2972b67f5502SColin Foster 		return err;
2973b67f5502SColin Foster 
2974b67f5502SColin Foster 	/* MEM_INIT is a self-clearing bit. Wait for it to be cleared (should be
2975b67f5502SColin Foster 	 * 100us) before enabling the switch core.
2976b67f5502SColin Foster 	 */
2977b67f5502SColin Foster 	err = readx_poll_timeout(ocelot_mem_init_status, ocelot, val, !val,
2978b67f5502SColin Foster 				 MEM_INIT_SLEEP_US, MEM_INIT_TIMEOUT_US);
2979b67f5502SColin Foster 	if (err)
2980b67f5502SColin Foster 		return err;
2981b67f5502SColin Foster 
2982b67f5502SColin Foster 	err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
2983b67f5502SColin Foster 	if (err)
2984b67f5502SColin Foster 		return err;
2985b67f5502SColin Foster 
2986b67f5502SColin Foster 	return regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
2987b67f5502SColin Foster }
2988b67f5502SColin Foster EXPORT_SYMBOL(ocelot_reset);
2989b67f5502SColin Foster 
ocelot_init(struct ocelot * ocelot)2990a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2991a556c76aSAlexandre Belloni {
299221468199SVladimir Oltean 	int i, ret;
299321468199SVladimir Oltean 	u32 port;
2994a556c76aSAlexandre Belloni 
29953a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
29963a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
29973a77b593SVladimir Oltean 		if (ret) {
29983a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
29993a77b593SVladimir Oltean 			return ret;
30003a77b593SVladimir Oltean 		}
30013a77b593SVladimir Oltean 	}
30023a77b593SVladimir Oltean 
30032468346cSVladimir Oltean 	mutex_init(&ocelot->mact_lock);
30048abe1970SVladimir Oltean 	mutex_init(&ocelot->fwd_domain_lock);
30054e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
300652849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
3007e83b49ecSVladimir Oltean 	spin_lock_init(&ocelot->inj_lock);
3008e83b49ecSVladimir Oltean 	spin_lock_init(&ocelot->xtr_lock);
3009a556c76aSAlexandre Belloni 
3010ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
3011fe90104cSVladimir Oltean 	if (!ocelot->owq)
3012ca0b272bSVladimir Oltean 		return -ENOMEM;
3013fe90104cSVladimir Oltean 
3014fe90104cSVladimir Oltean 	ret = ocelot_stats_init(ocelot);
30156505b680SVladimir Oltean 	if (ret)
30166505b680SVladimir Oltean 		goto err_stats_init;
3017ca0b272bSVladimir Oltean 
30182b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
3019e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
302090e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
3021961d8b69SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->lag_fdbs);
3022f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
3023a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
3024a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
3025aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
30262d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
3027a556c76aSAlexandre Belloni 
302823e2c506SXiaoliang Yang 	if (ocelot->ops->psfp_init)
302923e2c506SXiaoliang Yang 		ocelot->ops->psfp_init(ocelot);
303023e2c506SXiaoliang Yang 
30316505b680SVladimir Oltean 	if (ocelot->mm_supported) {
30326505b680SVladimir Oltean 		ret = ocelot_mm_init(ocelot);
30336505b680SVladimir Oltean 		if (ret)
30346505b680SVladimir Oltean 			goto err_mm_init;
30356505b680SVladimir Oltean 	}
30366505b680SVladimir Oltean 
3037a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3038a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
3039a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
3040a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
3041a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
3042a556c76aSAlexandre Belloni 	}
3043a556c76aSAlexandre Belloni 
3044a556c76aSAlexandre Belloni 	/* Only use S-Tag */
3045a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
3046a556c76aSAlexandre Belloni 
3047a556c76aSAlexandre Belloni 	/* Aggregation mode */
3048a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
3049a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
3050a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
3051f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
3052f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
3053f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
3054f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
3055a556c76aSAlexandre Belloni 
3056a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
3057a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
3058a556c76aSAlexandre Belloni 	 */
3059a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
3060a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
3061a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
3062a556c76aSAlexandre Belloni 
3063a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
3064a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
3065a556c76aSAlexandre Belloni 
3066a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
3067a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
3068a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
3069a556c76aSAlexandre Belloni 
3070a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
3071edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
3072a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
3073b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
3074a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
3075edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
3076a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
3077a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
3078a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
3079a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
3080a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
3081a556c76aSAlexandre Belloni 
3082a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3083a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
3084a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
3085a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
3086a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
3087a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
3088a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
3089a556c76aSAlexandre Belloni 				 port);
3090a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
3091a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
3092a556c76aSAlexandre Belloni 	}
3093a556c76aSAlexandre Belloni 
309496b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
3095a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
3096a556c76aSAlexandre Belloni 
3097a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
3098a556c76aSAlexandre Belloni 	}
3099ebb1bb40SHoratiu Vultur 
3100ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
3101ebb1bb40SHoratiu Vultur 
3102b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
3103b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3104b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3105a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
3106b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3107b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3108b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
3109a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
3110a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
3111a556c76aSAlexandre Belloni 
3112a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
3113a556c76aSAlexandre Belloni 	 * registers endianness.
3114a556c76aSAlexandre Belloni 	 */
3115a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
3116a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
3117a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
3118a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
3119a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
3120a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
3121a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
3122a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
3123a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
3124a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
3125a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
3126a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
3127a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
3128a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
3129a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
3130a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
3131a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
3132a556c76aSAlexandre Belloni 
3133a556c76aSAlexandre Belloni 	return 0;
31346505b680SVladimir Oltean 
31356505b680SVladimir Oltean err_mm_init:
31366505b680SVladimir Oltean 	ocelot_stats_deinit(ocelot);
31376505b680SVladimir Oltean err_stats_init:
31386505b680SVladimir Oltean 	destroy_workqueue(ocelot->owq);
31396505b680SVladimir Oltean 	return ret;
3140a556c76aSAlexandre Belloni }
3141a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
3142a556c76aSAlexandre Belloni 
ocelot_deinit(struct ocelot * ocelot)3143a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
3144a556c76aSAlexandre Belloni {
3145fe90104cSVladimir Oltean 	ocelot_stats_deinit(ocelot);
3146ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
3147a556c76aSAlexandre Belloni }
3148a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
3149a556c76aSAlexandre Belloni 
ocelot_deinit_port(struct ocelot * ocelot,int port)3150e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
3151e5fb512dSVladimir Oltean {
3152e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3153e5fb512dSVladimir Oltean 
3154e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
3155e5fb512dSVladimir Oltean }
3156e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
3157e5fb512dSVladimir Oltean 
3158a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
3159