xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 1c44ce560b4de639f237b458be1729489ff44d0a)
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>
24a556c76aSAlexandre Belloni 
25a556c76aSAlexandre Belloni #include "ocelot.h"
26b5962294SHoratiu Vultur #include "ocelot_ace.h"
27a556c76aSAlexandre Belloni 
28639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
29639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
30639c1b26SSteen Hegelund 
31a556c76aSAlexandre Belloni /* MAC table entry types.
32a556c76aSAlexandre Belloni  * ENTRYTYPE_NORMAL is subject to aging.
33a556c76aSAlexandre Belloni  * ENTRYTYPE_LOCKED is not subject to aging.
34a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
35a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
36a556c76aSAlexandre Belloni  */
37a556c76aSAlexandre Belloni enum macaccess_entry_type {
38a556c76aSAlexandre Belloni 	ENTRYTYPE_NORMAL = 0,
39a556c76aSAlexandre Belloni 	ENTRYTYPE_LOCKED,
40a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv4,
41a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv6,
42a556c76aSAlexandre Belloni };
43a556c76aSAlexandre Belloni 
44a556c76aSAlexandre Belloni struct ocelot_mact_entry {
45a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
46a556c76aSAlexandre Belloni 	u16 vid;
47a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
48a556c76aSAlexandre Belloni };
49a556c76aSAlexandre Belloni 
50639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
51639c1b26SSteen Hegelund {
52639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
53639c1b26SSteen Hegelund }
54639c1b26SSteen Hegelund 
55a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
56a556c76aSAlexandre Belloni {
57639c1b26SSteen Hegelund 	u32 val;
58a556c76aSAlexandre Belloni 
59639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
60639c1b26SSteen Hegelund 		ocelot, val,
61639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
62639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
63639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
64a556c76aSAlexandre Belloni }
65a556c76aSAlexandre Belloni 
66a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
67a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
68a556c76aSAlexandre Belloni 			       unsigned int vid)
69a556c76aSAlexandre Belloni {
70a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
71a556c76aSAlexandre Belloni 
72a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
73a556c76aSAlexandre Belloni 	 * understood by the hardware.
74a556c76aSAlexandre Belloni 	 */
75a556c76aSAlexandre Belloni 	mach |= vid    << 16;
76a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
77a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
78a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
79a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
80a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
81a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
82a556c76aSAlexandre Belloni 
83a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
84a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
85a556c76aSAlexandre Belloni 
86a556c76aSAlexandre Belloni }
87a556c76aSAlexandre Belloni 
88a556c76aSAlexandre Belloni static int ocelot_mact_learn(struct ocelot *ocelot, int port,
89a556c76aSAlexandre Belloni 			     const unsigned char mac[ETH_ALEN],
90a556c76aSAlexandre Belloni 			     unsigned int vid,
91a556c76aSAlexandre Belloni 			     enum macaccess_entry_type type)
92a556c76aSAlexandre Belloni {
93a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
94a556c76aSAlexandre Belloni 
95a556c76aSAlexandre Belloni 	/* Issue a write command */
96a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
97a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
98a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
99a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
100a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
101a556c76aSAlexandre Belloni 
102a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
103a556c76aSAlexandre Belloni }
104a556c76aSAlexandre Belloni 
105a556c76aSAlexandre Belloni static int ocelot_mact_forget(struct ocelot *ocelot,
106a556c76aSAlexandre Belloni 			      const unsigned char mac[ETH_ALEN],
107a556c76aSAlexandre Belloni 			      unsigned int vid)
108a556c76aSAlexandre Belloni {
109a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
110a556c76aSAlexandre Belloni 
111a556c76aSAlexandre Belloni 	/* Issue a forget command */
112a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
113a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
114a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
115a556c76aSAlexandre Belloni 
116a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
117a556c76aSAlexandre Belloni }
118a556c76aSAlexandre Belloni 
119a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
120a556c76aSAlexandre Belloni {
121a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
122a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
123a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
124a556c76aSAlexandre Belloni 	 */
125a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
126a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
127a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
128a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
129a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
130a556c76aSAlexandre Belloni 
131a556c76aSAlexandre Belloni 	/* Clear the MAC table */
132a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
133a556c76aSAlexandre Belloni }
134a556c76aSAlexandre Belloni 
135b5962294SHoratiu Vultur static void ocelot_vcap_enable(struct ocelot *ocelot, struct ocelot_port *port)
136b5962294SHoratiu Vultur {
137b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
138b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
139b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG, port->chip_port);
140b5962294SHoratiu Vultur }
141b5962294SHoratiu Vultur 
142639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
143639c1b26SSteen Hegelund {
144639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
145639c1b26SSteen Hegelund }
146639c1b26SSteen Hegelund 
147a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
148a556c76aSAlexandre Belloni {
149639c1b26SSteen Hegelund 	u32 val;
150a556c76aSAlexandre Belloni 
151639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
152639c1b26SSteen Hegelund 		ocelot,
153639c1b26SSteen Hegelund 		val,
154639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
155639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
156639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
157a556c76aSAlexandre Belloni }
158a556c76aSAlexandre Belloni 
1597142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1607142529fSAntoine Tenart {
1617142529fSAntoine Tenart 	/* Select the VID to configure */
1627142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1637142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1647142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1657142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1667142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1677142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1687142529fSAntoine Tenart 
1697142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1707142529fSAntoine Tenart }
1717142529fSAntoine Tenart 
1727142529fSAntoine Tenart static void ocelot_vlan_mode(struct ocelot_port *port,
1737142529fSAntoine Tenart 			     netdev_features_t features)
1747142529fSAntoine Tenart {
1757142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
1767142529fSAntoine Tenart 	u8 p = port->chip_port;
1777142529fSAntoine Tenart 	u32 val;
1787142529fSAntoine Tenart 
1797142529fSAntoine Tenart 	/* Filtering */
1807142529fSAntoine Tenart 	val = ocelot_read(ocelot, ANA_VLANMASK);
1817142529fSAntoine Tenart 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1827142529fSAntoine Tenart 		val |= BIT(p);
1837142529fSAntoine Tenart 	else
1847142529fSAntoine Tenart 		val &= ~BIT(p);
1857142529fSAntoine Tenart 	ocelot_write(ocelot, val, ANA_VLANMASK);
1867142529fSAntoine Tenart }
1877142529fSAntoine Tenart 
1887142529fSAntoine Tenart static void ocelot_vlan_port_apply(struct ocelot *ocelot,
1897142529fSAntoine Tenart 				   struct ocelot_port *port)
1907142529fSAntoine Tenart {
1917142529fSAntoine Tenart 	u32 val;
1927142529fSAntoine Tenart 
1937142529fSAntoine Tenart 	/* Ingress clasification (ANA_PORT_VLAN_CFG) */
1947142529fSAntoine Tenart 	/* Default vlan to clasify for untagged frames (may be zero) */
1957142529fSAntoine Tenart 	val = ANA_PORT_VLAN_CFG_VLAN_VID(port->pvid);
1967142529fSAntoine Tenart 	if (port->vlan_aware)
1977142529fSAntoine Tenart 		val |= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1987142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
1997142529fSAntoine Tenart 
2007142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2017142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_VID_M |
2027142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2037142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
2047142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG, port->chip_port);
2057142529fSAntoine Tenart 
2067142529fSAntoine Tenart 	/* Drop frames with multicast source address */
2077142529fSAntoine Tenart 	val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
2087142529fSAntoine Tenart 	if (port->vlan_aware && !port->vid)
2097142529fSAntoine Tenart 		/* If port is vlan-aware and tagged, drop untagged and priority
2107142529fSAntoine Tenart 		 * tagged frames.
2117142529fSAntoine Tenart 		 */
2127142529fSAntoine Tenart 		val |= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
2137142529fSAntoine Tenart 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
2147142529fSAntoine Tenart 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
2157142529fSAntoine Tenart 	ocelot_write_gix(ocelot, val, ANA_PORT_DROP_CFG, port->chip_port);
2167142529fSAntoine Tenart 
2177142529fSAntoine Tenart 	/* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q. */
2187142529fSAntoine Tenart 	val = REW_TAG_CFG_TAG_TPID_CFG(0);
2197142529fSAntoine Tenart 
2207142529fSAntoine Tenart 	if (port->vlan_aware) {
2217142529fSAntoine Tenart 		if (port->vid)
2227142529fSAntoine Tenart 			/* Tag all frames except when VID == DEFAULT_VLAN */
2237142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(1);
2247142529fSAntoine Tenart 		else
2257142529fSAntoine Tenart 			/* Tag all frames */
2267142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(3);
2277142529fSAntoine Tenart 	}
2287142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2297142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_TPID_CFG_M |
2307142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_CFG_M,
2317142529fSAntoine Tenart 		       REW_TAG_CFG, port->chip_port);
2327142529fSAntoine Tenart 
2337142529fSAntoine Tenart 	/* Set default VLAN and tag type to 8021Q. */
2347142529fSAntoine Tenart 	val = REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q) |
2357142529fSAntoine Tenart 	      REW_PORT_VLAN_CFG_PORT_VID(port->vid);
2367142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2377142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_TPID_M |
2387142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
2397142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG, port->chip_port);
2407142529fSAntoine Tenart }
2417142529fSAntoine Tenart 
2427142529fSAntoine Tenart static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
2437142529fSAntoine Tenart 			       bool untagged)
2447142529fSAntoine Tenart {
2457142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
2467142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
2477142529fSAntoine Tenart 	int ret;
2487142529fSAntoine Tenart 
2497142529fSAntoine Tenart 	/* Add the port MAC address to with the right VLAN information */
2507142529fSAntoine Tenart 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
2517142529fSAntoine Tenart 			  ENTRYTYPE_LOCKED);
2527142529fSAntoine Tenart 
2537142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
2547142529fSAntoine Tenart 	ocelot->vlan_mask[vid] |= BIT(port->chip_port);
2557142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2567142529fSAntoine Tenart 	if (ret)
2577142529fSAntoine Tenart 		return ret;
2587142529fSAntoine Tenart 
2597142529fSAntoine Tenart 	/* Default ingress vlan classification */
2607142529fSAntoine Tenart 	if (pvid)
2617142529fSAntoine Tenart 		port->pvid = vid;
2627142529fSAntoine Tenart 
2637142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
2647142529fSAntoine Tenart 	if (untagged)
2657142529fSAntoine Tenart 		port->vid = vid;
2667142529fSAntoine Tenart 
2677142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
2687142529fSAntoine Tenart 
2697142529fSAntoine Tenart 	return 0;
2707142529fSAntoine Tenart }
2717142529fSAntoine Tenart 
2727142529fSAntoine Tenart static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
2737142529fSAntoine Tenart {
2747142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
2757142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
2767142529fSAntoine Tenart 	int ret;
2777142529fSAntoine Tenart 
2787142529fSAntoine Tenart 	/* 8021q removes VID 0 on module unload for all interfaces
2797142529fSAntoine Tenart 	 * with VLAN filtering feature. We need to keep it to receive
2807142529fSAntoine Tenart 	 * untagged traffic.
2817142529fSAntoine Tenart 	 */
2827142529fSAntoine Tenart 	if (vid == 0)
2837142529fSAntoine Tenart 		return 0;
2847142529fSAntoine Tenart 
2857142529fSAntoine Tenart 	/* Del the port MAC address to with the right VLAN information */
2867142529fSAntoine Tenart 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
2877142529fSAntoine Tenart 
2887142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
2897142529fSAntoine Tenart 	ocelot->vlan_mask[vid] &= ~BIT(port->chip_port);
2907142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2917142529fSAntoine Tenart 	if (ret)
2927142529fSAntoine Tenart 		return ret;
2937142529fSAntoine Tenart 
2947142529fSAntoine Tenart 	/* Ingress */
2957142529fSAntoine Tenart 	if (port->pvid == vid)
2967142529fSAntoine Tenart 		port->pvid = 0;
2977142529fSAntoine Tenart 
2987142529fSAntoine Tenart 	/* Egress */
2997142529fSAntoine Tenart 	if (port->vid == vid)
3007142529fSAntoine Tenart 		port->vid = 0;
3017142529fSAntoine Tenart 
3027142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
3037142529fSAntoine Tenart 
3047142529fSAntoine Tenart 	return 0;
3057142529fSAntoine Tenart }
3067142529fSAntoine Tenart 
307a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
308a556c76aSAlexandre Belloni {
3097142529fSAntoine Tenart 	u16 port, vid;
3107142529fSAntoine Tenart 
311a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
312a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
313a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
314a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3157142529fSAntoine Tenart 
3167142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3177142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3187142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3197142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3207142529fSAntoine Tenart 	}
3217142529fSAntoine Tenart 
3227142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3237142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3247142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3257142529fSAntoine Tenart 	 */
3267142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3277142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3287142529fSAntoine Tenart 
3297142529fSAntoine Tenart 	/* Configure the CPU port to be VLAN aware */
3307142529fSAntoine Tenart 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
3317142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
3327142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
3337142529fSAntoine Tenart 			 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
3347142529fSAntoine Tenart 
3357142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3367142529fSAntoine Tenart 	 * default.
3377142529fSAntoine Tenart 	 */
3387142529fSAntoine Tenart 	ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
3397142529fSAntoine Tenart 
3407142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3417142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3427142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3437142529fSAntoine Tenart 	}
344a556c76aSAlexandre Belloni }
345a556c76aSAlexandre Belloni 
346a556c76aSAlexandre Belloni /* Watermark encode
347a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
348a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
349a556c76aSAlexandre Belloni  */
350a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
351a556c76aSAlexandre Belloni {
352a556c76aSAlexandre Belloni 	if (value >= BIT(8))
353a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
354a556c76aSAlexandre Belloni 
355a556c76aSAlexandre Belloni 	return value;
356a556c76aSAlexandre Belloni }
357a556c76aSAlexandre Belloni 
358a556c76aSAlexandre Belloni static void ocelot_port_adjust_link(struct net_device *dev)
359a556c76aSAlexandre Belloni {
360a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
361a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
362a556c76aSAlexandre Belloni 	u8 p = port->chip_port;
363a556c76aSAlexandre Belloni 	int speed, atop_wm, mode = 0;
364a556c76aSAlexandre Belloni 
365a556c76aSAlexandre Belloni 	switch (dev->phydev->speed) {
366a556c76aSAlexandre Belloni 	case SPEED_10:
367a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
368a556c76aSAlexandre Belloni 		break;
369a556c76aSAlexandre Belloni 	case SPEED_100:
370a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
371a556c76aSAlexandre Belloni 		break;
372a556c76aSAlexandre Belloni 	case SPEED_1000:
373a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
374a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
375a556c76aSAlexandre Belloni 		break;
376a556c76aSAlexandre Belloni 	case SPEED_2500:
377a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
378a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
379a556c76aSAlexandre Belloni 		break;
380a556c76aSAlexandre Belloni 	default:
381a556c76aSAlexandre Belloni 		netdev_err(dev, "Unsupported PHY speed: %d\n",
382a556c76aSAlexandre Belloni 			   dev->phydev->speed);
383a556c76aSAlexandre Belloni 		return;
384a556c76aSAlexandre Belloni 	}
385a556c76aSAlexandre Belloni 
386a556c76aSAlexandre Belloni 	phy_print_status(dev->phydev);
387a556c76aSAlexandre Belloni 
388a556c76aSAlexandre Belloni 	if (!dev->phydev->link)
389a556c76aSAlexandre Belloni 		return;
390a556c76aSAlexandre Belloni 
391a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
392a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
393a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
394a556c76aSAlexandre Belloni 
395a556c76aSAlexandre Belloni 	/* Set MAC IFG Gaps
396a556c76aSAlexandre Belloni 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
397a556c76aSAlexandre Belloni 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
398a556c76aSAlexandre Belloni 	 */
399a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
400a556c76aSAlexandre Belloni 
401a556c76aSAlexandre Belloni 	/* Load seed (0) and set MAC HDX late collision  */
402a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
403a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG_SEED_LOAD,
404a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
405a556c76aSAlexandre Belloni 	mdelay(1);
406a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
407a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
408a556c76aSAlexandre Belloni 
409a556c76aSAlexandre Belloni 	/* Disable HDX fast control */
410a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
411a556c76aSAlexandre Belloni 
412a556c76aSAlexandre Belloni 	/* SGMII only for now */
413a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
414a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
415a556c76aSAlexandre Belloni 
416a556c76aSAlexandre Belloni 	/* Enable PCS */
417a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
418a556c76aSAlexandre Belloni 
419a556c76aSAlexandre Belloni 	/* No aneg on SGMII */
420a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
421a556c76aSAlexandre Belloni 
422a556c76aSAlexandre Belloni 	/* No loopback */
423a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_LB_CFG);
424a556c76aSAlexandre Belloni 
425a556c76aSAlexandre Belloni 	/* Set Max Length and maximum tags allowed */
426a556c76aSAlexandre Belloni 	ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
427a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
428a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
429a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
430a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG);
431a556c76aSAlexandre Belloni 
432a556c76aSAlexandre Belloni 	/* Enable MAC module */
433a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
434a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
435a556c76aSAlexandre Belloni 
436a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
437a556c76aSAlexandre Belloni 	 * reset */
438a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
439a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
440a556c76aSAlexandre Belloni 
441a556c76aSAlexandre Belloni 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
442a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
443a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
444a556c76aSAlexandre Belloni 
445a556c76aSAlexandre Belloni 	/* No PFC */
446a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
447a556c76aSAlexandre Belloni 			 ANA_PFC_PFC_CFG, p);
448a556c76aSAlexandre Belloni 
449a556c76aSAlexandre Belloni 	/* Set Pause WM hysteresis
450a556c76aSAlexandre Belloni 	 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
451a556c76aSAlexandre Belloni 	 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
452a556c76aSAlexandre Belloni 	 */
453a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
454a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
455a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
456a556c76aSAlexandre Belloni 
457a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
458a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
459a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
460a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
461a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, p);
462a556c76aSAlexandre Belloni 
463a556c76aSAlexandre Belloni 	/* Flow control */
464a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
465a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
466a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
467a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
468a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
469a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG, p);
470a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
471a556c76aSAlexandre Belloni 
472a556c76aSAlexandre Belloni 	/* Tail dropping watermark */
473a556c76aSAlexandre Belloni 	atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
474a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
475a556c76aSAlexandre Belloni 			 SYS_ATOP, p);
476a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
477a556c76aSAlexandre Belloni }
478a556c76aSAlexandre Belloni 
479a556c76aSAlexandre Belloni static int ocelot_port_open(struct net_device *dev)
480a556c76aSAlexandre Belloni {
481a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
482a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
483a556c76aSAlexandre Belloni 	int err;
484a556c76aSAlexandre Belloni 
485a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
486a556c76aSAlexandre Belloni 	 * MAC addresses.
487a556c76aSAlexandre Belloni 	 */
488a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
489a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
490a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
491a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, port->chip_port);
492a556c76aSAlexandre Belloni 
49371e32a20SQuentin Schulz 	if (port->serdes) {
494c8fe6d7fSGrygorii Strashko 		err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
495c8fe6d7fSGrygorii Strashko 				       port->phy_mode);
49671e32a20SQuentin Schulz 		if (err) {
49771e32a20SQuentin Schulz 			netdev_err(dev, "Could not set mode of SerDes\n");
49871e32a20SQuentin Schulz 			return err;
49971e32a20SQuentin Schulz 		}
50071e32a20SQuentin Schulz 	}
50171e32a20SQuentin Schulz 
502a556c76aSAlexandre Belloni 	err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
50371e32a20SQuentin Schulz 				 port->phy_mode);
504a556c76aSAlexandre Belloni 	if (err) {
505a556c76aSAlexandre Belloni 		netdev_err(dev, "Could not attach to PHY\n");
506a556c76aSAlexandre Belloni 		return err;
507a556c76aSAlexandre Belloni 	}
508a556c76aSAlexandre Belloni 
509a556c76aSAlexandre Belloni 	dev->phydev = port->phy;
510a556c76aSAlexandre Belloni 
511a556c76aSAlexandre Belloni 	phy_attached_info(port->phy);
512a556c76aSAlexandre Belloni 	phy_start(port->phy);
513a556c76aSAlexandre Belloni 	return 0;
514a556c76aSAlexandre Belloni }
515a556c76aSAlexandre Belloni 
516a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev)
517a556c76aSAlexandre Belloni {
518a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
519a556c76aSAlexandre Belloni 
520a556c76aSAlexandre Belloni 	phy_disconnect(port->phy);
521a556c76aSAlexandre Belloni 
522a556c76aSAlexandre Belloni 	dev->phydev = NULL;
523a556c76aSAlexandre Belloni 
524a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
525a556c76aSAlexandre Belloni 	ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
526a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, port->chip_port);
527a556c76aSAlexandre Belloni 	return 0;
528a556c76aSAlexandre Belloni }
529a556c76aSAlexandre Belloni 
530a556c76aSAlexandre Belloni /* Generate the IFH for frame injection
531a556c76aSAlexandre Belloni  *
532a556c76aSAlexandre Belloni  * The IFH is a 128bit-value
533a556c76aSAlexandre Belloni  * bit 127: bypass the analyzer processing
534a556c76aSAlexandre Belloni  * bit 56-67: destination mask
535a556c76aSAlexandre Belloni  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
536a556c76aSAlexandre Belloni  * bit 20-27: cpu extraction queue mask
537a556c76aSAlexandre Belloni  * bit 16: tag type 0: C-tag, 1: S-tag
538a556c76aSAlexandre Belloni  * bit 0-11: VID
539a556c76aSAlexandre Belloni  */
540a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
541a556c76aSAlexandre Belloni {
5424e3b0468SAntoine Tenart 	ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
54308d02364SAntoine Tenart 	ifh[1] = (0xf00 & info->port) >> 8;
544a556c76aSAlexandre Belloni 	ifh[2] = (0xff & info->port) << 24;
54508d02364SAntoine Tenart 	ifh[3] = (info->tag_type << 16) | info->vid;
546a556c76aSAlexandre Belloni 
547a556c76aSAlexandre Belloni 	return 0;
548a556c76aSAlexandre Belloni }
549a556c76aSAlexandre Belloni 
550a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
551a556c76aSAlexandre Belloni {
5524e3b0468SAntoine Tenart 	struct skb_shared_info *shinfo = skb_shinfo(skb);
553a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
554a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
555a556c76aSAlexandre Belloni 	u32 val, ifh[IFH_LEN];
556a556c76aSAlexandre Belloni 	struct frame_info info = {};
557a556c76aSAlexandre Belloni 	u8 grp = 0; /* Send everything on CPU group 0 */
558a556c76aSAlexandre Belloni 	unsigned int i, count, last;
559a556c76aSAlexandre Belloni 
560a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, QS_INJ_STATUS);
561a556c76aSAlexandre Belloni 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
562a556c76aSAlexandre Belloni 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
563a556c76aSAlexandre Belloni 		return NETDEV_TX_BUSY;
564a556c76aSAlexandre Belloni 
565a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
566a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
567a556c76aSAlexandre Belloni 
568a556c76aSAlexandre Belloni 	info.port = BIT(port->chip_port);
56908d02364SAntoine Tenart 	info.tag_type = IFH_TAG_TYPE_C;
57008d02364SAntoine Tenart 	info.vid = skb_vlan_tag_get(skb);
5714e3b0468SAntoine Tenart 
5724e3b0468SAntoine Tenart 	/* Check if timestamping is needed */
5734e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
5744e3b0468SAntoine Tenart 		info.rew_op = port->ptp_cmd;
5754e3b0468SAntoine Tenart 		if (port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
5764e3b0468SAntoine Tenart 			info.rew_op |= (port->ts_id  % 4) << 3;
5774e3b0468SAntoine Tenart 	}
5784e3b0468SAntoine Tenart 
579a556c76aSAlexandre Belloni 	ocelot_gen_ifh(ifh, &info);
580a556c76aSAlexandre Belloni 
581a556c76aSAlexandre Belloni 	for (i = 0; i < IFH_LEN; i++)
582c2cd650bSAntoine Tenart 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
583c2cd650bSAntoine Tenart 				 QS_INJ_WR, grp);
584a556c76aSAlexandre Belloni 
585a556c76aSAlexandre Belloni 	count = (skb->len + 3) / 4;
586a556c76aSAlexandre Belloni 	last = skb->len % 4;
587a556c76aSAlexandre Belloni 	for (i = 0; i < count; i++) {
588a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
589a556c76aSAlexandre Belloni 	}
590a556c76aSAlexandre Belloni 
591a556c76aSAlexandre Belloni 	/* Add padding */
592a556c76aSAlexandre Belloni 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
593a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
594a556c76aSAlexandre Belloni 		i++;
595a556c76aSAlexandre Belloni 	}
596a556c76aSAlexandre Belloni 
597a556c76aSAlexandre Belloni 	/* Indicate EOF and valid bytes in last word */
598a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
599a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
600a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_EOF,
601a556c76aSAlexandre Belloni 			 QS_INJ_CTRL, grp);
602a556c76aSAlexandre Belloni 
603a556c76aSAlexandre Belloni 	/* Add dummy CRC */
604a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
605a556c76aSAlexandre Belloni 	skb_tx_timestamp(skb);
606a556c76aSAlexandre Belloni 
607a556c76aSAlexandre Belloni 	dev->stats.tx_packets++;
608a556c76aSAlexandre Belloni 	dev->stats.tx_bytes += skb->len;
6094e3b0468SAntoine Tenart 
6104e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
6114e3b0468SAntoine Tenart 	    port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
6124e3b0468SAntoine Tenart 		struct ocelot_skb *oskb =
6134e3b0468SAntoine Tenart 			kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
6144e3b0468SAntoine Tenart 
6154e3b0468SAntoine Tenart 		if (unlikely(!oskb))
6164e3b0468SAntoine Tenart 			goto out;
6174e3b0468SAntoine Tenart 
6184e3b0468SAntoine Tenart 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
6194e3b0468SAntoine Tenart 
6204e3b0468SAntoine Tenart 		oskb->skb = skb;
6214e3b0468SAntoine Tenart 		oskb->id = port->ts_id % 4;
6224e3b0468SAntoine Tenart 		port->ts_id++;
6234e3b0468SAntoine Tenart 
6244e3b0468SAntoine Tenart 		list_add_tail(&oskb->head, &port->skbs);
625a556c76aSAlexandre Belloni 
626a556c76aSAlexandre Belloni 		return NETDEV_TX_OK;
627a556c76aSAlexandre Belloni 	}
628a556c76aSAlexandre Belloni 
6294e3b0468SAntoine Tenart out:
6304e3b0468SAntoine Tenart 	dev_kfree_skb_any(skb);
6314e3b0468SAntoine Tenart 	return NETDEV_TX_OK;
6324e3b0468SAntoine Tenart }
6334e3b0468SAntoine Tenart 
6344e3b0468SAntoine Tenart void ocelot_get_hwtimestamp(struct ocelot *ocelot, struct timespec64 *ts)
6354e3b0468SAntoine Tenart {
6364e3b0468SAntoine Tenart 	unsigned long flags;
6374e3b0468SAntoine Tenart 	u32 val;
6384e3b0468SAntoine Tenart 
6394e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
6404e3b0468SAntoine Tenart 
6414e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
6424e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
6434e3b0468SAntoine Tenart 
6444e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
6454e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
6464e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
6474e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
6484e3b0468SAntoine Tenart 
6494e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
6504e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
6514e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
6524e3b0468SAntoine Tenart 
6534e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
6544e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
6554e3b0468SAntoine Tenart 		ts->tv_sec--;
6564e3b0468SAntoine Tenart 
6574e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
6584e3b0468SAntoine Tenart }
6594e3b0468SAntoine Tenart EXPORT_SYMBOL(ocelot_get_hwtimestamp);
6604e3b0468SAntoine Tenart 
66140a1578dSClaudiu Manoil static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
662a556c76aSAlexandre Belloni {
66340a1578dSClaudiu Manoil 	struct ocelot_port *port = netdev_priv(dev);
664a556c76aSAlexandre Belloni 
66540a1578dSClaudiu Manoil 	return ocelot_mact_forget(port->ocelot, addr, port->pvid);
666a556c76aSAlexandre Belloni }
667a556c76aSAlexandre Belloni 
66840a1578dSClaudiu Manoil static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
669a556c76aSAlexandre Belloni {
67040a1578dSClaudiu Manoil 	struct ocelot_port *port = netdev_priv(dev);
671a556c76aSAlexandre Belloni 
67240a1578dSClaudiu Manoil 	return ocelot_mact_learn(port->ocelot, PGID_CPU, addr, port->pvid,
673a556c76aSAlexandre Belloni 				 ENTRYTYPE_LOCKED);
674a556c76aSAlexandre Belloni }
675a556c76aSAlexandre Belloni 
676a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev)
677a556c76aSAlexandre Belloni {
678a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
679a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
680a556c76aSAlexandre Belloni 	int i;
681a556c76aSAlexandre Belloni 	u32 val;
682a556c76aSAlexandre Belloni 
683a556c76aSAlexandre Belloni 	/* This doesn't handle promiscuous mode because the bridge core is
684a556c76aSAlexandre Belloni 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
685a556c76aSAlexandre Belloni 	 * forwarded to the CPU port.
686a556c76aSAlexandre Belloni 	 */
687a556c76aSAlexandre Belloni 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
688a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
689a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
690a556c76aSAlexandre Belloni 
69140a1578dSClaudiu Manoil 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
692a556c76aSAlexandre Belloni }
693a556c76aSAlexandre Belloni 
694a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev,
695a556c76aSAlexandre Belloni 					  char *buf, size_t len)
696a556c76aSAlexandre Belloni {
697a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
698a556c76aSAlexandre Belloni 	int ret;
699a556c76aSAlexandre Belloni 
700a556c76aSAlexandre Belloni 	ret = snprintf(buf, len, "p%d", port->chip_port);
701a556c76aSAlexandre Belloni 	if (ret >= len)
702a556c76aSAlexandre Belloni 		return -EINVAL;
703a556c76aSAlexandre Belloni 
704a556c76aSAlexandre Belloni 	return 0;
705a556c76aSAlexandre Belloni }
706a556c76aSAlexandre Belloni 
707a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
708a556c76aSAlexandre Belloni {
709a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
710a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
711a556c76aSAlexandre Belloni 	const struct sockaddr *addr = p;
712a556c76aSAlexandre Belloni 
713a556c76aSAlexandre Belloni 	/* Learn the new net device MAC address in the mac table. */
714a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
715a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
716a556c76aSAlexandre Belloni 	/* Then forget the previous one. */
717a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
718a556c76aSAlexandre Belloni 
719a556c76aSAlexandre Belloni 	ether_addr_copy(dev->dev_addr, addr->sa_data);
720a556c76aSAlexandre Belloni 	return 0;
721a556c76aSAlexandre Belloni }
722a556c76aSAlexandre Belloni 
723a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev,
724a556c76aSAlexandre Belloni 			       struct rtnl_link_stats64 *stats)
725a556c76aSAlexandre Belloni {
726a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
727a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
728a556c76aSAlexandre Belloni 
729a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
730a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
731a556c76aSAlexandre Belloni 		     SYS_STAT_CFG);
732a556c76aSAlexandre Belloni 
733a556c76aSAlexandre Belloni 	/* Get Rx stats */
734a556c76aSAlexandre Belloni 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
735a556c76aSAlexandre Belloni 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
736a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
737a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
738a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
739a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
740a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
741a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
742a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
743a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
744a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
745a556c76aSAlexandre Belloni 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
746a556c76aSAlexandre Belloni 	stats->rx_dropped = dev->stats.rx_dropped;
747a556c76aSAlexandre Belloni 
748a556c76aSAlexandre Belloni 	/* Get Tx stats */
749a556c76aSAlexandre Belloni 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
750a556c76aSAlexandre Belloni 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
751a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
752a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
753a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
754a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
755a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
756a556c76aSAlexandre Belloni 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
757a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
758a556c76aSAlexandre Belloni 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
759a556c76aSAlexandre Belloni }
760a556c76aSAlexandre Belloni 
761a556c76aSAlexandre Belloni static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
762a556c76aSAlexandre Belloni 			  struct net_device *dev, const unsigned char *addr,
76387b0984eSPetr Machata 			  u16 vid, u16 flags,
76487b0984eSPetr Machata 			  struct netlink_ext_ack *extack)
765a556c76aSAlexandre Belloni {
766a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
767a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
768a556c76aSAlexandre Belloni 
7697142529fSAntoine Tenart 	if (!vid) {
7707142529fSAntoine Tenart 		if (!port->vlan_aware)
7717142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
7727142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
7737142529fSAntoine Tenart 			 * matches incoming untagged packets
7747142529fSAntoine Tenart 			 */
7757142529fSAntoine Tenart 			vid = port->pvid;
7767142529fSAntoine Tenart 		else
7777142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
7787142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
7797142529fSAntoine Tenart 			 */
7807142529fSAntoine Tenart 			return -EINVAL;
7817142529fSAntoine Tenart 	}
7827142529fSAntoine Tenart 
783a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
7848fd1a4afSAllan W. Nielsen 				 ENTRYTYPE_LOCKED);
785a556c76aSAlexandre Belloni }
786a556c76aSAlexandre Belloni 
787a556c76aSAlexandre Belloni static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
788a556c76aSAlexandre Belloni 			  struct net_device *dev,
789a556c76aSAlexandre Belloni 			  const unsigned char *addr, u16 vid)
790a556c76aSAlexandre Belloni {
791a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
792a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
793a556c76aSAlexandre Belloni 
794a556c76aSAlexandre Belloni 	return ocelot_mact_forget(ocelot, addr, vid);
795a556c76aSAlexandre Belloni }
796a556c76aSAlexandre Belloni 
797a556c76aSAlexandre Belloni struct ocelot_dump_ctx {
798a556c76aSAlexandre Belloni 	struct net_device *dev;
799a556c76aSAlexandre Belloni 	struct sk_buff *skb;
800a556c76aSAlexandre Belloni 	struct netlink_callback *cb;
801a556c76aSAlexandre Belloni 	int idx;
802a556c76aSAlexandre Belloni };
803a556c76aSAlexandre Belloni 
804a556c76aSAlexandre Belloni static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
805a556c76aSAlexandre Belloni 			      struct ocelot_dump_ctx *dump)
806a556c76aSAlexandre Belloni {
807a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
808a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
809a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
810a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
811a556c76aSAlexandre Belloni 
812a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
813a556c76aSAlexandre Belloni 		goto skip;
814a556c76aSAlexandre Belloni 
815a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
816a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
817a556c76aSAlexandre Belloni 	if (!nlh)
818a556c76aSAlexandre Belloni 		return -EMSGSIZE;
819a556c76aSAlexandre Belloni 
820a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
821a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
822a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
823a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
824a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
825a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
826a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
827a556c76aSAlexandre Belloni 	ndm->ndm_state   = NUD_REACHABLE;
828a556c76aSAlexandre Belloni 
829a556c76aSAlexandre Belloni 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
830a556c76aSAlexandre Belloni 		goto nla_put_failure;
831a556c76aSAlexandre Belloni 
832a556c76aSAlexandre Belloni 	if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
833a556c76aSAlexandre Belloni 		goto nla_put_failure;
834a556c76aSAlexandre Belloni 
835a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
836a556c76aSAlexandre Belloni 
837a556c76aSAlexandre Belloni skip:
838a556c76aSAlexandre Belloni 	dump->idx++;
839a556c76aSAlexandre Belloni 	return 0;
840a556c76aSAlexandre Belloni 
841a556c76aSAlexandre Belloni nla_put_failure:
842a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
843a556c76aSAlexandre Belloni 	return -EMSGSIZE;
844a556c76aSAlexandre Belloni }
845a556c76aSAlexandre Belloni 
846a556c76aSAlexandre Belloni static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
847a556c76aSAlexandre Belloni 				   struct ocelot_mact_entry *entry)
848a556c76aSAlexandre Belloni {
849a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
850a556c76aSAlexandre Belloni 	char mac[ETH_ALEN];
851a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
852a556c76aSAlexandre Belloni 
853a556c76aSAlexandre Belloni 	/* Set row and column to read from */
854a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
855a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
856a556c76aSAlexandre Belloni 
857a556c76aSAlexandre Belloni 	/* Issue a read command */
858a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
859a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
860a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
861a556c76aSAlexandre Belloni 
862a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
863a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
864a556c76aSAlexandre Belloni 
865a556c76aSAlexandre Belloni 	/* Read the entry flags */
866a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
867a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
868a556c76aSAlexandre Belloni 		return -EINVAL;
869a556c76aSAlexandre Belloni 
870a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
871a556c76aSAlexandre Belloni 	 * do not report it.
872a556c76aSAlexandre Belloni 	 */
873a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
874a556c76aSAlexandre Belloni 	if (dst != port->chip_port)
875a556c76aSAlexandre Belloni 		return -EINVAL;
876a556c76aSAlexandre Belloni 
877a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
878a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
879a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
880a556c76aSAlexandre Belloni 
881a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
882a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
883a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
884a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
885a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
886a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
887a556c76aSAlexandre Belloni 
888a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
889a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
890a556c76aSAlexandre Belloni 
891a556c76aSAlexandre Belloni 	return 0;
892a556c76aSAlexandre Belloni }
893a556c76aSAlexandre Belloni 
894a556c76aSAlexandre Belloni static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
895a556c76aSAlexandre Belloni 			   struct net_device *dev,
896a556c76aSAlexandre Belloni 			   struct net_device *filter_dev, int *idx)
897a556c76aSAlexandre Belloni {
898a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
899a556c76aSAlexandre Belloni 	int i, j, ret = 0;
900a556c76aSAlexandre Belloni 	struct ocelot_dump_ctx dump = {
901a556c76aSAlexandre Belloni 		.dev = dev,
902a556c76aSAlexandre Belloni 		.skb = skb,
903a556c76aSAlexandre Belloni 		.cb = cb,
904a556c76aSAlexandre Belloni 		.idx = *idx,
905a556c76aSAlexandre Belloni 	};
906a556c76aSAlexandre Belloni 
907a556c76aSAlexandre Belloni 	struct ocelot_mact_entry entry;
908a556c76aSAlexandre Belloni 
909a556c76aSAlexandre Belloni 	/* Loop through all the mac tables entries. There are 1024 rows of 4
910a556c76aSAlexandre Belloni 	 * entries.
911a556c76aSAlexandre Belloni 	 */
912a556c76aSAlexandre Belloni 	for (i = 0; i < 1024; i++) {
913a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
914a556c76aSAlexandre Belloni 			ret = ocelot_mact_read(port, i, j, &entry);
915a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
916a556c76aSAlexandre Belloni 			 * skip it.
917a556c76aSAlexandre Belloni 			 */
918a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
919a556c76aSAlexandre Belloni 				continue;
920a556c76aSAlexandre Belloni 			else if (ret)
921a556c76aSAlexandre Belloni 				goto end;
922a556c76aSAlexandre Belloni 
923a556c76aSAlexandre Belloni 			ret = ocelot_fdb_do_dump(&entry, &dump);
924a556c76aSAlexandre Belloni 			if (ret)
925a556c76aSAlexandre Belloni 				goto end;
926a556c76aSAlexandre Belloni 		}
927a556c76aSAlexandre Belloni 	}
928a556c76aSAlexandre Belloni 
929a556c76aSAlexandre Belloni end:
930a556c76aSAlexandre Belloni 	*idx = dump.idx;
931a556c76aSAlexandre Belloni 	return ret;
932a556c76aSAlexandre Belloni }
933a556c76aSAlexandre Belloni 
9347142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
9357142529fSAntoine Tenart 				  u16 vid)
9367142529fSAntoine Tenart {
937*1c44ce56SVladimir Oltean 	return ocelot_vlan_vid_add(dev, vid, false, false);
9387142529fSAntoine Tenart }
9397142529fSAntoine Tenart 
9407142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
9417142529fSAntoine Tenart 				   u16 vid)
9427142529fSAntoine Tenart {
9437142529fSAntoine Tenart 	return ocelot_vlan_vid_del(dev, vid);
9447142529fSAntoine Tenart }
9457142529fSAntoine Tenart 
9467142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev,
9477142529fSAntoine Tenart 			       netdev_features_t features)
9487142529fSAntoine Tenart {
9497142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
9507142529fSAntoine Tenart 	netdev_features_t changed = dev->features ^ features;
9517142529fSAntoine Tenart 
9522c1d029aSJoergen Andreasen 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
9532c1d029aSJoergen Andreasen 	    port->tc.offload_cnt) {
9542c1d029aSJoergen Andreasen 		netdev_err(dev,
9552c1d029aSJoergen Andreasen 			   "Cannot disable HW TC offload while offloads active\n");
9562c1d029aSJoergen Andreasen 		return -EBUSY;
9572c1d029aSJoergen Andreasen 	}
9582c1d029aSJoergen Andreasen 
9597142529fSAntoine Tenart 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
9607142529fSAntoine Tenart 		ocelot_vlan_mode(port, features);
9617142529fSAntoine Tenart 
9627142529fSAntoine Tenart 	return 0;
9637142529fSAntoine Tenart }
9647142529fSAntoine Tenart 
965751302c3SFlorian Fainelli static int ocelot_get_port_parent_id(struct net_device *dev,
966751302c3SFlorian Fainelli 				     struct netdev_phys_item_id *ppid)
967751302c3SFlorian Fainelli {
968751302c3SFlorian Fainelli 	struct ocelot_port *ocelot_port = netdev_priv(dev);
969751302c3SFlorian Fainelli 	struct ocelot *ocelot = ocelot_port->ocelot;
970751302c3SFlorian Fainelli 
971751302c3SFlorian Fainelli 	ppid->id_len = sizeof(ocelot->base_mac);
972751302c3SFlorian Fainelli 	memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
973751302c3SFlorian Fainelli 
974751302c3SFlorian Fainelli 	return 0;
975751302c3SFlorian Fainelli }
976751302c3SFlorian Fainelli 
9774e3b0468SAntoine Tenart static int ocelot_hwstamp_get(struct ocelot_port *port, struct ifreq *ifr)
9784e3b0468SAntoine Tenart {
9794e3b0468SAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
9804e3b0468SAntoine Tenart 
9814e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
9824e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
9834e3b0468SAntoine Tenart }
9844e3b0468SAntoine Tenart 
9854e3b0468SAntoine Tenart static int ocelot_hwstamp_set(struct ocelot_port *port, struct ifreq *ifr)
9864e3b0468SAntoine Tenart {
9874e3b0468SAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
9884e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
9894e3b0468SAntoine Tenart 
9904e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
9914e3b0468SAntoine Tenart 		return -EFAULT;
9924e3b0468SAntoine Tenart 
9934e3b0468SAntoine Tenart 	/* reserved for future extensions */
9944e3b0468SAntoine Tenart 	if (cfg.flags)
9954e3b0468SAntoine Tenart 		return -EINVAL;
9964e3b0468SAntoine Tenart 
9974e3b0468SAntoine Tenart 	/* Tx type sanity check */
9984e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
9994e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
10004e3b0468SAntoine Tenart 		port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
10014e3b0468SAntoine Tenart 		break;
10024e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
10034e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
10044e3b0468SAntoine Tenart 		 * need to update the origin time.
10054e3b0468SAntoine Tenart 		 */
10064e3b0468SAntoine Tenart 		port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
10074e3b0468SAntoine Tenart 		break;
10084e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
10094e3b0468SAntoine Tenart 		port->ptp_cmd = 0;
10104e3b0468SAntoine Tenart 		break;
10114e3b0468SAntoine Tenart 	default:
10124e3b0468SAntoine Tenart 		return -ERANGE;
10134e3b0468SAntoine Tenart 	}
10144e3b0468SAntoine Tenart 
10154e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
10164e3b0468SAntoine Tenart 
10174e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
10184e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
10194e3b0468SAntoine Tenart 		break;
10204e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
10214e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
10224e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
10234e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
10244e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
10254e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
10264e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
10274e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
10284e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
10294e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
10304e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
10314e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
10324e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
10334e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
10344e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
10354e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
10364e3b0468SAntoine Tenart 		break;
10374e3b0468SAntoine Tenart 	default:
10384e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
10394e3b0468SAntoine Tenart 		return -ERANGE;
10404e3b0468SAntoine Tenart 	}
10414e3b0468SAntoine Tenart 
10424e3b0468SAntoine Tenart 	/* Commit back the result & save it */
10434e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
10444e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
10454e3b0468SAntoine Tenart 
10464e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
10474e3b0468SAntoine Tenart }
10484e3b0468SAntoine Tenart 
10494e3b0468SAntoine Tenart static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10504e3b0468SAntoine Tenart {
10514e3b0468SAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
10524e3b0468SAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
10534e3b0468SAntoine Tenart 
10544e3b0468SAntoine Tenart 	/* The function is only used for PTP operations for now */
10554e3b0468SAntoine Tenart 	if (!ocelot->ptp)
10564e3b0468SAntoine Tenart 		return -EOPNOTSUPP;
10574e3b0468SAntoine Tenart 
10584e3b0468SAntoine Tenart 	switch (cmd) {
10594e3b0468SAntoine Tenart 	case SIOCSHWTSTAMP:
10604e3b0468SAntoine Tenart 		return ocelot_hwstamp_set(port, ifr);
10614e3b0468SAntoine Tenart 	case SIOCGHWTSTAMP:
10624e3b0468SAntoine Tenart 		return ocelot_hwstamp_get(port, ifr);
10634e3b0468SAntoine Tenart 	default:
10644e3b0468SAntoine Tenart 		return -EOPNOTSUPP;
10654e3b0468SAntoine Tenart 	}
10664e3b0468SAntoine Tenart }
10674e3b0468SAntoine Tenart 
1068a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = {
1069a556c76aSAlexandre Belloni 	.ndo_open			= ocelot_port_open,
1070a556c76aSAlexandre Belloni 	.ndo_stop			= ocelot_port_stop,
1071a556c76aSAlexandre Belloni 	.ndo_start_xmit			= ocelot_port_xmit,
1072a556c76aSAlexandre Belloni 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
1073a556c76aSAlexandre Belloni 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
1074a556c76aSAlexandre Belloni 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
1075a556c76aSAlexandre Belloni 	.ndo_get_stats64		= ocelot_get_stats64,
1076a556c76aSAlexandre Belloni 	.ndo_fdb_add			= ocelot_fdb_add,
1077a556c76aSAlexandre Belloni 	.ndo_fdb_del			= ocelot_fdb_del,
1078a556c76aSAlexandre Belloni 	.ndo_fdb_dump			= ocelot_fdb_dump,
10797142529fSAntoine Tenart 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
10807142529fSAntoine Tenart 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
10817142529fSAntoine Tenart 	.ndo_set_features		= ocelot_set_features,
1082751302c3SFlorian Fainelli 	.ndo_get_port_parent_id		= ocelot_get_port_parent_id,
10832c1d029aSJoergen Andreasen 	.ndo_setup_tc			= ocelot_setup_tc,
10844e3b0468SAntoine Tenart 	.ndo_do_ioctl			= ocelot_ioctl,
1085a556c76aSAlexandre Belloni };
1086a556c76aSAlexandre Belloni 
1087a556c76aSAlexandre Belloni static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
1088a556c76aSAlexandre Belloni {
1089a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(netdev);
1090a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1091a556c76aSAlexandre Belloni 	int i;
1092a556c76aSAlexandre Belloni 
1093a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1094a556c76aSAlexandre Belloni 		return;
1095a556c76aSAlexandre Belloni 
1096a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1097a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1098a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1099a556c76aSAlexandre Belloni }
1100a556c76aSAlexandre Belloni 
11011e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1102a556c76aSAlexandre Belloni {
1103a556c76aSAlexandre Belloni 	int i, j;
1104a556c76aSAlexandre Belloni 
1105a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1106a556c76aSAlexandre Belloni 
1107a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1108a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1109a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1110a556c76aSAlexandre Belloni 
1111a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1112a556c76aSAlexandre Belloni 			u32 val;
1113a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1114a556c76aSAlexandre Belloni 
1115a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1116a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1117a556c76aSAlexandre Belloni 
1118a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1119a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1120a556c76aSAlexandre Belloni 
1121a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1122a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1123a556c76aSAlexandre Belloni 		}
1124a556c76aSAlexandre Belloni 	}
1125a556c76aSAlexandre Belloni 
11261e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
11271e1caa97SClaudiu Manoil }
11281e1caa97SClaudiu Manoil 
11291e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
11301e1caa97SClaudiu Manoil {
11311e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
11321e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
11331e1caa97SClaudiu Manoil 					     stats_work);
11341e1caa97SClaudiu Manoil 
11351e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
11361e1caa97SClaudiu Manoil 
1137a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1138a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1139a556c76aSAlexandre Belloni }
1140a556c76aSAlexandre Belloni 
1141a556c76aSAlexandre Belloni static void ocelot_get_ethtool_stats(struct net_device *dev,
1142a556c76aSAlexandre Belloni 				     struct ethtool_stats *stats, u64 *data)
1143a556c76aSAlexandre Belloni {
1144a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1145a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1146a556c76aSAlexandre Belloni 	int i;
1147a556c76aSAlexandre Belloni 
1148a556c76aSAlexandre Belloni 	/* check and update now */
11491e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1150a556c76aSAlexandre Belloni 
1151a556c76aSAlexandre Belloni 	/* Copy all counters */
1152a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1153a556c76aSAlexandre Belloni 		*data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
1154a556c76aSAlexandre Belloni }
1155a556c76aSAlexandre Belloni 
1156a556c76aSAlexandre Belloni static int ocelot_get_sset_count(struct net_device *dev, int sset)
1157a556c76aSAlexandre Belloni {
1158a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1159a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1160a556c76aSAlexandre Belloni 
1161a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1162a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1163a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1164a556c76aSAlexandre Belloni }
1165a556c76aSAlexandre Belloni 
11664e3b0468SAntoine Tenart static int ocelot_get_ts_info(struct net_device *dev,
11674e3b0468SAntoine Tenart 			      struct ethtool_ts_info *info)
11684e3b0468SAntoine Tenart {
11694e3b0468SAntoine Tenart 	struct ocelot_port *ocelot_port = netdev_priv(dev);
11704e3b0468SAntoine Tenart 	struct ocelot *ocelot = ocelot_port->ocelot;
11714e3b0468SAntoine Tenart 
11724e3b0468SAntoine Tenart 	if (!ocelot->ptp)
11734e3b0468SAntoine Tenart 		return ethtool_op_get_ts_info(dev, info);
11744e3b0468SAntoine Tenart 
11754e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
11764e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
11774e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
11784e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
11794e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
11804e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
11814e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
11824e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
11834e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
11844e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
11854e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
11864e3b0468SAntoine Tenart 
11874e3b0468SAntoine Tenart 	return 0;
11884e3b0468SAntoine Tenart }
11894e3b0468SAntoine Tenart 
1190a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = {
1191a556c76aSAlexandre Belloni 	.get_strings		= ocelot_get_strings,
1192a556c76aSAlexandre Belloni 	.get_ethtool_stats	= ocelot_get_ethtool_stats,
1193a556c76aSAlexandre Belloni 	.get_sset_count		= ocelot_get_sset_count,
1194dc96ee37SAlexandre Belloni 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1195dc96ee37SAlexandre Belloni 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
11964e3b0468SAntoine Tenart 	.get_ts_info		= ocelot_get_ts_info,
1197a556c76aSAlexandre Belloni };
1198a556c76aSAlexandre Belloni 
1199a556c76aSAlexandre Belloni static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1200a556c76aSAlexandre Belloni 					  struct switchdev_trans *trans,
1201a556c76aSAlexandre Belloni 					  u8 state)
1202a556c76aSAlexandre Belloni {
1203a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1204a556c76aSAlexandre Belloni 	u32 port_cfg;
1205a556c76aSAlexandre Belloni 	int port, i;
1206a556c76aSAlexandre Belloni 
1207a556c76aSAlexandre Belloni 	if (switchdev_trans_ph_prepare(trans))
1208a556c76aSAlexandre Belloni 		return 0;
1209a556c76aSAlexandre Belloni 
1210a556c76aSAlexandre Belloni 	if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1211a556c76aSAlexandre Belloni 		return 0;
1212a556c76aSAlexandre Belloni 
1213a556c76aSAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1214a556c76aSAlexandre Belloni 				   ocelot_port->chip_port);
1215a556c76aSAlexandre Belloni 
1216a556c76aSAlexandre Belloni 	switch (state) {
1217a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
1218a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1219a556c76aSAlexandre Belloni 		/* Fallthrough */
1220a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1221a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1222a556c76aSAlexandre Belloni 		break;
1223a556c76aSAlexandre Belloni 
1224a556c76aSAlexandre Belloni 	default:
1225a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1226a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1227a556c76aSAlexandre Belloni 		break;
1228a556c76aSAlexandre Belloni 	}
1229a556c76aSAlexandre Belloni 
1230a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1231a556c76aSAlexandre Belloni 			 ocelot_port->chip_port);
1232a556c76aSAlexandre Belloni 
1233a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1234a556c76aSAlexandre Belloni 	 * a source for the other ports.
1235a556c76aSAlexandre Belloni 	 */
1236a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1237a556c76aSAlexandre Belloni 		if (ocelot->bridge_fwd_mask & BIT(port)) {
1238a556c76aSAlexandre Belloni 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1239a556c76aSAlexandre Belloni 
1240a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1241a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
1242a556c76aSAlexandre Belloni 
1243a556c76aSAlexandre Belloni 				if (!bond_mask)
1244a556c76aSAlexandre Belloni 					continue;
1245a556c76aSAlexandre Belloni 
1246a556c76aSAlexandre Belloni 				if (bond_mask & BIT(port)) {
1247a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
1248a556c76aSAlexandre Belloni 					break;
1249a556c76aSAlexandre Belloni 				}
1250a556c76aSAlexandre Belloni 			}
1251a556c76aSAlexandre Belloni 
1252a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1253a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports) | mask,
1254a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1255a556c76aSAlexandre Belloni 		} else {
1256a556c76aSAlexandre Belloni 			/* Only the CPU port, this is compatible with link
1257a556c76aSAlexandre Belloni 			 * aggregation.
1258a556c76aSAlexandre Belloni 			 */
1259a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1260a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports),
1261a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1262a556c76aSAlexandre Belloni 		}
1263a556c76aSAlexandre Belloni 	}
1264a556c76aSAlexandre Belloni 
1265a556c76aSAlexandre Belloni 	return 0;
1266a556c76aSAlexandre Belloni }
1267a556c76aSAlexandre Belloni 
1268a556c76aSAlexandre Belloni static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1269a556c76aSAlexandre Belloni 					unsigned long ageing_clock_t)
1270a556c76aSAlexandre Belloni {
1271a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1272a556c76aSAlexandre Belloni 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1273a556c76aSAlexandre Belloni 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1274a556c76aSAlexandre Belloni 
1275a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1276a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1277a556c76aSAlexandre Belloni }
1278a556c76aSAlexandre Belloni 
1279a556c76aSAlexandre Belloni static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1280a556c76aSAlexandre Belloni {
1281a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1282a556c76aSAlexandre Belloni 	u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1283a556c76aSAlexandre Belloni 				  port->chip_port);
1284a556c76aSAlexandre Belloni 
1285a556c76aSAlexandre Belloni 	if (mc)
1286a556c76aSAlexandre Belloni 		val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1287a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1288a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1289a556c76aSAlexandre Belloni 	else
1290a556c76aSAlexandre Belloni 		val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1291a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1292a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1293a556c76aSAlexandre Belloni 
1294a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1295a556c76aSAlexandre Belloni }
1296a556c76aSAlexandre Belloni 
1297a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev,
1298a556c76aSAlexandre Belloni 				const struct switchdev_attr *attr,
1299a556c76aSAlexandre Belloni 				struct switchdev_trans *trans)
1300a556c76aSAlexandre Belloni {
1301a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1302a556c76aSAlexandre Belloni 	int err = 0;
1303a556c76aSAlexandre Belloni 
1304a556c76aSAlexandre Belloni 	switch (attr->id) {
1305a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1306a556c76aSAlexandre Belloni 		ocelot_port_attr_stp_state_set(ocelot_port, trans,
1307a556c76aSAlexandre Belloni 					       attr->u.stp_state);
1308a556c76aSAlexandre Belloni 		break;
1309a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1310a556c76aSAlexandre Belloni 		ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1311a556c76aSAlexandre Belloni 		break;
13127142529fSAntoine Tenart 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
13137142529fSAntoine Tenart 		ocelot_port->vlan_aware = attr->u.vlan_filtering;
13147142529fSAntoine Tenart 		ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port);
13157142529fSAntoine Tenart 		break;
1316a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1317a556c76aSAlexandre Belloni 		ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1318a556c76aSAlexandre Belloni 		break;
1319a556c76aSAlexandre Belloni 	default:
1320a556c76aSAlexandre Belloni 		err = -EOPNOTSUPP;
1321a556c76aSAlexandre Belloni 		break;
1322a556c76aSAlexandre Belloni 	}
1323a556c76aSAlexandre Belloni 
1324a556c76aSAlexandre Belloni 	return err;
1325a556c76aSAlexandre Belloni }
1326a556c76aSAlexandre Belloni 
13277142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev,
13287142529fSAntoine Tenart 				    const struct switchdev_obj_port_vlan *vlan,
13297142529fSAntoine Tenart 				    struct switchdev_trans *trans)
13307142529fSAntoine Tenart {
13317142529fSAntoine Tenart 	int ret;
13327142529fSAntoine Tenart 	u16 vid;
13337142529fSAntoine Tenart 
13347142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
13357142529fSAntoine Tenart 		ret = ocelot_vlan_vid_add(dev, vid,
13367142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
13377142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
13387142529fSAntoine Tenart 		if (ret)
13397142529fSAntoine Tenart 			return ret;
13407142529fSAntoine Tenart 	}
13417142529fSAntoine Tenart 
13427142529fSAntoine Tenart 	return 0;
13437142529fSAntoine Tenart }
13447142529fSAntoine Tenart 
13457142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev,
13467142529fSAntoine Tenart 				     const struct switchdev_obj_port_vlan *vlan)
13477142529fSAntoine Tenart {
13487142529fSAntoine Tenart 	int ret;
13497142529fSAntoine Tenart 	u16 vid;
13507142529fSAntoine Tenart 
13517142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
13527142529fSAntoine Tenart 		ret = ocelot_vlan_vid_del(dev, vid);
13537142529fSAntoine Tenart 
13547142529fSAntoine Tenart 		if (ret)
13557142529fSAntoine Tenart 			return ret;
13567142529fSAntoine Tenart 	}
13577142529fSAntoine Tenart 
13587142529fSAntoine Tenart 	return 0;
13597142529fSAntoine Tenart }
13607142529fSAntoine Tenart 
1361a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1362a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1363a556c76aSAlexandre Belloni 						     u16 vid)
1364a556c76aSAlexandre Belloni {
1365a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1366a556c76aSAlexandre Belloni 
1367a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1368a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1369a556c76aSAlexandre Belloni 			return mc;
1370a556c76aSAlexandre Belloni 	}
1371a556c76aSAlexandre Belloni 
1372a556c76aSAlexandre Belloni 	return NULL;
1373a556c76aSAlexandre Belloni }
1374a556c76aSAlexandre Belloni 
1375a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev,
1376a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb,
1377a556c76aSAlexandre Belloni 				   struct switchdev_trans *trans)
1378a556c76aSAlexandre Belloni {
1379a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1380a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1381a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1382a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1383a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1384a556c76aSAlexandre Belloni 	bool new = false;
1385a556c76aSAlexandre Belloni 
1386a556c76aSAlexandre Belloni 	if (!vid)
13877142529fSAntoine Tenart 		vid = port->pvid;
1388a556c76aSAlexandre Belloni 
1389a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1390a556c76aSAlexandre Belloni 	if (!mc) {
1391a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1392a556c76aSAlexandre Belloni 		if (!mc)
1393a556c76aSAlexandre Belloni 			return -ENOMEM;
1394a556c76aSAlexandre Belloni 
1395a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1396a556c76aSAlexandre Belloni 		mc->vid = vid;
1397a556c76aSAlexandre Belloni 
1398a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1399a556c76aSAlexandre Belloni 		new = true;
1400a556c76aSAlexandre Belloni 	}
1401a556c76aSAlexandre Belloni 
1402a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1403a556c76aSAlexandre Belloni 	addr[0] = 0;
1404a556c76aSAlexandre Belloni 
1405a556c76aSAlexandre Belloni 	if (!new) {
1406a556c76aSAlexandre Belloni 		addr[2] = mc->ports << 0;
1407a556c76aSAlexandre Belloni 		addr[1] = mc->ports << 8;
1408a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1409a556c76aSAlexandre Belloni 	}
1410a556c76aSAlexandre Belloni 
1411a556c76aSAlexandre Belloni 	mc->ports |= BIT(port->chip_port);
1412a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1413a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1414a556c76aSAlexandre Belloni 
1415a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1416a556c76aSAlexandre Belloni }
1417a556c76aSAlexandre Belloni 
1418a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev,
1419a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb)
1420a556c76aSAlexandre Belloni {
1421a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1422a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1423a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1424a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1425a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1426a556c76aSAlexandre Belloni 
1427a556c76aSAlexandre Belloni 	if (!vid)
14287142529fSAntoine Tenart 		vid = port->pvid;
1429a556c76aSAlexandre Belloni 
1430a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1431a556c76aSAlexandre Belloni 	if (!mc)
1432a556c76aSAlexandre Belloni 		return -ENOENT;
1433a556c76aSAlexandre Belloni 
1434a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1435a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1436a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1437a556c76aSAlexandre Belloni 	addr[0] = 0;
1438a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1439a556c76aSAlexandre Belloni 
1440a556c76aSAlexandre Belloni 	mc->ports &= ~BIT(port->chip_port);
1441a556c76aSAlexandre Belloni 	if (!mc->ports) {
1442a556c76aSAlexandre Belloni 		list_del(&mc->list);
1443a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1444a556c76aSAlexandre Belloni 		return 0;
1445a556c76aSAlexandre Belloni 	}
1446a556c76aSAlexandre Belloni 
1447a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1448a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1449a556c76aSAlexandre Belloni 
1450a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1451a556c76aSAlexandre Belloni }
1452a556c76aSAlexandre Belloni 
1453a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev,
1454a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj,
145569213513SPetr Machata 			       struct switchdev_trans *trans,
145669213513SPetr Machata 			       struct netlink_ext_ack *extack)
1457a556c76aSAlexandre Belloni {
1458a556c76aSAlexandre Belloni 	int ret = 0;
1459a556c76aSAlexandre Belloni 
1460a556c76aSAlexandre Belloni 	switch (obj->id) {
14617142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
14627142529fSAntoine Tenart 		ret = ocelot_port_obj_add_vlan(dev,
14637142529fSAntoine Tenart 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
14647142529fSAntoine Tenart 					       trans);
14657142529fSAntoine Tenart 		break;
1466a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1467a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1468a556c76aSAlexandre Belloni 					      trans);
1469a556c76aSAlexandre Belloni 		break;
1470a556c76aSAlexandre Belloni 	default:
1471a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1472a556c76aSAlexandre Belloni 	}
1473a556c76aSAlexandre Belloni 
1474a556c76aSAlexandre Belloni 	return ret;
1475a556c76aSAlexandre Belloni }
1476a556c76aSAlexandre Belloni 
1477a556c76aSAlexandre Belloni static int ocelot_port_obj_del(struct net_device *dev,
1478a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj)
1479a556c76aSAlexandre Belloni {
1480a556c76aSAlexandre Belloni 	int ret = 0;
1481a556c76aSAlexandre Belloni 
1482a556c76aSAlexandre Belloni 	switch (obj->id) {
14837142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
14847142529fSAntoine Tenart 		ret = ocelot_port_vlan_del_vlan(dev,
14857142529fSAntoine Tenart 						SWITCHDEV_OBJ_PORT_VLAN(obj));
14867142529fSAntoine Tenart 		break;
1487a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1488a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1489a556c76aSAlexandre Belloni 		break;
1490a556c76aSAlexandre Belloni 	default:
1491a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1492a556c76aSAlexandre Belloni 	}
1493a556c76aSAlexandre Belloni 
1494a556c76aSAlexandre Belloni 	return ret;
1495a556c76aSAlexandre Belloni }
1496a556c76aSAlexandre Belloni 
1497a556c76aSAlexandre Belloni static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1498a556c76aSAlexandre Belloni 				   struct net_device *bridge)
1499a556c76aSAlexandre Belloni {
1500a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1501a556c76aSAlexandre Belloni 
1502a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1503a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1504a556c76aSAlexandre Belloni 	} else {
1505a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1506a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1507a556c76aSAlexandre Belloni 			 * unsupported */
1508a556c76aSAlexandre Belloni 			return -ENODEV;
1509a556c76aSAlexandre Belloni 	}
1510a556c76aSAlexandre Belloni 
1511a556c76aSAlexandre Belloni 	ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1512a556c76aSAlexandre Belloni 
1513a556c76aSAlexandre Belloni 	return 0;
1514a556c76aSAlexandre Belloni }
1515a556c76aSAlexandre Belloni 
1516a556c76aSAlexandre Belloni static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1517a556c76aSAlexandre Belloni 				     struct net_device *bridge)
1518a556c76aSAlexandre Belloni {
1519a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1520a556c76aSAlexandre Belloni 
1521a556c76aSAlexandre Belloni 	ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
1522a556c76aSAlexandre Belloni 
1523a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1524a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
15257142529fSAntoine Tenart 
15267142529fSAntoine Tenart 	/* Clear bridge vlan settings before calling ocelot_vlan_port_apply */
15277142529fSAntoine Tenart 	ocelot_port->vlan_aware = 0;
15287142529fSAntoine Tenart 	ocelot_port->pvid = 0;
15297142529fSAntoine Tenart 	ocelot_port->vid = 0;
1530a556c76aSAlexandre Belloni }
1531a556c76aSAlexandre Belloni 
1532dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1533dc96ee37SAlexandre Belloni {
1534dc96ee37SAlexandre Belloni 	int i, port, lag;
1535dc96ee37SAlexandre Belloni 
1536dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1537dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1538dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1539dc96ee37SAlexandre Belloni 
1540dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1541dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1542dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1543dc96ee37SAlexandre Belloni 
1544dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1545dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1546dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1547dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1548dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1549dc96ee37SAlexandre Belloni 
1550dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1551dc96ee37SAlexandre Belloni 		if (!bond_mask)
1552dc96ee37SAlexandre Belloni 			continue;
1553dc96ee37SAlexandre Belloni 
1554dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1555dc96ee37SAlexandre Belloni 			// Destination mask
1556dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1557dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1558dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1559dc96ee37SAlexandre Belloni 			aggr_count++;
1560dc96ee37SAlexandre Belloni 		}
1561dc96ee37SAlexandre Belloni 
1562dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1563dc96ee37SAlexandre Belloni 			u32 ac;
1564dc96ee37SAlexandre Belloni 
1565dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1566dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1567dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1568dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1569dc96ee37SAlexandre Belloni 		}
1570dc96ee37SAlexandre Belloni 	}
1571dc96ee37SAlexandre Belloni }
1572dc96ee37SAlexandre Belloni 
1573dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1574dc96ee37SAlexandre Belloni {
1575dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1576dc96ee37SAlexandre Belloni 	unsigned int p;
1577dc96ee37SAlexandre Belloni 
1578dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1579dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1580dc96ee37SAlexandre Belloni 
1581dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1582dc96ee37SAlexandre Belloni 
1583dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1584dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1585dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1586dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1587dc96ee37SAlexandre Belloni 	}
1588dc96ee37SAlexandre Belloni }
1589dc96ee37SAlexandre Belloni 
1590dc96ee37SAlexandre Belloni static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1591dc96ee37SAlexandre Belloni 				struct net_device *bond)
1592dc96ee37SAlexandre Belloni {
1593dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1594dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1595dc96ee37SAlexandre Belloni 	int lag, lp;
1596dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1597dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1598dc96ee37SAlexandre Belloni 
1599dc96ee37SAlexandre Belloni 	rcu_read_lock();
1600dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1601dc96ee37SAlexandre Belloni 		struct ocelot_port *port = netdev_priv(ndev);
1602dc96ee37SAlexandre Belloni 
1603dc96ee37SAlexandre Belloni 		bond_mask |= BIT(port->chip_port);
1604dc96ee37SAlexandre Belloni 	}
1605dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1606dc96ee37SAlexandre Belloni 
1607dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1608dc96ee37SAlexandre Belloni 
1609dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1610dc96ee37SAlexandre Belloni 	 * now on
1611dc96ee37SAlexandre Belloni 	 */
1612dc96ee37SAlexandre Belloni 	if (p == lp) {
1613dc96ee37SAlexandre Belloni 		lag = p;
1614dc96ee37SAlexandre Belloni 		ocelot->lags[p] = bond_mask;
1615dc96ee37SAlexandre Belloni 		bond_mask &= ~BIT(p);
1616dc96ee37SAlexandre Belloni 		if (bond_mask) {
1617dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1618dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1619dc96ee37SAlexandre Belloni 		}
1620dc96ee37SAlexandre Belloni 	} else {
1621dc96ee37SAlexandre Belloni 		lag = lp;
1622dc96ee37SAlexandre Belloni 		ocelot->lags[lp] |= BIT(p);
1623dc96ee37SAlexandre Belloni 	}
1624dc96ee37SAlexandre Belloni 
1625dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1626dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1627dc96ee37SAlexandre Belloni 
1628dc96ee37SAlexandre Belloni 	return 0;
1629dc96ee37SAlexandre Belloni }
1630dc96ee37SAlexandre Belloni 
1631dc96ee37SAlexandre Belloni static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1632dc96ee37SAlexandre Belloni 				  struct net_device *bond)
1633dc96ee37SAlexandre Belloni {
1634dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1635dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1636dc96ee37SAlexandre Belloni 	u32 port_cfg;
1637dc96ee37SAlexandre Belloni 	int i;
1638dc96ee37SAlexandre Belloni 
1639dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1640dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1641dc96ee37SAlexandre Belloni 		ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1642dc96ee37SAlexandre Belloni 
1643dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1644dc96ee37SAlexandre Belloni 	 * next port
1645dc96ee37SAlexandre Belloni 	 */
1646dc96ee37SAlexandre Belloni 	if (ocelot->lags[p]) {
1647dc96ee37SAlexandre Belloni 		int n = __ffs(ocelot->lags[p]);
1648dc96ee37SAlexandre Belloni 
1649dc96ee37SAlexandre Belloni 		ocelot->lags[n] = ocelot->lags[p];
1650dc96ee37SAlexandre Belloni 		ocelot->lags[p] = 0;
1651dc96ee37SAlexandre Belloni 
1652dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1653dc96ee37SAlexandre Belloni 	}
1654dc96ee37SAlexandre Belloni 
1655dc96ee37SAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1656dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1657dc96ee37SAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1658dc96ee37SAlexandre Belloni 			 ANA_PORT_PORT_CFG, p);
1659dc96ee37SAlexandre Belloni 
1660dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1661dc96ee37SAlexandre Belloni }
1662dc96ee37SAlexandre Belloni 
1663a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */
1664a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1665a556c76aSAlexandre Belloni {
1666a556c76aSAlexandre Belloni 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1667a556c76aSAlexandre Belloni }
1668a556c76aSAlexandre Belloni 
1669a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev,
1670a556c76aSAlexandre Belloni 				       unsigned long event,
1671a556c76aSAlexandre Belloni 				       struct netdev_notifier_changeupper_info *info)
1672a556c76aSAlexandre Belloni {
1673a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1674a556c76aSAlexandre Belloni 	int err = 0;
1675a556c76aSAlexandre Belloni 
1676a556c76aSAlexandre Belloni 	if (!ocelot_netdevice_dev_check(dev))
1677a556c76aSAlexandre Belloni 		return 0;
1678a556c76aSAlexandre Belloni 
1679a556c76aSAlexandre Belloni 	switch (event) {
1680a556c76aSAlexandre Belloni 	case NETDEV_CHANGEUPPER:
1681a556c76aSAlexandre Belloni 		if (netif_is_bridge_master(info->upper_dev)) {
1682a556c76aSAlexandre Belloni 			if (info->linking)
1683a556c76aSAlexandre Belloni 				err = ocelot_port_bridge_join(ocelot_port,
1684a556c76aSAlexandre Belloni 							      info->upper_dev);
1685a556c76aSAlexandre Belloni 			else
1686a556c76aSAlexandre Belloni 				ocelot_port_bridge_leave(ocelot_port,
1687a556c76aSAlexandre Belloni 							 info->upper_dev);
16887142529fSAntoine Tenart 
16897142529fSAntoine Tenart 			ocelot_vlan_port_apply(ocelot_port->ocelot,
16907142529fSAntoine Tenart 					       ocelot_port);
1691a556c76aSAlexandre Belloni 		}
1692dc96ee37SAlexandre Belloni 		if (netif_is_lag_master(info->upper_dev)) {
1693dc96ee37SAlexandre Belloni 			if (info->linking)
1694dc96ee37SAlexandre Belloni 				err = ocelot_port_lag_join(ocelot_port,
1695dc96ee37SAlexandre Belloni 							   info->upper_dev);
1696dc96ee37SAlexandre Belloni 			else
1697dc96ee37SAlexandre Belloni 				ocelot_port_lag_leave(ocelot_port,
1698dc96ee37SAlexandre Belloni 						      info->upper_dev);
1699dc96ee37SAlexandre Belloni 		}
1700a556c76aSAlexandre Belloni 		break;
1701a556c76aSAlexandre Belloni 	default:
1702a556c76aSAlexandre Belloni 		break;
1703a556c76aSAlexandre Belloni 	}
1704a556c76aSAlexandre Belloni 
1705a556c76aSAlexandre Belloni 	return err;
1706a556c76aSAlexandre Belloni }
1707a556c76aSAlexandre Belloni 
1708a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused,
1709a556c76aSAlexandre Belloni 				  unsigned long event, void *ptr)
1710a556c76aSAlexandre Belloni {
1711a556c76aSAlexandre Belloni 	struct netdev_notifier_changeupper_info *info = ptr;
1712a556c76aSAlexandre Belloni 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
17132ac0e152SGeert Uytterhoeven 	int ret = 0;
1714a556c76aSAlexandre Belloni 
1715dc96ee37SAlexandre Belloni 	if (event == NETDEV_PRECHANGEUPPER &&
1716dc96ee37SAlexandre Belloni 	    netif_is_lag_master(info->upper_dev)) {
1717dc96ee37SAlexandre Belloni 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1718dc96ee37SAlexandre Belloni 		struct netlink_ext_ack *extack;
1719dc96ee37SAlexandre Belloni 
1720dc96ee37SAlexandre Belloni 		if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1721dc96ee37SAlexandre Belloni 			extack = netdev_notifier_info_to_extack(&info->info);
1722dc96ee37SAlexandre Belloni 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1723dc96ee37SAlexandre Belloni 
1724dc96ee37SAlexandre Belloni 			ret = -EINVAL;
1725dc96ee37SAlexandre Belloni 			goto notify;
1726dc96ee37SAlexandre Belloni 		}
1727dc96ee37SAlexandre Belloni 	}
1728dc96ee37SAlexandre Belloni 
1729a556c76aSAlexandre Belloni 	if (netif_is_lag_master(dev)) {
1730a556c76aSAlexandre Belloni 		struct net_device *slave;
1731a556c76aSAlexandre Belloni 		struct list_head *iter;
1732a556c76aSAlexandre Belloni 
1733a556c76aSAlexandre Belloni 		netdev_for_each_lower_dev(dev, slave, iter) {
1734a556c76aSAlexandre Belloni 			ret = ocelot_netdevice_port_event(slave, event, info);
1735a556c76aSAlexandre Belloni 			if (ret)
1736a556c76aSAlexandre Belloni 				goto notify;
1737a556c76aSAlexandre Belloni 		}
1738a556c76aSAlexandre Belloni 	} else {
1739a556c76aSAlexandre Belloni 		ret = ocelot_netdevice_port_event(dev, event, info);
1740a556c76aSAlexandre Belloni 	}
1741a556c76aSAlexandre Belloni 
1742a556c76aSAlexandre Belloni notify:
1743a556c76aSAlexandre Belloni 	return notifier_from_errno(ret);
1744a556c76aSAlexandre Belloni }
1745a556c76aSAlexandre Belloni 
1746a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = {
1747a556c76aSAlexandre Belloni 	.notifier_call = ocelot_netdevice_event,
1748a556c76aSAlexandre Belloni };
1749a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb);
1750a556c76aSAlexandre Belloni 
175156da64bcSFlorian Fainelli static int ocelot_switchdev_event(struct notifier_block *unused,
175256da64bcSFlorian Fainelli 				  unsigned long event, void *ptr)
175356da64bcSFlorian Fainelli {
175456da64bcSFlorian Fainelli 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
175556da64bcSFlorian Fainelli 	int err;
175656da64bcSFlorian Fainelli 
175756da64bcSFlorian Fainelli 	switch (event) {
175856da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
175956da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
176056da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
176156da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
176256da64bcSFlorian Fainelli 		return notifier_from_errno(err);
176356da64bcSFlorian Fainelli 	}
176456da64bcSFlorian Fainelli 
176556da64bcSFlorian Fainelli 	return NOTIFY_DONE;
176656da64bcSFlorian Fainelli }
176756da64bcSFlorian Fainelli 
176856da64bcSFlorian Fainelli struct notifier_block ocelot_switchdev_nb __read_mostly = {
176956da64bcSFlorian Fainelli 	.notifier_call = ocelot_switchdev_event,
177056da64bcSFlorian Fainelli };
177156da64bcSFlorian Fainelli EXPORT_SYMBOL(ocelot_switchdev_nb);
177256da64bcSFlorian Fainelli 
17730e332c85SPetr Machata static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
17740e332c85SPetr Machata 					   unsigned long event, void *ptr)
17750e332c85SPetr Machata {
17760e332c85SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
17770e332c85SPetr Machata 	int err;
17780e332c85SPetr Machata 
17790e332c85SPetr Machata 	switch (event) {
17800e332c85SPetr Machata 		/* Blocking events. */
17810e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
17820e332c85SPetr Machata 		err = switchdev_handle_port_obj_add(dev, ptr,
17830e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
17840e332c85SPetr Machata 						    ocelot_port_obj_add);
17850e332c85SPetr Machata 		return notifier_from_errno(err);
17860e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
17870e332c85SPetr Machata 		err = switchdev_handle_port_obj_del(dev, ptr,
17880e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
17890e332c85SPetr Machata 						    ocelot_port_obj_del);
17900e332c85SPetr Machata 		return notifier_from_errno(err);
179156da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
179256da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
179356da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
179456da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
179556da64bcSFlorian Fainelli 		return notifier_from_errno(err);
17960e332c85SPetr Machata 	}
17970e332c85SPetr Machata 
17980e332c85SPetr Machata 	return NOTIFY_DONE;
17990e332c85SPetr Machata }
18000e332c85SPetr Machata 
18010e332c85SPetr Machata struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
18020e332c85SPetr Machata 	.notifier_call = ocelot_switchdev_blocking_event,
18030e332c85SPetr Machata };
18040e332c85SPetr Machata EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
18050e332c85SPetr Machata 
18064e3b0468SAntoine Tenart int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
18074e3b0468SAntoine Tenart {
18084e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
18094e3b0468SAntoine Tenart 	unsigned long flags;
18104e3b0468SAntoine Tenart 	time64_t s;
18114e3b0468SAntoine Tenart 	u32 val;
18124e3b0468SAntoine Tenart 	s64 ns;
18134e3b0468SAntoine Tenart 
18144e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
18154e3b0468SAntoine Tenart 
18164e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18174e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18184e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
18194e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18204e3b0468SAntoine Tenart 
18214e3b0468SAntoine Tenart 	s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
18224e3b0468SAntoine Tenart 	s <<= 32;
18234e3b0468SAntoine Tenart 	s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
18244e3b0468SAntoine Tenart 	ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
18254e3b0468SAntoine Tenart 
18264e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
18274e3b0468SAntoine Tenart 
18284e3b0468SAntoine Tenart 	/* Deal with negative values */
18294e3b0468SAntoine Tenart 	if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
18304e3b0468SAntoine Tenart 		s--;
18314e3b0468SAntoine Tenart 		ns &= 0xf;
18324e3b0468SAntoine Tenart 		ns += 999999984;
18334e3b0468SAntoine Tenart 	}
18344e3b0468SAntoine Tenart 
18354e3b0468SAntoine Tenart 	set_normalized_timespec64(ts, s, ns);
18364e3b0468SAntoine Tenart 	return 0;
18374e3b0468SAntoine Tenart }
18384e3b0468SAntoine Tenart EXPORT_SYMBOL(ocelot_ptp_gettime64);
18394e3b0468SAntoine Tenart 
18404e3b0468SAntoine Tenart static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
18414e3b0468SAntoine Tenart 				const struct timespec64 *ts)
18424e3b0468SAntoine Tenart {
18434e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
18444e3b0468SAntoine Tenart 	unsigned long flags;
18454e3b0468SAntoine Tenart 	u32 val;
18464e3b0468SAntoine Tenart 
18474e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
18484e3b0468SAntoine Tenart 
18494e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18504e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18514e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
18524e3b0468SAntoine Tenart 
18534e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18544e3b0468SAntoine Tenart 
18554e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
18564e3b0468SAntoine Tenart 			 TOD_ACC_PIN);
18574e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
18584e3b0468SAntoine Tenart 			 TOD_ACC_PIN);
18594e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
18604e3b0468SAntoine Tenart 
18614e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18624e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18634e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
18644e3b0468SAntoine Tenart 
18654e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18664e3b0468SAntoine Tenart 
18674e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
18684e3b0468SAntoine Tenart 	return 0;
18694e3b0468SAntoine Tenart }
18704e3b0468SAntoine Tenart 
18714e3b0468SAntoine Tenart static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
18724e3b0468SAntoine Tenart {
18734e3b0468SAntoine Tenart 	if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
18744e3b0468SAntoine Tenart 		struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
18754e3b0468SAntoine Tenart 		unsigned long flags;
18764e3b0468SAntoine Tenart 		u32 val;
18774e3b0468SAntoine Tenart 
18784e3b0468SAntoine Tenart 		spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
18794e3b0468SAntoine Tenart 
18804e3b0468SAntoine Tenart 		val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18814e3b0468SAntoine Tenart 		val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18824e3b0468SAntoine Tenart 		val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
18834e3b0468SAntoine Tenart 
18844e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18854e3b0468SAntoine Tenart 
18864e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
18874e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
18884e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
18894e3b0468SAntoine Tenart 
18904e3b0468SAntoine Tenart 		val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18914e3b0468SAntoine Tenart 		val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18924e3b0468SAntoine Tenart 		val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
18934e3b0468SAntoine Tenart 
18944e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18954e3b0468SAntoine Tenart 
18964e3b0468SAntoine Tenart 		spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
18974e3b0468SAntoine Tenart 	} else {
18984e3b0468SAntoine Tenart 		/* Fall back using ocelot_ptp_settime64 which is not exact. */
18994e3b0468SAntoine Tenart 		struct timespec64 ts;
19004e3b0468SAntoine Tenart 		u64 now;
19014e3b0468SAntoine Tenart 
19024e3b0468SAntoine Tenart 		ocelot_ptp_gettime64(ptp, &ts);
19034e3b0468SAntoine Tenart 
19044e3b0468SAntoine Tenart 		now = ktime_to_ns(timespec64_to_ktime(ts));
19054e3b0468SAntoine Tenart 		ts = ns_to_timespec64(now + delta);
19064e3b0468SAntoine Tenart 
19074e3b0468SAntoine Tenart 		ocelot_ptp_settime64(ptp, &ts);
19084e3b0468SAntoine Tenart 	}
19094e3b0468SAntoine Tenart 	return 0;
19104e3b0468SAntoine Tenart }
19114e3b0468SAntoine Tenart 
19124e3b0468SAntoine Tenart static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
19134e3b0468SAntoine Tenart {
19144e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
19154e3b0468SAntoine Tenart 	u32 unit = 0, direction = 0;
19164e3b0468SAntoine Tenart 	unsigned long flags;
19174e3b0468SAntoine Tenart 	u64 adj = 0;
19184e3b0468SAntoine Tenart 
19194e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
19204e3b0468SAntoine Tenart 
19214e3b0468SAntoine Tenart 	if (!scaled_ppm)
19224e3b0468SAntoine Tenart 		goto disable_adj;
19234e3b0468SAntoine Tenart 
19244e3b0468SAntoine Tenart 	if (scaled_ppm < 0) {
19254e3b0468SAntoine Tenart 		direction = PTP_CFG_CLK_ADJ_CFG_DIR;
19264e3b0468SAntoine Tenart 		scaled_ppm = -scaled_ppm;
19274e3b0468SAntoine Tenart 	}
19284e3b0468SAntoine Tenart 
19294e3b0468SAntoine Tenart 	adj = PSEC_PER_SEC << 16;
19304e3b0468SAntoine Tenart 	do_div(adj, scaled_ppm);
19314e3b0468SAntoine Tenart 	do_div(adj, 1000);
19324e3b0468SAntoine Tenart 
19334e3b0468SAntoine Tenart 	/* If the adjustment value is too large, use ns instead */
19344e3b0468SAntoine Tenart 	if (adj >= (1L << 30)) {
19354e3b0468SAntoine Tenart 		unit = PTP_CFG_CLK_ADJ_FREQ_NS;
19364e3b0468SAntoine Tenart 		do_div(adj, 1000);
19374e3b0468SAntoine Tenart 	}
19384e3b0468SAntoine Tenart 
19394e3b0468SAntoine Tenart 	/* Still too big */
19404e3b0468SAntoine Tenart 	if (adj >= (1L << 30))
19414e3b0468SAntoine Tenart 		goto disable_adj;
19424e3b0468SAntoine Tenart 
19434e3b0468SAntoine Tenart 	ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
19444e3b0468SAntoine Tenart 	ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
19454e3b0468SAntoine Tenart 		     PTP_CLK_CFG_ADJ_CFG);
19464e3b0468SAntoine Tenart 
19474e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19484e3b0468SAntoine Tenart 	return 0;
19494e3b0468SAntoine Tenart 
19504e3b0468SAntoine Tenart disable_adj:
19514e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
19524e3b0468SAntoine Tenart 
19534e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19544e3b0468SAntoine Tenart 	return 0;
19554e3b0468SAntoine Tenart }
19564e3b0468SAntoine Tenart 
19574e3b0468SAntoine Tenart static struct ptp_clock_info ocelot_ptp_clock_info = {
19584e3b0468SAntoine Tenart 	.owner		= THIS_MODULE,
19594e3b0468SAntoine Tenart 	.name		= "ocelot ptp",
19604e3b0468SAntoine Tenart 	.max_adj	= 0x7fffffff,
19614e3b0468SAntoine Tenart 	.n_alarm	= 0,
19624e3b0468SAntoine Tenart 	.n_ext_ts	= 0,
19634e3b0468SAntoine Tenart 	.n_per_out	= 0,
19644e3b0468SAntoine Tenart 	.n_pins		= 0,
19654e3b0468SAntoine Tenart 	.pps		= 0,
19664e3b0468SAntoine Tenart 	.gettime64	= ocelot_ptp_gettime64,
19674e3b0468SAntoine Tenart 	.settime64	= ocelot_ptp_settime64,
19684e3b0468SAntoine Tenart 	.adjtime	= ocelot_ptp_adjtime,
19694e3b0468SAntoine Tenart 	.adjfine	= ocelot_ptp_adjfine,
19704e3b0468SAntoine Tenart };
19714e3b0468SAntoine Tenart 
19724e3b0468SAntoine Tenart static int ocelot_init_timestamp(struct ocelot *ocelot)
19734e3b0468SAntoine Tenart {
19744e3b0468SAntoine Tenart 	ocelot->ptp_info = ocelot_ptp_clock_info;
19754e3b0468SAntoine Tenart 	ocelot->ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
19764e3b0468SAntoine Tenart 	if (IS_ERR(ocelot->ptp_clock))
19774e3b0468SAntoine Tenart 		return PTR_ERR(ocelot->ptp_clock);
19784e3b0468SAntoine Tenart 	/* Check if PHC support is missing at the configuration level */
19794e3b0468SAntoine Tenart 	if (!ocelot->ptp_clock)
19804e3b0468SAntoine Tenart 		return 0;
19814e3b0468SAntoine Tenart 
19824e3b0468SAntoine Tenart 	ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
19834e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
19844e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
19854e3b0468SAntoine Tenart 
19864e3b0468SAntoine Tenart 	ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
19874e3b0468SAntoine Tenart 
19884e3b0468SAntoine Tenart 	/* There is no device reconfiguration, PTP Rx stamping is always
19894e3b0468SAntoine Tenart 	 * enabled.
19904e3b0468SAntoine Tenart 	 */
19914e3b0468SAntoine Tenart 	ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
19924e3b0468SAntoine Tenart 
19934e3b0468SAntoine Tenart 	return 0;
19944e3b0468SAntoine Tenart }
19954e3b0468SAntoine Tenart 
1996a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port,
1997a556c76aSAlexandre Belloni 		      void __iomem *regs,
1998a556c76aSAlexandre Belloni 		      struct phy_device *phy)
1999a556c76aSAlexandre Belloni {
2000a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port;
2001a556c76aSAlexandre Belloni 	struct net_device *dev;
2002a556c76aSAlexandre Belloni 	int err;
2003a556c76aSAlexandre Belloni 
2004a556c76aSAlexandre Belloni 	dev = alloc_etherdev(sizeof(struct ocelot_port));
2005a556c76aSAlexandre Belloni 	if (!dev)
2006a556c76aSAlexandre Belloni 		return -ENOMEM;
2007a556c76aSAlexandre Belloni 	SET_NETDEV_DEV(dev, ocelot->dev);
2008a556c76aSAlexandre Belloni 	ocelot_port = netdev_priv(dev);
2009a556c76aSAlexandre Belloni 	ocelot_port->dev = dev;
2010a556c76aSAlexandre Belloni 	ocelot_port->ocelot = ocelot;
2011a556c76aSAlexandre Belloni 	ocelot_port->regs = regs;
2012a556c76aSAlexandre Belloni 	ocelot_port->chip_port = port;
2013a556c76aSAlexandre Belloni 	ocelot_port->phy = phy;
2014a556c76aSAlexandre Belloni 	ocelot->ports[port] = ocelot_port;
2015a556c76aSAlexandre Belloni 
2016a556c76aSAlexandre Belloni 	dev->netdev_ops = &ocelot_port_netdev_ops;
2017a556c76aSAlexandre Belloni 	dev->ethtool_ops = &ocelot_ethtool_ops;
2018a556c76aSAlexandre Belloni 
20192c1d029aSJoergen Andreasen 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
20202c1d029aSJoergen Andreasen 		NETIF_F_HW_TC;
20212c1d029aSJoergen Andreasen 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
20227142529fSAntoine Tenart 
2023a556c76aSAlexandre Belloni 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2024a556c76aSAlexandre Belloni 	dev->dev_addr[ETH_ALEN - 1] += port;
2025a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2026a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
2027a556c76aSAlexandre Belloni 
20284e3b0468SAntoine Tenart 	INIT_LIST_HEAD(&ocelot_port->skbs);
20294e3b0468SAntoine Tenart 
2030a556c76aSAlexandre Belloni 	err = register_netdev(dev);
2031a556c76aSAlexandre Belloni 	if (err) {
2032a556c76aSAlexandre Belloni 		dev_err(ocelot->dev, "register_netdev failed\n");
2033a556c76aSAlexandre Belloni 		goto err_register_netdev;
2034a556c76aSAlexandre Belloni 	}
2035a556c76aSAlexandre Belloni 
20367142529fSAntoine Tenart 	/* Basic L2 initialization */
20377142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, ocelot_port);
20387142529fSAntoine Tenart 
2039b5962294SHoratiu Vultur 	/* Enable vcap lookups */
2040b5962294SHoratiu Vultur 	ocelot_vcap_enable(ocelot, ocelot_port);
2041b5962294SHoratiu Vultur 
2042a556c76aSAlexandre Belloni 	return 0;
2043a556c76aSAlexandre Belloni 
2044a556c76aSAlexandre Belloni err_register_netdev:
2045a556c76aSAlexandre Belloni 	free_netdev(dev);
2046a556c76aSAlexandre Belloni 	return err;
2047a556c76aSAlexandre Belloni }
2048a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port);
2049a556c76aSAlexandre Belloni 
2050a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2051a556c76aSAlexandre Belloni {
2052a556c76aSAlexandre Belloni 	u32 port;
20534e3b0468SAntoine Tenart 	int i, ret, cpu = ocelot->num_phys_ports;
2054a556c76aSAlexandre Belloni 	char queue_name[32];
2055a556c76aSAlexandre Belloni 
2056dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2057dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
2058dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
2059dc96ee37SAlexandre Belloni 		return -ENOMEM;
2060dc96ee37SAlexandre Belloni 
2061a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2062a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2063a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2064a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2065a556c76aSAlexandre Belloni 		return -ENOMEM;
2066a556c76aSAlexandre Belloni 
2067a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
20684e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
20694e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
2070a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2071a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2072a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2073a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2074a556c76aSAlexandre Belloni 		return -ENOMEM;
2075a556c76aSAlexandre Belloni 
2076a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2077a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2078b5962294SHoratiu Vultur 	ocelot_ace_init(ocelot);
2079a556c76aSAlexandre Belloni 
2080a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2081a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2082a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2083a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2084a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2085a556c76aSAlexandre Belloni 	}
2086a556c76aSAlexandre Belloni 
2087a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2088a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2089a556c76aSAlexandre Belloni 
2090a556c76aSAlexandre Belloni 	/* Aggregation mode */
2091a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2092a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2093a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2094a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2095a556c76aSAlexandre Belloni 
2096a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2097a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2098a556c76aSAlexandre Belloni 	 */
2099a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2100a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2101a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2102a556c76aSAlexandre Belloni 
2103a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2104a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2105a556c76aSAlexandre Belloni 
2106a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2107a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2108a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2109a556c76aSAlexandre Belloni 
2110a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2111a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2112a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2113a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2114a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
2115a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2116a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2117a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2118a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2119a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2120a556c76aSAlexandre Belloni 
2121a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2122a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2123a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2124a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2125a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2126a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2127a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2128a556c76aSAlexandre Belloni 				 port);
2129a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2130a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2131a556c76aSAlexandre Belloni 	}
2132a556c76aSAlexandre Belloni 
2133a556c76aSAlexandre Belloni 	/* Configure and enable the CPU port. */
2134a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2135a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2136a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2137a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2138a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, cpu);
2139a556c76aSAlexandre Belloni 
2140a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
2141a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2142a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2143a556c76aSAlexandre Belloni 
2144a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2145a556c76aSAlexandre Belloni 	}
2146a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
2147a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2148a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
2149a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2150a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2151a556c76aSAlexandre Belloni 
2152a556c76aSAlexandre Belloni 	/* CPU port Injection/Extraction configuration */
2153a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2154a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2155a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2156a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, cpu);
2157a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
2158a556c76aSAlexandre Belloni 			 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
2159a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2160a556c76aSAlexandre Belloni 	 * registers endianness.
2161a556c76aSAlexandre Belloni 	 */
2162a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2163a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2164a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2165a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2166a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2167a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2168a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2169a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2170a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2171a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2172a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2173a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2174a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2175a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2176a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2177a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2178a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2179a556c76aSAlexandre Belloni 
21801e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2181a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2182a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
21834e3b0468SAntoine Tenart 
21844e3b0468SAntoine Tenart 	if (ocelot->ptp) {
21854e3b0468SAntoine Tenart 		ret = ocelot_init_timestamp(ocelot);
21864e3b0468SAntoine Tenart 		if (ret) {
21874e3b0468SAntoine Tenart 			dev_err(ocelot->dev,
21884e3b0468SAntoine Tenart 				"Timestamp initialization failed\n");
21894e3b0468SAntoine Tenart 			return ret;
21904e3b0468SAntoine Tenart 		}
21914e3b0468SAntoine Tenart 	}
21924e3b0468SAntoine Tenart 
2193a556c76aSAlexandre Belloni 	return 0;
2194a556c76aSAlexandre Belloni }
2195a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2196a556c76aSAlexandre Belloni 
2197a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2198a556c76aSAlexandre Belloni {
21994e3b0468SAntoine Tenart 	struct list_head *pos, *tmp;
22004e3b0468SAntoine Tenart 	struct ocelot_port *port;
22014e3b0468SAntoine Tenart 	struct ocelot_skb *entry;
22024e3b0468SAntoine Tenart 	int i;
22034e3b0468SAntoine Tenart 
2204c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2205a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2206a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2207b5962294SHoratiu Vultur 	ocelot_ace_deinit();
22084e3b0468SAntoine Tenart 
22094e3b0468SAntoine Tenart 	for (i = 0; i < ocelot->num_phys_ports; i++) {
22104e3b0468SAntoine Tenart 		port = ocelot->ports[i];
22114e3b0468SAntoine Tenart 
22124e3b0468SAntoine Tenart 		list_for_each_safe(pos, tmp, &port->skbs) {
22134e3b0468SAntoine Tenart 			entry = list_entry(pos, struct ocelot_skb, head);
22144e3b0468SAntoine Tenart 
22154e3b0468SAntoine Tenart 			list_del(pos);
22164e3b0468SAntoine Tenart 			dev_kfree_skb_any(entry->skb);
22174e3b0468SAntoine Tenart 			kfree(entry);
22184e3b0468SAntoine Tenart 		}
22194e3b0468SAntoine Tenart 	}
2220a556c76aSAlexandre Belloni }
2221a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2222a556c76aSAlexandre Belloni 
2223a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2224