xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 306fd44b1af9b2a5c245b0f14979cfe62e50fae3)
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  */
7a556c76aSAlexandre Belloni #include <linux/etherdevice.h>
8a556c76aSAlexandre Belloni #include <linux/ethtool.h>
9a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
10a556c76aSAlexandre Belloni #include <linux/if_ether.h>
11a556c76aSAlexandre Belloni #include <linux/if_vlan.h>
12a556c76aSAlexandre Belloni #include <linux/interrupt.h>
13a556c76aSAlexandre Belloni #include <linux/kernel.h>
14a556c76aSAlexandre Belloni #include <linux/module.h>
15a556c76aSAlexandre Belloni #include <linux/netdevice.h>
16a556c76aSAlexandre Belloni #include <linux/phy.h>
174e3b0468SAntoine Tenart #include <linux/ptp_clock_kernel.h>
18a556c76aSAlexandre Belloni #include <linux/skbuff.h>
19639c1b26SSteen Hegelund #include <linux/iopoll.h>
20a556c76aSAlexandre Belloni #include <net/arp.h>
21a556c76aSAlexandre Belloni #include <net/netevent.h>
22a556c76aSAlexandre Belloni #include <net/rtnetlink.h>
23a556c76aSAlexandre Belloni #include <net/switchdev.h>
24531ee1a6SVladimir Oltean #include <net/dsa.h>
25a556c76aSAlexandre Belloni 
26a556c76aSAlexandre Belloni #include "ocelot.h"
27b5962294SHoratiu Vultur #include "ocelot_ace.h"
28a556c76aSAlexandre Belloni 
29639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
30639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
31639c1b26SSteen Hegelund 
32a556c76aSAlexandre Belloni /* MAC table entry types.
33a556c76aSAlexandre Belloni  * ENTRYTYPE_NORMAL is subject to aging.
34a556c76aSAlexandre Belloni  * ENTRYTYPE_LOCKED is not subject to aging.
35a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
36a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
37a556c76aSAlexandre Belloni  */
38a556c76aSAlexandre Belloni enum macaccess_entry_type {
39a556c76aSAlexandre Belloni 	ENTRYTYPE_NORMAL = 0,
40a556c76aSAlexandre Belloni 	ENTRYTYPE_LOCKED,
41a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv4,
42a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv6,
43a556c76aSAlexandre Belloni };
44a556c76aSAlexandre Belloni 
45a556c76aSAlexandre Belloni struct ocelot_mact_entry {
46a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
47a556c76aSAlexandre Belloni 	u16 vid;
48a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
49a556c76aSAlexandre Belloni };
50a556c76aSAlexandre Belloni 
51639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
52639c1b26SSteen Hegelund {
53639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
54639c1b26SSteen Hegelund }
55639c1b26SSteen Hegelund 
56a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
57a556c76aSAlexandre Belloni {
58639c1b26SSteen Hegelund 	u32 val;
59a556c76aSAlexandre Belloni 
60639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
61639c1b26SSteen Hegelund 		ocelot, val,
62639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
63639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
64639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
65a556c76aSAlexandre Belloni }
66a556c76aSAlexandre Belloni 
67a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
68a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
69a556c76aSAlexandre Belloni 			       unsigned int vid)
70a556c76aSAlexandre Belloni {
71a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
72a556c76aSAlexandre Belloni 
73a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
74a556c76aSAlexandre Belloni 	 * understood by the hardware.
75a556c76aSAlexandre Belloni 	 */
76a556c76aSAlexandre Belloni 	mach |= vid    << 16;
77a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
78a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
79a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
80a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
81a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
82a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
83a556c76aSAlexandre Belloni 
84a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
85a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
86a556c76aSAlexandre Belloni 
87a556c76aSAlexandre Belloni }
88a556c76aSAlexandre Belloni 
89a556c76aSAlexandre Belloni static int ocelot_mact_learn(struct ocelot *ocelot, int port,
90a556c76aSAlexandre Belloni 			     const unsigned char mac[ETH_ALEN],
91a556c76aSAlexandre Belloni 			     unsigned int vid,
92a556c76aSAlexandre Belloni 			     enum macaccess_entry_type type)
93a556c76aSAlexandre Belloni {
94a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
95a556c76aSAlexandre Belloni 
96a556c76aSAlexandre Belloni 	/* Issue a write command */
97a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
98a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
99a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
100a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
101a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
102a556c76aSAlexandre Belloni 
103a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
104a556c76aSAlexandre Belloni }
105a556c76aSAlexandre Belloni 
106a556c76aSAlexandre Belloni static int ocelot_mact_forget(struct ocelot *ocelot,
107a556c76aSAlexandre Belloni 			      const unsigned char mac[ETH_ALEN],
108a556c76aSAlexandre Belloni 			      unsigned int vid)
109a556c76aSAlexandre Belloni {
110a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
111a556c76aSAlexandre Belloni 
112a556c76aSAlexandre Belloni 	/* Issue a forget command */
113a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
114a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
115a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
116a556c76aSAlexandre Belloni 
117a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
118a556c76aSAlexandre Belloni }
119a556c76aSAlexandre Belloni 
120a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
121a556c76aSAlexandre Belloni {
122a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
123a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
124a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
125a556c76aSAlexandre Belloni 	 */
126a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
127a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
128a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
129a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
130a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
131a556c76aSAlexandre Belloni 
132a556c76aSAlexandre Belloni 	/* Clear the MAC table */
133a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
134a556c76aSAlexandre Belloni }
135a556c76aSAlexandre Belloni 
136b5962294SHoratiu Vultur static void ocelot_vcap_enable(struct ocelot *ocelot, struct ocelot_port *port)
137b5962294SHoratiu Vultur {
138b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
139b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
140b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG, port->chip_port);
141b5962294SHoratiu Vultur }
142b5962294SHoratiu Vultur 
143639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
144639c1b26SSteen Hegelund {
145639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
146639c1b26SSteen Hegelund }
147639c1b26SSteen Hegelund 
148a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
149a556c76aSAlexandre Belloni {
150639c1b26SSteen Hegelund 	u32 val;
151a556c76aSAlexandre Belloni 
152639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
153639c1b26SSteen Hegelund 		ocelot,
154639c1b26SSteen Hegelund 		val,
155639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
156639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
157639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
158a556c76aSAlexandre Belloni }
159a556c76aSAlexandre Belloni 
1607142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1617142529fSAntoine Tenart {
1627142529fSAntoine Tenart 	/* Select the VID to configure */
1637142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1647142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1657142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1667142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1677142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1687142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1697142529fSAntoine Tenart 
1707142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1717142529fSAntoine Tenart }
1727142529fSAntoine Tenart 
1737142529fSAntoine Tenart static void ocelot_vlan_mode(struct ocelot_port *port,
1747142529fSAntoine Tenart 			     netdev_features_t features)
1757142529fSAntoine Tenart {
1767142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
1777142529fSAntoine Tenart 	u8 p = port->chip_port;
1787142529fSAntoine Tenart 	u32 val;
1797142529fSAntoine Tenart 
1807142529fSAntoine Tenart 	/* Filtering */
1817142529fSAntoine Tenart 	val = ocelot_read(ocelot, ANA_VLANMASK);
1827142529fSAntoine Tenart 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1837142529fSAntoine Tenart 		val |= BIT(p);
1847142529fSAntoine Tenart 	else
1857142529fSAntoine Tenart 		val &= ~BIT(p);
1867142529fSAntoine Tenart 	ocelot_write(ocelot, val, ANA_VLANMASK);
1877142529fSAntoine Tenart }
1887142529fSAntoine Tenart 
18997bb69e1SVladimir Oltean static void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
19097bb69e1SVladimir Oltean 				       bool vlan_aware)
1917142529fSAntoine Tenart {
19297bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1937142529fSAntoine Tenart 	u32 val;
1947142529fSAntoine Tenart 
19597bb69e1SVladimir Oltean 	if (vlan_aware)
19697bb69e1SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1977142529fSAntoine Tenart 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
19897bb69e1SVladimir Oltean 	else
19997bb69e1SVladimir Oltean 		val = 0;
2007142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2017142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2027142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
20397bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
2047142529fSAntoine Tenart 
20597bb69e1SVladimir Oltean 	if (vlan_aware && !ocelot_port->vid)
2067142529fSAntoine Tenart 		/* If port is vlan-aware and tagged, drop untagged and priority
2077142529fSAntoine Tenart 		 * tagged frames.
2087142529fSAntoine Tenart 		 */
20997bb69e1SVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
2107142529fSAntoine Tenart 		      ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
2117142529fSAntoine Tenart 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
21297bb69e1SVladimir Oltean 	else
21397bb69e1SVladimir Oltean 		val = 0;
21497bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
21597bb69e1SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
21697bb69e1SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
21797bb69e1SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
21897bb69e1SVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
2197142529fSAntoine Tenart 
22097bb69e1SVladimir Oltean 	if (vlan_aware) {
22197bb69e1SVladimir Oltean 		if (ocelot_port->vid)
2227142529fSAntoine Tenart 			/* Tag all frames except when VID == DEFAULT_VLAN */
2237142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(1);
2247142529fSAntoine Tenart 		else
2257142529fSAntoine Tenart 			/* Tag all frames */
2267142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(3);
22797bb69e1SVladimir Oltean 	} else {
22897bb69e1SVladimir Oltean 		/* Port tagging disabled. */
22997bb69e1SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
2307142529fSAntoine Tenart 	}
2317142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2327142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_CFG_M,
23397bb69e1SVladimir Oltean 		       REW_TAG_CFG, port);
2347142529fSAntoine Tenart 
23597bb69e1SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
23697bb69e1SVladimir Oltean }
23797bb69e1SVladimir Oltean 
23897bb69e1SVladimir Oltean static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
23997bb69e1SVladimir Oltean 				       u16 vid)
24097bb69e1SVladimir Oltean {
24197bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
24297bb69e1SVladimir Oltean 
24397bb69e1SVladimir Oltean 	if (ocelot_port->vid != vid) {
24497bb69e1SVladimir Oltean 		/* Always permit deleting the native VLAN (vid = 0) */
24597bb69e1SVladimir Oltean 		if (ocelot_port->vid && vid) {
24697bb69e1SVladimir Oltean 			dev_err(ocelot->dev,
24797bb69e1SVladimir Oltean 				"Port already has a native VLAN: %d\n",
24897bb69e1SVladimir Oltean 				ocelot_port->vid);
24997bb69e1SVladimir Oltean 			return -EBUSY;
25097bb69e1SVladimir Oltean 		}
25197bb69e1SVladimir Oltean 		ocelot_port->vid = vid;
25297bb69e1SVladimir Oltean 	}
25397bb69e1SVladimir Oltean 
25497bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
2557142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
25697bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
25797bb69e1SVladimir Oltean 
25897bb69e1SVladimir Oltean 	return 0;
25997bb69e1SVladimir Oltean }
26097bb69e1SVladimir Oltean 
26197bb69e1SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
26297bb69e1SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
26397bb69e1SVladimir Oltean {
26497bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
26597bb69e1SVladimir Oltean 
26697bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot,
26797bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
26897bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
26997bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
27097bb69e1SVladimir Oltean 
27197bb69e1SVladimir Oltean 	ocelot_port->pvid = pvid;
2727142529fSAntoine Tenart }
2737142529fSAntoine Tenart 
2749855934cSVladimir Oltean static int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2757142529fSAntoine Tenart 			   bool untagged)
2767142529fSAntoine Tenart {
2777142529fSAntoine Tenart 	int ret;
2787142529fSAntoine Tenart 
2797142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
28097bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2817142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2827142529fSAntoine Tenart 	if (ret)
2837142529fSAntoine Tenart 		return ret;
2847142529fSAntoine Tenart 
2857142529fSAntoine Tenart 	/* Default ingress vlan classification */
2867142529fSAntoine Tenart 	if (pvid)
28797bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, vid);
2887142529fSAntoine Tenart 
2897142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
29097bb69e1SVladimir Oltean 	if (untagged) {
29197bb69e1SVladimir Oltean 		ret = ocelot_port_set_native_vlan(ocelot, port, vid);
29297bb69e1SVladimir Oltean 		if (ret)
29397bb69e1SVladimir Oltean 			return ret;
294b9cd75e6SVladimir Oltean 	}
2957142529fSAntoine Tenart 
2967142529fSAntoine Tenart 	return 0;
2977142529fSAntoine Tenart }
2987142529fSAntoine Tenart 
2999855934cSVladimir Oltean static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
3009855934cSVladimir Oltean 			       bool untagged)
3017142529fSAntoine Tenart {
30297bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = netdev_priv(dev);
30397bb69e1SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
30497bb69e1SVladimir Oltean 	int port = ocelot_port->chip_port;
3057142529fSAntoine Tenart 	int ret;
3067142529fSAntoine Tenart 
3079855934cSVladimir Oltean 	ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
3089855934cSVladimir Oltean 	if (ret)
3099855934cSVladimir Oltean 		return ret;
3107142529fSAntoine Tenart 
3119855934cSVladimir Oltean 	/* Add the port MAC address to with the right VLAN information */
3129855934cSVladimir Oltean 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
3139855934cSVladimir Oltean 			  ENTRYTYPE_LOCKED);
3149855934cSVladimir Oltean 
3159855934cSVladimir Oltean 	return 0;
3169855934cSVladimir Oltean }
3179855934cSVladimir Oltean 
3189855934cSVladimir Oltean static int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3199855934cSVladimir Oltean {
3209855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3219855934cSVladimir Oltean 	int ret;
3227142529fSAntoine Tenart 
3237142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
32497bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3257142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3267142529fSAntoine Tenart 	if (ret)
3277142529fSAntoine Tenart 		return ret;
3287142529fSAntoine Tenart 
3297142529fSAntoine Tenart 	/* Ingress */
33097bb69e1SVladimir Oltean 	if (ocelot_port->pvid == vid)
33197bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, 0);
3327142529fSAntoine Tenart 
3337142529fSAntoine Tenart 	/* Egress */
33497bb69e1SVladimir Oltean 	if (ocelot_port->vid == vid)
33597bb69e1SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, 0);
3367142529fSAntoine Tenart 
3377142529fSAntoine Tenart 	return 0;
3387142529fSAntoine Tenart }
3397142529fSAntoine Tenart 
3409855934cSVladimir Oltean static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
3419855934cSVladimir Oltean {
3429855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = netdev_priv(dev);
3439855934cSVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
3449855934cSVladimir Oltean 	int port = ocelot_port->chip_port;
3459855934cSVladimir Oltean 	int ret;
3469855934cSVladimir Oltean 
3479855934cSVladimir Oltean 	/* 8021q removes VID 0 on module unload for all interfaces
3489855934cSVladimir Oltean 	 * with VLAN filtering feature. We need to keep it to receive
3499855934cSVladimir Oltean 	 * untagged traffic.
3509855934cSVladimir Oltean 	 */
3519855934cSVladimir Oltean 	if (vid == 0)
3529855934cSVladimir Oltean 		return 0;
3539855934cSVladimir Oltean 
3549855934cSVladimir Oltean 	ret = ocelot_vlan_del(ocelot, port, vid);
3559855934cSVladimir Oltean 	if (ret)
3569855934cSVladimir Oltean 		return ret;
3579855934cSVladimir Oltean 
3589855934cSVladimir Oltean 	/* Del the port MAC address to with the right VLAN information */
3599855934cSVladimir Oltean 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
3609855934cSVladimir Oltean 
3619855934cSVladimir Oltean 	return 0;
3629855934cSVladimir Oltean }
3639855934cSVladimir Oltean 
364a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
365a556c76aSAlexandre Belloni {
3667142529fSAntoine Tenart 	u16 port, vid;
3677142529fSAntoine Tenart 
368a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
369a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
370a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
371a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3727142529fSAntoine Tenart 
3737142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3747142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3757142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3767142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3777142529fSAntoine Tenart 	}
3787142529fSAntoine Tenart 
3797142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3807142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3817142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3827142529fSAntoine Tenart 	 */
3837142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3847142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3857142529fSAntoine Tenart 
3867142529fSAntoine Tenart 	/* Configure the CPU port to be VLAN aware */
3877142529fSAntoine Tenart 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
3887142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
3897142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
3907142529fSAntoine Tenart 			 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
3917142529fSAntoine Tenart 
3927142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3937142529fSAntoine Tenart 	 * default.
3947142529fSAntoine Tenart 	 */
3957142529fSAntoine Tenart 	ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
3967142529fSAntoine Tenart 
3977142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3987142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3997142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
4007142529fSAntoine Tenart 	}
401a556c76aSAlexandre Belloni }
402a556c76aSAlexandre Belloni 
403a556c76aSAlexandre Belloni /* Watermark encode
404a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
405a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
406a556c76aSAlexandre Belloni  */
407a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
408a556c76aSAlexandre Belloni {
409a556c76aSAlexandre Belloni 	if (value >= BIT(8))
410a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
411a556c76aSAlexandre Belloni 
412a556c76aSAlexandre Belloni 	return value;
413a556c76aSAlexandre Belloni }
414a556c76aSAlexandre Belloni 
415a556c76aSAlexandre Belloni static void ocelot_port_adjust_link(struct net_device *dev)
416a556c76aSAlexandre Belloni {
417a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
418a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
419a556c76aSAlexandre Belloni 	u8 p = port->chip_port;
420a556c76aSAlexandre Belloni 	int speed, atop_wm, mode = 0;
421a556c76aSAlexandre Belloni 
422a556c76aSAlexandre Belloni 	switch (dev->phydev->speed) {
423a556c76aSAlexandre Belloni 	case SPEED_10:
424a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
425a556c76aSAlexandre Belloni 		break;
426a556c76aSAlexandre Belloni 	case SPEED_100:
427a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
428a556c76aSAlexandre Belloni 		break;
429a556c76aSAlexandre Belloni 	case SPEED_1000:
430a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
431a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
432a556c76aSAlexandre Belloni 		break;
433a556c76aSAlexandre Belloni 	case SPEED_2500:
434a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
435a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
436a556c76aSAlexandre Belloni 		break;
437a556c76aSAlexandre Belloni 	default:
438a556c76aSAlexandre Belloni 		netdev_err(dev, "Unsupported PHY speed: %d\n",
439a556c76aSAlexandre Belloni 			   dev->phydev->speed);
440a556c76aSAlexandre Belloni 		return;
441a556c76aSAlexandre Belloni 	}
442a556c76aSAlexandre Belloni 
443a556c76aSAlexandre Belloni 	phy_print_status(dev->phydev);
444a556c76aSAlexandre Belloni 
445a556c76aSAlexandre Belloni 	if (!dev->phydev->link)
446a556c76aSAlexandre Belloni 		return;
447a556c76aSAlexandre Belloni 
448a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
449a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
450a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
451a556c76aSAlexandre Belloni 
452a556c76aSAlexandre Belloni 	/* Set MAC IFG Gaps
453a556c76aSAlexandre Belloni 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
454a556c76aSAlexandre Belloni 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
455a556c76aSAlexandre Belloni 	 */
456a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
457a556c76aSAlexandre Belloni 
458a556c76aSAlexandre Belloni 	/* Load seed (0) and set MAC HDX late collision  */
459a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
460a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG_SEED_LOAD,
461a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
462a556c76aSAlexandre Belloni 	mdelay(1);
463a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
464a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
465a556c76aSAlexandre Belloni 
466a556c76aSAlexandre Belloni 	/* Disable HDX fast control */
467a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
468a556c76aSAlexandre Belloni 
469a556c76aSAlexandre Belloni 	/* SGMII only for now */
470a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
471a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
472a556c76aSAlexandre Belloni 
473a556c76aSAlexandre Belloni 	/* Enable PCS */
474a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
475a556c76aSAlexandre Belloni 
476a556c76aSAlexandre Belloni 	/* No aneg on SGMII */
477a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
478a556c76aSAlexandre Belloni 
479a556c76aSAlexandre Belloni 	/* No loopback */
480a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_LB_CFG);
481a556c76aSAlexandre Belloni 
482a556c76aSAlexandre Belloni 	/* Set Max Length and maximum tags allowed */
483a556c76aSAlexandre Belloni 	ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
484a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
485a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
486a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
487a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG);
488a556c76aSAlexandre Belloni 
489a556c76aSAlexandre Belloni 	/* Enable MAC module */
490a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
491a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
492a556c76aSAlexandre Belloni 
493a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
494a556c76aSAlexandre Belloni 	 * reset */
495a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
496a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
497a556c76aSAlexandre Belloni 
498a556c76aSAlexandre Belloni 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
499a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
500a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
501a556c76aSAlexandre Belloni 
502a556c76aSAlexandre Belloni 	/* No PFC */
503a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
504a556c76aSAlexandre Belloni 			 ANA_PFC_PFC_CFG, p);
505a556c76aSAlexandre Belloni 
506a556c76aSAlexandre Belloni 	/* Set Pause WM hysteresis
507a556c76aSAlexandre Belloni 	 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
508a556c76aSAlexandre Belloni 	 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
509a556c76aSAlexandre Belloni 	 */
510a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
511a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
512a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
513a556c76aSAlexandre Belloni 
514a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
515a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
516a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
517a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
518a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, p);
519a556c76aSAlexandre Belloni 
520a556c76aSAlexandre Belloni 	/* Flow control */
521a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
522a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
523a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
524a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
525a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
526a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG, p);
527a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
528a556c76aSAlexandre Belloni 
529a556c76aSAlexandre Belloni 	/* Tail dropping watermark */
530a556c76aSAlexandre Belloni 	atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
531a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
532a556c76aSAlexandre Belloni 			 SYS_ATOP, p);
533a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
534a556c76aSAlexandre Belloni }
535a556c76aSAlexandre Belloni 
536a556c76aSAlexandre Belloni static int ocelot_port_open(struct net_device *dev)
537a556c76aSAlexandre Belloni {
538a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
539a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
540a556c76aSAlexandre Belloni 	int err;
541a556c76aSAlexandre Belloni 
542a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
543a556c76aSAlexandre Belloni 	 * MAC addresses.
544a556c76aSAlexandre Belloni 	 */
545a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
546a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
547a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
548a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, port->chip_port);
549a556c76aSAlexandre Belloni 
55071e32a20SQuentin Schulz 	if (port->serdes) {
551c8fe6d7fSGrygorii Strashko 		err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
552c8fe6d7fSGrygorii Strashko 				       port->phy_mode);
55371e32a20SQuentin Schulz 		if (err) {
55471e32a20SQuentin Schulz 			netdev_err(dev, "Could not set mode of SerDes\n");
55571e32a20SQuentin Schulz 			return err;
55671e32a20SQuentin Schulz 		}
55771e32a20SQuentin Schulz 	}
55871e32a20SQuentin Schulz 
559a556c76aSAlexandre Belloni 	err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
56071e32a20SQuentin Schulz 				 port->phy_mode);
561a556c76aSAlexandre Belloni 	if (err) {
562a556c76aSAlexandre Belloni 		netdev_err(dev, "Could not attach to PHY\n");
563a556c76aSAlexandre Belloni 		return err;
564a556c76aSAlexandre Belloni 	}
565a556c76aSAlexandre Belloni 
566a556c76aSAlexandre Belloni 	dev->phydev = port->phy;
567a556c76aSAlexandre Belloni 
568a556c76aSAlexandre Belloni 	phy_attached_info(port->phy);
569a556c76aSAlexandre Belloni 	phy_start(port->phy);
570a556c76aSAlexandre Belloni 	return 0;
571a556c76aSAlexandre Belloni }
572a556c76aSAlexandre Belloni 
573a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev)
574a556c76aSAlexandre Belloni {
575a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
576a556c76aSAlexandre Belloni 
577a556c76aSAlexandre Belloni 	phy_disconnect(port->phy);
578a556c76aSAlexandre Belloni 
579a556c76aSAlexandre Belloni 	dev->phydev = NULL;
580a556c76aSAlexandre Belloni 
581a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
582a556c76aSAlexandre Belloni 	ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
583a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, port->chip_port);
584a556c76aSAlexandre Belloni 	return 0;
585a556c76aSAlexandre Belloni }
586a556c76aSAlexandre Belloni 
587a556c76aSAlexandre Belloni /* Generate the IFH for frame injection
588a556c76aSAlexandre Belloni  *
589a556c76aSAlexandre Belloni  * The IFH is a 128bit-value
590a556c76aSAlexandre Belloni  * bit 127: bypass the analyzer processing
591a556c76aSAlexandre Belloni  * bit 56-67: destination mask
592a556c76aSAlexandre Belloni  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
593a556c76aSAlexandre Belloni  * bit 20-27: cpu extraction queue mask
594a556c76aSAlexandre Belloni  * bit 16: tag type 0: C-tag, 1: S-tag
595a556c76aSAlexandre Belloni  * bit 0-11: VID
596a556c76aSAlexandre Belloni  */
597a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
598a556c76aSAlexandre Belloni {
5994e3b0468SAntoine Tenart 	ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
60008d02364SAntoine Tenart 	ifh[1] = (0xf00 & info->port) >> 8;
601a556c76aSAlexandre Belloni 	ifh[2] = (0xff & info->port) << 24;
60208d02364SAntoine Tenart 	ifh[3] = (info->tag_type << 16) | info->vid;
603a556c76aSAlexandre Belloni 
604a556c76aSAlexandre Belloni 	return 0;
605a556c76aSAlexandre Belloni }
606a556c76aSAlexandre Belloni 
607a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
608a556c76aSAlexandre Belloni {
6094e3b0468SAntoine Tenart 	struct skb_shared_info *shinfo = skb_shinfo(skb);
610a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
611a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
612a556c76aSAlexandre Belloni 	u32 val, ifh[IFH_LEN];
613a556c76aSAlexandre Belloni 	struct frame_info info = {};
614a556c76aSAlexandre Belloni 	u8 grp = 0; /* Send everything on CPU group 0 */
615a556c76aSAlexandre Belloni 	unsigned int i, count, last;
616a556c76aSAlexandre Belloni 
617a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, QS_INJ_STATUS);
618a556c76aSAlexandre Belloni 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
619a556c76aSAlexandre Belloni 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
620a556c76aSAlexandre Belloni 		return NETDEV_TX_BUSY;
621a556c76aSAlexandre Belloni 
622a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
623a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
624a556c76aSAlexandre Belloni 
625a556c76aSAlexandre Belloni 	info.port = BIT(port->chip_port);
62608d02364SAntoine Tenart 	info.tag_type = IFH_TAG_TYPE_C;
62708d02364SAntoine Tenart 	info.vid = skb_vlan_tag_get(skb);
6284e3b0468SAntoine Tenart 
6294e3b0468SAntoine Tenart 	/* Check if timestamping is needed */
6304e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
6314e3b0468SAntoine Tenart 		info.rew_op = port->ptp_cmd;
6324e3b0468SAntoine Tenart 		if (port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
6334e3b0468SAntoine Tenart 			info.rew_op |= (port->ts_id  % 4) << 3;
6344e3b0468SAntoine Tenart 	}
6354e3b0468SAntoine Tenart 
636a556c76aSAlexandre Belloni 	ocelot_gen_ifh(ifh, &info);
637a556c76aSAlexandre Belloni 
638a556c76aSAlexandre Belloni 	for (i = 0; i < IFH_LEN; i++)
639c2cd650bSAntoine Tenart 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
640c2cd650bSAntoine Tenart 				 QS_INJ_WR, grp);
641a556c76aSAlexandre Belloni 
642a556c76aSAlexandre Belloni 	count = (skb->len + 3) / 4;
643a556c76aSAlexandre Belloni 	last = skb->len % 4;
644a556c76aSAlexandre Belloni 	for (i = 0; i < count; i++) {
645a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
646a556c76aSAlexandre Belloni 	}
647a556c76aSAlexandre Belloni 
648a556c76aSAlexandre Belloni 	/* Add padding */
649a556c76aSAlexandre Belloni 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
650a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
651a556c76aSAlexandre Belloni 		i++;
652a556c76aSAlexandre Belloni 	}
653a556c76aSAlexandre Belloni 
654a556c76aSAlexandre Belloni 	/* Indicate EOF and valid bytes in last word */
655a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
656a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
657a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_EOF,
658a556c76aSAlexandre Belloni 			 QS_INJ_CTRL, grp);
659a556c76aSAlexandre Belloni 
660a556c76aSAlexandre Belloni 	/* Add dummy CRC */
661a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
662a556c76aSAlexandre Belloni 	skb_tx_timestamp(skb);
663a556c76aSAlexandre Belloni 
664a556c76aSAlexandre Belloni 	dev->stats.tx_packets++;
665a556c76aSAlexandre Belloni 	dev->stats.tx_bytes += skb->len;
6664e3b0468SAntoine Tenart 
6674e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
6684e3b0468SAntoine Tenart 	    port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
6694e3b0468SAntoine Tenart 		struct ocelot_skb *oskb =
6704e3b0468SAntoine Tenart 			kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
6714e3b0468SAntoine Tenart 
6724e3b0468SAntoine Tenart 		if (unlikely(!oskb))
6734e3b0468SAntoine Tenart 			goto out;
6744e3b0468SAntoine Tenart 
6754e3b0468SAntoine Tenart 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
6764e3b0468SAntoine Tenart 
6774e3b0468SAntoine Tenart 		oskb->skb = skb;
6784e3b0468SAntoine Tenart 		oskb->id = port->ts_id % 4;
6794e3b0468SAntoine Tenart 		port->ts_id++;
6804e3b0468SAntoine Tenart 
6814e3b0468SAntoine Tenart 		list_add_tail(&oskb->head, &port->skbs);
682a556c76aSAlexandre Belloni 
683a556c76aSAlexandre Belloni 		return NETDEV_TX_OK;
684a556c76aSAlexandre Belloni 	}
685a556c76aSAlexandre Belloni 
6864e3b0468SAntoine Tenart out:
6874e3b0468SAntoine Tenart 	dev_kfree_skb_any(skb);
6884e3b0468SAntoine Tenart 	return NETDEV_TX_OK;
6894e3b0468SAntoine Tenart }
6904e3b0468SAntoine Tenart 
6914e3b0468SAntoine Tenart void ocelot_get_hwtimestamp(struct ocelot *ocelot, struct timespec64 *ts)
6924e3b0468SAntoine Tenart {
6934e3b0468SAntoine Tenart 	unsigned long flags;
6944e3b0468SAntoine Tenart 	u32 val;
6954e3b0468SAntoine Tenart 
6964e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
6974e3b0468SAntoine Tenart 
6984e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
6994e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
7004e3b0468SAntoine Tenart 
7014e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
7024e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
7034e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
7044e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
7054e3b0468SAntoine Tenart 
7064e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
7074e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
7084e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
7094e3b0468SAntoine Tenart 
7104e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
7114e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
7124e3b0468SAntoine Tenart 		ts->tv_sec--;
7134e3b0468SAntoine Tenart 
7144e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
7154e3b0468SAntoine Tenart }
7164e3b0468SAntoine Tenart EXPORT_SYMBOL(ocelot_get_hwtimestamp);
7174e3b0468SAntoine Tenart 
71840a1578dSClaudiu Manoil static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
719a556c76aSAlexandre Belloni {
72040a1578dSClaudiu Manoil 	struct ocelot_port *port = netdev_priv(dev);
721a556c76aSAlexandre Belloni 
72240a1578dSClaudiu Manoil 	return ocelot_mact_forget(port->ocelot, addr, port->pvid);
723a556c76aSAlexandre Belloni }
724a556c76aSAlexandre Belloni 
72540a1578dSClaudiu Manoil static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
726a556c76aSAlexandre Belloni {
72740a1578dSClaudiu Manoil 	struct ocelot_port *port = netdev_priv(dev);
728a556c76aSAlexandre Belloni 
72940a1578dSClaudiu Manoil 	return ocelot_mact_learn(port->ocelot, PGID_CPU, addr, port->pvid,
730a556c76aSAlexandre Belloni 				 ENTRYTYPE_LOCKED);
731a556c76aSAlexandre Belloni }
732a556c76aSAlexandre Belloni 
733a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev)
734a556c76aSAlexandre Belloni {
735a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
736a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
737a556c76aSAlexandre Belloni 	int i;
738a556c76aSAlexandre Belloni 	u32 val;
739a556c76aSAlexandre Belloni 
740a556c76aSAlexandre Belloni 	/* This doesn't handle promiscuous mode because the bridge core is
741a556c76aSAlexandre Belloni 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
742a556c76aSAlexandre Belloni 	 * forwarded to the CPU port.
743a556c76aSAlexandre Belloni 	 */
744a556c76aSAlexandre Belloni 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
745a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
746a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
747a556c76aSAlexandre Belloni 
74840a1578dSClaudiu Manoil 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
749a556c76aSAlexandre Belloni }
750a556c76aSAlexandre Belloni 
751a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev,
752a556c76aSAlexandre Belloni 					  char *buf, size_t len)
753a556c76aSAlexandre Belloni {
754a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
755a556c76aSAlexandre Belloni 	int ret;
756a556c76aSAlexandre Belloni 
757a556c76aSAlexandre Belloni 	ret = snprintf(buf, len, "p%d", port->chip_port);
758a556c76aSAlexandre Belloni 	if (ret >= len)
759a556c76aSAlexandre Belloni 		return -EINVAL;
760a556c76aSAlexandre Belloni 
761a556c76aSAlexandre Belloni 	return 0;
762a556c76aSAlexandre Belloni }
763a556c76aSAlexandre Belloni 
764a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
765a556c76aSAlexandre Belloni {
766a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
767a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
768a556c76aSAlexandre Belloni 	const struct sockaddr *addr = p;
769a556c76aSAlexandre Belloni 
770a556c76aSAlexandre Belloni 	/* Learn the new net device MAC address in the mac table. */
771a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
772a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
773a556c76aSAlexandre Belloni 	/* Then forget the previous one. */
774a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
775a556c76aSAlexandre Belloni 
776a556c76aSAlexandre Belloni 	ether_addr_copy(dev->dev_addr, addr->sa_data);
777a556c76aSAlexandre Belloni 	return 0;
778a556c76aSAlexandre Belloni }
779a556c76aSAlexandre Belloni 
780a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev,
781a556c76aSAlexandre Belloni 			       struct rtnl_link_stats64 *stats)
782a556c76aSAlexandre Belloni {
783a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
784a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
785a556c76aSAlexandre Belloni 
786a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
787a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
788a556c76aSAlexandre Belloni 		     SYS_STAT_CFG);
789a556c76aSAlexandre Belloni 
790a556c76aSAlexandre Belloni 	/* Get Rx stats */
791a556c76aSAlexandre Belloni 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
792a556c76aSAlexandre Belloni 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
793a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
794a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
795a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
796a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
797a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
798a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
799a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
800a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
801a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
802a556c76aSAlexandre Belloni 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
803a556c76aSAlexandre Belloni 	stats->rx_dropped = dev->stats.rx_dropped;
804a556c76aSAlexandre Belloni 
805a556c76aSAlexandre Belloni 	/* Get Tx stats */
806a556c76aSAlexandre Belloni 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
807a556c76aSAlexandre Belloni 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
808a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
809a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
810a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
811a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
812a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
813a556c76aSAlexandre Belloni 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
814a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
815a556c76aSAlexandre Belloni 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
816a556c76aSAlexandre Belloni }
817a556c76aSAlexandre Belloni 
818531ee1a6SVladimir Oltean static int ocelot_fdb_add(struct ocelot *ocelot, int port,
819531ee1a6SVladimir Oltean 			  const unsigned char *addr, u16 vid)
820a556c76aSAlexandre Belloni {
821531ee1a6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
822a556c76aSAlexandre Belloni 
8237142529fSAntoine Tenart 	if (!vid) {
824531ee1a6SVladimir Oltean 		if (!ocelot_port->vlan_aware)
8257142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
8267142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
8277142529fSAntoine Tenart 			 * matches incoming untagged packets
8287142529fSAntoine Tenart 			 */
829531ee1a6SVladimir Oltean 			vid = ocelot_port->pvid;
8307142529fSAntoine Tenart 		else
8317142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
8327142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
8337142529fSAntoine Tenart 			 */
8347142529fSAntoine Tenart 			return -EINVAL;
8357142529fSAntoine Tenart 	}
8367142529fSAntoine Tenart 
837531ee1a6SVladimir Oltean 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
838a556c76aSAlexandre Belloni }
839a556c76aSAlexandre Belloni 
840531ee1a6SVladimir Oltean static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
841531ee1a6SVladimir Oltean 			       struct net_device *dev,
842531ee1a6SVladimir Oltean 			       const unsigned char *addr,
843531ee1a6SVladimir Oltean 			       u16 vid, u16 flags,
844531ee1a6SVladimir Oltean 			       struct netlink_ext_ack *extack)
845531ee1a6SVladimir Oltean {
846531ee1a6SVladimir Oltean 	struct ocelot_port *ocelot_port = netdev_priv(dev);
847531ee1a6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
848531ee1a6SVladimir Oltean 
849531ee1a6SVladimir Oltean 	return ocelot_fdb_add(ocelot, ocelot_port->chip_port, addr, vid);
850531ee1a6SVladimir Oltean }
851531ee1a6SVladimir Oltean 
852531ee1a6SVladimir Oltean static int ocelot_fdb_del(struct ocelot *ocelot, int port,
853531ee1a6SVladimir Oltean 			  const unsigned char *addr, u16 vid)
854531ee1a6SVladimir Oltean {
855531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
856531ee1a6SVladimir Oltean }
857531ee1a6SVladimir Oltean 
858531ee1a6SVladimir Oltean static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
859a556c76aSAlexandre Belloni 			       struct net_device *dev,
860a556c76aSAlexandre Belloni 			       const unsigned char *addr, u16 vid)
861a556c76aSAlexandre Belloni {
862531ee1a6SVladimir Oltean 	struct ocelot_port *ocelot_port = netdev_priv(dev);
863531ee1a6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
864a556c76aSAlexandre Belloni 
865531ee1a6SVladimir Oltean 	return ocelot_fdb_del(ocelot, ocelot_port->chip_port, addr, vid);
866a556c76aSAlexandre Belloni }
867a556c76aSAlexandre Belloni 
868a556c76aSAlexandre Belloni struct ocelot_dump_ctx {
869a556c76aSAlexandre Belloni 	struct net_device *dev;
870a556c76aSAlexandre Belloni 	struct sk_buff *skb;
871a556c76aSAlexandre Belloni 	struct netlink_callback *cb;
872a556c76aSAlexandre Belloni 	int idx;
873a556c76aSAlexandre Belloni };
874a556c76aSAlexandre Belloni 
875531ee1a6SVladimir Oltean static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
876531ee1a6SVladimir Oltean 				   bool is_static, void *data)
877a556c76aSAlexandre Belloni {
878531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
879a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
880a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
881a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
882a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
883a556c76aSAlexandre Belloni 
884a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
885a556c76aSAlexandre Belloni 		goto skip;
886a556c76aSAlexandre Belloni 
887a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
888a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
889a556c76aSAlexandre Belloni 	if (!nlh)
890a556c76aSAlexandre Belloni 		return -EMSGSIZE;
891a556c76aSAlexandre Belloni 
892a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
893a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
894a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
895a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
896a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
897a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
898a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
899531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
900a556c76aSAlexandre Belloni 
901531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
902a556c76aSAlexandre Belloni 		goto nla_put_failure;
903a556c76aSAlexandre Belloni 
904531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
905a556c76aSAlexandre Belloni 		goto nla_put_failure;
906a556c76aSAlexandre Belloni 
907a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
908a556c76aSAlexandre Belloni 
909a556c76aSAlexandre Belloni skip:
910a556c76aSAlexandre Belloni 	dump->idx++;
911a556c76aSAlexandre Belloni 	return 0;
912a556c76aSAlexandre Belloni 
913a556c76aSAlexandre Belloni nla_put_failure:
914a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
915a556c76aSAlexandre Belloni 	return -EMSGSIZE;
916a556c76aSAlexandre Belloni }
917a556c76aSAlexandre Belloni 
918531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
919a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
920a556c76aSAlexandre Belloni {
921a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
922531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
923a556c76aSAlexandre Belloni 
924a556c76aSAlexandre Belloni 	/* Set row and column to read from */
925a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
926a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
927a556c76aSAlexandre Belloni 
928a556c76aSAlexandre Belloni 	/* Issue a read command */
929a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
930a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
931a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
932a556c76aSAlexandre Belloni 
933a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
934a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
935a556c76aSAlexandre Belloni 
936a556c76aSAlexandre Belloni 	/* Read the entry flags */
937a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
938a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
939a556c76aSAlexandre Belloni 		return -EINVAL;
940a556c76aSAlexandre Belloni 
941a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
942a556c76aSAlexandre Belloni 	 * do not report it.
943a556c76aSAlexandre Belloni 	 */
944a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
945531ee1a6SVladimir Oltean 	if (dst != port)
946a556c76aSAlexandre Belloni 		return -EINVAL;
947a556c76aSAlexandre Belloni 
948a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
949a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
950a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
951a556c76aSAlexandre Belloni 
952a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
953a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
954a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
955a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
956a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
957a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
958a556c76aSAlexandre Belloni 
959a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
960a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
961a556c76aSAlexandre Belloni 
962a556c76aSAlexandre Belloni 	return 0;
963a556c76aSAlexandre Belloni }
964a556c76aSAlexandre Belloni 
965531ee1a6SVladimir Oltean static int ocelot_fdb_dump(struct ocelot *ocelot, int port,
966531ee1a6SVladimir Oltean 			   dsa_fdb_dump_cb_t *cb, void *data)
967a556c76aSAlexandre Belloni {
968531ee1a6SVladimir Oltean 	int i, j;
969a556c76aSAlexandre Belloni 
970a556c76aSAlexandre Belloni 	/* Loop through all the mac tables entries. There are 1024 rows of 4
971a556c76aSAlexandre Belloni 	 * entries.
972a556c76aSAlexandre Belloni 	 */
973a556c76aSAlexandre Belloni 	for (i = 0; i < 1024; i++) {
974a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
975531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
976531ee1a6SVladimir Oltean 			bool is_static;
977531ee1a6SVladimir Oltean 			int ret;
978531ee1a6SVladimir Oltean 
979531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
980a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
981a556c76aSAlexandre Belloni 			 * skip it.
982a556c76aSAlexandre Belloni 			 */
983a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
984a556c76aSAlexandre Belloni 				continue;
985a556c76aSAlexandre Belloni 			else if (ret)
986531ee1a6SVladimir Oltean 				return ret;
987a556c76aSAlexandre Belloni 
988531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
989531ee1a6SVladimir Oltean 
990531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
991a556c76aSAlexandre Belloni 			if (ret)
992531ee1a6SVladimir Oltean 				return ret;
993a556c76aSAlexandre Belloni 		}
994a556c76aSAlexandre Belloni 	}
995a556c76aSAlexandre Belloni 
996531ee1a6SVladimir Oltean 	return 0;
997531ee1a6SVladimir Oltean }
998531ee1a6SVladimir Oltean 
999531ee1a6SVladimir Oltean static int ocelot_port_fdb_dump(struct sk_buff *skb,
1000531ee1a6SVladimir Oltean 				struct netlink_callback *cb,
1001531ee1a6SVladimir Oltean 				struct net_device *dev,
1002531ee1a6SVladimir Oltean 				struct net_device *filter_dev, int *idx)
1003531ee1a6SVladimir Oltean {
1004531ee1a6SVladimir Oltean 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1005531ee1a6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1006531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx dump = {
1007531ee1a6SVladimir Oltean 		.dev = dev,
1008531ee1a6SVladimir Oltean 		.skb = skb,
1009531ee1a6SVladimir Oltean 		.cb = cb,
1010531ee1a6SVladimir Oltean 		.idx = *idx,
1011531ee1a6SVladimir Oltean 	};
1012531ee1a6SVladimir Oltean 	int ret;
1013531ee1a6SVladimir Oltean 
1014531ee1a6SVladimir Oltean 	ret = ocelot_fdb_dump(ocelot, ocelot_port->chip_port,
1015531ee1a6SVladimir Oltean 			      ocelot_port_fdb_do_dump, &dump);
1016531ee1a6SVladimir Oltean 
1017a556c76aSAlexandre Belloni 	*idx = dump.idx;
1018531ee1a6SVladimir Oltean 
1019a556c76aSAlexandre Belloni 	return ret;
1020a556c76aSAlexandre Belloni }
1021a556c76aSAlexandre Belloni 
10227142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
10237142529fSAntoine Tenart 				  u16 vid)
10247142529fSAntoine Tenart {
10251c44ce56SVladimir Oltean 	return ocelot_vlan_vid_add(dev, vid, false, false);
10267142529fSAntoine Tenart }
10277142529fSAntoine Tenart 
10287142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
10297142529fSAntoine Tenart 				   u16 vid)
10307142529fSAntoine Tenart {
10317142529fSAntoine Tenart 	return ocelot_vlan_vid_del(dev, vid);
10327142529fSAntoine Tenart }
10337142529fSAntoine Tenart 
10347142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev,
10357142529fSAntoine Tenart 			       netdev_features_t features)
10367142529fSAntoine Tenart {
10377142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
10387142529fSAntoine Tenart 	netdev_features_t changed = dev->features ^ features;
10397142529fSAntoine Tenart 
10402c1d029aSJoergen Andreasen 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
10412c1d029aSJoergen Andreasen 	    port->tc.offload_cnt) {
10422c1d029aSJoergen Andreasen 		netdev_err(dev,
10432c1d029aSJoergen Andreasen 			   "Cannot disable HW TC offload while offloads active\n");
10442c1d029aSJoergen Andreasen 		return -EBUSY;
10452c1d029aSJoergen Andreasen 	}
10462c1d029aSJoergen Andreasen 
10477142529fSAntoine Tenart 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
10487142529fSAntoine Tenart 		ocelot_vlan_mode(port, features);
10497142529fSAntoine Tenart 
10507142529fSAntoine Tenart 	return 0;
10517142529fSAntoine Tenart }
10527142529fSAntoine Tenart 
1053751302c3SFlorian Fainelli static int ocelot_get_port_parent_id(struct net_device *dev,
1054751302c3SFlorian Fainelli 				     struct netdev_phys_item_id *ppid)
1055751302c3SFlorian Fainelli {
1056751302c3SFlorian Fainelli 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1057751302c3SFlorian Fainelli 	struct ocelot *ocelot = ocelot_port->ocelot;
1058751302c3SFlorian Fainelli 
1059751302c3SFlorian Fainelli 	ppid->id_len = sizeof(ocelot->base_mac);
1060751302c3SFlorian Fainelli 	memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
1061751302c3SFlorian Fainelli 
1062751302c3SFlorian Fainelli 	return 0;
1063751302c3SFlorian Fainelli }
1064751302c3SFlorian Fainelli 
1065*306fd44bSVladimir Oltean static int ocelot_hwstamp_get(struct ocelot *ocelot, int port,
1066*306fd44bSVladimir Oltean 			      struct ifreq *ifr)
10674e3b0468SAntoine Tenart {
10684e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
10694e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
10704e3b0468SAntoine Tenart }
10714e3b0468SAntoine Tenart 
1072*306fd44bSVladimir Oltean static int ocelot_hwstamp_set(struct ocelot *ocelot, int port,
1073*306fd44bSVladimir Oltean 			      struct ifreq *ifr)
10744e3b0468SAntoine Tenart {
1075*306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10764e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
10774e3b0468SAntoine Tenart 
10784e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
10794e3b0468SAntoine Tenart 		return -EFAULT;
10804e3b0468SAntoine Tenart 
10814e3b0468SAntoine Tenart 	/* reserved for future extensions */
10824e3b0468SAntoine Tenart 	if (cfg.flags)
10834e3b0468SAntoine Tenart 		return -EINVAL;
10844e3b0468SAntoine Tenart 
10854e3b0468SAntoine Tenart 	/* Tx type sanity check */
10864e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
10874e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1088*306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
10894e3b0468SAntoine Tenart 		break;
10904e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
10914e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
10924e3b0468SAntoine Tenart 		 * need to update the origin time.
10934e3b0468SAntoine Tenart 		 */
1094*306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
10954e3b0468SAntoine Tenart 		break;
10964e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1097*306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
10984e3b0468SAntoine Tenart 		break;
10994e3b0468SAntoine Tenart 	default:
11004e3b0468SAntoine Tenart 		return -ERANGE;
11014e3b0468SAntoine Tenart 	}
11024e3b0468SAntoine Tenart 
11034e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
11044e3b0468SAntoine Tenart 
11054e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
11064e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
11074e3b0468SAntoine Tenart 		break;
11084e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
11094e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
11104e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
11114e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
11124e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
11134e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
11144e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
11154e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
11164e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
11174e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
11184e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
11194e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
11204e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
11214e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
11224e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
11234e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
11244e3b0468SAntoine Tenart 		break;
11254e3b0468SAntoine Tenart 	default:
11264e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
11274e3b0468SAntoine Tenart 		return -ERANGE;
11284e3b0468SAntoine Tenart 	}
11294e3b0468SAntoine Tenart 
11304e3b0468SAntoine Tenart 	/* Commit back the result & save it */
11314e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
11324e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
11334e3b0468SAntoine Tenart 
11344e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
11354e3b0468SAntoine Tenart }
11364e3b0468SAntoine Tenart 
11374e3b0468SAntoine Tenart static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11384e3b0468SAntoine Tenart {
1139*306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1140*306fd44bSVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1141*306fd44bSVladimir Oltean 	int port = ocelot_port->chip_port;
11424e3b0468SAntoine Tenart 
11434e3b0468SAntoine Tenart 	/* The function is only used for PTP operations for now */
11444e3b0468SAntoine Tenart 	if (!ocelot->ptp)
11454e3b0468SAntoine Tenart 		return -EOPNOTSUPP;
11464e3b0468SAntoine Tenart 
11474e3b0468SAntoine Tenart 	switch (cmd) {
11484e3b0468SAntoine Tenart 	case SIOCSHWTSTAMP:
1149*306fd44bSVladimir Oltean 		return ocelot_hwstamp_set(ocelot, port, ifr);
11504e3b0468SAntoine Tenart 	case SIOCGHWTSTAMP:
1151*306fd44bSVladimir Oltean 		return ocelot_hwstamp_get(ocelot, port, ifr);
11524e3b0468SAntoine Tenart 	default:
11534e3b0468SAntoine Tenart 		return -EOPNOTSUPP;
11544e3b0468SAntoine Tenart 	}
11554e3b0468SAntoine Tenart }
11564e3b0468SAntoine Tenart 
1157a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = {
1158a556c76aSAlexandre Belloni 	.ndo_open			= ocelot_port_open,
1159a556c76aSAlexandre Belloni 	.ndo_stop			= ocelot_port_stop,
1160a556c76aSAlexandre Belloni 	.ndo_start_xmit			= ocelot_port_xmit,
1161a556c76aSAlexandre Belloni 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
1162a556c76aSAlexandre Belloni 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
1163a556c76aSAlexandre Belloni 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
1164a556c76aSAlexandre Belloni 	.ndo_get_stats64		= ocelot_get_stats64,
1165531ee1a6SVladimir Oltean 	.ndo_fdb_add			= ocelot_port_fdb_add,
1166531ee1a6SVladimir Oltean 	.ndo_fdb_del			= ocelot_port_fdb_del,
1167531ee1a6SVladimir Oltean 	.ndo_fdb_dump			= ocelot_port_fdb_dump,
11687142529fSAntoine Tenart 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
11697142529fSAntoine Tenart 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
11707142529fSAntoine Tenart 	.ndo_set_features		= ocelot_set_features,
1171751302c3SFlorian Fainelli 	.ndo_get_port_parent_id		= ocelot_get_port_parent_id,
11722c1d029aSJoergen Andreasen 	.ndo_setup_tc			= ocelot_setup_tc,
11734e3b0468SAntoine Tenart 	.ndo_do_ioctl			= ocelot_ioctl,
1174a556c76aSAlexandre Belloni };
1175a556c76aSAlexandre Belloni 
1176a556c76aSAlexandre Belloni static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
1177a556c76aSAlexandre Belloni {
1178a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(netdev);
1179a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1180a556c76aSAlexandre Belloni 	int i;
1181a556c76aSAlexandre Belloni 
1182a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1183a556c76aSAlexandre Belloni 		return;
1184a556c76aSAlexandre Belloni 
1185a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1186a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1187a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1188a556c76aSAlexandre Belloni }
1189a556c76aSAlexandre Belloni 
11901e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1191a556c76aSAlexandre Belloni {
1192a556c76aSAlexandre Belloni 	int i, j;
1193a556c76aSAlexandre Belloni 
1194a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1195a556c76aSAlexandre Belloni 
1196a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1197a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1198a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1199a556c76aSAlexandre Belloni 
1200a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1201a556c76aSAlexandre Belloni 			u32 val;
1202a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1203a556c76aSAlexandre Belloni 
1204a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1205a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1206a556c76aSAlexandre Belloni 
1207a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1208a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1209a556c76aSAlexandre Belloni 
1210a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1211a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1212a556c76aSAlexandre Belloni 		}
1213a556c76aSAlexandre Belloni 	}
1214a556c76aSAlexandre Belloni 
12151e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
12161e1caa97SClaudiu Manoil }
12171e1caa97SClaudiu Manoil 
12181e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
12191e1caa97SClaudiu Manoil {
12201e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
12211e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
12221e1caa97SClaudiu Manoil 					     stats_work);
12231e1caa97SClaudiu Manoil 
12241e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
12251e1caa97SClaudiu Manoil 
1226a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1227a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1228a556c76aSAlexandre Belloni }
1229a556c76aSAlexandre Belloni 
1230a556c76aSAlexandre Belloni static void ocelot_get_ethtool_stats(struct net_device *dev,
1231a556c76aSAlexandre Belloni 				     struct ethtool_stats *stats, u64 *data)
1232a556c76aSAlexandre Belloni {
1233a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1234a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1235a556c76aSAlexandre Belloni 	int i;
1236a556c76aSAlexandre Belloni 
1237a556c76aSAlexandre Belloni 	/* check and update now */
12381e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1239a556c76aSAlexandre Belloni 
1240a556c76aSAlexandre Belloni 	/* Copy all counters */
1241a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1242a556c76aSAlexandre Belloni 		*data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
1243a556c76aSAlexandre Belloni }
1244a556c76aSAlexandre Belloni 
1245a556c76aSAlexandre Belloni static int ocelot_get_sset_count(struct net_device *dev, int sset)
1246a556c76aSAlexandre Belloni {
1247a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1248a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1249a556c76aSAlexandre Belloni 
1250a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1251a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1252a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1253a556c76aSAlexandre Belloni }
1254a556c76aSAlexandre Belloni 
12554e3b0468SAntoine Tenart static int ocelot_get_ts_info(struct net_device *dev,
12564e3b0468SAntoine Tenart 			      struct ethtool_ts_info *info)
12574e3b0468SAntoine Tenart {
12584e3b0468SAntoine Tenart 	struct ocelot_port *ocelot_port = netdev_priv(dev);
12594e3b0468SAntoine Tenart 	struct ocelot *ocelot = ocelot_port->ocelot;
12604e3b0468SAntoine Tenart 
12614e3b0468SAntoine Tenart 	if (!ocelot->ptp)
12624e3b0468SAntoine Tenart 		return ethtool_op_get_ts_info(dev, info);
12634e3b0468SAntoine Tenart 
12644e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
12654e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
12664e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
12674e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
12684e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
12694e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
12704e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
12714e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
12724e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
12734e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
12744e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
12754e3b0468SAntoine Tenart 
12764e3b0468SAntoine Tenart 	return 0;
12774e3b0468SAntoine Tenart }
12784e3b0468SAntoine Tenart 
1279a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = {
1280a556c76aSAlexandre Belloni 	.get_strings		= ocelot_get_strings,
1281a556c76aSAlexandre Belloni 	.get_ethtool_stats	= ocelot_get_ethtool_stats,
1282a556c76aSAlexandre Belloni 	.get_sset_count		= ocelot_get_sset_count,
1283dc96ee37SAlexandre Belloni 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1284dc96ee37SAlexandre Belloni 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
12854e3b0468SAntoine Tenart 	.get_ts_info		= ocelot_get_ts_info,
1286a556c76aSAlexandre Belloni };
1287a556c76aSAlexandre Belloni 
1288a556c76aSAlexandre Belloni static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1289a556c76aSAlexandre Belloni 					  struct switchdev_trans *trans,
1290a556c76aSAlexandre Belloni 					  u8 state)
1291a556c76aSAlexandre Belloni {
1292a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1293a556c76aSAlexandre Belloni 	u32 port_cfg;
1294a556c76aSAlexandre Belloni 	int port, i;
1295a556c76aSAlexandre Belloni 
1296a556c76aSAlexandre Belloni 	if (switchdev_trans_ph_prepare(trans))
1297a556c76aSAlexandre Belloni 		return 0;
1298a556c76aSAlexandre Belloni 
1299a556c76aSAlexandre Belloni 	if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1300a556c76aSAlexandre Belloni 		return 0;
1301a556c76aSAlexandre Belloni 
1302a556c76aSAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1303a556c76aSAlexandre Belloni 				   ocelot_port->chip_port);
1304a556c76aSAlexandre Belloni 
1305a556c76aSAlexandre Belloni 	switch (state) {
1306a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
1307a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1308a556c76aSAlexandre Belloni 		/* Fallthrough */
1309a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1310a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1311a556c76aSAlexandre Belloni 		break;
1312a556c76aSAlexandre Belloni 
1313a556c76aSAlexandre Belloni 	default:
1314a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1315a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1316a556c76aSAlexandre Belloni 		break;
1317a556c76aSAlexandre Belloni 	}
1318a556c76aSAlexandre Belloni 
1319a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1320a556c76aSAlexandre Belloni 			 ocelot_port->chip_port);
1321a556c76aSAlexandre Belloni 
1322a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1323a556c76aSAlexandre Belloni 	 * a source for the other ports.
1324a556c76aSAlexandre Belloni 	 */
1325a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1326a556c76aSAlexandre Belloni 		if (ocelot->bridge_fwd_mask & BIT(port)) {
1327a556c76aSAlexandre Belloni 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1328a556c76aSAlexandre Belloni 
1329a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1330a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
1331a556c76aSAlexandre Belloni 
1332a556c76aSAlexandre Belloni 				if (!bond_mask)
1333a556c76aSAlexandre Belloni 					continue;
1334a556c76aSAlexandre Belloni 
1335a556c76aSAlexandre Belloni 				if (bond_mask & BIT(port)) {
1336a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
1337a556c76aSAlexandre Belloni 					break;
1338a556c76aSAlexandre Belloni 				}
1339a556c76aSAlexandre Belloni 			}
1340a556c76aSAlexandre Belloni 
1341a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1342a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports) | mask,
1343a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1344a556c76aSAlexandre Belloni 		} else {
1345a556c76aSAlexandre Belloni 			/* Only the CPU port, this is compatible with link
1346a556c76aSAlexandre Belloni 			 * aggregation.
1347a556c76aSAlexandre Belloni 			 */
1348a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1349a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports),
1350a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1351a556c76aSAlexandre Belloni 		}
1352a556c76aSAlexandre Belloni 	}
1353a556c76aSAlexandre Belloni 
1354a556c76aSAlexandre Belloni 	return 0;
1355a556c76aSAlexandre Belloni }
1356a556c76aSAlexandre Belloni 
1357a556c76aSAlexandre Belloni static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1358a556c76aSAlexandre Belloni 					unsigned long ageing_clock_t)
1359a556c76aSAlexandre Belloni {
1360a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1361a556c76aSAlexandre Belloni 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1362a556c76aSAlexandre Belloni 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1363a556c76aSAlexandre Belloni 
1364a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1365a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1366a556c76aSAlexandre Belloni }
1367a556c76aSAlexandre Belloni 
1368a556c76aSAlexandre Belloni static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1369a556c76aSAlexandre Belloni {
1370a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1371a556c76aSAlexandre Belloni 	u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1372a556c76aSAlexandre Belloni 				  port->chip_port);
1373a556c76aSAlexandre Belloni 
1374a556c76aSAlexandre Belloni 	if (mc)
1375a556c76aSAlexandre Belloni 		val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1376a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1377a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1378a556c76aSAlexandre Belloni 	else
1379a556c76aSAlexandre Belloni 		val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1380a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1381a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1382a556c76aSAlexandre Belloni 
1383a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1384a556c76aSAlexandre Belloni }
1385a556c76aSAlexandre Belloni 
1386a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev,
1387a556c76aSAlexandre Belloni 				const struct switchdev_attr *attr,
1388a556c76aSAlexandre Belloni 				struct switchdev_trans *trans)
1389a556c76aSAlexandre Belloni {
1390a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
139197bb69e1SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1392a556c76aSAlexandre Belloni 	int err = 0;
1393a556c76aSAlexandre Belloni 
1394a556c76aSAlexandre Belloni 	switch (attr->id) {
1395a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1396a556c76aSAlexandre Belloni 		ocelot_port_attr_stp_state_set(ocelot_port, trans,
1397a556c76aSAlexandre Belloni 					       attr->u.stp_state);
1398a556c76aSAlexandre Belloni 		break;
1399a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1400a556c76aSAlexandre Belloni 		ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1401a556c76aSAlexandre Belloni 		break;
14027142529fSAntoine Tenart 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
140397bb69e1SVladimir Oltean 		ocelot_port_vlan_filtering(ocelot, ocelot_port->chip_port,
140497bb69e1SVladimir Oltean 					   attr->u.vlan_filtering);
14057142529fSAntoine Tenart 		break;
1406a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1407a556c76aSAlexandre Belloni 		ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1408a556c76aSAlexandre Belloni 		break;
1409a556c76aSAlexandre Belloni 	default:
1410a556c76aSAlexandre Belloni 		err = -EOPNOTSUPP;
1411a556c76aSAlexandre Belloni 		break;
1412a556c76aSAlexandre Belloni 	}
1413a556c76aSAlexandre Belloni 
1414a556c76aSAlexandre Belloni 	return err;
1415a556c76aSAlexandre Belloni }
1416a556c76aSAlexandre Belloni 
14177142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev,
14187142529fSAntoine Tenart 				    const struct switchdev_obj_port_vlan *vlan,
14197142529fSAntoine Tenart 				    struct switchdev_trans *trans)
14207142529fSAntoine Tenart {
14217142529fSAntoine Tenart 	int ret;
14227142529fSAntoine Tenart 	u16 vid;
14237142529fSAntoine Tenart 
14247142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
14257142529fSAntoine Tenart 		ret = ocelot_vlan_vid_add(dev, vid,
14267142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
14277142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
14287142529fSAntoine Tenart 		if (ret)
14297142529fSAntoine Tenart 			return ret;
14307142529fSAntoine Tenart 	}
14317142529fSAntoine Tenart 
14327142529fSAntoine Tenart 	return 0;
14337142529fSAntoine Tenart }
14347142529fSAntoine Tenart 
14357142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev,
14367142529fSAntoine Tenart 				     const struct switchdev_obj_port_vlan *vlan)
14377142529fSAntoine Tenart {
14387142529fSAntoine Tenart 	int ret;
14397142529fSAntoine Tenart 	u16 vid;
14407142529fSAntoine Tenart 
14417142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
14427142529fSAntoine Tenart 		ret = ocelot_vlan_vid_del(dev, vid);
14437142529fSAntoine Tenart 
14447142529fSAntoine Tenart 		if (ret)
14457142529fSAntoine Tenart 			return ret;
14467142529fSAntoine Tenart 	}
14477142529fSAntoine Tenart 
14487142529fSAntoine Tenart 	return 0;
14497142529fSAntoine Tenart }
14507142529fSAntoine Tenart 
1451a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1452a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1453a556c76aSAlexandre Belloni 						     u16 vid)
1454a556c76aSAlexandre Belloni {
1455a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1456a556c76aSAlexandre Belloni 
1457a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1458a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1459a556c76aSAlexandre Belloni 			return mc;
1460a556c76aSAlexandre Belloni 	}
1461a556c76aSAlexandre Belloni 
1462a556c76aSAlexandre Belloni 	return NULL;
1463a556c76aSAlexandre Belloni }
1464a556c76aSAlexandre Belloni 
1465a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev,
1466a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb,
1467a556c76aSAlexandre Belloni 				   struct switchdev_trans *trans)
1468a556c76aSAlexandre Belloni {
1469a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1470a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1471a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1472a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1473a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1474a556c76aSAlexandre Belloni 	bool new = false;
1475a556c76aSAlexandre Belloni 
1476a556c76aSAlexandre Belloni 	if (!vid)
14777142529fSAntoine Tenart 		vid = port->pvid;
1478a556c76aSAlexandre Belloni 
1479a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1480a556c76aSAlexandre Belloni 	if (!mc) {
1481a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1482a556c76aSAlexandre Belloni 		if (!mc)
1483a556c76aSAlexandre Belloni 			return -ENOMEM;
1484a556c76aSAlexandre Belloni 
1485a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1486a556c76aSAlexandre Belloni 		mc->vid = vid;
1487a556c76aSAlexandre Belloni 
1488a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1489a556c76aSAlexandre Belloni 		new = true;
1490a556c76aSAlexandre Belloni 	}
1491a556c76aSAlexandre Belloni 
1492a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1493a556c76aSAlexandre Belloni 	addr[0] = 0;
1494a556c76aSAlexandre Belloni 
1495a556c76aSAlexandre Belloni 	if (!new) {
1496a556c76aSAlexandre Belloni 		addr[2] = mc->ports << 0;
1497a556c76aSAlexandre Belloni 		addr[1] = mc->ports << 8;
1498a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1499a556c76aSAlexandre Belloni 	}
1500a556c76aSAlexandre Belloni 
1501a556c76aSAlexandre Belloni 	mc->ports |= BIT(port->chip_port);
1502a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1503a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1504a556c76aSAlexandre Belloni 
1505a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1506a556c76aSAlexandre Belloni }
1507a556c76aSAlexandre Belloni 
1508a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev,
1509a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb)
1510a556c76aSAlexandre Belloni {
1511a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1512a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1513a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1514a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1515a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1516a556c76aSAlexandre Belloni 
1517a556c76aSAlexandre Belloni 	if (!vid)
15187142529fSAntoine Tenart 		vid = port->pvid;
1519a556c76aSAlexandre Belloni 
1520a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1521a556c76aSAlexandre Belloni 	if (!mc)
1522a556c76aSAlexandre Belloni 		return -ENOENT;
1523a556c76aSAlexandre Belloni 
1524a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1525a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1526a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1527a556c76aSAlexandre Belloni 	addr[0] = 0;
1528a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1529a556c76aSAlexandre Belloni 
1530a556c76aSAlexandre Belloni 	mc->ports &= ~BIT(port->chip_port);
1531a556c76aSAlexandre Belloni 	if (!mc->ports) {
1532a556c76aSAlexandre Belloni 		list_del(&mc->list);
1533a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1534a556c76aSAlexandre Belloni 		return 0;
1535a556c76aSAlexandre Belloni 	}
1536a556c76aSAlexandre Belloni 
1537a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1538a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1539a556c76aSAlexandre Belloni 
1540a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1541a556c76aSAlexandre Belloni }
1542a556c76aSAlexandre Belloni 
1543a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev,
1544a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj,
154569213513SPetr Machata 			       struct switchdev_trans *trans,
154669213513SPetr Machata 			       struct netlink_ext_ack *extack)
1547a556c76aSAlexandre Belloni {
1548a556c76aSAlexandre Belloni 	int ret = 0;
1549a556c76aSAlexandre Belloni 
1550a556c76aSAlexandre Belloni 	switch (obj->id) {
15517142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
15527142529fSAntoine Tenart 		ret = ocelot_port_obj_add_vlan(dev,
15537142529fSAntoine Tenart 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
15547142529fSAntoine Tenart 					       trans);
15557142529fSAntoine Tenart 		break;
1556a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1557a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1558a556c76aSAlexandre Belloni 					      trans);
1559a556c76aSAlexandre Belloni 		break;
1560a556c76aSAlexandre Belloni 	default:
1561a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1562a556c76aSAlexandre Belloni 	}
1563a556c76aSAlexandre Belloni 
1564a556c76aSAlexandre Belloni 	return ret;
1565a556c76aSAlexandre Belloni }
1566a556c76aSAlexandre Belloni 
1567a556c76aSAlexandre Belloni static int ocelot_port_obj_del(struct net_device *dev,
1568a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj)
1569a556c76aSAlexandre Belloni {
1570a556c76aSAlexandre Belloni 	int ret = 0;
1571a556c76aSAlexandre Belloni 
1572a556c76aSAlexandre Belloni 	switch (obj->id) {
15737142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
15747142529fSAntoine Tenart 		ret = ocelot_port_vlan_del_vlan(dev,
15757142529fSAntoine Tenart 						SWITCHDEV_OBJ_PORT_VLAN(obj));
15767142529fSAntoine Tenart 		break;
1577a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1578a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1579a556c76aSAlexandre Belloni 		break;
1580a556c76aSAlexandre Belloni 	default:
1581a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1582a556c76aSAlexandre Belloni 	}
1583a556c76aSAlexandre Belloni 
1584a556c76aSAlexandre Belloni 	return ret;
1585a556c76aSAlexandre Belloni }
1586a556c76aSAlexandre Belloni 
1587a556c76aSAlexandre Belloni static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1588a556c76aSAlexandre Belloni 				   struct net_device *bridge)
1589a556c76aSAlexandre Belloni {
1590a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1591a556c76aSAlexandre Belloni 
1592a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1593a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1594a556c76aSAlexandre Belloni 	} else {
1595a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1596a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1597a556c76aSAlexandre Belloni 			 * unsupported */
1598a556c76aSAlexandre Belloni 			return -ENODEV;
1599a556c76aSAlexandre Belloni 	}
1600a556c76aSAlexandre Belloni 
1601a556c76aSAlexandre Belloni 	ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1602a556c76aSAlexandre Belloni 
1603a556c76aSAlexandre Belloni 	return 0;
1604a556c76aSAlexandre Belloni }
1605a556c76aSAlexandre Belloni 
160697bb69e1SVladimir Oltean static int ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1607a556c76aSAlexandre Belloni 				    struct net_device *bridge)
1608a556c76aSAlexandre Belloni {
1609a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
161097bb69e1SVladimir Oltean 	int port = ocelot_port->chip_port;
1611a556c76aSAlexandre Belloni 
161297bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1613a556c76aSAlexandre Belloni 
1614a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1615a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
16167142529fSAntoine Tenart 
161797bb69e1SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, 0);
161897bb69e1SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, 0);
161997bb69e1SVladimir Oltean 	return ocelot_port_set_native_vlan(ocelot, port, 0);
1620a556c76aSAlexandre Belloni }
1621a556c76aSAlexandre Belloni 
1622dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1623dc96ee37SAlexandre Belloni {
1624dc96ee37SAlexandre Belloni 	int i, port, lag;
1625dc96ee37SAlexandre Belloni 
1626dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1627dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1628dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1629dc96ee37SAlexandre Belloni 
1630dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1631dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1632dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1633dc96ee37SAlexandre Belloni 
1634dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1635dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1636dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1637dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1638dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1639dc96ee37SAlexandre Belloni 
1640dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1641dc96ee37SAlexandre Belloni 		if (!bond_mask)
1642dc96ee37SAlexandre Belloni 			continue;
1643dc96ee37SAlexandre Belloni 
1644dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1645dc96ee37SAlexandre Belloni 			// Destination mask
1646dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1647dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1648dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1649dc96ee37SAlexandre Belloni 			aggr_count++;
1650dc96ee37SAlexandre Belloni 		}
1651dc96ee37SAlexandre Belloni 
1652dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1653dc96ee37SAlexandre Belloni 			u32 ac;
1654dc96ee37SAlexandre Belloni 
1655dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1656dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1657dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1658dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1659dc96ee37SAlexandre Belloni 		}
1660dc96ee37SAlexandre Belloni 	}
1661dc96ee37SAlexandre Belloni }
1662dc96ee37SAlexandre Belloni 
1663dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1664dc96ee37SAlexandre Belloni {
1665dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1666dc96ee37SAlexandre Belloni 	unsigned int p;
1667dc96ee37SAlexandre Belloni 
1668dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1669dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1670dc96ee37SAlexandre Belloni 
1671dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1672dc96ee37SAlexandre Belloni 
1673dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1674dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1675dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1676dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1677dc96ee37SAlexandre Belloni 	}
1678dc96ee37SAlexandre Belloni }
1679dc96ee37SAlexandre Belloni 
1680dc96ee37SAlexandre Belloni static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1681dc96ee37SAlexandre Belloni 				struct net_device *bond)
1682dc96ee37SAlexandre Belloni {
1683dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1684dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1685dc96ee37SAlexandre Belloni 	int lag, lp;
1686dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1687dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1688dc96ee37SAlexandre Belloni 
1689dc96ee37SAlexandre Belloni 	rcu_read_lock();
1690dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1691dc96ee37SAlexandre Belloni 		struct ocelot_port *port = netdev_priv(ndev);
1692dc96ee37SAlexandre Belloni 
1693dc96ee37SAlexandre Belloni 		bond_mask |= BIT(port->chip_port);
1694dc96ee37SAlexandre Belloni 	}
1695dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1696dc96ee37SAlexandre Belloni 
1697dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1698dc96ee37SAlexandre Belloni 
1699dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1700dc96ee37SAlexandre Belloni 	 * now on
1701dc96ee37SAlexandre Belloni 	 */
1702dc96ee37SAlexandre Belloni 	if (p == lp) {
1703dc96ee37SAlexandre Belloni 		lag = p;
1704dc96ee37SAlexandre Belloni 		ocelot->lags[p] = bond_mask;
1705dc96ee37SAlexandre Belloni 		bond_mask &= ~BIT(p);
1706dc96ee37SAlexandre Belloni 		if (bond_mask) {
1707dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1708dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1709dc96ee37SAlexandre Belloni 		}
1710dc96ee37SAlexandre Belloni 	} else {
1711dc96ee37SAlexandre Belloni 		lag = lp;
1712dc96ee37SAlexandre Belloni 		ocelot->lags[lp] |= BIT(p);
1713dc96ee37SAlexandre Belloni 	}
1714dc96ee37SAlexandre Belloni 
1715dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1716dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1717dc96ee37SAlexandre Belloni 
1718dc96ee37SAlexandre Belloni 	return 0;
1719dc96ee37SAlexandre Belloni }
1720dc96ee37SAlexandre Belloni 
1721dc96ee37SAlexandre Belloni static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1722dc96ee37SAlexandre Belloni 				  struct net_device *bond)
1723dc96ee37SAlexandre Belloni {
1724dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1725dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1726dc96ee37SAlexandre Belloni 	u32 port_cfg;
1727dc96ee37SAlexandre Belloni 	int i;
1728dc96ee37SAlexandre Belloni 
1729dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1730dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1731dc96ee37SAlexandre Belloni 		ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1732dc96ee37SAlexandre Belloni 
1733dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1734dc96ee37SAlexandre Belloni 	 * next port
1735dc96ee37SAlexandre Belloni 	 */
1736dc96ee37SAlexandre Belloni 	if (ocelot->lags[p]) {
1737dc96ee37SAlexandre Belloni 		int n = __ffs(ocelot->lags[p]);
1738dc96ee37SAlexandre Belloni 
1739dc96ee37SAlexandre Belloni 		ocelot->lags[n] = ocelot->lags[p];
1740dc96ee37SAlexandre Belloni 		ocelot->lags[p] = 0;
1741dc96ee37SAlexandre Belloni 
1742dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1743dc96ee37SAlexandre Belloni 	}
1744dc96ee37SAlexandre Belloni 
1745dc96ee37SAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1746dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1747dc96ee37SAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1748dc96ee37SAlexandre Belloni 			 ANA_PORT_PORT_CFG, p);
1749dc96ee37SAlexandre Belloni 
1750dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1751dc96ee37SAlexandre Belloni }
1752dc96ee37SAlexandre Belloni 
1753a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */
1754a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1755a556c76aSAlexandre Belloni {
1756a556c76aSAlexandre Belloni 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1757a556c76aSAlexandre Belloni }
1758a556c76aSAlexandre Belloni 
1759a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev,
1760a556c76aSAlexandre Belloni 				       unsigned long event,
1761a556c76aSAlexandre Belloni 				       struct netdev_notifier_changeupper_info *info)
1762a556c76aSAlexandre Belloni {
1763a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1764a556c76aSAlexandre Belloni 	int err = 0;
1765a556c76aSAlexandre Belloni 
1766a556c76aSAlexandre Belloni 	switch (event) {
1767a556c76aSAlexandre Belloni 	case NETDEV_CHANGEUPPER:
1768a556c76aSAlexandre Belloni 		if (netif_is_bridge_master(info->upper_dev)) {
1769a556c76aSAlexandre Belloni 			if (info->linking)
1770a556c76aSAlexandre Belloni 				err = ocelot_port_bridge_join(ocelot_port,
1771a556c76aSAlexandre Belloni 							      info->upper_dev);
1772a556c76aSAlexandre Belloni 			else
177397bb69e1SVladimir Oltean 				err = ocelot_port_bridge_leave(ocelot_port,
1774a556c76aSAlexandre Belloni 							       info->upper_dev);
1775a556c76aSAlexandre Belloni 		}
1776dc96ee37SAlexandre Belloni 		if (netif_is_lag_master(info->upper_dev)) {
1777dc96ee37SAlexandre Belloni 			if (info->linking)
1778dc96ee37SAlexandre Belloni 				err = ocelot_port_lag_join(ocelot_port,
1779dc96ee37SAlexandre Belloni 							   info->upper_dev);
1780dc96ee37SAlexandre Belloni 			else
1781dc96ee37SAlexandre Belloni 				ocelot_port_lag_leave(ocelot_port,
1782dc96ee37SAlexandre Belloni 						      info->upper_dev);
1783dc96ee37SAlexandre Belloni 		}
1784a556c76aSAlexandre Belloni 		break;
1785a556c76aSAlexandre Belloni 	default:
1786a556c76aSAlexandre Belloni 		break;
1787a556c76aSAlexandre Belloni 	}
1788a556c76aSAlexandre Belloni 
1789a556c76aSAlexandre Belloni 	return err;
1790a556c76aSAlexandre Belloni }
1791a556c76aSAlexandre Belloni 
1792a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused,
1793a556c76aSAlexandre Belloni 				  unsigned long event, void *ptr)
1794a556c76aSAlexandre Belloni {
1795a556c76aSAlexandre Belloni 	struct netdev_notifier_changeupper_info *info = ptr;
1796a556c76aSAlexandre Belloni 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
17972ac0e152SGeert Uytterhoeven 	int ret = 0;
1798a556c76aSAlexandre Belloni 
17997afb3e57SClaudiu Manoil 	if (!ocelot_netdevice_dev_check(dev))
18007afb3e57SClaudiu Manoil 		return 0;
18017afb3e57SClaudiu Manoil 
1802dc96ee37SAlexandre Belloni 	if (event == NETDEV_PRECHANGEUPPER &&
1803dc96ee37SAlexandre Belloni 	    netif_is_lag_master(info->upper_dev)) {
1804dc96ee37SAlexandre Belloni 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1805dc96ee37SAlexandre Belloni 		struct netlink_ext_ack *extack;
1806dc96ee37SAlexandre Belloni 
18073b3eed8eSClaudiu Manoil 		if (lag_upper_info &&
18083b3eed8eSClaudiu Manoil 		    lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1809dc96ee37SAlexandre Belloni 			extack = netdev_notifier_info_to_extack(&info->info);
1810dc96ee37SAlexandre Belloni 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1811dc96ee37SAlexandre Belloni 
1812dc96ee37SAlexandre Belloni 			ret = -EINVAL;
1813dc96ee37SAlexandre Belloni 			goto notify;
1814dc96ee37SAlexandre Belloni 		}
1815dc96ee37SAlexandre Belloni 	}
1816dc96ee37SAlexandre Belloni 
1817a556c76aSAlexandre Belloni 	if (netif_is_lag_master(dev)) {
1818a556c76aSAlexandre Belloni 		struct net_device *slave;
1819a556c76aSAlexandre Belloni 		struct list_head *iter;
1820a556c76aSAlexandre Belloni 
1821a556c76aSAlexandre Belloni 		netdev_for_each_lower_dev(dev, slave, iter) {
1822a556c76aSAlexandre Belloni 			ret = ocelot_netdevice_port_event(slave, event, info);
1823a556c76aSAlexandre Belloni 			if (ret)
1824a556c76aSAlexandre Belloni 				goto notify;
1825a556c76aSAlexandre Belloni 		}
1826a556c76aSAlexandre Belloni 	} else {
1827a556c76aSAlexandre Belloni 		ret = ocelot_netdevice_port_event(dev, event, info);
1828a556c76aSAlexandre Belloni 	}
1829a556c76aSAlexandre Belloni 
1830a556c76aSAlexandre Belloni notify:
1831a556c76aSAlexandre Belloni 	return notifier_from_errno(ret);
1832a556c76aSAlexandre Belloni }
1833a556c76aSAlexandre Belloni 
1834a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = {
1835a556c76aSAlexandre Belloni 	.notifier_call = ocelot_netdevice_event,
1836a556c76aSAlexandre Belloni };
1837a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb);
1838a556c76aSAlexandre Belloni 
183956da64bcSFlorian Fainelli static int ocelot_switchdev_event(struct notifier_block *unused,
184056da64bcSFlorian Fainelli 				  unsigned long event, void *ptr)
184156da64bcSFlorian Fainelli {
184256da64bcSFlorian Fainelli 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
184356da64bcSFlorian Fainelli 	int err;
184456da64bcSFlorian Fainelli 
184556da64bcSFlorian Fainelli 	switch (event) {
184656da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
184756da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
184856da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
184956da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
185056da64bcSFlorian Fainelli 		return notifier_from_errno(err);
185156da64bcSFlorian Fainelli 	}
185256da64bcSFlorian Fainelli 
185356da64bcSFlorian Fainelli 	return NOTIFY_DONE;
185456da64bcSFlorian Fainelli }
185556da64bcSFlorian Fainelli 
185656da64bcSFlorian Fainelli struct notifier_block ocelot_switchdev_nb __read_mostly = {
185756da64bcSFlorian Fainelli 	.notifier_call = ocelot_switchdev_event,
185856da64bcSFlorian Fainelli };
185956da64bcSFlorian Fainelli EXPORT_SYMBOL(ocelot_switchdev_nb);
186056da64bcSFlorian Fainelli 
18610e332c85SPetr Machata static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
18620e332c85SPetr Machata 					   unsigned long event, void *ptr)
18630e332c85SPetr Machata {
18640e332c85SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
18650e332c85SPetr Machata 	int err;
18660e332c85SPetr Machata 
18670e332c85SPetr Machata 	switch (event) {
18680e332c85SPetr Machata 		/* Blocking events. */
18690e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
18700e332c85SPetr Machata 		err = switchdev_handle_port_obj_add(dev, ptr,
18710e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
18720e332c85SPetr Machata 						    ocelot_port_obj_add);
18730e332c85SPetr Machata 		return notifier_from_errno(err);
18740e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
18750e332c85SPetr Machata 		err = switchdev_handle_port_obj_del(dev, ptr,
18760e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
18770e332c85SPetr Machata 						    ocelot_port_obj_del);
18780e332c85SPetr Machata 		return notifier_from_errno(err);
187956da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
188056da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
188156da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
188256da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
188356da64bcSFlorian Fainelli 		return notifier_from_errno(err);
18840e332c85SPetr Machata 	}
18850e332c85SPetr Machata 
18860e332c85SPetr Machata 	return NOTIFY_DONE;
18870e332c85SPetr Machata }
18880e332c85SPetr Machata 
18890e332c85SPetr Machata struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
18900e332c85SPetr Machata 	.notifier_call = ocelot_switchdev_blocking_event,
18910e332c85SPetr Machata };
18920e332c85SPetr Machata EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
18930e332c85SPetr Machata 
18944e3b0468SAntoine Tenart int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
18954e3b0468SAntoine Tenart {
18964e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
18974e3b0468SAntoine Tenart 	unsigned long flags;
18984e3b0468SAntoine Tenart 	time64_t s;
18994e3b0468SAntoine Tenart 	u32 val;
19004e3b0468SAntoine Tenart 	s64 ns;
19014e3b0468SAntoine Tenart 
19024e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
19034e3b0468SAntoine Tenart 
19044e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19054e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19064e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
19074e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19084e3b0468SAntoine Tenart 
19094e3b0468SAntoine Tenart 	s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
19104e3b0468SAntoine Tenart 	s <<= 32;
19114e3b0468SAntoine Tenart 	s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
19124e3b0468SAntoine Tenart 	ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
19134e3b0468SAntoine Tenart 
19144e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19154e3b0468SAntoine Tenart 
19164e3b0468SAntoine Tenart 	/* Deal with negative values */
19174e3b0468SAntoine Tenart 	if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
19184e3b0468SAntoine Tenart 		s--;
19194e3b0468SAntoine Tenart 		ns &= 0xf;
19204e3b0468SAntoine Tenart 		ns += 999999984;
19214e3b0468SAntoine Tenart 	}
19224e3b0468SAntoine Tenart 
19234e3b0468SAntoine Tenart 	set_normalized_timespec64(ts, s, ns);
19244e3b0468SAntoine Tenart 	return 0;
19254e3b0468SAntoine Tenart }
19264e3b0468SAntoine Tenart EXPORT_SYMBOL(ocelot_ptp_gettime64);
19274e3b0468SAntoine Tenart 
19284e3b0468SAntoine Tenart static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
19294e3b0468SAntoine Tenart 				const struct timespec64 *ts)
19304e3b0468SAntoine Tenart {
19314e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
19324e3b0468SAntoine Tenart 	unsigned long flags;
19334e3b0468SAntoine Tenart 	u32 val;
19344e3b0468SAntoine Tenart 
19354e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
19364e3b0468SAntoine Tenart 
19374e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19384e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19394e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
19404e3b0468SAntoine Tenart 
19414e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19424e3b0468SAntoine Tenart 
19434e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
19444e3b0468SAntoine Tenart 			 TOD_ACC_PIN);
19454e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
19464e3b0468SAntoine Tenart 			 TOD_ACC_PIN);
19474e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
19484e3b0468SAntoine Tenart 
19494e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19504e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19514e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
19524e3b0468SAntoine Tenart 
19534e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19544e3b0468SAntoine Tenart 
19554e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19564e3b0468SAntoine Tenart 	return 0;
19574e3b0468SAntoine Tenart }
19584e3b0468SAntoine Tenart 
19594e3b0468SAntoine Tenart static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
19604e3b0468SAntoine Tenart {
19614e3b0468SAntoine Tenart 	if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
19624e3b0468SAntoine Tenart 		struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
19634e3b0468SAntoine Tenart 		unsigned long flags;
19644e3b0468SAntoine Tenart 		u32 val;
19654e3b0468SAntoine Tenart 
19664e3b0468SAntoine Tenart 		spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
19674e3b0468SAntoine Tenart 
19684e3b0468SAntoine Tenart 		val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19694e3b0468SAntoine Tenart 		val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19704e3b0468SAntoine Tenart 		val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
19714e3b0468SAntoine Tenart 
19724e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19734e3b0468SAntoine Tenart 
19744e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
19754e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
19764e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
19774e3b0468SAntoine Tenart 
19784e3b0468SAntoine Tenart 		val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19794e3b0468SAntoine Tenart 		val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19804e3b0468SAntoine Tenart 		val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
19814e3b0468SAntoine Tenart 
19824e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19834e3b0468SAntoine Tenart 
19844e3b0468SAntoine Tenart 		spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19854e3b0468SAntoine Tenart 	} else {
19864e3b0468SAntoine Tenart 		/* Fall back using ocelot_ptp_settime64 which is not exact. */
19874e3b0468SAntoine Tenart 		struct timespec64 ts;
19884e3b0468SAntoine Tenart 		u64 now;
19894e3b0468SAntoine Tenart 
19904e3b0468SAntoine Tenart 		ocelot_ptp_gettime64(ptp, &ts);
19914e3b0468SAntoine Tenart 
19924e3b0468SAntoine Tenart 		now = ktime_to_ns(timespec64_to_ktime(ts));
19934e3b0468SAntoine Tenart 		ts = ns_to_timespec64(now + delta);
19944e3b0468SAntoine Tenart 
19954e3b0468SAntoine Tenart 		ocelot_ptp_settime64(ptp, &ts);
19964e3b0468SAntoine Tenart 	}
19974e3b0468SAntoine Tenart 	return 0;
19984e3b0468SAntoine Tenart }
19994e3b0468SAntoine Tenart 
20004e3b0468SAntoine Tenart static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
20014e3b0468SAntoine Tenart {
20024e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
20034e3b0468SAntoine Tenart 	u32 unit = 0, direction = 0;
20044e3b0468SAntoine Tenart 	unsigned long flags;
20054e3b0468SAntoine Tenart 	u64 adj = 0;
20064e3b0468SAntoine Tenart 
20074e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
20084e3b0468SAntoine Tenart 
20094e3b0468SAntoine Tenart 	if (!scaled_ppm)
20104e3b0468SAntoine Tenart 		goto disable_adj;
20114e3b0468SAntoine Tenart 
20124e3b0468SAntoine Tenart 	if (scaled_ppm < 0) {
20134e3b0468SAntoine Tenart 		direction = PTP_CFG_CLK_ADJ_CFG_DIR;
20144e3b0468SAntoine Tenart 		scaled_ppm = -scaled_ppm;
20154e3b0468SAntoine Tenart 	}
20164e3b0468SAntoine Tenart 
20174e3b0468SAntoine Tenart 	adj = PSEC_PER_SEC << 16;
20184e3b0468SAntoine Tenart 	do_div(adj, scaled_ppm);
20194e3b0468SAntoine Tenart 	do_div(adj, 1000);
20204e3b0468SAntoine Tenart 
20214e3b0468SAntoine Tenart 	/* If the adjustment value is too large, use ns instead */
20224e3b0468SAntoine Tenart 	if (adj >= (1L << 30)) {
20234e3b0468SAntoine Tenart 		unit = PTP_CFG_CLK_ADJ_FREQ_NS;
20244e3b0468SAntoine Tenart 		do_div(adj, 1000);
20254e3b0468SAntoine Tenart 	}
20264e3b0468SAntoine Tenart 
20274e3b0468SAntoine Tenart 	/* Still too big */
20284e3b0468SAntoine Tenart 	if (adj >= (1L << 30))
20294e3b0468SAntoine Tenart 		goto disable_adj;
20304e3b0468SAntoine Tenart 
20314e3b0468SAntoine Tenart 	ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
20324e3b0468SAntoine Tenart 	ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
20334e3b0468SAntoine Tenart 		     PTP_CLK_CFG_ADJ_CFG);
20344e3b0468SAntoine Tenart 
20354e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
20364e3b0468SAntoine Tenart 	return 0;
20374e3b0468SAntoine Tenart 
20384e3b0468SAntoine Tenart disable_adj:
20394e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
20404e3b0468SAntoine Tenart 
20414e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
20424e3b0468SAntoine Tenart 	return 0;
20434e3b0468SAntoine Tenart }
20444e3b0468SAntoine Tenart 
20454e3b0468SAntoine Tenart static struct ptp_clock_info ocelot_ptp_clock_info = {
20464e3b0468SAntoine Tenart 	.owner		= THIS_MODULE,
20474e3b0468SAntoine Tenart 	.name		= "ocelot ptp",
20484e3b0468SAntoine Tenart 	.max_adj	= 0x7fffffff,
20494e3b0468SAntoine Tenart 	.n_alarm	= 0,
20504e3b0468SAntoine Tenart 	.n_ext_ts	= 0,
20514e3b0468SAntoine Tenart 	.n_per_out	= 0,
20524e3b0468SAntoine Tenart 	.n_pins		= 0,
20534e3b0468SAntoine Tenart 	.pps		= 0,
20544e3b0468SAntoine Tenart 	.gettime64	= ocelot_ptp_gettime64,
20554e3b0468SAntoine Tenart 	.settime64	= ocelot_ptp_settime64,
20564e3b0468SAntoine Tenart 	.adjtime	= ocelot_ptp_adjtime,
20574e3b0468SAntoine Tenart 	.adjfine	= ocelot_ptp_adjfine,
20584e3b0468SAntoine Tenart };
20594e3b0468SAntoine Tenart 
20604e3b0468SAntoine Tenart static int ocelot_init_timestamp(struct ocelot *ocelot)
20614e3b0468SAntoine Tenart {
20624e3b0468SAntoine Tenart 	ocelot->ptp_info = ocelot_ptp_clock_info;
20634e3b0468SAntoine Tenart 	ocelot->ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
20644e3b0468SAntoine Tenart 	if (IS_ERR(ocelot->ptp_clock))
20654e3b0468SAntoine Tenart 		return PTR_ERR(ocelot->ptp_clock);
20664e3b0468SAntoine Tenart 	/* Check if PHC support is missing at the configuration level */
20674e3b0468SAntoine Tenart 	if (!ocelot->ptp_clock)
20684e3b0468SAntoine Tenart 		return 0;
20694e3b0468SAntoine Tenart 
20704e3b0468SAntoine Tenart 	ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
20714e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
20724e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
20734e3b0468SAntoine Tenart 
20744e3b0468SAntoine Tenart 	ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
20754e3b0468SAntoine Tenart 
20764e3b0468SAntoine Tenart 	/* There is no device reconfiguration, PTP Rx stamping is always
20774e3b0468SAntoine Tenart 	 * enabled.
20784e3b0468SAntoine Tenart 	 */
20794e3b0468SAntoine Tenart 	ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
20804e3b0468SAntoine Tenart 
20814e3b0468SAntoine Tenart 	return 0;
20824e3b0468SAntoine Tenart }
20834e3b0468SAntoine Tenart 
2084a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2085a556c76aSAlexandre Belloni 		      void __iomem *regs,
2086a556c76aSAlexandre Belloni 		      struct phy_device *phy)
2087a556c76aSAlexandre Belloni {
2088a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port;
2089a556c76aSAlexandre Belloni 	struct net_device *dev;
209097bb69e1SVladimir Oltean 	u32 val;
2091a556c76aSAlexandre Belloni 	int err;
2092a556c76aSAlexandre Belloni 
2093a556c76aSAlexandre Belloni 	dev = alloc_etherdev(sizeof(struct ocelot_port));
2094a556c76aSAlexandre Belloni 	if (!dev)
2095a556c76aSAlexandre Belloni 		return -ENOMEM;
2096a556c76aSAlexandre Belloni 	SET_NETDEV_DEV(dev, ocelot->dev);
2097a556c76aSAlexandre Belloni 	ocelot_port = netdev_priv(dev);
2098a556c76aSAlexandre Belloni 	ocelot_port->dev = dev;
2099a556c76aSAlexandre Belloni 	ocelot_port->ocelot = ocelot;
2100a556c76aSAlexandre Belloni 	ocelot_port->regs = regs;
2101a556c76aSAlexandre Belloni 	ocelot_port->chip_port = port;
2102a556c76aSAlexandre Belloni 	ocelot_port->phy = phy;
2103a556c76aSAlexandre Belloni 	ocelot->ports[port] = ocelot_port;
2104a556c76aSAlexandre Belloni 
2105a556c76aSAlexandre Belloni 	dev->netdev_ops = &ocelot_port_netdev_ops;
2106a556c76aSAlexandre Belloni 	dev->ethtool_ops = &ocelot_ethtool_ops;
2107a556c76aSAlexandre Belloni 
21082c1d029aSJoergen Andreasen 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
21092c1d029aSJoergen Andreasen 		NETIF_F_HW_TC;
21102c1d029aSJoergen Andreasen 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
21117142529fSAntoine Tenart 
2112a556c76aSAlexandre Belloni 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2113a556c76aSAlexandre Belloni 	dev->dev_addr[ETH_ALEN - 1] += port;
2114a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2115a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
2116a556c76aSAlexandre Belloni 
21174e3b0468SAntoine Tenart 	INIT_LIST_HEAD(&ocelot_port->skbs);
21184e3b0468SAntoine Tenart 
2119a556c76aSAlexandre Belloni 	err = register_netdev(dev);
2120a556c76aSAlexandre Belloni 	if (err) {
2121a556c76aSAlexandre Belloni 		dev_err(ocelot->dev, "register_netdev failed\n");
2122a556c76aSAlexandre Belloni 		goto err_register_netdev;
2123a556c76aSAlexandre Belloni 	}
2124a556c76aSAlexandre Belloni 
21257142529fSAntoine Tenart 	/* Basic L2 initialization */
212697bb69e1SVladimir Oltean 
212797bb69e1SVladimir Oltean 	/* Drop frames with multicast source address */
212897bb69e1SVladimir Oltean 	val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
212997bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, val, val, ANA_PORT_DROP_CFG, port);
213097bb69e1SVladimir Oltean 
213197bb69e1SVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
213297bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
213397bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
213497bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
21357142529fSAntoine Tenart 
2136b5962294SHoratiu Vultur 	/* Enable vcap lookups */
2137b5962294SHoratiu Vultur 	ocelot_vcap_enable(ocelot, ocelot_port);
2138b5962294SHoratiu Vultur 
2139a556c76aSAlexandre Belloni 	return 0;
2140a556c76aSAlexandre Belloni 
2141a556c76aSAlexandre Belloni err_register_netdev:
2142a556c76aSAlexandre Belloni 	free_netdev(dev);
2143a556c76aSAlexandre Belloni 	return err;
2144a556c76aSAlexandre Belloni }
2145a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port);
2146a556c76aSAlexandre Belloni 
2147a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2148a556c76aSAlexandre Belloni {
2149a556c76aSAlexandre Belloni 	u32 port;
21504e3b0468SAntoine Tenart 	int i, ret, cpu = ocelot->num_phys_ports;
2151a556c76aSAlexandre Belloni 	char queue_name[32];
2152a556c76aSAlexandre Belloni 
2153dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2154dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
2155dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
2156dc96ee37SAlexandre Belloni 		return -ENOMEM;
2157dc96ee37SAlexandre Belloni 
2158a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2159a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2160a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2161a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2162a556c76aSAlexandre Belloni 		return -ENOMEM;
2163a556c76aSAlexandre Belloni 
2164a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
21654e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
21664e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
2167a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2168a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2169a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2170a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2171a556c76aSAlexandre Belloni 		return -ENOMEM;
2172a556c76aSAlexandre Belloni 
2173a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2174a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2175b5962294SHoratiu Vultur 	ocelot_ace_init(ocelot);
2176a556c76aSAlexandre Belloni 
2177a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2178a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2179a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2180a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2181a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2182a556c76aSAlexandre Belloni 	}
2183a556c76aSAlexandre Belloni 
2184a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2185a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2186a556c76aSAlexandre Belloni 
2187a556c76aSAlexandre Belloni 	/* Aggregation mode */
2188a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2189a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2190a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2191a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2192a556c76aSAlexandre Belloni 
2193a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2194a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2195a556c76aSAlexandre Belloni 	 */
2196a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2197a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2198a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2199a556c76aSAlexandre Belloni 
2200a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2201a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2202a556c76aSAlexandre Belloni 
2203a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2204a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2205a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2206a556c76aSAlexandre Belloni 
2207a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2208a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2209a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2210a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2211a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
2212a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2213a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2214a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2215a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2216a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2217a556c76aSAlexandre Belloni 
2218a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2219a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2220a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2221a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2222a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2223a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2224a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2225a556c76aSAlexandre Belloni 				 port);
2226a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2227a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2228a556c76aSAlexandre Belloni 	}
2229a556c76aSAlexandre Belloni 
2230a556c76aSAlexandre Belloni 	/* Configure and enable the CPU port. */
2231a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2232a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2233a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2234a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2235a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, cpu);
2236a556c76aSAlexandre Belloni 
2237a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
2238a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2239a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2240a556c76aSAlexandre Belloni 
2241a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2242a556c76aSAlexandre Belloni 	}
2243a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
2244a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2245a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
2246a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2247a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2248a556c76aSAlexandre Belloni 
2249a556c76aSAlexandre Belloni 	/* CPU port Injection/Extraction configuration */
2250a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2251a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2252a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2253a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, cpu);
2254a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
2255a556c76aSAlexandre Belloni 			 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
2256a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2257a556c76aSAlexandre Belloni 	 * registers endianness.
2258a556c76aSAlexandre Belloni 	 */
2259a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2260a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2261a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2262a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2263a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2264a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2265a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2266a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2267a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2268a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2269a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2270a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2271a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2272a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2273a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2274a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2275a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2276a556c76aSAlexandre Belloni 
22771e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2278a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2279a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
22804e3b0468SAntoine Tenart 
22814e3b0468SAntoine Tenart 	if (ocelot->ptp) {
22824e3b0468SAntoine Tenart 		ret = ocelot_init_timestamp(ocelot);
22834e3b0468SAntoine Tenart 		if (ret) {
22844e3b0468SAntoine Tenart 			dev_err(ocelot->dev,
22854e3b0468SAntoine Tenart 				"Timestamp initialization failed\n");
22864e3b0468SAntoine Tenart 			return ret;
22874e3b0468SAntoine Tenart 		}
22884e3b0468SAntoine Tenart 	}
22894e3b0468SAntoine Tenart 
2290a556c76aSAlexandre Belloni 	return 0;
2291a556c76aSAlexandre Belloni }
2292a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2293a556c76aSAlexandre Belloni 
2294a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2295a556c76aSAlexandre Belloni {
22964e3b0468SAntoine Tenart 	struct list_head *pos, *tmp;
22974e3b0468SAntoine Tenart 	struct ocelot_port *port;
22984e3b0468SAntoine Tenart 	struct ocelot_skb *entry;
22994e3b0468SAntoine Tenart 	int i;
23004e3b0468SAntoine Tenart 
2301c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2302a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2303a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2304b5962294SHoratiu Vultur 	ocelot_ace_deinit();
23054e3b0468SAntoine Tenart 
23064e3b0468SAntoine Tenart 	for (i = 0; i < ocelot->num_phys_ports; i++) {
23074e3b0468SAntoine Tenart 		port = ocelot->ports[i];
23084e3b0468SAntoine Tenart 
23094e3b0468SAntoine Tenart 		list_for_each_safe(pos, tmp, &port->skbs) {
23104e3b0468SAntoine Tenart 			entry = list_entry(pos, struct ocelot_skb, head);
23114e3b0468SAntoine Tenart 
23124e3b0468SAntoine Tenart 			list_del(pos);
23134e3b0468SAntoine Tenart 			dev_kfree_skb_any(entry->skb);
23144e3b0468SAntoine Tenart 			kfree(entry);
23154e3b0468SAntoine Tenart 		}
23164e3b0468SAntoine Tenart 	}
2317a556c76aSAlexandre Belloni }
2318a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2319a556c76aSAlexandre Belloni 
2320a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2321