xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 23e2c506ad6c588b469e3d06cc20299434440d02)
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
16639c1b26SSteen Hegelund 
17a556c76aSAlexandre Belloni struct ocelot_mact_entry {
18a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
19a556c76aSAlexandre Belloni 	u16 vid;
20a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
21a556c76aSAlexandre Belloni };
22a556c76aSAlexandre Belloni 
232468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
24639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
25639c1b26SSteen Hegelund {
26639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
27639c1b26SSteen Hegelund }
28639c1b26SSteen Hegelund 
292468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
30a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
31a556c76aSAlexandre Belloni {
32639c1b26SSteen Hegelund 	u32 val;
33a556c76aSAlexandre Belloni 
34639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
35639c1b26SSteen Hegelund 		ocelot, val,
36639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
37639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
38639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
39a556c76aSAlexandre Belloni }
40a556c76aSAlexandre Belloni 
412468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
42a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
43a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
44a556c76aSAlexandre Belloni 			       unsigned int vid)
45a556c76aSAlexandre Belloni {
46a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
47a556c76aSAlexandre Belloni 
48a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
49a556c76aSAlexandre Belloni 	 * understood by the hardware.
50a556c76aSAlexandre Belloni 	 */
51a556c76aSAlexandre Belloni 	mach |= vid    << 16;
52a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
53a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
54a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
55a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
56a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
57a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
58a556c76aSAlexandre Belloni 
59a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
60a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
61a556c76aSAlexandre Belloni 
62a556c76aSAlexandre Belloni }
63a556c76aSAlexandre Belloni 
640568c3bfSXiaoliang Yang static int __ocelot_mact_learn(struct ocelot *ocelot, int port,
65a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
669c90eea3SVladimir Oltean 			       unsigned int vid, enum macaccess_entry_type type)
67a556c76aSAlexandre Belloni {
68584b7cfcSAlban Bedel 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
69584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
70584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
71584b7cfcSAlban Bedel 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
72584b7cfcSAlban Bedel 	unsigned int mc_ports;
732468346cSVladimir Oltean 	int err;
74584b7cfcSAlban Bedel 
75584b7cfcSAlban Bedel 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
76584b7cfcSAlban Bedel 	if (type == ENTRYTYPE_MACv4)
77584b7cfcSAlban Bedel 		mc_ports = (mac[1] << 8) | mac[2];
78584b7cfcSAlban Bedel 	else if (type == ENTRYTYPE_MACv6)
79584b7cfcSAlban Bedel 		mc_ports = (mac[0] << 8) | mac[1];
80584b7cfcSAlban Bedel 	else
81584b7cfcSAlban Bedel 		mc_ports = 0;
82584b7cfcSAlban Bedel 
83584b7cfcSAlban Bedel 	if (mc_ports & BIT(ocelot->num_phys_ports))
84584b7cfcSAlban Bedel 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
85584b7cfcSAlban Bedel 
86a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
87a556c76aSAlexandre Belloni 
88a556c76aSAlexandre Belloni 	/* Issue a write command */
89584b7cfcSAlban Bedel 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
90a556c76aSAlexandre Belloni 
912468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
922468346cSVladimir Oltean 
930568c3bfSXiaoliang Yang 	return err;
940568c3bfSXiaoliang Yang }
950568c3bfSXiaoliang Yang 
960568c3bfSXiaoliang Yang int ocelot_mact_learn(struct ocelot *ocelot, int port,
970568c3bfSXiaoliang Yang 		      const unsigned char mac[ETH_ALEN],
980568c3bfSXiaoliang Yang 		      unsigned int vid, enum macaccess_entry_type type)
990568c3bfSXiaoliang Yang {
1000568c3bfSXiaoliang Yang 	int ret;
1010568c3bfSXiaoliang Yang 
1020568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1030568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, port, mac, vid, type);
1042468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1052468346cSVladimir Oltean 
1060568c3bfSXiaoliang Yang 	return ret;
107a556c76aSAlexandre Belloni }
1089c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_learn);
109a556c76aSAlexandre Belloni 
1109c90eea3SVladimir Oltean int ocelot_mact_forget(struct ocelot *ocelot,
1119c90eea3SVladimir Oltean 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
112a556c76aSAlexandre Belloni {
1132468346cSVladimir Oltean 	int err;
1142468346cSVladimir Oltean 
1152468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
1162468346cSVladimir Oltean 
117a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
118a556c76aSAlexandre Belloni 
119a556c76aSAlexandre Belloni 	/* Issue a forget command */
120a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
121a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
122a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
123a556c76aSAlexandre Belloni 
1242468346cSVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
1252468346cSVladimir Oltean 
1262468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
1272468346cSVladimir Oltean 
1282468346cSVladimir Oltean 	return err;
129a556c76aSAlexandre Belloni }
1309c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_mact_forget);
131a556c76aSAlexandre Belloni 
1320568c3bfSXiaoliang Yang int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx,
1330568c3bfSXiaoliang Yang 		       const unsigned char mac[ETH_ALEN],
1340568c3bfSXiaoliang Yang 		       unsigned int vid, enum macaccess_entry_type *type)
1350568c3bfSXiaoliang Yang {
1360568c3bfSXiaoliang Yang 	int val;
1370568c3bfSXiaoliang Yang 
1380568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1390568c3bfSXiaoliang Yang 
1400568c3bfSXiaoliang Yang 	ocelot_mact_select(ocelot, mac, vid);
1410568c3bfSXiaoliang Yang 
1420568c3bfSXiaoliang Yang 	/* Issue a read command with MACACCESS_VALID=1. */
1430568c3bfSXiaoliang Yang 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
1440568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1450568c3bfSXiaoliang Yang 		     ANA_TABLES_MACACCESS);
1460568c3bfSXiaoliang Yang 
1470568c3bfSXiaoliang Yang 	if (ocelot_mact_wait_for_completion(ocelot)) {
1480568c3bfSXiaoliang Yang 		mutex_unlock(&ocelot->mact_lock);
1490568c3bfSXiaoliang Yang 		return -ETIMEDOUT;
1500568c3bfSXiaoliang Yang 	}
1510568c3bfSXiaoliang Yang 
1520568c3bfSXiaoliang Yang 	/* Read back the entry flags */
1530568c3bfSXiaoliang Yang 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1540568c3bfSXiaoliang Yang 
1550568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1560568c3bfSXiaoliang Yang 
1570568c3bfSXiaoliang Yang 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1580568c3bfSXiaoliang Yang 		return -ENOENT;
1590568c3bfSXiaoliang Yang 
1600568c3bfSXiaoliang Yang 	*dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val);
1610568c3bfSXiaoliang Yang 	*type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val);
1620568c3bfSXiaoliang Yang 
1630568c3bfSXiaoliang Yang 	return 0;
1640568c3bfSXiaoliang Yang }
1650568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_lookup);
1660568c3bfSXiaoliang Yang 
1670568c3bfSXiaoliang Yang int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx,
1680568c3bfSXiaoliang Yang 				 const unsigned char mac[ETH_ALEN],
1690568c3bfSXiaoliang Yang 				 unsigned int vid,
1700568c3bfSXiaoliang Yang 				 enum macaccess_entry_type type,
1710568c3bfSXiaoliang Yang 				 int sfid, int ssid)
1720568c3bfSXiaoliang Yang {
1730568c3bfSXiaoliang Yang 	int ret;
1740568c3bfSXiaoliang Yang 
1750568c3bfSXiaoliang Yang 	mutex_lock(&ocelot->mact_lock);
1760568c3bfSXiaoliang Yang 
1770568c3bfSXiaoliang Yang 	ocelot_write(ocelot,
1780568c3bfSXiaoliang Yang 		     (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) |
1790568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SFID(sfid) |
1800568c3bfSXiaoliang Yang 		     (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) |
1810568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA_SSID(ssid),
1820568c3bfSXiaoliang Yang 		     ANA_TABLES_STREAMDATA);
1830568c3bfSXiaoliang Yang 
1840568c3bfSXiaoliang Yang 	ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type);
1850568c3bfSXiaoliang Yang 
1860568c3bfSXiaoliang Yang 	mutex_unlock(&ocelot->mact_lock);
1870568c3bfSXiaoliang Yang 
1880568c3bfSXiaoliang Yang 	return ret;
1890568c3bfSXiaoliang Yang }
1900568c3bfSXiaoliang Yang EXPORT_SYMBOL(ocelot_mact_learn_streamdata);
1910568c3bfSXiaoliang Yang 
192a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
193a556c76aSAlexandre Belloni {
194a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
195a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
196a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
197a556c76aSAlexandre Belloni 	 */
198a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
199a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
200a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
201a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
202a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
203a556c76aSAlexandre Belloni 
2042468346cSVladimir Oltean 	/* Clear the MAC table. We are not concurrent with anyone, so
2052468346cSVladimir Oltean 	 * holding &ocelot->mact_lock is pointless.
2062468346cSVladimir Oltean 	 */
207a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
208a556c76aSAlexandre Belloni }
209a556c76aSAlexandre Belloni 
210f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
211b5962294SHoratiu Vultur {
212b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
213b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
214f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
21575944fdaSXiaoliang Yang 
21675944fdaSXiaoliang Yang 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
21775944fdaSXiaoliang Yang 			 ANA_PORT_VCAP_CFG, port);
2182f17c050SXiaoliang Yang 
2192f17c050SXiaoliang Yang 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
2202f17c050SXiaoliang Yang 		       REW_PORT_CFG_ES0_EN,
2212f17c050SXiaoliang Yang 		       REW_PORT_CFG, port);
222b5962294SHoratiu Vultur }
223b5962294SHoratiu Vultur 
224639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
225639c1b26SSteen Hegelund {
226639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
227639c1b26SSteen Hegelund }
228639c1b26SSteen Hegelund 
229a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
230a556c76aSAlexandre Belloni {
231639c1b26SSteen Hegelund 	u32 val;
232a556c76aSAlexandre Belloni 
233639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
234639c1b26SSteen Hegelund 		ocelot,
235639c1b26SSteen Hegelund 		val,
236639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
237639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
238639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
239a556c76aSAlexandre Belloni }
240a556c76aSAlexandre Belloni 
2417142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
2427142529fSAntoine Tenart {
2437142529fSAntoine Tenart 	/* Select the VID to configure */
2447142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
2457142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
2467142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
2477142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
2487142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
2497142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
2507142529fSAntoine Tenart 
2517142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
2527142529fSAntoine Tenart }
2537142529fSAntoine Tenart 
2540da1a1c4SVladimir Oltean static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
2550da1a1c4SVladimir Oltean {
2560da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
2570da1a1c4SVladimir Oltean 	int num_untagged = 0;
2580da1a1c4SVladimir Oltean 
2590da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
2600da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
2610da1a1c4SVladimir Oltean 			continue;
2620da1a1c4SVladimir Oltean 
2630da1a1c4SVladimir Oltean 		if (vlan->untagged & BIT(port))
2640da1a1c4SVladimir Oltean 			num_untagged++;
2650da1a1c4SVladimir Oltean 	}
2660da1a1c4SVladimir Oltean 
2670da1a1c4SVladimir Oltean 	return num_untagged;
2680da1a1c4SVladimir Oltean }
2690da1a1c4SVladimir Oltean 
2700da1a1c4SVladimir Oltean static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
2710da1a1c4SVladimir Oltean {
2720da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
2730da1a1c4SVladimir Oltean 	int num_tagged = 0;
2740da1a1c4SVladimir Oltean 
2750da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list) {
2760da1a1c4SVladimir Oltean 		if (!(vlan->portmask & BIT(port)))
2770da1a1c4SVladimir Oltean 			continue;
2780da1a1c4SVladimir Oltean 
2790da1a1c4SVladimir Oltean 		if (!(vlan->untagged & BIT(port)))
2800da1a1c4SVladimir Oltean 			num_tagged++;
2810da1a1c4SVladimir Oltean 	}
2820da1a1c4SVladimir Oltean 
2830da1a1c4SVladimir Oltean 	return num_tagged;
2840da1a1c4SVladimir Oltean }
2850da1a1c4SVladimir Oltean 
2860da1a1c4SVladimir Oltean /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
2870da1a1c4SVladimir Oltean  * _one_ egress-untagged VLAN (_the_ native VLAN)
2880da1a1c4SVladimir Oltean  */
2890da1a1c4SVladimir Oltean static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
2900da1a1c4SVladimir Oltean {
2910da1a1c4SVladimir Oltean 	return ocelot_port_num_tagged_vlans(ocelot, port) &&
2920da1a1c4SVladimir Oltean 	       ocelot_port_num_untagged_vlans(ocelot, port) == 1;
2930da1a1c4SVladimir Oltean }
2940da1a1c4SVladimir Oltean 
2950da1a1c4SVladimir Oltean static struct ocelot_bridge_vlan *
2960da1a1c4SVladimir Oltean ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
2970da1a1c4SVladimir Oltean {
2980da1a1c4SVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
2990da1a1c4SVladimir Oltean 
3000da1a1c4SVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
3010da1a1c4SVladimir Oltean 		if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
3020da1a1c4SVladimir Oltean 			return vlan;
3030da1a1c4SVladimir Oltean 
3040da1a1c4SVladimir Oltean 	return NULL;
3050da1a1c4SVladimir Oltean }
3060da1a1c4SVladimir Oltean 
3070da1a1c4SVladimir Oltean /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
3080da1a1c4SVladimir Oltean  * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
3090da1a1c4SVladimir Oltean  * state of the port.
3100da1a1c4SVladimir Oltean  */
3110da1a1c4SVladimir Oltean static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
31297bb69e1SVladimir Oltean {
31397bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
31462a22bcbSVladimir Oltean 	enum ocelot_port_tag_config tag_cfg;
3150da1a1c4SVladimir Oltean 	bool uses_native_vlan = false;
31697bb69e1SVladimir Oltean 
31787b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
3180da1a1c4SVladimir Oltean 		uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
3190da1a1c4SVladimir Oltean 
3200da1a1c4SVladimir Oltean 		if (uses_native_vlan)
32162a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_NATIVE;
3220da1a1c4SVladimir Oltean 		else if (ocelot_port_num_untagged_vlans(ocelot, port))
3230da1a1c4SVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_DISABLED;
32487b0f983SVladimir Oltean 		else
32562a22bcbSVladimir Oltean 			tag_cfg = OCELOT_PORT_TAG_TRUNK;
32687b0f983SVladimir Oltean 	} else {
32762a22bcbSVladimir Oltean 		tag_cfg = OCELOT_PORT_TAG_DISABLED;
32887b0f983SVladimir Oltean 	}
3290da1a1c4SVladimir Oltean 
33062a22bcbSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
33187b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
33287b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
3330da1a1c4SVladimir Oltean 
3340da1a1c4SVladimir Oltean 	if (uses_native_vlan) {
3350da1a1c4SVladimir Oltean 		struct ocelot_bridge_vlan *native_vlan;
3360da1a1c4SVladimir Oltean 
3370da1a1c4SVladimir Oltean 		/* Not having a native VLAN is impossible, because
3380da1a1c4SVladimir Oltean 		 * ocelot_port_num_untagged_vlans has returned 1.
3390da1a1c4SVladimir Oltean 		 * So there is no use in checking for NULL here.
3400da1a1c4SVladimir Oltean 		 */
3410da1a1c4SVladimir Oltean 		native_vlan = ocelot_port_find_native_vlan(ocelot, port);
3420da1a1c4SVladimir Oltean 
3430da1a1c4SVladimir Oltean 		ocelot_rmw_gix(ocelot,
3440da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
3450da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG_PORT_VID_M,
3460da1a1c4SVladimir Oltean 			       REW_PORT_VLAN_CFG, port);
3470da1a1c4SVladimir Oltean 	}
34897bb69e1SVladimir Oltean }
34997bb69e1SVladimir Oltean 
35075e5a554SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
351c3e58a75SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
352d4004422SVladimir Oltean 				 const struct ocelot_bridge_vlan *pvid_vlan)
35375e5a554SVladimir Oltean {
35475e5a554SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
355d4004422SVladimir Oltean 	u16 pvid = OCELOT_VLAN_UNAWARE_PVID;
356be0576feSVladimir Oltean 	u32 val = 0;
35775e5a554SVladimir Oltean 
358c3e58a75SVladimir Oltean 	ocelot_port->pvid_vlan = pvid_vlan;
35975e5a554SVladimir Oltean 
360d4004422SVladimir Oltean 	if (ocelot_port->vlan_aware && pvid_vlan)
361d4004422SVladimir Oltean 		pvid = pvid_vlan->vid;
36275e5a554SVladimir Oltean 
36375e5a554SVladimir Oltean 	ocelot_rmw_gix(ocelot,
364d4004422SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
36575e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
36675e5a554SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
367be0576feSVladimir Oltean 
368be0576feSVladimir Oltean 	/* If there's no pvid, we should drop not only untagged traffic (which
369be0576feSVladimir Oltean 	 * happens automatically), but also 802.1p traffic which gets
370be0576feSVladimir Oltean 	 * classified to VLAN 0, but that is always in our RX filter, so it
371be0576feSVladimir Oltean 	 * would get accepted were it not for this setting.
372be0576feSVladimir Oltean 	 */
373d4004422SVladimir Oltean 	if (!pvid_vlan && ocelot_port->vlan_aware)
374be0576feSVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
375be0576feSVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
376be0576feSVladimir Oltean 
377be0576feSVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
378be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
379be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
380be0576feSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
38175e5a554SVladimir Oltean }
38275e5a554SVladimir Oltean 
38390e0aa8dSVladimir Oltean static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
38490e0aa8dSVladimir Oltean 							  u16 vid)
385bbf6a2d9SVladimir Oltean {
38690e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan;
387bbf6a2d9SVladimir Oltean 
38890e0aa8dSVladimir Oltean 	list_for_each_entry(vlan, &ocelot->vlans, list)
38990e0aa8dSVladimir Oltean 		if (vlan->vid == vid)
39090e0aa8dSVladimir Oltean 			return vlan;
391bbf6a2d9SVladimir Oltean 
39290e0aa8dSVladimir Oltean 	return NULL;
393bbf6a2d9SVladimir Oltean }
394bbf6a2d9SVladimir Oltean 
3950da1a1c4SVladimir Oltean static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
3960da1a1c4SVladimir Oltean 				  bool untagged)
397bbf6a2d9SVladimir Oltean {
39890e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
39990e0aa8dSVladimir Oltean 	unsigned long portmask;
40090e0aa8dSVladimir Oltean 	int err;
40190e0aa8dSVladimir Oltean 
40290e0aa8dSVladimir Oltean 	if (vlan) {
40390e0aa8dSVladimir Oltean 		portmask = vlan->portmask | BIT(port);
40490e0aa8dSVladimir Oltean 
40590e0aa8dSVladimir Oltean 		err = ocelot_vlant_set_mask(ocelot, vid, portmask);
40690e0aa8dSVladimir Oltean 		if (err)
40790e0aa8dSVladimir Oltean 			return err;
40890e0aa8dSVladimir Oltean 
40990e0aa8dSVladimir Oltean 		vlan->portmask = portmask;
4100da1a1c4SVladimir Oltean 		/* Bridge VLANs can be overwritten with a different
4110da1a1c4SVladimir Oltean 		 * egress-tagging setting, so make sure to override an untagged
4120da1a1c4SVladimir Oltean 		 * with a tagged VID if that's going on.
4130da1a1c4SVladimir Oltean 		 */
4140da1a1c4SVladimir Oltean 		if (untagged)
4150da1a1c4SVladimir Oltean 			vlan->untagged |= BIT(port);
4160da1a1c4SVladimir Oltean 		else
4170da1a1c4SVladimir Oltean 			vlan->untagged &= ~BIT(port);
41890e0aa8dSVladimir Oltean 
41990e0aa8dSVladimir Oltean 		return 0;
42090e0aa8dSVladimir Oltean 	}
42190e0aa8dSVladimir Oltean 
42290e0aa8dSVladimir Oltean 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
42390e0aa8dSVladimir Oltean 	if (!vlan)
42490e0aa8dSVladimir Oltean 		return -ENOMEM;
42590e0aa8dSVladimir Oltean 
42690e0aa8dSVladimir Oltean 	portmask = BIT(port);
42790e0aa8dSVladimir Oltean 
42890e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
42990e0aa8dSVladimir Oltean 	if (err) {
43090e0aa8dSVladimir Oltean 		kfree(vlan);
43190e0aa8dSVladimir Oltean 		return err;
43290e0aa8dSVladimir Oltean 	}
43390e0aa8dSVladimir Oltean 
43490e0aa8dSVladimir Oltean 	vlan->vid = vid;
43590e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
4360da1a1c4SVladimir Oltean 	if (untagged)
4370da1a1c4SVladimir Oltean 		vlan->untagged = BIT(port);
43890e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&vlan->list);
43990e0aa8dSVladimir Oltean 	list_add_tail(&vlan->list, &ocelot->vlans);
44090e0aa8dSVladimir Oltean 
44190e0aa8dSVladimir Oltean 	return 0;
442bbf6a2d9SVladimir Oltean }
443bbf6a2d9SVladimir Oltean 
444bbf6a2d9SVladimir Oltean static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
445bbf6a2d9SVladimir Oltean {
44690e0aa8dSVladimir Oltean 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
44790e0aa8dSVladimir Oltean 	unsigned long portmask;
44890e0aa8dSVladimir Oltean 	int err;
44990e0aa8dSVladimir Oltean 
45090e0aa8dSVladimir Oltean 	if (!vlan)
45190e0aa8dSVladimir Oltean 		return 0;
45290e0aa8dSVladimir Oltean 
45390e0aa8dSVladimir Oltean 	portmask = vlan->portmask & ~BIT(port);
45490e0aa8dSVladimir Oltean 
45590e0aa8dSVladimir Oltean 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
45690e0aa8dSVladimir Oltean 	if (err)
45790e0aa8dSVladimir Oltean 		return err;
45890e0aa8dSVladimir Oltean 
45990e0aa8dSVladimir Oltean 	vlan->portmask = portmask;
46090e0aa8dSVladimir Oltean 	if (vlan->portmask)
46190e0aa8dSVladimir Oltean 		return 0;
46290e0aa8dSVladimir Oltean 
46390e0aa8dSVladimir Oltean 	list_del(&vlan->list);
46490e0aa8dSVladimir Oltean 	kfree(vlan);
46590e0aa8dSVladimir Oltean 
46690e0aa8dSVladimir Oltean 	return 0;
467bbf6a2d9SVladimir Oltean }
468bbf6a2d9SVladimir Oltean 
4692e554a7aSVladimir Oltean int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
4703b95d1b2SVladimir Oltean 			       bool vlan_aware, struct netlink_ext_ack *extack)
47187b0f983SVladimir Oltean {
47270edfae1SVladimir Oltean 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
473bae33f2bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
47470edfae1SVladimir Oltean 	struct ocelot_vcap_filter *filter;
475bae33f2bSVladimir Oltean 	u32 val;
47670edfae1SVladimir Oltean 
47770edfae1SVladimir Oltean 	list_for_each_entry(filter, &block->rules, list) {
47870edfae1SVladimir Oltean 		if (filter->ingress_port_mask & BIT(port) &&
47970edfae1SVladimir Oltean 		    filter->action.vid_replace_ena) {
4803b95d1b2SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
4813b95d1b2SVladimir Oltean 					   "Cannot change VLAN state with vlan modify rules active");
48270edfae1SVladimir Oltean 			return -EBUSY;
48370edfae1SVladimir Oltean 		}
48470edfae1SVladimir Oltean 	}
48570edfae1SVladimir Oltean 
48687b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
48787b0f983SVladimir Oltean 
48887b0f983SVladimir Oltean 	if (vlan_aware)
48987b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
49087b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
49187b0f983SVladimir Oltean 	else
49287b0f983SVladimir Oltean 		val = 0;
49387b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
49487b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
49587b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
49687b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
49787b0f983SVladimir Oltean 
498c3e58a75SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
4990da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
5002e554a7aSVladimir Oltean 
5012e554a7aSVladimir Oltean 	return 0;
50287b0f983SVladimir Oltean }
50387b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
50487b0f983SVladimir Oltean 
5052f0402feSVladimir Oltean int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
50601af940eSVladimir Oltean 			bool untagged, struct netlink_ext_ack *extack)
5072f0402feSVladimir Oltean {
5080da1a1c4SVladimir Oltean 	if (untagged) {
5090da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
5100da1a1c4SVladimir Oltean 		if (ocelot_port_uses_native_vlan(ocelot, port)) {
51101af940eSVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
5120da1a1c4SVladimir Oltean 					   "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
5132f0402feSVladimir Oltean 			return -EBUSY;
5142f0402feSVladimir Oltean 		}
5150da1a1c4SVladimir Oltean 	} else {
5160da1a1c4SVladimir Oltean 		/* We are adding an egress-tagged VLAN */
5170da1a1c4SVladimir Oltean 		if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
5180da1a1c4SVladimir Oltean 			NL_SET_ERR_MSG_MOD(extack,
5190da1a1c4SVladimir Oltean 					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
5200da1a1c4SVladimir Oltean 			return -EBUSY;
5210da1a1c4SVladimir Oltean 		}
5220da1a1c4SVladimir Oltean 	}
5232f0402feSVladimir Oltean 
5242f0402feSVladimir Oltean 	return 0;
5252f0402feSVladimir Oltean }
5262f0402feSVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_prepare);
5272f0402feSVladimir Oltean 
5285e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
5297142529fSAntoine Tenart 		    bool untagged)
5307142529fSAntoine Tenart {
531bbf6a2d9SVladimir Oltean 	int err;
5327142529fSAntoine Tenart 
5330da1a1c4SVladimir Oltean 	err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
534bbf6a2d9SVladimir Oltean 	if (err)
535bbf6a2d9SVladimir Oltean 		return err;
5367142529fSAntoine Tenart 
5377142529fSAntoine Tenart 	/* Default ingress vlan classification */
538d4004422SVladimir Oltean 	if (pvid)
539d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port,
540d4004422SVladimir Oltean 				     ocelot_bridge_vlan_find(ocelot, vid));
5417142529fSAntoine Tenart 
5427142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
5430da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
5447142529fSAntoine Tenart 
5457142529fSAntoine Tenart 	return 0;
5467142529fSAntoine Tenart }
5475e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
5487142529fSAntoine Tenart 
5495e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
5509855934cSVladimir Oltean {
5519855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
552bbf6a2d9SVladimir Oltean 	int err;
5537142529fSAntoine Tenart 
554bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
555bbf6a2d9SVladimir Oltean 	if (err)
556bbf6a2d9SVladimir Oltean 		return err;
5577142529fSAntoine Tenart 
558be0576feSVladimir Oltean 	/* Ingress */
559d4004422SVladimir Oltean 	if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
560d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, NULL);
561be0576feSVladimir Oltean 
5627142529fSAntoine Tenart 	/* Egress */
5630da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
5647142529fSAntoine Tenart 
5657142529fSAntoine Tenart 	return 0;
5667142529fSAntoine Tenart }
5675e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
5687142529fSAntoine Tenart 
569a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
570a556c76aSAlexandre Belloni {
571bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
5727142529fSAntoine Tenart 	u16 port, vid;
5737142529fSAntoine Tenart 
574a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
575a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
576a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
577a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
5787142529fSAntoine Tenart 
5797142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
580bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
58190e0aa8dSVladimir Oltean 		ocelot_vlant_set_mask(ocelot, vid, 0);
5827142529fSAntoine Tenart 
5837142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
5847142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
5857142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
5867142529fSAntoine Tenart 	 */
587bfbab310SVladimir Oltean 	ocelot_vlant_set_mask(ocelot, OCELOT_VLAN_UNAWARE_PVID, all_ports);
5887142529fSAntoine Tenart 
5897142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
5907142529fSAntoine Tenart 	 * default.
5917142529fSAntoine Tenart 	 */
592bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
5937142529fSAntoine Tenart 
5947142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
5957142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
5967142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
5977142529fSAntoine Tenart 	}
598a556c76aSAlexandre Belloni }
599a556c76aSAlexandre Belloni 
600eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
601eb4733d7SVladimir Oltean {
602eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
603eb4733d7SVladimir Oltean }
604eb4733d7SVladimir Oltean 
605e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
606eb4733d7SVladimir Oltean {
6071650bdb1SVladimir Oltean 	unsigned int pause_ena;
608eb4733d7SVladimir Oltean 	int err, val;
609eb4733d7SVladimir Oltean 
610eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
611eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
612eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
613eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
614eb4733d7SVladimir Oltean 
615eb4733d7SVladimir Oltean 	/* Disable flow control */
6161650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
617eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
618eb4733d7SVladimir Oltean 
619eb4733d7SVladimir Oltean 	/* Disable priority flow control */
620eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
621eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
622eb4733d7SVladimir Oltean 
623eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
624eb4733d7SVladimir Oltean 	 * at the port.
625eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
626eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
627eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
628eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
629eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
630eb4733d7SVladimir Oltean 	 */
631eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
632eb4733d7SVladimir Oltean 
633eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
634eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
635eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
636eb4733d7SVladimir Oltean 
637eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
638eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
639eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
640eb4733d7SVladimir Oltean 
641eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
642eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
643eb4733d7SVladimir Oltean 		       port);
644eb4733d7SVladimir Oltean 
645eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
646eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
647eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
648eb4733d7SVladimir Oltean 
649eb4733d7SVladimir Oltean 	/* Clear flushing again. */
650eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
651eb4733d7SVladimir Oltean 
6521650bdb1SVladimir Oltean 	/* Re-enable flow control */
6531650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
6541650bdb1SVladimir Oltean 
655eb4733d7SVladimir Oltean 	return err;
656eb4733d7SVladimir Oltean }
657eb4733d7SVladimir Oltean 
658e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
659e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
660e6e12df6SVladimir Oltean 				  phy_interface_t interface,
661e6e12df6SVladimir Oltean 				  unsigned long quirks)
662a556c76aSAlexandre Belloni {
66326f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
664e6e12df6SVladimir Oltean 	int err;
665a556c76aSAlexandre Belloni 
666e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
667e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
668e6e12df6SVladimir Oltean 
669e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
670e6e12df6SVladimir Oltean 
671e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
672e6e12df6SVladimir Oltean 	if (err)
673e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
674e6e12df6SVladimir Oltean 			port, err);
675e6e12df6SVladimir Oltean 
676e6e12df6SVladimir Oltean 	/* Put the port in reset. */
677e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
678e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
679e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
680e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
68174a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
682e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
68374a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
684e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
685e6e12df6SVladimir Oltean }
686e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
687e6e12df6SVladimir Oltean 
688e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
689e6e12df6SVladimir Oltean 				struct phy_device *phydev,
690e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
691e6e12df6SVladimir Oltean 				phy_interface_t interface,
692e6e12df6SVladimir Oltean 				int speed, int duplex,
693e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
694e6e12df6SVladimir Oltean 				unsigned long quirks)
695e6e12df6SVladimir Oltean {
696e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
697e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
698e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
699e6e12df6SVladimir Oltean 
700e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
701e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
702e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
703e6e12df6SVladimir Oltean 	 * (which is also its default value).
704e6e12df6SVladimir Oltean 	 */
705e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
706e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
707e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
708e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
709e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
710e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
711e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
712e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
713e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
714e6e12df6SVladimir Oltean 	} else {
715e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
716e6e12df6SVladimir Oltean 	}
717e6e12df6SVladimir Oltean 
718e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
719e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
720e6e12df6SVladimir Oltean 
721e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
722e6e12df6SVladimir Oltean 
723e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
724e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
725e6e12df6SVladimir Oltean 	 */
726e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
727e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
728e6e12df6SVladimir Oltean 
729e6e12df6SVladimir Oltean 	switch (speed) {
730a556c76aSAlexandre Belloni 	case SPEED_10:
731e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
732a556c76aSAlexandre Belloni 		break;
733a556c76aSAlexandre Belloni 	case SPEED_100:
734e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
735a556c76aSAlexandre Belloni 		break;
736a556c76aSAlexandre Belloni 	case SPEED_1000:
737a556c76aSAlexandre Belloni 	case SPEED_2500:
738e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
739a556c76aSAlexandre Belloni 		break;
740a556c76aSAlexandre Belloni 	default:
741e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
742e6e12df6SVladimir Oltean 			port, speed);
743a556c76aSAlexandre Belloni 		return;
744a556c76aSAlexandre Belloni 	}
745a556c76aSAlexandre Belloni 
746e6e12df6SVladimir Oltean 	/* Handle RX pause in all cases, with 2500base-X this is used for rate
747e6e12df6SVladimir Oltean 	 * adaptation.
748e6e12df6SVladimir Oltean 	 */
749e6e12df6SVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
750a556c76aSAlexandre Belloni 
751e6e12df6SVladimir Oltean 	if (tx_pause)
752e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
753e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
754e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
755e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
756a556c76aSAlexandre Belloni 
757e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
758e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
759e6e12df6SVladimir Oltean 	 */
760e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
761a556c76aSAlexandre Belloni 
762e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
7631ba8f656SVladimir Oltean 
764e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, tx_pause);
7651ba8f656SVladimir Oltean 
766e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
767e6e12df6SVladimir Oltean 	 * enable MAC module
768e6e12df6SVladimir Oltean 	 */
769004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
770a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
771a556c76aSAlexandre Belloni 
772a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
773886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
774886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
775a556c76aSAlexandre Belloni }
776e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
777889b8950SVladimir Oltean 
77852849bcfSVladimir Oltean static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
779e2f9a8feSVladimir Oltean 					struct sk_buff *clone)
780400928bfSYangbo Lu {
781e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
78252849bcfSVladimir Oltean 	unsigned long flags;
783400928bfSYangbo Lu 
78452849bcfSVladimir Oltean 	spin_lock_irqsave(&ocelot->ts_id_lock, flags);
78552849bcfSVladimir Oltean 
78652849bcfSVladimir Oltean 	if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
78752849bcfSVladimir Oltean 	    ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
78852849bcfSVladimir Oltean 		spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
78952849bcfSVladimir Oltean 		return -EBUSY;
79052849bcfSVladimir Oltean 	}
7916565243cSVladimir Oltean 
792e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
793c4b364ceSYangbo Lu 	/* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
794c4b364ceSYangbo Lu 	OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
79552849bcfSVladimir Oltean 
796c57fe003SVladimir Oltean 	ocelot_port->ts_id++;
797c57fe003SVladimir Oltean 	if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
798c57fe003SVladimir Oltean 		ocelot_port->ts_id = 0;
79952849bcfSVladimir Oltean 
80052849bcfSVladimir Oltean 	ocelot_port->ptp_skbs_in_flight++;
80152849bcfSVladimir Oltean 	ocelot->ptp_skbs_in_flight++;
80252849bcfSVladimir Oltean 
803e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
8046565243cSVladimir Oltean 
80552849bcfSVladimir Oltean 	spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
80652849bcfSVladimir Oltean 
80752849bcfSVladimir Oltean 	return 0;
808400928bfSYangbo Lu }
809682eaad9SYangbo Lu 
810fba01283SVladimir Oltean static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
811fba01283SVladimir Oltean 				       unsigned int ptp_class)
81239e5308bSYangbo Lu {
81339e5308bSYangbo Lu 	struct ptp_header *hdr;
81439e5308bSYangbo Lu 	u8 msgtype, twostep;
81539e5308bSYangbo Lu 
81639e5308bSYangbo Lu 	hdr = ptp_parse_header(skb, ptp_class);
81739e5308bSYangbo Lu 	if (!hdr)
81839e5308bSYangbo Lu 		return false;
81939e5308bSYangbo Lu 
82039e5308bSYangbo Lu 	msgtype = ptp_get_msgtype(hdr, ptp_class);
82139e5308bSYangbo Lu 	twostep = hdr->flag_field[0] & 0x2;
82239e5308bSYangbo Lu 
82339e5308bSYangbo Lu 	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
82439e5308bSYangbo Lu 		return true;
82539e5308bSYangbo Lu 
82639e5308bSYangbo Lu 	return false;
82739e5308bSYangbo Lu }
82839e5308bSYangbo Lu 
829682eaad9SYangbo Lu int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
830682eaad9SYangbo Lu 				 struct sk_buff *skb,
831682eaad9SYangbo Lu 				 struct sk_buff **clone)
832682eaad9SYangbo Lu {
833682eaad9SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
834682eaad9SYangbo Lu 	u8 ptp_cmd = ocelot_port->ptp_cmd;
835fba01283SVladimir Oltean 	unsigned int ptp_class;
83652849bcfSVladimir Oltean 	int err;
837682eaad9SYangbo Lu 
838fba01283SVladimir Oltean 	/* Don't do anything if PTP timestamping not enabled */
839fba01283SVladimir Oltean 	if (!ptp_cmd)
840fba01283SVladimir Oltean 		return 0;
841fba01283SVladimir Oltean 
842fba01283SVladimir Oltean 	ptp_class = ptp_classify_raw(skb);
843fba01283SVladimir Oltean 	if (ptp_class == PTP_CLASS_NONE)
844fba01283SVladimir Oltean 		return -EINVAL;
845682eaad9SYangbo Lu 
84639e5308bSYangbo Lu 	/* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
84739e5308bSYangbo Lu 	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
848fba01283SVladimir Oltean 		if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
84939e5308bSYangbo Lu 			OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
85039e5308bSYangbo Lu 			return 0;
85139e5308bSYangbo Lu 		}
85239e5308bSYangbo Lu 
85339e5308bSYangbo Lu 		/* Fall back to two-step timestamping */
85439e5308bSYangbo Lu 		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
85539e5308bSYangbo Lu 	}
85639e5308bSYangbo Lu 
857682eaad9SYangbo Lu 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
858682eaad9SYangbo Lu 		*clone = skb_clone_sk(skb);
859682eaad9SYangbo Lu 		if (!(*clone))
860682eaad9SYangbo Lu 			return -ENOMEM;
861682eaad9SYangbo Lu 
86252849bcfSVladimir Oltean 		err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
86352849bcfSVladimir Oltean 		if (err)
86452849bcfSVladimir Oltean 			return err;
86552849bcfSVladimir Oltean 
86639e5308bSYangbo Lu 		OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
867ebb4c6a9SVladimir Oltean 		OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
868682eaad9SYangbo Lu 	}
869682eaad9SYangbo Lu 
870682eaad9SYangbo Lu 	return 0;
871682eaad9SYangbo Lu }
872682eaad9SYangbo Lu EXPORT_SYMBOL(ocelot_port_txtstamp_request);
873400928bfSYangbo Lu 
874e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
875e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
8764e3b0468SAntoine Tenart {
8774e3b0468SAntoine Tenart 	unsigned long flags;
8784e3b0468SAntoine Tenart 	u32 val;
8794e3b0468SAntoine Tenart 
8804e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
8814e3b0468SAntoine Tenart 
8824e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
8834e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
8844e3b0468SAntoine Tenart 
8854e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
8864e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
8874e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
8884e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
8894e3b0468SAntoine Tenart 
8904e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
8914e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
8924e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
8934e3b0468SAntoine Tenart 
8944e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
8954e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
8964e3b0468SAntoine Tenart 		ts->tv_sec--;
8974e3b0468SAntoine Tenart 
8984e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
8994e3b0468SAntoine Tenart }
900e23a7b3eSYangbo Lu 
901ebb4c6a9SVladimir Oltean static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
902ebb4c6a9SVladimir Oltean {
903ebb4c6a9SVladimir Oltean 	struct ptp_header *hdr;
904ebb4c6a9SVladimir Oltean 
905ebb4c6a9SVladimir Oltean 	hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
906ebb4c6a9SVladimir Oltean 	if (WARN_ON(!hdr))
907ebb4c6a9SVladimir Oltean 		return false;
908ebb4c6a9SVladimir Oltean 
909ebb4c6a9SVladimir Oltean 	return seqid == ntohs(hdr->sequence_id);
910ebb4c6a9SVladimir Oltean }
911ebb4c6a9SVladimir Oltean 
912e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
913e23a7b3eSYangbo Lu {
914e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
915e23a7b3eSYangbo Lu 
916e23a7b3eSYangbo Lu 	while (budget--) {
917b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
918e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
919ebb4c6a9SVladimir Oltean 		u32 val, id, seqid, txport;
920e23a7b3eSYangbo Lu 		struct ocelot_port *port;
921e23a7b3eSYangbo Lu 		struct timespec64 ts;
922b049da13SYangbo Lu 		unsigned long flags;
923e23a7b3eSYangbo Lu 
924e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
925e23a7b3eSYangbo Lu 
926e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
927e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
928e23a7b3eSYangbo Lu 			break;
929e23a7b3eSYangbo Lu 
930e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
931e23a7b3eSYangbo Lu 
932e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
933e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
934e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
935ebb4c6a9SVladimir Oltean 		seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
936e23a7b3eSYangbo Lu 
937e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
938e23a7b3eSYangbo Lu 
93952849bcfSVladimir Oltean 		spin_lock(&ocelot->ts_id_lock);
94052849bcfSVladimir Oltean 		port->ptp_skbs_in_flight--;
94152849bcfSVladimir Oltean 		ocelot->ptp_skbs_in_flight--;
94252849bcfSVladimir Oltean 		spin_unlock(&ocelot->ts_id_lock);
94352849bcfSVladimir Oltean 
94452849bcfSVladimir Oltean 		/* Retrieve its associated skb */
945ebb4c6a9SVladimir Oltean try_again:
946b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
947b049da13SYangbo Lu 
948b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
949c4b364ceSYangbo Lu 			if (OCELOT_SKB_CB(skb)->ts_id != id)
950e23a7b3eSYangbo Lu 				continue;
951b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
952b049da13SYangbo Lu 			skb_match = skb;
953fc62c094SYangbo Lu 			break;
954e23a7b3eSYangbo Lu 		}
955e23a7b3eSYangbo Lu 
956b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
957b049da13SYangbo Lu 
9589fde506eSVladimir Oltean 		if (WARN_ON(!skb_match))
9599fde506eSVladimir Oltean 			continue;
9609fde506eSVladimir Oltean 
961ebb4c6a9SVladimir Oltean 		if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
962ebb4c6a9SVladimir Oltean 			dev_err_ratelimited(ocelot->dev,
963ebb4c6a9SVladimir Oltean 					    "port %d received stale TX timestamp for seqid %d, discarding\n",
964ebb4c6a9SVladimir Oltean 					    txport, seqid);
965ebb4c6a9SVladimir Oltean 			dev_kfree_skb_any(skb);
966ebb4c6a9SVladimir Oltean 			goto try_again;
967ebb4c6a9SVladimir Oltean 		}
968ebb4c6a9SVladimir Oltean 
9695fd82200Slaurent brando 		/* Get the h/w timestamp */
9705fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
971e23a7b3eSYangbo Lu 
972e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
973e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
974e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
975e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
9765fd82200Slaurent brando 
9775fd82200Slaurent brando 		/* Next ts */
9785fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
979e23a7b3eSYangbo Lu 	}
980e23a7b3eSYangbo Lu }
981e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
9824e3b0468SAntoine Tenart 
983924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
984924ee317SVladimir Oltean 				u32 *rval)
985924ee317SVladimir Oltean {
986924ee317SVladimir Oltean 	u32 bytes_valid, val;
987924ee317SVladimir Oltean 
988924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
989924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
990924ee317SVladimir Oltean 		if (ifh)
991924ee317SVladimir Oltean 			return -EIO;
992924ee317SVladimir Oltean 
993924ee317SVladimir Oltean 		do {
994924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
995924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
996924ee317SVladimir Oltean 	}
997924ee317SVladimir Oltean 
998924ee317SVladimir Oltean 	switch (val) {
999924ee317SVladimir Oltean 	case XTR_ABORT:
1000924ee317SVladimir Oltean 		return -EIO;
1001924ee317SVladimir Oltean 	case XTR_EOF_0:
1002924ee317SVladimir Oltean 	case XTR_EOF_1:
1003924ee317SVladimir Oltean 	case XTR_EOF_2:
1004924ee317SVladimir Oltean 	case XTR_EOF_3:
1005924ee317SVladimir Oltean 	case XTR_PRUNED:
1006924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
1007924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1008924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
1009924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1010924ee317SVladimir Oltean 		else
1011924ee317SVladimir Oltean 			*rval = val;
1012924ee317SVladimir Oltean 
1013924ee317SVladimir Oltean 		return bytes_valid;
1014924ee317SVladimir Oltean 	case XTR_ESCAPE:
1015924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1016924ee317SVladimir Oltean 
1017924ee317SVladimir Oltean 		return 4;
1018924ee317SVladimir Oltean 	default:
1019924ee317SVladimir Oltean 		*rval = val;
1020924ee317SVladimir Oltean 
1021924ee317SVladimir Oltean 		return 4;
1022924ee317SVladimir Oltean 	}
1023924ee317SVladimir Oltean }
1024924ee317SVladimir Oltean 
1025924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
1026924ee317SVladimir Oltean {
1027924ee317SVladimir Oltean 	int i, err = 0;
1028924ee317SVladimir Oltean 
1029924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
1030924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
1031924ee317SVladimir Oltean 		if (err != 4)
1032924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
1033924ee317SVladimir Oltean 	}
1034924ee317SVladimir Oltean 
1035924ee317SVladimir Oltean 	return 0;
1036924ee317SVladimir Oltean }
1037924ee317SVladimir Oltean 
1038924ee317SVladimir Oltean int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
1039924ee317SVladimir Oltean {
1040924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
10412ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
1042924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
1043924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
1044924ee317SVladimir Oltean 	struct net_device *dev;
1045924ee317SVladimir Oltean 	struct timespec64 ts;
1046924ee317SVladimir Oltean 	struct sk_buff *skb;
1047924ee317SVladimir Oltean 	int sz, buf_len;
1048924ee317SVladimir Oltean 	u32 val, *buf;
1049924ee317SVladimir Oltean 	int err;
1050924ee317SVladimir Oltean 
1051924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1052924ee317SVladimir Oltean 	if (err)
1053924ee317SVladimir Oltean 		return err;
1054924ee317SVladimir Oltean 
1055924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
1056924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
1057924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1058924ee317SVladimir Oltean 
1059924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1060924ee317SVladimir Oltean 		return -EINVAL;
1061924ee317SVladimir Oltean 
1062924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1063924ee317SVladimir Oltean 	if (!dev)
1064924ee317SVladimir Oltean 		return -EINVAL;
1065924ee317SVladimir Oltean 
1066924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
1067924ee317SVladimir Oltean 	if (unlikely(!skb)) {
1068924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
1069924ee317SVladimir Oltean 		return -ENOMEM;
1070924ee317SVladimir Oltean 	}
1071924ee317SVladimir Oltean 
1072924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
1073924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
1074924ee317SVladimir Oltean 
1075924ee317SVladimir Oltean 	len = 0;
1076924ee317SVladimir Oltean 	do {
1077924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1078924ee317SVladimir Oltean 		if (sz < 0) {
1079924ee317SVladimir Oltean 			err = sz;
1080924ee317SVladimir Oltean 			goto out_free_skb;
1081924ee317SVladimir Oltean 		}
1082924ee317SVladimir Oltean 		*buf++ = val;
1083924ee317SVladimir Oltean 		len += sz;
1084924ee317SVladimir Oltean 	} while (len < buf_len);
1085924ee317SVladimir Oltean 
1086924ee317SVladimir Oltean 	/* Read the FCS */
1087924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1088924ee317SVladimir Oltean 	if (sz < 0) {
1089924ee317SVladimir Oltean 		err = sz;
1090924ee317SVladimir Oltean 		goto out_free_skb;
1091924ee317SVladimir Oltean 	}
1092924ee317SVladimir Oltean 
1093924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1094924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1095924ee317SVladimir Oltean 
1096924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1097924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1098924ee317SVladimir Oltean 		*buf = val;
1099924ee317SVladimir Oltean 	}
1100924ee317SVladimir Oltean 
1101924ee317SVladimir Oltean 	if (ocelot->ptp) {
1102924ee317SVladimir Oltean 		ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1103924ee317SVladimir Oltean 
1104924ee317SVladimir Oltean 		tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1105924ee317SVladimir Oltean 		if ((tod_in_ns & 0xffffffff) < timestamp)
1106924ee317SVladimir Oltean 			full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1107924ee317SVladimir Oltean 					timestamp;
1108924ee317SVladimir Oltean 		else
1109924ee317SVladimir Oltean 			full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1110924ee317SVladimir Oltean 					timestamp;
1111924ee317SVladimir Oltean 
1112924ee317SVladimir Oltean 		shhwtstamps = skb_hwtstamps(skb);
1113924ee317SVladimir Oltean 		memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1114924ee317SVladimir Oltean 		shhwtstamps->hwtstamp = full_ts_in_ns;
1115924ee317SVladimir Oltean 	}
1116924ee317SVladimir Oltean 
1117924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1118924ee317SVladimir Oltean 	 * has already been forwarded.
1119924ee317SVladimir Oltean 	 */
1120df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1121924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1122924ee317SVladimir Oltean 
1123924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1124d8ea7ff3SHoratiu Vultur 
1125924ee317SVladimir Oltean 	*nskb = skb;
1126924ee317SVladimir Oltean 
1127924ee317SVladimir Oltean 	return 0;
1128924ee317SVladimir Oltean 
1129924ee317SVladimir Oltean out_free_skb:
1130924ee317SVladimir Oltean 	kfree_skb(skb);
1131924ee317SVladimir Oltean 	return err;
1132924ee317SVladimir Oltean }
1133924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1134924ee317SVladimir Oltean 
1135137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1136137ffbc4SVladimir Oltean {
1137137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1138137ffbc4SVladimir Oltean 
1139137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1140137ffbc4SVladimir Oltean 		return false;
1141137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1142137ffbc4SVladimir Oltean 		return false;
1143137ffbc4SVladimir Oltean 
1144137ffbc4SVladimir Oltean 	return true;
1145137ffbc4SVladimir Oltean }
1146137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1147137ffbc4SVladimir Oltean 
1148137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1149137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1150137ffbc4SVladimir Oltean {
115140d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1152137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1153137ffbc4SVladimir Oltean 
1154137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1155137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1156137ffbc4SVladimir Oltean 
115740d3f295SVladimir Oltean 	ocelot_ifh_set_bypass(ifh, 1);
11581f778d50SVladimir Oltean 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
115940d3f295SVladimir Oltean 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1160e8c07229SVladimir Oltean 	ocelot_ifh_set_vlan_tci(ifh, skb_vlan_tag_get(skb));
116140d3f295SVladimir Oltean 	ocelot_ifh_set_rew_op(ifh, rew_op);
1162137ffbc4SVladimir Oltean 
1163137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
116440d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1165137ffbc4SVladimir Oltean 
1166137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1167137ffbc4SVladimir Oltean 	last = skb->len % 4;
1168137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1169137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1170137ffbc4SVladimir Oltean 
1171137ffbc4SVladimir Oltean 	/* Add padding */
1172137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1173137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1174137ffbc4SVladimir Oltean 		i++;
1175137ffbc4SVladimir Oltean 	}
1176137ffbc4SVladimir Oltean 
1177137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1178137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1179137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1180137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1181137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1182137ffbc4SVladimir Oltean 
1183137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1184137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1185137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1186137ffbc4SVladimir Oltean 
1187137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1188137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1189137ffbc4SVladimir Oltean }
1190137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1191137ffbc4SVladimir Oltean 
11920a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
11930a6f17c6SVladimir Oltean {
11940a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
11950a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
11960a6f17c6SVladimir Oltean }
11970a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
11980a6f17c6SVladimir Oltean 
11995e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
120087b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
1201a556c76aSAlexandre Belloni {
1202471beb11SVladimir Oltean 	int pgid = port;
1203471beb11SVladimir Oltean 
1204471beb11SVladimir Oltean 	if (port == ocelot->npi)
1205471beb11SVladimir Oltean 		pgid = PGID_CPU;
1206a556c76aSAlexandre Belloni 
1207471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
1208a556c76aSAlexandre Belloni }
12095e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1210a556c76aSAlexandre Belloni 
12115e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
1212531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
1213531ee1a6SVladimir Oltean {
1214531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1215531ee1a6SVladimir Oltean }
12165e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1217531ee1a6SVladimir Oltean 
12189c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1219531ee1a6SVladimir Oltean 			    bool is_static, void *data)
1220a556c76aSAlexandre Belloni {
1221531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
1222a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1223a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
1224a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
1225a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
1226a556c76aSAlexandre Belloni 
1227a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
1228a556c76aSAlexandre Belloni 		goto skip;
1229a556c76aSAlexandre Belloni 
1230a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1231a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
1232a556c76aSAlexandre Belloni 	if (!nlh)
1233a556c76aSAlexandre Belloni 		return -EMSGSIZE;
1234a556c76aSAlexandre Belloni 
1235a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
1236a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
1237a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
1238a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
1239a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
1240a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
1241a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
1242531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
1243a556c76aSAlexandre Belloni 
1244531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
1245a556c76aSAlexandre Belloni 		goto nla_put_failure;
1246a556c76aSAlexandre Belloni 
1247531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
1248a556c76aSAlexandre Belloni 		goto nla_put_failure;
1249a556c76aSAlexandre Belloni 
1250a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
1251a556c76aSAlexandre Belloni 
1252a556c76aSAlexandre Belloni skip:
1253a556c76aSAlexandre Belloni 	dump->idx++;
1254a556c76aSAlexandre Belloni 	return 0;
1255a556c76aSAlexandre Belloni 
1256a556c76aSAlexandre Belloni nla_put_failure:
1257a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
1258a556c76aSAlexandre Belloni 	return -EMSGSIZE;
1259a556c76aSAlexandre Belloni }
12609c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
1261a556c76aSAlexandre Belloni 
12622468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
1263531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1264a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1265a556c76aSAlexandre Belloni {
1266a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1267531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1268a556c76aSAlexandre Belloni 
1269a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1270a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1271a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1272a556c76aSAlexandre Belloni 
1273a556c76aSAlexandre Belloni 	/* Issue a read command */
1274a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1275a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1276a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1277a556c76aSAlexandre Belloni 
1278a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1279a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1280a556c76aSAlexandre Belloni 
1281a556c76aSAlexandre Belloni 	/* Read the entry flags */
1282a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1283a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1284a556c76aSAlexandre Belloni 		return -EINVAL;
1285a556c76aSAlexandre Belloni 
1286a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1287a556c76aSAlexandre Belloni 	 * do not report it.
1288a556c76aSAlexandre Belloni 	 */
1289a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1290531ee1a6SVladimir Oltean 	if (dst != port)
1291a556c76aSAlexandre Belloni 		return -EINVAL;
1292a556c76aSAlexandre Belloni 
1293a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1294a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1295a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1296a556c76aSAlexandre Belloni 
1297a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1298a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1299a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1300a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1301a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1302a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1303a556c76aSAlexandre Belloni 
1304a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1305a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1306a556c76aSAlexandre Belloni 
1307a556c76aSAlexandre Belloni 	return 0;
1308a556c76aSAlexandre Belloni }
1309a556c76aSAlexandre Belloni 
13105e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1311531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1312a556c76aSAlexandre Belloni {
13132468346cSVladimir Oltean 	int err = 0;
1314531ee1a6SVladimir Oltean 	int i, j;
1315a556c76aSAlexandre Belloni 
13162468346cSVladimir Oltean 	/* We could take the lock just around ocelot_mact_read, but doing so
13172468346cSVladimir Oltean 	 * thousands of times in a row seems rather pointless and inefficient.
13182468346cSVladimir Oltean 	 */
13192468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
13202468346cSVladimir Oltean 
132121ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
132221ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1323a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1324531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1325531ee1a6SVladimir Oltean 			bool is_static;
1326531ee1a6SVladimir Oltean 
13272468346cSVladimir Oltean 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1328a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1329a556c76aSAlexandre Belloni 			 * skip it.
1330a556c76aSAlexandre Belloni 			 */
13312468346cSVladimir Oltean 			if (err == -EINVAL)
1332a556c76aSAlexandre Belloni 				continue;
13332468346cSVladimir Oltean 			else if (err)
13342468346cSVladimir Oltean 				break;
1335a556c76aSAlexandre Belloni 
1336531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1337531ee1a6SVladimir Oltean 
13382468346cSVladimir Oltean 			err = cb(entry.mac, entry.vid, is_static, data);
13392468346cSVladimir Oltean 			if (err)
13402468346cSVladimir Oltean 				break;
1341a556c76aSAlexandre Belloni 		}
1342a556c76aSAlexandre Belloni 	}
1343a556c76aSAlexandre Belloni 
13442468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
13452468346cSVladimir Oltean 
13462468346cSVladimir Oltean 	return err;
1347531ee1a6SVladimir Oltean }
13485e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1349531ee1a6SVladimir Oltean 
1350f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
13514e3b0468SAntoine Tenart {
13524e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
13534e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
13544e3b0468SAntoine Tenart }
1355f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
13564e3b0468SAntoine Tenart 
1357f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
13584e3b0468SAntoine Tenart {
1359306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
13604e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
13614e3b0468SAntoine Tenart 
13624e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
13634e3b0468SAntoine Tenart 		return -EFAULT;
13644e3b0468SAntoine Tenart 
13654e3b0468SAntoine Tenart 	/* reserved for future extensions */
13664e3b0468SAntoine Tenart 	if (cfg.flags)
13674e3b0468SAntoine Tenart 		return -EINVAL;
13684e3b0468SAntoine Tenart 
13694e3b0468SAntoine Tenart 	/* Tx type sanity check */
13704e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
13714e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1372306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
13734e3b0468SAntoine Tenart 		break;
13744e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
13754e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
13764e3b0468SAntoine Tenart 		 * need to update the origin time.
13774e3b0468SAntoine Tenart 		 */
1378306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
13794e3b0468SAntoine Tenart 		break;
13804e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1381306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
13824e3b0468SAntoine Tenart 		break;
13834e3b0468SAntoine Tenart 	default:
13844e3b0468SAntoine Tenart 		return -ERANGE;
13854e3b0468SAntoine Tenart 	}
13864e3b0468SAntoine Tenart 
13874e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
13884e3b0468SAntoine Tenart 
13894e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
13904e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
13914e3b0468SAntoine Tenart 		break;
13924e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
13934e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
13944e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
13954e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
13964e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
13974e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
13984e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
13994e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
14004e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
14014e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
14024e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
14034e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
14044e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
14054e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
14064e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
14074e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
14084e3b0468SAntoine Tenart 		break;
14094e3b0468SAntoine Tenart 	default:
14104e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
14114e3b0468SAntoine Tenart 		return -ERANGE;
14124e3b0468SAntoine Tenart 	}
14134e3b0468SAntoine Tenart 
14144e3b0468SAntoine Tenart 	/* Commit back the result & save it */
14154e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
14164e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
14174e3b0468SAntoine Tenart 
14184e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
14194e3b0468SAntoine Tenart }
1420f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
14214e3b0468SAntoine Tenart 
14225e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1423a556c76aSAlexandre Belloni {
1424a556c76aSAlexandre Belloni 	int i;
1425a556c76aSAlexandre Belloni 
1426a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1427a556c76aSAlexandre Belloni 		return;
1428a556c76aSAlexandre Belloni 
1429a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1430a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1431a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1432a556c76aSAlexandre Belloni }
14335e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1434a556c76aSAlexandre Belloni 
14351e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1436a556c76aSAlexandre Belloni {
1437a556c76aSAlexandre Belloni 	int i, j;
1438a556c76aSAlexandre Belloni 
1439a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1440a556c76aSAlexandre Belloni 
1441a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1442a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1443a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1444a556c76aSAlexandre Belloni 
1445a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1446a556c76aSAlexandre Belloni 			u32 val;
1447a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1448a556c76aSAlexandre Belloni 
1449a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1450a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1451a556c76aSAlexandre Belloni 
1452a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1453a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1454a556c76aSAlexandre Belloni 
1455a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1456a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1457a556c76aSAlexandre Belloni 		}
1458a556c76aSAlexandre Belloni 	}
1459a556c76aSAlexandre Belloni 
14601e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
14611e1caa97SClaudiu Manoil }
14621e1caa97SClaudiu Manoil 
14631e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
14641e1caa97SClaudiu Manoil {
14651e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
14661e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
14671e1caa97SClaudiu Manoil 					     stats_work);
14681e1caa97SClaudiu Manoil 
14691e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
14701e1caa97SClaudiu Manoil 
1471a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1472a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1473a556c76aSAlexandre Belloni }
1474a556c76aSAlexandre Belloni 
14755e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1476a556c76aSAlexandre Belloni {
1477a556c76aSAlexandre Belloni 	int i;
1478a556c76aSAlexandre Belloni 
1479a556c76aSAlexandre Belloni 	/* check and update now */
14801e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1481a556c76aSAlexandre Belloni 
1482a556c76aSAlexandre Belloni 	/* Copy all counters */
1483a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1484004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1485a556c76aSAlexandre Belloni }
14865e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1487a556c76aSAlexandre Belloni 
14885e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1489c7282d38SVladimir Oltean {
1490a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1491a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1492c7282d38SVladimir Oltean 
1493a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1494a556c76aSAlexandre Belloni }
14955e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1496a556c76aSAlexandre Belloni 
14975e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1498c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1499c7282d38SVladimir Oltean {
15004e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
15014e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1502d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1503d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1504d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1505d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1506d2b09a8eSYangbo Lu 		return 0;
1507d2b09a8eSYangbo Lu 	}
15084e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
15094e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
15104e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
15114e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
15124e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
15134e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
15144e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
15154e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
15164e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
15174e3b0468SAntoine Tenart 
15184e3b0468SAntoine Tenart 	return 0;
15194e3b0468SAntoine Tenart }
15205e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
15214e3b0468SAntoine Tenart 
152223ca3b72SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond,
152323ca3b72SVladimir Oltean 				bool only_active_ports)
1524b80af659SVladimir Oltean {
1525b80af659SVladimir Oltean 	u32 mask = 0;
1526b80af659SVladimir Oltean 	int port;
1527b80af659SVladimir Oltean 
1528b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1529b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1530b80af659SVladimir Oltean 
1531b80af659SVladimir Oltean 		if (!ocelot_port)
1532b80af659SVladimir Oltean 			continue;
1533b80af659SVladimir Oltean 
153423ca3b72SVladimir Oltean 		if (ocelot_port->bond == bond) {
153523ca3b72SVladimir Oltean 			if (only_active_ports && !ocelot_port->lag_tx_active)
153623ca3b72SVladimir Oltean 				continue;
153723ca3b72SVladimir Oltean 
1538b80af659SVladimir Oltean 			mask |= BIT(port);
1539b80af659SVladimir Oltean 		}
154023ca3b72SVladimir Oltean 	}
1541b80af659SVladimir Oltean 
1542b80af659SVladimir Oltean 	return mask;
1543b80af659SVladimir Oltean }
1544b80af659SVladimir Oltean 
1545acc64f52SVladimir Oltean static u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port,
1546df291e54SVladimir Oltean 				      struct net_device *bridge)
1547df291e54SVladimir Oltean {
1548acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1549df291e54SVladimir Oltean 	u32 mask = 0;
1550df291e54SVladimir Oltean 	int port;
1551df291e54SVladimir Oltean 
1552acc64f52SVladimir Oltean 	if (!ocelot_port || ocelot_port->bridge != bridge ||
1553acc64f52SVladimir Oltean 	    ocelot_port->stp_state != BR_STATE_FORWARDING)
1554acc64f52SVladimir Oltean 		return 0;
1555acc64f52SVladimir Oltean 
1556df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1557acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1558df291e54SVladimir Oltean 
1559df291e54SVladimir Oltean 		if (!ocelot_port)
1560df291e54SVladimir Oltean 			continue;
1561df291e54SVladimir Oltean 
1562df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1563df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1564df291e54SVladimir Oltean 			mask |= BIT(port);
1565df291e54SVladimir Oltean 	}
1566df291e54SVladimir Oltean 
1567df291e54SVladimir Oltean 	return mask;
1568df291e54SVladimir Oltean }
1569df291e54SVladimir Oltean 
1570e21268efSVladimir Oltean static u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
15719b521250SVladimir Oltean {
1572e21268efSVladimir Oltean 	u32 mask = 0;
15739b521250SVladimir Oltean 	int port;
15749b521250SVladimir Oltean 
1575e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1576e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1577e21268efSVladimir Oltean 
1578e21268efSVladimir Oltean 		if (!ocelot_port)
1579e21268efSVladimir Oltean 			continue;
1580e21268efSVladimir Oltean 
1581e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
1582e21268efSVladimir Oltean 			mask |= BIT(port);
1583e21268efSVladimir Oltean 	}
1584e21268efSVladimir Oltean 
1585e21268efSVladimir Oltean 	return mask;
1586e21268efSVladimir Oltean }
1587e21268efSVladimir Oltean 
1588e21268efSVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot)
1589e21268efSVladimir Oltean {
1590e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
1591e21268efSVladimir Oltean 	int port;
1592e21268efSVladimir Oltean 
1593e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
1594e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
1595e21268efSVladimir Oltean 	 * those are bridged or standalone.
1596e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
1597e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
1598e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
1599e21268efSVladimir Oltean 	 */
1600e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1601e21268efSVladimir Oltean 
16029b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
16039b521250SVladimir Oltean 	 * a source for the other ports.
16049b521250SVladimir Oltean 	 */
16059b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1606e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1607e21268efSVladimir Oltean 		unsigned long mask;
1608e21268efSVladimir Oltean 
1609e21268efSVladimir Oltean 		if (!ocelot_port) {
1610e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
1611e21268efSVladimir Oltean 			mask = 0;
1612e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1613e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
1614e21268efSVladimir Oltean 			 * forward packets to all other ports except for
1615e21268efSVladimir Oltean 			 * themselves
1616e21268efSVladimir Oltean 			 */
1617e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1618e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
1619df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
1620df291e54SVladimir Oltean 			struct net_device *bridge = ocelot_port->bridge;
1621528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
16229b521250SVladimir Oltean 
1623acc64f52SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port, bridge);
1624c1930148SVladimir Oltean 			mask |= cpu_fwd_mask;
1625df291e54SVladimir Oltean 			mask &= ~BIT(port);
162623ca3b72SVladimir Oltean 			if (bond) {
162723ca3b72SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond,
162823ca3b72SVladimir Oltean 							      false);
162923ca3b72SVladimir Oltean 			}
16309b521250SVladimir Oltean 		} else {
1631e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
1632e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
1633e21268efSVladimir Oltean 			 * module otherwise.
1634e21268efSVladimir Oltean 			 */
1635e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
1636e21268efSVladimir Oltean 		}
1637e21268efSVladimir Oltean 
1638e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
16399b521250SVladimir Oltean 	}
16409b521250SVladimir Oltean }
1641e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
16429b521250SVladimir Oltean 
16435e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1644a556c76aSAlexandre Belloni {
1645421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1646df291e54SVladimir Oltean 	u32 learn_ena = 0;
1647a556c76aSAlexandre Belloni 
1648df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
1649a556c76aSAlexandre Belloni 
1650df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1651df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
1652df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1653a556c76aSAlexandre Belloni 
1654df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1655df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
1656a556c76aSAlexandre Belloni 
16579b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1658a556c76aSAlexandre Belloni }
16595e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1660a556c76aSAlexandre Belloni 
16615e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
16624bda1415SVladimir Oltean {
1663c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1664c0d7eccbSVladimir Oltean 
1665c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1666c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1667c0d7eccbSVladimir Oltean 	 */
1668c0d7eccbSVladimir Oltean 	if (!age_period)
1669c0d7eccbSVladimir Oltean 		age_period = 1;
1670c0d7eccbSVladimir Oltean 
1671c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1672a556c76aSAlexandre Belloni }
16735e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1674a556c76aSAlexandre Belloni 
1675a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1676a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1677a556c76aSAlexandre Belloni 						     u16 vid)
1678a556c76aSAlexandre Belloni {
1679a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1680a556c76aSAlexandre Belloni 
1681a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1682a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1683a556c76aSAlexandre Belloni 			return mc;
1684a556c76aSAlexandre Belloni 	}
1685a556c76aSAlexandre Belloni 
1686a556c76aSAlexandre Belloni 	return NULL;
1687a556c76aSAlexandre Belloni }
1688a556c76aSAlexandre Belloni 
16899403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
16909403c158SVladimir Oltean {
16919403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
16929403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
16939403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
16949403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
16957c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
16969403c158SVladimir Oltean }
16979403c158SVladimir Oltean 
1698e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1699e5d1f896SVladimir Oltean 					     unsigned long ports)
1700e5d1f896SVladimir Oltean {
1701e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1702e5d1f896SVladimir Oltean 
1703e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1704e5d1f896SVladimir Oltean 	if (!pgid)
1705e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
1706e5d1f896SVladimir Oltean 
1707e5d1f896SVladimir Oltean 	pgid->ports = ports;
1708e5d1f896SVladimir Oltean 	pgid->index = index;
1709e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
1710e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
1711e5d1f896SVladimir Oltean 
1712e5d1f896SVladimir Oltean 	return pgid;
1713e5d1f896SVladimir Oltean }
1714e5d1f896SVladimir Oltean 
1715e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1716e5d1f896SVladimir Oltean {
1717e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
1718e5d1f896SVladimir Oltean 		return;
1719e5d1f896SVladimir Oltean 
1720e5d1f896SVladimir Oltean 	list_del(&pgid->list);
1721e5d1f896SVladimir Oltean 	kfree(pgid);
1722e5d1f896SVladimir Oltean }
1723e5d1f896SVladimir Oltean 
1724e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1725bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
17269403c158SVladimir Oltean {
1727e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1728e5d1f896SVladimir Oltean 	int index;
17299403c158SVladimir Oltean 
17309403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
17319403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
17329403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
17339403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
17349403c158SVladimir Oltean 	 */
1735bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1736bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
1737e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
17389403c158SVladimir Oltean 
1739e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1740e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
1741e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1742e5d1f896SVladimir Oltean 		 */
1743e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
1744e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
1745e5d1f896SVladimir Oltean 			return pgid;
1746e5d1f896SVladimir Oltean 		}
1747e5d1f896SVladimir Oltean 	}
1748e5d1f896SVladimir Oltean 
1749e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
1750e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
17519403c158SVladimir Oltean 		bool used = false;
17529403c158SVladimir Oltean 
1753e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1754e5d1f896SVladimir Oltean 			if (pgid->index == index) {
17559403c158SVladimir Oltean 				used = true;
17569403c158SVladimir Oltean 				break;
17579403c158SVladimir Oltean 			}
17589403c158SVladimir Oltean 		}
17599403c158SVladimir Oltean 
17609403c158SVladimir Oltean 		if (!used)
1761e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
17629403c158SVladimir Oltean 	}
17639403c158SVladimir Oltean 
1764e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
17659403c158SVladimir Oltean }
17669403c158SVladimir Oltean 
17679403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1768bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
17699403c158SVladimir Oltean {
1770ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
17719403c158SVladimir Oltean 
1772bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
17739403c158SVladimir Oltean 		addr[0] = 0;
17749403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
17759403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
1776bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
17779403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
17789403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
17799403c158SVladimir Oltean 	}
17809403c158SVladimir Oltean }
17819403c158SVladimir Oltean 
1782209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1783209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
1784a556c76aSAlexandre Belloni {
1785a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1786004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1787e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1788a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1789a556c76aSAlexandre Belloni 
1790471beb11SVladimir Oltean 	if (port == ocelot->npi)
1791471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1792471beb11SVladimir Oltean 
1793a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1794a556c76aSAlexandre Belloni 	if (!mc) {
1795728e69aeSVladimir Oltean 		/* New entry */
1796bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1797bb8d53fdSVladimir Oltean 		if (!mc)
1798bb8d53fdSVladimir Oltean 			return -ENOMEM;
1799bb8d53fdSVladimir Oltean 
1800bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1801bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
1802bb8d53fdSVladimir Oltean 		mc->vid = vid;
1803bb8d53fdSVladimir Oltean 
1804a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1805728e69aeSVladimir Oltean 	} else {
1806e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
1807e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
1808e5d1f896SVladimir Oltean 		 */
1809e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
1810bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
1811a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1812a556c76aSAlexandre Belloni 	}
1813a556c76aSAlexandre Belloni 
1814004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1815e5d1f896SVladimir Oltean 
1816e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1817e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
1818e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
1819e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
1820e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
1821e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
1822e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1823e5d1f896SVladimir Oltean 	}
1824e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1825e5d1f896SVladimir Oltean 
1826bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1827a556c76aSAlexandre Belloni 
1828e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1829e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1830e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1831e5d1f896SVladimir Oltean 				 pgid->index);
1832e5d1f896SVladimir Oltean 
1833e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1834bb8d53fdSVladimir Oltean 				 mc->entry_type);
1835a556c76aSAlexandre Belloni }
1836209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
1837a556c76aSAlexandre Belloni 
1838209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1839a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
1840a556c76aSAlexandre Belloni {
1841a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1842004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1843e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
1844a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1845a556c76aSAlexandre Belloni 
1846471beb11SVladimir Oltean 	if (port == ocelot->npi)
1847471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
1848471beb11SVladimir Oltean 
1849a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1850a556c76aSAlexandre Belloni 	if (!mc)
1851a556c76aSAlexandre Belloni 		return -ENOENT;
1852a556c76aSAlexandre Belloni 
1853bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1854a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1855a556c76aSAlexandre Belloni 
1856e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
1857004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1858a556c76aSAlexandre Belloni 	if (!mc->ports) {
1859a556c76aSAlexandre Belloni 		list_del(&mc->list);
1860a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1861a556c76aSAlexandre Belloni 		return 0;
1862a556c76aSAlexandre Belloni 	}
1863a556c76aSAlexandre Belloni 
1864e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
1865e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1866e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
1867e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
1868e5d1f896SVladimir Oltean 	mc->pgid = pgid;
1869e5d1f896SVladimir Oltean 
1870bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
1871a556c76aSAlexandre Belloni 
1872e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1873e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
1874e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1875e5d1f896SVladimir Oltean 				 pgid->index);
1876e5d1f896SVladimir Oltean 
1877e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1878bb8d53fdSVladimir Oltean 				 mc->entry_type);
1879a556c76aSAlexandre Belloni }
1880209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
1881a556c76aSAlexandre Belloni 
1882e4bd44e8SVladimir Oltean void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1883a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1884a556c76aSAlexandre Belloni {
1885df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1886a556c76aSAlexandre Belloni 
1887df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
1888a556c76aSAlexandre Belloni 
1889e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1890a556c76aSAlexandre Belloni }
18915e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1892a556c76aSAlexandre Belloni 
1893e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1894a556c76aSAlexandre Belloni 			      struct net_device *bridge)
1895a556c76aSAlexandre Belloni {
1896df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
18972e554a7aSVladimir Oltean 
1898df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
18997142529fSAntoine Tenart 
1900d4004422SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, NULL);
19010da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
1902e4bd44e8SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
1903a556c76aSAlexandre Belloni }
19045e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1905a556c76aSAlexandre Belloni 
1906dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1907dc96ee37SAlexandre Belloni {
1908528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
1909dc96ee37SAlexandre Belloni 	int i, port, lag;
1910dc96ee37SAlexandre Belloni 
1911dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
191296b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
1913dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1914dc96ee37SAlexandre Belloni 
191596b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
1916dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1917dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1918dc96ee37SAlexandre Belloni 
1919528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
1920528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
1921528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
1922528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
1923528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
1924528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
1925528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
1926528d3f19SVladimir Oltean 	 */
1927528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1928528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1929528d3f19SVladimir Oltean 
1930528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
1931528d3f19SVladimir Oltean 			continue;
1932528d3f19SVladimir Oltean 
1933528d3f19SVladimir Oltean 		visited &= ~BIT(port);
1934528d3f19SVladimir Oltean 	}
1935528d3f19SVladimir Oltean 
1936528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
1937dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1938528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
193923ca3b72SVladimir Oltean 		int num_active_ports = 0;
1940dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1941dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1942dc96ee37SAlexandre Belloni 
1943528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
1944dc96ee37SAlexandre Belloni 			continue;
1945dc96ee37SAlexandre Belloni 
194623ca3b72SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond, true);
1947528d3f19SVladimir Oltean 
1948dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1949dc96ee37SAlexandre Belloni 			// Destination mask
1950dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1951dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
195223ca3b72SVladimir Oltean 			aggr_idx[num_active_ports++] = port;
1953dc96ee37SAlexandre Belloni 		}
1954dc96ee37SAlexandre Belloni 
195596b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
1956dc96ee37SAlexandre Belloni 			u32 ac;
1957dc96ee37SAlexandre Belloni 
1958dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1959dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
196023ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
196123ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
196223ca3b72SVladimir Oltean 			 */
196323ca3b72SVladimir Oltean 			if (num_active_ports)
196423ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
1965dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1966dc96ee37SAlexandre Belloni 		}
1967528d3f19SVladimir Oltean 
1968528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
1969528d3f19SVladimir Oltean 		 * the same config again.
1970528d3f19SVladimir Oltean 		 */
1971528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
1972528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
1973528d3f19SVladimir Oltean 
1974528d3f19SVladimir Oltean 			if (!ocelot_port)
1975528d3f19SVladimir Oltean 				continue;
1976528d3f19SVladimir Oltean 
1977528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
1978528d3f19SVladimir Oltean 				visited |= BIT(port);
1979528d3f19SVladimir Oltean 		}
1980dc96ee37SAlexandre Belloni 	}
1981dc96ee37SAlexandre Belloni }
1982dc96ee37SAlexandre Belloni 
19832527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
19842527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
19852527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
19862527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
19872527f2e8SVladimir Oltean  */
19882527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
1989dc96ee37SAlexandre Belloni {
19902527f2e8SVladimir Oltean 	int port;
1991dc96ee37SAlexandre Belloni 
19922527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
19932527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
19942527f2e8SVladimir Oltean 		struct net_device *bond;
1995dc96ee37SAlexandre Belloni 
19962527f2e8SVladimir Oltean 		if (!ocelot_port)
19972527f2e8SVladimir Oltean 			continue;
1998dc96ee37SAlexandre Belloni 
19992527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
20002527f2e8SVladimir Oltean 		if (bond) {
200123ca3b72SVladimir Oltean 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond,
200223ca3b72SVladimir Oltean 							     false));
20032527f2e8SVladimir Oltean 
20042527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
2005dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
20062527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
20072527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
20082527f2e8SVladimir Oltean 		} else {
20092527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
20102527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
20112527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
20122527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
20132527f2e8SVladimir Oltean 		}
2014dc96ee37SAlexandre Belloni 	}
2015dc96ee37SAlexandre Belloni }
2016dc96ee37SAlexandre Belloni 
20179c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2018583cbbe3SVladimir Oltean 			 struct net_device *bond,
2019583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
2020dc96ee37SAlexandre Belloni {
2021583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
2022583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
2023583cbbe3SVladimir Oltean 
2024b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
2025dc96ee37SAlexandre Belloni 
20262527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
20279b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
2028dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2029dc96ee37SAlexandre Belloni 
2030dc96ee37SAlexandre Belloni 	return 0;
2031dc96ee37SAlexandre Belloni }
20329c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
2033dc96ee37SAlexandre Belloni 
20349c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2035dc96ee37SAlexandre Belloni 			   struct net_device *bond)
2036dc96ee37SAlexandre Belloni {
2037b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
2038b80af659SVladimir Oltean 
20392527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
20409b521250SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot);
2041dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2042dc96ee37SAlexandre Belloni }
20439c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
20440e332c85SPetr Machata 
204523ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
204623ca3b72SVladimir Oltean {
204723ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
204823ca3b72SVladimir Oltean 
204923ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
205023ca3b72SVladimir Oltean 
205123ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
205223ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
205323ca3b72SVladimir Oltean }
205423ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
205523ca3b72SVladimir Oltean 
2056a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2057a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
20580b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
20590b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
20600b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2061a8015dedSVladimir Oltean  */
20620b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
206331350d7fSVladimir Oltean {
206431350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2065a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2066e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
2067601e984fSVladimir Oltean 	int atop, atop_tot;
206831350d7fSVladimir Oltean 
20690b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
20700b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
20710b912fc9SVladimir Oltean 
2072cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
20730b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2074cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
20750b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
20760b912fc9SVladimir Oltean 	}
20770b912fc9SVladimir Oltean 
2078a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2079fa914e9cSVladimir Oltean 
2080e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
2081e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2082e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2083541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2084541132f0SMaxim Kochetkov 			    pause_start);
2085541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2086541132f0SMaxim Kochetkov 			    pause_stop);
2087fa914e9cSVladimir Oltean 
2088601e984fSVladimir Oltean 	/* Tail dropping watermarks */
2089f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2090a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2091601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2092601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2093601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2094fa914e9cSVladimir Oltean }
20950b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
20960b912fc9SVladimir Oltean 
20970b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
20980b912fc9SVladimir Oltean {
20990b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
21000b912fc9SVladimir Oltean 
21010b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
21020b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
21030b912fc9SVladimir Oltean 
2104cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
21050b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2106cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
21070b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
21080b912fc9SVladimir Oltean 	}
21090b912fc9SVladimir Oltean 
21100b912fc9SVladimir Oltean 	return max_mtu;
21110b912fc9SVladimir Oltean }
21120b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2113fa914e9cSVladimir Oltean 
2114421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2115421741eaSVladimir Oltean 				     bool enabled)
2116421741eaSVladimir Oltean {
2117421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2118421741eaSVladimir Oltean 	u32 val = 0;
2119421741eaSVladimir Oltean 
2120421741eaSVladimir Oltean 	if (enabled)
2121421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2122421741eaSVladimir Oltean 
2123421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2124421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2125421741eaSVladimir Oltean 
2126421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2127421741eaSVladimir Oltean }
2128421741eaSVladimir Oltean 
2129421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2130421741eaSVladimir Oltean 					bool enabled)
2131421741eaSVladimir Oltean {
2132421741eaSVladimir Oltean 	u32 val = 0;
2133421741eaSVladimir Oltean 
2134421741eaSVladimir Oltean 	if (enabled)
2135421741eaSVladimir Oltean 		val = BIT(port);
2136421741eaSVladimir Oltean 
2137421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2138421741eaSVladimir Oltean }
2139421741eaSVladimir Oltean 
2140421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2141421741eaSVladimir Oltean 					bool enabled)
2142421741eaSVladimir Oltean {
2143421741eaSVladimir Oltean 	u32 val = 0;
2144421741eaSVladimir Oltean 
2145421741eaSVladimir Oltean 	if (enabled)
2146421741eaSVladimir Oltean 		val = BIT(port);
2147421741eaSVladimir Oltean 
2148421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
2149421741eaSVladimir Oltean }
2150421741eaSVladimir Oltean 
2151421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2152421741eaSVladimir Oltean 					bool enabled)
2153421741eaSVladimir Oltean {
2154421741eaSVladimir Oltean 	u32 val = 0;
2155421741eaSVladimir Oltean 
2156421741eaSVladimir Oltean 	if (enabled)
2157421741eaSVladimir Oltean 		val = BIT(port);
2158421741eaSVladimir Oltean 
2159421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2160421741eaSVladimir Oltean }
2161421741eaSVladimir Oltean 
2162421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2163421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2164421741eaSVladimir Oltean {
2165421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2166421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2167421741eaSVladimir Oltean 		return -EINVAL;
2168421741eaSVladimir Oltean 
2169421741eaSVladimir Oltean 	return 0;
2170421741eaSVladimir Oltean }
2171421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2172421741eaSVladimir Oltean 
2173421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2174421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2175421741eaSVladimir Oltean {
2176421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2177421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2178421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2179421741eaSVladimir Oltean 
2180421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2181421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2182421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2183421741eaSVladimir Oltean 
2184421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2185421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2186421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2187421741eaSVladimir Oltean 
2188421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2189421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2190421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2191421741eaSVladimir Oltean }
2192421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2193421741eaSVladimir Oltean 
21945e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2195fa914e9cSVladimir Oltean {
2196fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2197fa914e9cSVladimir Oltean 
2198b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
219931350d7fSVladimir Oltean 
220031350d7fSVladimir Oltean 	/* Basic L2 initialization */
220131350d7fSVladimir Oltean 
22025bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
22035bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
22045bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
22055bc9d2e6SVladimir Oltean 	 */
22065bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
22075bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
22085bc9d2e6SVladimir Oltean 
22095bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
22105bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
22115bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
22125bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
22135bc9d2e6SVladimir Oltean 	mdelay(1);
22145bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
22155bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
22165bc9d2e6SVladimir Oltean 
22175bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2218a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
22195bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
22205bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2221a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
22225bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
22235bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
22245bc9d2e6SVladimir Oltean 
22255bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
22265bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
22275bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
22285bc9d2e6SVladimir Oltean 
2229e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2230541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2231e8e6e73dSVladimir Oltean 
223231350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
223331350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
223431350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
223531350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
223631350d7fSVladimir Oltean 
223731350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
223831350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
223931350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
224031350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
224131350d7fSVladimir Oltean 
2242421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2243421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2244421741eaSVladimir Oltean 
224546efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
224646efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
224746efe4efSVladimir Oltean 	 * automatic.
224846efe4efSVladimir Oltean 	 */
224946efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
225046efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
225146efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
225246efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
225346efe4efSVladimir Oltean 
225431350d7fSVladimir Oltean 	/* Enable vcap lookups */
225531350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
225631350d7fSVladimir Oltean }
22575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
225831350d7fSVladimir Oltean 
22592d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
22602d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
22612d44b097SVladimir Oltean  * NPI mode is used).
226269df578cSVladimir Oltean  */
22632d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
226421468199SVladimir Oltean {
226569df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
226669df578cSVladimir Oltean 
226769df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
226821468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
226969df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
227069df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
227169df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
227269df578cSVladimir Oltean 	 */
227321468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
227421468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
227521468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
227621468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
227721468199SVladimir Oltean 
227869df578cSVladimir Oltean 	/* Enable CPU port module */
2279886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
228069df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2281886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2282cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2283886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2284cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
228521468199SVladimir Oltean 
228621468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
2287bfbab310SVladimir Oltean 	ocelot_write_gix(ocelot,
2288bfbab310SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_VLAN_UNAWARE_PVID) |
228921468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
229021468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
229121468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
229221468199SVladimir Oltean }
229321468199SVladimir Oltean 
2294f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2295f6fe01d6SVladimir Oltean {
2296f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2297f6fe01d6SVladimir Oltean 
2298f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2299f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2300f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2301f6fe01d6SVladimir Oltean 	 */
2302f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2303f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2304f6fe01d6SVladimir Oltean 
2305f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2306f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2307f6fe01d6SVladimir Oltean }
2308f6fe01d6SVladimir Oltean 
2309a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2310a556c76aSAlexandre Belloni {
2311a556c76aSAlexandre Belloni 	char queue_name[32];
231221468199SVladimir Oltean 	int i, ret;
231321468199SVladimir Oltean 	u32 port;
2314a556c76aSAlexandre Belloni 
23153a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
23163a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
23173a77b593SVladimir Oltean 		if (ret) {
23183a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
23193a77b593SVladimir Oltean 			return ret;
23203a77b593SVladimir Oltean 		}
23213a77b593SVladimir Oltean 	}
23223a77b593SVladimir Oltean 
2323a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2324a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2325a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2326a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2327a556c76aSAlexandre Belloni 		return -ENOMEM;
2328a556c76aSAlexandre Belloni 
2329a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
23304e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
23312468346cSVladimir Oltean 	mutex_init(&ocelot->mact_lock);
23324e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
233352849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
2334a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2335a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2336a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2337a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2338a556c76aSAlexandre Belloni 		return -ENOMEM;
2339a556c76aSAlexandre Belloni 
2340ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2341ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
2342ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
2343ca0b272bSVladimir Oltean 		return -ENOMEM;
2344ca0b272bSVladimir Oltean 	}
2345ca0b272bSVladimir Oltean 
23462b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2347e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
234890e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
2349f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2350a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2351a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2352aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
23532d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2354a556c76aSAlexandre Belloni 
2355*23e2c506SXiaoliang Yang 	if (ocelot->ops->psfp_init)
2356*23e2c506SXiaoliang Yang 		ocelot->ops->psfp_init(ocelot);
2357*23e2c506SXiaoliang Yang 
2358a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2359a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2360a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2361a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2362a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2363a556c76aSAlexandre Belloni 	}
2364a556c76aSAlexandre Belloni 
2365a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2366a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2367a556c76aSAlexandre Belloni 
2368a556c76aSAlexandre Belloni 	/* Aggregation mode */
2369a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2370a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2371a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2372f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2373f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2374f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2375f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2376a556c76aSAlexandre Belloni 
2377a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2378a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2379a556c76aSAlexandre Belloni 	 */
2380a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2381a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2382a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2383a556c76aSAlexandre Belloni 
2384a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2385a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2386a556c76aSAlexandre Belloni 
2387a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2388a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2389a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2390a556c76aSAlexandre Belloni 
2391a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2392edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2393a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2394b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2395a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2396edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2397a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2398a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2399a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2400a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2401a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2402a556c76aSAlexandre Belloni 
2403a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2404a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2405a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2406a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2407a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2408a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2409a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2410a556c76aSAlexandre Belloni 				 port);
2411a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2412a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2413a556c76aSAlexandre Belloni 	}
2414a556c76aSAlexandre Belloni 
241596b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2416a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2417a556c76aSAlexandre Belloni 
2418a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2419a556c76aSAlexandre Belloni 	}
2420ebb1bb40SHoratiu Vultur 
2421ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2422ebb1bb40SHoratiu Vultur 
2423b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2424b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2425b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2426a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2427b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2428b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2429b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2430a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2431a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2432a556c76aSAlexandre Belloni 
2433a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2434a556c76aSAlexandre Belloni 	 * registers endianness.
2435a556c76aSAlexandre Belloni 	 */
2436a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2437a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2438a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2439a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2440a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2441a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2442a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2443a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2444a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2445a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2446a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2447a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2448a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2449a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2450a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2451a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2452a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2453a556c76aSAlexandre Belloni 
24541e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2455a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2456a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
24574e3b0468SAntoine Tenart 
2458a556c76aSAlexandre Belloni 	return 0;
2459a556c76aSAlexandre Belloni }
2460a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2461a556c76aSAlexandre Belloni 
2462a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2463a556c76aSAlexandre Belloni {
2464c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2465a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2466ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
2467a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2468a556c76aSAlexandre Belloni }
2469a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2470a556c76aSAlexandre Belloni 
2471e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
2472e5fb512dSVladimir Oltean {
2473e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2474e5fb512dSVladimir Oltean 
2475e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
2476e5fb512dSVladimir Oltean }
2477e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
2478e5fb512dSVladimir Oltean 
2479a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2480