xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 4cf35a2b627a020fe1a6b6fc7a6a12394644e474)
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>
939e5308bSYangbo Lu #include <linux/ptp_classify.h>
1020968054SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
11a556c76aSAlexandre Belloni #include "ocelot.h"
123c83654fSVladimir Oltean #include "ocelot_vcap.h"
13a556c76aSAlexandre Belloni 
14639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
15639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
1654c31984SVladimir Oltean #define OCELOT_RSV_VLAN_RANGE_START 4000
17639c1b26SSteen Hegelund 
18a556c76aSAlexandre Belloni struct ocelot_mact_entry {
19a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
20a556c76aSAlexandre Belloni 	u16 vid;
21a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
22a556c76aSAlexandre Belloni };
23a556c76aSAlexandre Belloni 
242468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
25639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
26639c1b26SSteen Hegelund {
27639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
28639c1b26SSteen Hegelund }
29639c1b26SSteen Hegelund 
302468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
31a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
32a556c76aSAlexandre Belloni {
33639c1b26SSteen Hegelund 	u32 val;
34a556c76aSAlexandre Belloni 
35639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
36639c1b26SSteen Hegelund 		ocelot, val,
37639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
38639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
39639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
40a556c76aSAlexandre Belloni }
41a556c76aSAlexandre Belloni 
422468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
43a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
44a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
45a556c76aSAlexandre Belloni 			       unsigned int vid)
46a556c76aSAlexandre Belloni {
47a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
48a556c76aSAlexandre Belloni 
49a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
50a556c76aSAlexandre Belloni 	 * understood by the hardware.
51a556c76aSAlexandre Belloni 	 */
52a556c76aSAlexandre Belloni 	mach |= vid    << 16;
53a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
54a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
55a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
56a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
57a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
58a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
59a556c76aSAlexandre Belloni 
60a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
61a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
62a556c76aSAlexandre Belloni 
63a556c76aSAlexandre Belloni }
64a556c76aSAlexandre Belloni 
650568c3bfSXiaoliang Yang static int __ocelot_mact_learn(struct ocelot *ocelot, int port,
66a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
679c90eea3SVladimir Oltean 			       unsigned int vid, enum macaccess_entry_type type)
68a556c76aSAlexandre Belloni {
69584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
70584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
71584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
72584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
73584b7cfcSAlban Bedel 	unsigned int mc_ports;
742468346cSVladimir Oltean 	int err;
75584b7cfcSAlban Bedel 
76584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
77584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
78584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
79584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
80584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
81584b7cfcSAlban Bedel 	else
82584b7cfcSAlban Bedel 		mc_ports = 0;
83584b7cfcSAlban Bedel 
84584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
85584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
86584b7cfcSAlban Bedel 
87a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
88a556c76aSAlexandre Belloni 
89a556c76aSAlexandre Belloni 	/* Issue a write command */
90584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
91a556c76aSAlexandre Belloni 
922468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
932468346cSVladimir Oltean 
940568c3bfSXiaoliang Yang 	return err;
950568c3bfSXiaoliang Yang }
960568c3bfSXiaoliang Yang 
970568c3bfSXiaoliang Yang int ocelot_mact_learn(struct ocelot *ocelot, int port,
980568c3bfSXiaoliang Yang 		      const unsigned char mac[ETH_ALEN],
990568c3bfSXiaoliang Yang 		      unsigned int vid, enum macaccess_entry_type type)
1000568c3bfSXiaoliang Yang {
1010568c3bfSXiaoliang Yang 	int ret;
1020568c3bfSXiaoliang Yang 
1030568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1040568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, port, mac, vid, type);
1052468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1062468346cSVladimir Oltean 
1070568c3bfSXiaoliang Yang 	return ret;
108a556c76aSAlexandre Belloni }
1099c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
110a556c76aSAlexandre Belloni 
1119c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
1129c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
113a556c76aSAlexandre Belloni {
1142468346cSVladimir Oltean 	int err;
1152468346cSVladimir Oltean 
1162468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
1172468346cSVladimir Oltean 
118a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
119a556c76aSAlexandre Belloni 
120a556c76aSAlexandre Belloni 	/* Issue a forget command */
121a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
122a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
123a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
124a556c76aSAlexandre Belloni 
1252468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
1262468346cSVladimir Oltean 
1272468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1282468346cSVladimir Oltean 
1292468346cSVladimir Oltean 	return err;
130a556c76aSAlexandre Belloni }
1319c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
132a556c76aSAlexandre Belloni 
1330568c3bfSXiaoliang Yang int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx,
1340568c3bfSXiaoliang Yang 		       const unsigned char mac[ETH_ALEN],
1350568c3bfSXiaoliang Yang 		       unsigned int vid, enum macaccess_entry_type *type)
1360568c3bfSXiaoliang Yang {
1370568c3bfSXiaoliang Yang 	int val;
1380568c3bfSXiaoliang Yang 
1390568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1400568c3bfSXiaoliang Yang 
1410568c3bfSXiaoliang Yang 	ocelot_mact_select(ocelot, mac, vid);
1420568c3bfSXiaoliang Yang 
1430568c3bfSXiaoliang Yang 	/* Issue a read command with MACACCESS_VALID=1. */
1440568c3bfSXiaoliang Yang 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
1450568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1460568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS);
1470568c3bfSXiaoliang Yang 
1480568c3bfSXiaoliang Yang 	if (ocelot_mact_wait_for_completion(ocelot)) {
1490568c3bfSXiaoliang Yang 		mutex_unlock(&ocelot->mact_lock);
1500568c3bfSXiaoliang Yang 		return -ETIMEDOUT;
1510568c3bfSXiaoliang Yang 	}
1520568c3bfSXiaoliang Yang 
1530568c3bfSXiaoliang Yang 	/* Read back the entry flags */
1540568c3bfSXiaoliang Yang 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1550568c3bfSXiaoliang Yang 
1560568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1570568c3bfSXiaoliang Yang 
1580568c3bfSXiaoliang Yang 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1590568c3bfSXiaoliang Yang 		return -ENOENT;
1600568c3bfSXiaoliang Yang 
1610568c3bfSXiaoliang Yang 	*dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val);
1620568c3bfSXiaoliang Yang 	*type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val);
1630568c3bfSXiaoliang Yang 
1640568c3bfSXiaoliang Yang 	return 0;
1650568c3bfSXiaoliang Yang }
1660568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_lookup);
1670568c3bfSXiaoliang Yang 
1680568c3bfSXiaoliang Yang int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx,
1690568c3bfSXiaoliang Yang 				 const unsigned char mac[ETH_ALEN],
1700568c3bfSXiaoliang Yang 				 unsigned int vid,
1710568c3bfSXiaoliang Yang 				 enum macaccess_entry_type type,
1720568c3bfSXiaoliang Yang 				 int sfid, int ssid)
1730568c3bfSXiaoliang Yang {
1740568c3bfSXiaoliang Yang 	int ret;
1750568c3bfSXiaoliang Yang 
1760568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1770568c3bfSXiaoliang Yang 
1780568c3bfSXiaoliang Yang 	ocelot_write(ocelot,
1790568c3bfSXiaoliang Yang 		     (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) |
1800568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SFID(sfid) |
1810568c3bfSXiaoliang Yang 		     (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) |
1820568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SSID(ssid),
1830568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA);
1840568c3bfSXiaoliang Yang 
1850568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type);
1860568c3bfSXiaoliang Yang 
1870568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1880568c3bfSXiaoliang Yang 
1890568c3bfSXiaoliang Yang 	return ret;
1900568c3bfSXiaoliang Yang }
1910568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_learn_streamdata);
1920568c3bfSXiaoliang Yang 
193a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
194a556c76aSAlexandre Belloni {
195a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
196a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
197a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
198a556c76aSAlexandre Belloni 	 */
199a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
200a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
201a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
202a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
203a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
204a556c76aSAlexandre Belloni 
2052468346cSVladimir Oltean 	/* Clear the MAC table. We are not concurrent with anyone, so
2062468346cSVladimir Oltean 	 * holding &ocelot->mact_lock is pointless.
2072468346cSVladimir Oltean 	 */
208a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
209a556c76aSAlexandre Belloni }
210a556c76aSAlexandre Belloni 
211f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
212b5962294SHoratiu Vultur {
213b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
214b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
215f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
21675944fdaSXiaoliang Yang 
21775944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
21875944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
2192f17c050SXiaoliang Yang 
2202f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
2212f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
2222f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
223b5962294SHoratiu Vultur }
224b5962294SHoratiu Vultur 
22554c31984SVladimir Oltean static int ocelot_single_vlan_aware_bridge(struct ocelot *ocelot,
22654c31984SVladimir Oltean 					   struct netlink_ext_ack *extack)
22754c31984SVladimir Oltean {
22854c31984SVladimir Oltean 	struct net_device *bridge = NULL;
22954c31984SVladimir Oltean 	int port;
23054c31984SVladimir Oltean 
23154c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
23254c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
23354c31984SVladimir Oltean 
23454c31984SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bridge ||
23554c31984SVladimir Oltean 		    !br_vlan_enabled(ocelot_port->bridge))
23654c31984SVladimir Oltean 			continue;
23754c31984SVladimir Oltean 
23854c31984SVladimir Oltean 		if (!bridge) {
23954c31984SVladimir Oltean 			bridge = ocelot_port->bridge;
24054c31984SVladimir Oltean 			continue;
24154c31984SVladimir Oltean 		}
24254c31984SVladimir Oltean 
24354c31984SVladimir Oltean 		if (bridge == ocelot_port->bridge)
24454c31984SVladimir Oltean 			continue;
24554c31984SVladimir Oltean 
24654c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
24754c31984SVladimir Oltean 				   "Only one VLAN-aware bridge is supported");
24854c31984SVladimir Oltean 		return -EBUSY;
24954c31984SVladimir Oltean 	}
25054c31984SVladimir Oltean 
25154c31984SVladimir Oltean 	return 0;
25254c31984SVladimir Oltean }
25354c31984SVladimir Oltean 
254639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
255639c1b26SSteen Hegelund {
256639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
257639c1b26SSteen Hegelund }
258639c1b26SSteen Hegelund 
259a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
260a556c76aSAlexandre Belloni {
261639c1b26SSteen Hegelund 	u32 val;
262a556c76aSAlexandre Belloni 
263639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
264639c1b26SSteen Hegelund 		ocelot,
265639c1b26SSteen Hegelund 		val,
266639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
267639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
268639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
269a556c76aSAlexandre Belloni }
270a556c76aSAlexandre Belloni 
2717142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
2727142529fSAntoine Tenart {
2737142529fSAntoine Tenart 	/* Select the VID to configure */
2747142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
2757142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
2767142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
2777142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
2787142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
2797142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
2807142529fSAntoine Tenart 
2817142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
2827142529fSAntoine Tenart }
2837142529fSAntoine Tenart 
2840da1a1c4SVladimir Oltean static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
2850da1a1c4SVladimir Oltean {
2860da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
2870da1a1c4SVladimir Oltean 	int num_untagged = 0;
2880da1a1c4SVladimir Oltean 
2890da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
2900da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
2910da1a1c4SVladimir Oltean 			continue;
2920da1a1c4SVladimir Oltean 
2930da1a1c4SVladimir Oltean 		if (vlan->untagged & BIT(port))
2940da1a1c4SVladimir Oltean 			num_untagged++;
2950da1a1c4SVladimir Oltean 	}
2960da1a1c4SVladimir Oltean 
2970da1a1c4SVladimir Oltean 	return num_untagged;
2980da1a1c4SVladimir Oltean }
2990da1a1c4SVladimir Oltean 
3000da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
3010da1a1c4SVladimir Oltean {
3020da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3030da1a1c4SVladimir Oltean 	int num_tagged = 0;
3040da1a1c4SVladimir Oltean 
3050da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
3060da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
3070da1a1c4SVladimir Oltean 			continue;
3080da1a1c4SVladimir Oltean 
3090da1a1c4SVladimir Oltean 		if (!(vlan->untagged & BIT(port)))
3100da1a1c4SVladimir Oltean 			num_tagged++;
3110da1a1c4SVladimir Oltean 	}
3120da1a1c4SVladimir Oltean 
3130da1a1c4SVladimir Oltean 	return num_tagged;
3140da1a1c4SVladimir Oltean }
3150da1a1c4SVladimir Oltean 
3160da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
3170da1a1c4SVladimir Oltean  * _one_ egress-untagged VLAN (_the_ native VLAN)
3180da1a1c4SVladimir Oltean  */
3190da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
3200da1a1c4SVladimir Oltean {
3210da1a1c4SVladimir Oltean 	return ocelot_port_num_tagged_vlans(ocelot, port) &&
3220da1a1c4SVladimir Oltean 	       ocelot_port_num_untagged_vlans(ocelot, port) == 1;
3230da1a1c4SVladimir Oltean }
3240da1a1c4SVladimir Oltean 
3250da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan *
3260da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
3270da1a1c4SVladimir Oltean {
3280da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
3290da1a1c4SVladimir Oltean 
3300da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
3310da1a1c4SVladimir Oltean 		if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
3320da1a1c4SVladimir Oltean 			return vlan;
3330da1a1c4SVladimir Oltean 
3340da1a1c4SVladimir Oltean 	return NULL;
3350da1a1c4SVladimir Oltean }
3360da1a1c4SVladimir Oltean 
3370da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
3380da1a1c4SVladimir Oltean  * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
3390da1a1c4SVladimir Oltean  * state of the port.
3400da1a1c4SVladimir Oltean  */
3410da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
34297bb69e1SVladimir Oltean {
34397bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
34462a22bcbSVladimir Oltean 	enum ocelot_port_tag_config tag_cfg;
3450da1a1c4SVladimir Oltean 	bool uses_native_vlan = false;
34697bb69e1SVladimir Oltean 
34787b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
3480da1a1c4SVladimir Oltean 		uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
3490da1a1c4SVladimir Oltean 
3500da1a1c4SVladimir Oltean 		if (uses_native_vlan)
35162a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_NATIVE;
3520da1a1c4SVladimir Oltean 		else if (ocelot_port_num_untagged_vlans(ocelot, port))
3530da1a1c4SVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_DISABLED;
35487b0f983SVladimir Oltean 		else
35562a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_TRUNK;
35687b0f983SVladimir Oltean 	} else {
35762a22bcbSVladimir Oltean 		tag_cfg = OCELOT_PORT_TAG_DISABLED;
35887b0f983SVladimir Oltean 	}
3590da1a1c4SVladimir Oltean 
36062a22bcbSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
36187b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
36287b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
3630da1a1c4SVladimir Oltean 
3640da1a1c4SVladimir Oltean 	if (uses_native_vlan) {
3650da1a1c4SVladimir Oltean 		struct ocelot_bridge_vlan *native_vlan;
3660da1a1c4SVladimir Oltean 
3670da1a1c4SVladimir Oltean 		/* Not having a native VLAN is impossible, because
3680da1a1c4SVladimir Oltean 		 * ocelot_port_num_untagged_vlans has returned 1.
3690da1a1c4SVladimir Oltean 		 * So there is no use in checking for NULL here.
3700da1a1c4SVladimir Oltean 		 */
3710da1a1c4SVladimir Oltean 		native_vlan = ocelot_port_find_native_vlan(ocelot, port);
3720da1a1c4SVladimir Oltean 
3730da1a1c4SVladimir Oltean 		ocelot_rmw_gix(ocelot,
3740da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
3750da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID_M,
3760da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG, port);
3770da1a1c4SVladimir Oltean 	}
37897bb69e1SVladimir Oltean }
37997bb69e1SVladimir Oltean 
38054c31984SVladimir Oltean int ocelot_bridge_num_find(struct ocelot *ocelot,
38154c31984SVladimir Oltean 			   const struct net_device *bridge)
38254c31984SVladimir Oltean {
38354c31984SVladimir Oltean 	int port;
38454c31984SVladimir Oltean 
38554c31984SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
38654c31984SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
38754c31984SVladimir Oltean 
38854c31984SVladimir Oltean 		if (ocelot_port && ocelot_port->bridge == bridge)
38954c31984SVladimir Oltean 			return ocelot_port->bridge_num;
39054c31984SVladimir Oltean 	}
39154c31984SVladimir Oltean 
39254c31984SVladimir Oltean 	return -1;
39354c31984SVladimir Oltean }
39454c31984SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_bridge_num_find);
39554c31984SVladimir Oltean 
39654c31984SVladimir Oltean static u16 ocelot_vlan_unaware_pvid(struct ocelot *ocelot,
39754c31984SVladimir Oltean 				    const struct net_device *bridge)
39854c31984SVladimir Oltean {
39954c31984SVladimir Oltean 	int bridge_num;
40054c31984SVladimir Oltean 
40154c31984SVladimir Oltean 	/* Standalone ports use VID 0 */
40254c31984SVladimir Oltean 	if (!bridge)
40354c31984SVladimir Oltean 		return 0;
40454c31984SVladimir Oltean 
40554c31984SVladimir Oltean 	bridge_num = ocelot_bridge_num_find(ocelot, bridge);
40654c31984SVladimir Oltean 	if (WARN_ON(bridge_num < 0))
40754c31984SVladimir Oltean 		return 0;
40854c31984SVladimir Oltean 
40954c31984SVladimir Oltean 	/* VLAN-unaware bridges use a reserved VID going from 4095 downwards */
41054c31984SVladimir Oltean 	return VLAN_N_VID - bridge_num - 1;
41154c31984SVladimir Oltean }
41254c31984SVladimir Oltean 
41375e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
414c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
415d4004422SVladimir Oltean 				 const struct ocelot_bridge_vlan *pvid_vlan)
41675e5a554SVladimir Oltean {
41775e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
41854c31984SVladimir Oltean 	u16 pvid = ocelot_vlan_unaware_pvid(ocelot, ocelot_port->bridge);
419be0576feSVladimir Oltean 	u32 val = 0;
42075e5a554SVladimir Oltean 
421c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
42275e5a554SVladimir Oltean 
423d4004422SVladimir Oltean 	if (ocelot_port->vlan_aware && pvid_vlan)
424d4004422SVladimir Oltean 		pvid = pvid_vlan->vid;
42575e5a554SVladimir Oltean 
42675e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
427d4004422SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
42875e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
42975e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
430be0576feSVladimir Oltean 
431be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
432be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
433be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
434be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
435be0576feSVladimir Oltean 	 */
436d4004422SVladimir Oltean 	if (!pvid_vlan && ocelot_port->vlan_aware)
437be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
438be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
439be0576feSVladimir Oltean 
440be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
441be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
442be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
443be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
44475e5a554SVladimir Oltean }
44575e5a554SVladimir Oltean 
44690e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
44790e0aa8dSVladimir Oltean 							  u16 vid)
448bbf6a2d9SVladimir Oltean {
44990e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
450bbf6a2d9SVladimir Oltean 
45190e0aa8dSVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
45290e0aa8dSVladimir Oltean 		if (vlan->vid == vid)
45390e0aa8dSVladimir Oltean 			return vlan;
454bbf6a2d9SVladimir Oltean 
45590e0aa8dSVladimir Oltean 	return NULL;
456bbf6a2d9SVladimir Oltean }
457bbf6a2d9SVladimir Oltean 
4580da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
4590da1a1c4SVladimir Oltean 				  bool untagged)
460bbf6a2d9SVladimir Oltean {
46190e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
46290e0aa8dSVladimir Oltean 	unsigned long portmask;
46390e0aa8dSVladimir Oltean 	int err;
46490e0aa8dSVladimir Oltean 
46590e0aa8dSVladimir Oltean 	if (vlan) {
46690e0aa8dSVladimir Oltean 		portmask = vlan->portmask | BIT(port);
46790e0aa8dSVladimir Oltean 
46890e0aa8dSVladimir Oltean 		err = ocelot_vlant_set_mask(ocelot, vid, portmask);
46990e0aa8dSVladimir Oltean 		if (err)
47090e0aa8dSVladimir Oltean 			return err;
47190e0aa8dSVladimir Oltean 
47290e0aa8dSVladimir Oltean 		vlan->portmask = portmask;
4730da1a1c4SVladimir Oltean 		/* Bridge VLANs can be overwritten with a different
4740da1a1c4SVladimir Oltean 		 * egress-tagging setting, so make sure to override an untagged
4750da1a1c4SVladimir Oltean 		 * with a tagged VID if that's going on.
4760da1a1c4SVladimir Oltean 		 */
4770da1a1c4SVladimir Oltean 		if (untagged)
4780da1a1c4SVladimir Oltean 			vlan->untagged |= BIT(port);
4790da1a1c4SVladimir Oltean 		else
4800da1a1c4SVladimir Oltean 			vlan->untagged &= ~BIT(port);
48190e0aa8dSVladimir Oltean 
48290e0aa8dSVladimir Oltean 		return 0;
48390e0aa8dSVladimir Oltean 	}
48490e0aa8dSVladimir Oltean 
48590e0aa8dSVladimir Oltean 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
48690e0aa8dSVladimir Oltean 	if (!vlan)
48790e0aa8dSVladimir Oltean 		return -ENOMEM;
48890e0aa8dSVladimir Oltean 
48990e0aa8dSVladimir Oltean 	portmask = BIT(port);
49090e0aa8dSVladimir Oltean 
49190e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
49290e0aa8dSVladimir Oltean 	if (err) {
49390e0aa8dSVladimir Oltean 		kfree(vlan);
49490e0aa8dSVladimir Oltean 		return err;
49590e0aa8dSVladimir Oltean 	}
49690e0aa8dSVladimir Oltean 
49790e0aa8dSVladimir Oltean 	vlan->vid = vid;
49890e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
4990da1a1c4SVladimir Oltean 	if (untagged)
5000da1a1c4SVladimir Oltean 		vlan->untagged = BIT(port);
50190e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&vlan->list);
50290e0aa8dSVladimir Oltean 	list_add_tail(&vlan->list, &ocelot->vlans);
50390e0aa8dSVladimir Oltean 
50490e0aa8dSVladimir Oltean 	return 0;
505bbf6a2d9SVladimir Oltean }
506bbf6a2d9SVladimir Oltean 
507bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
508bbf6a2d9SVladimir Oltean {
50990e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
51090e0aa8dSVladimir Oltean 	unsigned long portmask;
51190e0aa8dSVladimir Oltean 	int err;
51290e0aa8dSVladimir Oltean 
51390e0aa8dSVladimir Oltean 	if (!vlan)
51490e0aa8dSVladimir Oltean 		return 0;
51590e0aa8dSVladimir Oltean 
51690e0aa8dSVladimir Oltean 	portmask = vlan->portmask & ~BIT(port);
51790e0aa8dSVladimir Oltean 
51890e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
51990e0aa8dSVladimir Oltean 	if (err)
52090e0aa8dSVladimir Oltean 		return err;
52190e0aa8dSVladimir Oltean 
52290e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
52390e0aa8dSVladimir Oltean 	if (vlan->portmask)
52490e0aa8dSVladimir Oltean 		return 0;
52590e0aa8dSVladimir Oltean 
52690e0aa8dSVladimir Oltean 	list_del(&vlan->list);
52790e0aa8dSVladimir Oltean 	kfree(vlan);
52890e0aa8dSVladimir Oltean 
52990e0aa8dSVladimir Oltean 	return 0;
530bbf6a2d9SVladimir Oltean }
531bbf6a2d9SVladimir Oltean 
53254c31984SVladimir Oltean static int ocelot_add_vlan_unaware_pvid(struct ocelot *ocelot, int port,
53354c31984SVladimir Oltean 					const struct net_device *bridge)
53454c31984SVladimir Oltean {
53554c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
53654c31984SVladimir Oltean 
53754c31984SVladimir Oltean 	return ocelot_vlan_member_add(ocelot, port, vid, true);
53854c31984SVladimir Oltean }
53954c31984SVladimir Oltean 
54054c31984SVladimir Oltean static int ocelot_del_vlan_unaware_pvid(struct ocelot *ocelot, int port,
54154c31984SVladimir Oltean 					const struct net_device *bridge)
54254c31984SVladimir Oltean {
54354c31984SVladimir Oltean 	u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
54454c31984SVladimir Oltean 
54554c31984SVladimir Oltean 	return ocelot_vlan_member_del(ocelot, port, vid);
54654c31984SVladimir Oltean }
54754c31984SVladimir Oltean 
5482e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
5493b95d1b2SVladimir Oltean 			       bool vlan_aware, struct netlink_ext_ack *extack)
55087b0f983SVladimir Oltean {
55170edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
552bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
55370edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
55454c31984SVladimir Oltean 	int err;
555bae33f2bSVladimir Oltean 	u32 val;
55670edfae1SVladimir Oltean 
55770edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
55870edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
55970edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
5603b95d1b2SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
5613b95d1b2SVladimir Oltean 					   "Cannot change VLAN state with vlan modify rules active");
56270edfae1SVladimir Oltean 			return -EBUSY;
56370edfae1SVladimir Oltean 		}
56470edfae1SVladimir Oltean 	}
56570edfae1SVladimir Oltean 
56654c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
56754c31984SVladimir Oltean 	if (err)
56854c31984SVladimir Oltean 		return err;
56954c31984SVladimir Oltean 
57054c31984SVladimir Oltean 	if (vlan_aware)
57154c31984SVladimir Oltean 		err = ocelot_del_vlan_unaware_pvid(ocelot, port,
57254c31984SVladimir Oltean 						   ocelot_port->bridge);
57354c31984SVladimir Oltean 	else
57454c31984SVladimir Oltean 		err = ocelot_add_vlan_unaware_pvid(ocelot, port,
57554c31984SVladimir Oltean 						   ocelot_port->bridge);
57654c31984SVladimir Oltean 	if (err)
57754c31984SVladimir Oltean 		return err;
57854c31984SVladimir Oltean 
57987b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
58087b0f983SVladimir Oltean 
58187b0f983SVladimir Oltean 	if (vlan_aware)
58287b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
58387b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
58487b0f983SVladimir Oltean 	else
58587b0f983SVladimir Oltean 		val = 0;
58687b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
58787b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
58887b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
58987b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
59087b0f983SVladimir Oltean 
591c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
5920da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
5932e554a7aSVladimir Oltean 
5942e554a7aSVladimir Oltean 	return 0;
59587b0f983SVladimir Oltean }
59687b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
59787b0f983SVladimir Oltean 
5982f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
59901af940eSVladimir Oltean 			bool untagged, struct netlink_ext_ack *extack)
6002f0402feSVladimir Oltean {
6010da1a1c4SVladimir Oltean 	if (untagged) {
6020da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6030da1a1c4SVladimir Oltean 		if (ocelot_port_uses_native_vlan(ocelot, port)) {
60401af940eSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6050da1a1c4SVladimir Oltean 					   "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
6062f0402feSVladimir Oltean 			return -EBUSY;
6072f0402feSVladimir Oltean 		}
6080da1a1c4SVladimir Oltean 	} else {
6090da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
6100da1a1c4SVladimir Oltean 		if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
6110da1a1c4SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
6120da1a1c4SVladimir Oltean 					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
6130da1a1c4SVladimir Oltean 			return -EBUSY;
6140da1a1c4SVladimir Oltean 		}
6150da1a1c4SVladimir Oltean 	}
6162f0402feSVladimir Oltean 
61754c31984SVladimir Oltean 	if (vid > OCELOT_RSV_VLAN_RANGE_START) {
61854c31984SVladimir Oltean 		NL_SET_ERR_MSG_MOD(extack,
61954c31984SVladimir Oltean 				   "VLAN range 4000-4095 reserved for VLAN-unaware bridging");
62054c31984SVladimir Oltean 		return -EBUSY;
62154c31984SVladimir Oltean 	}
62254c31984SVladimir Oltean 
6232f0402feSVladimir Oltean 	return 0;
6242f0402feSVladimir Oltean }
6252f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
6262f0402feSVladimir Oltean 
6275e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
6287142529fSAntoine Tenart 		    bool untagged)
6297142529fSAntoine Tenart {
630bbf6a2d9SVladimir Oltean 	int err;
6317142529fSAntoine Tenart 
6320da1a1c4SVladimir Oltean 	err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
633bbf6a2d9SVladimir Oltean 	if (err)
634bbf6a2d9SVladimir Oltean 		return err;
6357142529fSAntoine Tenart 
6367142529fSAntoine Tenart 	/* Default ingress vlan classification */
637d4004422SVladimir Oltean 	if (pvid)
638d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port,
639d4004422SVladimir Oltean 				     ocelot_bridge_vlan_find(ocelot, vid));
6407142529fSAntoine Tenart 
6417142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
6420da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6437142529fSAntoine Tenart 
6447142529fSAntoine Tenart 	return 0;
6457142529fSAntoine Tenart }
6465e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
6477142529fSAntoine Tenart 
6485e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
6499855934cSVladimir Oltean {
6509855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
651ef576405SVladimir Oltean 	bool del_pvid = false;
652bbf6a2d9SVladimir Oltean 	int err;
6537142529fSAntoine Tenart 
654ef576405SVladimir Oltean 	if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
655ef576405SVladimir Oltean 		del_pvid = true;
656ef576405SVladimir Oltean 
657bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
658bbf6a2d9SVladimir Oltean 	if (err)
659bbf6a2d9SVladimir Oltean 		return err;
6607142529fSAntoine Tenart 
661be0576feSVladimir Oltean 	/* Ingress */
662ef576405SVladimir Oltean 	if (del_pvid)
663d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, NULL);
664be0576feSVladimir Oltean 
6657142529fSAntoine Tenart 	/* Egress */
6660da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
6677142529fSAntoine Tenart 
6687142529fSAntoine Tenart 	return 0;
6697142529fSAntoine Tenart }
6705e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
6717142529fSAntoine Tenart 
672a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
673a556c76aSAlexandre Belloni {
674bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
6757142529fSAntoine Tenart 	u16 port, vid;
6767142529fSAntoine Tenart 
677a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
678a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
679a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
680a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
6817142529fSAntoine Tenart 
6827142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
683bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
68490e0aa8dSVladimir Oltean 		ocelot_vlant_set_mask(ocelot, vid, 0);
6857142529fSAntoine Tenart 
68654c31984SVladimir Oltean 	/* We need VID 0 to get traffic on standalone ports.
68754c31984SVladimir Oltean 	 * It is added automatically if the 8021q module is loaded, but we
68854c31984SVladimir Oltean 	 * can't rely on that since it might not be.
6897142529fSAntoine Tenart 	 */
69054c31984SVladimir Oltean 	ocelot_vlant_set_mask(ocelot, OCELOT_STANDALONE_PVID, all_ports);
6917142529fSAntoine Tenart 
6927142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
6937142529fSAntoine Tenart 	 * default.
6947142529fSAntoine Tenart 	 */
695bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
6967142529fSAntoine Tenart 
6977142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
6987142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
6997142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
7007142529fSAntoine Tenart 	}
701a556c76aSAlexandre Belloni }
702a556c76aSAlexandre Belloni 
703eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
704eb4733d7SVladimir Oltean {
705eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
706eb4733d7SVladimir Oltean }
707eb4733d7SVladimir Oltean 
708e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
709eb4733d7SVladimir Oltean {
7101650bdb1SVladimir Oltean 	unsigned int pause_ena;
711eb4733d7SVladimir Oltean 	int err, val;
712eb4733d7SVladimir Oltean 
713eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
714eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
715eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
716eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
717eb4733d7SVladimir Oltean 
718eb4733d7SVladimir Oltean 	/* Disable flow control */
7191650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
720eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
721eb4733d7SVladimir Oltean 
722eb4733d7SVladimir Oltean 	/* Disable priority flow control */
723eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
724eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
725eb4733d7SVladimir Oltean 
726eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
727eb4733d7SVladimir Oltean 	 * at the port.
728eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
729eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
730eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
731eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
732eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
733eb4733d7SVladimir Oltean 	 */
734eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
735eb4733d7SVladimir Oltean 
736eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
737eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
738eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
739eb4733d7SVladimir Oltean 
740eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
741eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
742eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
743eb4733d7SVladimir Oltean 
744eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
745eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
746eb4733d7SVladimir Oltean 		       port);
747eb4733d7SVladimir Oltean 
748eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
749eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
750eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
751eb4733d7SVladimir Oltean 
752eb4733d7SVladimir Oltean 	/* Clear flushing again. */
753eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
754eb4733d7SVladimir Oltean 
7551650bdb1SVladimir Oltean 	/* Re-enable flow control */
7561650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
7571650bdb1SVladimir Oltean 
758eb4733d7SVladimir Oltean 	return err;
759eb4733d7SVladimir Oltean }
760eb4733d7SVladimir Oltean 
761e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
762e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
763e6e12df6SVladimir Oltean 				  phy_interface_t interface,
764e6e12df6SVladimir Oltean 				  unsigned long quirks)
765a556c76aSAlexandre Belloni {
76626f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
767e6e12df6SVladimir Oltean 	int err;
768a556c76aSAlexandre Belloni 
7698abe1970SVladimir Oltean 	ocelot_port->speed = SPEED_UNKNOWN;
7708abe1970SVladimir Oltean 
771e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
772e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
773e6e12df6SVladimir Oltean 
7748abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
7758abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
7768abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
7778abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
7788abe1970SVladimir Oltean 	}
7798abe1970SVladimir Oltean 
780e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
781e6e12df6SVladimir Oltean 
782e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
783e6e12df6SVladimir Oltean 	if (err)
784e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
785e6e12df6SVladimir Oltean 			port, err);
786e6e12df6SVladimir Oltean 
787e6e12df6SVladimir Oltean 	/* Put the port in reset. */
788e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
789e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
790e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
791e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
79274a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
793e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
79474a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
795e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
796e6e12df6SVladimir Oltean }
797e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
798e6e12df6SVladimir Oltean 
799e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
800e6e12df6SVladimir Oltean 				struct phy_device *phydev,
801e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
802e6e12df6SVladimir Oltean 				phy_interface_t interface,
803e6e12df6SVladimir Oltean 				int speed, int duplex,
804e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
805e6e12df6SVladimir Oltean 				unsigned long quirks)
806e6e12df6SVladimir Oltean {
807e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
808e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
809e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
810e6e12df6SVladimir Oltean 
8118abe1970SVladimir Oltean 	ocelot_port->speed = speed;
8128abe1970SVladimir Oltean 
813e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
814e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
815e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
816e6e12df6SVladimir Oltean 	 * (which is also its default value).
817e6e12df6SVladimir Oltean 	 */
818e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
819e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
820e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
821e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
822e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
823e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
824e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
825e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
826e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
827e6e12df6SVladimir Oltean 	} else {
828e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
829e6e12df6SVladimir Oltean 	}
830e6e12df6SVladimir Oltean 
831e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
832e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
833e6e12df6SVladimir Oltean 
834e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
835e6e12df6SVladimir Oltean 
836e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
837e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
838e6e12df6SVladimir Oltean 	 */
839e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
840e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
841e6e12df6SVladimir Oltean 
842e6e12df6SVladimir Oltean 	switch (speed) {
843a556c76aSAlexandre Belloni 	case SPEED_10:
844e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
845a556c76aSAlexandre Belloni 		break;
846a556c76aSAlexandre Belloni 	case SPEED_100:
847e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
848a556c76aSAlexandre Belloni 		break;
849a556c76aSAlexandre Belloni 	case SPEED_1000:
850a556c76aSAlexandre Belloni 	case SPEED_2500:
851e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
852a556c76aSAlexandre Belloni 		break;
853a556c76aSAlexandre Belloni 	default:
854e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
855e6e12df6SVladimir Oltean 			port, speed);
856a556c76aSAlexandre Belloni 		return;
857a556c76aSAlexandre Belloni 	}
858a556c76aSAlexandre Belloni 
859e6e12df6SVladimir Oltean 	/* Handle RX pause in all cases, with 2500base-X this is used for rate
860e6e12df6SVladimir Oltean 	 * adaptation.
861e6e12df6SVladimir Oltean 	 */
862e6e12df6SVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
863a556c76aSAlexandre Belloni 
864e6e12df6SVladimir Oltean 	if (tx_pause)
865e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
866e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
867e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
868e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
869a556c76aSAlexandre Belloni 
870e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
871e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
872e6e12df6SVladimir Oltean 	 */
873e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
874a556c76aSAlexandre Belloni 
875e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
8761ba8f656SVladimir Oltean 
87733cb0ff3SVladimir Oltean 	/* Don't attempt to send PAUSE frames on the NPI port, it's broken */
87833cb0ff3SVladimir Oltean 	if (port != ocelot->npi)
87933cb0ff3SVladimir Oltean 		ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
88033cb0ff3SVladimir Oltean 				    tx_pause);
8811ba8f656SVladimir Oltean 
882e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
883e6e12df6SVladimir Oltean 	 * enable MAC module
884e6e12df6SVladimir Oltean 	 */
885004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
886a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
887a556c76aSAlexandre Belloni 
8888abe1970SVladimir Oltean 	/* If the port supports cut-through forwarding, update the masks before
8898abe1970SVladimir Oltean 	 * enabling forwarding on the port.
8908abe1970SVladimir Oltean 	 */
8918abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
8928abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
8938abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
8948abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
8958abe1970SVladimir Oltean 	}
8968abe1970SVladimir Oltean 
897a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
898886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
899886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
900a556c76aSAlexandre Belloni }
901e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
902889b8950SVladimir Oltean 
90352849bcfSVladimir Oltean static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
904e2f9a8feSVladimir Oltean 					struct sk_buff *clone)
905400928bfSYangbo Lu {
906e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
90752849bcfSVladimir Oltean 	unsigned long flags;
908400928bfSYangbo Lu 
90952849bcfSVladimir Oltean 	spin_lock_irqsave(&ocelot->ts_id_lock, flags);
91052849bcfSVladimir Oltean 
91152849bcfSVladimir Oltean 	if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
91252849bcfSVladimir Oltean 	    ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
91352849bcfSVladimir Oltean 		spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
91452849bcfSVladimir Oltean 		return -EBUSY;
91552849bcfSVladimir Oltean 	}
9166565243cSVladimir Oltean 
917e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
918c4b364ceSYangbo Lu 	/* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
919c4b364ceSYangbo Lu 	OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
92052849bcfSVladimir Oltean 
921c57fe003SVladimir Oltean 	ocelot_port->ts_id++;
922c57fe003SVladimir Oltean 	if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
923c57fe003SVladimir Oltean 		ocelot_port->ts_id = 0;
92452849bcfSVladimir Oltean 
92552849bcfSVladimir Oltean 	ocelot_port->ptp_skbs_in_flight++;
92652849bcfSVladimir Oltean 	ocelot->ptp_skbs_in_flight++;
92752849bcfSVladimir Oltean 
928e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
9296565243cSVladimir Oltean 
93052849bcfSVladimir Oltean 	spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
93152849bcfSVladimir Oltean 
93252849bcfSVladimir Oltean 	return 0;
933400928bfSYangbo Lu }
934682eaad9SYangbo Lu 
935fba01283SVladimir Oltean static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
936fba01283SVladimir Oltean 				       unsigned int ptp_class)
93739e5308bSYangbo Lu {
93839e5308bSYangbo Lu 	struct ptp_header *hdr;
93939e5308bSYangbo Lu 	u8 msgtype, twostep;
94039e5308bSYangbo Lu 
94139e5308bSYangbo Lu 	hdr = ptp_parse_header(skb, ptp_class);
94239e5308bSYangbo Lu 	if (!hdr)
94339e5308bSYangbo Lu 		return false;
94439e5308bSYangbo Lu 
94539e5308bSYangbo Lu 	msgtype = ptp_get_msgtype(hdr, ptp_class);
94639e5308bSYangbo Lu 	twostep = hdr->flag_field[0] & 0x2;
94739e5308bSYangbo Lu 
94839e5308bSYangbo Lu 	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
94939e5308bSYangbo Lu 		return true;
95039e5308bSYangbo Lu 
95139e5308bSYangbo Lu 	return false;
95239e5308bSYangbo Lu }
95339e5308bSYangbo Lu 
954682eaad9SYangbo Lu int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
955682eaad9SYangbo Lu 				 struct sk_buff *skb,
956682eaad9SYangbo Lu 				 struct sk_buff **clone)
957682eaad9SYangbo Lu {
958682eaad9SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
959682eaad9SYangbo Lu 	u8 ptp_cmd = ocelot_port->ptp_cmd;
960fba01283SVladimir Oltean 	unsigned int ptp_class;
96152849bcfSVladimir Oltean 	int err;
962682eaad9SYangbo Lu 
963fba01283SVladimir Oltean 	/* Don't do anything if PTP timestamping not enabled */
964fba01283SVladimir Oltean 	if (!ptp_cmd)
965fba01283SVladimir Oltean 		return 0;
966fba01283SVladimir Oltean 
967fba01283SVladimir Oltean 	ptp_class = ptp_classify_raw(skb);
968fba01283SVladimir Oltean 	if (ptp_class == PTP_CLASS_NONE)
969fba01283SVladimir Oltean 		return -EINVAL;
970682eaad9SYangbo Lu 
97139e5308bSYangbo Lu 	/* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
97239e5308bSYangbo Lu 	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
973fba01283SVladimir Oltean 		if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
97439e5308bSYangbo Lu 			OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
97539e5308bSYangbo Lu 			return 0;
97639e5308bSYangbo Lu 		}
97739e5308bSYangbo Lu 
97839e5308bSYangbo Lu 		/* Fall back to two-step timestamping */
97939e5308bSYangbo Lu 		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
98039e5308bSYangbo Lu 	}
98139e5308bSYangbo Lu 
982682eaad9SYangbo Lu 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
983682eaad9SYangbo Lu 		*clone = skb_clone_sk(skb);
984682eaad9SYangbo Lu 		if (!(*clone))
985682eaad9SYangbo Lu 			return -ENOMEM;
986682eaad9SYangbo Lu 
98752849bcfSVladimir Oltean 		err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
98852849bcfSVladimir Oltean 		if (err)
98952849bcfSVladimir Oltean 			return err;
99052849bcfSVladimir Oltean 
99139e5308bSYangbo Lu 		OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
992ebb4c6a9SVladimir Oltean 		OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
993682eaad9SYangbo Lu 	}
994682eaad9SYangbo Lu 
995682eaad9SYangbo Lu 	return 0;
996682eaad9SYangbo Lu }
997682eaad9SYangbo Lu EXPORT_SYMBOL(ocelot_port_txtstamp_request);
998400928bfSYangbo Lu 
999e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
1000e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
10014e3b0468SAntoine Tenart {
10024e3b0468SAntoine Tenart 	unsigned long flags;
10034e3b0468SAntoine Tenart 	u32 val;
10044e3b0468SAntoine Tenart 
10054e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
10064e3b0468SAntoine Tenart 
10074e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
10084e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
10094e3b0468SAntoine Tenart 
10104e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
10114e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
10124e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
10134e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
10144e3b0468SAntoine Tenart 
10154e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
10164e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
10174e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
10184e3b0468SAntoine Tenart 
10194e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
10204e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
10214e3b0468SAntoine Tenart 		ts->tv_sec--;
10224e3b0468SAntoine Tenart 
10234e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
10244e3b0468SAntoine Tenart }
1025e23a7b3eSYangbo Lu 
1026ebb4c6a9SVladimir Oltean static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
1027ebb4c6a9SVladimir Oltean {
1028ebb4c6a9SVladimir Oltean 	struct ptp_header *hdr;
1029ebb4c6a9SVladimir Oltean 
1030ebb4c6a9SVladimir Oltean 	hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
1031ebb4c6a9SVladimir Oltean 	if (WARN_ON(!hdr))
1032ebb4c6a9SVladimir Oltean 		return false;
1033ebb4c6a9SVladimir Oltean 
1034ebb4c6a9SVladimir Oltean 	return seqid == ntohs(hdr->sequence_id);
1035ebb4c6a9SVladimir Oltean }
1036ebb4c6a9SVladimir Oltean 
1037e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
1038e23a7b3eSYangbo Lu {
1039e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
1040e23a7b3eSYangbo Lu 
1041e23a7b3eSYangbo Lu 	while (budget--) {
1042b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
1043e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
1044ebb4c6a9SVladimir Oltean 		u32 val, id, seqid, txport;
1045e23a7b3eSYangbo Lu 		struct ocelot_port *port;
1046e23a7b3eSYangbo Lu 		struct timespec64 ts;
1047b049da13SYangbo Lu 		unsigned long flags;
1048e23a7b3eSYangbo Lu 
1049e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
1050e23a7b3eSYangbo Lu 
1051e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
1052e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
1053e23a7b3eSYangbo Lu 			break;
1054e23a7b3eSYangbo Lu 
1055e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
1056e23a7b3eSYangbo Lu 
1057e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
1058e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
1059e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
1060ebb4c6a9SVladimir Oltean 		seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
1061e23a7b3eSYangbo Lu 
1062e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
1063e23a7b3eSYangbo Lu 
106452849bcfSVladimir Oltean 		spin_lock(&ocelot->ts_id_lock);
106552849bcfSVladimir Oltean 		port->ptp_skbs_in_flight--;
106652849bcfSVladimir Oltean 		ocelot->ptp_skbs_in_flight--;
106752849bcfSVladimir Oltean 		spin_unlock(&ocelot->ts_id_lock);
106852849bcfSVladimir Oltean 
106952849bcfSVladimir Oltean 		/* Retrieve its associated skb */
1070ebb4c6a9SVladimir Oltean try_again:
1071b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
1072b049da13SYangbo Lu 
1073b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
1074c4b364ceSYangbo Lu 			if (OCELOT_SKB_CB(skb)->ts_id != id)
1075e23a7b3eSYangbo Lu 				continue;
1076b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
1077b049da13SYangbo Lu 			skb_match = skb;
1078fc62c094SYangbo Lu 			break;
1079e23a7b3eSYangbo Lu 		}
1080e23a7b3eSYangbo Lu 
1081b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
1082b049da13SYangbo Lu 
10839fde506eSVladimir Oltean 		if (WARN_ON(!skb_match))
10849fde506eSVladimir Oltean 			continue;
10859fde506eSVladimir Oltean 
1086ebb4c6a9SVladimir Oltean 		if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
1087ebb4c6a9SVladimir Oltean 			dev_err_ratelimited(ocelot->dev,
1088ebb4c6a9SVladimir Oltean 					    "port %d received stale TX timestamp for seqid %d, discarding\n",
1089ebb4c6a9SVladimir Oltean 					    txport, seqid);
1090ebb4c6a9SVladimir Oltean 			dev_kfree_skb_any(skb);
1091ebb4c6a9SVladimir Oltean 			goto try_again;
1092ebb4c6a9SVladimir Oltean 		}
1093ebb4c6a9SVladimir Oltean 
10945fd82200Slaurent brando 		/* Get the h/w timestamp */
10955fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
1096e23a7b3eSYangbo Lu 
1097e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
1098e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1099e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1100e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
11015fd82200Slaurent brando 
11025fd82200Slaurent brando 		/* Next ts */
11035fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
1104e23a7b3eSYangbo Lu 	}
1105e23a7b3eSYangbo Lu }
1106e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
11074e3b0468SAntoine Tenart 
1108924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
1109924ee317SVladimir Oltean 				u32 *rval)
1110924ee317SVladimir Oltean {
1111924ee317SVladimir Oltean 	u32 bytes_valid, val;
1112924ee317SVladimir Oltean 
1113924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1114924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
1115924ee317SVladimir Oltean 		if (ifh)
1116924ee317SVladimir Oltean 			return -EIO;
1117924ee317SVladimir Oltean 
1118924ee317SVladimir Oltean 		do {
1119924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1120924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
1121924ee317SVladimir Oltean 	}
1122924ee317SVladimir Oltean 
1123924ee317SVladimir Oltean 	switch (val) {
1124924ee317SVladimir Oltean 	case XTR_ABORT:
1125924ee317SVladimir Oltean 		return -EIO;
1126924ee317SVladimir Oltean 	case XTR_EOF_0:
1127924ee317SVladimir Oltean 	case XTR_EOF_1:
1128924ee317SVladimir Oltean 	case XTR_EOF_2:
1129924ee317SVladimir Oltean 	case XTR_EOF_3:
1130924ee317SVladimir Oltean 	case XTR_PRUNED:
1131924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
1132924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1133924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
1134924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1135924ee317SVladimir Oltean 		else
1136924ee317SVladimir Oltean 			*rval = val;
1137924ee317SVladimir Oltean 
1138924ee317SVladimir Oltean 		return bytes_valid;
1139924ee317SVladimir Oltean 	case XTR_ESCAPE:
1140924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1141924ee317SVladimir Oltean 
1142924ee317SVladimir Oltean 		return 4;
1143924ee317SVladimir Oltean 	default:
1144924ee317SVladimir Oltean 		*rval = val;
1145924ee317SVladimir Oltean 
1146924ee317SVladimir Oltean 		return 4;
1147924ee317SVladimir Oltean 	}
1148924ee317SVladimir Oltean }
1149924ee317SVladimir Oltean 
1150924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
1151924ee317SVladimir Oltean {
1152924ee317SVladimir Oltean 	int i, err = 0;
1153924ee317SVladimir Oltean 
1154924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
1155924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
1156924ee317SVladimir Oltean 		if (err != 4)
1157924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
1158924ee317SVladimir Oltean 	}
1159924ee317SVladimir Oltean 
1160924ee317SVladimir Oltean 	return 0;
1161924ee317SVladimir Oltean }
1162924ee317SVladimir Oltean 
1163b471a71eSClément Léger void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
1164b471a71eSClément Léger 			     u64 timestamp)
1165924ee317SVladimir Oltean {
1166924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
11672ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
1168b471a71eSClément Léger 	struct timespec64 ts;
1169b471a71eSClément Léger 
1170b471a71eSClément Léger 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1171b471a71eSClément Léger 
1172b471a71eSClément Léger 	tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1173b471a71eSClément Léger 	if ((tod_in_ns & 0xffffffff) < timestamp)
1174b471a71eSClément Léger 		full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1175b471a71eSClément Léger 				timestamp;
1176b471a71eSClément Léger 	else
1177b471a71eSClément Léger 		full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1178b471a71eSClément Léger 				timestamp;
1179b471a71eSClément Léger 
1180b471a71eSClément Léger 	shhwtstamps = skb_hwtstamps(skb);
1181b471a71eSClément Léger 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1182b471a71eSClément Léger 	shhwtstamps->hwtstamp = full_ts_in_ns;
1183b471a71eSClément Léger }
1184b471a71eSClément Léger EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
1185b471a71eSClément Léger 
1186b471a71eSClément Léger int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
1187b471a71eSClément Léger {
1188924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
1189924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
1190924ee317SVladimir Oltean 	struct net_device *dev;
1191924ee317SVladimir Oltean 	struct sk_buff *skb;
1192924ee317SVladimir Oltean 	int sz, buf_len;
1193924ee317SVladimir Oltean 	u32 val, *buf;
1194924ee317SVladimir Oltean 	int err;
1195924ee317SVladimir Oltean 
1196924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1197924ee317SVladimir Oltean 	if (err)
1198924ee317SVladimir Oltean 		return err;
1199924ee317SVladimir Oltean 
1200924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
1201924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
1202924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1203924ee317SVladimir Oltean 
1204924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1205924ee317SVladimir Oltean 		return -EINVAL;
1206924ee317SVladimir Oltean 
1207924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1208924ee317SVladimir Oltean 	if (!dev)
1209924ee317SVladimir Oltean 		return -EINVAL;
1210924ee317SVladimir Oltean 
1211924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
1212924ee317SVladimir Oltean 	if (unlikely(!skb)) {
1213924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
1214924ee317SVladimir Oltean 		return -ENOMEM;
1215924ee317SVladimir Oltean 	}
1216924ee317SVladimir Oltean 
1217924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
1218924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
1219924ee317SVladimir Oltean 
1220924ee317SVladimir Oltean 	len = 0;
1221924ee317SVladimir Oltean 	do {
1222924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1223924ee317SVladimir Oltean 		if (sz < 0) {
1224924ee317SVladimir Oltean 			err = sz;
1225924ee317SVladimir Oltean 			goto out_free_skb;
1226924ee317SVladimir Oltean 		}
1227924ee317SVladimir Oltean 		*buf++ = val;
1228924ee317SVladimir Oltean 		len += sz;
1229924ee317SVladimir Oltean 	} while (len < buf_len);
1230924ee317SVladimir Oltean 
1231924ee317SVladimir Oltean 	/* Read the FCS */
1232924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1233924ee317SVladimir Oltean 	if (sz < 0) {
1234924ee317SVladimir Oltean 		err = sz;
1235924ee317SVladimir Oltean 		goto out_free_skb;
1236924ee317SVladimir Oltean 	}
1237924ee317SVladimir Oltean 
1238924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1239924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1240924ee317SVladimir Oltean 
1241924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1242924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1243924ee317SVladimir Oltean 		*buf = val;
1244924ee317SVladimir Oltean 	}
1245924ee317SVladimir Oltean 
1246b471a71eSClément Léger 	if (ocelot->ptp)
1247b471a71eSClément Léger 		ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
1248924ee317SVladimir Oltean 
1249924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1250924ee317SVladimir Oltean 	 * has already been forwarded.
1251924ee317SVladimir Oltean 	 */
1252df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1253924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1254924ee317SVladimir Oltean 
1255924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1256d8ea7ff3SHoratiu Vultur 
1257924ee317SVladimir Oltean 	*nskb = skb;
1258924ee317SVladimir Oltean 
1259924ee317SVladimir Oltean 	return 0;
1260924ee317SVladimir Oltean 
1261924ee317SVladimir Oltean out_free_skb:
1262924ee317SVladimir Oltean 	kfree_skb(skb);
1263924ee317SVladimir Oltean 	return err;
1264924ee317SVladimir Oltean }
1265924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1266924ee317SVladimir Oltean 
1267137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1268137ffbc4SVladimir Oltean {
1269137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1270137ffbc4SVladimir Oltean 
1271137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1272137ffbc4SVladimir Oltean 		return false;
1273137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1274137ffbc4SVladimir Oltean 		return false;
1275137ffbc4SVladimir Oltean 
1276137ffbc4SVladimir Oltean 	return true;
1277137ffbc4SVladimir Oltean }
1278137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1279137ffbc4SVladimir Oltean 
1280e5150f00SClément Léger void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag)
1281e5150f00SClément Léger {
1282e5150f00SClément Léger 	ocelot_ifh_set_bypass(ifh, 1);
1283e5150f00SClément Léger 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1284e5150f00SClément Léger 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1285e5150f00SClément Léger 	if (vlan_tag)
1286e5150f00SClément Léger 		ocelot_ifh_set_vlan_tci(ifh, vlan_tag);
1287e5150f00SClément Léger 	if (rew_op)
1288e5150f00SClément Léger 		ocelot_ifh_set_rew_op(ifh, rew_op);
1289e5150f00SClément Léger }
1290e5150f00SClément Léger EXPORT_SYMBOL(ocelot_ifh_port_set);
1291e5150f00SClément Léger 
1292137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1293137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1294137ffbc4SVladimir Oltean {
129540d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1296137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1297137ffbc4SVladimir Oltean 
1298137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1299137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1300137ffbc4SVladimir Oltean 
1301e5150f00SClément Léger 	ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
1302137ffbc4SVladimir Oltean 
1303137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
130440d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1305137ffbc4SVladimir Oltean 
1306137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1307137ffbc4SVladimir Oltean 	last = skb->len % 4;
1308137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1309137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1310137ffbc4SVladimir Oltean 
1311137ffbc4SVladimir Oltean 	/* Add padding */
1312137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1313137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1314137ffbc4SVladimir Oltean 		i++;
1315137ffbc4SVladimir Oltean 	}
1316137ffbc4SVladimir Oltean 
1317137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1318137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1319137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1320137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1321137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1322137ffbc4SVladimir Oltean 
1323137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1324137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1325137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1326137ffbc4SVladimir Oltean 
1327137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1328137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1329137ffbc4SVladimir Oltean }
1330137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1331137ffbc4SVladimir Oltean 
13320a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
13330a6f17c6SVladimir Oltean {
13340a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
13350a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
13360a6f17c6SVladimir Oltean }
13370a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
13380a6f17c6SVladimir Oltean 
133954c31984SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port, const unsigned char *addr,
134054c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1341a556c76aSAlexandre Belloni {
1342471beb11SVladimir Oltean 	int pgid = port;
1343471beb11SVladimir Oltean 
1344471beb11SVladimir Oltean 	if (port == ocelot->npi)
1345471beb11SVladimir Oltean 		pgid = PGID_CPU;
1346a556c76aSAlexandre Belloni 
134754c31984SVladimir Oltean 	if (!vid)
134854c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
134954c31984SVladimir Oltean 
1350471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
1351a556c76aSAlexandre Belloni }
13525e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1353a556c76aSAlexandre Belloni 
135454c31984SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port, const unsigned char *addr,
135554c31984SVladimir Oltean 		   u16 vid, const struct net_device *bridge)
1356531ee1a6SVladimir Oltean {
135754c31984SVladimir Oltean 	if (!vid)
135854c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
135954c31984SVladimir Oltean 
1360531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1361531ee1a6SVladimir Oltean }
13625e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1363531ee1a6SVladimir Oltean 
13649c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1365531ee1a6SVladimir Oltean 			    bool is_static, void *data)
1366a556c76aSAlexandre Belloni {
1367531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
1368a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1369a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
1370a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
1371a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
1372a556c76aSAlexandre Belloni 
1373a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
1374a556c76aSAlexandre Belloni 		goto skip;
1375a556c76aSAlexandre Belloni 
1376a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1377a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
1378a556c76aSAlexandre Belloni 	if (!nlh)
1379a556c76aSAlexandre Belloni 		return -EMSGSIZE;
1380a556c76aSAlexandre Belloni 
1381a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
1382a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
1383a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
1384a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
1385a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
1386a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
1387a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
1388531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
1389a556c76aSAlexandre Belloni 
1390531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
1391a556c76aSAlexandre Belloni 		goto nla_put_failure;
1392a556c76aSAlexandre Belloni 
1393531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
1394a556c76aSAlexandre Belloni 		goto nla_put_failure;
1395a556c76aSAlexandre Belloni 
1396a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
1397a556c76aSAlexandre Belloni 
1398a556c76aSAlexandre Belloni skip:
1399a556c76aSAlexandre Belloni 	dump->idx++;
1400a556c76aSAlexandre Belloni 	return 0;
1401a556c76aSAlexandre Belloni 
1402a556c76aSAlexandre Belloni nla_put_failure:
1403a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
1404a556c76aSAlexandre Belloni 	return -EMSGSIZE;
1405a556c76aSAlexandre Belloni }
14069c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
1407a556c76aSAlexandre Belloni 
14082468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
1409531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1410a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1411a556c76aSAlexandre Belloni {
1412a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1413531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1414a556c76aSAlexandre Belloni 
1415a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1416a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1417a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1418a556c76aSAlexandre Belloni 
1419a556c76aSAlexandre Belloni 	/* Issue a read command */
1420a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1421a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1422a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1423a556c76aSAlexandre Belloni 
1424a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1425a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1426a556c76aSAlexandre Belloni 
1427a556c76aSAlexandre Belloni 	/* Read the entry flags */
1428a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1429a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1430a556c76aSAlexandre Belloni 		return -EINVAL;
1431a556c76aSAlexandre Belloni 
1432a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1433a556c76aSAlexandre Belloni 	 * do not report it.
1434a556c76aSAlexandre Belloni 	 */
1435a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1436531ee1a6SVladimir Oltean 	if (dst != port)
1437a556c76aSAlexandre Belloni 		return -EINVAL;
1438a556c76aSAlexandre Belloni 
1439a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1440a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1441a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1442a556c76aSAlexandre Belloni 
1443a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1444a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1445a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1446a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1447a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1448a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1449a556c76aSAlexandre Belloni 
1450a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1451a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1452a556c76aSAlexandre Belloni 
1453a556c76aSAlexandre Belloni 	return 0;
1454a556c76aSAlexandre Belloni }
1455a556c76aSAlexandre Belloni 
14565cad43a5SVladimir Oltean int ocelot_mact_flush(struct ocelot *ocelot, int port)
14575cad43a5SVladimir Oltean {
14585cad43a5SVladimir Oltean 	int err;
14595cad43a5SVladimir Oltean 
14605cad43a5SVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
14615cad43a5SVladimir Oltean 
14625cad43a5SVladimir Oltean 	/* Program ageing filter for a single port */
14635cad43a5SVladimir Oltean 	ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
14645cad43a5SVladimir Oltean 		     ANA_ANAGEFIL);
14655cad43a5SVladimir Oltean 
14665cad43a5SVladimir Oltean 	/* Flushing dynamic FDB entries requires two successive age scans */
14675cad43a5SVladimir Oltean 	ocelot_write(ocelot,
14685cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
14695cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
14705cad43a5SVladimir Oltean 
14715cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
14725cad43a5SVladimir Oltean 	if (err) {
14735cad43a5SVladimir Oltean 		mutex_unlock(&ocelot->mact_lock);
14745cad43a5SVladimir Oltean 		return err;
14755cad43a5SVladimir Oltean 	}
14765cad43a5SVladimir Oltean 
14775cad43a5SVladimir Oltean 	/* And second... */
14785cad43a5SVladimir Oltean 	ocelot_write(ocelot,
14795cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
14805cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
14815cad43a5SVladimir Oltean 
14825cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
14835cad43a5SVladimir Oltean 
14845cad43a5SVladimir Oltean 	/* Restore ageing filter */
14855cad43a5SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_ANAGEFIL);
14865cad43a5SVladimir Oltean 
14875cad43a5SVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
14885cad43a5SVladimir Oltean 
14895cad43a5SVladimir Oltean 	return err;
14905cad43a5SVladimir Oltean }
14915cad43a5SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_mact_flush);
14925cad43a5SVladimir Oltean 
14935e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1494531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1495a556c76aSAlexandre Belloni {
14962468346cSVladimir Oltean 	int err = 0;
1497531ee1a6SVladimir Oltean 	int i, j;
1498a556c76aSAlexandre Belloni 
14992468346cSVladimir Oltean 	/* We could take the lock just around ocelot_mact_read, but doing so
15002468346cSVladimir Oltean 	 * thousands of times in a row seems rather pointless and inefficient.
15012468346cSVladimir Oltean 	 */
15022468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
15032468346cSVladimir Oltean 
150421ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
150521ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1506a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1507531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1508531ee1a6SVladimir Oltean 			bool is_static;
1509531ee1a6SVladimir Oltean 
15102468346cSVladimir Oltean 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1511a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1512a556c76aSAlexandre Belloni 			 * skip it.
1513a556c76aSAlexandre Belloni 			 */
15142468346cSVladimir Oltean 			if (err == -EINVAL)
1515a556c76aSAlexandre Belloni 				continue;
15162468346cSVladimir Oltean 			else if (err)
15172468346cSVladimir Oltean 				break;
1518a556c76aSAlexandre Belloni 
1519531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1520531ee1a6SVladimir Oltean 
152154c31984SVladimir Oltean 			/* Hide the reserved VLANs used for
152254c31984SVladimir Oltean 			 * VLAN-unaware bridging.
152354c31984SVladimir Oltean 			 */
152454c31984SVladimir Oltean 			if (entry.vid > OCELOT_RSV_VLAN_RANGE_START)
152554c31984SVladimir Oltean 				entry.vid = 0;
152654c31984SVladimir Oltean 
15272468346cSVladimir Oltean 			err = cb(entry.mac, entry.vid, is_static, data);
15282468346cSVladimir Oltean 			if (err)
15292468346cSVladimir Oltean 				break;
1530a556c76aSAlexandre Belloni 		}
1531a556c76aSAlexandre Belloni 	}
1532a556c76aSAlexandre Belloni 
15332468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
15342468346cSVladimir Oltean 
15352468346cSVladimir Oltean 	return err;
1536531ee1a6SVladimir Oltean }
15375e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1538531ee1a6SVladimir Oltean 
153996ca08c0SVladimir Oltean static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
154096ca08c0SVladimir Oltean {
154196ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_ETYPE;
154296ca08c0SVladimir Oltean 	*(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588);
154396ca08c0SVladimir Oltean 	*(__be16 *)trap->key.etype.etype.mask = htons(0xffff);
154496ca08c0SVladimir Oltean }
154596ca08c0SVladimir Oltean 
154696ca08c0SVladimir Oltean static void
154796ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
154896ca08c0SVladimir Oltean {
154996ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_IPV4;
155059085208SVladimir Oltean 	trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
155159085208SVladimir Oltean 	trap->key.ipv4.proto.mask[0] = 0xff;
155296ca08c0SVladimir Oltean 	trap->key.ipv4.dport.value = PTP_EV_PORT;
155396ca08c0SVladimir Oltean 	trap->key.ipv4.dport.mask = 0xffff;
155496ca08c0SVladimir Oltean }
155596ca08c0SVladimir Oltean 
155696ca08c0SVladimir Oltean static void
155796ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
155896ca08c0SVladimir Oltean {
155996ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_IPV6;
156059085208SVladimir Oltean 	trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
156159085208SVladimir Oltean 	trap->key.ipv4.proto.mask[0] = 0xff;
156296ca08c0SVladimir Oltean 	trap->key.ipv6.dport.value = PTP_EV_PORT;
156396ca08c0SVladimir Oltean 	trap->key.ipv6.dport.mask = 0xffff;
156496ca08c0SVladimir Oltean }
156596ca08c0SVladimir Oltean 
156696ca08c0SVladimir Oltean static void
156796ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
156896ca08c0SVladimir Oltean {
156996ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_IPV4;
157059085208SVladimir Oltean 	trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
157159085208SVladimir Oltean 	trap->key.ipv4.proto.mask[0] = 0xff;
157296ca08c0SVladimir Oltean 	trap->key.ipv4.dport.value = PTP_GEN_PORT;
157396ca08c0SVladimir Oltean 	trap->key.ipv4.dport.mask = 0xffff;
157496ca08c0SVladimir Oltean }
157596ca08c0SVladimir Oltean 
157696ca08c0SVladimir Oltean static void
157796ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
157896ca08c0SVladimir Oltean {
157996ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_IPV6;
158059085208SVladimir Oltean 	trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
158159085208SVladimir Oltean 	trap->key.ipv4.proto.mask[0] = 0xff;
158296ca08c0SVladimir Oltean 	trap->key.ipv6.dport.value = PTP_GEN_PORT;
158396ca08c0SVladimir Oltean 	trap->key.ipv6.dport.mask = 0xffff;
158496ca08c0SVladimir Oltean }
158596ca08c0SVladimir Oltean 
15869d75b881SVladimir Oltean int ocelot_trap_add(struct ocelot *ocelot, int port,
15879d75b881SVladimir Oltean 		    unsigned long cookie, bool take_ts,
158896ca08c0SVladimir Oltean 		    void (*populate)(struct ocelot_vcap_filter *f))
158996ca08c0SVladimir Oltean {
159096ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
159196ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
159296ca08c0SVladimir Oltean 	bool new = false;
159396ca08c0SVladimir Oltean 	int err;
159496ca08c0SVladimir Oltean 
159596ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
159696ca08c0SVladimir Oltean 
159796ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
159896ca08c0SVladimir Oltean 						   false);
159996ca08c0SVladimir Oltean 	if (!trap) {
160096ca08c0SVladimir Oltean 		trap = kzalloc(sizeof(*trap), GFP_KERNEL);
160196ca08c0SVladimir Oltean 		if (!trap)
160296ca08c0SVladimir Oltean 			return -ENOMEM;
160396ca08c0SVladimir Oltean 
160496ca08c0SVladimir Oltean 		populate(trap);
160596ca08c0SVladimir Oltean 		trap->prio = 1;
160696ca08c0SVladimir Oltean 		trap->id.cookie = cookie;
160796ca08c0SVladimir Oltean 		trap->id.tc_offload = false;
160896ca08c0SVladimir Oltean 		trap->block_id = VCAP_IS2;
160996ca08c0SVladimir Oltean 		trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
161096ca08c0SVladimir Oltean 		trap->lookup = 0;
161196ca08c0SVladimir Oltean 		trap->action.cpu_copy_ena = true;
161296ca08c0SVladimir Oltean 		trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
161396ca08c0SVladimir Oltean 		trap->action.port_mask = 0;
16149d75b881SVladimir Oltean 		trap->take_ts = take_ts;
1615e42bd4edSVladimir Oltean 		list_add_tail(&trap->trap_list, &ocelot->traps);
161696ca08c0SVladimir Oltean 		new = true;
161796ca08c0SVladimir Oltean 	}
161896ca08c0SVladimir Oltean 
161996ca08c0SVladimir Oltean 	trap->ingress_port_mask |= BIT(port);
162096ca08c0SVladimir Oltean 
162196ca08c0SVladimir Oltean 	if (new)
162296ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_add(ocelot, trap, NULL);
162396ca08c0SVladimir Oltean 	else
162496ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
162596ca08c0SVladimir Oltean 	if (err) {
162696ca08c0SVladimir Oltean 		trap->ingress_port_mask &= ~BIT(port);
1627e42bd4edSVladimir Oltean 		if (!trap->ingress_port_mask) {
1628e42bd4edSVladimir Oltean 			list_del(&trap->trap_list);
162996ca08c0SVladimir Oltean 			kfree(trap);
1630e42bd4edSVladimir Oltean 		}
163196ca08c0SVladimir Oltean 		return err;
163296ca08c0SVladimir Oltean 	}
163396ca08c0SVladimir Oltean 
163496ca08c0SVladimir Oltean 	return 0;
163596ca08c0SVladimir Oltean }
163696ca08c0SVladimir Oltean 
1637b9bace6eSVladimir Oltean int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie)
163896ca08c0SVladimir Oltean {
163996ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
164096ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
164196ca08c0SVladimir Oltean 
164296ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
164396ca08c0SVladimir Oltean 
164496ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
164596ca08c0SVladimir Oltean 						   false);
164696ca08c0SVladimir Oltean 	if (!trap)
164796ca08c0SVladimir Oltean 		return 0;
164896ca08c0SVladimir Oltean 
164996ca08c0SVladimir Oltean 	trap->ingress_port_mask &= ~BIT(port);
1650e42bd4edSVladimir Oltean 	if (!trap->ingress_port_mask) {
1651e42bd4edSVladimir Oltean 		list_del(&trap->trap_list);
1652e42bd4edSVladimir Oltean 
165396ca08c0SVladimir Oltean 		return ocelot_vcap_filter_del(ocelot, trap);
1654e42bd4edSVladimir Oltean 	}
165596ca08c0SVladimir Oltean 
165696ca08c0SVladimir Oltean 	return ocelot_vcap_filter_replace(ocelot, trap);
165796ca08c0SVladimir Oltean }
165896ca08c0SVladimir Oltean 
165996ca08c0SVladimir Oltean static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
166096ca08c0SVladimir Oltean {
1661c518afecSVladimir Oltean 	unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
166296ca08c0SVladimir Oltean 
16639d75b881SVladimir Oltean 	return ocelot_trap_add(ocelot, port, l2_cookie, true,
166496ca08c0SVladimir Oltean 			       ocelot_populate_l2_ptp_trap_key);
166596ca08c0SVladimir Oltean }
166696ca08c0SVladimir Oltean 
166796ca08c0SVladimir Oltean static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
166896ca08c0SVladimir Oltean {
1669c518afecSVladimir Oltean 	unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
167096ca08c0SVladimir Oltean 
167196ca08c0SVladimir Oltean 	return ocelot_trap_del(ocelot, port, l2_cookie);
167296ca08c0SVladimir Oltean }
167396ca08c0SVladimir Oltean 
167496ca08c0SVladimir Oltean static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
167596ca08c0SVladimir Oltean {
1676c518afecSVladimir Oltean 	unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
1677c518afecSVladimir Oltean 	unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
167896ca08c0SVladimir Oltean 	int err;
167996ca08c0SVladimir Oltean 
16809d75b881SVladimir Oltean 	err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, true,
168196ca08c0SVladimir Oltean 			      ocelot_populate_ipv4_ptp_event_trap_key);
168296ca08c0SVladimir Oltean 	if (err)
168396ca08c0SVladimir Oltean 		return err;
168496ca08c0SVladimir Oltean 
16859d75b881SVladimir Oltean 	err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, false,
168696ca08c0SVladimir Oltean 			      ocelot_populate_ipv4_ptp_general_trap_key);
168796ca08c0SVladimir Oltean 	if (err)
168896ca08c0SVladimir Oltean 		ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
168996ca08c0SVladimir Oltean 
169096ca08c0SVladimir Oltean 	return err;
169196ca08c0SVladimir Oltean }
169296ca08c0SVladimir Oltean 
169396ca08c0SVladimir Oltean static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
169496ca08c0SVladimir Oltean {
1695c518afecSVladimir Oltean 	unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
1696c518afecSVladimir Oltean 	unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
169796ca08c0SVladimir Oltean 	int err;
169896ca08c0SVladimir Oltean 
169996ca08c0SVladimir Oltean 	err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
170096ca08c0SVladimir Oltean 	err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
170196ca08c0SVladimir Oltean 	return err;
170296ca08c0SVladimir Oltean }
170396ca08c0SVladimir Oltean 
170496ca08c0SVladimir Oltean static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
170596ca08c0SVladimir Oltean {
1706c518afecSVladimir Oltean 	unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
1707c518afecSVladimir Oltean 	unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
170896ca08c0SVladimir Oltean 	int err;
170996ca08c0SVladimir Oltean 
17109d75b881SVladimir Oltean 	err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, true,
171196ca08c0SVladimir Oltean 			      ocelot_populate_ipv6_ptp_event_trap_key);
171296ca08c0SVladimir Oltean 	if (err)
171396ca08c0SVladimir Oltean 		return err;
171496ca08c0SVladimir Oltean 
17159d75b881SVladimir Oltean 	err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, false,
171696ca08c0SVladimir Oltean 			      ocelot_populate_ipv6_ptp_general_trap_key);
171796ca08c0SVladimir Oltean 	if (err)
171896ca08c0SVladimir Oltean 		ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
171996ca08c0SVladimir Oltean 
172096ca08c0SVladimir Oltean 	return err;
172196ca08c0SVladimir Oltean }
172296ca08c0SVladimir Oltean 
172396ca08c0SVladimir Oltean static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
172496ca08c0SVladimir Oltean {
1725c518afecSVladimir Oltean 	unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
1726c518afecSVladimir Oltean 	unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
172796ca08c0SVladimir Oltean 	int err;
172896ca08c0SVladimir Oltean 
172996ca08c0SVladimir Oltean 	err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
173096ca08c0SVladimir Oltean 	err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
173196ca08c0SVladimir Oltean 	return err;
173296ca08c0SVladimir Oltean }
173396ca08c0SVladimir Oltean 
173496ca08c0SVladimir Oltean static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
173596ca08c0SVladimir Oltean 				  bool l2, bool l4)
173696ca08c0SVladimir Oltean {
173796ca08c0SVladimir Oltean 	int err;
173896ca08c0SVladimir Oltean 
173996ca08c0SVladimir Oltean 	if (l2)
174096ca08c0SVladimir Oltean 		err = ocelot_l2_ptp_trap_add(ocelot, port);
174196ca08c0SVladimir Oltean 	else
174296ca08c0SVladimir Oltean 		err = ocelot_l2_ptp_trap_del(ocelot, port);
174396ca08c0SVladimir Oltean 	if (err)
174496ca08c0SVladimir Oltean 		return err;
174596ca08c0SVladimir Oltean 
174696ca08c0SVladimir Oltean 	if (l4) {
174796ca08c0SVladimir Oltean 		err = ocelot_ipv4_ptp_trap_add(ocelot, port);
174896ca08c0SVladimir Oltean 		if (err)
174996ca08c0SVladimir Oltean 			goto err_ipv4;
175096ca08c0SVladimir Oltean 
175196ca08c0SVladimir Oltean 		err = ocelot_ipv6_ptp_trap_add(ocelot, port);
175296ca08c0SVladimir Oltean 		if (err)
175396ca08c0SVladimir Oltean 			goto err_ipv6;
175496ca08c0SVladimir Oltean 	} else {
175596ca08c0SVladimir Oltean 		err = ocelot_ipv4_ptp_trap_del(ocelot, port);
175696ca08c0SVladimir Oltean 
175796ca08c0SVladimir Oltean 		err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
175896ca08c0SVladimir Oltean 	}
175996ca08c0SVladimir Oltean 	if (err)
176096ca08c0SVladimir Oltean 		return err;
176196ca08c0SVladimir Oltean 
176296ca08c0SVladimir Oltean 	return 0;
176396ca08c0SVladimir Oltean 
176496ca08c0SVladimir Oltean err_ipv6:
176596ca08c0SVladimir Oltean 	ocelot_ipv4_ptp_trap_del(ocelot, port);
176696ca08c0SVladimir Oltean err_ipv4:
176796ca08c0SVladimir Oltean 	if (l2)
176896ca08c0SVladimir Oltean 		ocelot_l2_ptp_trap_del(ocelot, port);
176996ca08c0SVladimir Oltean 	return err;
177096ca08c0SVladimir Oltean }
177196ca08c0SVladimir Oltean 
1772f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
17734e3b0468SAntoine Tenart {
17744e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
17754e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
17764e3b0468SAntoine Tenart }
1777f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
17784e3b0468SAntoine Tenart 
1779f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
17804e3b0468SAntoine Tenart {
1781306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
178296ca08c0SVladimir Oltean 	bool l2 = false, l4 = false;
17834e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
178496ca08c0SVladimir Oltean 	int err;
17854e3b0468SAntoine Tenart 
17864e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
17874e3b0468SAntoine Tenart 		return -EFAULT;
17884e3b0468SAntoine Tenart 
17894e3b0468SAntoine Tenart 	/* Tx type sanity check */
17904e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
17914e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1792306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
17934e3b0468SAntoine Tenart 		break;
17944e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
17954e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
17964e3b0468SAntoine Tenart 		 * need to update the origin time.
17974e3b0468SAntoine Tenart 		 */
1798306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
17994e3b0468SAntoine Tenart 		break;
18004e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1801306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
18024e3b0468SAntoine Tenart 		break;
18034e3b0468SAntoine Tenart 	default:
18044e3b0468SAntoine Tenart 		return -ERANGE;
18054e3b0468SAntoine Tenart 	}
18064e3b0468SAntoine Tenart 
18074e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
18084e3b0468SAntoine Tenart 
18094e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
18104e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
18114e3b0468SAntoine Tenart 		break;
18124e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
18134e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
18144e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
181596ca08c0SVladimir Oltean 		l4 = true;
181696ca08c0SVladimir Oltean 		break;
18174e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
18184e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
18194e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
182096ca08c0SVladimir Oltean 		l2 = true;
182196ca08c0SVladimir Oltean 		break;
18224e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
18234e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
18244e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
182596ca08c0SVladimir Oltean 		l2 = true;
182696ca08c0SVladimir Oltean 		l4 = true;
18274e3b0468SAntoine Tenart 		break;
18284e3b0468SAntoine Tenart 	default:
18294e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
18304e3b0468SAntoine Tenart 		return -ERANGE;
18314e3b0468SAntoine Tenart 	}
18324e3b0468SAntoine Tenart 
183396ca08c0SVladimir Oltean 	err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
18349c32950fSLv Ruyi 	if (err) {
18359c32950fSLv Ruyi 		mutex_unlock(&ocelot->ptp_lock);
183696ca08c0SVladimir Oltean 		return err;
18379c32950fSLv Ruyi 	}
183896ca08c0SVladimir Oltean 
183996ca08c0SVladimir Oltean 	if (l2 && l4)
184096ca08c0SVladimir Oltean 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
184196ca08c0SVladimir Oltean 	else if (l2)
184296ca08c0SVladimir Oltean 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
184396ca08c0SVladimir Oltean 	else if (l4)
184496ca08c0SVladimir Oltean 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
184596ca08c0SVladimir Oltean 	else
184696ca08c0SVladimir Oltean 		cfg.rx_filter = HWTSTAMP_FILTER_NONE;
184796ca08c0SVladimir Oltean 
18484e3b0468SAntoine Tenart 	/* Commit back the result & save it */
18494e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
18504e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
18514e3b0468SAntoine Tenart 
18524e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
18534e3b0468SAntoine Tenart }
1854f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
18554e3b0468SAntoine Tenart 
18565e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1857a556c76aSAlexandre Belloni {
1858a556c76aSAlexandre Belloni 	int i;
1859a556c76aSAlexandre Belloni 
1860a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1861a556c76aSAlexandre Belloni 		return;
1862a556c76aSAlexandre Belloni 
1863a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1864a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1865a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1866a556c76aSAlexandre Belloni }
18675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1868a556c76aSAlexandre Belloni 
18697fbf6795SColin Foster /* Caller must hold &ocelot->stats_lock */
1870d87b1c08SColin Foster static int ocelot_port_update_stats(struct ocelot *ocelot, int port)
1871a556c76aSAlexandre Belloni {
1872d87b1c08SColin Foster 	unsigned int idx = port * ocelot->num_stats;
1873d87b1c08SColin Foster 	struct ocelot_stats_region *region;
1874d87b1c08SColin Foster 	int err, j;
1875a556c76aSAlexandre Belloni 
1876a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
1877e27d785eSColin Foster 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port), SYS_STAT_CFG);
1878a556c76aSAlexandre Belloni 
1879d87b1c08SColin Foster 	list_for_each_entry(region, &ocelot->stats_regions, node) {
1880d87b1c08SColin Foster 		err = ocelot_bulk_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1881d87b1c08SColin Foster 					   region->offset, region->buf,
1882d87b1c08SColin Foster 					   region->count);
1883d87b1c08SColin Foster 		if (err)
1884d87b1c08SColin Foster 			return err;
1885a556c76aSAlexandre Belloni 
1886d87b1c08SColin Foster 		for (j = 0; j < region->count; j++) {
1887d87b1c08SColin Foster 			u64 *stat = &ocelot->stats[idx + j];
1888d87b1c08SColin Foster 			u64 val = region->buf[j];
1889a556c76aSAlexandre Belloni 
1890d87b1c08SColin Foster 			if (val < (*stat & U32_MAX))
1891d87b1c08SColin Foster 				*stat += (u64)1 << 32;
1892a556c76aSAlexandre Belloni 
1893d87b1c08SColin Foster 			*stat = (*stat & ~(u64)U32_MAX) + val;
1894a556c76aSAlexandre Belloni 		}
1895d87b1c08SColin Foster 
1896d87b1c08SColin Foster 		idx += region->count;
1897d87b1c08SColin Foster 	}
1898d87b1c08SColin Foster 
1899d87b1c08SColin Foster 	return err;
19001e1caa97SClaudiu Manoil }
19011e1caa97SClaudiu Manoil 
19021e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
19031e1caa97SClaudiu Manoil {
19041e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
19051e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
19061e1caa97SClaudiu Manoil 					     stats_work);
1907d87b1c08SColin Foster 	int i, err;
19081e1caa97SClaudiu Manoil 
19097fbf6795SColin Foster 	mutex_lock(&ocelot->stats_lock);
1910d87b1c08SColin Foster 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1911d87b1c08SColin Foster 		err = ocelot_port_update_stats(ocelot, i);
1912d87b1c08SColin Foster 		if (err)
1913d87b1c08SColin Foster 			break;
1914d87b1c08SColin Foster 	}
19157fbf6795SColin Foster 	mutex_unlock(&ocelot->stats_lock);
19161e1caa97SClaudiu Manoil 
1917d87b1c08SColin Foster 	if (err)
1918d87b1c08SColin Foster 		dev_err(ocelot->dev, "Error %d updating ethtool stats\n",  err);
1919d87b1c08SColin Foster 
1920a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1921a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1922a556c76aSAlexandre Belloni }
1923a556c76aSAlexandre Belloni 
19245e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1925a556c76aSAlexandre Belloni {
1926d87b1c08SColin Foster 	int i, err;
1927a556c76aSAlexandre Belloni 
19287fbf6795SColin Foster 	mutex_lock(&ocelot->stats_lock);
19297fbf6795SColin Foster 
1930a556c76aSAlexandre Belloni 	/* check and update now */
1931d87b1c08SColin Foster 	err = ocelot_port_update_stats(ocelot, port);
1932a556c76aSAlexandre Belloni 
1933a556c76aSAlexandre Belloni 	/* Copy all counters */
1934a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1935004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
19367fbf6795SColin Foster 
19377fbf6795SColin Foster 	mutex_unlock(&ocelot->stats_lock);
1938d87b1c08SColin Foster 
1939d87b1c08SColin Foster 	if (err)
1940d87b1c08SColin Foster 		dev_err(ocelot->dev, "Error %d updating ethtool stats\n", err);
1941a556c76aSAlexandre Belloni }
19425e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1943a556c76aSAlexandre Belloni 
19445e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1945c7282d38SVladimir Oltean {
1946a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1947a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1948c7282d38SVladimir Oltean 
1949a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1950a556c76aSAlexandre Belloni }
19515e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1952a556c76aSAlexandre Belloni 
1953d87b1c08SColin Foster static int ocelot_prepare_stats_regions(struct ocelot *ocelot)
1954d87b1c08SColin Foster {
1955d87b1c08SColin Foster 	struct ocelot_stats_region *region = NULL;
1956d87b1c08SColin Foster 	unsigned int last;
1957d87b1c08SColin Foster 	int i;
1958d87b1c08SColin Foster 
1959d87b1c08SColin Foster 	INIT_LIST_HEAD(&ocelot->stats_regions);
1960d87b1c08SColin Foster 
1961d87b1c08SColin Foster 	for (i = 0; i < ocelot->num_stats; i++) {
1962d87b1c08SColin Foster 		if (region && ocelot->stats_layout[i].offset == last + 1) {
1963d87b1c08SColin Foster 			region->count++;
1964d87b1c08SColin Foster 		} else {
1965d87b1c08SColin Foster 			region = devm_kzalloc(ocelot->dev, sizeof(*region),
1966d87b1c08SColin Foster 					      GFP_KERNEL);
1967d87b1c08SColin Foster 			if (!region)
1968d87b1c08SColin Foster 				return -ENOMEM;
1969d87b1c08SColin Foster 
1970d87b1c08SColin Foster 			region->offset = ocelot->stats_layout[i].offset;
1971d87b1c08SColin Foster 			region->count = 1;
1972d87b1c08SColin Foster 			list_add_tail(&region->node, &ocelot->stats_regions);
1973d87b1c08SColin Foster 		}
1974d87b1c08SColin Foster 
1975d87b1c08SColin Foster 		last = ocelot->stats_layout[i].offset;
1976d87b1c08SColin Foster 	}
1977d87b1c08SColin Foster 
1978d87b1c08SColin Foster 	list_for_each_entry(region, &ocelot->stats_regions, node) {
1979d87b1c08SColin Foster 		region->buf = devm_kcalloc(ocelot->dev, region->count,
1980d87b1c08SColin Foster 					   sizeof(*region->buf), GFP_KERNEL);
1981d87b1c08SColin Foster 		if (!region->buf)
1982d87b1c08SColin Foster 			return -ENOMEM;
1983d87b1c08SColin Foster 	}
1984d87b1c08SColin Foster 
1985d87b1c08SColin Foster 	return 0;
1986d87b1c08SColin Foster }
1987d87b1c08SColin Foster 
19885e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1989c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1990c7282d38SVladimir Oltean {
19914e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
19924e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1993d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1994d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1995d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1996d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1997d2b09a8eSYangbo Lu 		return 0;
1998d2b09a8eSYangbo Lu 	}
19994e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
20004e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
20014e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
20024e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
20034e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
20044e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
20054e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
20064e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
2007c49a35eeSVladimir Oltean 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
2008c49a35eeSVladimir Oltean 			   BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
2009c49a35eeSVladimir Oltean 			   BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
2010c49a35eeSVladimir Oltean 			   BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
20114e3b0468SAntoine Tenart 
20124e3b0468SAntoine Tenart 	return 0;
20134e3b0468SAntoine Tenart }
20145e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
20154e3b0468SAntoine Tenart 
2016a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
2017b80af659SVladimir Oltean {
2018b80af659SVladimir Oltean 	u32 mask = 0;
2019b80af659SVladimir Oltean 	int port;
2020b80af659SVladimir Oltean 
2021961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2022961d8b69SVladimir Oltean 
2023b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2024b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2025b80af659SVladimir Oltean 
2026b80af659SVladimir Oltean 		if (!ocelot_port)
2027b80af659SVladimir Oltean 			continue;
2028b80af659SVladimir Oltean 
2029a14e6b69SVladimir Oltean 		if (ocelot_port->bond == bond)
2030b80af659SVladimir Oltean 			mask |= BIT(port);
2031b80af659SVladimir Oltean 	}
2032b80af659SVladimir Oltean 
2033b80af659SVladimir Oltean 	return mask;
2034b80af659SVladimir Oltean }
2035b80af659SVladimir Oltean 
2036961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical
2037961d8b69SVladimir Oltean  * port ID present in that LAG. It may change if that port ever leaves the LAG.
2038961d8b69SVladimir Oltean  */
2039961d8b69SVladimir Oltean static int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond)
2040961d8b69SVladimir Oltean {
2041961d8b69SVladimir Oltean 	int bond_mask = ocelot_get_bond_mask(ocelot, bond);
2042961d8b69SVladimir Oltean 
2043961d8b69SVladimir Oltean 	if (!bond_mask)
2044961d8b69SVladimir Oltean 		return -ENOENT;
2045961d8b69SVladimir Oltean 
2046961d8b69SVladimir Oltean 	return __ffs(bond_mask);
2047961d8b69SVladimir Oltean }
2048961d8b69SVladimir Oltean 
20498abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
2050df291e54SVladimir Oltean {
2051acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
2052a8bd9fa5SVladimir Oltean 	const struct net_device *bridge;
2053df291e54SVladimir Oltean 	u32 mask = 0;
2054df291e54SVladimir Oltean 	int port;
2055df291e54SVladimir Oltean 
2056a8bd9fa5SVladimir Oltean 	if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
2057a8bd9fa5SVladimir Oltean 		return 0;
2058a8bd9fa5SVladimir Oltean 
2059a8bd9fa5SVladimir Oltean 	bridge = ocelot_port->bridge;
2060a8bd9fa5SVladimir Oltean 	if (!bridge)
2061acc64f52SVladimir Oltean 		return 0;
2062acc64f52SVladimir Oltean 
2063df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2064acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
2065df291e54SVladimir Oltean 
2066df291e54SVladimir Oltean 		if (!ocelot_port)
2067df291e54SVladimir Oltean 			continue;
2068df291e54SVladimir Oltean 
2069df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
2070df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
2071df291e54SVladimir Oltean 			mask |= BIT(port);
2072df291e54SVladimir Oltean 	}
2073df291e54SVladimir Oltean 
2074df291e54SVladimir Oltean 	return mask;
2075df291e54SVladimir Oltean }
20768abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
2077df291e54SVladimir Oltean 
20788abe1970SVladimir Oltean u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
20799b521250SVladimir Oltean {
2080e21268efSVladimir Oltean 	u32 mask = 0;
20819b521250SVladimir Oltean 	int port;
20829b521250SVladimir Oltean 
2083e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2084e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2085e21268efSVladimir Oltean 
2086e21268efSVladimir Oltean 		if (!ocelot_port)
2087e21268efSVladimir Oltean 			continue;
2088e21268efSVladimir Oltean 
2089e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
2090e21268efSVladimir Oltean 			mask |= BIT(port);
2091e21268efSVladimir Oltean 	}
2092e21268efSVladimir Oltean 
2093e21268efSVladimir Oltean 	return mask;
2094e21268efSVladimir Oltean }
20958abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_dsa_8021q_cpu_mask);
2096e21268efSVladimir Oltean 
20978abe1970SVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
2098e21268efSVladimir Oltean {
2099e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
2100e21268efSVladimir Oltean 	int port;
2101e21268efSVladimir Oltean 
21028abe1970SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
21038abe1970SVladimir Oltean 
21048abe1970SVladimir Oltean 	/* If cut-through forwarding is supported, update the masks before a
21058abe1970SVladimir Oltean 	 * port joins the forwarding domain, to avoid potential underruns if it
21068abe1970SVladimir Oltean 	 * has the highest speed from the new domain.
21078abe1970SVladimir Oltean 	 */
21088abe1970SVladimir Oltean 	if (joining && ocelot->ops->cut_through_fwd)
21098abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
21108abe1970SVladimir Oltean 
2111e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
2112e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
2113e21268efSVladimir Oltean 	 * those are bridged or standalone.
2114e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
2115e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
2116e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
2117e21268efSVladimir Oltean 	 */
2118e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
2119e21268efSVladimir Oltean 
21209b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
21219b521250SVladimir Oltean 	 * a source for the other ports.
21229b521250SVladimir Oltean 	 */
21239b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2124e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2125e21268efSVladimir Oltean 		unsigned long mask;
2126e21268efSVladimir Oltean 
2127e21268efSVladimir Oltean 		if (!ocelot_port) {
2128e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
2129e21268efSVladimir Oltean 			mask = 0;
2130e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
2131e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
2132e21268efSVladimir Oltean 			 * forward packets to all other ports except for
2133e21268efSVladimir Oltean 			 * themselves
2134e21268efSVladimir Oltean 			 */
2135e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
2136e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
2137df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
2138528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
21399b521250SVladimir Oltean 
2140a8bd9fa5SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
2141c1930148SVladimir Oltean 			mask |= cpu_fwd_mask;
2142df291e54SVladimir Oltean 			mask &= ~BIT(port);
2143a14e6b69SVladimir Oltean 			if (bond)
2144a14e6b69SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond);
21459b521250SVladimir Oltean 		} else {
2146e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
2147e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
2148e21268efSVladimir Oltean 			 * module otherwise.
2149e21268efSVladimir Oltean 			 */
2150e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
2151e21268efSVladimir Oltean 		}
2152e21268efSVladimir Oltean 
2153e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
21549b521250SVladimir Oltean 	}
21558abe1970SVladimir Oltean 
21568abe1970SVladimir Oltean 	/* If cut-through forwarding is supported and a port is leaving, there
21578abe1970SVladimir Oltean 	 * is a chance that cut-through was disabled on the other ports due to
21588abe1970SVladimir Oltean 	 * the port which is leaving (it has a higher link speed). We need to
21598abe1970SVladimir Oltean 	 * update the cut-through masks of the remaining ports no earlier than
21608abe1970SVladimir Oltean 	 * after the port has left, to prevent underruns from happening between
21618abe1970SVladimir Oltean 	 * the cut-through update and the forwarding domain update.
21628abe1970SVladimir Oltean 	 */
21638abe1970SVladimir Oltean 	if (!joining && ocelot->ops->cut_through_fwd)
21648abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
21659b521250SVladimir Oltean }
2166e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
21679b521250SVladimir Oltean 
216854c31984SVladimir Oltean void ocelot_port_set_dsa_8021q_cpu(struct ocelot *ocelot, int port)
216954c31984SVladimir Oltean {
217054c31984SVladimir Oltean 	u16 vid;
217154c31984SVladimir Oltean 
217254c31984SVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = true;
217354c31984SVladimir Oltean 
217454c31984SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
217554c31984SVladimir Oltean 		ocelot_vlan_member_add(ocelot, port, vid, true);
217654c31984SVladimir Oltean }
217754c31984SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_dsa_8021q_cpu);
217854c31984SVladimir Oltean 
217954c31984SVladimir Oltean void ocelot_port_unset_dsa_8021q_cpu(struct ocelot *ocelot, int port)
218054c31984SVladimir Oltean {
218154c31984SVladimir Oltean 	u16 vid;
218254c31984SVladimir Oltean 
218354c31984SVladimir Oltean 	ocelot->ports[port]->is_dsa_8021q_cpu = false;
218454c31984SVladimir Oltean 
218554c31984SVladimir Oltean 	for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
218654c31984SVladimir Oltean 		ocelot_vlan_member_del(ocelot, port, vid);
218754c31984SVladimir Oltean }
218854c31984SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_unset_dsa_8021q_cpu);
218954c31984SVladimir Oltean 
21905e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
2191a556c76aSAlexandre Belloni {
2192421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2193df291e54SVladimir Oltean 	u32 learn_ena = 0;
2194a556c76aSAlexandre Belloni 
21958abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
21968abe1970SVladimir Oltean 
2197df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
2198a556c76aSAlexandre Belloni 
2199df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
2200df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
2201df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
2202a556c76aSAlexandre Belloni 
2203df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
2204df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2205a556c76aSAlexandre Belloni 
22068abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
22078abe1970SVladimir Oltean 
22088abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2209a556c76aSAlexandre Belloni }
22105e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
2211a556c76aSAlexandre Belloni 
22125e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
22134bda1415SVladimir Oltean {
2214c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
2215c0d7eccbSVladimir Oltean 
2216c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
2217c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
2218c0d7eccbSVladimir Oltean 	 */
2219c0d7eccbSVladimir Oltean 	if (!age_period)
2220c0d7eccbSVladimir Oltean 		age_period = 1;
2221c0d7eccbSVladimir Oltean 
2222c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
2223a556c76aSAlexandre Belloni }
22245e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
2225a556c76aSAlexandre Belloni 
2226a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
2227a556c76aSAlexandre Belloni 						     const unsigned char *addr,
2228a556c76aSAlexandre Belloni 						     u16 vid)
2229a556c76aSAlexandre Belloni {
2230a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
2231a556c76aSAlexandre Belloni 
2232a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
2233a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
2234a556c76aSAlexandre Belloni 			return mc;
2235a556c76aSAlexandre Belloni 	}
2236a556c76aSAlexandre Belloni 
2237a556c76aSAlexandre Belloni 	return NULL;
2238a556c76aSAlexandre Belloni }
2239a556c76aSAlexandre Belloni 
22409403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
22419403c158SVladimir Oltean {
22429403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
22439403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
22449403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
22459403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
22467c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
22479403c158SVladimir Oltean }
22489403c158SVladimir Oltean 
2249e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
2250e5d1f896SVladimir Oltean 					     unsigned long ports)
2251e5d1f896SVladimir Oltean {
2252e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2253e5d1f896SVladimir Oltean 
2254e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
2255e5d1f896SVladimir Oltean 	if (!pgid)
2256e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
2257e5d1f896SVladimir Oltean 
2258e5d1f896SVladimir Oltean 	pgid->ports = ports;
2259e5d1f896SVladimir Oltean 	pgid->index = index;
2260e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
2261e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
2262e5d1f896SVladimir Oltean 
2263e5d1f896SVladimir Oltean 	return pgid;
2264e5d1f896SVladimir Oltean }
2265e5d1f896SVladimir Oltean 
2266e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
2267e5d1f896SVladimir Oltean {
2268e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
2269e5d1f896SVladimir Oltean 		return;
2270e5d1f896SVladimir Oltean 
2271e5d1f896SVladimir Oltean 	list_del(&pgid->list);
2272e5d1f896SVladimir Oltean 	kfree(pgid);
2273e5d1f896SVladimir Oltean }
2274e5d1f896SVladimir Oltean 
2275e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
2276bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
22779403c158SVladimir Oltean {
2278e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2279e5d1f896SVladimir Oltean 	int index;
22809403c158SVladimir Oltean 
22819403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
22829403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
22839403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
22849403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
22859403c158SVladimir Oltean 	 */
2286bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
2287bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
2288e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
22899403c158SVladimir Oltean 
2290e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
2291e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
2292e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
2293e5d1f896SVladimir Oltean 		 */
2294e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
2295e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
2296e5d1f896SVladimir Oltean 			return pgid;
2297e5d1f896SVladimir Oltean 		}
2298e5d1f896SVladimir Oltean 	}
2299e5d1f896SVladimir Oltean 
2300e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
2301e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
23029403c158SVladimir Oltean 		bool used = false;
23039403c158SVladimir Oltean 
2304e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
2305e5d1f896SVladimir Oltean 			if (pgid->index == index) {
23069403c158SVladimir Oltean 				used = true;
23079403c158SVladimir Oltean 				break;
23089403c158SVladimir Oltean 			}
23099403c158SVladimir Oltean 		}
23109403c158SVladimir Oltean 
23119403c158SVladimir Oltean 		if (!used)
2312e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
23139403c158SVladimir Oltean 	}
23149403c158SVladimir Oltean 
2315e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
23169403c158SVladimir Oltean }
23179403c158SVladimir Oltean 
23189403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
2319bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
23209403c158SVladimir Oltean {
2321ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
23229403c158SVladimir Oltean 
2323bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
23249403c158SVladimir Oltean 		addr[0] = 0;
23259403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
23269403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
2327bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
23289403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
23299403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
23309403c158SVladimir Oltean 	}
23319403c158SVladimir Oltean }
23329403c158SVladimir Oltean 
2333209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
233454c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
233554c31984SVladimir Oltean 			const struct net_device *bridge)
2336a556c76aSAlexandre Belloni {
2337a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
2338004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
2339e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2340a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
2341a556c76aSAlexandre Belloni 
2342471beb11SVladimir Oltean 	if (port == ocelot->npi)
2343471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
2344471beb11SVladimir Oltean 
234554c31984SVladimir Oltean 	if (!vid)
234654c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
234754c31984SVladimir Oltean 
2348a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2349a556c76aSAlexandre Belloni 	if (!mc) {
2350728e69aeSVladimir Oltean 		/* New entry */
2351bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
2352bb8d53fdSVladimir Oltean 		if (!mc)
2353bb8d53fdSVladimir Oltean 			return -ENOMEM;
2354bb8d53fdSVladimir Oltean 
2355bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
2356bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
2357bb8d53fdSVladimir Oltean 		mc->vid = vid;
2358bb8d53fdSVladimir Oltean 
2359a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
2360728e69aeSVladimir Oltean 	} else {
2361e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
2362e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
2363e5d1f896SVladimir Oltean 		 */
2364e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
2365bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
2366a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
2367a556c76aSAlexandre Belloni 	}
2368a556c76aSAlexandre Belloni 
2369004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
2370e5d1f896SVladimir Oltean 
2371e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
2372e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
2373e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
2374e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
2375e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
2376e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
2377e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
2378e5d1f896SVladimir Oltean 	}
2379e5d1f896SVladimir Oltean 	mc->pgid = pgid;
2380e5d1f896SVladimir Oltean 
2381bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2382a556c76aSAlexandre Belloni 
2383e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
2384e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
2385e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2386e5d1f896SVladimir Oltean 				 pgid->index);
2387e5d1f896SVladimir Oltean 
2388e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
2389bb8d53fdSVladimir Oltean 				 mc->entry_type);
2390a556c76aSAlexandre Belloni }
2391209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
2392a556c76aSAlexandre Belloni 
2393209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
239454c31984SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb,
239554c31984SVladimir Oltean 			const struct net_device *bridge)
2396a556c76aSAlexandre Belloni {
2397a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
2398004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
2399e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2400a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
2401a556c76aSAlexandre Belloni 
2402471beb11SVladimir Oltean 	if (port == ocelot->npi)
2403471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
2404471beb11SVladimir Oltean 
240554c31984SVladimir Oltean 	if (!vid)
240654c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
240754c31984SVladimir Oltean 
2408a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2409a556c76aSAlexandre Belloni 	if (!mc)
2410a556c76aSAlexandre Belloni 		return -ENOENT;
2411a556c76aSAlexandre Belloni 
2412bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2413a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
2414a556c76aSAlexandre Belloni 
2415e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
2416004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
2417a556c76aSAlexandre Belloni 	if (!mc->ports) {
2418a556c76aSAlexandre Belloni 		list_del(&mc->list);
2419a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
2420a556c76aSAlexandre Belloni 		return 0;
2421a556c76aSAlexandre Belloni 	}
2422a556c76aSAlexandre Belloni 
2423e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
2424e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
2425e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
2426e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
2427e5d1f896SVladimir Oltean 	mc->pgid = pgid;
2428e5d1f896SVladimir Oltean 
2429bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2430a556c76aSAlexandre Belloni 
2431e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
2432e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
2433e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2434e5d1f896SVladimir Oltean 				 pgid->index);
2435e5d1f896SVladimir Oltean 
2436e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
2437bb8d53fdSVladimir Oltean 				 mc->entry_type);
2438a556c76aSAlexandre Belloni }
2439209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
2440a556c76aSAlexandre Belloni 
244154c31984SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
244254c31984SVladimir Oltean 			    struct net_device *bridge, int bridge_num,
244354c31984SVladimir Oltean 			    struct netlink_ext_ack *extack)
2444a556c76aSAlexandre Belloni {
2445df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
244654c31984SVladimir Oltean 	int err;
244754c31984SVladimir Oltean 
244854c31984SVladimir Oltean 	err = ocelot_single_vlan_aware_bridge(ocelot, extack);
244954c31984SVladimir Oltean 	if (err)
245054c31984SVladimir Oltean 		return err;
2451a556c76aSAlexandre Belloni 
24528abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
24538abe1970SVladimir Oltean 
2454df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
245554c31984SVladimir Oltean 	ocelot_port->bridge_num = bridge_num;
2456a556c76aSAlexandre Belloni 
24578abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
24588abe1970SVladimir Oltean 
24598abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
246054c31984SVladimir Oltean 
246154c31984SVladimir Oltean 	if (br_vlan_enabled(bridge))
246254c31984SVladimir Oltean 		return 0;
246354c31984SVladimir Oltean 
246454c31984SVladimir Oltean 	return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge);
2465a556c76aSAlexandre Belloni }
24665e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
2467a556c76aSAlexandre Belloni 
2468e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
2469a556c76aSAlexandre Belloni 			      struct net_device *bridge)
2470a556c76aSAlexandre Belloni {
2471df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
24722e554a7aSVladimir Oltean 
24738abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
24748abe1970SVladimir Oltean 
247554c31984SVladimir Oltean 	if (!br_vlan_enabled(bridge))
247654c31984SVladimir Oltean 		ocelot_del_vlan_unaware_pvid(ocelot, port, bridge);
247754c31984SVladimir Oltean 
2478df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
247954c31984SVladimir Oltean 	ocelot_port->bridge_num = -1;
24807142529fSAntoine Tenart 
2481d4004422SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, NULL);
24820da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
24838abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
24848abe1970SVladimir Oltean 
24858abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2486a556c76aSAlexandre Belloni }
24875e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
2488a556c76aSAlexandre Belloni 
2489dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
2490dc96ee37SAlexandre Belloni {
2491528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
2492dc96ee37SAlexandre Belloni 	int i, port, lag;
2493dc96ee37SAlexandre Belloni 
2494dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
249596b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
2496dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2497dc96ee37SAlexandre Belloni 
249896b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
2499dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
2500dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
2501dc96ee37SAlexandre Belloni 
2502528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
2503528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
2504528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
2505528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
2506528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
2507528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
2508528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
2509528d3f19SVladimir Oltean 	 */
2510528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2511528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2512528d3f19SVladimir Oltean 
2513528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
2514528d3f19SVladimir Oltean 			continue;
2515528d3f19SVladimir Oltean 
2516528d3f19SVladimir Oltean 		visited &= ~BIT(port);
2517528d3f19SVladimir Oltean 	}
2518528d3f19SVladimir Oltean 
2519528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
2520dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
2521528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
252223ca3b72SVladimir Oltean 		int num_active_ports = 0;
2523dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
2524dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
2525dc96ee37SAlexandre Belloni 
2526528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
2527dc96ee37SAlexandre Belloni 			continue;
2528dc96ee37SAlexandre Belloni 
2529a14e6b69SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond);
2530528d3f19SVladimir Oltean 
2531dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
2532a14e6b69SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2533a14e6b69SVladimir Oltean 
2534dc96ee37SAlexandre Belloni 			// Destination mask
2535dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
2536dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
2537a14e6b69SVladimir Oltean 
2538a14e6b69SVladimir Oltean 			if (ocelot_port->lag_tx_active)
253923ca3b72SVladimir Oltean 				aggr_idx[num_active_ports++] = port;
2540dc96ee37SAlexandre Belloni 		}
2541dc96ee37SAlexandre Belloni 
254296b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
2543dc96ee37SAlexandre Belloni 			u32 ac;
2544dc96ee37SAlexandre Belloni 
2545dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
2546dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
254723ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
254823ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
254923ca3b72SVladimir Oltean 			 */
255023ca3b72SVladimir Oltean 			if (num_active_ports)
255123ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
2552dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
2553dc96ee37SAlexandre Belloni 		}
2554528d3f19SVladimir Oltean 
2555528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
2556528d3f19SVladimir Oltean 		 * the same config again.
2557528d3f19SVladimir Oltean 		 */
2558528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
2559528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2560528d3f19SVladimir Oltean 
2561528d3f19SVladimir Oltean 			if (!ocelot_port)
2562528d3f19SVladimir Oltean 				continue;
2563528d3f19SVladimir Oltean 
2564528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
2565528d3f19SVladimir Oltean 				visited |= BIT(port);
2566528d3f19SVladimir Oltean 		}
2567dc96ee37SAlexandre Belloni 	}
2568dc96ee37SAlexandre Belloni }
2569dc96ee37SAlexandre Belloni 
25702527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
25712527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
25722527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
25732527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
25742527f2e8SVladimir Oltean  */
25752527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
2576dc96ee37SAlexandre Belloni {
25772527f2e8SVladimir Oltean 	int port;
2578dc96ee37SAlexandre Belloni 
25792527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
25802527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
25812527f2e8SVladimir Oltean 		struct net_device *bond;
2582dc96ee37SAlexandre Belloni 
25832527f2e8SVladimir Oltean 		if (!ocelot_port)
25842527f2e8SVladimir Oltean 			continue;
2585dc96ee37SAlexandre Belloni 
25862527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
25872527f2e8SVladimir Oltean 		if (bond) {
2588961d8b69SVladimir Oltean 			int lag = ocelot_bond_get_id(ocelot, bond);
25892527f2e8SVladimir Oltean 
25902527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
2591dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
25922527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
25932527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
25942527f2e8SVladimir Oltean 		} else {
25952527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
25962527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
25972527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
25982527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
25992527f2e8SVladimir Oltean 		}
2600dc96ee37SAlexandre Belloni 	}
2601dc96ee37SAlexandre Belloni }
2602dc96ee37SAlexandre Belloni 
2603961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says:
2604961d8b69SVladimir Oltean  *     Logical port number for front port. If port is not a member of a LLAG,
2605961d8b69SVladimir Oltean  *     then PORTID must be set to the physical port number.
2606961d8b69SVladimir Oltean  *     If port is a member of a LLAG, then PORTID must be set to the common
2607961d8b69SVladimir Oltean  *     PORTID_VAL used for all member ports of the LLAG.
2608961d8b69SVladimir Oltean  *     The value must not exceed the number of physical ports on the device.
2609961d8b69SVladimir Oltean  *
2610961d8b69SVladimir Oltean  * This means we have little choice but to migrate FDB entries pointing towards
2611961d8b69SVladimir Oltean  * a logical port when that changes.
2612961d8b69SVladimir Oltean  */
2613961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot,
2614961d8b69SVladimir Oltean 				    struct net_device *bond,
2615961d8b69SVladimir Oltean 				    int lag)
2616961d8b69SVladimir Oltean {
2617961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2618961d8b69SVladimir Oltean 	int err;
2619961d8b69SVladimir Oltean 
2620961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2621961d8b69SVladimir Oltean 
2622961d8b69SVladimir Oltean 	list_for_each_entry(fdb, &ocelot->lag_fdbs, list) {
2623961d8b69SVladimir Oltean 		if (fdb->bond != bond)
2624961d8b69SVladimir Oltean 			continue;
2625961d8b69SVladimir Oltean 
2626961d8b69SVladimir Oltean 		err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid);
2627961d8b69SVladimir Oltean 		if (err) {
2628961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2629961d8b69SVladimir Oltean 				"failed to delete LAG %s FDB %pM vid %d: %pe\n",
2630961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2631961d8b69SVladimir Oltean 		}
2632961d8b69SVladimir Oltean 
2633961d8b69SVladimir Oltean 		err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid,
2634961d8b69SVladimir Oltean 					ENTRYTYPE_LOCKED);
2635961d8b69SVladimir Oltean 		if (err) {
2636961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2637961d8b69SVladimir Oltean 				"failed to migrate LAG %s FDB %pM vid %d: %pe\n",
2638961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2639961d8b69SVladimir Oltean 		}
2640961d8b69SVladimir Oltean 	}
2641961d8b69SVladimir Oltean }
2642961d8b69SVladimir Oltean 
26439c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2644583cbbe3SVladimir Oltean 			 struct net_device *bond,
2645583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
2646dc96ee37SAlexandre Belloni {
2647583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
2648583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
2649583cbbe3SVladimir Oltean 
26508abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
26518abe1970SVladimir Oltean 
2652b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
2653dc96ee37SAlexandre Belloni 
26542527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
26558abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2656dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2657dc96ee37SAlexandre Belloni 
26588abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
26598abe1970SVladimir Oltean 
2660dc96ee37SAlexandre Belloni 	return 0;
2661dc96ee37SAlexandre Belloni }
26629c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
2663dc96ee37SAlexandre Belloni 
26649c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2665dc96ee37SAlexandre Belloni 			   struct net_device *bond)
2666dc96ee37SAlexandre Belloni {
2667961d8b69SVladimir Oltean 	int old_lag_id, new_lag_id;
2668961d8b69SVladimir Oltean 
26698abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
26708abe1970SVladimir Oltean 
2671961d8b69SVladimir Oltean 	old_lag_id = ocelot_bond_get_id(ocelot, bond);
2672961d8b69SVladimir Oltean 
2673b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
2674b80af659SVladimir Oltean 
26752527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
26768abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2677dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
26788abe1970SVladimir Oltean 
2679961d8b69SVladimir Oltean 	new_lag_id = ocelot_bond_get_id(ocelot, bond);
2680961d8b69SVladimir Oltean 
2681961d8b69SVladimir Oltean 	if (new_lag_id >= 0 && old_lag_id != new_lag_id)
2682961d8b69SVladimir Oltean 		ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id);
2683961d8b69SVladimir Oltean 
26848abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2685dc96ee37SAlexandre Belloni }
26869c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
26870e332c85SPetr Machata 
268823ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
268923ca3b72SVladimir Oltean {
269023ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
269123ca3b72SVladimir Oltean 
2692961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2693961d8b69SVladimir Oltean 
269423ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
269523ca3b72SVladimir Oltean 
269623ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
269723ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
2698961d8b69SVladimir Oltean 
2699961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
270023ca3b72SVladimir Oltean }
270123ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
270223ca3b72SVladimir Oltean 
2703961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond,
270454c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
270554c31984SVladimir Oltean 		       const struct net_device *bridge)
2706961d8b69SVladimir Oltean {
2707961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2708961d8b69SVladimir Oltean 	int lag, err;
2709961d8b69SVladimir Oltean 
2710961d8b69SVladimir Oltean 	fdb = kzalloc(sizeof(*fdb), GFP_KERNEL);
2711961d8b69SVladimir Oltean 	if (!fdb)
2712961d8b69SVladimir Oltean 		return -ENOMEM;
2713961d8b69SVladimir Oltean 
271454c31984SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
271554c31984SVladimir Oltean 
271654c31984SVladimir Oltean 	if (!vid)
271754c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
271854c31984SVladimir Oltean 
2719961d8b69SVladimir Oltean 	ether_addr_copy(fdb->addr, addr);
2720961d8b69SVladimir Oltean 	fdb->vid = vid;
2721961d8b69SVladimir Oltean 	fdb->bond = bond;
2722961d8b69SVladimir Oltean 
2723961d8b69SVladimir Oltean 	lag = ocelot_bond_get_id(ocelot, bond);
2724961d8b69SVladimir Oltean 
2725961d8b69SVladimir Oltean 	err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED);
2726961d8b69SVladimir Oltean 	if (err) {
2727961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2728961d8b69SVladimir Oltean 		kfree(fdb);
2729961d8b69SVladimir Oltean 		return err;
2730961d8b69SVladimir Oltean 	}
2731961d8b69SVladimir Oltean 
2732961d8b69SVladimir Oltean 	list_add_tail(&fdb->list, &ocelot->lag_fdbs);
2733961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2734961d8b69SVladimir Oltean 
2735961d8b69SVladimir Oltean 	return 0;
2736961d8b69SVladimir Oltean }
2737961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add);
2738961d8b69SVladimir Oltean 
2739961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond,
274054c31984SVladimir Oltean 		       const unsigned char *addr, u16 vid,
274154c31984SVladimir Oltean 		       const struct net_device *bridge)
2742961d8b69SVladimir Oltean {
2743961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb, *tmp;
2744961d8b69SVladimir Oltean 
2745961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2746961d8b69SVladimir Oltean 
274754c31984SVladimir Oltean 	if (!vid)
274854c31984SVladimir Oltean 		vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
274954c31984SVladimir Oltean 
2750961d8b69SVladimir Oltean 	list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) {
2751961d8b69SVladimir Oltean 		if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid ||
2752961d8b69SVladimir Oltean 		    fdb->bond != bond)
2753961d8b69SVladimir Oltean 			continue;
2754961d8b69SVladimir Oltean 
2755961d8b69SVladimir Oltean 		ocelot_mact_forget(ocelot, addr, vid);
2756961d8b69SVladimir Oltean 		list_del(&fdb->list);
2757961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2758961d8b69SVladimir Oltean 		kfree(fdb);
2759961d8b69SVladimir Oltean 
2760961d8b69SVladimir Oltean 		return 0;
2761961d8b69SVladimir Oltean 	}
2762961d8b69SVladimir Oltean 
2763961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2764961d8b69SVladimir Oltean 
2765961d8b69SVladimir Oltean 	return -ENOENT;
2766961d8b69SVladimir Oltean }
2767961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del);
2768961d8b69SVladimir Oltean 
2769a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2770a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
27710b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
27720b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
27730b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2774a8015dedSVladimir Oltean  */
27750b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
277631350d7fSVladimir Oltean {
277731350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2778a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2779e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
2780601e984fSVladimir Oltean 	int atop, atop_tot;
278131350d7fSVladimir Oltean 
27820b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
27830b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
27840b912fc9SVladimir Oltean 
2785cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
27860b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2787cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
27880b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
27890b912fc9SVladimir Oltean 	}
27900b912fc9SVladimir Oltean 
2791a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2792fa914e9cSVladimir Oltean 
2793e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
2794e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2795e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2796541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2797541132f0SMaxim Kochetkov 			    pause_start);
2798541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2799541132f0SMaxim Kochetkov 			    pause_stop);
2800fa914e9cSVladimir Oltean 
2801601e984fSVladimir Oltean 	/* Tail dropping watermarks */
2802f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2803a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2804601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2805601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2806601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2807fa914e9cSVladimir Oltean }
28080b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
28090b912fc9SVladimir Oltean 
28100b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
28110b912fc9SVladimir Oltean {
28120b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
28130b912fc9SVladimir Oltean 
28140b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
28150b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
28160b912fc9SVladimir Oltean 
2817cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
28180b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2819cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
28200b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
28210b912fc9SVladimir Oltean 	}
28220b912fc9SVladimir Oltean 
28230b912fc9SVladimir Oltean 	return max_mtu;
28240b912fc9SVladimir Oltean }
28250b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2826fa914e9cSVladimir Oltean 
2827421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2828421741eaSVladimir Oltean 				     bool enabled)
2829421741eaSVladimir Oltean {
2830421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2831421741eaSVladimir Oltean 	u32 val = 0;
2832421741eaSVladimir Oltean 
2833421741eaSVladimir Oltean 	if (enabled)
2834421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2835421741eaSVladimir Oltean 
2836421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2837421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2838421741eaSVladimir Oltean 
2839421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2840421741eaSVladimir Oltean }
2841421741eaSVladimir Oltean 
2842421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2843421741eaSVladimir Oltean 					bool enabled)
2844421741eaSVladimir Oltean {
2845421741eaSVladimir Oltean 	u32 val = 0;
2846421741eaSVladimir Oltean 
2847421741eaSVladimir Oltean 	if (enabled)
2848421741eaSVladimir Oltean 		val = BIT(port);
2849421741eaSVladimir Oltean 
2850421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2851421741eaSVladimir Oltean }
2852421741eaSVladimir Oltean 
2853421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2854421741eaSVladimir Oltean 					bool enabled)
2855421741eaSVladimir Oltean {
2856421741eaSVladimir Oltean 	u32 val = 0;
2857421741eaSVladimir Oltean 
2858421741eaSVladimir Oltean 	if (enabled)
2859421741eaSVladimir Oltean 		val = BIT(port);
2860421741eaSVladimir Oltean 
2861421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
2862*4cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4);
2863*4cf35a2bSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6);
2864421741eaSVladimir Oltean }
2865421741eaSVladimir Oltean 
2866421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2867421741eaSVladimir Oltean 					bool enabled)
2868421741eaSVladimir Oltean {
2869421741eaSVladimir Oltean 	u32 val = 0;
2870421741eaSVladimir Oltean 
2871421741eaSVladimir Oltean 	if (enabled)
2872421741eaSVladimir Oltean 		val = BIT(port);
2873421741eaSVladimir Oltean 
2874421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2875421741eaSVladimir Oltean }
2876421741eaSVladimir Oltean 
2877421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2878421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2879421741eaSVladimir Oltean {
2880421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2881421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2882421741eaSVladimir Oltean 		return -EINVAL;
2883421741eaSVladimir Oltean 
2884421741eaSVladimir Oltean 	return 0;
2885421741eaSVladimir Oltean }
2886421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2887421741eaSVladimir Oltean 
2888421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2889421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2890421741eaSVladimir Oltean {
2891ac455209SVladimir Oltean 	if (port == ocelot->npi)
2892ac455209SVladimir Oltean 		port = ocelot->num_phys_ports;
2893ac455209SVladimir Oltean 
2894421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2895421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2896421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2897421741eaSVladimir Oltean 
2898421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2899421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2900421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2901421741eaSVladimir Oltean 
2902421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2903421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2904421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2905421741eaSVladimir Oltean 
2906421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2907421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2908421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2909421741eaSVladimir Oltean }
2910421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2911421741eaSVladimir Oltean 
2912978777d0SVladimir Oltean int ocelot_port_get_default_prio(struct ocelot *ocelot, int port)
2913978777d0SVladimir Oltean {
2914978777d0SVladimir Oltean 	int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2915978777d0SVladimir Oltean 
2916978777d0SVladimir Oltean 	return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val);
2917978777d0SVladimir Oltean }
2918978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio);
2919978777d0SVladimir Oltean 
2920978777d0SVladimir Oltean int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio)
2921978777d0SVladimir Oltean {
292272f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2923978777d0SVladimir Oltean 		return -ERANGE;
2924978777d0SVladimir Oltean 
2925978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot,
2926978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio),
2927978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M,
2928978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG,
2929978777d0SVladimir Oltean 		       port);
2930978777d0SVladimir Oltean 
2931978777d0SVladimir Oltean 	return 0;
2932978777d0SVladimir Oltean }
2933978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio);
2934978777d0SVladimir Oltean 
2935978777d0SVladimir Oltean int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp)
2936978777d0SVladimir Oltean {
2937978777d0SVladimir Oltean 	int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
2938978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2939978777d0SVladimir Oltean 
2940978777d0SVladimir Oltean 	/* Return error if DSCP prioritization isn't enabled */
2941978777d0SVladimir Oltean 	if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA))
2942978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2943978777d0SVladimir Oltean 
2944978777d0SVladimir Oltean 	if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) {
2945978777d0SVladimir Oltean 		dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg);
2946978777d0SVladimir Oltean 		/* Re-read ANA_DSCP_CFG for the translated DSCP */
2947978777d0SVladimir Oltean 		dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2948978777d0SVladimir Oltean 	}
2949978777d0SVladimir Oltean 
2950978777d0SVladimir Oltean 	/* If the DSCP value is not trusted, the QoS classification falls back
2951978777d0SVladimir Oltean 	 * to VLAN PCP or port-based default.
2952978777d0SVladimir Oltean 	 */
2953978777d0SVladimir Oltean 	if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA))
2954978777d0SVladimir Oltean 		return -EOPNOTSUPP;
2955978777d0SVladimir Oltean 
2956978777d0SVladimir Oltean 	return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg);
2957978777d0SVladimir Oltean }
2958978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio);
2959978777d0SVladimir Oltean 
2960978777d0SVladimir Oltean int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2961978777d0SVladimir Oltean {
2962978777d0SVladimir Oltean 	int mask, val;
2963978777d0SVladimir Oltean 
296472f56fdbSVladimir Oltean 	if (prio >= OCELOT_NUM_TC)
2965978777d0SVladimir Oltean 		return -ERANGE;
2966978777d0SVladimir Oltean 
2967978777d0SVladimir Oltean 	/* There is at least one app table priority (this one), so we need to
2968978777d0SVladimir Oltean 	 * make sure DSCP prioritization is enabled on the port.
2969978777d0SVladimir Oltean 	 * Also make sure DSCP translation is disabled
2970978777d0SVladimir Oltean 	 * (dcbnl doesn't support it).
2971978777d0SVladimir Oltean 	 */
2972978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
2973978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
2974978777d0SVladimir Oltean 
2975978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask,
2976978777d0SVladimir Oltean 		       ANA_PORT_QOS_CFG, port);
2977978777d0SVladimir Oltean 
2978978777d0SVladimir Oltean 	/* Trust this DSCP value and map it to the given QoS class */
2979978777d0SVladimir Oltean 	val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio);
2980978777d0SVladimir Oltean 
2981978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp);
2982978777d0SVladimir Oltean 
2983978777d0SVladimir Oltean 	return 0;
2984978777d0SVladimir Oltean }
2985978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio);
2986978777d0SVladimir Oltean 
2987978777d0SVladimir Oltean int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
2988978777d0SVladimir Oltean {
2989978777d0SVladimir Oltean 	int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
2990978777d0SVladimir Oltean 	int mask, i;
2991978777d0SVladimir Oltean 
2992978777d0SVladimir Oltean 	/* During a "dcb app replace" command, the new app table entry will be
2993978777d0SVladimir Oltean 	 * added first, then the old one will be deleted. But the hardware only
2994978777d0SVladimir Oltean 	 * supports one QoS class per DSCP value (duh), so if we blindly delete
2995978777d0SVladimir Oltean 	 * the app table entry for this DSCP value, we end up deleting the
2996978777d0SVladimir Oltean 	 * entry with the new priority. Avoid that by checking whether user
2997978777d0SVladimir Oltean 	 * space wants to delete the priority which is currently configured, or
2998978777d0SVladimir Oltean 	 * something else which is no longer current.
2999978777d0SVladimir Oltean 	 */
3000978777d0SVladimir Oltean 	if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio)
3001978777d0SVladimir Oltean 		return 0;
3002978777d0SVladimir Oltean 
3003978777d0SVladimir Oltean 	/* Untrust this DSCP value */
3004978777d0SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp);
3005978777d0SVladimir Oltean 
3006978777d0SVladimir Oltean 	for (i = 0; i < 64; i++) {
3007978777d0SVladimir Oltean 		int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i);
3008978777d0SVladimir Oltean 
3009978777d0SVladimir Oltean 		/* There are still app table entries on the port, so we need to
3010978777d0SVladimir Oltean 		 * keep DSCP enabled, nothing to do.
3011978777d0SVladimir Oltean 		 */
3012978777d0SVladimir Oltean 		if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)
3013978777d0SVladimir Oltean 			return 0;
3014978777d0SVladimir Oltean 	}
3015978777d0SVladimir Oltean 
3016978777d0SVladimir Oltean 	/* Disable DSCP QoS classification if there isn't any trusted
3017978777d0SVladimir Oltean 	 * DSCP value left.
3018978777d0SVladimir Oltean 	 */
3019978777d0SVladimir Oltean 	mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
3020978777d0SVladimir Oltean 	       ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
3021978777d0SVladimir Oltean 
3022978777d0SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port);
3023978777d0SVladimir Oltean 
3024978777d0SVladimir Oltean 	return 0;
3025978777d0SVladimir Oltean }
3026978777d0SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio);
3027978777d0SVladimir Oltean 
3028f2a0e216SVladimir Oltean struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to,
3029ccb6ed42SVladimir Oltean 					struct netlink_ext_ack *extack)
3030ccb6ed42SVladimir Oltean {
3031ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
3032ccb6ed42SVladimir Oltean 
3033ccb6ed42SVladimir Oltean 	if (m) {
3034ccb6ed42SVladimir Oltean 		if (m->to != to) {
3035ccb6ed42SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
3036ccb6ed42SVladimir Oltean 					   "Mirroring already configured towards different egress port");
3037ccb6ed42SVladimir Oltean 			return ERR_PTR(-EBUSY);
3038ccb6ed42SVladimir Oltean 		}
3039ccb6ed42SVladimir Oltean 
3040ccb6ed42SVladimir Oltean 		refcount_inc(&m->refcount);
3041ccb6ed42SVladimir Oltean 		return m;
3042ccb6ed42SVladimir Oltean 	}
3043ccb6ed42SVladimir Oltean 
3044ccb6ed42SVladimir Oltean 	m = kzalloc(sizeof(*m), GFP_KERNEL);
3045ccb6ed42SVladimir Oltean 	if (!m)
3046ccb6ed42SVladimir Oltean 		return ERR_PTR(-ENOMEM);
3047ccb6ed42SVladimir Oltean 
3048ccb6ed42SVladimir Oltean 	m->to = to;
3049ccb6ed42SVladimir Oltean 	refcount_set(&m->refcount, 1);
3050ccb6ed42SVladimir Oltean 	ocelot->mirror = m;
3051ccb6ed42SVladimir Oltean 
3052ccb6ed42SVladimir Oltean 	/* Program the mirror port to hardware */
3053ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS);
3054ccb6ed42SVladimir Oltean 
3055ccb6ed42SVladimir Oltean 	return m;
3056ccb6ed42SVladimir Oltean }
3057ccb6ed42SVladimir Oltean 
3058f2a0e216SVladimir Oltean void ocelot_mirror_put(struct ocelot *ocelot)
3059ccb6ed42SVladimir Oltean {
3060ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot->mirror;
3061ccb6ed42SVladimir Oltean 
3062ccb6ed42SVladimir Oltean 	if (!refcount_dec_and_test(&m->refcount))
3063ccb6ed42SVladimir Oltean 		return;
3064ccb6ed42SVladimir Oltean 
3065ccb6ed42SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_MIRRORPORTS);
3066ccb6ed42SVladimir Oltean 	ocelot->mirror = NULL;
3067ccb6ed42SVladimir Oltean 	kfree(m);
3068ccb6ed42SVladimir Oltean }
3069ccb6ed42SVladimir Oltean 
3070ccb6ed42SVladimir Oltean int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to,
3071ccb6ed42SVladimir Oltean 			   bool ingress, struct netlink_ext_ack *extack)
3072ccb6ed42SVladimir Oltean {
3073ccb6ed42SVladimir Oltean 	struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack);
3074ccb6ed42SVladimir Oltean 
3075ccb6ed42SVladimir Oltean 	if (IS_ERR(m))
3076ccb6ed42SVladimir Oltean 		return PTR_ERR(m);
3077ccb6ed42SVladimir Oltean 
3078ccb6ed42SVladimir Oltean 	if (ingress) {
3079ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
3080ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
3081ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
3082ccb6ed42SVladimir Oltean 	} else {
3083ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, BIT(from), BIT(from),
3084ccb6ed42SVladimir Oltean 			   ANA_EMIRRORPORTS);
3085ccb6ed42SVladimir Oltean 	}
3086ccb6ed42SVladimir Oltean 
3087ccb6ed42SVladimir Oltean 	return 0;
3088ccb6ed42SVladimir Oltean }
3089ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_add);
3090ccb6ed42SVladimir Oltean 
3091ccb6ed42SVladimir Oltean void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress)
3092ccb6ed42SVladimir Oltean {
3093ccb6ed42SVladimir Oltean 	if (ingress) {
3094ccb6ed42SVladimir Oltean 		ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
3095ccb6ed42SVladimir Oltean 			       ANA_PORT_PORT_CFG, from);
3096ccb6ed42SVladimir Oltean 	} else {
3097ccb6ed42SVladimir Oltean 		ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS);
3098ccb6ed42SVladimir Oltean 	}
3099ccb6ed42SVladimir Oltean 
3100ccb6ed42SVladimir Oltean 	ocelot_mirror_put(ocelot);
3101ccb6ed42SVladimir Oltean }
3102ccb6ed42SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_port_mirror_del);
3103ccb6ed42SVladimir Oltean 
31045e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
3105fa914e9cSVladimir Oltean {
3106fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3107fa914e9cSVladimir Oltean 
3108b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
310931350d7fSVladimir Oltean 
311031350d7fSVladimir Oltean 	/* Basic L2 initialization */
311131350d7fSVladimir Oltean 
31125bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
31135bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
31145bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
31155bc9d2e6SVladimir Oltean 	 */
31165bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
31175bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
31185bc9d2e6SVladimir Oltean 
31195bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
31205bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
31215bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
31225bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
31235bc9d2e6SVladimir Oltean 	mdelay(1);
31245bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
31255bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
31265bc9d2e6SVladimir Oltean 
31275bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
3128a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
31295bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
31305bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
3131a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
31325bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
31335bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
31345bc9d2e6SVladimir Oltean 
31355bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
31365bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
31375bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
31385bc9d2e6SVladimir Oltean 
3139e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
3140541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
3141e8e6e73dSVladimir Oltean 
314231350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
314331350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
314431350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
314531350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
314631350d7fSVladimir Oltean 
314731350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
314831350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
314931350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
315031350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
315131350d7fSVladimir Oltean 
3152421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
3153421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
3154421741eaSVladimir Oltean 
315546efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
315646efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
315746efe4efSVladimir Oltean 	 * automatic.
315846efe4efSVladimir Oltean 	 */
315946efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
316046efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
316146efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
316246efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
316346efe4efSVladimir Oltean 
316431350d7fSVladimir Oltean 	/* Enable vcap lookups */
316531350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
316631350d7fSVladimir Oltean }
31675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
316831350d7fSVladimir Oltean 
31692d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
31702d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
31712d44b097SVladimir Oltean  * NPI mode is used).
317269df578cSVladimir Oltean  */
31732d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
317421468199SVladimir Oltean {
317569df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
317669df578cSVladimir Oltean 
317769df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
317821468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
317969df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
318069df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
318169df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
318269df578cSVladimir Oltean 	 */
318321468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
318421468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
318521468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
318621468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
318721468199SVladimir Oltean 
318869df578cSVladimir Oltean 	/* Enable CPU port module */
3189886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
319069df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
3191886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
3192cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
3193886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
3194cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
319521468199SVladimir Oltean 
319621468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
3197bfbab310SVladimir Oltean 	ocelot_write_gix(ocelot,
319854c31984SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) |
319921468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
320021468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
320121468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
320221468199SVladimir Oltean }
320321468199SVladimir Oltean 
3204f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
3205f6fe01d6SVladimir Oltean {
3206f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
3207f6fe01d6SVladimir Oltean 
3208f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
3209f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
3210f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
3211f6fe01d6SVladimir Oltean 	 */
3212f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
3213f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
3214f6fe01d6SVladimir Oltean 
3215f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
3216f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
3217f6fe01d6SVladimir Oltean }
3218f6fe01d6SVladimir Oltean 
3219a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
3220a556c76aSAlexandre Belloni {
3221a556c76aSAlexandre Belloni 	char queue_name[32];
322221468199SVladimir Oltean 	int i, ret;
322321468199SVladimir Oltean 	u32 port;
3224a556c76aSAlexandre Belloni 
32253a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
32263a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
32273a77b593SVladimir Oltean 		if (ret) {
32283a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
32293a77b593SVladimir Oltean 			return ret;
32303a77b593SVladimir Oltean 		}
32313a77b593SVladimir Oltean 	}
32323a77b593SVladimir Oltean 
3233a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
3234a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
3235a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
3236a556c76aSAlexandre Belloni 	if (!ocelot->stats)
3237a556c76aSAlexandre Belloni 		return -ENOMEM;
3238a556c76aSAlexandre Belloni 
3239a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
32404e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
32412468346cSVladimir Oltean 	mutex_init(&ocelot->mact_lock);
32428abe1970SVladimir Oltean 	mutex_init(&ocelot->fwd_domain_lock);
32434e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
324452849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
3245a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
3246a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
3247a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
3248a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
3249a556c76aSAlexandre Belloni 		return -ENOMEM;
3250a556c76aSAlexandre Belloni 
3251ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
3252ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
3253ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
3254ca0b272bSVladimir Oltean 		return -ENOMEM;
3255ca0b272bSVladimir Oltean 	}
3256ca0b272bSVladimir Oltean 
32572b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
3258e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
325990e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
3260961d8b69SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->lag_fdbs);
3261f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
3262a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
3263a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
3264aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
32652d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
3266a556c76aSAlexandre Belloni 
326723e2c506SXiaoliang Yang 	if (ocelot->ops->psfp_init)
326823e2c506SXiaoliang Yang 		ocelot->ops->psfp_init(ocelot);
326923e2c506SXiaoliang Yang 
3270a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3271a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
3272a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
3273a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
3274a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
3275a556c76aSAlexandre Belloni 	}
3276a556c76aSAlexandre Belloni 
3277a556c76aSAlexandre Belloni 	/* Only use S-Tag */
3278a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
3279a556c76aSAlexandre Belloni 
3280a556c76aSAlexandre Belloni 	/* Aggregation mode */
3281a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
3282a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
3283a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
3284f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
3285f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
3286f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
3287f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
3288a556c76aSAlexandre Belloni 
3289a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
3290a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
3291a556c76aSAlexandre Belloni 	 */
3292a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
3293a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
3294a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
3295a556c76aSAlexandre Belloni 
3296a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
3297a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
3298a556c76aSAlexandre Belloni 
3299a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
3300a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
3301a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
3302a556c76aSAlexandre Belloni 
3303a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
3304edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
3305a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
3306b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
3307a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
3308edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
3309a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
3310a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
3311a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
3312a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
3313a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
3314a556c76aSAlexandre Belloni 
3315a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3316a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
3317a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
3318a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
3319a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
3320a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
3321a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
3322a556c76aSAlexandre Belloni 				 port);
3323a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
3324a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
3325a556c76aSAlexandre Belloni 	}
3326a556c76aSAlexandre Belloni 
332796b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
3328a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
3329a556c76aSAlexandre Belloni 
3330a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
3331a556c76aSAlexandre Belloni 	}
3332ebb1bb40SHoratiu Vultur 
3333ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
3334ebb1bb40SHoratiu Vultur 
3335b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
3336b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3337b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3338a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
3339b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3340b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
3341b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
3342a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
3343a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
3344a556c76aSAlexandre Belloni 
3345a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
3346a556c76aSAlexandre Belloni 	 * registers endianness.
3347a556c76aSAlexandre Belloni 	 */
3348a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
3349a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
3350a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
3351a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
3352a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
3353a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
3354a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
3355a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
3356a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
3357a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
3358a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
3359a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
3360a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
3361a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
3362a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
3363a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
3364a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
3365a556c76aSAlexandre Belloni 
3366d87b1c08SColin Foster 	ret = ocelot_prepare_stats_regions(ocelot);
3367d87b1c08SColin Foster 	if (ret) {
3368d87b1c08SColin Foster 		destroy_workqueue(ocelot->stats_queue);
3369d87b1c08SColin Foster 		destroy_workqueue(ocelot->owq);
3370d87b1c08SColin Foster 		return ret;
3371d87b1c08SColin Foster 	}
3372d87b1c08SColin Foster 
33731e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
3374a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
3375a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
33764e3b0468SAntoine Tenart 
3377a556c76aSAlexandre Belloni 	return 0;
3378a556c76aSAlexandre Belloni }
3379a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
3380a556c76aSAlexandre Belloni 
3381a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
3382a556c76aSAlexandre Belloni {
3383c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
3384a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
3385ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
3386a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
3387a556c76aSAlexandre Belloni }
3388a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
3389a556c76aSAlexandre Belloni 
3390e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
3391e5fb512dSVladimir Oltean {
3392e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3393e5fb512dSVladimir Oltean 
3394e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
3395e5fb512dSVladimir Oltean }
3396e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
3397e5fb512dSVladimir Oltean 
3398a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
3399