xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 961d8b699070070fb3a9639af61641a52c8e49ef)
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];
552ef576405SVladimir Oltean 	bool del_pvid = false;
553bbf6a2d9SVladimir Oltean 	int err;
5547142529fSAntoine Tenart 
555ef576405SVladimir Oltean 	if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
556ef576405SVladimir Oltean 		del_pvid = true;
557ef576405SVladimir Oltean 
558bbf6a2d9SVladimir Oltean 	err = ocelot_vlan_member_del(ocelot, port, vid);
559bbf6a2d9SVladimir Oltean 	if (err)
560bbf6a2d9SVladimir Oltean 		return err;
5617142529fSAntoine Tenart 
562be0576feSVladimir Oltean 	/* Ingress */
563ef576405SVladimir Oltean 	if (del_pvid)
564d4004422SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, NULL);
565be0576feSVladimir Oltean 
5667142529fSAntoine Tenart 	/* Egress */
5670da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
5687142529fSAntoine Tenart 
5697142529fSAntoine Tenart 	return 0;
5707142529fSAntoine Tenart }
5715e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
5727142529fSAntoine Tenart 
573a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
574a556c76aSAlexandre Belloni {
575bbf6a2d9SVladimir Oltean 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
5767142529fSAntoine Tenart 	u16 port, vid;
5777142529fSAntoine Tenart 
578a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
579a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
580a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
581a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
5827142529fSAntoine Tenart 
5837142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
584bbf6a2d9SVladimir Oltean 	for (vid = 1; vid < VLAN_N_VID; vid++)
58590e0aa8dSVladimir Oltean 		ocelot_vlant_set_mask(ocelot, vid, 0);
5867142529fSAntoine Tenart 
5877142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
5887142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
5897142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
5907142529fSAntoine Tenart 	 */
591bfbab310SVladimir Oltean 	ocelot_vlant_set_mask(ocelot, OCELOT_VLAN_UNAWARE_PVID, all_ports);
5927142529fSAntoine Tenart 
5937142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
5947142529fSAntoine Tenart 	 * default.
5957142529fSAntoine Tenart 	 */
596bbf6a2d9SVladimir Oltean 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
5977142529fSAntoine Tenart 
5987142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
5997142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
6007142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
6017142529fSAntoine Tenart 	}
602a556c76aSAlexandre Belloni }
603a556c76aSAlexandre Belloni 
604eb4733d7SVladimir Oltean static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
605eb4733d7SVladimir Oltean {
606eb4733d7SVladimir Oltean 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
607eb4733d7SVladimir Oltean }
608eb4733d7SVladimir Oltean 
609e6e12df6SVladimir Oltean static int ocelot_port_flush(struct ocelot *ocelot, int port)
610eb4733d7SVladimir Oltean {
6111650bdb1SVladimir Oltean 	unsigned int pause_ena;
612eb4733d7SVladimir Oltean 	int err, val;
613eb4733d7SVladimir Oltean 
614eb4733d7SVladimir Oltean 	/* Disable dequeuing from the egress queues */
615eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
616eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE_DEQUEUE_DIS,
617eb4733d7SVladimir Oltean 		       QSYS_PORT_MODE, port);
618eb4733d7SVladimir Oltean 
619eb4733d7SVladimir Oltean 	/* Disable flow control */
6201650bdb1SVladimir Oltean 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
621eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
622eb4733d7SVladimir Oltean 
623eb4733d7SVladimir Oltean 	/* Disable priority flow control */
624eb4733d7SVladimir Oltean 	ocelot_fields_write(ocelot, port,
625eb4733d7SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
626eb4733d7SVladimir Oltean 
627eb4733d7SVladimir Oltean 	/* Wait at least the time it takes to receive a frame of maximum length
628eb4733d7SVladimir Oltean 	 * at the port.
629eb4733d7SVladimir Oltean 	 * Worst-case delays for 10 kilobyte jumbo frames are:
630eb4733d7SVladimir Oltean 	 * 8 ms on a 10M port
631eb4733d7SVladimir Oltean 	 * 800 μs on a 100M port
632eb4733d7SVladimir Oltean 	 * 80 μs on a 1G port
633eb4733d7SVladimir Oltean 	 * 32 μs on a 2.5G port
634eb4733d7SVladimir Oltean 	 */
635eb4733d7SVladimir Oltean 	usleep_range(8000, 10000);
636eb4733d7SVladimir Oltean 
637eb4733d7SVladimir Oltean 	/* Disable half duplex backpressure. */
638eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
639eb4733d7SVladimir Oltean 		       SYS_FRONT_PORT_MODE, port);
640eb4733d7SVladimir Oltean 
641eb4733d7SVladimir Oltean 	/* Flush the queues associated with the port. */
642eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
643eb4733d7SVladimir Oltean 		       REW_PORT_CFG, port);
644eb4733d7SVladimir Oltean 
645eb4733d7SVladimir Oltean 	/* Enable dequeuing from the egress queues. */
646eb4733d7SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
647eb4733d7SVladimir Oltean 		       port);
648eb4733d7SVladimir Oltean 
649eb4733d7SVladimir Oltean 	/* Wait until flushing is complete. */
650eb4733d7SVladimir Oltean 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
651eb4733d7SVladimir Oltean 				100, 2000000, false, ocelot, port);
652eb4733d7SVladimir Oltean 
653eb4733d7SVladimir Oltean 	/* Clear flushing again. */
654eb4733d7SVladimir Oltean 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
655eb4733d7SVladimir Oltean 
6561650bdb1SVladimir Oltean 	/* Re-enable flow control */
6571650bdb1SVladimir Oltean 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
6581650bdb1SVladimir Oltean 
659eb4733d7SVladimir Oltean 	return err;
660eb4733d7SVladimir Oltean }
661eb4733d7SVladimir Oltean 
662e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
663e6e12df6SVladimir Oltean 				  unsigned int link_an_mode,
664e6e12df6SVladimir Oltean 				  phy_interface_t interface,
665e6e12df6SVladimir Oltean 				  unsigned long quirks)
666a556c76aSAlexandre Belloni {
66726f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
668e6e12df6SVladimir Oltean 	int err;
669a556c76aSAlexandre Belloni 
6708abe1970SVladimir Oltean 	ocelot_port->speed = SPEED_UNKNOWN;
6718abe1970SVladimir Oltean 
672e6e12df6SVladimir Oltean 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
673e6e12df6SVladimir Oltean 			 DEV_MAC_ENA_CFG);
674e6e12df6SVladimir Oltean 
6758abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
6768abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
6778abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
6788abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
6798abe1970SVladimir Oltean 	}
6808abe1970SVladimir Oltean 
681e6e12df6SVladimir Oltean 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
682e6e12df6SVladimir Oltean 
683e6e12df6SVladimir Oltean 	err = ocelot_port_flush(ocelot, port);
684e6e12df6SVladimir Oltean 	if (err)
685e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
686e6e12df6SVladimir Oltean 			port, err);
687e6e12df6SVladimir Oltean 
688e6e12df6SVladimir Oltean 	/* Put the port in reset. */
689e6e12df6SVladimir Oltean 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
690e6e12df6SVladimir Oltean 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
691e6e12df6SVladimir Oltean 		ocelot_port_rmwl(ocelot_port,
692e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
69374a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
694e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG_MAC_TX_RST |
69574a3bc42SWan Jiabing 				 DEV_CLOCK_CFG_MAC_RX_RST,
696e6e12df6SVladimir Oltean 				 DEV_CLOCK_CFG);
697e6e12df6SVladimir Oltean }
698e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
699e6e12df6SVladimir Oltean 
700e6e12df6SVladimir Oltean void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
701e6e12df6SVladimir Oltean 				struct phy_device *phydev,
702e6e12df6SVladimir Oltean 				unsigned int link_an_mode,
703e6e12df6SVladimir Oltean 				phy_interface_t interface,
704e6e12df6SVladimir Oltean 				int speed, int duplex,
705e6e12df6SVladimir Oltean 				bool tx_pause, bool rx_pause,
706e6e12df6SVladimir Oltean 				unsigned long quirks)
707e6e12df6SVladimir Oltean {
708e6e12df6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
709e6e12df6SVladimir Oltean 	int mac_speed, mode = 0;
710e6e12df6SVladimir Oltean 	u32 mac_fc_cfg;
711e6e12df6SVladimir Oltean 
7128abe1970SVladimir Oltean 	ocelot_port->speed = speed;
7138abe1970SVladimir Oltean 
714e6e12df6SVladimir Oltean 	/* The MAC might be integrated in systems where the MAC speed is fixed
715e6e12df6SVladimir Oltean 	 * and it's the PCS who is performing the rate adaptation, so we have
716e6e12df6SVladimir Oltean 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
717e6e12df6SVladimir Oltean 	 * (which is also its default value).
718e6e12df6SVladimir Oltean 	 */
719e6e12df6SVladimir Oltean 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
720e6e12df6SVladimir Oltean 	    speed == SPEED_1000) {
721e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_1000;
722e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
723e6e12df6SVladimir Oltean 	} else if (speed == SPEED_2500) {
724e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_2500;
725e6e12df6SVladimir Oltean 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
726e6e12df6SVladimir Oltean 	} else if (speed == SPEED_100) {
727e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_100;
728e6e12df6SVladimir Oltean 	} else {
729e6e12df6SVladimir Oltean 		mac_speed = OCELOT_SPEED_10;
730e6e12df6SVladimir Oltean 	}
731e6e12df6SVladimir Oltean 
732e6e12df6SVladimir Oltean 	if (duplex == DUPLEX_FULL)
733e6e12df6SVladimir Oltean 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
734e6e12df6SVladimir Oltean 
735e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
736e6e12df6SVladimir Oltean 
737e6e12df6SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
738e6e12df6SVladimir Oltean 	 * PORT_RST bits in DEV_CLOCK_CFG.
739e6e12df6SVladimir Oltean 	 */
740e6e12df6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
741e6e12df6SVladimir Oltean 			   DEV_CLOCK_CFG);
742e6e12df6SVladimir Oltean 
743e6e12df6SVladimir Oltean 	switch (speed) {
744a556c76aSAlexandre Belloni 	case SPEED_10:
745e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
746a556c76aSAlexandre Belloni 		break;
747a556c76aSAlexandre Belloni 	case SPEED_100:
748e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
749a556c76aSAlexandre Belloni 		break;
750a556c76aSAlexandre Belloni 	case SPEED_1000:
751a556c76aSAlexandre Belloni 	case SPEED_2500:
752e6e12df6SVladimir Oltean 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
753a556c76aSAlexandre Belloni 		break;
754a556c76aSAlexandre Belloni 	default:
755e6e12df6SVladimir Oltean 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
756e6e12df6SVladimir Oltean 			port, speed);
757a556c76aSAlexandre Belloni 		return;
758a556c76aSAlexandre Belloni 	}
759a556c76aSAlexandre Belloni 
760e6e12df6SVladimir Oltean 	/* Handle RX pause in all cases, with 2500base-X this is used for rate
761e6e12df6SVladimir Oltean 	 * adaptation.
762e6e12df6SVladimir Oltean 	 */
763e6e12df6SVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
764a556c76aSAlexandre Belloni 
765e6e12df6SVladimir Oltean 	if (tx_pause)
766e6e12df6SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
767e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
768e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
769e6e12df6SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
770a556c76aSAlexandre Belloni 
771e6e12df6SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
772e6e12df6SVladimir Oltean 	 * specification in incoming pause frames.
773e6e12df6SVladimir Oltean 	 */
774e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
775a556c76aSAlexandre Belloni 
776e6e12df6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
7771ba8f656SVladimir Oltean 
77833cb0ff3SVladimir Oltean 	/* Don't attempt to send PAUSE frames on the NPI port, it's broken */
77933cb0ff3SVladimir Oltean 	if (port != ocelot->npi)
78033cb0ff3SVladimir Oltean 		ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
78133cb0ff3SVladimir Oltean 				    tx_pause);
7821ba8f656SVladimir Oltean 
783e6e12df6SVladimir Oltean 	/* Undo the effects of ocelot_phylink_mac_link_down:
784e6e12df6SVladimir Oltean 	 * enable MAC module
785e6e12df6SVladimir Oltean 	 */
786004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
787a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
788a556c76aSAlexandre Belloni 
7898abe1970SVladimir Oltean 	/* If the port supports cut-through forwarding, update the masks before
7908abe1970SVladimir Oltean 	 * enabling forwarding on the port.
7918abe1970SVladimir Oltean 	 */
7928abe1970SVladimir Oltean 	if (ocelot->ops->cut_through_fwd) {
7938abe1970SVladimir Oltean 		mutex_lock(&ocelot->fwd_domain_lock);
7948abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
7958abe1970SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
7968abe1970SVladimir Oltean 	}
7978abe1970SVladimir Oltean 
798a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
799886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, port,
800886e1387SVladimir Oltean 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
801a556c76aSAlexandre Belloni }
802e6e12df6SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
803889b8950SVladimir Oltean 
80452849bcfSVladimir Oltean static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
805e2f9a8feSVladimir Oltean 					struct sk_buff *clone)
806400928bfSYangbo Lu {
807e2f9a8feSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
80852849bcfSVladimir Oltean 	unsigned long flags;
809400928bfSYangbo Lu 
81052849bcfSVladimir Oltean 	spin_lock_irqsave(&ocelot->ts_id_lock, flags);
81152849bcfSVladimir Oltean 
81252849bcfSVladimir Oltean 	if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
81352849bcfSVladimir Oltean 	    ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
81452849bcfSVladimir Oltean 		spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
81552849bcfSVladimir Oltean 		return -EBUSY;
81652849bcfSVladimir Oltean 	}
8176565243cSVladimir Oltean 
818e2f9a8feSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
819c4b364ceSYangbo Lu 	/* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
820c4b364ceSYangbo Lu 	OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
82152849bcfSVladimir Oltean 
822c57fe003SVladimir Oltean 	ocelot_port->ts_id++;
823c57fe003SVladimir Oltean 	if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
824c57fe003SVladimir Oltean 		ocelot_port->ts_id = 0;
82552849bcfSVladimir Oltean 
82652849bcfSVladimir Oltean 	ocelot_port->ptp_skbs_in_flight++;
82752849bcfSVladimir Oltean 	ocelot->ptp_skbs_in_flight++;
82852849bcfSVladimir Oltean 
829e2f9a8feSVladimir Oltean 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
8306565243cSVladimir Oltean 
83152849bcfSVladimir Oltean 	spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
83252849bcfSVladimir Oltean 
83352849bcfSVladimir Oltean 	return 0;
834400928bfSYangbo Lu }
835682eaad9SYangbo Lu 
836fba01283SVladimir Oltean static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
837fba01283SVladimir Oltean 				       unsigned int ptp_class)
83839e5308bSYangbo Lu {
83939e5308bSYangbo Lu 	struct ptp_header *hdr;
84039e5308bSYangbo Lu 	u8 msgtype, twostep;
84139e5308bSYangbo Lu 
84239e5308bSYangbo Lu 	hdr = ptp_parse_header(skb, ptp_class);
84339e5308bSYangbo Lu 	if (!hdr)
84439e5308bSYangbo Lu 		return false;
84539e5308bSYangbo Lu 
84639e5308bSYangbo Lu 	msgtype = ptp_get_msgtype(hdr, ptp_class);
84739e5308bSYangbo Lu 	twostep = hdr->flag_field[0] & 0x2;
84839e5308bSYangbo Lu 
84939e5308bSYangbo Lu 	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
85039e5308bSYangbo Lu 		return true;
85139e5308bSYangbo Lu 
85239e5308bSYangbo Lu 	return false;
85339e5308bSYangbo Lu }
85439e5308bSYangbo Lu 
855682eaad9SYangbo Lu int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
856682eaad9SYangbo Lu 				 struct sk_buff *skb,
857682eaad9SYangbo Lu 				 struct sk_buff **clone)
858682eaad9SYangbo Lu {
859682eaad9SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
860682eaad9SYangbo Lu 	u8 ptp_cmd = ocelot_port->ptp_cmd;
861fba01283SVladimir Oltean 	unsigned int ptp_class;
86252849bcfSVladimir Oltean 	int err;
863682eaad9SYangbo Lu 
864fba01283SVladimir Oltean 	/* Don't do anything if PTP timestamping not enabled */
865fba01283SVladimir Oltean 	if (!ptp_cmd)
866fba01283SVladimir Oltean 		return 0;
867fba01283SVladimir Oltean 
868fba01283SVladimir Oltean 	ptp_class = ptp_classify_raw(skb);
869fba01283SVladimir Oltean 	if (ptp_class == PTP_CLASS_NONE)
870fba01283SVladimir Oltean 		return -EINVAL;
871682eaad9SYangbo Lu 
87239e5308bSYangbo Lu 	/* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
87339e5308bSYangbo Lu 	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
874fba01283SVladimir Oltean 		if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
87539e5308bSYangbo Lu 			OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
87639e5308bSYangbo Lu 			return 0;
87739e5308bSYangbo Lu 		}
87839e5308bSYangbo Lu 
87939e5308bSYangbo Lu 		/* Fall back to two-step timestamping */
88039e5308bSYangbo Lu 		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
88139e5308bSYangbo Lu 	}
88239e5308bSYangbo Lu 
883682eaad9SYangbo Lu 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
884682eaad9SYangbo Lu 		*clone = skb_clone_sk(skb);
885682eaad9SYangbo Lu 		if (!(*clone))
886682eaad9SYangbo Lu 			return -ENOMEM;
887682eaad9SYangbo Lu 
88852849bcfSVladimir Oltean 		err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
88952849bcfSVladimir Oltean 		if (err)
89052849bcfSVladimir Oltean 			return err;
89152849bcfSVladimir Oltean 
89239e5308bSYangbo Lu 		OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
893ebb4c6a9SVladimir Oltean 		OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
894682eaad9SYangbo Lu 	}
895682eaad9SYangbo Lu 
896682eaad9SYangbo Lu 	return 0;
897682eaad9SYangbo Lu }
898682eaad9SYangbo Lu EXPORT_SYMBOL(ocelot_port_txtstamp_request);
899400928bfSYangbo Lu 
900e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
901e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
9024e3b0468SAntoine Tenart {
9034e3b0468SAntoine Tenart 	unsigned long flags;
9044e3b0468SAntoine Tenart 	u32 val;
9054e3b0468SAntoine Tenart 
9064e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
9074e3b0468SAntoine Tenart 
9084e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
9094e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
9104e3b0468SAntoine Tenart 
9114e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
9124e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
9134e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
9144e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
9154e3b0468SAntoine Tenart 
9164e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
9174e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
9184e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
9194e3b0468SAntoine Tenart 
9204e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
9214e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
9224e3b0468SAntoine Tenart 		ts->tv_sec--;
9234e3b0468SAntoine Tenart 
9244e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
9254e3b0468SAntoine Tenart }
926e23a7b3eSYangbo Lu 
927ebb4c6a9SVladimir Oltean static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
928ebb4c6a9SVladimir Oltean {
929ebb4c6a9SVladimir Oltean 	struct ptp_header *hdr;
930ebb4c6a9SVladimir Oltean 
931ebb4c6a9SVladimir Oltean 	hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
932ebb4c6a9SVladimir Oltean 	if (WARN_ON(!hdr))
933ebb4c6a9SVladimir Oltean 		return false;
934ebb4c6a9SVladimir Oltean 
935ebb4c6a9SVladimir Oltean 	return seqid == ntohs(hdr->sequence_id);
936ebb4c6a9SVladimir Oltean }
937ebb4c6a9SVladimir Oltean 
938e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
939e23a7b3eSYangbo Lu {
940e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
941e23a7b3eSYangbo Lu 
942e23a7b3eSYangbo Lu 	while (budget--) {
943b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
944e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
945ebb4c6a9SVladimir Oltean 		u32 val, id, seqid, txport;
946e23a7b3eSYangbo Lu 		struct ocelot_port *port;
947e23a7b3eSYangbo Lu 		struct timespec64 ts;
948b049da13SYangbo Lu 		unsigned long flags;
949e23a7b3eSYangbo Lu 
950e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
951e23a7b3eSYangbo Lu 
952e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
953e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
954e23a7b3eSYangbo Lu 			break;
955e23a7b3eSYangbo Lu 
956e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
957e23a7b3eSYangbo Lu 
958e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
959e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
960e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
961ebb4c6a9SVladimir Oltean 		seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
962e23a7b3eSYangbo Lu 
963e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
964e23a7b3eSYangbo Lu 
96552849bcfSVladimir Oltean 		spin_lock(&ocelot->ts_id_lock);
96652849bcfSVladimir Oltean 		port->ptp_skbs_in_flight--;
96752849bcfSVladimir Oltean 		ocelot->ptp_skbs_in_flight--;
96852849bcfSVladimir Oltean 		spin_unlock(&ocelot->ts_id_lock);
96952849bcfSVladimir Oltean 
97052849bcfSVladimir Oltean 		/* Retrieve its associated skb */
971ebb4c6a9SVladimir Oltean try_again:
972b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
973b049da13SYangbo Lu 
974b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
975c4b364ceSYangbo Lu 			if (OCELOT_SKB_CB(skb)->ts_id != id)
976e23a7b3eSYangbo Lu 				continue;
977b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
978b049da13SYangbo Lu 			skb_match = skb;
979fc62c094SYangbo Lu 			break;
980e23a7b3eSYangbo Lu 		}
981e23a7b3eSYangbo Lu 
982b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
983b049da13SYangbo Lu 
9849fde506eSVladimir Oltean 		if (WARN_ON(!skb_match))
9859fde506eSVladimir Oltean 			continue;
9869fde506eSVladimir Oltean 
987ebb4c6a9SVladimir Oltean 		if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
988ebb4c6a9SVladimir Oltean 			dev_err_ratelimited(ocelot->dev,
989ebb4c6a9SVladimir Oltean 					    "port %d received stale TX timestamp for seqid %d, discarding\n",
990ebb4c6a9SVladimir Oltean 					    txport, seqid);
991ebb4c6a9SVladimir Oltean 			dev_kfree_skb_any(skb);
992ebb4c6a9SVladimir Oltean 			goto try_again;
993ebb4c6a9SVladimir Oltean 		}
994ebb4c6a9SVladimir Oltean 
9955fd82200Slaurent brando 		/* Get the h/w timestamp */
9965fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
997e23a7b3eSYangbo Lu 
998e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
999e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1000e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
1001e2f9a8feSVladimir Oltean 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
10025fd82200Slaurent brando 
10035fd82200Slaurent brando 		/* Next ts */
10045fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
1005e23a7b3eSYangbo Lu 	}
1006e23a7b3eSYangbo Lu }
1007e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
10084e3b0468SAntoine Tenart 
1009924ee317SVladimir Oltean static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
1010924ee317SVladimir Oltean 				u32 *rval)
1011924ee317SVladimir Oltean {
1012924ee317SVladimir Oltean 	u32 bytes_valid, val;
1013924ee317SVladimir Oltean 
1014924ee317SVladimir Oltean 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1015924ee317SVladimir Oltean 	if (val == XTR_NOT_READY) {
1016924ee317SVladimir Oltean 		if (ifh)
1017924ee317SVladimir Oltean 			return -EIO;
1018924ee317SVladimir Oltean 
1019924ee317SVladimir Oltean 		do {
1020924ee317SVladimir Oltean 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1021924ee317SVladimir Oltean 		} while (val == XTR_NOT_READY);
1022924ee317SVladimir Oltean 	}
1023924ee317SVladimir Oltean 
1024924ee317SVladimir Oltean 	switch (val) {
1025924ee317SVladimir Oltean 	case XTR_ABORT:
1026924ee317SVladimir Oltean 		return -EIO;
1027924ee317SVladimir Oltean 	case XTR_EOF_0:
1028924ee317SVladimir Oltean 	case XTR_EOF_1:
1029924ee317SVladimir Oltean 	case XTR_EOF_2:
1030924ee317SVladimir Oltean 	case XTR_EOF_3:
1031924ee317SVladimir Oltean 	case XTR_PRUNED:
1032924ee317SVladimir Oltean 		bytes_valid = XTR_VALID_BYTES(val);
1033924ee317SVladimir Oltean 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1034924ee317SVladimir Oltean 		if (val == XTR_ESCAPE)
1035924ee317SVladimir Oltean 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1036924ee317SVladimir Oltean 		else
1037924ee317SVladimir Oltean 			*rval = val;
1038924ee317SVladimir Oltean 
1039924ee317SVladimir Oltean 		return bytes_valid;
1040924ee317SVladimir Oltean 	case XTR_ESCAPE:
1041924ee317SVladimir Oltean 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1042924ee317SVladimir Oltean 
1043924ee317SVladimir Oltean 		return 4;
1044924ee317SVladimir Oltean 	default:
1045924ee317SVladimir Oltean 		*rval = val;
1046924ee317SVladimir Oltean 
1047924ee317SVladimir Oltean 		return 4;
1048924ee317SVladimir Oltean 	}
1049924ee317SVladimir Oltean }
1050924ee317SVladimir Oltean 
1051924ee317SVladimir Oltean static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
1052924ee317SVladimir Oltean {
1053924ee317SVladimir Oltean 	int i, err = 0;
1054924ee317SVladimir Oltean 
1055924ee317SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
1056924ee317SVladimir Oltean 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
1057924ee317SVladimir Oltean 		if (err != 4)
1058924ee317SVladimir Oltean 			return (err < 0) ? err : -EIO;
1059924ee317SVladimir Oltean 	}
1060924ee317SVladimir Oltean 
1061924ee317SVladimir Oltean 	return 0;
1062924ee317SVladimir Oltean }
1063924ee317SVladimir Oltean 
1064b471a71eSClément Léger void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
1065b471a71eSClément Léger 			     u64 timestamp)
1066924ee317SVladimir Oltean {
1067924ee317SVladimir Oltean 	struct skb_shared_hwtstamps *shhwtstamps;
10682ed2c5f0SHoratiu Vultur 	u64 tod_in_ns, full_ts_in_ns;
1069b471a71eSClément Léger 	struct timespec64 ts;
1070b471a71eSClément Léger 
1071b471a71eSClément Léger 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1072b471a71eSClément Léger 
1073b471a71eSClément Léger 	tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1074b471a71eSClément Léger 	if ((tod_in_ns & 0xffffffff) < timestamp)
1075b471a71eSClément Léger 		full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1076b471a71eSClément Léger 				timestamp;
1077b471a71eSClément Léger 	else
1078b471a71eSClément Léger 		full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1079b471a71eSClément Léger 				timestamp;
1080b471a71eSClément Léger 
1081b471a71eSClément Léger 	shhwtstamps = skb_hwtstamps(skb);
1082b471a71eSClément Léger 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1083b471a71eSClément Léger 	shhwtstamps->hwtstamp = full_ts_in_ns;
1084b471a71eSClément Léger }
1085b471a71eSClément Léger EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
1086b471a71eSClément Léger 
1087b471a71eSClément Léger int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
1088b471a71eSClément Léger {
1089924ee317SVladimir Oltean 	u64 timestamp, src_port, len;
1090924ee317SVladimir Oltean 	u32 xfh[OCELOT_TAG_LEN / 4];
1091924ee317SVladimir Oltean 	struct net_device *dev;
1092924ee317SVladimir Oltean 	struct sk_buff *skb;
1093924ee317SVladimir Oltean 	int sz, buf_len;
1094924ee317SVladimir Oltean 	u32 val, *buf;
1095924ee317SVladimir Oltean 	int err;
1096924ee317SVladimir Oltean 
1097924ee317SVladimir Oltean 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1098924ee317SVladimir Oltean 	if (err)
1099924ee317SVladimir Oltean 		return err;
1100924ee317SVladimir Oltean 
1101924ee317SVladimir Oltean 	ocelot_xfh_get_src_port(xfh, &src_port);
1102924ee317SVladimir Oltean 	ocelot_xfh_get_len(xfh, &len);
1103924ee317SVladimir Oltean 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1104924ee317SVladimir Oltean 
1105924ee317SVladimir Oltean 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1106924ee317SVladimir Oltean 		return -EINVAL;
1107924ee317SVladimir Oltean 
1108924ee317SVladimir Oltean 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1109924ee317SVladimir Oltean 	if (!dev)
1110924ee317SVladimir Oltean 		return -EINVAL;
1111924ee317SVladimir Oltean 
1112924ee317SVladimir Oltean 	skb = netdev_alloc_skb(dev, len);
1113924ee317SVladimir Oltean 	if (unlikely(!skb)) {
1114924ee317SVladimir Oltean 		netdev_err(dev, "Unable to allocate sk_buff\n");
1115924ee317SVladimir Oltean 		return -ENOMEM;
1116924ee317SVladimir Oltean 	}
1117924ee317SVladimir Oltean 
1118924ee317SVladimir Oltean 	buf_len = len - ETH_FCS_LEN;
1119924ee317SVladimir Oltean 	buf = (u32 *)skb_put(skb, buf_len);
1120924ee317SVladimir Oltean 
1121924ee317SVladimir Oltean 	len = 0;
1122924ee317SVladimir Oltean 	do {
1123924ee317SVladimir Oltean 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1124924ee317SVladimir Oltean 		if (sz < 0) {
1125924ee317SVladimir Oltean 			err = sz;
1126924ee317SVladimir Oltean 			goto out_free_skb;
1127924ee317SVladimir Oltean 		}
1128924ee317SVladimir Oltean 		*buf++ = val;
1129924ee317SVladimir Oltean 		len += sz;
1130924ee317SVladimir Oltean 	} while (len < buf_len);
1131924ee317SVladimir Oltean 
1132924ee317SVladimir Oltean 	/* Read the FCS */
1133924ee317SVladimir Oltean 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1134924ee317SVladimir Oltean 	if (sz < 0) {
1135924ee317SVladimir Oltean 		err = sz;
1136924ee317SVladimir Oltean 		goto out_free_skb;
1137924ee317SVladimir Oltean 	}
1138924ee317SVladimir Oltean 
1139924ee317SVladimir Oltean 	/* Update the statistics if part of the FCS was read before */
1140924ee317SVladimir Oltean 	len -= ETH_FCS_LEN - sz;
1141924ee317SVladimir Oltean 
1142924ee317SVladimir Oltean 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1143924ee317SVladimir Oltean 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1144924ee317SVladimir Oltean 		*buf = val;
1145924ee317SVladimir Oltean 	}
1146924ee317SVladimir Oltean 
1147b471a71eSClément Léger 	if (ocelot->ptp)
1148b471a71eSClément Léger 		ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
1149924ee317SVladimir Oltean 
1150924ee317SVladimir Oltean 	/* Everything we see on an interface that is in the HW bridge
1151924ee317SVladimir Oltean 	 * has already been forwarded.
1152924ee317SVladimir Oltean 	 */
1153df291e54SVladimir Oltean 	if (ocelot->ports[src_port]->bridge)
1154924ee317SVladimir Oltean 		skb->offload_fwd_mark = 1;
1155924ee317SVladimir Oltean 
1156924ee317SVladimir Oltean 	skb->protocol = eth_type_trans(skb, dev);
1157d8ea7ff3SHoratiu Vultur 
1158924ee317SVladimir Oltean 	*nskb = skb;
1159924ee317SVladimir Oltean 
1160924ee317SVladimir Oltean 	return 0;
1161924ee317SVladimir Oltean 
1162924ee317SVladimir Oltean out_free_skb:
1163924ee317SVladimir Oltean 	kfree_skb(skb);
1164924ee317SVladimir Oltean 	return err;
1165924ee317SVladimir Oltean }
1166924ee317SVladimir Oltean EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1167924ee317SVladimir Oltean 
1168137ffbc4SVladimir Oltean bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1169137ffbc4SVladimir Oltean {
1170137ffbc4SVladimir Oltean 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1171137ffbc4SVladimir Oltean 
1172137ffbc4SVladimir Oltean 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1173137ffbc4SVladimir Oltean 		return false;
1174137ffbc4SVladimir Oltean 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1175137ffbc4SVladimir Oltean 		return false;
1176137ffbc4SVladimir Oltean 
1177137ffbc4SVladimir Oltean 	return true;
1178137ffbc4SVladimir Oltean }
1179137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_can_inject);
1180137ffbc4SVladimir Oltean 
1181e5150f00SClément Léger void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag)
1182e5150f00SClément Léger {
1183e5150f00SClément Léger 	ocelot_ifh_set_bypass(ifh, 1);
1184e5150f00SClément Léger 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1185e5150f00SClément Léger 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1186e5150f00SClément Léger 	if (vlan_tag)
1187e5150f00SClément Léger 		ocelot_ifh_set_vlan_tci(ifh, vlan_tag);
1188e5150f00SClément Léger 	if (rew_op)
1189e5150f00SClément Léger 		ocelot_ifh_set_rew_op(ifh, rew_op);
1190e5150f00SClément Léger }
1191e5150f00SClément Léger EXPORT_SYMBOL(ocelot_ifh_port_set);
1192e5150f00SClément Léger 
1193137ffbc4SVladimir Oltean void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1194137ffbc4SVladimir Oltean 			      u32 rew_op, struct sk_buff *skb)
1195137ffbc4SVladimir Oltean {
119640d3f295SVladimir Oltean 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1197137ffbc4SVladimir Oltean 	unsigned int i, count, last;
1198137ffbc4SVladimir Oltean 
1199137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1200137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1201137ffbc4SVladimir Oltean 
1202e5150f00SClément Léger 	ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
1203137ffbc4SVladimir Oltean 
1204137ffbc4SVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
120540d3f295SVladimir Oltean 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1206137ffbc4SVladimir Oltean 
1207137ffbc4SVladimir Oltean 	count = DIV_ROUND_UP(skb->len, 4);
1208137ffbc4SVladimir Oltean 	last = skb->len % 4;
1209137ffbc4SVladimir Oltean 	for (i = 0; i < count; i++)
1210137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1211137ffbc4SVladimir Oltean 
1212137ffbc4SVladimir Oltean 	/* Add padding */
1213137ffbc4SVladimir Oltean 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1214137ffbc4SVladimir Oltean 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1215137ffbc4SVladimir Oltean 		i++;
1216137ffbc4SVladimir Oltean 	}
1217137ffbc4SVladimir Oltean 
1218137ffbc4SVladimir Oltean 	/* Indicate EOF and valid bytes in last word */
1219137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1220137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1221137ffbc4SVladimir Oltean 			 QS_INJ_CTRL_EOF,
1222137ffbc4SVladimir Oltean 			 QS_INJ_CTRL, grp);
1223137ffbc4SVladimir Oltean 
1224137ffbc4SVladimir Oltean 	/* Add dummy CRC */
1225137ffbc4SVladimir Oltean 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1226137ffbc4SVladimir Oltean 	skb_tx_timestamp(skb);
1227137ffbc4SVladimir Oltean 
1228137ffbc4SVladimir Oltean 	skb->dev->stats.tx_packets++;
1229137ffbc4SVladimir Oltean 	skb->dev->stats.tx_bytes += skb->len;
1230137ffbc4SVladimir Oltean }
1231137ffbc4SVladimir Oltean EXPORT_SYMBOL(ocelot_port_inject_frame);
1232137ffbc4SVladimir Oltean 
12330a6f17c6SVladimir Oltean void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
12340a6f17c6SVladimir Oltean {
12350a6f17c6SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
12360a6f17c6SVladimir Oltean 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
12370a6f17c6SVladimir Oltean }
12380a6f17c6SVladimir Oltean EXPORT_SYMBOL(ocelot_drain_cpu_queue);
12390a6f17c6SVladimir Oltean 
12405e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
124187b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
1242a556c76aSAlexandre Belloni {
1243471beb11SVladimir Oltean 	int pgid = port;
1244471beb11SVladimir Oltean 
1245471beb11SVladimir Oltean 	if (port == ocelot->npi)
1246471beb11SVladimir Oltean 		pgid = PGID_CPU;
1247a556c76aSAlexandre Belloni 
1248471beb11SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
1249a556c76aSAlexandre Belloni }
12505e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
1251a556c76aSAlexandre Belloni 
12525e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
1253531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
1254531ee1a6SVladimir Oltean {
1255531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
1256531ee1a6SVladimir Oltean }
12575e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
1258531ee1a6SVladimir Oltean 
12599c90eea3SVladimir Oltean int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1260531ee1a6SVladimir Oltean 			    bool is_static, void *data)
1261a556c76aSAlexandre Belloni {
1262531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
1263a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1264a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
1265a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
1266a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
1267a556c76aSAlexandre Belloni 
1268a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
1269a556c76aSAlexandre Belloni 		goto skip;
1270a556c76aSAlexandre Belloni 
1271a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1272a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
1273a556c76aSAlexandre Belloni 	if (!nlh)
1274a556c76aSAlexandre Belloni 		return -EMSGSIZE;
1275a556c76aSAlexandre Belloni 
1276a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
1277a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
1278a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
1279a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
1280a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
1281a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
1282a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
1283531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
1284a556c76aSAlexandre Belloni 
1285531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
1286a556c76aSAlexandre Belloni 		goto nla_put_failure;
1287a556c76aSAlexandre Belloni 
1288531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
1289a556c76aSAlexandre Belloni 		goto nla_put_failure;
1290a556c76aSAlexandre Belloni 
1291a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
1292a556c76aSAlexandre Belloni 
1293a556c76aSAlexandre Belloni skip:
1294a556c76aSAlexandre Belloni 	dump->idx++;
1295a556c76aSAlexandre Belloni 	return 0;
1296a556c76aSAlexandre Belloni 
1297a556c76aSAlexandre Belloni nla_put_failure:
1298a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
1299a556c76aSAlexandre Belloni 	return -EMSGSIZE;
1300a556c76aSAlexandre Belloni }
13019c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
1302a556c76aSAlexandre Belloni 
13032468346cSVladimir Oltean /* Caller must hold &ocelot->mact_lock */
1304531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1305a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
1306a556c76aSAlexandre Belloni {
1307a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
1308531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
1309a556c76aSAlexandre Belloni 
1310a556c76aSAlexandre Belloni 	/* Set row and column to read from */
1311a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1312a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1313a556c76aSAlexandre Belloni 
1314a556c76aSAlexandre Belloni 	/* Issue a read command */
1315a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1316a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1317a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
1318a556c76aSAlexandre Belloni 
1319a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
1320a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
1321a556c76aSAlexandre Belloni 
1322a556c76aSAlexandre Belloni 	/* Read the entry flags */
1323a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1324a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1325a556c76aSAlexandre Belloni 		return -EINVAL;
1326a556c76aSAlexandre Belloni 
1327a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1328a556c76aSAlexandre Belloni 	 * do not report it.
1329a556c76aSAlexandre Belloni 	 */
1330a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1331531ee1a6SVladimir Oltean 	if (dst != port)
1332a556c76aSAlexandre Belloni 		return -EINVAL;
1333a556c76aSAlexandre Belloni 
1334a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1335a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1336a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1337a556c76aSAlexandre Belloni 
1338a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1339a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1340a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1341a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1342a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1343a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1344a556c76aSAlexandre Belloni 
1345a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1346a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1347a556c76aSAlexandre Belloni 
1348a556c76aSAlexandre Belloni 	return 0;
1349a556c76aSAlexandre Belloni }
1350a556c76aSAlexandre Belloni 
13515cad43a5SVladimir Oltean int ocelot_mact_flush(struct ocelot *ocelot, int port)
13525cad43a5SVladimir Oltean {
13535cad43a5SVladimir Oltean 	int err;
13545cad43a5SVladimir Oltean 
13555cad43a5SVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
13565cad43a5SVladimir Oltean 
13575cad43a5SVladimir Oltean 	/* Program ageing filter for a single port */
13585cad43a5SVladimir Oltean 	ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
13595cad43a5SVladimir Oltean 		     ANA_ANAGEFIL);
13605cad43a5SVladimir Oltean 
13615cad43a5SVladimir Oltean 	/* Flushing dynamic FDB entries requires two successive age scans */
13625cad43a5SVladimir Oltean 	ocelot_write(ocelot,
13635cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
13645cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
13655cad43a5SVladimir Oltean 
13665cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
13675cad43a5SVladimir Oltean 	if (err) {
13685cad43a5SVladimir Oltean 		mutex_unlock(&ocelot->mact_lock);
13695cad43a5SVladimir Oltean 		return err;
13705cad43a5SVladimir Oltean 	}
13715cad43a5SVladimir Oltean 
13725cad43a5SVladimir Oltean 	/* And second... */
13735cad43a5SVladimir Oltean 	ocelot_write(ocelot,
13745cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
13755cad43a5SVladimir Oltean 		     ANA_TABLES_MACACCESS);
13765cad43a5SVladimir Oltean 
13775cad43a5SVladimir Oltean 	err = ocelot_mact_wait_for_completion(ocelot);
13785cad43a5SVladimir Oltean 
13795cad43a5SVladimir Oltean 	/* Restore ageing filter */
13805cad43a5SVladimir Oltean 	ocelot_write(ocelot, 0, ANA_ANAGEFIL);
13815cad43a5SVladimir Oltean 
13825cad43a5SVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
13835cad43a5SVladimir Oltean 
13845cad43a5SVladimir Oltean 	return err;
13855cad43a5SVladimir Oltean }
13865cad43a5SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_mact_flush);
13875cad43a5SVladimir Oltean 
13885e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1389531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1390a556c76aSAlexandre Belloni {
13912468346cSVladimir Oltean 	int err = 0;
1392531ee1a6SVladimir Oltean 	int i, j;
1393a556c76aSAlexandre Belloni 
13942468346cSVladimir Oltean 	/* We could take the lock just around ocelot_mact_read, but doing so
13952468346cSVladimir Oltean 	 * thousands of times in a row seems rather pointless and inefficient.
13962468346cSVladimir Oltean 	 */
13972468346cSVladimir Oltean 	mutex_lock(&ocelot->mact_lock);
13982468346cSVladimir Oltean 
139921ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
140021ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1401a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1402531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1403531ee1a6SVladimir Oltean 			bool is_static;
1404531ee1a6SVladimir Oltean 
14052468346cSVladimir Oltean 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1406a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1407a556c76aSAlexandre Belloni 			 * skip it.
1408a556c76aSAlexandre Belloni 			 */
14092468346cSVladimir Oltean 			if (err == -EINVAL)
1410a556c76aSAlexandre Belloni 				continue;
14112468346cSVladimir Oltean 			else if (err)
14122468346cSVladimir Oltean 				break;
1413a556c76aSAlexandre Belloni 
1414531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1415531ee1a6SVladimir Oltean 
14162468346cSVladimir Oltean 			err = cb(entry.mac, entry.vid, is_static, data);
14172468346cSVladimir Oltean 			if (err)
14182468346cSVladimir Oltean 				break;
1419a556c76aSAlexandre Belloni 		}
1420a556c76aSAlexandre Belloni 	}
1421a556c76aSAlexandre Belloni 
14222468346cSVladimir Oltean 	mutex_unlock(&ocelot->mact_lock);
14232468346cSVladimir Oltean 
14242468346cSVladimir Oltean 	return err;
1425531ee1a6SVladimir Oltean }
14265e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1427531ee1a6SVladimir Oltean 
142896ca08c0SVladimir Oltean static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
142996ca08c0SVladimir Oltean {
143096ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_ETYPE;
143196ca08c0SVladimir Oltean 	*(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588);
143296ca08c0SVladimir Oltean 	*(__be16 *)trap->key.etype.etype.mask = htons(0xffff);
143396ca08c0SVladimir Oltean }
143496ca08c0SVladimir Oltean 
143596ca08c0SVladimir Oltean static void
143696ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
143796ca08c0SVladimir Oltean {
143896ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_IPV4;
143959085208SVladimir Oltean 	trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
144059085208SVladimir Oltean 	trap->key.ipv4.proto.mask[0] = 0xff;
144196ca08c0SVladimir Oltean 	trap->key.ipv4.dport.value = PTP_EV_PORT;
144296ca08c0SVladimir Oltean 	trap->key.ipv4.dport.mask = 0xffff;
144396ca08c0SVladimir Oltean }
144496ca08c0SVladimir Oltean 
144596ca08c0SVladimir Oltean static void
144696ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
144796ca08c0SVladimir Oltean {
144896ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_IPV6;
144959085208SVladimir Oltean 	trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
145059085208SVladimir Oltean 	trap->key.ipv4.proto.mask[0] = 0xff;
145196ca08c0SVladimir Oltean 	trap->key.ipv6.dport.value = PTP_EV_PORT;
145296ca08c0SVladimir Oltean 	trap->key.ipv6.dport.mask = 0xffff;
145396ca08c0SVladimir Oltean }
145496ca08c0SVladimir Oltean 
145596ca08c0SVladimir Oltean static void
145696ca08c0SVladimir Oltean ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
145796ca08c0SVladimir Oltean {
145896ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_IPV4;
145959085208SVladimir Oltean 	trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
146059085208SVladimir Oltean 	trap->key.ipv4.proto.mask[0] = 0xff;
146196ca08c0SVladimir Oltean 	trap->key.ipv4.dport.value = PTP_GEN_PORT;
146296ca08c0SVladimir Oltean 	trap->key.ipv4.dport.mask = 0xffff;
146396ca08c0SVladimir Oltean }
146496ca08c0SVladimir Oltean 
146596ca08c0SVladimir Oltean static void
146696ca08c0SVladimir Oltean ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
146796ca08c0SVladimir Oltean {
146896ca08c0SVladimir Oltean 	trap->key_type = OCELOT_VCAP_KEY_IPV6;
146959085208SVladimir Oltean 	trap->key.ipv4.proto.value[0] = IPPROTO_UDP;
147059085208SVladimir Oltean 	trap->key.ipv4.proto.mask[0] = 0xff;
147196ca08c0SVladimir Oltean 	trap->key.ipv6.dport.value = PTP_GEN_PORT;
147296ca08c0SVladimir Oltean 	trap->key.ipv6.dport.mask = 0xffff;
147396ca08c0SVladimir Oltean }
147496ca08c0SVladimir Oltean 
14759d75b881SVladimir Oltean int ocelot_trap_add(struct ocelot *ocelot, int port,
14769d75b881SVladimir Oltean 		    unsigned long cookie, bool take_ts,
147796ca08c0SVladimir Oltean 		    void (*populate)(struct ocelot_vcap_filter *f))
147896ca08c0SVladimir Oltean {
147996ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
148096ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
148196ca08c0SVladimir Oltean 	bool new = false;
148296ca08c0SVladimir Oltean 	int err;
148396ca08c0SVladimir Oltean 
148496ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
148596ca08c0SVladimir Oltean 
148696ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
148796ca08c0SVladimir Oltean 						   false);
148896ca08c0SVladimir Oltean 	if (!trap) {
148996ca08c0SVladimir Oltean 		trap = kzalloc(sizeof(*trap), GFP_KERNEL);
149096ca08c0SVladimir Oltean 		if (!trap)
149196ca08c0SVladimir Oltean 			return -ENOMEM;
149296ca08c0SVladimir Oltean 
149396ca08c0SVladimir Oltean 		populate(trap);
149496ca08c0SVladimir Oltean 		trap->prio = 1;
149596ca08c0SVladimir Oltean 		trap->id.cookie = cookie;
149696ca08c0SVladimir Oltean 		trap->id.tc_offload = false;
149796ca08c0SVladimir Oltean 		trap->block_id = VCAP_IS2;
149896ca08c0SVladimir Oltean 		trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
149996ca08c0SVladimir Oltean 		trap->lookup = 0;
150096ca08c0SVladimir Oltean 		trap->action.cpu_copy_ena = true;
150196ca08c0SVladimir Oltean 		trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
150296ca08c0SVladimir Oltean 		trap->action.port_mask = 0;
15039d75b881SVladimir Oltean 		trap->take_ts = take_ts;
1504e42bd4edSVladimir Oltean 		list_add_tail(&trap->trap_list, &ocelot->traps);
150596ca08c0SVladimir Oltean 		new = true;
150696ca08c0SVladimir Oltean 	}
150796ca08c0SVladimir Oltean 
150896ca08c0SVladimir Oltean 	trap->ingress_port_mask |= BIT(port);
150996ca08c0SVladimir Oltean 
151096ca08c0SVladimir Oltean 	if (new)
151196ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_add(ocelot, trap, NULL);
151296ca08c0SVladimir Oltean 	else
151396ca08c0SVladimir Oltean 		err = ocelot_vcap_filter_replace(ocelot, trap);
151496ca08c0SVladimir Oltean 	if (err) {
151596ca08c0SVladimir Oltean 		trap->ingress_port_mask &= ~BIT(port);
1516e42bd4edSVladimir Oltean 		if (!trap->ingress_port_mask) {
1517e42bd4edSVladimir Oltean 			list_del(&trap->trap_list);
151896ca08c0SVladimir Oltean 			kfree(trap);
1519e42bd4edSVladimir Oltean 		}
152096ca08c0SVladimir Oltean 		return err;
152196ca08c0SVladimir Oltean 	}
152296ca08c0SVladimir Oltean 
152396ca08c0SVladimir Oltean 	return 0;
152496ca08c0SVladimir Oltean }
152596ca08c0SVladimir Oltean 
1526b9bace6eSVladimir Oltean int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie)
152796ca08c0SVladimir Oltean {
152896ca08c0SVladimir Oltean 	struct ocelot_vcap_block *block_vcap_is2;
152996ca08c0SVladimir Oltean 	struct ocelot_vcap_filter *trap;
153096ca08c0SVladimir Oltean 
153196ca08c0SVladimir Oltean 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
153296ca08c0SVladimir Oltean 
153396ca08c0SVladimir Oltean 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
153496ca08c0SVladimir Oltean 						   false);
153596ca08c0SVladimir Oltean 	if (!trap)
153696ca08c0SVladimir Oltean 		return 0;
153796ca08c0SVladimir Oltean 
153896ca08c0SVladimir Oltean 	trap->ingress_port_mask &= ~BIT(port);
1539e42bd4edSVladimir Oltean 	if (!trap->ingress_port_mask) {
1540e42bd4edSVladimir Oltean 		list_del(&trap->trap_list);
1541e42bd4edSVladimir Oltean 
154296ca08c0SVladimir Oltean 		return ocelot_vcap_filter_del(ocelot, trap);
1543e42bd4edSVladimir Oltean 	}
154496ca08c0SVladimir Oltean 
154596ca08c0SVladimir Oltean 	return ocelot_vcap_filter_replace(ocelot, trap);
154696ca08c0SVladimir Oltean }
154796ca08c0SVladimir Oltean 
154896ca08c0SVladimir Oltean static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
154996ca08c0SVladimir Oltean {
1550c518afecSVladimir Oltean 	unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
155196ca08c0SVladimir Oltean 
15529d75b881SVladimir Oltean 	return ocelot_trap_add(ocelot, port, l2_cookie, true,
155396ca08c0SVladimir Oltean 			       ocelot_populate_l2_ptp_trap_key);
155496ca08c0SVladimir Oltean }
155596ca08c0SVladimir Oltean 
155696ca08c0SVladimir Oltean static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
155796ca08c0SVladimir Oltean {
1558c518afecSVladimir Oltean 	unsigned long l2_cookie = OCELOT_VCAP_IS2_L2_PTP_TRAP(ocelot);
155996ca08c0SVladimir Oltean 
156096ca08c0SVladimir Oltean 	return ocelot_trap_del(ocelot, port, l2_cookie);
156196ca08c0SVladimir Oltean }
156296ca08c0SVladimir Oltean 
156396ca08c0SVladimir Oltean static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
156496ca08c0SVladimir Oltean {
1565c518afecSVladimir Oltean 	unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
1566c518afecSVladimir Oltean 	unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
156796ca08c0SVladimir Oltean 	int err;
156896ca08c0SVladimir Oltean 
15699d75b881SVladimir Oltean 	err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie, true,
157096ca08c0SVladimir Oltean 			      ocelot_populate_ipv4_ptp_event_trap_key);
157196ca08c0SVladimir Oltean 	if (err)
157296ca08c0SVladimir Oltean 		return err;
157396ca08c0SVladimir Oltean 
15749d75b881SVladimir Oltean 	err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie, false,
157596ca08c0SVladimir Oltean 			      ocelot_populate_ipv4_ptp_general_trap_key);
157696ca08c0SVladimir Oltean 	if (err)
157796ca08c0SVladimir Oltean 		ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
157896ca08c0SVladimir Oltean 
157996ca08c0SVladimir Oltean 	return err;
158096ca08c0SVladimir Oltean }
158196ca08c0SVladimir Oltean 
158296ca08c0SVladimir Oltean static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
158396ca08c0SVladimir Oltean {
1584c518afecSVladimir Oltean 	unsigned long ipv4_gen_cookie = OCELOT_VCAP_IS2_IPV4_GEN_PTP_TRAP(ocelot);
1585c518afecSVladimir Oltean 	unsigned long ipv4_ev_cookie = OCELOT_VCAP_IS2_IPV4_EV_PTP_TRAP(ocelot);
158696ca08c0SVladimir Oltean 	int err;
158796ca08c0SVladimir Oltean 
158896ca08c0SVladimir Oltean 	err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
158996ca08c0SVladimir Oltean 	err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
159096ca08c0SVladimir Oltean 	return err;
159196ca08c0SVladimir Oltean }
159296ca08c0SVladimir Oltean 
159396ca08c0SVladimir Oltean static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
159496ca08c0SVladimir Oltean {
1595c518afecSVladimir Oltean 	unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
1596c518afecSVladimir Oltean 	unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
159796ca08c0SVladimir Oltean 	int err;
159896ca08c0SVladimir Oltean 
15999d75b881SVladimir Oltean 	err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie, true,
160096ca08c0SVladimir Oltean 			      ocelot_populate_ipv6_ptp_event_trap_key);
160196ca08c0SVladimir Oltean 	if (err)
160296ca08c0SVladimir Oltean 		return err;
160396ca08c0SVladimir Oltean 
16049d75b881SVladimir Oltean 	err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie, false,
160596ca08c0SVladimir Oltean 			      ocelot_populate_ipv6_ptp_general_trap_key);
160696ca08c0SVladimir Oltean 	if (err)
160796ca08c0SVladimir Oltean 		ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
160896ca08c0SVladimir Oltean 
160996ca08c0SVladimir Oltean 	return err;
161096ca08c0SVladimir Oltean }
161196ca08c0SVladimir Oltean 
161296ca08c0SVladimir Oltean static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
161396ca08c0SVladimir Oltean {
1614c518afecSVladimir Oltean 	unsigned long ipv6_gen_cookie = OCELOT_VCAP_IS2_IPV6_GEN_PTP_TRAP(ocelot);
1615c518afecSVladimir Oltean 	unsigned long ipv6_ev_cookie = OCELOT_VCAP_IS2_IPV6_EV_PTP_TRAP(ocelot);
161696ca08c0SVladimir Oltean 	int err;
161796ca08c0SVladimir Oltean 
161896ca08c0SVladimir Oltean 	err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
161996ca08c0SVladimir Oltean 	err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
162096ca08c0SVladimir Oltean 	return err;
162196ca08c0SVladimir Oltean }
162296ca08c0SVladimir Oltean 
162396ca08c0SVladimir Oltean static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
162496ca08c0SVladimir Oltean 				  bool l2, bool l4)
162596ca08c0SVladimir Oltean {
162696ca08c0SVladimir Oltean 	int err;
162796ca08c0SVladimir Oltean 
162896ca08c0SVladimir Oltean 	if (l2)
162996ca08c0SVladimir Oltean 		err = ocelot_l2_ptp_trap_add(ocelot, port);
163096ca08c0SVladimir Oltean 	else
163196ca08c0SVladimir Oltean 		err = ocelot_l2_ptp_trap_del(ocelot, port);
163296ca08c0SVladimir Oltean 	if (err)
163396ca08c0SVladimir Oltean 		return err;
163496ca08c0SVladimir Oltean 
163596ca08c0SVladimir Oltean 	if (l4) {
163696ca08c0SVladimir Oltean 		err = ocelot_ipv4_ptp_trap_add(ocelot, port);
163796ca08c0SVladimir Oltean 		if (err)
163896ca08c0SVladimir Oltean 			goto err_ipv4;
163996ca08c0SVladimir Oltean 
164096ca08c0SVladimir Oltean 		err = ocelot_ipv6_ptp_trap_add(ocelot, port);
164196ca08c0SVladimir Oltean 		if (err)
164296ca08c0SVladimir Oltean 			goto err_ipv6;
164396ca08c0SVladimir Oltean 	} else {
164496ca08c0SVladimir Oltean 		err = ocelot_ipv4_ptp_trap_del(ocelot, port);
164596ca08c0SVladimir Oltean 
164696ca08c0SVladimir Oltean 		err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
164796ca08c0SVladimir Oltean 	}
164896ca08c0SVladimir Oltean 	if (err)
164996ca08c0SVladimir Oltean 		return err;
165096ca08c0SVladimir Oltean 
165196ca08c0SVladimir Oltean 	return 0;
165296ca08c0SVladimir Oltean 
165396ca08c0SVladimir Oltean err_ipv6:
165496ca08c0SVladimir Oltean 	ocelot_ipv4_ptp_trap_del(ocelot, port);
165596ca08c0SVladimir Oltean err_ipv4:
165696ca08c0SVladimir Oltean 	if (l2)
165796ca08c0SVladimir Oltean 		ocelot_l2_ptp_trap_del(ocelot, port);
165896ca08c0SVladimir Oltean 	return err;
165996ca08c0SVladimir Oltean }
166096ca08c0SVladimir Oltean 
1661f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
16624e3b0468SAntoine Tenart {
16634e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
16644e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
16654e3b0468SAntoine Tenart }
1666f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
16674e3b0468SAntoine Tenart 
1668f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
16694e3b0468SAntoine Tenart {
1670306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
167196ca08c0SVladimir Oltean 	bool l2 = false, l4 = false;
16724e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
167396ca08c0SVladimir Oltean 	int err;
16744e3b0468SAntoine Tenart 
16754e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
16764e3b0468SAntoine Tenart 		return -EFAULT;
16774e3b0468SAntoine Tenart 
16784e3b0468SAntoine Tenart 	/* Tx type sanity check */
16794e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
16804e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1681306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
16824e3b0468SAntoine Tenart 		break;
16834e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
16844e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
16854e3b0468SAntoine Tenart 		 * need to update the origin time.
16864e3b0468SAntoine Tenart 		 */
1687306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
16884e3b0468SAntoine Tenart 		break;
16894e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1690306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
16914e3b0468SAntoine Tenart 		break;
16924e3b0468SAntoine Tenart 	default:
16934e3b0468SAntoine Tenart 		return -ERANGE;
16944e3b0468SAntoine Tenart 	}
16954e3b0468SAntoine Tenart 
16964e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
16974e3b0468SAntoine Tenart 
16984e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
16994e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
17004e3b0468SAntoine Tenart 		break;
17014e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
17024e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
17034e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
170496ca08c0SVladimir Oltean 		l4 = true;
170596ca08c0SVladimir Oltean 		break;
17064e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
17074e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
17084e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
170996ca08c0SVladimir Oltean 		l2 = true;
171096ca08c0SVladimir Oltean 		break;
17114e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
17124e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
17134e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
171496ca08c0SVladimir Oltean 		l2 = true;
171596ca08c0SVladimir Oltean 		l4 = true;
17164e3b0468SAntoine Tenart 		break;
17174e3b0468SAntoine Tenart 	default:
17184e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
17194e3b0468SAntoine Tenart 		return -ERANGE;
17204e3b0468SAntoine Tenart 	}
17214e3b0468SAntoine Tenart 
172296ca08c0SVladimir Oltean 	err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
17239c32950fSLv Ruyi 	if (err) {
17249c32950fSLv Ruyi 		mutex_unlock(&ocelot->ptp_lock);
172596ca08c0SVladimir Oltean 		return err;
17269c32950fSLv Ruyi 	}
172796ca08c0SVladimir Oltean 
172896ca08c0SVladimir Oltean 	if (l2 && l4)
172996ca08c0SVladimir Oltean 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
173096ca08c0SVladimir Oltean 	else if (l2)
173196ca08c0SVladimir Oltean 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
173296ca08c0SVladimir Oltean 	else if (l4)
173396ca08c0SVladimir Oltean 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
173496ca08c0SVladimir Oltean 	else
173596ca08c0SVladimir Oltean 		cfg.rx_filter = HWTSTAMP_FILTER_NONE;
173696ca08c0SVladimir Oltean 
17374e3b0468SAntoine Tenart 	/* Commit back the result & save it */
17384e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
17394e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
17404e3b0468SAntoine Tenart 
17414e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
17424e3b0468SAntoine Tenart }
1743f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
17444e3b0468SAntoine Tenart 
17455e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1746a556c76aSAlexandre Belloni {
1747a556c76aSAlexandre Belloni 	int i;
1748a556c76aSAlexandre Belloni 
1749a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1750a556c76aSAlexandre Belloni 		return;
1751a556c76aSAlexandre Belloni 
1752a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1753a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1754a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1755a556c76aSAlexandre Belloni }
17565e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1757a556c76aSAlexandre Belloni 
17587fbf6795SColin Foster /* Caller must hold &ocelot->stats_lock */
1759d87b1c08SColin Foster static int ocelot_port_update_stats(struct ocelot *ocelot, int port)
1760a556c76aSAlexandre Belloni {
1761d87b1c08SColin Foster 	unsigned int idx = port * ocelot->num_stats;
1762d87b1c08SColin Foster 	struct ocelot_stats_region *region;
1763d87b1c08SColin Foster 	int err, j;
1764a556c76aSAlexandre Belloni 
1765a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
1766e27d785eSColin Foster 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port), SYS_STAT_CFG);
1767a556c76aSAlexandre Belloni 
1768d87b1c08SColin Foster 	list_for_each_entry(region, &ocelot->stats_regions, node) {
1769d87b1c08SColin Foster 		err = ocelot_bulk_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1770d87b1c08SColin Foster 					   region->offset, region->buf,
1771d87b1c08SColin Foster 					   region->count);
1772d87b1c08SColin Foster 		if (err)
1773d87b1c08SColin Foster 			return err;
1774a556c76aSAlexandre Belloni 
1775d87b1c08SColin Foster 		for (j = 0; j < region->count; j++) {
1776d87b1c08SColin Foster 			u64 *stat = &ocelot->stats[idx + j];
1777d87b1c08SColin Foster 			u64 val = region->buf[j];
1778a556c76aSAlexandre Belloni 
1779d87b1c08SColin Foster 			if (val < (*stat & U32_MAX))
1780d87b1c08SColin Foster 				*stat += (u64)1 << 32;
1781a556c76aSAlexandre Belloni 
1782d87b1c08SColin Foster 			*stat = (*stat & ~(u64)U32_MAX) + val;
1783a556c76aSAlexandre Belloni 		}
1784d87b1c08SColin Foster 
1785d87b1c08SColin Foster 		idx += region->count;
1786d87b1c08SColin Foster 	}
1787d87b1c08SColin Foster 
1788d87b1c08SColin Foster 	return err;
17891e1caa97SClaudiu Manoil }
17901e1caa97SClaudiu Manoil 
17911e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
17921e1caa97SClaudiu Manoil {
17931e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
17941e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
17951e1caa97SClaudiu Manoil 					     stats_work);
1796d87b1c08SColin Foster 	int i, err;
17971e1caa97SClaudiu Manoil 
17987fbf6795SColin Foster 	mutex_lock(&ocelot->stats_lock);
1799d87b1c08SColin Foster 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1800d87b1c08SColin Foster 		err = ocelot_port_update_stats(ocelot, i);
1801d87b1c08SColin Foster 		if (err)
1802d87b1c08SColin Foster 			break;
1803d87b1c08SColin Foster 	}
18047fbf6795SColin Foster 	mutex_unlock(&ocelot->stats_lock);
18051e1caa97SClaudiu Manoil 
1806d87b1c08SColin Foster 	if (err)
1807d87b1c08SColin Foster 		dev_err(ocelot->dev, "Error %d updating ethtool stats\n",  err);
1808d87b1c08SColin Foster 
1809a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1810a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1811a556c76aSAlexandre Belloni }
1812a556c76aSAlexandre Belloni 
18135e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1814a556c76aSAlexandre Belloni {
1815d87b1c08SColin Foster 	int i, err;
1816a556c76aSAlexandre Belloni 
18177fbf6795SColin Foster 	mutex_lock(&ocelot->stats_lock);
18187fbf6795SColin Foster 
1819a556c76aSAlexandre Belloni 	/* check and update now */
1820d87b1c08SColin Foster 	err = ocelot_port_update_stats(ocelot, port);
1821a556c76aSAlexandre Belloni 
1822a556c76aSAlexandre Belloni 	/* Copy all counters */
1823a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1824004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
18257fbf6795SColin Foster 
18267fbf6795SColin Foster 	mutex_unlock(&ocelot->stats_lock);
1827d87b1c08SColin Foster 
1828d87b1c08SColin Foster 	if (err)
1829d87b1c08SColin Foster 		dev_err(ocelot->dev, "Error %d updating ethtool stats\n", err);
1830a556c76aSAlexandre Belloni }
18315e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1832a556c76aSAlexandre Belloni 
18335e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1834c7282d38SVladimir Oltean {
1835a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1836a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1837c7282d38SVladimir Oltean 
1838a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1839a556c76aSAlexandre Belloni }
18405e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1841a556c76aSAlexandre Belloni 
1842d87b1c08SColin Foster static int ocelot_prepare_stats_regions(struct ocelot *ocelot)
1843d87b1c08SColin Foster {
1844d87b1c08SColin Foster 	struct ocelot_stats_region *region = NULL;
1845d87b1c08SColin Foster 	unsigned int last;
1846d87b1c08SColin Foster 	int i;
1847d87b1c08SColin Foster 
1848d87b1c08SColin Foster 	INIT_LIST_HEAD(&ocelot->stats_regions);
1849d87b1c08SColin Foster 
1850d87b1c08SColin Foster 	for (i = 0; i < ocelot->num_stats; i++) {
1851d87b1c08SColin Foster 		if (region && ocelot->stats_layout[i].offset == last + 1) {
1852d87b1c08SColin Foster 			region->count++;
1853d87b1c08SColin Foster 		} else {
1854d87b1c08SColin Foster 			region = devm_kzalloc(ocelot->dev, sizeof(*region),
1855d87b1c08SColin Foster 					      GFP_KERNEL);
1856d87b1c08SColin Foster 			if (!region)
1857d87b1c08SColin Foster 				return -ENOMEM;
1858d87b1c08SColin Foster 
1859d87b1c08SColin Foster 			region->offset = ocelot->stats_layout[i].offset;
1860d87b1c08SColin Foster 			region->count = 1;
1861d87b1c08SColin Foster 			list_add_tail(&region->node, &ocelot->stats_regions);
1862d87b1c08SColin Foster 		}
1863d87b1c08SColin Foster 
1864d87b1c08SColin Foster 		last = ocelot->stats_layout[i].offset;
1865d87b1c08SColin Foster 	}
1866d87b1c08SColin Foster 
1867d87b1c08SColin Foster 	list_for_each_entry(region, &ocelot->stats_regions, node) {
1868d87b1c08SColin Foster 		region->buf = devm_kcalloc(ocelot->dev, region->count,
1869d87b1c08SColin Foster 					   sizeof(*region->buf), GFP_KERNEL);
1870d87b1c08SColin Foster 		if (!region->buf)
1871d87b1c08SColin Foster 			return -ENOMEM;
1872d87b1c08SColin Foster 	}
1873d87b1c08SColin Foster 
1874d87b1c08SColin Foster 	return 0;
1875d87b1c08SColin Foster }
1876d87b1c08SColin Foster 
18775e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1878c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1879c7282d38SVladimir Oltean {
18804e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
18814e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1882d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1883d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1884d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1885d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1886d2b09a8eSYangbo Lu 		return 0;
1887d2b09a8eSYangbo Lu 	}
18884e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
18894e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
18904e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
18914e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
18924e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
18934e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
18944e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
18954e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1896c49a35eeSVladimir Oltean 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1897c49a35eeSVladimir Oltean 			   BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
1898c49a35eeSVladimir Oltean 			   BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1899c49a35eeSVladimir Oltean 			   BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
19004e3b0468SAntoine Tenart 
19014e3b0468SAntoine Tenart 	return 0;
19024e3b0468SAntoine Tenart }
19035e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
19044e3b0468SAntoine Tenart 
1905a14e6b69SVladimir Oltean static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
1906b80af659SVladimir Oltean {
1907b80af659SVladimir Oltean 	u32 mask = 0;
1908b80af659SVladimir Oltean 	int port;
1909b80af659SVladimir Oltean 
1910*961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
1911*961d8b69SVladimir Oltean 
1912b80af659SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1913b80af659SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1914b80af659SVladimir Oltean 
1915b80af659SVladimir Oltean 		if (!ocelot_port)
1916b80af659SVladimir Oltean 			continue;
1917b80af659SVladimir Oltean 
1918a14e6b69SVladimir Oltean 		if (ocelot_port->bond == bond)
1919b80af659SVladimir Oltean 			mask |= BIT(port);
1920b80af659SVladimir Oltean 	}
1921b80af659SVladimir Oltean 
1922b80af659SVladimir Oltean 	return mask;
1923b80af659SVladimir Oltean }
1924b80af659SVladimir Oltean 
1925*961d8b69SVladimir Oltean /* The logical port number of a LAG is equal to the lowest numbered physical
1926*961d8b69SVladimir Oltean  * port ID present in that LAG. It may change if that port ever leaves the LAG.
1927*961d8b69SVladimir Oltean  */
1928*961d8b69SVladimir Oltean static int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond)
1929*961d8b69SVladimir Oltean {
1930*961d8b69SVladimir Oltean 	int bond_mask = ocelot_get_bond_mask(ocelot, bond);
1931*961d8b69SVladimir Oltean 
1932*961d8b69SVladimir Oltean 	if (!bond_mask)
1933*961d8b69SVladimir Oltean 		return -ENOENT;
1934*961d8b69SVladimir Oltean 
1935*961d8b69SVladimir Oltean 	return __ffs(bond_mask);
1936*961d8b69SVladimir Oltean }
1937*961d8b69SVladimir Oltean 
19388abe1970SVladimir Oltean u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
1939df291e54SVladimir Oltean {
1940acc64f52SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1941a8bd9fa5SVladimir Oltean 	const struct net_device *bridge;
1942df291e54SVladimir Oltean 	u32 mask = 0;
1943df291e54SVladimir Oltean 	int port;
1944df291e54SVladimir Oltean 
1945a8bd9fa5SVladimir Oltean 	if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
1946a8bd9fa5SVladimir Oltean 		return 0;
1947a8bd9fa5SVladimir Oltean 
1948a8bd9fa5SVladimir Oltean 	bridge = ocelot_port->bridge;
1949a8bd9fa5SVladimir Oltean 	if (!bridge)
1950acc64f52SVladimir Oltean 		return 0;
1951acc64f52SVladimir Oltean 
1952df291e54SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1953acc64f52SVladimir Oltean 		ocelot_port = ocelot->ports[port];
1954df291e54SVladimir Oltean 
1955df291e54SVladimir Oltean 		if (!ocelot_port)
1956df291e54SVladimir Oltean 			continue;
1957df291e54SVladimir Oltean 
1958df291e54SVladimir Oltean 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1959df291e54SVladimir Oltean 		    ocelot_port->bridge == bridge)
1960df291e54SVladimir Oltean 			mask |= BIT(port);
1961df291e54SVladimir Oltean 	}
1962df291e54SVladimir Oltean 
1963df291e54SVladimir Oltean 	return mask;
1964df291e54SVladimir Oltean }
19658abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
1966df291e54SVladimir Oltean 
19678abe1970SVladimir Oltean u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
19689b521250SVladimir Oltean {
1969e21268efSVladimir Oltean 	u32 mask = 0;
19709b521250SVladimir Oltean 	int port;
19719b521250SVladimir Oltean 
1972e21268efSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1973e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1974e21268efSVladimir Oltean 
1975e21268efSVladimir Oltean 		if (!ocelot_port)
1976e21268efSVladimir Oltean 			continue;
1977e21268efSVladimir Oltean 
1978e21268efSVladimir Oltean 		if (ocelot_port->is_dsa_8021q_cpu)
1979e21268efSVladimir Oltean 			mask |= BIT(port);
1980e21268efSVladimir Oltean 	}
1981e21268efSVladimir Oltean 
1982e21268efSVladimir Oltean 	return mask;
1983e21268efSVladimir Oltean }
19848abe1970SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_get_dsa_8021q_cpu_mask);
1985e21268efSVladimir Oltean 
19868abe1970SVladimir Oltean void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
1987e21268efSVladimir Oltean {
1988e21268efSVladimir Oltean 	unsigned long cpu_fwd_mask;
1989e21268efSVladimir Oltean 	int port;
1990e21268efSVladimir Oltean 
19918abe1970SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
19928abe1970SVladimir Oltean 
19938abe1970SVladimir Oltean 	/* If cut-through forwarding is supported, update the masks before a
19948abe1970SVladimir Oltean 	 * port joins the forwarding domain, to avoid potential underruns if it
19958abe1970SVladimir Oltean 	 * has the highest speed from the new domain.
19968abe1970SVladimir Oltean 	 */
19978abe1970SVladimir Oltean 	if (joining && ocelot->ops->cut_through_fwd)
19988abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
19998abe1970SVladimir Oltean 
2000e21268efSVladimir Oltean 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
2001e21268efSVladimir Oltean 	 * regular forwarding path of the front ports regardless of whether
2002e21268efSVladimir Oltean 	 * those are bridged or standalone.
2003e21268efSVladimir Oltean 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
2004e21268efSVladimir Oltean 	 * the hardware-based CPU port module can be a destination for packets
2005e21268efSVladimir Oltean 	 * even if it isn't part of PGID_SRC.
2006e21268efSVladimir Oltean 	 */
2007e21268efSVladimir Oltean 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
2008e21268efSVladimir Oltean 
20099b521250SVladimir Oltean 	/* Apply FWD mask. The loop is needed to add/remove the current port as
20109b521250SVladimir Oltean 	 * a source for the other ports.
20119b521250SVladimir Oltean 	 */
20129b521250SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2013e21268efSVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2014e21268efSVladimir Oltean 		unsigned long mask;
2015e21268efSVladimir Oltean 
2016e21268efSVladimir Oltean 		if (!ocelot_port) {
2017e21268efSVladimir Oltean 			/* Unused ports can't send anywhere */
2018e21268efSVladimir Oltean 			mask = 0;
2019e21268efSVladimir Oltean 		} else if (ocelot_port->is_dsa_8021q_cpu) {
2020e21268efSVladimir Oltean 			/* The DSA tag_8021q CPU ports need to be able to
2021e21268efSVladimir Oltean 			 * forward packets to all other ports except for
2022e21268efSVladimir Oltean 			 * themselves
2023e21268efSVladimir Oltean 			 */
2024e21268efSVladimir Oltean 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
2025e21268efSVladimir Oltean 			mask &= ~cpu_fwd_mask;
2026df291e54SVladimir Oltean 		} else if (ocelot_port->bridge) {
2027528d3f19SVladimir Oltean 			struct net_device *bond = ocelot_port->bond;
20289b521250SVladimir Oltean 
2029a8bd9fa5SVladimir Oltean 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
2030c1930148SVladimir Oltean 			mask |= cpu_fwd_mask;
2031df291e54SVladimir Oltean 			mask &= ~BIT(port);
2032a14e6b69SVladimir Oltean 			if (bond)
2033a14e6b69SVladimir Oltean 				mask &= ~ocelot_get_bond_mask(ocelot, bond);
20349b521250SVladimir Oltean 		} else {
2035e21268efSVladimir Oltean 			/* Standalone ports forward only to DSA tag_8021q CPU
2036e21268efSVladimir Oltean 			 * ports (if those exist), or to the hardware CPU port
2037e21268efSVladimir Oltean 			 * module otherwise.
2038e21268efSVladimir Oltean 			 */
2039e21268efSVladimir Oltean 			mask = cpu_fwd_mask;
2040e21268efSVladimir Oltean 		}
2041e21268efSVladimir Oltean 
2042e21268efSVladimir Oltean 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
20439b521250SVladimir Oltean 	}
20448abe1970SVladimir Oltean 
20458abe1970SVladimir Oltean 	/* If cut-through forwarding is supported and a port is leaving, there
20468abe1970SVladimir Oltean 	 * is a chance that cut-through was disabled on the other ports due to
20478abe1970SVladimir Oltean 	 * the port which is leaving (it has a higher link speed). We need to
20488abe1970SVladimir Oltean 	 * update the cut-through masks of the remaining ports no earlier than
20498abe1970SVladimir Oltean 	 * after the port has left, to prevent underruns from happening between
20508abe1970SVladimir Oltean 	 * the cut-through update and the forwarding domain update.
20518abe1970SVladimir Oltean 	 */
20528abe1970SVladimir Oltean 	if (!joining && ocelot->ops->cut_through_fwd)
20538abe1970SVladimir Oltean 		ocelot->ops->cut_through_fwd(ocelot);
20549b521250SVladimir Oltean }
2055e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
20569b521250SVladimir Oltean 
20575e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
2058a556c76aSAlexandre Belloni {
2059421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2060df291e54SVladimir Oltean 	u32 learn_ena = 0;
2061a556c76aSAlexandre Belloni 
20628abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
20638abe1970SVladimir Oltean 
2064df291e54SVladimir Oltean 	ocelot_port->stp_state = state;
2065a556c76aSAlexandre Belloni 
2066df291e54SVladimir Oltean 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
2067df291e54SVladimir Oltean 	    ocelot_port->learn_ena)
2068df291e54SVladimir Oltean 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
2069a556c76aSAlexandre Belloni 
2070df291e54SVladimir Oltean 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
2071df291e54SVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2072a556c76aSAlexandre Belloni 
20738abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
20748abe1970SVladimir Oltean 
20758abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2076a556c76aSAlexandre Belloni }
20775e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
2078a556c76aSAlexandre Belloni 
20795e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
20804bda1415SVladimir Oltean {
2081c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
2082c0d7eccbSVladimir Oltean 
2083c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
2084c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
2085c0d7eccbSVladimir Oltean 	 */
2086c0d7eccbSVladimir Oltean 	if (!age_period)
2087c0d7eccbSVladimir Oltean 		age_period = 1;
2088c0d7eccbSVladimir Oltean 
2089c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
2090a556c76aSAlexandre Belloni }
20915e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
2092a556c76aSAlexandre Belloni 
2093a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
2094a556c76aSAlexandre Belloni 						     const unsigned char *addr,
2095a556c76aSAlexandre Belloni 						     u16 vid)
2096a556c76aSAlexandre Belloni {
2097a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
2098a556c76aSAlexandre Belloni 
2099a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
2100a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
2101a556c76aSAlexandre Belloni 			return mc;
2102a556c76aSAlexandre Belloni 	}
2103a556c76aSAlexandre Belloni 
2104a556c76aSAlexandre Belloni 	return NULL;
2105a556c76aSAlexandre Belloni }
2106a556c76aSAlexandre Belloni 
21079403c158SVladimir Oltean static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
21089403c158SVladimir Oltean {
21099403c158SVladimir Oltean 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
21109403c158SVladimir Oltean 		return ENTRYTYPE_MACv4;
21119403c158SVladimir Oltean 	if (addr[0] == 0x33 && addr[1] == 0x33)
21129403c158SVladimir Oltean 		return ENTRYTYPE_MACv6;
21137c313143SVladimir Oltean 	return ENTRYTYPE_LOCKED;
21149403c158SVladimir Oltean }
21159403c158SVladimir Oltean 
2116e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
2117e5d1f896SVladimir Oltean 					     unsigned long ports)
2118e5d1f896SVladimir Oltean {
2119e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2120e5d1f896SVladimir Oltean 
2121e5d1f896SVladimir Oltean 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
2122e5d1f896SVladimir Oltean 	if (!pgid)
2123e5d1f896SVladimir Oltean 		return ERR_PTR(-ENOMEM);
2124e5d1f896SVladimir Oltean 
2125e5d1f896SVladimir Oltean 	pgid->ports = ports;
2126e5d1f896SVladimir Oltean 	pgid->index = index;
2127e5d1f896SVladimir Oltean 	refcount_set(&pgid->refcount, 1);
2128e5d1f896SVladimir Oltean 	list_add_tail(&pgid->list, &ocelot->pgids);
2129e5d1f896SVladimir Oltean 
2130e5d1f896SVladimir Oltean 	return pgid;
2131e5d1f896SVladimir Oltean }
2132e5d1f896SVladimir Oltean 
2133e5d1f896SVladimir Oltean static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
2134e5d1f896SVladimir Oltean {
2135e5d1f896SVladimir Oltean 	if (!refcount_dec_and_test(&pgid->refcount))
2136e5d1f896SVladimir Oltean 		return;
2137e5d1f896SVladimir Oltean 
2138e5d1f896SVladimir Oltean 	list_del(&pgid->list);
2139e5d1f896SVladimir Oltean 	kfree(pgid);
2140e5d1f896SVladimir Oltean }
2141e5d1f896SVladimir Oltean 
2142e5d1f896SVladimir Oltean static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
2143bb8d53fdSVladimir Oltean 					       const struct ocelot_multicast *mc)
21449403c158SVladimir Oltean {
2145e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2146e5d1f896SVladimir Oltean 	int index;
21479403c158SVladimir Oltean 
21489403c158SVladimir Oltean 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
21499403c158SVladimir Oltean 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
21509403c158SVladimir Oltean 	 * destination mask table (PGID), the destination set is programmed as
21519403c158SVladimir Oltean 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
21529403c158SVladimir Oltean 	 */
2153bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
2154bb8d53fdSVladimir Oltean 	    mc->entry_type == ENTRYTYPE_MACv6)
2155e5d1f896SVladimir Oltean 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
21569403c158SVladimir Oltean 
2157e5d1f896SVladimir Oltean 	list_for_each_entry(pgid, &ocelot->pgids, list) {
2158e5d1f896SVladimir Oltean 		/* When searching for a nonreserved multicast PGID, ignore the
2159e5d1f896SVladimir Oltean 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
2160e5d1f896SVladimir Oltean 		 */
2161e5d1f896SVladimir Oltean 		if (pgid->index && pgid->ports == mc->ports) {
2162e5d1f896SVladimir Oltean 			refcount_inc(&pgid->refcount);
2163e5d1f896SVladimir Oltean 			return pgid;
2164e5d1f896SVladimir Oltean 		}
2165e5d1f896SVladimir Oltean 	}
2166e5d1f896SVladimir Oltean 
2167e5d1f896SVladimir Oltean 	/* Search for a free index in the nonreserved multicast PGID area */
2168e5d1f896SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
21699403c158SVladimir Oltean 		bool used = false;
21709403c158SVladimir Oltean 
2171e5d1f896SVladimir Oltean 		list_for_each_entry(pgid, &ocelot->pgids, list) {
2172e5d1f896SVladimir Oltean 			if (pgid->index == index) {
21739403c158SVladimir Oltean 				used = true;
21749403c158SVladimir Oltean 				break;
21759403c158SVladimir Oltean 			}
21769403c158SVladimir Oltean 		}
21779403c158SVladimir Oltean 
21789403c158SVladimir Oltean 		if (!used)
2179e5d1f896SVladimir Oltean 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
21809403c158SVladimir Oltean 	}
21819403c158SVladimir Oltean 
2182e5d1f896SVladimir Oltean 	return ERR_PTR(-ENOSPC);
21839403c158SVladimir Oltean }
21849403c158SVladimir Oltean 
21859403c158SVladimir Oltean static void ocelot_encode_ports_to_mdb(unsigned char *addr,
2186bb8d53fdSVladimir Oltean 				       struct ocelot_multicast *mc)
21879403c158SVladimir Oltean {
2188ebbd860eSVladimir Oltean 	ether_addr_copy(addr, mc->addr);
21899403c158SVladimir Oltean 
2190bb8d53fdSVladimir Oltean 	if (mc->entry_type == ENTRYTYPE_MACv4) {
21919403c158SVladimir Oltean 		addr[0] = 0;
21929403c158SVladimir Oltean 		addr[1] = mc->ports >> 8;
21939403c158SVladimir Oltean 		addr[2] = mc->ports & 0xff;
2194bb8d53fdSVladimir Oltean 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
21959403c158SVladimir Oltean 		addr[0] = mc->ports >> 8;
21969403c158SVladimir Oltean 		addr[1] = mc->ports & 0xff;
21979403c158SVladimir Oltean 	}
21989403c158SVladimir Oltean }
21999403c158SVladimir Oltean 
2200209edf95SVladimir Oltean int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
2201209edf95SVladimir Oltean 			const struct switchdev_obj_port_mdb *mdb)
2202a556c76aSAlexandre Belloni {
2203a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
2204004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
2205e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2206a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
2207a556c76aSAlexandre Belloni 
2208471beb11SVladimir Oltean 	if (port == ocelot->npi)
2209471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
2210471beb11SVladimir Oltean 
2211a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2212a556c76aSAlexandre Belloni 	if (!mc) {
2213728e69aeSVladimir Oltean 		/* New entry */
2214bb8d53fdSVladimir Oltean 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
2215bb8d53fdSVladimir Oltean 		if (!mc)
2216bb8d53fdSVladimir Oltean 			return -ENOMEM;
2217bb8d53fdSVladimir Oltean 
2218bb8d53fdSVladimir Oltean 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
2219bb8d53fdSVladimir Oltean 		ether_addr_copy(mc->addr, mdb->addr);
2220bb8d53fdSVladimir Oltean 		mc->vid = vid;
2221bb8d53fdSVladimir Oltean 
2222a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
2223728e69aeSVladimir Oltean 	} else {
2224e5d1f896SVladimir Oltean 		/* Existing entry. Clean up the current port mask from
2225e5d1f896SVladimir Oltean 		 * hardware now, because we'll be modifying it.
2226e5d1f896SVladimir Oltean 		 */
2227e5d1f896SVladimir Oltean 		ocelot_pgid_free(ocelot, mc->pgid);
2228bb8d53fdSVladimir Oltean 		ocelot_encode_ports_to_mdb(addr, mc);
2229a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
2230a556c76aSAlexandre Belloni 	}
2231a556c76aSAlexandre Belloni 
2232004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
2233e5d1f896SVladimir Oltean 
2234e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
2235e5d1f896SVladimir Oltean 	if (IS_ERR(pgid)) {
2236e5d1f896SVladimir Oltean 		dev_err(ocelot->dev,
2237e5d1f896SVladimir Oltean 			"Cannot allocate PGID for mdb %pM vid %d\n",
2238e5d1f896SVladimir Oltean 			mc->addr, mc->vid);
2239e5d1f896SVladimir Oltean 		devm_kfree(ocelot->dev, mc);
2240e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
2241e5d1f896SVladimir Oltean 	}
2242e5d1f896SVladimir Oltean 	mc->pgid = pgid;
2243e5d1f896SVladimir Oltean 
2244bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2245a556c76aSAlexandre Belloni 
2246e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
2247e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
2248e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2249e5d1f896SVladimir Oltean 				 pgid->index);
2250e5d1f896SVladimir Oltean 
2251e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
2252bb8d53fdSVladimir Oltean 				 mc->entry_type);
2253a556c76aSAlexandre Belloni }
2254209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_add);
2255a556c76aSAlexandre Belloni 
2256209edf95SVladimir Oltean int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
2257a556c76aSAlexandre Belloni 			const struct switchdev_obj_port_mdb *mdb)
2258a556c76aSAlexandre Belloni {
2259a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
2260004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
2261e5d1f896SVladimir Oltean 	struct ocelot_pgid *pgid;
2262a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
2263a556c76aSAlexandre Belloni 
2264471beb11SVladimir Oltean 	if (port == ocelot->npi)
2265471beb11SVladimir Oltean 		port = ocelot->num_phys_ports;
2266471beb11SVladimir Oltean 
2267a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2268a556c76aSAlexandre Belloni 	if (!mc)
2269a556c76aSAlexandre Belloni 		return -ENOENT;
2270a556c76aSAlexandre Belloni 
2271bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2272a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
2273a556c76aSAlexandre Belloni 
2274e5d1f896SVladimir Oltean 	ocelot_pgid_free(ocelot, mc->pgid);
2275004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
2276a556c76aSAlexandre Belloni 	if (!mc->ports) {
2277a556c76aSAlexandre Belloni 		list_del(&mc->list);
2278a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
2279a556c76aSAlexandre Belloni 		return 0;
2280a556c76aSAlexandre Belloni 	}
2281a556c76aSAlexandre Belloni 
2282e5d1f896SVladimir Oltean 	/* We have a PGID with fewer ports now */
2283e5d1f896SVladimir Oltean 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
2284e5d1f896SVladimir Oltean 	if (IS_ERR(pgid))
2285e5d1f896SVladimir Oltean 		return PTR_ERR(pgid);
2286e5d1f896SVladimir Oltean 	mc->pgid = pgid;
2287e5d1f896SVladimir Oltean 
2288bb8d53fdSVladimir Oltean 	ocelot_encode_ports_to_mdb(addr, mc);
2289a556c76aSAlexandre Belloni 
2290e5d1f896SVladimir Oltean 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
2291e5d1f896SVladimir Oltean 	    mc->entry_type != ENTRYTYPE_MACv6)
2292e5d1f896SVladimir Oltean 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2293e5d1f896SVladimir Oltean 				 pgid->index);
2294e5d1f896SVladimir Oltean 
2295e5d1f896SVladimir Oltean 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
2296bb8d53fdSVladimir Oltean 				 mc->entry_type);
2297a556c76aSAlexandre Belloni }
2298209edf95SVladimir Oltean EXPORT_SYMBOL(ocelot_port_mdb_del);
2299a556c76aSAlexandre Belloni 
2300e4bd44e8SVladimir Oltean void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
2301a556c76aSAlexandre Belloni 			     struct net_device *bridge)
2302a556c76aSAlexandre Belloni {
2303df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2304a556c76aSAlexandre Belloni 
23058abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
23068abe1970SVladimir Oltean 
2307df291e54SVladimir Oltean 	ocelot_port->bridge = bridge;
2308a556c76aSAlexandre Belloni 
23098abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
23108abe1970SVladimir Oltean 
23118abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2312a556c76aSAlexandre Belloni }
23135e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
2314a556c76aSAlexandre Belloni 
2315e4bd44e8SVladimir Oltean void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
2316a556c76aSAlexandre Belloni 			      struct net_device *bridge)
2317a556c76aSAlexandre Belloni {
2318df291e54SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
23192e554a7aSVladimir Oltean 
23208abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
23218abe1970SVladimir Oltean 
2322df291e54SVladimir Oltean 	ocelot_port->bridge = NULL;
23237142529fSAntoine Tenart 
2324d4004422SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, NULL);
23250da1a1c4SVladimir Oltean 	ocelot_port_manage_port_tag(ocelot, port);
23268abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
23278abe1970SVladimir Oltean 
23288abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2329a556c76aSAlexandre Belloni }
23305e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
2331a556c76aSAlexandre Belloni 
2332dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
2333dc96ee37SAlexandre Belloni {
2334528d3f19SVladimir Oltean 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
2335dc96ee37SAlexandre Belloni 	int i, port, lag;
2336dc96ee37SAlexandre Belloni 
2337dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
233896b029b0SVladimir Oltean 	for_each_unicast_dest_pgid(ocelot, port)
2339dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2340dc96ee37SAlexandre Belloni 
234196b029b0SVladimir Oltean 	for_each_aggr_pgid(ocelot, i)
2342dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
2343dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
2344dc96ee37SAlexandre Belloni 
2345528d3f19SVladimir Oltean 	/* The visited ports bitmask holds the list of ports offloading any
2346528d3f19SVladimir Oltean 	 * bonding interface. Initially we mark all these ports as unvisited,
2347528d3f19SVladimir Oltean 	 * then every time we visit a port in this bitmask, we know that it is
2348528d3f19SVladimir Oltean 	 * the lowest numbered port, i.e. the one whose logical ID == physical
2349528d3f19SVladimir Oltean 	 * port ID == LAG ID. So we mark as visited all further ports in the
2350528d3f19SVladimir Oltean 	 * bitmask that are offloading the same bonding interface. This way,
2351528d3f19SVladimir Oltean 	 * we set up the aggregation PGIDs only once per bonding interface.
2352528d3f19SVladimir Oltean 	 */
2353528d3f19SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2354528d3f19SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2355528d3f19SVladimir Oltean 
2356528d3f19SVladimir Oltean 		if (!ocelot_port || !ocelot_port->bond)
2357528d3f19SVladimir Oltean 			continue;
2358528d3f19SVladimir Oltean 
2359528d3f19SVladimir Oltean 		visited &= ~BIT(port);
2360528d3f19SVladimir Oltean 	}
2361528d3f19SVladimir Oltean 
2362528d3f19SVladimir Oltean 	/* Now, set PGIDs for each active LAG */
2363dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
2364528d3f19SVladimir Oltean 		struct net_device *bond = ocelot->ports[lag]->bond;
236523ca3b72SVladimir Oltean 		int num_active_ports = 0;
2366dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
2367dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
2368dc96ee37SAlexandre Belloni 
2369528d3f19SVladimir Oltean 		if (!bond || (visited & BIT(lag)))
2370dc96ee37SAlexandre Belloni 			continue;
2371dc96ee37SAlexandre Belloni 
2372a14e6b69SVladimir Oltean 		bond_mask = ocelot_get_bond_mask(ocelot, bond);
2373528d3f19SVladimir Oltean 
2374dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
2375a14e6b69SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2376a14e6b69SVladimir Oltean 
2377dc96ee37SAlexandre Belloni 			// Destination mask
2378dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
2379dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
2380a14e6b69SVladimir Oltean 
2381a14e6b69SVladimir Oltean 			if (ocelot_port->lag_tx_active)
238223ca3b72SVladimir Oltean 				aggr_idx[num_active_ports++] = port;
2383dc96ee37SAlexandre Belloni 		}
2384dc96ee37SAlexandre Belloni 
238596b029b0SVladimir Oltean 		for_each_aggr_pgid(ocelot, i) {
2386dc96ee37SAlexandre Belloni 			u32 ac;
2387dc96ee37SAlexandre Belloni 
2388dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
2389dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
239023ca3b72SVladimir Oltean 			/* Don't do division by zero if there was no active
239123ca3b72SVladimir Oltean 			 * port. Just make all aggregation codes zero.
239223ca3b72SVladimir Oltean 			 */
239323ca3b72SVladimir Oltean 			if (num_active_ports)
239423ca3b72SVladimir Oltean 				ac |= BIT(aggr_idx[i % num_active_ports]);
2395dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
2396dc96ee37SAlexandre Belloni 		}
2397528d3f19SVladimir Oltean 
2398528d3f19SVladimir Oltean 		/* Mark all ports in the same LAG as visited to avoid applying
2399528d3f19SVladimir Oltean 		 * the same config again.
2400528d3f19SVladimir Oltean 		 */
2401528d3f19SVladimir Oltean 		for (port = lag; port < ocelot->num_phys_ports; port++) {
2402528d3f19SVladimir Oltean 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2403528d3f19SVladimir Oltean 
2404528d3f19SVladimir Oltean 			if (!ocelot_port)
2405528d3f19SVladimir Oltean 				continue;
2406528d3f19SVladimir Oltean 
2407528d3f19SVladimir Oltean 			if (ocelot_port->bond == bond)
2408528d3f19SVladimir Oltean 				visited |= BIT(port);
2409528d3f19SVladimir Oltean 		}
2410dc96ee37SAlexandre Belloni 	}
2411dc96ee37SAlexandre Belloni }
2412dc96ee37SAlexandre Belloni 
24132527f2e8SVladimir Oltean /* When offloading a bonding interface, the switch ports configured under the
24142527f2e8SVladimir Oltean  * same bond must have the same logical port ID, equal to the physical port ID
24152527f2e8SVladimir Oltean  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
24162527f2e8SVladimir Oltean  * bridged mode, each port has a logical port ID equal to its physical port ID.
24172527f2e8SVladimir Oltean  */
24182527f2e8SVladimir Oltean static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
2419dc96ee37SAlexandre Belloni {
24202527f2e8SVladimir Oltean 	int port;
2421dc96ee37SAlexandre Belloni 
24222527f2e8SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
24232527f2e8SVladimir Oltean 		struct ocelot_port *ocelot_port = ocelot->ports[port];
24242527f2e8SVladimir Oltean 		struct net_device *bond;
2425dc96ee37SAlexandre Belloni 
24262527f2e8SVladimir Oltean 		if (!ocelot_port)
24272527f2e8SVladimir Oltean 			continue;
2428dc96ee37SAlexandre Belloni 
24292527f2e8SVladimir Oltean 		bond = ocelot_port->bond;
24302527f2e8SVladimir Oltean 		if (bond) {
2431*961d8b69SVladimir Oltean 			int lag = ocelot_bond_get_id(ocelot, bond);
24322527f2e8SVladimir Oltean 
24332527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
2434dc96ee37SAlexandre Belloni 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
24352527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
24362527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
24372527f2e8SVladimir Oltean 		} else {
24382527f2e8SVladimir Oltean 			ocelot_rmw_gix(ocelot,
24392527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
24402527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
24412527f2e8SVladimir Oltean 				       ANA_PORT_PORT_CFG, port);
24422527f2e8SVladimir Oltean 		}
2443dc96ee37SAlexandre Belloni 	}
2444dc96ee37SAlexandre Belloni }
2445dc96ee37SAlexandre Belloni 
2446*961d8b69SVladimir Oltean /* Documentation for PORTID_VAL says:
2447*961d8b69SVladimir Oltean  *     Logical port number for front port. If port is not a member of a LLAG,
2448*961d8b69SVladimir Oltean  *     then PORTID must be set to the physical port number.
2449*961d8b69SVladimir Oltean  *     If port is a member of a LLAG, then PORTID must be set to the common
2450*961d8b69SVladimir Oltean  *     PORTID_VAL used for all member ports of the LLAG.
2451*961d8b69SVladimir Oltean  *     The value must not exceed the number of physical ports on the device.
2452*961d8b69SVladimir Oltean  *
2453*961d8b69SVladimir Oltean  * This means we have little choice but to migrate FDB entries pointing towards
2454*961d8b69SVladimir Oltean  * a logical port when that changes.
2455*961d8b69SVladimir Oltean  */
2456*961d8b69SVladimir Oltean static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot,
2457*961d8b69SVladimir Oltean 				    struct net_device *bond,
2458*961d8b69SVladimir Oltean 				    int lag)
2459*961d8b69SVladimir Oltean {
2460*961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2461*961d8b69SVladimir Oltean 	int err;
2462*961d8b69SVladimir Oltean 
2463*961d8b69SVladimir Oltean 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2464*961d8b69SVladimir Oltean 
2465*961d8b69SVladimir Oltean 	list_for_each_entry(fdb, &ocelot->lag_fdbs, list) {
2466*961d8b69SVladimir Oltean 		if (fdb->bond != bond)
2467*961d8b69SVladimir Oltean 			continue;
2468*961d8b69SVladimir Oltean 
2469*961d8b69SVladimir Oltean 		err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid);
2470*961d8b69SVladimir Oltean 		if (err) {
2471*961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2472*961d8b69SVladimir Oltean 				"failed to delete LAG %s FDB %pM vid %d: %pe\n",
2473*961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2474*961d8b69SVladimir Oltean 		}
2475*961d8b69SVladimir Oltean 
2476*961d8b69SVladimir Oltean 		err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid,
2477*961d8b69SVladimir Oltean 					ENTRYTYPE_LOCKED);
2478*961d8b69SVladimir Oltean 		if (err) {
2479*961d8b69SVladimir Oltean 			dev_err(ocelot->dev,
2480*961d8b69SVladimir Oltean 				"failed to migrate LAG %s FDB %pM vid %d: %pe\n",
2481*961d8b69SVladimir Oltean 				bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
2482*961d8b69SVladimir Oltean 		}
2483*961d8b69SVladimir Oltean 	}
2484*961d8b69SVladimir Oltean }
2485*961d8b69SVladimir Oltean 
24869c90eea3SVladimir Oltean int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2487583cbbe3SVladimir Oltean 			 struct net_device *bond,
2488583cbbe3SVladimir Oltean 			 struct netdev_lag_upper_info *info)
2489dc96ee37SAlexandre Belloni {
2490583cbbe3SVladimir Oltean 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
2491583cbbe3SVladimir Oltean 		return -EOPNOTSUPP;
2492583cbbe3SVladimir Oltean 
24938abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
24948abe1970SVladimir Oltean 
2495b80af659SVladimir Oltean 	ocelot->ports[port]->bond = bond;
2496dc96ee37SAlexandre Belloni 
24972527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
24988abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2499dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
2500dc96ee37SAlexandre Belloni 
25018abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
25028abe1970SVladimir Oltean 
2503dc96ee37SAlexandre Belloni 	return 0;
2504dc96ee37SAlexandre Belloni }
25059c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_join);
2506dc96ee37SAlexandre Belloni 
25079c90eea3SVladimir Oltean void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2508dc96ee37SAlexandre Belloni 			   struct net_device *bond)
2509dc96ee37SAlexandre Belloni {
2510*961d8b69SVladimir Oltean 	int old_lag_id, new_lag_id;
2511*961d8b69SVladimir Oltean 
25128abe1970SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
25138abe1970SVladimir Oltean 
2514*961d8b69SVladimir Oltean 	old_lag_id = ocelot_bond_get_id(ocelot, bond);
2515*961d8b69SVladimir Oltean 
2516b80af659SVladimir Oltean 	ocelot->ports[port]->bond = NULL;
2517b80af659SVladimir Oltean 
25182527f2e8SVladimir Oltean 	ocelot_setup_logical_port_ids(ocelot);
25198abe1970SVladimir Oltean 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2520dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
25218abe1970SVladimir Oltean 
2522*961d8b69SVladimir Oltean 	new_lag_id = ocelot_bond_get_id(ocelot, bond);
2523*961d8b69SVladimir Oltean 
2524*961d8b69SVladimir Oltean 	if (new_lag_id >= 0 && old_lag_id != new_lag_id)
2525*961d8b69SVladimir Oltean 		ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id);
2526*961d8b69SVladimir Oltean 
25278abe1970SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2528dc96ee37SAlexandre Belloni }
25299c90eea3SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_leave);
25300e332c85SPetr Machata 
253123ca3b72SVladimir Oltean void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
253223ca3b72SVladimir Oltean {
253323ca3b72SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
253423ca3b72SVladimir Oltean 
2535*961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2536*961d8b69SVladimir Oltean 
253723ca3b72SVladimir Oltean 	ocelot_port->lag_tx_active = lag_tx_active;
253823ca3b72SVladimir Oltean 
253923ca3b72SVladimir Oltean 	/* Rebalance the LAGs */
254023ca3b72SVladimir Oltean 	ocelot_set_aggr_pgids(ocelot);
2541*961d8b69SVladimir Oltean 
2542*961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
254323ca3b72SVladimir Oltean }
254423ca3b72SVladimir Oltean EXPORT_SYMBOL(ocelot_port_lag_change);
254523ca3b72SVladimir Oltean 
2546*961d8b69SVladimir Oltean int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond,
2547*961d8b69SVladimir Oltean 		       const unsigned char *addr, u16 vid)
2548*961d8b69SVladimir Oltean {
2549*961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb;
2550*961d8b69SVladimir Oltean 	int lag, err;
2551*961d8b69SVladimir Oltean 
2552*961d8b69SVladimir Oltean 	fdb = kzalloc(sizeof(*fdb), GFP_KERNEL);
2553*961d8b69SVladimir Oltean 	if (!fdb)
2554*961d8b69SVladimir Oltean 		return -ENOMEM;
2555*961d8b69SVladimir Oltean 
2556*961d8b69SVladimir Oltean 	ether_addr_copy(fdb->addr, addr);
2557*961d8b69SVladimir Oltean 	fdb->vid = vid;
2558*961d8b69SVladimir Oltean 	fdb->bond = bond;
2559*961d8b69SVladimir Oltean 
2560*961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2561*961d8b69SVladimir Oltean 	lag = ocelot_bond_get_id(ocelot, bond);
2562*961d8b69SVladimir Oltean 
2563*961d8b69SVladimir Oltean 	err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED);
2564*961d8b69SVladimir Oltean 	if (err) {
2565*961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2566*961d8b69SVladimir Oltean 		kfree(fdb);
2567*961d8b69SVladimir Oltean 		return err;
2568*961d8b69SVladimir Oltean 	}
2569*961d8b69SVladimir Oltean 
2570*961d8b69SVladimir Oltean 	list_add_tail(&fdb->list, &ocelot->lag_fdbs);
2571*961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2572*961d8b69SVladimir Oltean 
2573*961d8b69SVladimir Oltean 	return 0;
2574*961d8b69SVladimir Oltean }
2575*961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add);
2576*961d8b69SVladimir Oltean 
2577*961d8b69SVladimir Oltean int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond,
2578*961d8b69SVladimir Oltean 		       const unsigned char *addr, u16 vid)
2579*961d8b69SVladimir Oltean {
2580*961d8b69SVladimir Oltean 	struct ocelot_lag_fdb *fdb, *tmp;
2581*961d8b69SVladimir Oltean 
2582*961d8b69SVladimir Oltean 	mutex_lock(&ocelot->fwd_domain_lock);
2583*961d8b69SVladimir Oltean 
2584*961d8b69SVladimir Oltean 	list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) {
2585*961d8b69SVladimir Oltean 		if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid ||
2586*961d8b69SVladimir Oltean 		    fdb->bond != bond)
2587*961d8b69SVladimir Oltean 			continue;
2588*961d8b69SVladimir Oltean 
2589*961d8b69SVladimir Oltean 		ocelot_mact_forget(ocelot, addr, vid);
2590*961d8b69SVladimir Oltean 		list_del(&fdb->list);
2591*961d8b69SVladimir Oltean 		mutex_unlock(&ocelot->fwd_domain_lock);
2592*961d8b69SVladimir Oltean 		kfree(fdb);
2593*961d8b69SVladimir Oltean 
2594*961d8b69SVladimir Oltean 		return 0;
2595*961d8b69SVladimir Oltean 	}
2596*961d8b69SVladimir Oltean 
2597*961d8b69SVladimir Oltean 	mutex_unlock(&ocelot->fwd_domain_lock);
2598*961d8b69SVladimir Oltean 
2599*961d8b69SVladimir Oltean 	return -ENOENT;
2600*961d8b69SVladimir Oltean }
2601*961d8b69SVladimir Oltean EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del);
2602*961d8b69SVladimir Oltean 
2603a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2604a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
26050b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
26060b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
26070b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2608a8015dedSVladimir Oltean  */
26090b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
261031350d7fSVladimir Oltean {
261131350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2612a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2613e8e6e73dSVladimir Oltean 	int pause_start, pause_stop;
2614601e984fSVladimir Oltean 	int atop, atop_tot;
261531350d7fSVladimir Oltean 
26160b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
26170b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
26180b912fc9SVladimir Oltean 
2619cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
26200b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2621cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
26220b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
26230b912fc9SVladimir Oltean 	}
26240b912fc9SVladimir Oltean 
2625a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2626fa914e9cSVladimir Oltean 
2627e8e6e73dSVladimir Oltean 	/* Set Pause watermark hysteresis */
2628e8e6e73dSVladimir Oltean 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2629e8e6e73dSVladimir Oltean 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2630541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2631541132f0SMaxim Kochetkov 			    pause_start);
2632541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2633541132f0SMaxim Kochetkov 			    pause_stop);
2634fa914e9cSVladimir Oltean 
2635601e984fSVladimir Oltean 	/* Tail dropping watermarks */
2636f6fe01d6SVladimir Oltean 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2637a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2638601e984fSVladimir Oltean 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2639601e984fSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2640601e984fSVladimir Oltean 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2641fa914e9cSVladimir Oltean }
26420b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
26430b912fc9SVladimir Oltean 
26440b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
26450b912fc9SVladimir Oltean {
26460b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
26470b912fc9SVladimir Oltean 
26480b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
26490b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
26500b912fc9SVladimir Oltean 
2651cacea62fSVladimir Oltean 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
26520b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2653cacea62fSVladimir Oltean 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
26540b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
26550b912fc9SVladimir Oltean 	}
26560b912fc9SVladimir Oltean 
26570b912fc9SVladimir Oltean 	return max_mtu;
26580b912fc9SVladimir Oltean }
26590b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2660fa914e9cSVladimir Oltean 
2661421741eaSVladimir Oltean static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2662421741eaSVladimir Oltean 				     bool enabled)
2663421741eaSVladimir Oltean {
2664421741eaSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2665421741eaSVladimir Oltean 	u32 val = 0;
2666421741eaSVladimir Oltean 
2667421741eaSVladimir Oltean 	if (enabled)
2668421741eaSVladimir Oltean 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2669421741eaSVladimir Oltean 
2670421741eaSVladimir Oltean 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2671421741eaSVladimir Oltean 		       ANA_PORT_PORT_CFG, port);
2672421741eaSVladimir Oltean 
2673421741eaSVladimir Oltean 	ocelot_port->learn_ena = enabled;
2674421741eaSVladimir Oltean }
2675421741eaSVladimir Oltean 
2676421741eaSVladimir Oltean static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2677421741eaSVladimir Oltean 					bool enabled)
2678421741eaSVladimir Oltean {
2679421741eaSVladimir Oltean 	u32 val = 0;
2680421741eaSVladimir Oltean 
2681421741eaSVladimir Oltean 	if (enabled)
2682421741eaSVladimir Oltean 		val = BIT(port);
2683421741eaSVladimir Oltean 
2684421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2685421741eaSVladimir Oltean }
2686421741eaSVladimir Oltean 
2687421741eaSVladimir Oltean static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2688421741eaSVladimir Oltean 					bool enabled)
2689421741eaSVladimir Oltean {
2690421741eaSVladimir Oltean 	u32 val = 0;
2691421741eaSVladimir Oltean 
2692421741eaSVladimir Oltean 	if (enabled)
2693421741eaSVladimir Oltean 		val = BIT(port);
2694421741eaSVladimir Oltean 
2695421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
2696421741eaSVladimir Oltean }
2697421741eaSVladimir Oltean 
2698421741eaSVladimir Oltean static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2699421741eaSVladimir Oltean 					bool enabled)
2700421741eaSVladimir Oltean {
2701421741eaSVladimir Oltean 	u32 val = 0;
2702421741eaSVladimir Oltean 
2703421741eaSVladimir Oltean 	if (enabled)
2704421741eaSVladimir Oltean 		val = BIT(port);
2705421741eaSVladimir Oltean 
2706421741eaSVladimir Oltean 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2707421741eaSVladimir Oltean }
2708421741eaSVladimir Oltean 
2709421741eaSVladimir Oltean int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2710421741eaSVladimir Oltean 				 struct switchdev_brport_flags flags)
2711421741eaSVladimir Oltean {
2712421741eaSVladimir Oltean 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2713421741eaSVladimir Oltean 			   BR_BCAST_FLOOD))
2714421741eaSVladimir Oltean 		return -EINVAL;
2715421741eaSVladimir Oltean 
2716421741eaSVladimir Oltean 	return 0;
2717421741eaSVladimir Oltean }
2718421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2719421741eaSVladimir Oltean 
2720421741eaSVladimir Oltean void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2721421741eaSVladimir Oltean 			      struct switchdev_brport_flags flags)
2722421741eaSVladimir Oltean {
2723421741eaSVladimir Oltean 	if (flags.mask & BR_LEARNING)
2724421741eaSVladimir Oltean 		ocelot_port_set_learning(ocelot, port,
2725421741eaSVladimir Oltean 					 !!(flags.val & BR_LEARNING));
2726421741eaSVladimir Oltean 
2727421741eaSVladimir Oltean 	if (flags.mask & BR_FLOOD)
2728421741eaSVladimir Oltean 		ocelot_port_set_ucast_flood(ocelot, port,
2729421741eaSVladimir Oltean 					    !!(flags.val & BR_FLOOD));
2730421741eaSVladimir Oltean 
2731421741eaSVladimir Oltean 	if (flags.mask & BR_MCAST_FLOOD)
2732421741eaSVladimir Oltean 		ocelot_port_set_mcast_flood(ocelot, port,
2733421741eaSVladimir Oltean 					    !!(flags.val & BR_MCAST_FLOOD));
2734421741eaSVladimir Oltean 
2735421741eaSVladimir Oltean 	if (flags.mask & BR_BCAST_FLOOD)
2736421741eaSVladimir Oltean 		ocelot_port_set_bcast_flood(ocelot, port,
2737421741eaSVladimir Oltean 					    !!(flags.val & BR_BCAST_FLOOD));
2738421741eaSVladimir Oltean }
2739421741eaSVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_flags);
2740421741eaSVladimir Oltean 
27415e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2742fa914e9cSVladimir Oltean {
2743fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2744fa914e9cSVladimir Oltean 
2745b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
274631350d7fSVladimir Oltean 
274731350d7fSVladimir Oltean 	/* Basic L2 initialization */
274831350d7fSVladimir Oltean 
27495bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
27505bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
27515bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
27525bc9d2e6SVladimir Oltean 	 */
27535bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
27545bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
27555bc9d2e6SVladimir Oltean 
27565bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
27575bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
27585bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
27595bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
27605bc9d2e6SVladimir Oltean 	mdelay(1);
27615bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
27625bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
27635bc9d2e6SVladimir Oltean 
27645bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2765a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
27665bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
27675bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2768a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
27695bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
27705bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
27715bc9d2e6SVladimir Oltean 
27725bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
27735bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
27745bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
27755bc9d2e6SVladimir Oltean 
2776e8e6e73dSVladimir Oltean 	/* Enable transmission of pause frames */
2777541132f0SMaxim Kochetkov 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2778e8e6e73dSVladimir Oltean 
277931350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
278031350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
278131350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
278231350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
278331350d7fSVladimir Oltean 
278431350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
278531350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
278631350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
278731350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
278831350d7fSVladimir Oltean 
2789421741eaSVladimir Oltean 	/* Disable source address learning for standalone mode */
2790421741eaSVladimir Oltean 	ocelot_port_set_learning(ocelot, port, false);
2791421741eaSVladimir Oltean 
279246efe4efSVladimir Oltean 	/* Set the port's initial logical port ID value, enable receiving
279346efe4efSVladimir Oltean 	 * frames on it, and configure the MAC address learning type to
279446efe4efSVladimir Oltean 	 * automatic.
279546efe4efSVladimir Oltean 	 */
279646efe4efSVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
279746efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
279846efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
279946efe4efSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
280046efe4efSVladimir Oltean 
280131350d7fSVladimir Oltean 	/* Enable vcap lookups */
280231350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
280331350d7fSVladimir Oltean }
28045e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
280531350d7fSVladimir Oltean 
28062d44b097SVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues
28072d44b097SVladimir Oltean  * accessible through register MMIO, frame DMA or Ethernet (in case
28082d44b097SVladimir Oltean  * NPI mode is used).
280969df578cSVladimir Oltean  */
28102d44b097SVladimir Oltean static void ocelot_cpu_port_init(struct ocelot *ocelot)
281121468199SVladimir Oltean {
281269df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
281369df578cSVladimir Oltean 
281469df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
281521468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
281669df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
281769df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
281869df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
281969df578cSVladimir Oltean 	 */
282021468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
282121468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
282221468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
282321468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
282421468199SVladimir Oltean 
282569df578cSVladimir Oltean 	/* Enable CPU port module */
2826886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
282769df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
2828886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2829cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
2830886e1387SVladimir Oltean 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2831cacea62fSVladimir Oltean 			    OCELOT_TAG_PREFIX_NONE);
283221468199SVladimir Oltean 
283321468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
2834bfbab310SVladimir Oltean 	ocelot_write_gix(ocelot,
2835bfbab310SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_VLAN_UNAWARE_PVID) |
283621468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
283721468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
283821468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
283921468199SVladimir Oltean }
284021468199SVladimir Oltean 
2841f6fe01d6SVladimir Oltean static void ocelot_detect_features(struct ocelot *ocelot)
2842f6fe01d6SVladimir Oltean {
2843f6fe01d6SVladimir Oltean 	int mmgt, eq_ctrl;
2844f6fe01d6SVladimir Oltean 
2845f6fe01d6SVladimir Oltean 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2846f6fe01d6SVladimir Oltean 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2847f6fe01d6SVladimir Oltean 	 * 192 bytes as the documentation incorrectly says.
2848f6fe01d6SVladimir Oltean 	 */
2849f6fe01d6SVladimir Oltean 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2850f6fe01d6SVladimir Oltean 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2851f6fe01d6SVladimir Oltean 
2852f6fe01d6SVladimir Oltean 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2853f6fe01d6SVladimir Oltean 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2854f6fe01d6SVladimir Oltean }
2855f6fe01d6SVladimir Oltean 
2856a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2857a556c76aSAlexandre Belloni {
2858a556c76aSAlexandre Belloni 	char queue_name[32];
285921468199SVladimir Oltean 	int i, ret;
286021468199SVladimir Oltean 	u32 port;
2861a556c76aSAlexandre Belloni 
28623a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
28633a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
28643a77b593SVladimir Oltean 		if (ret) {
28653a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
28663a77b593SVladimir Oltean 			return ret;
28673a77b593SVladimir Oltean 		}
28683a77b593SVladimir Oltean 	}
28693a77b593SVladimir Oltean 
2870a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2871a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2872a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2873a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2874a556c76aSAlexandre Belloni 		return -ENOMEM;
2875a556c76aSAlexandre Belloni 
2876a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
28774e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
28782468346cSVladimir Oltean 	mutex_init(&ocelot->mact_lock);
28798abe1970SVladimir Oltean 	mutex_init(&ocelot->fwd_domain_lock);
28804e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
288152849bcfSVladimir Oltean 	spin_lock_init(&ocelot->ts_id_lock);
2882a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2883a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2884a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2885a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2886a556c76aSAlexandre Belloni 		return -ENOMEM;
2887a556c76aSAlexandre Belloni 
2888ca0b272bSVladimir Oltean 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2889ca0b272bSVladimir Oltean 	if (!ocelot->owq) {
2890ca0b272bSVladimir Oltean 		destroy_workqueue(ocelot->stats_queue);
2891ca0b272bSVladimir Oltean 		return -ENOMEM;
2892ca0b272bSVladimir Oltean 	}
2893ca0b272bSVladimir Oltean 
28942b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2895e5d1f896SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->pgids);
289690e0aa8dSVladimir Oltean 	INIT_LIST_HEAD(&ocelot->vlans);
2897*961d8b69SVladimir Oltean 	INIT_LIST_HEAD(&ocelot->lag_fdbs);
2898f6fe01d6SVladimir Oltean 	ocelot_detect_features(ocelot);
2899a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2900a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2901aae4e500SVladimir Oltean 	ocelot_vcap_init(ocelot);
29022d44b097SVladimir Oltean 	ocelot_cpu_port_init(ocelot);
2903a556c76aSAlexandre Belloni 
290423e2c506SXiaoliang Yang 	if (ocelot->ops->psfp_init)
290523e2c506SXiaoliang Yang 		ocelot->ops->psfp_init(ocelot);
290623e2c506SXiaoliang Yang 
2907a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2908a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2909a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2910a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2911a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2912a556c76aSAlexandre Belloni 	}
2913a556c76aSAlexandre Belloni 
2914a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2915a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2916a556c76aSAlexandre Belloni 
2917a556c76aSAlexandre Belloni 	/* Aggregation mode */
2918a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2919a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2920a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2921f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2922f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2923f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2924f79c20c8SVladimir Oltean 			     ANA_AGGR_CFG);
2925a556c76aSAlexandre Belloni 
2926a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2927a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2928a556c76aSAlexandre Belloni 	 */
2929a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2930a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2931a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2932a556c76aSAlexandre Belloni 
2933a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2934a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2935a556c76aSAlexandre Belloni 
2936a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2937a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2938a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2939a556c76aSAlexandre Belloni 
2940a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2941edd2410bSVladimir Oltean 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2942a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2943b360d94fSVladimir Oltean 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2944a556c76aSAlexandre Belloni 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2945edd2410bSVladimir Oltean 				 ANA_FLOODING, i);
2946a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2947a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2948a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2949a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2950a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2951a556c76aSAlexandre Belloni 
2952a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2953a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2954a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2955a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2956a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2957a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2958a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2959a556c76aSAlexandre Belloni 				 port);
2960a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2961a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2962a556c76aSAlexandre Belloni 	}
2963a556c76aSAlexandre Belloni 
296496b029b0SVladimir Oltean 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2965a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2966a556c76aSAlexandre Belloni 
2967a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2968a556c76aSAlexandre Belloni 	}
2969ebb1bb40SHoratiu Vultur 
2970ebb1bb40SHoratiu Vultur 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2971ebb1bb40SHoratiu Vultur 
2972b360d94fSVladimir Oltean 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2973b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2974b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2975a556c76aSAlexandre Belloni 		       ANA_PGID_PGID, PGID_MC);
2976b360d94fSVladimir Oltean 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2977b360d94fSVladimir Oltean 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2978b360d94fSVladimir Oltean 		       ANA_PGID_PGID, PGID_BC);
2979a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2980a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2981a556c76aSAlexandre Belloni 
2982a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2983a556c76aSAlexandre Belloni 	 * registers endianness.
2984a556c76aSAlexandre Belloni 	 */
2985a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2986a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2987a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2988a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2989a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2990a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2991a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2992a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2993a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2994a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2995a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2996a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2997a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2998a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2999a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
3000a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
3001a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
3002a556c76aSAlexandre Belloni 
3003d87b1c08SColin Foster 	ret = ocelot_prepare_stats_regions(ocelot);
3004d87b1c08SColin Foster 	if (ret) {
3005d87b1c08SColin Foster 		destroy_workqueue(ocelot->stats_queue);
3006d87b1c08SColin Foster 		destroy_workqueue(ocelot->owq);
3007d87b1c08SColin Foster 		return ret;
3008d87b1c08SColin Foster 	}
3009d87b1c08SColin Foster 
30101e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
3011a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
3012a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
30134e3b0468SAntoine Tenart 
3014a556c76aSAlexandre Belloni 	return 0;
3015a556c76aSAlexandre Belloni }
3016a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
3017a556c76aSAlexandre Belloni 
3018a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
3019a556c76aSAlexandre Belloni {
3020c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
3021a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
3022ca0b272bSVladimir Oltean 	destroy_workqueue(ocelot->owq);
3023a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
3024a556c76aSAlexandre Belloni }
3025a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
3026a556c76aSAlexandre Belloni 
3027e5fb512dSVladimir Oltean void ocelot_deinit_port(struct ocelot *ocelot, int port)
3028e5fb512dSVladimir Oltean {
3029e5fb512dSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3030e5fb512dSVladimir Oltean 
3031e5fb512dSVladimir Oltean 	skb_queue_purge(&ocelot_port->tx_skbs);
3032e5fb512dSVladimir Oltean }
3033e5fb512dSVladimir Oltean EXPORT_SYMBOL(ocelot_deinit_port);
3034e5fb512dSVladimir Oltean 
3035a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
3036