xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision f24711fddc36aa7286af724393ef7334b92c5702)
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 
136f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int 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),
140f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, 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 
173f270dbfaSVladimir Oltean static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
1747142529fSAntoine Tenart 			     netdev_features_t features)
1757142529fSAntoine Tenart {
1767142529fSAntoine Tenart 	u32 val;
1777142529fSAntoine Tenart 
1787142529fSAntoine Tenart 	/* Filtering */
1797142529fSAntoine Tenart 	val = ocelot_read(ocelot, ANA_VLANMASK);
1807142529fSAntoine Tenart 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
181f270dbfaSVladimir Oltean 		val |= BIT(port);
1827142529fSAntoine Tenart 	else
183f270dbfaSVladimir Oltean 		val &= ~BIT(port);
1847142529fSAntoine Tenart 	ocelot_write(ocelot, val, ANA_VLANMASK);
1857142529fSAntoine Tenart }
1867142529fSAntoine Tenart 
18797bb69e1SVladimir Oltean static void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
18897bb69e1SVladimir Oltean 				       bool vlan_aware)
1897142529fSAntoine Tenart {
19097bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1917142529fSAntoine Tenart 	u32 val;
1927142529fSAntoine Tenart 
19397bb69e1SVladimir Oltean 	if (vlan_aware)
19497bb69e1SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1957142529fSAntoine Tenart 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
19697bb69e1SVladimir Oltean 	else
19797bb69e1SVladimir Oltean 		val = 0;
1987142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
1997142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2007142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
20197bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
2027142529fSAntoine Tenart 
20397bb69e1SVladimir Oltean 	if (vlan_aware && !ocelot_port->vid)
2047142529fSAntoine Tenart 		/* If port is vlan-aware and tagged, drop untagged and priority
2057142529fSAntoine Tenart 		 * tagged frames.
2067142529fSAntoine Tenart 		 */
20797bb69e1SVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
2087142529fSAntoine Tenart 		      ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
2097142529fSAntoine Tenart 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
21097bb69e1SVladimir Oltean 	else
21197bb69e1SVladimir Oltean 		val = 0;
21297bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
21397bb69e1SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
21497bb69e1SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
21597bb69e1SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
21697bb69e1SVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
2177142529fSAntoine Tenart 
21897bb69e1SVladimir Oltean 	if (vlan_aware) {
21997bb69e1SVladimir Oltean 		if (ocelot_port->vid)
2207142529fSAntoine Tenart 			/* Tag all frames except when VID == DEFAULT_VLAN */
2217142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(1);
2227142529fSAntoine Tenart 		else
2237142529fSAntoine Tenart 			/* Tag all frames */
2247142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(3);
22597bb69e1SVladimir Oltean 	} else {
22697bb69e1SVladimir Oltean 		/* Port tagging disabled. */
22797bb69e1SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
2287142529fSAntoine Tenart 	}
2297142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2307142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_CFG_M,
23197bb69e1SVladimir Oltean 		       REW_TAG_CFG, port);
23297bb69e1SVladimir Oltean }
23397bb69e1SVladimir Oltean 
23497bb69e1SVladimir Oltean static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
23597bb69e1SVladimir Oltean 				       u16 vid)
23697bb69e1SVladimir Oltean {
23797bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
23897bb69e1SVladimir Oltean 
23997bb69e1SVladimir Oltean 	if (ocelot_port->vid != vid) {
24097bb69e1SVladimir Oltean 		/* Always permit deleting the native VLAN (vid = 0) */
24197bb69e1SVladimir Oltean 		if (ocelot_port->vid && vid) {
24297bb69e1SVladimir Oltean 			dev_err(ocelot->dev,
24397bb69e1SVladimir Oltean 				"Port already has a native VLAN: %d\n",
24497bb69e1SVladimir Oltean 				ocelot_port->vid);
24597bb69e1SVladimir Oltean 			return -EBUSY;
24697bb69e1SVladimir Oltean 		}
24797bb69e1SVladimir Oltean 		ocelot_port->vid = vid;
24897bb69e1SVladimir Oltean 	}
24997bb69e1SVladimir Oltean 
25097bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
2517142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
25297bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
25397bb69e1SVladimir Oltean 
25497bb69e1SVladimir Oltean 	return 0;
25597bb69e1SVladimir Oltean }
25697bb69e1SVladimir Oltean 
25797bb69e1SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
25897bb69e1SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
25997bb69e1SVladimir Oltean {
26097bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
26197bb69e1SVladimir Oltean 
26297bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot,
26397bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
26497bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
26597bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
26697bb69e1SVladimir Oltean 
26797bb69e1SVladimir Oltean 	ocelot_port->pvid = pvid;
2687142529fSAntoine Tenart }
2697142529fSAntoine Tenart 
2709855934cSVladimir Oltean static int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2717142529fSAntoine Tenart 			   bool untagged)
2727142529fSAntoine Tenart {
2737142529fSAntoine Tenart 	int ret;
2747142529fSAntoine Tenart 
2757142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
27697bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2777142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2787142529fSAntoine Tenart 	if (ret)
2797142529fSAntoine Tenart 		return ret;
2807142529fSAntoine Tenart 
2817142529fSAntoine Tenart 	/* Default ingress vlan classification */
2827142529fSAntoine Tenart 	if (pvid)
28397bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, vid);
2847142529fSAntoine Tenart 
2857142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
28697bb69e1SVladimir Oltean 	if (untagged) {
28797bb69e1SVladimir Oltean 		ret = ocelot_port_set_native_vlan(ocelot, port, vid);
28897bb69e1SVladimir Oltean 		if (ret)
28997bb69e1SVladimir Oltean 			return ret;
290b9cd75e6SVladimir Oltean 	}
2917142529fSAntoine Tenart 
2927142529fSAntoine Tenart 	return 0;
2937142529fSAntoine Tenart }
2947142529fSAntoine Tenart 
2959855934cSVladimir Oltean static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
2969855934cSVladimir Oltean 			       bool untagged)
2977142529fSAntoine Tenart {
298004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
299004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
30097bb69e1SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
301004d44f6SVladimir Oltean 	int port = priv->chip_port;
3027142529fSAntoine Tenart 	int ret;
3037142529fSAntoine Tenart 
3049855934cSVladimir Oltean 	ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
3059855934cSVladimir Oltean 	if (ret)
3069855934cSVladimir Oltean 		return ret;
3077142529fSAntoine Tenart 
3089855934cSVladimir Oltean 	/* Add the port MAC address to with the right VLAN information */
3099855934cSVladimir Oltean 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
3109855934cSVladimir Oltean 			  ENTRYTYPE_LOCKED);
3119855934cSVladimir Oltean 
3129855934cSVladimir Oltean 	return 0;
3139855934cSVladimir Oltean }
3149855934cSVladimir Oltean 
3159855934cSVladimir Oltean static int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3169855934cSVladimir Oltean {
3179855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3189855934cSVladimir Oltean 	int ret;
3197142529fSAntoine Tenart 
3207142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
32197bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3227142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3237142529fSAntoine Tenart 	if (ret)
3247142529fSAntoine Tenart 		return ret;
3257142529fSAntoine Tenart 
3267142529fSAntoine Tenart 	/* Ingress */
32797bb69e1SVladimir Oltean 	if (ocelot_port->pvid == vid)
32897bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, 0);
3297142529fSAntoine Tenart 
3307142529fSAntoine Tenart 	/* Egress */
33197bb69e1SVladimir Oltean 	if (ocelot_port->vid == vid)
33297bb69e1SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, 0);
3337142529fSAntoine Tenart 
3347142529fSAntoine Tenart 	return 0;
3357142529fSAntoine Tenart }
3367142529fSAntoine Tenart 
3379855934cSVladimir Oltean static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
3389855934cSVladimir Oltean {
339004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
340004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
341004d44f6SVladimir Oltean 	int port = priv->chip_port;
3429855934cSVladimir Oltean 	int ret;
3439855934cSVladimir Oltean 
3449855934cSVladimir Oltean 	/* 8021q removes VID 0 on module unload for all interfaces
3459855934cSVladimir Oltean 	 * with VLAN filtering feature. We need to keep it to receive
3469855934cSVladimir Oltean 	 * untagged traffic.
3479855934cSVladimir Oltean 	 */
3489855934cSVladimir Oltean 	if (vid == 0)
3499855934cSVladimir Oltean 		return 0;
3509855934cSVladimir Oltean 
3519855934cSVladimir Oltean 	ret = ocelot_vlan_del(ocelot, port, vid);
3529855934cSVladimir Oltean 	if (ret)
3539855934cSVladimir Oltean 		return ret;
3549855934cSVladimir Oltean 
3559855934cSVladimir Oltean 	/* Del the port MAC address to with the right VLAN information */
3569855934cSVladimir Oltean 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
3579855934cSVladimir Oltean 
3589855934cSVladimir Oltean 	return 0;
3599855934cSVladimir Oltean }
3609855934cSVladimir Oltean 
361a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
362a556c76aSAlexandre Belloni {
3637142529fSAntoine Tenart 	u16 port, vid;
3647142529fSAntoine Tenart 
365a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
366a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
367a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
368a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3697142529fSAntoine Tenart 
3707142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3717142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3727142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3737142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3747142529fSAntoine Tenart 	}
3757142529fSAntoine Tenart 
3767142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3777142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3787142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3797142529fSAntoine Tenart 	 */
3807142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3817142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3827142529fSAntoine Tenart 
3837142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3847142529fSAntoine Tenart 	 * default.
3857142529fSAntoine Tenart 	 */
386714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
387714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3887142529fSAntoine Tenart 
3897142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3907142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3917142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3927142529fSAntoine Tenart 	}
393a556c76aSAlexandre Belloni }
394a556c76aSAlexandre Belloni 
395a556c76aSAlexandre Belloni /* Watermark encode
396a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
397a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
398a556c76aSAlexandre Belloni  */
399a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
400a556c76aSAlexandre Belloni {
401a556c76aSAlexandre Belloni 	if (value >= BIT(8))
402a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
403a556c76aSAlexandre Belloni 
404a556c76aSAlexandre Belloni 	return value;
405a556c76aSAlexandre Belloni }
406a556c76aSAlexandre Belloni 
40726f4dbabSVladimir Oltean static void ocelot_adjust_link(struct ocelot *ocelot, int port,
40826f4dbabSVladimir Oltean 			       struct phy_device *phydev)
409a556c76aSAlexandre Belloni {
41026f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
4115bc9d2e6SVladimir Oltean 	int speed, mode = 0;
412a556c76aSAlexandre Belloni 
41326f4dbabSVladimir Oltean 	switch (phydev->speed) {
414a556c76aSAlexandre Belloni 	case SPEED_10:
415a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
416a556c76aSAlexandre Belloni 		break;
417a556c76aSAlexandre Belloni 	case SPEED_100:
418a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
419a556c76aSAlexandre Belloni 		break;
420a556c76aSAlexandre Belloni 	case SPEED_1000:
421a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
422a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
423a556c76aSAlexandre Belloni 		break;
424a556c76aSAlexandre Belloni 	case SPEED_2500:
425a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
426a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
427a556c76aSAlexandre Belloni 		break;
428a556c76aSAlexandre Belloni 	default:
42926f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
43026f4dbabSVladimir Oltean 			port, phydev->speed);
431a556c76aSAlexandre Belloni 		return;
432a556c76aSAlexandre Belloni 	}
433a556c76aSAlexandre Belloni 
43426f4dbabSVladimir Oltean 	phy_print_status(phydev);
435a556c76aSAlexandre Belloni 
43626f4dbabSVladimir Oltean 	if (!phydev->link)
437a556c76aSAlexandre Belloni 		return;
438a556c76aSAlexandre Belloni 
439a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
440004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
441a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
442a556c76aSAlexandre Belloni 
443dc3de2a2SClaudiu Manoil 	if (ocelot->ops->pcs_init)
444dc3de2a2SClaudiu Manoil 		ocelot->ops->pcs_init(ocelot, port);
445a556c76aSAlexandre Belloni 
446a556c76aSAlexandre Belloni 	/* Enable MAC module */
447004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
448a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
449a556c76aSAlexandre Belloni 
450a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
451a556c76aSAlexandre Belloni 	 * reset */
452004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
453a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
454a556c76aSAlexandre Belloni 
455a556c76aSAlexandre Belloni 	/* No PFC */
456a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
457004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
458a556c76aSAlexandre Belloni 
459a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
460a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
461a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
462a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
463004d44f6SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, port);
464a556c76aSAlexandre Belloni 
465a556c76aSAlexandre Belloni 	/* Flow control */
466a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
467a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
468a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
469a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
470a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
471004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
472004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
473a556c76aSAlexandre Belloni }
474a556c76aSAlexandre Belloni 
47526f4dbabSVladimir Oltean static void ocelot_port_adjust_link(struct net_device *dev)
47626f4dbabSVladimir Oltean {
47726f4dbabSVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
47826f4dbabSVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
47926f4dbabSVladimir Oltean 	int port = priv->chip_port;
48026f4dbabSVladimir Oltean 
48126f4dbabSVladimir Oltean 	ocelot_adjust_link(ocelot, port, dev->phydev);
48226f4dbabSVladimir Oltean }
48326f4dbabSVladimir Oltean 
484889b8950SVladimir Oltean static void ocelot_port_enable(struct ocelot *ocelot, int port,
485889b8950SVladimir Oltean 			       struct phy_device *phy)
486a556c76aSAlexandre Belloni {
487a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
488a556c76aSAlexandre Belloni 	 * MAC addresses.
489a556c76aSAlexandre Belloni 	 */
490a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
491a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
492004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
493004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
494889b8950SVladimir Oltean }
495889b8950SVladimir Oltean 
496889b8950SVladimir Oltean static int ocelot_port_open(struct net_device *dev)
497889b8950SVladimir Oltean {
498889b8950SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
499889b8950SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
500889b8950SVladimir Oltean 	int port = priv->chip_port;
501889b8950SVladimir Oltean 	int err;
502a556c76aSAlexandre Belloni 
503004d44f6SVladimir Oltean 	if (priv->serdes) {
504004d44f6SVladimir Oltean 		err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
505004d44f6SVladimir Oltean 				       priv->phy_mode);
50671e32a20SQuentin Schulz 		if (err) {
50771e32a20SQuentin Schulz 			netdev_err(dev, "Could not set mode of SerDes\n");
50871e32a20SQuentin Schulz 			return err;
50971e32a20SQuentin Schulz 		}
51071e32a20SQuentin Schulz 	}
51171e32a20SQuentin Schulz 
512004d44f6SVladimir Oltean 	err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
513004d44f6SVladimir Oltean 				 priv->phy_mode);
514a556c76aSAlexandre Belloni 	if (err) {
515a556c76aSAlexandre Belloni 		netdev_err(dev, "Could not attach to PHY\n");
516a556c76aSAlexandre Belloni 		return err;
517a556c76aSAlexandre Belloni 	}
518a556c76aSAlexandre Belloni 
519004d44f6SVladimir Oltean 	dev->phydev = priv->phy;
520a556c76aSAlexandre Belloni 
521004d44f6SVladimir Oltean 	phy_attached_info(priv->phy);
522004d44f6SVladimir Oltean 	phy_start(priv->phy);
523889b8950SVladimir Oltean 
524889b8950SVladimir Oltean 	ocelot_port_enable(ocelot, port, priv->phy);
525889b8950SVladimir Oltean 
526a556c76aSAlexandre Belloni 	return 0;
527a556c76aSAlexandre Belloni }
528a556c76aSAlexandre Belloni 
529889b8950SVladimir Oltean static void ocelot_port_disable(struct ocelot *ocelot, int port)
530889b8950SVladimir Oltean {
531889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
532889b8950SVladimir Oltean 
533889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
534889b8950SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
535889b8950SVladimir Oltean 		       QSYS_SWITCH_PORT_MODE, port);
536889b8950SVladimir Oltean }
537889b8950SVladimir Oltean 
538a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev)
539a556c76aSAlexandre Belloni {
540004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
541889b8950SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
542889b8950SVladimir Oltean 	int port = priv->chip_port;
543a556c76aSAlexandre Belloni 
544004d44f6SVladimir Oltean 	phy_disconnect(priv->phy);
545a556c76aSAlexandre Belloni 
546a556c76aSAlexandre Belloni 	dev->phydev = NULL;
547a556c76aSAlexandre Belloni 
548889b8950SVladimir Oltean 	ocelot_port_disable(ocelot, port);
549889b8950SVladimir Oltean 
550a556c76aSAlexandre Belloni 	return 0;
551a556c76aSAlexandre Belloni }
552a556c76aSAlexandre Belloni 
553a556c76aSAlexandre Belloni /* Generate the IFH for frame injection
554a556c76aSAlexandre Belloni  *
555a556c76aSAlexandre Belloni  * The IFH is a 128bit-value
556a556c76aSAlexandre Belloni  * bit 127: bypass the analyzer processing
557a556c76aSAlexandre Belloni  * bit 56-67: destination mask
558a556c76aSAlexandre Belloni  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
559a556c76aSAlexandre Belloni  * bit 20-27: cpu extraction queue mask
560a556c76aSAlexandre Belloni  * bit 16: tag type 0: C-tag, 1: S-tag
561a556c76aSAlexandre Belloni  * bit 0-11: VID
562a556c76aSAlexandre Belloni  */
563a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
564a556c76aSAlexandre Belloni {
5654e3b0468SAntoine Tenart 	ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
56608d02364SAntoine Tenart 	ifh[1] = (0xf00 & info->port) >> 8;
567a556c76aSAlexandre Belloni 	ifh[2] = (0xff & info->port) << 24;
56808d02364SAntoine Tenart 	ifh[3] = (info->tag_type << 16) | info->vid;
569a556c76aSAlexandre Belloni 
570a556c76aSAlexandre Belloni 	return 0;
571a556c76aSAlexandre Belloni }
572a556c76aSAlexandre Belloni 
573a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
574a556c76aSAlexandre Belloni {
575004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
5764e3b0468SAntoine Tenart 	struct skb_shared_info *shinfo = skb_shinfo(skb);
577004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
578004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
579*f24711fdSVladimir Oltean 	u32 val, ifh[OCELOT_TAG_LEN / 4];
580a556c76aSAlexandre Belloni 	struct frame_info info = {};
581a556c76aSAlexandre Belloni 	u8 grp = 0; /* Send everything on CPU group 0 */
582a556c76aSAlexandre Belloni 	unsigned int i, count, last;
583004d44f6SVladimir Oltean 	int port = priv->chip_port;
584a556c76aSAlexandre Belloni 
585a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, QS_INJ_STATUS);
586a556c76aSAlexandre Belloni 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
587a556c76aSAlexandre Belloni 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
588a556c76aSAlexandre Belloni 		return NETDEV_TX_BUSY;
589a556c76aSAlexandre Belloni 
590a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
591a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
592a556c76aSAlexandre Belloni 
593004d44f6SVladimir Oltean 	info.port = BIT(port);
59408d02364SAntoine Tenart 	info.tag_type = IFH_TAG_TYPE_C;
59508d02364SAntoine Tenart 	info.vid = skb_vlan_tag_get(skb);
5964e3b0468SAntoine Tenart 
5974e3b0468SAntoine Tenart 	/* Check if timestamping is needed */
5984e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
599004d44f6SVladimir Oltean 		info.rew_op = ocelot_port->ptp_cmd;
600004d44f6SVladimir Oltean 		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
601004d44f6SVladimir Oltean 			info.rew_op |= (ocelot_port->ts_id  % 4) << 3;
6024e3b0468SAntoine Tenart 	}
6034e3b0468SAntoine Tenart 
604a556c76aSAlexandre Belloni 	ocelot_gen_ifh(ifh, &info);
605a556c76aSAlexandre Belloni 
606*f24711fdSVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
607c2cd650bSAntoine Tenart 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
608c2cd650bSAntoine Tenart 				 QS_INJ_WR, grp);
609a556c76aSAlexandre Belloni 
610a556c76aSAlexandre Belloni 	count = (skb->len + 3) / 4;
611a556c76aSAlexandre Belloni 	last = skb->len % 4;
612a556c76aSAlexandre Belloni 	for (i = 0; i < count; i++) {
613a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
614a556c76aSAlexandre Belloni 	}
615a556c76aSAlexandre Belloni 
616a556c76aSAlexandre Belloni 	/* Add padding */
617a556c76aSAlexandre Belloni 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
618a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
619a556c76aSAlexandre Belloni 		i++;
620a556c76aSAlexandre Belloni 	}
621a556c76aSAlexandre Belloni 
622a556c76aSAlexandre Belloni 	/* Indicate EOF and valid bytes in last word */
623a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
624a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
625a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_EOF,
626a556c76aSAlexandre Belloni 			 QS_INJ_CTRL, grp);
627a556c76aSAlexandre Belloni 
628a556c76aSAlexandre Belloni 	/* Add dummy CRC */
629a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
630a556c76aSAlexandre Belloni 	skb_tx_timestamp(skb);
631a556c76aSAlexandre Belloni 
632a556c76aSAlexandre Belloni 	dev->stats.tx_packets++;
633a556c76aSAlexandre Belloni 	dev->stats.tx_bytes += skb->len;
6344e3b0468SAntoine Tenart 
6354e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
636004d44f6SVladimir Oltean 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
6374e3b0468SAntoine Tenart 		struct ocelot_skb *oskb =
6384e3b0468SAntoine Tenart 			kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
6394e3b0468SAntoine Tenart 
6404e3b0468SAntoine Tenart 		if (unlikely(!oskb))
6414e3b0468SAntoine Tenart 			goto out;
6424e3b0468SAntoine Tenart 
6434e3b0468SAntoine Tenart 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
6444e3b0468SAntoine Tenart 
6454e3b0468SAntoine Tenart 		oskb->skb = skb;
646004d44f6SVladimir Oltean 		oskb->id = ocelot_port->ts_id % 4;
647004d44f6SVladimir Oltean 		ocelot_port->ts_id++;
6484e3b0468SAntoine Tenart 
649004d44f6SVladimir Oltean 		list_add_tail(&oskb->head, &ocelot_port->skbs);
650a556c76aSAlexandre Belloni 
651a556c76aSAlexandre Belloni 		return NETDEV_TX_OK;
652a556c76aSAlexandre Belloni 	}
653a556c76aSAlexandre Belloni 
6544e3b0468SAntoine Tenart out:
6554e3b0468SAntoine Tenart 	dev_kfree_skb_any(skb);
6564e3b0468SAntoine Tenart 	return NETDEV_TX_OK;
6574e3b0468SAntoine Tenart }
6584e3b0468SAntoine Tenart 
6594e3b0468SAntoine Tenart void ocelot_get_hwtimestamp(struct ocelot *ocelot, struct timespec64 *ts)
6604e3b0468SAntoine Tenart {
6614e3b0468SAntoine Tenart 	unsigned long flags;
6624e3b0468SAntoine Tenart 	u32 val;
6634e3b0468SAntoine Tenart 
6644e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
6654e3b0468SAntoine Tenart 
6664e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
6674e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
6684e3b0468SAntoine Tenart 
6694e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
6704e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
6714e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
6724e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
6734e3b0468SAntoine Tenart 
6744e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
6754e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
6764e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
6774e3b0468SAntoine Tenart 
6784e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
6794e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
6804e3b0468SAntoine Tenart 		ts->tv_sec--;
6814e3b0468SAntoine Tenart 
6824e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
6834e3b0468SAntoine Tenart }
6844e3b0468SAntoine Tenart EXPORT_SYMBOL(ocelot_get_hwtimestamp);
6854e3b0468SAntoine Tenart 
68640a1578dSClaudiu Manoil static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
687a556c76aSAlexandre Belloni {
688004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
689004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
690004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
691a556c76aSAlexandre Belloni 
692004d44f6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, ocelot_port->pvid);
693a556c76aSAlexandre Belloni }
694a556c76aSAlexandre Belloni 
69540a1578dSClaudiu Manoil static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
696a556c76aSAlexandre Belloni {
697004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
698004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
699004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
700a556c76aSAlexandre Belloni 
701004d44f6SVladimir Oltean 	return ocelot_mact_learn(ocelot, PGID_CPU, addr, ocelot_port->pvid,
702a556c76aSAlexandre Belloni 				 ENTRYTYPE_LOCKED);
703a556c76aSAlexandre Belloni }
704a556c76aSAlexandre Belloni 
705a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev)
706a556c76aSAlexandre Belloni {
707004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
708004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
709a556c76aSAlexandre Belloni 	u32 val;
710004d44f6SVladimir Oltean 	int i;
711a556c76aSAlexandre Belloni 
712a556c76aSAlexandre Belloni 	/* This doesn't handle promiscuous mode because the bridge core is
713a556c76aSAlexandre Belloni 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
714a556c76aSAlexandre Belloni 	 * forwarded to the CPU port.
715a556c76aSAlexandre Belloni 	 */
716a556c76aSAlexandre Belloni 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
717a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
718a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
719a556c76aSAlexandre Belloni 
72040a1578dSClaudiu Manoil 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
721a556c76aSAlexandre Belloni }
722a556c76aSAlexandre Belloni 
723a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev,
724a556c76aSAlexandre Belloni 					  char *buf, size_t len)
725a556c76aSAlexandre Belloni {
726004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
727004d44f6SVladimir Oltean 	int port = priv->chip_port;
728a556c76aSAlexandre Belloni 	int ret;
729a556c76aSAlexandre Belloni 
730004d44f6SVladimir Oltean 	ret = snprintf(buf, len, "p%d", port);
731a556c76aSAlexandre Belloni 	if (ret >= len)
732a556c76aSAlexandre Belloni 		return -EINVAL;
733a556c76aSAlexandre Belloni 
734a556c76aSAlexandre Belloni 	return 0;
735a556c76aSAlexandre Belloni }
736a556c76aSAlexandre Belloni 
737a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
738a556c76aSAlexandre Belloni {
739004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
740004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
741004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
742a556c76aSAlexandre Belloni 	const struct sockaddr *addr = p;
743a556c76aSAlexandre Belloni 
744a556c76aSAlexandre Belloni 	/* Learn the new net device MAC address in the mac table. */
745004d44f6SVladimir Oltean 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, ocelot_port->pvid,
746a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
747a556c76aSAlexandre Belloni 	/* Then forget the previous one. */
748004d44f6SVladimir Oltean 	ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid);
749a556c76aSAlexandre Belloni 
750a556c76aSAlexandre Belloni 	ether_addr_copy(dev->dev_addr, addr->sa_data);
751a556c76aSAlexandre Belloni 	return 0;
752a556c76aSAlexandre Belloni }
753a556c76aSAlexandre Belloni 
754a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev,
755a556c76aSAlexandre Belloni 			       struct rtnl_link_stats64 *stats)
756a556c76aSAlexandre Belloni {
757004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
758004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
759004d44f6SVladimir Oltean 	int port = priv->chip_port;
760a556c76aSAlexandre Belloni 
761a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
762004d44f6SVladimir Oltean 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
763a556c76aSAlexandre Belloni 		     SYS_STAT_CFG);
764a556c76aSAlexandre Belloni 
765a556c76aSAlexandre Belloni 	/* Get Rx stats */
766a556c76aSAlexandre Belloni 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
767a556c76aSAlexandre Belloni 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
768a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
769a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
770a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
771a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
772a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
773a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
774a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
775a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
776a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
777a556c76aSAlexandre Belloni 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
778a556c76aSAlexandre Belloni 	stats->rx_dropped = dev->stats.rx_dropped;
779a556c76aSAlexandre Belloni 
780a556c76aSAlexandre Belloni 	/* Get Tx stats */
781a556c76aSAlexandre Belloni 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
782a556c76aSAlexandre Belloni 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
783a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
784a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
785a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
786a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
787a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
788a556c76aSAlexandre Belloni 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
789a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
790a556c76aSAlexandre Belloni 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
791a556c76aSAlexandre Belloni }
792a556c76aSAlexandre Belloni 
793531ee1a6SVladimir Oltean static int ocelot_fdb_add(struct ocelot *ocelot, int port,
794004d44f6SVladimir Oltean 			  const unsigned char *addr, u16 vid,
795004d44f6SVladimir Oltean 			  bool vlan_aware)
796a556c76aSAlexandre Belloni {
797531ee1a6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
798a556c76aSAlexandre Belloni 
7997142529fSAntoine Tenart 	if (!vid) {
800004d44f6SVladimir Oltean 		if (!vlan_aware)
8017142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
8027142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
8037142529fSAntoine Tenart 			 * matches incoming untagged packets
8047142529fSAntoine Tenart 			 */
805531ee1a6SVladimir Oltean 			vid = ocelot_port->pvid;
8067142529fSAntoine Tenart 		else
8077142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
8087142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
8097142529fSAntoine Tenart 			 */
8107142529fSAntoine Tenart 			return -EINVAL;
8117142529fSAntoine Tenart 	}
8127142529fSAntoine Tenart 
813531ee1a6SVladimir Oltean 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
814a556c76aSAlexandre Belloni }
815a556c76aSAlexandre Belloni 
816531ee1a6SVladimir Oltean static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
817531ee1a6SVladimir Oltean 			       struct net_device *dev,
818531ee1a6SVladimir Oltean 			       const unsigned char *addr,
819531ee1a6SVladimir Oltean 			       u16 vid, u16 flags,
820531ee1a6SVladimir Oltean 			       struct netlink_ext_ack *extack)
821531ee1a6SVladimir Oltean {
822004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
823004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
824004d44f6SVladimir Oltean 	int port = priv->chip_port;
825531ee1a6SVladimir Oltean 
826004d44f6SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, priv->vlan_aware);
827531ee1a6SVladimir Oltean }
828531ee1a6SVladimir Oltean 
829531ee1a6SVladimir Oltean static int ocelot_fdb_del(struct ocelot *ocelot, int port,
830531ee1a6SVladimir Oltean 			  const unsigned char *addr, u16 vid)
831531ee1a6SVladimir Oltean {
832531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
833531ee1a6SVladimir Oltean }
834531ee1a6SVladimir Oltean 
835531ee1a6SVladimir Oltean static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
836a556c76aSAlexandre Belloni 			       struct net_device *dev,
837a556c76aSAlexandre Belloni 			       const unsigned char *addr, u16 vid)
838a556c76aSAlexandre Belloni {
839004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
840004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
841004d44f6SVladimir Oltean 	int port = priv->chip_port;
842a556c76aSAlexandre Belloni 
843004d44f6SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
844a556c76aSAlexandre Belloni }
845a556c76aSAlexandre Belloni 
846a556c76aSAlexandre Belloni struct ocelot_dump_ctx {
847a556c76aSAlexandre Belloni 	struct net_device *dev;
848a556c76aSAlexandre Belloni 	struct sk_buff *skb;
849a556c76aSAlexandre Belloni 	struct netlink_callback *cb;
850a556c76aSAlexandre Belloni 	int idx;
851a556c76aSAlexandre Belloni };
852a556c76aSAlexandre Belloni 
853531ee1a6SVladimir Oltean static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
854531ee1a6SVladimir Oltean 				   bool is_static, void *data)
855a556c76aSAlexandre Belloni {
856531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
857a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
858a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
859a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
860a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
861a556c76aSAlexandre Belloni 
862a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
863a556c76aSAlexandre Belloni 		goto skip;
864a556c76aSAlexandre Belloni 
865a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
866a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
867a556c76aSAlexandre Belloni 	if (!nlh)
868a556c76aSAlexandre Belloni 		return -EMSGSIZE;
869a556c76aSAlexandre Belloni 
870a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
871a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
872a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
873a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
874a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
875a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
876a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
877531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
878a556c76aSAlexandre Belloni 
879531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
880a556c76aSAlexandre Belloni 		goto nla_put_failure;
881a556c76aSAlexandre Belloni 
882531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
883a556c76aSAlexandre Belloni 		goto nla_put_failure;
884a556c76aSAlexandre Belloni 
885a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
886a556c76aSAlexandre Belloni 
887a556c76aSAlexandre Belloni skip:
888a556c76aSAlexandre Belloni 	dump->idx++;
889a556c76aSAlexandre Belloni 	return 0;
890a556c76aSAlexandre Belloni 
891a556c76aSAlexandre Belloni nla_put_failure:
892a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
893a556c76aSAlexandre Belloni 	return -EMSGSIZE;
894a556c76aSAlexandre Belloni }
895a556c76aSAlexandre Belloni 
896531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
897a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
898a556c76aSAlexandre Belloni {
899a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
900531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
901a556c76aSAlexandre Belloni 
902a556c76aSAlexandre Belloni 	/* Set row and column to read from */
903a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
904a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
905a556c76aSAlexandre Belloni 
906a556c76aSAlexandre Belloni 	/* Issue a read command */
907a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
908a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
909a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
910a556c76aSAlexandre Belloni 
911a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
912a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
913a556c76aSAlexandre Belloni 
914a556c76aSAlexandre Belloni 	/* Read the entry flags */
915a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
916a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
917a556c76aSAlexandre Belloni 		return -EINVAL;
918a556c76aSAlexandre Belloni 
919a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
920a556c76aSAlexandre Belloni 	 * do not report it.
921a556c76aSAlexandre Belloni 	 */
922a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
923531ee1a6SVladimir Oltean 	if (dst != port)
924a556c76aSAlexandre Belloni 		return -EINVAL;
925a556c76aSAlexandre Belloni 
926a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
927a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
928a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
929a556c76aSAlexandre Belloni 
930a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
931a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
932a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
933a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
934a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
935a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
936a556c76aSAlexandre Belloni 
937a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
938a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
939a556c76aSAlexandre Belloni 
940a556c76aSAlexandre Belloni 	return 0;
941a556c76aSAlexandre Belloni }
942a556c76aSAlexandre Belloni 
943531ee1a6SVladimir Oltean static int ocelot_fdb_dump(struct ocelot *ocelot, int port,
944531ee1a6SVladimir Oltean 			   dsa_fdb_dump_cb_t *cb, void *data)
945a556c76aSAlexandre Belloni {
946531ee1a6SVladimir Oltean 	int i, j;
947a556c76aSAlexandre Belloni 
948a556c76aSAlexandre Belloni 	/* Loop through all the mac tables entries. There are 1024 rows of 4
949a556c76aSAlexandre Belloni 	 * entries.
950a556c76aSAlexandre Belloni 	 */
951a556c76aSAlexandre Belloni 	for (i = 0; i < 1024; i++) {
952a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
953531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
954531ee1a6SVladimir Oltean 			bool is_static;
955531ee1a6SVladimir Oltean 			int ret;
956531ee1a6SVladimir Oltean 
957531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
958a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
959a556c76aSAlexandre Belloni 			 * skip it.
960a556c76aSAlexandre Belloni 			 */
961a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
962a556c76aSAlexandre Belloni 				continue;
963a556c76aSAlexandre Belloni 			else if (ret)
964531ee1a6SVladimir Oltean 				return ret;
965a556c76aSAlexandre Belloni 
966531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
967531ee1a6SVladimir Oltean 
968531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
969a556c76aSAlexandre Belloni 			if (ret)
970531ee1a6SVladimir Oltean 				return ret;
971a556c76aSAlexandre Belloni 		}
972a556c76aSAlexandre Belloni 	}
973a556c76aSAlexandre Belloni 
974531ee1a6SVladimir Oltean 	return 0;
975531ee1a6SVladimir Oltean }
976531ee1a6SVladimir Oltean 
977531ee1a6SVladimir Oltean static int ocelot_port_fdb_dump(struct sk_buff *skb,
978531ee1a6SVladimir Oltean 				struct netlink_callback *cb,
979531ee1a6SVladimir Oltean 				struct net_device *dev,
980531ee1a6SVladimir Oltean 				struct net_device *filter_dev, int *idx)
981531ee1a6SVladimir Oltean {
982004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
983004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
984531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx dump = {
985531ee1a6SVladimir Oltean 		.dev = dev,
986531ee1a6SVladimir Oltean 		.skb = skb,
987531ee1a6SVladimir Oltean 		.cb = cb,
988531ee1a6SVladimir Oltean 		.idx = *idx,
989531ee1a6SVladimir Oltean 	};
990004d44f6SVladimir Oltean 	int port = priv->chip_port;
991531ee1a6SVladimir Oltean 	int ret;
992531ee1a6SVladimir Oltean 
993004d44f6SVladimir Oltean 	ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
994531ee1a6SVladimir Oltean 
995a556c76aSAlexandre Belloni 	*idx = dump.idx;
996531ee1a6SVladimir Oltean 
997a556c76aSAlexandre Belloni 	return ret;
998a556c76aSAlexandre Belloni }
999a556c76aSAlexandre Belloni 
10007142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
10017142529fSAntoine Tenart 				  u16 vid)
10027142529fSAntoine Tenart {
10031c44ce56SVladimir Oltean 	return ocelot_vlan_vid_add(dev, vid, false, false);
10047142529fSAntoine Tenart }
10057142529fSAntoine Tenart 
10067142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
10077142529fSAntoine Tenart 				   u16 vid)
10087142529fSAntoine Tenart {
10097142529fSAntoine Tenart 	return ocelot_vlan_vid_del(dev, vid);
10107142529fSAntoine Tenart }
10117142529fSAntoine Tenart 
10127142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev,
10137142529fSAntoine Tenart 			       netdev_features_t features)
10147142529fSAntoine Tenart {
10157142529fSAntoine Tenart 	netdev_features_t changed = dev->features ^ features;
1016004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1017004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1018004d44f6SVladimir Oltean 	int port = priv->chip_port;
10197142529fSAntoine Tenart 
10202c1d029aSJoergen Andreasen 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
1021004d44f6SVladimir Oltean 	    priv->tc.offload_cnt) {
10222c1d029aSJoergen Andreasen 		netdev_err(dev,
10232c1d029aSJoergen Andreasen 			   "Cannot disable HW TC offload while offloads active\n");
10242c1d029aSJoergen Andreasen 		return -EBUSY;
10252c1d029aSJoergen Andreasen 	}
10262c1d029aSJoergen Andreasen 
10277142529fSAntoine Tenart 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
1028f270dbfaSVladimir Oltean 		ocelot_vlan_mode(ocelot, port, features);
10297142529fSAntoine Tenart 
10307142529fSAntoine Tenart 	return 0;
10317142529fSAntoine Tenart }
10327142529fSAntoine Tenart 
1033751302c3SFlorian Fainelli static int ocelot_get_port_parent_id(struct net_device *dev,
1034751302c3SFlorian Fainelli 				     struct netdev_phys_item_id *ppid)
1035751302c3SFlorian Fainelli {
1036004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1037004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1038751302c3SFlorian Fainelli 
1039751302c3SFlorian Fainelli 	ppid->id_len = sizeof(ocelot->base_mac);
1040751302c3SFlorian Fainelli 	memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
1041751302c3SFlorian Fainelli 
1042751302c3SFlorian Fainelli 	return 0;
1043751302c3SFlorian Fainelli }
1044751302c3SFlorian Fainelli 
1045306fd44bSVladimir Oltean static int ocelot_hwstamp_get(struct ocelot *ocelot, int port,
1046306fd44bSVladimir Oltean 			      struct ifreq *ifr)
10474e3b0468SAntoine Tenart {
10484e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
10494e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
10504e3b0468SAntoine Tenart }
10514e3b0468SAntoine Tenart 
1052306fd44bSVladimir Oltean static int ocelot_hwstamp_set(struct ocelot *ocelot, int port,
1053306fd44bSVladimir Oltean 			      struct ifreq *ifr)
10544e3b0468SAntoine Tenart {
1055306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
10564e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
10574e3b0468SAntoine Tenart 
10584e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
10594e3b0468SAntoine Tenart 		return -EFAULT;
10604e3b0468SAntoine Tenart 
10614e3b0468SAntoine Tenart 	/* reserved for future extensions */
10624e3b0468SAntoine Tenart 	if (cfg.flags)
10634e3b0468SAntoine Tenart 		return -EINVAL;
10644e3b0468SAntoine Tenart 
10654e3b0468SAntoine Tenart 	/* Tx type sanity check */
10664e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
10674e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1068306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
10694e3b0468SAntoine Tenart 		break;
10704e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
10714e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
10724e3b0468SAntoine Tenart 		 * need to update the origin time.
10734e3b0468SAntoine Tenart 		 */
1074306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
10754e3b0468SAntoine Tenart 		break;
10764e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1077306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
10784e3b0468SAntoine Tenart 		break;
10794e3b0468SAntoine Tenart 	default:
10804e3b0468SAntoine Tenart 		return -ERANGE;
10814e3b0468SAntoine Tenart 	}
10824e3b0468SAntoine Tenart 
10834e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
10844e3b0468SAntoine Tenart 
10854e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
10864e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
10874e3b0468SAntoine Tenart 		break;
10884e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
10894e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
10904e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
10914e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
10924e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
10934e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
10944e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
10954e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
10964e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
10974e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
10984e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
10994e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
11004e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
11014e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
11024e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
11034e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
11044e3b0468SAntoine Tenart 		break;
11054e3b0468SAntoine Tenart 	default:
11064e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
11074e3b0468SAntoine Tenart 		return -ERANGE;
11084e3b0468SAntoine Tenart 	}
11094e3b0468SAntoine Tenart 
11104e3b0468SAntoine Tenart 	/* Commit back the result & save it */
11114e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
11124e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
11134e3b0468SAntoine Tenart 
11144e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
11154e3b0468SAntoine Tenart }
11164e3b0468SAntoine Tenart 
11174e3b0468SAntoine Tenart static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11184e3b0468SAntoine Tenart {
1119004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1120004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1121004d44f6SVladimir Oltean 	int port = priv->chip_port;
11224e3b0468SAntoine Tenart 
11234e3b0468SAntoine Tenart 	/* The function is only used for PTP operations for now */
11244e3b0468SAntoine Tenart 	if (!ocelot->ptp)
11254e3b0468SAntoine Tenart 		return -EOPNOTSUPP;
11264e3b0468SAntoine Tenart 
11274e3b0468SAntoine Tenart 	switch (cmd) {
11284e3b0468SAntoine Tenart 	case SIOCSHWTSTAMP:
1129306fd44bSVladimir Oltean 		return ocelot_hwstamp_set(ocelot, port, ifr);
11304e3b0468SAntoine Tenart 	case SIOCGHWTSTAMP:
1131306fd44bSVladimir Oltean 		return ocelot_hwstamp_get(ocelot, port, ifr);
11324e3b0468SAntoine Tenart 	default:
11334e3b0468SAntoine Tenart 		return -EOPNOTSUPP;
11344e3b0468SAntoine Tenart 	}
11354e3b0468SAntoine Tenart }
11364e3b0468SAntoine Tenart 
1137a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = {
1138a556c76aSAlexandre Belloni 	.ndo_open			= ocelot_port_open,
1139a556c76aSAlexandre Belloni 	.ndo_stop			= ocelot_port_stop,
1140a556c76aSAlexandre Belloni 	.ndo_start_xmit			= ocelot_port_xmit,
1141a556c76aSAlexandre Belloni 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
1142a556c76aSAlexandre Belloni 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
1143a556c76aSAlexandre Belloni 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
1144a556c76aSAlexandre Belloni 	.ndo_get_stats64		= ocelot_get_stats64,
1145531ee1a6SVladimir Oltean 	.ndo_fdb_add			= ocelot_port_fdb_add,
1146531ee1a6SVladimir Oltean 	.ndo_fdb_del			= ocelot_port_fdb_del,
1147531ee1a6SVladimir Oltean 	.ndo_fdb_dump			= ocelot_port_fdb_dump,
11487142529fSAntoine Tenart 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
11497142529fSAntoine Tenart 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
11507142529fSAntoine Tenart 	.ndo_set_features		= ocelot_set_features,
1151751302c3SFlorian Fainelli 	.ndo_get_port_parent_id		= ocelot_get_port_parent_id,
11522c1d029aSJoergen Andreasen 	.ndo_setup_tc			= ocelot_setup_tc,
11534e3b0468SAntoine Tenart 	.ndo_do_ioctl			= ocelot_ioctl,
1154a556c76aSAlexandre Belloni };
1155a556c76aSAlexandre Belloni 
1156c7282d38SVladimir Oltean static void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset,
1157c7282d38SVladimir Oltean 			       u8 *data)
1158a556c76aSAlexandre Belloni {
1159a556c76aSAlexandre Belloni 	int i;
1160a556c76aSAlexandre Belloni 
1161a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1162a556c76aSAlexandre Belloni 		return;
1163a556c76aSAlexandre Belloni 
1164a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1165a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1166a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1167a556c76aSAlexandre Belloni }
1168a556c76aSAlexandre Belloni 
1169c7282d38SVladimir Oltean static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
1170c7282d38SVladimir Oltean 				    u8 *data)
1171c7282d38SVladimir Oltean {
1172c7282d38SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(netdev);
1173c7282d38SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1174c7282d38SVladimir Oltean 	int port = priv->chip_port;
1175c7282d38SVladimir Oltean 
1176c7282d38SVladimir Oltean 	ocelot_get_strings(ocelot, port, sset, data);
1177c7282d38SVladimir Oltean }
1178c7282d38SVladimir Oltean 
11791e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1180a556c76aSAlexandre Belloni {
1181a556c76aSAlexandre Belloni 	int i, j;
1182a556c76aSAlexandre Belloni 
1183a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1184a556c76aSAlexandre Belloni 
1185a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1186a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1187a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1188a556c76aSAlexandre Belloni 
1189a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1190a556c76aSAlexandre Belloni 			u32 val;
1191a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1192a556c76aSAlexandre Belloni 
1193a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1194a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1195a556c76aSAlexandre Belloni 
1196a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1197a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1198a556c76aSAlexandre Belloni 
1199a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1200a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1201a556c76aSAlexandre Belloni 		}
1202a556c76aSAlexandre Belloni 	}
1203a556c76aSAlexandre Belloni 
12041e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
12051e1caa97SClaudiu Manoil }
12061e1caa97SClaudiu Manoil 
12071e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
12081e1caa97SClaudiu Manoil {
12091e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
12101e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
12111e1caa97SClaudiu Manoil 					     stats_work);
12121e1caa97SClaudiu Manoil 
12131e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
12141e1caa97SClaudiu Manoil 
1215a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1216a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1217a556c76aSAlexandre Belloni }
1218a556c76aSAlexandre Belloni 
1219c7282d38SVladimir Oltean static void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1220a556c76aSAlexandre Belloni {
1221a556c76aSAlexandre Belloni 	int i;
1222a556c76aSAlexandre Belloni 
1223a556c76aSAlexandre Belloni 	/* check and update now */
12241e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1225a556c76aSAlexandre Belloni 
1226a556c76aSAlexandre Belloni 	/* Copy all counters */
1227a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1228004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1229a556c76aSAlexandre Belloni }
1230a556c76aSAlexandre Belloni 
1231c7282d38SVladimir Oltean static void ocelot_port_get_ethtool_stats(struct net_device *dev,
1232c7282d38SVladimir Oltean 					  struct ethtool_stats *stats,
1233c7282d38SVladimir Oltean 					  u64 *data)
1234a556c76aSAlexandre Belloni {
1235004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1236004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1237c7282d38SVladimir Oltean 	int port = priv->chip_port;
1238a556c76aSAlexandre Belloni 
1239c7282d38SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
1240c7282d38SVladimir Oltean }
1241c7282d38SVladimir Oltean 
1242c7282d38SVladimir Oltean static int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1243c7282d38SVladimir Oltean {
1244a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1245a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1246c7282d38SVladimir Oltean 
1247a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1248a556c76aSAlexandre Belloni }
1249a556c76aSAlexandre Belloni 
1250c7282d38SVladimir Oltean static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
12514e3b0468SAntoine Tenart {
1252004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1253004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1254c7282d38SVladimir Oltean 	int port = priv->chip_port;
12554e3b0468SAntoine Tenart 
1256c7282d38SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
1257c7282d38SVladimir Oltean }
12584e3b0468SAntoine Tenart 
1259c7282d38SVladimir Oltean static int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1260c7282d38SVladimir Oltean 			      struct ethtool_ts_info *info)
1261c7282d38SVladimir Oltean {
12624e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
12634e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
12644e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
12654e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
12664e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
12674e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
12684e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
12694e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
12704e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
12714e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
12724e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
12734e3b0468SAntoine Tenart 
12744e3b0468SAntoine Tenart 	return 0;
12754e3b0468SAntoine Tenart }
12764e3b0468SAntoine Tenart 
1277c7282d38SVladimir Oltean static int ocelot_port_get_ts_info(struct net_device *dev,
1278c7282d38SVladimir Oltean 				   struct ethtool_ts_info *info)
1279c7282d38SVladimir Oltean {
1280c7282d38SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1281c7282d38SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1282c7282d38SVladimir Oltean 	int port = priv->chip_port;
1283c7282d38SVladimir Oltean 
1284c7282d38SVladimir Oltean 	if (!ocelot->ptp)
1285c7282d38SVladimir Oltean 		return ethtool_op_get_ts_info(dev, info);
1286c7282d38SVladimir Oltean 
1287c7282d38SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
1288c7282d38SVladimir Oltean }
1289c7282d38SVladimir Oltean 
1290a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = {
1291c7282d38SVladimir Oltean 	.get_strings		= ocelot_port_get_strings,
1292c7282d38SVladimir Oltean 	.get_ethtool_stats	= ocelot_port_get_ethtool_stats,
1293c7282d38SVladimir Oltean 	.get_sset_count		= ocelot_port_get_sset_count,
1294dc96ee37SAlexandre Belloni 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1295dc96ee37SAlexandre Belloni 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1296c7282d38SVladimir Oltean 	.get_ts_info		= ocelot_port_get_ts_info,
1297a556c76aSAlexandre Belloni };
1298a556c76aSAlexandre Belloni 
12994bda1415SVladimir Oltean static void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port,
1300a556c76aSAlexandre Belloni 					u8 state)
1301a556c76aSAlexandre Belloni {
1302a556c76aSAlexandre Belloni 	u32 port_cfg;
13034bda1415SVladimir Oltean 	int p, i;
1304a556c76aSAlexandre Belloni 
13054bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
13064bda1415SVladimir Oltean 		return;
1307a556c76aSAlexandre Belloni 
13084bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1309a556c76aSAlexandre Belloni 
1310a556c76aSAlexandre Belloni 	switch (state) {
1311a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
13124bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
1313a556c76aSAlexandre Belloni 		/* Fallthrough */
1314a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1315a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1316a556c76aSAlexandre Belloni 		break;
1317a556c76aSAlexandre Belloni 
1318a556c76aSAlexandre Belloni 	default:
1319a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
13204bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
1321a556c76aSAlexandre Belloni 		break;
1322a556c76aSAlexandre Belloni 	}
1323a556c76aSAlexandre Belloni 
13244bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
1325a556c76aSAlexandre Belloni 
1326a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1327a556c76aSAlexandre Belloni 	 * a source for the other ports.
1328a556c76aSAlexandre Belloni 	 */
13294bda1415SVladimir Oltean 	for (p = 0; p < ocelot->num_phys_ports; p++) {
1330c9d2203bSVladimir Oltean 		if (p == ocelot->cpu || (ocelot->bridge_fwd_mask & BIT(p))) {
13314bda1415SVladimir Oltean 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
1332a556c76aSAlexandre Belloni 
1333a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1334a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
1335a556c76aSAlexandre Belloni 
1336a556c76aSAlexandre Belloni 				if (!bond_mask)
1337a556c76aSAlexandre Belloni 					continue;
1338a556c76aSAlexandre Belloni 
13394bda1415SVladimir Oltean 				if (bond_mask & BIT(p)) {
1340a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
1341a556c76aSAlexandre Belloni 					break;
1342a556c76aSAlexandre Belloni 				}
1343a556c76aSAlexandre Belloni 			}
1344a556c76aSAlexandre Belloni 
1345c9d2203bSVladimir Oltean 			/* Avoid the NPI port from looping back to itself */
1346c9d2203bSVladimir Oltean 			if (p != ocelot->cpu)
1347c9d2203bSVladimir Oltean 				mask |= BIT(ocelot->cpu);
1348c9d2203bSVladimir Oltean 
1349c9d2203bSVladimir Oltean 			ocelot_write_rix(ocelot, mask,
13504bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
1351a556c76aSAlexandre Belloni 		} else {
1352a556c76aSAlexandre Belloni 			/* Only the CPU port, this is compatible with link
1353a556c76aSAlexandre Belloni 			 * aggregation.
1354a556c76aSAlexandre Belloni 			 */
1355a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1356c9d2203bSVladimir Oltean 					 BIT(ocelot->cpu),
13574bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
13584bda1415SVladimir Oltean 		}
1359a556c76aSAlexandre Belloni 	}
1360a556c76aSAlexandre Belloni }
1361a556c76aSAlexandre Belloni 
13624bda1415SVladimir Oltean static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
13634bda1415SVladimir Oltean 					   struct switchdev_trans *trans,
13644bda1415SVladimir Oltean 					   u8 state)
1365a556c76aSAlexandre Belloni {
13664bda1415SVladimir Oltean 	if (switchdev_trans_ph_prepare(trans))
13674bda1415SVladimir Oltean 		return;
1368a556c76aSAlexandre Belloni 
13694bda1415SVladimir Oltean 	ocelot_bridge_stp_state_set(ocelot, port, state);
13704bda1415SVladimir Oltean }
13714bda1415SVladimir Oltean 
13724bda1415SVladimir Oltean static void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
13734bda1415SVladimir Oltean {
13744bda1415SVladimir Oltean 	ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(msecs / 2),
1375a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1376a556c76aSAlexandre Belloni }
1377a556c76aSAlexandre Belloni 
13784bda1415SVladimir Oltean static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
13794bda1415SVladimir Oltean 					unsigned long ageing_clock_t)
1380a556c76aSAlexandre Belloni {
13814bda1415SVladimir Oltean 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
13824bda1415SVladimir Oltean 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1383a556c76aSAlexandre Belloni 
13844bda1415SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
13854bda1415SVladimir Oltean }
13864bda1415SVladimir Oltean 
13874bda1415SVladimir Oltean static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
13884bda1415SVladimir Oltean {
13894bda1415SVladimir Oltean 	u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1390a556c76aSAlexandre Belloni 			    ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1391a556c76aSAlexandre Belloni 			    ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
13924bda1415SVladimir Oltean 	u32 val = 0;
1393a556c76aSAlexandre Belloni 
13944bda1415SVladimir Oltean 	if (mc)
13954bda1415SVladimir Oltean 		val = cpu_fwd_mcast;
13964bda1415SVladimir Oltean 
13974bda1415SVladimir Oltean 	ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
13984bda1415SVladimir Oltean 		       ANA_PORT_CPU_FWD_CFG, port);
1399a556c76aSAlexandre Belloni }
1400a556c76aSAlexandre Belloni 
1401a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev,
1402a556c76aSAlexandre Belloni 				const struct switchdev_attr *attr,
1403a556c76aSAlexandre Belloni 				struct switchdev_trans *trans)
1404a556c76aSAlexandre Belloni {
1405004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1406004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1407004d44f6SVladimir Oltean 	int port = priv->chip_port;
1408a556c76aSAlexandre Belloni 	int err = 0;
1409a556c76aSAlexandre Belloni 
1410a556c76aSAlexandre Belloni 	switch (attr->id) {
1411a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
14124bda1415SVladimir Oltean 		ocelot_port_attr_stp_state_set(ocelot, port, trans,
1413a556c76aSAlexandre Belloni 					       attr->u.stp_state);
1414a556c76aSAlexandre Belloni 		break;
1415a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
14164bda1415SVladimir Oltean 		ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
1417a556c76aSAlexandre Belloni 		break;
14187142529fSAntoine Tenart 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1419004d44f6SVladimir Oltean 		priv->vlan_aware = attr->u.vlan_filtering;
1420004d44f6SVladimir Oltean 		ocelot_port_vlan_filtering(ocelot, port, priv->vlan_aware);
14217142529fSAntoine Tenart 		break;
1422a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
14234bda1415SVladimir Oltean 		ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
1424a556c76aSAlexandre Belloni 		break;
1425a556c76aSAlexandre Belloni 	default:
1426a556c76aSAlexandre Belloni 		err = -EOPNOTSUPP;
1427a556c76aSAlexandre Belloni 		break;
1428a556c76aSAlexandre Belloni 	}
1429a556c76aSAlexandre Belloni 
1430a556c76aSAlexandre Belloni 	return err;
1431a556c76aSAlexandre Belloni }
1432a556c76aSAlexandre Belloni 
14337142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev,
14347142529fSAntoine Tenart 				    const struct switchdev_obj_port_vlan *vlan,
14357142529fSAntoine Tenart 				    struct switchdev_trans *trans)
14367142529fSAntoine Tenart {
14377142529fSAntoine Tenart 	int ret;
14387142529fSAntoine Tenart 	u16 vid;
14397142529fSAntoine Tenart 
14407142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
14417142529fSAntoine Tenart 		ret = ocelot_vlan_vid_add(dev, vid,
14427142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
14437142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
14447142529fSAntoine Tenart 		if (ret)
14457142529fSAntoine Tenart 			return ret;
14467142529fSAntoine Tenart 	}
14477142529fSAntoine Tenart 
14487142529fSAntoine Tenart 	return 0;
14497142529fSAntoine Tenart }
14507142529fSAntoine Tenart 
14517142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev,
14527142529fSAntoine Tenart 				     const struct switchdev_obj_port_vlan *vlan)
14537142529fSAntoine Tenart {
14547142529fSAntoine Tenart 	int ret;
14557142529fSAntoine Tenart 	u16 vid;
14567142529fSAntoine Tenart 
14577142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
14587142529fSAntoine Tenart 		ret = ocelot_vlan_vid_del(dev, vid);
14597142529fSAntoine Tenart 
14607142529fSAntoine Tenart 		if (ret)
14617142529fSAntoine Tenart 			return ret;
14627142529fSAntoine Tenart 	}
14637142529fSAntoine Tenart 
14647142529fSAntoine Tenart 	return 0;
14657142529fSAntoine Tenart }
14667142529fSAntoine Tenart 
1467a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1468a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1469a556c76aSAlexandre Belloni 						     u16 vid)
1470a556c76aSAlexandre Belloni {
1471a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1472a556c76aSAlexandre Belloni 
1473a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1474a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1475a556c76aSAlexandre Belloni 			return mc;
1476a556c76aSAlexandre Belloni 	}
1477a556c76aSAlexandre Belloni 
1478a556c76aSAlexandre Belloni 	return NULL;
1479a556c76aSAlexandre Belloni }
1480a556c76aSAlexandre Belloni 
1481a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev,
1482a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb,
1483a556c76aSAlexandre Belloni 				   struct switchdev_trans *trans)
1484a556c76aSAlexandre Belloni {
1485004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1486004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
1487004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1488a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1489004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1490004d44f6SVladimir Oltean 	int port = priv->chip_port;
1491a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1492a556c76aSAlexandre Belloni 	bool new = false;
1493a556c76aSAlexandre Belloni 
1494a556c76aSAlexandre Belloni 	if (!vid)
1495004d44f6SVladimir Oltean 		vid = ocelot_port->pvid;
1496a556c76aSAlexandre Belloni 
1497a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1498a556c76aSAlexandre Belloni 	if (!mc) {
1499a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1500a556c76aSAlexandre Belloni 		if (!mc)
1501a556c76aSAlexandre Belloni 			return -ENOMEM;
1502a556c76aSAlexandre Belloni 
1503a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1504a556c76aSAlexandre Belloni 		mc->vid = vid;
1505a556c76aSAlexandre Belloni 
1506a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1507a556c76aSAlexandre Belloni 		new = true;
1508a556c76aSAlexandre Belloni 	}
1509a556c76aSAlexandre Belloni 
1510a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1511a556c76aSAlexandre Belloni 	addr[0] = 0;
1512a556c76aSAlexandre Belloni 
1513a556c76aSAlexandre Belloni 	if (!new) {
1514a556c76aSAlexandre Belloni 		addr[2] = mc->ports << 0;
1515a556c76aSAlexandre Belloni 		addr[1] = mc->ports << 8;
1516a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1517a556c76aSAlexandre Belloni 	}
1518a556c76aSAlexandre Belloni 
1519004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1520a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1521a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1522a556c76aSAlexandre Belloni 
1523a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1524a556c76aSAlexandre Belloni }
1525a556c76aSAlexandre Belloni 
1526a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev,
1527a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb)
1528a556c76aSAlexandre Belloni {
1529004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1530004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
1531004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1532a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1533004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1534004d44f6SVladimir Oltean 	int port = priv->chip_port;
1535a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1536a556c76aSAlexandre Belloni 
1537a556c76aSAlexandre Belloni 	if (!vid)
1538004d44f6SVladimir Oltean 		vid = ocelot_port->pvid;
1539a556c76aSAlexandre Belloni 
1540a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1541a556c76aSAlexandre Belloni 	if (!mc)
1542a556c76aSAlexandre Belloni 		return -ENOENT;
1543a556c76aSAlexandre Belloni 
1544a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1545a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1546a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1547a556c76aSAlexandre Belloni 	addr[0] = 0;
1548a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1549a556c76aSAlexandre Belloni 
1550004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1551a556c76aSAlexandre Belloni 	if (!mc->ports) {
1552a556c76aSAlexandre Belloni 		list_del(&mc->list);
1553a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1554a556c76aSAlexandre Belloni 		return 0;
1555a556c76aSAlexandre Belloni 	}
1556a556c76aSAlexandre Belloni 
1557a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1558a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1559a556c76aSAlexandre Belloni 
1560a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1561a556c76aSAlexandre Belloni }
1562a556c76aSAlexandre Belloni 
1563a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev,
1564a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj,
156569213513SPetr Machata 			       struct switchdev_trans *trans,
156669213513SPetr Machata 			       struct netlink_ext_ack *extack)
1567a556c76aSAlexandre Belloni {
1568a556c76aSAlexandre Belloni 	int ret = 0;
1569a556c76aSAlexandre Belloni 
1570a556c76aSAlexandre Belloni 	switch (obj->id) {
15717142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
15727142529fSAntoine Tenart 		ret = ocelot_port_obj_add_vlan(dev,
15737142529fSAntoine Tenart 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
15747142529fSAntoine Tenart 					       trans);
15757142529fSAntoine Tenart 		break;
1576a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1577a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1578a556c76aSAlexandre Belloni 					      trans);
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_obj_del(struct net_device *dev,
1588a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj)
1589a556c76aSAlexandre Belloni {
1590a556c76aSAlexandre Belloni 	int ret = 0;
1591a556c76aSAlexandre Belloni 
1592a556c76aSAlexandre Belloni 	switch (obj->id) {
15937142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
15947142529fSAntoine Tenart 		ret = ocelot_port_vlan_del_vlan(dev,
15957142529fSAntoine Tenart 						SWITCHDEV_OBJ_PORT_VLAN(obj));
15967142529fSAntoine Tenart 		break;
1597a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1598a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1599a556c76aSAlexandre Belloni 		break;
1600a556c76aSAlexandre Belloni 	default:
1601a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1602a556c76aSAlexandre Belloni 	}
1603a556c76aSAlexandre Belloni 
1604a556c76aSAlexandre Belloni 	return ret;
1605a556c76aSAlexandre Belloni }
1606a556c76aSAlexandre Belloni 
1607f270dbfaSVladimir Oltean static int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1608a556c76aSAlexandre Belloni 				   struct net_device *bridge)
1609a556c76aSAlexandre Belloni {
1610a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1611a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1612a556c76aSAlexandre Belloni 	} else {
1613a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1614a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1615a556c76aSAlexandre Belloni 			 * unsupported */
1616a556c76aSAlexandre Belloni 			return -ENODEV;
1617a556c76aSAlexandre Belloni 	}
1618a556c76aSAlexandre Belloni 
1619f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1620a556c76aSAlexandre Belloni 
1621a556c76aSAlexandre Belloni 	return 0;
1622a556c76aSAlexandre Belloni }
1623a556c76aSAlexandre Belloni 
1624f270dbfaSVladimir Oltean static int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1625a556c76aSAlexandre Belloni 				    struct net_device *bridge)
1626a556c76aSAlexandre Belloni {
162797bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1628a556c76aSAlexandre Belloni 
1629a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1630a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
16317142529fSAntoine Tenart 
163297bb69e1SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, 0);
163397bb69e1SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, 0);
163497bb69e1SVladimir Oltean 	return ocelot_port_set_native_vlan(ocelot, port, 0);
1635a556c76aSAlexandre Belloni }
1636a556c76aSAlexandre Belloni 
1637dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1638dc96ee37SAlexandre Belloni {
1639dc96ee37SAlexandre Belloni 	int i, port, lag;
1640dc96ee37SAlexandre Belloni 
1641dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1642dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1643dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1644dc96ee37SAlexandre Belloni 
1645dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1646dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1647dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1648dc96ee37SAlexandre Belloni 
1649dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1650dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1651dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1652dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1653dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1654dc96ee37SAlexandre Belloni 
1655dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1656dc96ee37SAlexandre Belloni 		if (!bond_mask)
1657dc96ee37SAlexandre Belloni 			continue;
1658dc96ee37SAlexandre Belloni 
1659dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1660dc96ee37SAlexandre Belloni 			// Destination mask
1661dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1662dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1663dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1664dc96ee37SAlexandre Belloni 			aggr_count++;
1665dc96ee37SAlexandre Belloni 		}
1666dc96ee37SAlexandre Belloni 
1667dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1668dc96ee37SAlexandre Belloni 			u32 ac;
1669dc96ee37SAlexandre Belloni 
1670dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1671dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1672dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1673dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1674dc96ee37SAlexandre Belloni 		}
1675dc96ee37SAlexandre Belloni 	}
1676dc96ee37SAlexandre Belloni }
1677dc96ee37SAlexandre Belloni 
1678dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1679dc96ee37SAlexandre Belloni {
1680dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1681dc96ee37SAlexandre Belloni 	unsigned int p;
1682dc96ee37SAlexandre Belloni 
1683dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1684dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1685dc96ee37SAlexandre Belloni 
1686dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1687dc96ee37SAlexandre Belloni 
1688dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1689dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1690dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1691dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1692dc96ee37SAlexandre Belloni 	}
1693dc96ee37SAlexandre Belloni }
1694dc96ee37SAlexandre Belloni 
1695f270dbfaSVladimir Oltean static int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1696dc96ee37SAlexandre Belloni 				struct net_device *bond)
1697dc96ee37SAlexandre Belloni {
1698dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1699dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1700f270dbfaSVladimir Oltean 	int lag, lp;
1701dc96ee37SAlexandre Belloni 
1702dc96ee37SAlexandre Belloni 	rcu_read_lock();
1703dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1704004d44f6SVladimir Oltean 		struct ocelot_port_private *priv = netdev_priv(ndev);
1705dc96ee37SAlexandre Belloni 
1706004d44f6SVladimir Oltean 		bond_mask |= BIT(priv->chip_port);
1707dc96ee37SAlexandre Belloni 	}
1708dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1709dc96ee37SAlexandre Belloni 
1710dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1711dc96ee37SAlexandre Belloni 
1712dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1713dc96ee37SAlexandre Belloni 	 * now on
1714dc96ee37SAlexandre Belloni 	 */
1715f270dbfaSVladimir Oltean 	if (port == lp) {
1716f270dbfaSVladimir Oltean 		lag = port;
1717f270dbfaSVladimir Oltean 		ocelot->lags[port] = bond_mask;
1718f270dbfaSVladimir Oltean 		bond_mask &= ~BIT(port);
1719dc96ee37SAlexandre Belloni 		if (bond_mask) {
1720dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1721dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1722dc96ee37SAlexandre Belloni 		}
1723dc96ee37SAlexandre Belloni 	} else {
1724dc96ee37SAlexandre Belloni 		lag = lp;
1725f270dbfaSVladimir Oltean 		ocelot->lags[lp] |= BIT(port);
1726dc96ee37SAlexandre Belloni 	}
1727dc96ee37SAlexandre Belloni 
1728dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1729dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1730dc96ee37SAlexandre Belloni 
1731dc96ee37SAlexandre Belloni 	return 0;
1732dc96ee37SAlexandre Belloni }
1733dc96ee37SAlexandre Belloni 
1734f270dbfaSVladimir Oltean static void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1735dc96ee37SAlexandre Belloni 				  struct net_device *bond)
1736dc96ee37SAlexandre Belloni {
1737dc96ee37SAlexandre Belloni 	u32 port_cfg;
1738dc96ee37SAlexandre Belloni 	int i;
1739dc96ee37SAlexandre Belloni 
1740dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1741dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1742f270dbfaSVladimir Oltean 		ocelot->lags[i] &= ~BIT(port);
1743dc96ee37SAlexandre Belloni 
1744dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1745dc96ee37SAlexandre Belloni 	 * next port
1746dc96ee37SAlexandre Belloni 	 */
1747f270dbfaSVladimir Oltean 	if (ocelot->lags[port]) {
1748f270dbfaSVladimir Oltean 		int n = __ffs(ocelot->lags[port]);
1749dc96ee37SAlexandre Belloni 
1750f270dbfaSVladimir Oltean 		ocelot->lags[n] = ocelot->lags[port];
1751f270dbfaSVladimir Oltean 		ocelot->lags[port] = 0;
1752dc96ee37SAlexandre Belloni 
1753dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1754dc96ee37SAlexandre Belloni 	}
1755dc96ee37SAlexandre Belloni 
1756f270dbfaSVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1757dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1758f270dbfaSVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1759f270dbfaSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
1760dc96ee37SAlexandre Belloni 
1761dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1762dc96ee37SAlexandre Belloni }
1763dc96ee37SAlexandre Belloni 
1764a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */
1765a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1766a556c76aSAlexandre Belloni {
1767a556c76aSAlexandre Belloni 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1768a556c76aSAlexandre Belloni }
1769a556c76aSAlexandre Belloni 
1770a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev,
1771a556c76aSAlexandre Belloni 				       unsigned long event,
1772a556c76aSAlexandre Belloni 				       struct netdev_notifier_changeupper_info *info)
1773a556c76aSAlexandre Belloni {
1774004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1775004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
1776f270dbfaSVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1777004d44f6SVladimir Oltean 	int port = priv->chip_port;
1778a556c76aSAlexandre Belloni 	int err = 0;
1779a556c76aSAlexandre Belloni 
1780a556c76aSAlexandre Belloni 	switch (event) {
1781a556c76aSAlexandre Belloni 	case NETDEV_CHANGEUPPER:
1782a556c76aSAlexandre Belloni 		if (netif_is_bridge_master(info->upper_dev)) {
1783004d44f6SVladimir Oltean 			if (info->linking) {
1784f270dbfaSVladimir Oltean 				err = ocelot_port_bridge_join(ocelot, port,
1785a556c76aSAlexandre Belloni 							      info->upper_dev);
1786004d44f6SVladimir Oltean 			} else {
1787f270dbfaSVladimir Oltean 				err = ocelot_port_bridge_leave(ocelot, port,
1788a556c76aSAlexandre Belloni 							       info->upper_dev);
1789004d44f6SVladimir Oltean 				priv->vlan_aware = false;
1790004d44f6SVladimir Oltean 			}
1791a556c76aSAlexandre Belloni 		}
1792dc96ee37SAlexandre Belloni 		if (netif_is_lag_master(info->upper_dev)) {
1793dc96ee37SAlexandre Belloni 			if (info->linking)
1794f270dbfaSVladimir Oltean 				err = ocelot_port_lag_join(ocelot, port,
1795dc96ee37SAlexandre Belloni 							   info->upper_dev);
1796dc96ee37SAlexandre Belloni 			else
1797f270dbfaSVladimir Oltean 				ocelot_port_lag_leave(ocelot, port,
1798dc96ee37SAlexandre Belloni 						      info->upper_dev);
1799dc96ee37SAlexandre Belloni 		}
1800a556c76aSAlexandre Belloni 		break;
1801a556c76aSAlexandre Belloni 	default:
1802a556c76aSAlexandre Belloni 		break;
1803a556c76aSAlexandre Belloni 	}
1804a556c76aSAlexandre Belloni 
1805a556c76aSAlexandre Belloni 	return err;
1806a556c76aSAlexandre Belloni }
1807a556c76aSAlexandre Belloni 
1808a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused,
1809a556c76aSAlexandre Belloni 				  unsigned long event, void *ptr)
1810a556c76aSAlexandre Belloni {
1811a556c76aSAlexandre Belloni 	struct netdev_notifier_changeupper_info *info = ptr;
1812a556c76aSAlexandre Belloni 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
18132ac0e152SGeert Uytterhoeven 	int ret = 0;
1814a556c76aSAlexandre Belloni 
18157afb3e57SClaudiu Manoil 	if (!ocelot_netdevice_dev_check(dev))
18167afb3e57SClaudiu Manoil 		return 0;
18177afb3e57SClaudiu Manoil 
1818dc96ee37SAlexandre Belloni 	if (event == NETDEV_PRECHANGEUPPER &&
1819dc96ee37SAlexandre Belloni 	    netif_is_lag_master(info->upper_dev)) {
1820dc96ee37SAlexandre Belloni 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1821dc96ee37SAlexandre Belloni 		struct netlink_ext_ack *extack;
1822dc96ee37SAlexandre Belloni 
18233b3eed8eSClaudiu Manoil 		if (lag_upper_info &&
18243b3eed8eSClaudiu Manoil 		    lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1825dc96ee37SAlexandre Belloni 			extack = netdev_notifier_info_to_extack(&info->info);
1826dc96ee37SAlexandre Belloni 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1827dc96ee37SAlexandre Belloni 
1828dc96ee37SAlexandre Belloni 			ret = -EINVAL;
1829dc96ee37SAlexandre Belloni 			goto notify;
1830dc96ee37SAlexandre Belloni 		}
1831dc96ee37SAlexandre Belloni 	}
1832dc96ee37SAlexandre Belloni 
1833a556c76aSAlexandre Belloni 	if (netif_is_lag_master(dev)) {
1834a556c76aSAlexandre Belloni 		struct net_device *slave;
1835a556c76aSAlexandre Belloni 		struct list_head *iter;
1836a556c76aSAlexandre Belloni 
1837a556c76aSAlexandre Belloni 		netdev_for_each_lower_dev(dev, slave, iter) {
1838a556c76aSAlexandre Belloni 			ret = ocelot_netdevice_port_event(slave, event, info);
1839a556c76aSAlexandre Belloni 			if (ret)
1840a556c76aSAlexandre Belloni 				goto notify;
1841a556c76aSAlexandre Belloni 		}
1842a556c76aSAlexandre Belloni 	} else {
1843a556c76aSAlexandre Belloni 		ret = ocelot_netdevice_port_event(dev, event, info);
1844a556c76aSAlexandre Belloni 	}
1845a556c76aSAlexandre Belloni 
1846a556c76aSAlexandre Belloni notify:
1847a556c76aSAlexandre Belloni 	return notifier_from_errno(ret);
1848a556c76aSAlexandre Belloni }
1849a556c76aSAlexandre Belloni 
1850a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = {
1851a556c76aSAlexandre Belloni 	.notifier_call = ocelot_netdevice_event,
1852a556c76aSAlexandre Belloni };
1853a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb);
1854a556c76aSAlexandre Belloni 
185556da64bcSFlorian Fainelli static int ocelot_switchdev_event(struct notifier_block *unused,
185656da64bcSFlorian Fainelli 				  unsigned long event, void *ptr)
185756da64bcSFlorian Fainelli {
185856da64bcSFlorian Fainelli 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
185956da64bcSFlorian Fainelli 	int err;
186056da64bcSFlorian Fainelli 
186156da64bcSFlorian Fainelli 	switch (event) {
186256da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
186356da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
186456da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
186556da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
186656da64bcSFlorian Fainelli 		return notifier_from_errno(err);
186756da64bcSFlorian Fainelli 	}
186856da64bcSFlorian Fainelli 
186956da64bcSFlorian Fainelli 	return NOTIFY_DONE;
187056da64bcSFlorian Fainelli }
187156da64bcSFlorian Fainelli 
187256da64bcSFlorian Fainelli struct notifier_block ocelot_switchdev_nb __read_mostly = {
187356da64bcSFlorian Fainelli 	.notifier_call = ocelot_switchdev_event,
187456da64bcSFlorian Fainelli };
187556da64bcSFlorian Fainelli EXPORT_SYMBOL(ocelot_switchdev_nb);
187656da64bcSFlorian Fainelli 
18770e332c85SPetr Machata static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
18780e332c85SPetr Machata 					   unsigned long event, void *ptr)
18790e332c85SPetr Machata {
18800e332c85SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
18810e332c85SPetr Machata 	int err;
18820e332c85SPetr Machata 
18830e332c85SPetr Machata 	switch (event) {
18840e332c85SPetr Machata 		/* Blocking events. */
18850e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
18860e332c85SPetr Machata 		err = switchdev_handle_port_obj_add(dev, ptr,
18870e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
18880e332c85SPetr Machata 						    ocelot_port_obj_add);
18890e332c85SPetr Machata 		return notifier_from_errno(err);
18900e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
18910e332c85SPetr Machata 		err = switchdev_handle_port_obj_del(dev, ptr,
18920e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
18930e332c85SPetr Machata 						    ocelot_port_obj_del);
18940e332c85SPetr Machata 		return notifier_from_errno(err);
189556da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
189656da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
189756da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
189856da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
189956da64bcSFlorian Fainelli 		return notifier_from_errno(err);
19000e332c85SPetr Machata 	}
19010e332c85SPetr Machata 
19020e332c85SPetr Machata 	return NOTIFY_DONE;
19030e332c85SPetr Machata }
19040e332c85SPetr Machata 
19050e332c85SPetr Machata struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
19060e332c85SPetr Machata 	.notifier_call = ocelot_switchdev_blocking_event,
19070e332c85SPetr Machata };
19080e332c85SPetr Machata EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
19090e332c85SPetr Machata 
19104e3b0468SAntoine Tenart int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
19114e3b0468SAntoine Tenart {
19124e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
19134e3b0468SAntoine Tenart 	unsigned long flags;
19144e3b0468SAntoine Tenart 	time64_t s;
19154e3b0468SAntoine Tenart 	u32 val;
19164e3b0468SAntoine Tenart 	s64 ns;
19174e3b0468SAntoine Tenart 
19184e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
19194e3b0468SAntoine Tenart 
19204e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19214e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19224e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
19234e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19244e3b0468SAntoine Tenart 
19254e3b0468SAntoine Tenart 	s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
19264e3b0468SAntoine Tenart 	s <<= 32;
19274e3b0468SAntoine Tenart 	s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
19284e3b0468SAntoine Tenart 	ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
19294e3b0468SAntoine Tenart 
19304e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19314e3b0468SAntoine Tenart 
19324e3b0468SAntoine Tenart 	/* Deal with negative values */
19334e3b0468SAntoine Tenart 	if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
19344e3b0468SAntoine Tenart 		s--;
19354e3b0468SAntoine Tenart 		ns &= 0xf;
19364e3b0468SAntoine Tenart 		ns += 999999984;
19374e3b0468SAntoine Tenart 	}
19384e3b0468SAntoine Tenart 
19394e3b0468SAntoine Tenart 	set_normalized_timespec64(ts, s, ns);
19404e3b0468SAntoine Tenart 	return 0;
19414e3b0468SAntoine Tenart }
19424e3b0468SAntoine Tenart EXPORT_SYMBOL(ocelot_ptp_gettime64);
19434e3b0468SAntoine Tenart 
19444e3b0468SAntoine Tenart static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
19454e3b0468SAntoine Tenart 				const struct timespec64 *ts)
19464e3b0468SAntoine Tenart {
19474e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
19484e3b0468SAntoine Tenart 	unsigned long flags;
19494e3b0468SAntoine Tenart 	u32 val;
19504e3b0468SAntoine Tenart 
19514e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
19524e3b0468SAntoine Tenart 
19534e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19544e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19554e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
19564e3b0468SAntoine Tenart 
19574e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19584e3b0468SAntoine Tenart 
19594e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
19604e3b0468SAntoine Tenart 			 TOD_ACC_PIN);
19614e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
19624e3b0468SAntoine Tenart 			 TOD_ACC_PIN);
19634e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
19644e3b0468SAntoine Tenart 
19654e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19664e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19674e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
19684e3b0468SAntoine Tenart 
19694e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19704e3b0468SAntoine Tenart 
19714e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19724e3b0468SAntoine Tenart 	return 0;
19734e3b0468SAntoine Tenart }
19744e3b0468SAntoine Tenart 
19754e3b0468SAntoine Tenart static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
19764e3b0468SAntoine Tenart {
19774e3b0468SAntoine Tenart 	if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
19784e3b0468SAntoine Tenart 		struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
19794e3b0468SAntoine Tenart 		unsigned long flags;
19804e3b0468SAntoine Tenart 		u32 val;
19814e3b0468SAntoine Tenart 
19824e3b0468SAntoine Tenart 		spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
19834e3b0468SAntoine Tenart 
19844e3b0468SAntoine Tenart 		val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19854e3b0468SAntoine Tenart 		val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19864e3b0468SAntoine Tenart 		val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
19874e3b0468SAntoine Tenart 
19884e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19894e3b0468SAntoine Tenart 
19904e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
19914e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
19924e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
19934e3b0468SAntoine Tenart 
19944e3b0468SAntoine Tenart 		val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
19954e3b0468SAntoine Tenart 		val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
19964e3b0468SAntoine Tenart 		val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
19974e3b0468SAntoine Tenart 
19984e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19994e3b0468SAntoine Tenart 
20004e3b0468SAntoine Tenart 		spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
20014e3b0468SAntoine Tenart 	} else {
20024e3b0468SAntoine Tenart 		/* Fall back using ocelot_ptp_settime64 which is not exact. */
20034e3b0468SAntoine Tenart 		struct timespec64 ts;
20044e3b0468SAntoine Tenart 		u64 now;
20054e3b0468SAntoine Tenart 
20064e3b0468SAntoine Tenart 		ocelot_ptp_gettime64(ptp, &ts);
20074e3b0468SAntoine Tenart 
20084e3b0468SAntoine Tenart 		now = ktime_to_ns(timespec64_to_ktime(ts));
20094e3b0468SAntoine Tenart 		ts = ns_to_timespec64(now + delta);
20104e3b0468SAntoine Tenart 
20114e3b0468SAntoine Tenart 		ocelot_ptp_settime64(ptp, &ts);
20124e3b0468SAntoine Tenart 	}
20134e3b0468SAntoine Tenart 	return 0;
20144e3b0468SAntoine Tenart }
20154e3b0468SAntoine Tenart 
20164e3b0468SAntoine Tenart static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
20174e3b0468SAntoine Tenart {
20184e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
20194e3b0468SAntoine Tenart 	u32 unit = 0, direction = 0;
20204e3b0468SAntoine Tenart 	unsigned long flags;
20214e3b0468SAntoine Tenart 	u64 adj = 0;
20224e3b0468SAntoine Tenart 
20234e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
20244e3b0468SAntoine Tenart 
20254e3b0468SAntoine Tenart 	if (!scaled_ppm)
20264e3b0468SAntoine Tenart 		goto disable_adj;
20274e3b0468SAntoine Tenart 
20284e3b0468SAntoine Tenart 	if (scaled_ppm < 0) {
20294e3b0468SAntoine Tenart 		direction = PTP_CFG_CLK_ADJ_CFG_DIR;
20304e3b0468SAntoine Tenart 		scaled_ppm = -scaled_ppm;
20314e3b0468SAntoine Tenart 	}
20324e3b0468SAntoine Tenart 
20334e3b0468SAntoine Tenart 	adj = PSEC_PER_SEC << 16;
20344e3b0468SAntoine Tenart 	do_div(adj, scaled_ppm);
20354e3b0468SAntoine Tenart 	do_div(adj, 1000);
20364e3b0468SAntoine Tenart 
20374e3b0468SAntoine Tenart 	/* If the adjustment value is too large, use ns instead */
20384e3b0468SAntoine Tenart 	if (adj >= (1L << 30)) {
20394e3b0468SAntoine Tenart 		unit = PTP_CFG_CLK_ADJ_FREQ_NS;
20404e3b0468SAntoine Tenart 		do_div(adj, 1000);
20414e3b0468SAntoine Tenart 	}
20424e3b0468SAntoine Tenart 
20434e3b0468SAntoine Tenart 	/* Still too big */
20444e3b0468SAntoine Tenart 	if (adj >= (1L << 30))
20454e3b0468SAntoine Tenart 		goto disable_adj;
20464e3b0468SAntoine Tenart 
20474e3b0468SAntoine Tenart 	ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
20484e3b0468SAntoine Tenart 	ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
20494e3b0468SAntoine Tenart 		     PTP_CLK_CFG_ADJ_CFG);
20504e3b0468SAntoine Tenart 
20514e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
20524e3b0468SAntoine Tenart 	return 0;
20534e3b0468SAntoine Tenart 
20544e3b0468SAntoine Tenart disable_adj:
20554e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
20564e3b0468SAntoine Tenart 
20574e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
20584e3b0468SAntoine Tenart 	return 0;
20594e3b0468SAntoine Tenart }
20604e3b0468SAntoine Tenart 
20614e3b0468SAntoine Tenart static struct ptp_clock_info ocelot_ptp_clock_info = {
20624e3b0468SAntoine Tenart 	.owner		= THIS_MODULE,
20634e3b0468SAntoine Tenart 	.name		= "ocelot ptp",
20644e3b0468SAntoine Tenart 	.max_adj	= 0x7fffffff,
20654e3b0468SAntoine Tenart 	.n_alarm	= 0,
20664e3b0468SAntoine Tenart 	.n_ext_ts	= 0,
20674e3b0468SAntoine Tenart 	.n_per_out	= 0,
20684e3b0468SAntoine Tenart 	.n_pins		= 0,
20694e3b0468SAntoine Tenart 	.pps		= 0,
20704e3b0468SAntoine Tenart 	.gettime64	= ocelot_ptp_gettime64,
20714e3b0468SAntoine Tenart 	.settime64	= ocelot_ptp_settime64,
20724e3b0468SAntoine Tenart 	.adjtime	= ocelot_ptp_adjtime,
20734e3b0468SAntoine Tenart 	.adjfine	= ocelot_ptp_adjfine,
20744e3b0468SAntoine Tenart };
20754e3b0468SAntoine Tenart 
20764e3b0468SAntoine Tenart static int ocelot_init_timestamp(struct ocelot *ocelot)
20774e3b0468SAntoine Tenart {
20784e3b0468SAntoine Tenart 	ocelot->ptp_info = ocelot_ptp_clock_info;
20794e3b0468SAntoine Tenart 	ocelot->ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
20804e3b0468SAntoine Tenart 	if (IS_ERR(ocelot->ptp_clock))
20814e3b0468SAntoine Tenart 		return PTR_ERR(ocelot->ptp_clock);
20824e3b0468SAntoine Tenart 	/* Check if PHC support is missing at the configuration level */
20834e3b0468SAntoine Tenart 	if (!ocelot->ptp_clock)
20844e3b0468SAntoine Tenart 		return 0;
20854e3b0468SAntoine Tenart 
20864e3b0468SAntoine Tenart 	ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
20874e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
20884e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
20894e3b0468SAntoine Tenart 
20904e3b0468SAntoine Tenart 	ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
20914e3b0468SAntoine Tenart 
20924e3b0468SAntoine Tenart 	/* There is no device reconfiguration, PTP Rx stamping is always
20934e3b0468SAntoine Tenart 	 * enabled.
20944e3b0468SAntoine Tenart 	 */
20954e3b0468SAntoine Tenart 	ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
20964e3b0468SAntoine Tenart 
20974e3b0468SAntoine Tenart 	return 0;
20984e3b0468SAntoine Tenart }
20994e3b0468SAntoine Tenart 
2100fa914e9cSVladimir Oltean static void ocelot_port_set_mtu(struct ocelot *ocelot, int port, size_t mtu)
210131350d7fSVladimir Oltean {
210231350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
21035bc9d2e6SVladimir Oltean 	int atop_wm;
210431350d7fSVladimir Oltean 
2105fa914e9cSVladimir Oltean 	ocelot_port_writel(ocelot_port, mtu, DEV_MAC_MAXLEN_CFG);
2106fa914e9cSVladimir Oltean 
2107fa914e9cSVladimir Oltean 	/* Set Pause WM hysteresis
2108fa914e9cSVladimir Oltean 	 * 152 = 6 * mtu / OCELOT_BUFFER_CELL_SZ
2109fa914e9cSVladimir Oltean 	 * 101 = 4 * mtu / OCELOT_BUFFER_CELL_SZ
2110fa914e9cSVladimir Oltean 	 */
2111fa914e9cSVladimir Oltean 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
2112fa914e9cSVladimir Oltean 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
2113fa914e9cSVladimir Oltean 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, port);
2114fa914e9cSVladimir Oltean 
2115fa914e9cSVladimir Oltean 	/* Tail dropping watermark */
2116fa914e9cSVladimir Oltean 	atop_wm = (ocelot->shared_queue_sz - 9 * mtu) / OCELOT_BUFFER_CELL_SZ;
2117fa914e9cSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * mtu),
2118fa914e9cSVladimir Oltean 			 SYS_ATOP, port);
2119fa914e9cSVladimir Oltean 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
2120fa914e9cSVladimir Oltean }
2121fa914e9cSVladimir Oltean 
2122fa914e9cSVladimir Oltean static void ocelot_init_port(struct ocelot *ocelot, int port)
2123fa914e9cSVladimir Oltean {
2124fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2125fa914e9cSVladimir Oltean 
212631350d7fSVladimir Oltean 	INIT_LIST_HEAD(&ocelot_port->skbs);
212731350d7fSVladimir Oltean 
212831350d7fSVladimir Oltean 	/* Basic L2 initialization */
212931350d7fSVladimir Oltean 
21305bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
21315bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
21325bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
21335bc9d2e6SVladimir Oltean 	 */
21345bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
21355bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
21365bc9d2e6SVladimir Oltean 
21375bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
21385bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
21395bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
21405bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
21415bc9d2e6SVladimir Oltean 	mdelay(1);
21425bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
21435bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
21445bc9d2e6SVladimir Oltean 
21455bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2146fa914e9cSVladimir Oltean 	ocelot_port_set_mtu(ocelot, port, VLAN_ETH_FRAME_LEN);
21475bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
21485bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
21495bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
21505bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
21515bc9d2e6SVladimir Oltean 
21525bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
21535bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
21545bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
21555bc9d2e6SVladimir Oltean 
215631350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
215731350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
215831350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
215931350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
216031350d7fSVladimir Oltean 
216131350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
216231350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
216331350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
216431350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
216531350d7fSVladimir Oltean 
216631350d7fSVladimir Oltean 	/* Enable vcap lookups */
216731350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
216831350d7fSVladimir Oltean }
216931350d7fSVladimir Oltean 
2170a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2171a556c76aSAlexandre Belloni 		      void __iomem *regs,
2172a556c76aSAlexandre Belloni 		      struct phy_device *phy)
2173a556c76aSAlexandre Belloni {
2174004d44f6SVladimir Oltean 	struct ocelot_port_private *priv;
2175a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port;
2176a556c76aSAlexandre Belloni 	struct net_device *dev;
2177a556c76aSAlexandre Belloni 	int err;
2178a556c76aSAlexandre Belloni 
2179004d44f6SVladimir Oltean 	dev = alloc_etherdev(sizeof(struct ocelot_port_private));
2180a556c76aSAlexandre Belloni 	if (!dev)
2181a556c76aSAlexandre Belloni 		return -ENOMEM;
2182a556c76aSAlexandre Belloni 	SET_NETDEV_DEV(dev, ocelot->dev);
2183004d44f6SVladimir Oltean 	priv = netdev_priv(dev);
2184004d44f6SVladimir Oltean 	priv->dev = dev;
2185004d44f6SVladimir Oltean 	priv->phy = phy;
2186004d44f6SVladimir Oltean 	priv->chip_port = port;
2187004d44f6SVladimir Oltean 	ocelot_port = &priv->port;
2188a556c76aSAlexandre Belloni 	ocelot_port->ocelot = ocelot;
2189a556c76aSAlexandre Belloni 	ocelot_port->regs = regs;
2190a556c76aSAlexandre Belloni 	ocelot->ports[port] = ocelot_port;
2191a556c76aSAlexandre Belloni 
2192a556c76aSAlexandre Belloni 	dev->netdev_ops = &ocelot_port_netdev_ops;
2193a556c76aSAlexandre Belloni 	dev->ethtool_ops = &ocelot_ethtool_ops;
2194a556c76aSAlexandre Belloni 
21952c1d029aSJoergen Andreasen 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
21962c1d029aSJoergen Andreasen 		NETIF_F_HW_TC;
21972c1d029aSJoergen Andreasen 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
21987142529fSAntoine Tenart 
2199a556c76aSAlexandre Belloni 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2200a556c76aSAlexandre Belloni 	dev->dev_addr[ETH_ALEN - 1] += port;
2201a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2202a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
2203a556c76aSAlexandre Belloni 
220431350d7fSVladimir Oltean 	ocelot_init_port(ocelot, port);
22054e3b0468SAntoine Tenart 
2206a556c76aSAlexandre Belloni 	err = register_netdev(dev);
2207a556c76aSAlexandre Belloni 	if (err) {
2208a556c76aSAlexandre Belloni 		dev_err(ocelot->dev, "register_netdev failed\n");
220931350d7fSVladimir Oltean 		free_netdev(dev);
2210a556c76aSAlexandre Belloni 	}
2211a556c76aSAlexandre Belloni 
2212a556c76aSAlexandre Belloni 	return err;
2213a556c76aSAlexandre Belloni }
2214a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port);
2215a556c76aSAlexandre Belloni 
221621468199SVladimir Oltean void ocelot_set_cpu_port(struct ocelot *ocelot, int cpu,
221721468199SVladimir Oltean 			 enum ocelot_tag_prefix injection,
221821468199SVladimir Oltean 			 enum ocelot_tag_prefix extraction)
221921468199SVladimir Oltean {
222021468199SVladimir Oltean 	/* Configure and enable the CPU port. */
222121468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
222221468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
222321468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
222421468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
222521468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
222621468199SVladimir Oltean 
222721468199SVladimir Oltean 	/* If the CPU port is a physical port, set up the port in Node
222821468199SVladimir Oltean 	 * Processor Interface (NPI) mode. This is the mode through which
222921468199SVladimir Oltean 	 * frames can be injected from and extracted to an external CPU.
223021468199SVladimir Oltean 	 * Only one port can be an NPI at the same time.
223121468199SVladimir Oltean 	 */
223221468199SVladimir Oltean 	if (cpu < ocelot->num_phys_ports) {
223321468199SVladimir Oltean 		ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
223421468199SVladimir Oltean 			     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(cpu),
223521468199SVladimir Oltean 			     QSYS_EXT_CPU_CFG);
223621468199SVladimir Oltean 	}
223721468199SVladimir Oltean 
223821468199SVladimir Oltean 	/* CPU port Injection/Extraction configuration */
223921468199SVladimir Oltean 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
224021468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
224121468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
224221468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, cpu);
224321468199SVladimir Oltean 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
224421468199SVladimir Oltean 			 SYS_PORT_MODE_INCL_INJ_HDR(injection),
224521468199SVladimir Oltean 			 SYS_PORT_MODE, cpu);
224621468199SVladimir Oltean 
224721468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
224821468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
224921468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
225021468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
225121468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
225221468199SVladimir Oltean 
225321468199SVladimir Oltean 	ocelot->cpu = cpu;
225421468199SVladimir Oltean }
225521468199SVladimir Oltean EXPORT_SYMBOL(ocelot_set_cpu_port);
225621468199SVladimir Oltean 
2257a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2258a556c76aSAlexandre Belloni {
2259a556c76aSAlexandre Belloni 	char queue_name[32];
226021468199SVladimir Oltean 	int i, ret;
226121468199SVladimir Oltean 	u32 port;
2262a556c76aSAlexandre Belloni 
2263dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2264dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
2265dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
2266dc96ee37SAlexandre Belloni 		return -ENOMEM;
2267dc96ee37SAlexandre Belloni 
2268a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2269a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2270a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2271a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2272a556c76aSAlexandre Belloni 		return -ENOMEM;
2273a556c76aSAlexandre Belloni 
2274a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
22754e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
22764e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
2277a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2278a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2279a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2280a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2281a556c76aSAlexandre Belloni 		return -ENOMEM;
2282a556c76aSAlexandre Belloni 
22832b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2284a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2285a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2286b5962294SHoratiu Vultur 	ocelot_ace_init(ocelot);
2287a556c76aSAlexandre Belloni 
2288a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2289a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2290a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2291a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2292a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2293a556c76aSAlexandre Belloni 	}
2294a556c76aSAlexandre Belloni 
2295a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2296a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2297a556c76aSAlexandre Belloni 
2298a556c76aSAlexandre Belloni 	/* Aggregation mode */
2299a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2300a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2301a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2302a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2303a556c76aSAlexandre Belloni 
2304a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2305a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2306a556c76aSAlexandre Belloni 	 */
2307a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2308a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2309a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2310a556c76aSAlexandre Belloni 
2311a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2312a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2313a556c76aSAlexandre Belloni 
2314a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2315a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2316a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2317a556c76aSAlexandre Belloni 
2318a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2319a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2320a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2321a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2322a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
2323a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2324a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2325a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2326a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2327a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2328a556c76aSAlexandre Belloni 
2329a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2330a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2331a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2332a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2333a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2334a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2335a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2336a556c76aSAlexandre Belloni 				 port);
2337a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2338a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2339a556c76aSAlexandre Belloni 	}
2340a556c76aSAlexandre Belloni 
2341a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
2342a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2343a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2344a556c76aSAlexandre Belloni 
2345a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2346a556c76aSAlexandre Belloni 	}
2347a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
2348a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2349a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
2350a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2351a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2352a556c76aSAlexandre Belloni 
2353a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2354a556c76aSAlexandre Belloni 	 * registers endianness.
2355a556c76aSAlexandre Belloni 	 */
2356a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2357a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2358a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2359a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2360a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2361a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2362a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2363a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2364a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2365a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2366a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2367a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2368a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2369a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2370a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2371a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2372a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2373a556c76aSAlexandre Belloni 
23741e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2375a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2376a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
23774e3b0468SAntoine Tenart 
23784e3b0468SAntoine Tenart 	if (ocelot->ptp) {
23794e3b0468SAntoine Tenart 		ret = ocelot_init_timestamp(ocelot);
23804e3b0468SAntoine Tenart 		if (ret) {
23814e3b0468SAntoine Tenart 			dev_err(ocelot->dev,
23824e3b0468SAntoine Tenart 				"Timestamp initialization failed\n");
23834e3b0468SAntoine Tenart 			return ret;
23844e3b0468SAntoine Tenart 		}
23854e3b0468SAntoine Tenart 	}
23864e3b0468SAntoine Tenart 
2387a556c76aSAlexandre Belloni 	return 0;
2388a556c76aSAlexandre Belloni }
2389a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2390a556c76aSAlexandre Belloni 
2391a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2392a556c76aSAlexandre Belloni {
23934e3b0468SAntoine Tenart 	struct list_head *pos, *tmp;
23944e3b0468SAntoine Tenart 	struct ocelot_port *port;
23954e3b0468SAntoine Tenart 	struct ocelot_skb *entry;
23964e3b0468SAntoine Tenart 	int i;
23974e3b0468SAntoine Tenart 
2398c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2399a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2400a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2401b5962294SHoratiu Vultur 	ocelot_ace_deinit();
24024e3b0468SAntoine Tenart 
24034e3b0468SAntoine Tenart 	for (i = 0; i < ocelot->num_phys_ports; i++) {
24044e3b0468SAntoine Tenart 		port = ocelot->ports[i];
24054e3b0468SAntoine Tenart 
24064e3b0468SAntoine Tenart 		list_for_each_safe(pos, tmp, &port->skbs) {
24074e3b0468SAntoine Tenart 			entry = list_entry(pos, struct ocelot_skb, head);
24084e3b0468SAntoine Tenart 
24094e3b0468SAntoine Tenart 			list_del(pos);
24104e3b0468SAntoine Tenart 			dev_kfree_skb_any(entry->skb);
24114e3b0468SAntoine Tenart 			kfree(entry);
24124e3b0468SAntoine Tenart 		}
24134e3b0468SAntoine Tenart 	}
2414a556c76aSAlexandre Belloni }
2415a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2416a556c76aSAlexandre Belloni 
2417a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2418