xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 7afb3e575e5aa9f5a200a3eb3f45d8130f6d6601)
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 */
264b9cd75e6SVladimir Oltean 	if (untagged && port->vid != vid) {
265b9cd75e6SVladimir Oltean 		if (port->vid) {
266b9cd75e6SVladimir Oltean 			dev_err(ocelot->dev,
267b9cd75e6SVladimir Oltean 				"Port already has a native VLAN: %d\n",
268b9cd75e6SVladimir Oltean 				port->vid);
269b9cd75e6SVladimir Oltean 			return -EBUSY;
270b9cd75e6SVladimir Oltean 		}
2717142529fSAntoine Tenart 		port->vid = vid;
272b9cd75e6SVladimir Oltean 	}
2737142529fSAntoine Tenart 
2747142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
2757142529fSAntoine Tenart 
2767142529fSAntoine Tenart 	return 0;
2777142529fSAntoine Tenart }
2787142529fSAntoine Tenart 
2797142529fSAntoine Tenart static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
2807142529fSAntoine Tenart {
2817142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
2827142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
2837142529fSAntoine Tenart 	int ret;
2847142529fSAntoine Tenart 
2857142529fSAntoine Tenart 	/* 8021q removes VID 0 on module unload for all interfaces
2867142529fSAntoine Tenart 	 * with VLAN filtering feature. We need to keep it to receive
2877142529fSAntoine Tenart 	 * untagged traffic.
2887142529fSAntoine Tenart 	 */
2897142529fSAntoine Tenart 	if (vid == 0)
2907142529fSAntoine Tenart 		return 0;
2917142529fSAntoine Tenart 
2927142529fSAntoine Tenart 	/* Del the port MAC address to with the right VLAN information */
2937142529fSAntoine Tenart 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
2947142529fSAntoine Tenart 
2957142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
2967142529fSAntoine Tenart 	ocelot->vlan_mask[vid] &= ~BIT(port->chip_port);
2977142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2987142529fSAntoine Tenart 	if (ret)
2997142529fSAntoine Tenart 		return ret;
3007142529fSAntoine Tenart 
3017142529fSAntoine Tenart 	/* Ingress */
3027142529fSAntoine Tenart 	if (port->pvid == vid)
3037142529fSAntoine Tenart 		port->pvid = 0;
3047142529fSAntoine Tenart 
3057142529fSAntoine Tenart 	/* Egress */
3067142529fSAntoine Tenart 	if (port->vid == vid)
3077142529fSAntoine Tenart 		port->vid = 0;
3087142529fSAntoine Tenart 
3097142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
3107142529fSAntoine Tenart 
3117142529fSAntoine Tenart 	return 0;
3127142529fSAntoine Tenart }
3137142529fSAntoine Tenart 
314a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
315a556c76aSAlexandre Belloni {
3167142529fSAntoine Tenart 	u16 port, vid;
3177142529fSAntoine Tenart 
318a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
319a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
320a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
321a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3227142529fSAntoine Tenart 
3237142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3247142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3257142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3267142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3277142529fSAntoine Tenart 	}
3287142529fSAntoine Tenart 
3297142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3307142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3317142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3327142529fSAntoine Tenart 	 */
3337142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3347142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3357142529fSAntoine Tenart 
3367142529fSAntoine Tenart 	/* Configure the CPU port to be VLAN aware */
3377142529fSAntoine Tenart 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
3387142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
3397142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
3407142529fSAntoine Tenart 			 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
3417142529fSAntoine Tenart 
3427142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3437142529fSAntoine Tenart 	 * default.
3447142529fSAntoine Tenart 	 */
3457142529fSAntoine Tenart 	ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
3467142529fSAntoine Tenart 
3477142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3487142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3497142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3507142529fSAntoine Tenart 	}
351a556c76aSAlexandre Belloni }
352a556c76aSAlexandre Belloni 
353a556c76aSAlexandre Belloni /* Watermark encode
354a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
355a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
356a556c76aSAlexandre Belloni  */
357a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
358a556c76aSAlexandre Belloni {
359a556c76aSAlexandre Belloni 	if (value >= BIT(8))
360a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
361a556c76aSAlexandre Belloni 
362a556c76aSAlexandre Belloni 	return value;
363a556c76aSAlexandre Belloni }
364a556c76aSAlexandre Belloni 
365a556c76aSAlexandre Belloni static void ocelot_port_adjust_link(struct net_device *dev)
366a556c76aSAlexandre Belloni {
367a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
368a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
369a556c76aSAlexandre Belloni 	u8 p = port->chip_port;
370a556c76aSAlexandre Belloni 	int speed, atop_wm, mode = 0;
371a556c76aSAlexandre Belloni 
372a556c76aSAlexandre Belloni 	switch (dev->phydev->speed) {
373a556c76aSAlexandre Belloni 	case SPEED_10:
374a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
375a556c76aSAlexandre Belloni 		break;
376a556c76aSAlexandre Belloni 	case SPEED_100:
377a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
378a556c76aSAlexandre Belloni 		break;
379a556c76aSAlexandre Belloni 	case SPEED_1000:
380a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
381a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
382a556c76aSAlexandre Belloni 		break;
383a556c76aSAlexandre Belloni 	case SPEED_2500:
384a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
385a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
386a556c76aSAlexandre Belloni 		break;
387a556c76aSAlexandre Belloni 	default:
388a556c76aSAlexandre Belloni 		netdev_err(dev, "Unsupported PHY speed: %d\n",
389a556c76aSAlexandre Belloni 			   dev->phydev->speed);
390a556c76aSAlexandre Belloni 		return;
391a556c76aSAlexandre Belloni 	}
392a556c76aSAlexandre Belloni 
393a556c76aSAlexandre Belloni 	phy_print_status(dev->phydev);
394a556c76aSAlexandre Belloni 
395a556c76aSAlexandre Belloni 	if (!dev->phydev->link)
396a556c76aSAlexandre Belloni 		return;
397a556c76aSAlexandre Belloni 
398a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
399a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
400a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
401a556c76aSAlexandre Belloni 
402a556c76aSAlexandre Belloni 	/* Set MAC IFG Gaps
403a556c76aSAlexandre Belloni 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
404a556c76aSAlexandre Belloni 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
405a556c76aSAlexandre Belloni 	 */
406a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
407a556c76aSAlexandre Belloni 
408a556c76aSAlexandre Belloni 	/* Load seed (0) and set MAC HDX late collision  */
409a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
410a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG_SEED_LOAD,
411a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
412a556c76aSAlexandre Belloni 	mdelay(1);
413a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
414a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
415a556c76aSAlexandre Belloni 
416a556c76aSAlexandre Belloni 	/* Disable HDX fast control */
417a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
418a556c76aSAlexandre Belloni 
419a556c76aSAlexandre Belloni 	/* SGMII only for now */
420a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
421a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
422a556c76aSAlexandre Belloni 
423a556c76aSAlexandre Belloni 	/* Enable PCS */
424a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
425a556c76aSAlexandre Belloni 
426a556c76aSAlexandre Belloni 	/* No aneg on SGMII */
427a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
428a556c76aSAlexandre Belloni 
429a556c76aSAlexandre Belloni 	/* No loopback */
430a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_LB_CFG);
431a556c76aSAlexandre Belloni 
432a556c76aSAlexandre Belloni 	/* Set Max Length and maximum tags allowed */
433a556c76aSAlexandre Belloni 	ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
434a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
435a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
436a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
437a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG);
438a556c76aSAlexandre Belloni 
439a556c76aSAlexandre Belloni 	/* Enable MAC module */
440a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
441a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
442a556c76aSAlexandre Belloni 
443a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
444a556c76aSAlexandre Belloni 	 * reset */
445a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
446a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
447a556c76aSAlexandre Belloni 
448a556c76aSAlexandre Belloni 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
449a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
450a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
451a556c76aSAlexandre Belloni 
452a556c76aSAlexandre Belloni 	/* No PFC */
453a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
454a556c76aSAlexandre Belloni 			 ANA_PFC_PFC_CFG, p);
455a556c76aSAlexandre Belloni 
456a556c76aSAlexandre Belloni 	/* Set Pause WM hysteresis
457a556c76aSAlexandre Belloni 	 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
458a556c76aSAlexandre Belloni 	 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
459a556c76aSAlexandre Belloni 	 */
460a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
461a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
462a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
463a556c76aSAlexandre Belloni 
464a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
465a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
466a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
467a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
468a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, p);
469a556c76aSAlexandre Belloni 
470a556c76aSAlexandre Belloni 	/* Flow control */
471a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
472a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
473a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
474a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
475a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
476a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG, p);
477a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
478a556c76aSAlexandre Belloni 
479a556c76aSAlexandre Belloni 	/* Tail dropping watermark */
480a556c76aSAlexandre Belloni 	atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
481a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
482a556c76aSAlexandre Belloni 			 SYS_ATOP, p);
483a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
484a556c76aSAlexandre Belloni }
485a556c76aSAlexandre Belloni 
486a556c76aSAlexandre Belloni static int ocelot_port_open(struct net_device *dev)
487a556c76aSAlexandre Belloni {
488a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
489a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
490a556c76aSAlexandre Belloni 	int err;
491a556c76aSAlexandre Belloni 
492a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
493a556c76aSAlexandre Belloni 	 * MAC addresses.
494a556c76aSAlexandre Belloni 	 */
495a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
496a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
497a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
498a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, port->chip_port);
499a556c76aSAlexandre Belloni 
50071e32a20SQuentin Schulz 	if (port->serdes) {
501c8fe6d7fSGrygorii Strashko 		err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
502c8fe6d7fSGrygorii Strashko 				       port->phy_mode);
50371e32a20SQuentin Schulz 		if (err) {
50471e32a20SQuentin Schulz 			netdev_err(dev, "Could not set mode of SerDes\n");
50571e32a20SQuentin Schulz 			return err;
50671e32a20SQuentin Schulz 		}
50771e32a20SQuentin Schulz 	}
50871e32a20SQuentin Schulz 
509a556c76aSAlexandre Belloni 	err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
51071e32a20SQuentin Schulz 				 port->phy_mode);
511a556c76aSAlexandre Belloni 	if (err) {
512a556c76aSAlexandre Belloni 		netdev_err(dev, "Could not attach to PHY\n");
513a556c76aSAlexandre Belloni 		return err;
514a556c76aSAlexandre Belloni 	}
515a556c76aSAlexandre Belloni 
516a556c76aSAlexandre Belloni 	dev->phydev = port->phy;
517a556c76aSAlexandre Belloni 
518a556c76aSAlexandre Belloni 	phy_attached_info(port->phy);
519a556c76aSAlexandre Belloni 	phy_start(port->phy);
520a556c76aSAlexandre Belloni 	return 0;
521a556c76aSAlexandre Belloni }
522a556c76aSAlexandre Belloni 
523a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev)
524a556c76aSAlexandre Belloni {
525a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
526a556c76aSAlexandre Belloni 
527a556c76aSAlexandre Belloni 	phy_disconnect(port->phy);
528a556c76aSAlexandre Belloni 
529a556c76aSAlexandre Belloni 	dev->phydev = NULL;
530a556c76aSAlexandre Belloni 
531a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
532a556c76aSAlexandre Belloni 	ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
533a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, port->chip_port);
534a556c76aSAlexandre Belloni 	return 0;
535a556c76aSAlexandre Belloni }
536a556c76aSAlexandre Belloni 
537a556c76aSAlexandre Belloni /* Generate the IFH for frame injection
538a556c76aSAlexandre Belloni  *
539a556c76aSAlexandre Belloni  * The IFH is a 128bit-value
540a556c76aSAlexandre Belloni  * bit 127: bypass the analyzer processing
541a556c76aSAlexandre Belloni  * bit 56-67: destination mask
542a556c76aSAlexandre Belloni  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
543a556c76aSAlexandre Belloni  * bit 20-27: cpu extraction queue mask
544a556c76aSAlexandre Belloni  * bit 16: tag type 0: C-tag, 1: S-tag
545a556c76aSAlexandre Belloni  * bit 0-11: VID
546a556c76aSAlexandre Belloni  */
547a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
548a556c76aSAlexandre Belloni {
5494e3b0468SAntoine Tenart 	ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
55008d02364SAntoine Tenart 	ifh[1] = (0xf00 & info->port) >> 8;
551a556c76aSAlexandre Belloni 	ifh[2] = (0xff & info->port) << 24;
55208d02364SAntoine Tenart 	ifh[3] = (info->tag_type << 16) | info->vid;
553a556c76aSAlexandre Belloni 
554a556c76aSAlexandre Belloni 	return 0;
555a556c76aSAlexandre Belloni }
556a556c76aSAlexandre Belloni 
557a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
558a556c76aSAlexandre Belloni {
5594e3b0468SAntoine Tenart 	struct skb_shared_info *shinfo = skb_shinfo(skb);
560a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
561a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
562a556c76aSAlexandre Belloni 	u32 val, ifh[IFH_LEN];
563a556c76aSAlexandre Belloni 	struct frame_info info = {};
564a556c76aSAlexandre Belloni 	u8 grp = 0; /* Send everything on CPU group 0 */
565a556c76aSAlexandre Belloni 	unsigned int i, count, last;
566a556c76aSAlexandre Belloni 
567a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, QS_INJ_STATUS);
568a556c76aSAlexandre Belloni 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
569a556c76aSAlexandre Belloni 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
570a556c76aSAlexandre Belloni 		return NETDEV_TX_BUSY;
571a556c76aSAlexandre Belloni 
572a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
573a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
574a556c76aSAlexandre Belloni 
575a556c76aSAlexandre Belloni 	info.port = BIT(port->chip_port);
57608d02364SAntoine Tenart 	info.tag_type = IFH_TAG_TYPE_C;
57708d02364SAntoine Tenart 	info.vid = skb_vlan_tag_get(skb);
5784e3b0468SAntoine Tenart 
5794e3b0468SAntoine Tenart 	/* Check if timestamping is needed */
5804e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
5814e3b0468SAntoine Tenart 		info.rew_op = port->ptp_cmd;
5824e3b0468SAntoine Tenart 		if (port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
5834e3b0468SAntoine Tenart 			info.rew_op |= (port->ts_id  % 4) << 3;
5844e3b0468SAntoine Tenart 	}
5854e3b0468SAntoine Tenart 
586a556c76aSAlexandre Belloni 	ocelot_gen_ifh(ifh, &info);
587a556c76aSAlexandre Belloni 
588a556c76aSAlexandre Belloni 	for (i = 0; i < IFH_LEN; i++)
589c2cd650bSAntoine Tenart 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
590c2cd650bSAntoine Tenart 				 QS_INJ_WR, grp);
591a556c76aSAlexandre Belloni 
592a556c76aSAlexandre Belloni 	count = (skb->len + 3) / 4;
593a556c76aSAlexandre Belloni 	last = skb->len % 4;
594a556c76aSAlexandre Belloni 	for (i = 0; i < count; i++) {
595a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
596a556c76aSAlexandre Belloni 	}
597a556c76aSAlexandre Belloni 
598a556c76aSAlexandre Belloni 	/* Add padding */
599a556c76aSAlexandre Belloni 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
600a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
601a556c76aSAlexandre Belloni 		i++;
602a556c76aSAlexandre Belloni 	}
603a556c76aSAlexandre Belloni 
604a556c76aSAlexandre Belloni 	/* Indicate EOF and valid bytes in last word */
605a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
606a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
607a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_EOF,
608a556c76aSAlexandre Belloni 			 QS_INJ_CTRL, grp);
609a556c76aSAlexandre Belloni 
610a556c76aSAlexandre Belloni 	/* Add dummy CRC */
611a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
612a556c76aSAlexandre Belloni 	skb_tx_timestamp(skb);
613a556c76aSAlexandre Belloni 
614a556c76aSAlexandre Belloni 	dev->stats.tx_packets++;
615a556c76aSAlexandre Belloni 	dev->stats.tx_bytes += skb->len;
6164e3b0468SAntoine Tenart 
6174e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
6184e3b0468SAntoine Tenart 	    port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
6194e3b0468SAntoine Tenart 		struct ocelot_skb *oskb =
6204e3b0468SAntoine Tenart 			kzalloc(sizeof(struct ocelot_skb), GFP_ATOMIC);
6214e3b0468SAntoine Tenart 
6224e3b0468SAntoine Tenart 		if (unlikely(!oskb))
6234e3b0468SAntoine Tenart 			goto out;
6244e3b0468SAntoine Tenart 
6254e3b0468SAntoine Tenart 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
6264e3b0468SAntoine Tenart 
6274e3b0468SAntoine Tenart 		oskb->skb = skb;
6284e3b0468SAntoine Tenart 		oskb->id = port->ts_id % 4;
6294e3b0468SAntoine Tenart 		port->ts_id++;
6304e3b0468SAntoine Tenart 
6314e3b0468SAntoine Tenart 		list_add_tail(&oskb->head, &port->skbs);
632a556c76aSAlexandre Belloni 
633a556c76aSAlexandre Belloni 		return NETDEV_TX_OK;
634a556c76aSAlexandre Belloni 	}
635a556c76aSAlexandre Belloni 
6364e3b0468SAntoine Tenart out:
6374e3b0468SAntoine Tenart 	dev_kfree_skb_any(skb);
6384e3b0468SAntoine Tenart 	return NETDEV_TX_OK;
6394e3b0468SAntoine Tenart }
6404e3b0468SAntoine Tenart 
6414e3b0468SAntoine Tenart void ocelot_get_hwtimestamp(struct ocelot *ocelot, struct timespec64 *ts)
6424e3b0468SAntoine Tenart {
6434e3b0468SAntoine Tenart 	unsigned long flags;
6444e3b0468SAntoine Tenart 	u32 val;
6454e3b0468SAntoine Tenart 
6464e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
6474e3b0468SAntoine Tenart 
6484e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
6494e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
6504e3b0468SAntoine Tenart 
6514e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
6524e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
6534e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
6544e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
6554e3b0468SAntoine Tenart 
6564e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
6574e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
6584e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
6594e3b0468SAntoine Tenart 
6604e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
6614e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
6624e3b0468SAntoine Tenart 		ts->tv_sec--;
6634e3b0468SAntoine Tenart 
6644e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
6654e3b0468SAntoine Tenart }
6664e3b0468SAntoine Tenart EXPORT_SYMBOL(ocelot_get_hwtimestamp);
6674e3b0468SAntoine Tenart 
66840a1578dSClaudiu Manoil static int ocelot_mc_unsync(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_forget(port->ocelot, addr, port->pvid);
673a556c76aSAlexandre Belloni }
674a556c76aSAlexandre Belloni 
67540a1578dSClaudiu Manoil static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
676a556c76aSAlexandre Belloni {
67740a1578dSClaudiu Manoil 	struct ocelot_port *port = netdev_priv(dev);
678a556c76aSAlexandre Belloni 
67940a1578dSClaudiu Manoil 	return ocelot_mact_learn(port->ocelot, PGID_CPU, addr, port->pvid,
680a556c76aSAlexandre Belloni 				 ENTRYTYPE_LOCKED);
681a556c76aSAlexandre Belloni }
682a556c76aSAlexandre Belloni 
683a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev)
684a556c76aSAlexandre Belloni {
685a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
686a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
687a556c76aSAlexandre Belloni 	int i;
688a556c76aSAlexandre Belloni 	u32 val;
689a556c76aSAlexandre Belloni 
690a556c76aSAlexandre Belloni 	/* This doesn't handle promiscuous mode because the bridge core is
691a556c76aSAlexandre Belloni 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
692a556c76aSAlexandre Belloni 	 * forwarded to the CPU port.
693a556c76aSAlexandre Belloni 	 */
694a556c76aSAlexandre Belloni 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
695a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
696a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
697a556c76aSAlexandre Belloni 
69840a1578dSClaudiu Manoil 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
699a556c76aSAlexandre Belloni }
700a556c76aSAlexandre Belloni 
701a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev,
702a556c76aSAlexandre Belloni 					  char *buf, size_t len)
703a556c76aSAlexandre Belloni {
704a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
705a556c76aSAlexandre Belloni 	int ret;
706a556c76aSAlexandre Belloni 
707a556c76aSAlexandre Belloni 	ret = snprintf(buf, len, "p%d", port->chip_port);
708a556c76aSAlexandre Belloni 	if (ret >= len)
709a556c76aSAlexandre Belloni 		return -EINVAL;
710a556c76aSAlexandre Belloni 
711a556c76aSAlexandre Belloni 	return 0;
712a556c76aSAlexandre Belloni }
713a556c76aSAlexandre Belloni 
714a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
715a556c76aSAlexandre Belloni {
716a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
717a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
718a556c76aSAlexandre Belloni 	const struct sockaddr *addr = p;
719a556c76aSAlexandre Belloni 
720a556c76aSAlexandre Belloni 	/* Learn the new net device MAC address in the mac table. */
721a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
722a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
723a556c76aSAlexandre Belloni 	/* Then forget the previous one. */
724a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
725a556c76aSAlexandre Belloni 
726a556c76aSAlexandre Belloni 	ether_addr_copy(dev->dev_addr, addr->sa_data);
727a556c76aSAlexandre Belloni 	return 0;
728a556c76aSAlexandre Belloni }
729a556c76aSAlexandre Belloni 
730a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev,
731a556c76aSAlexandre Belloni 			       struct rtnl_link_stats64 *stats)
732a556c76aSAlexandre Belloni {
733a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
734a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
735a556c76aSAlexandre Belloni 
736a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
737a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
738a556c76aSAlexandre Belloni 		     SYS_STAT_CFG);
739a556c76aSAlexandre Belloni 
740a556c76aSAlexandre Belloni 	/* Get Rx stats */
741a556c76aSAlexandre Belloni 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
742a556c76aSAlexandre Belloni 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
743a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
744a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
745a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
746a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
747a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
748a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
749a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
750a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
751a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
752a556c76aSAlexandre Belloni 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
753a556c76aSAlexandre Belloni 	stats->rx_dropped = dev->stats.rx_dropped;
754a556c76aSAlexandre Belloni 
755a556c76aSAlexandre Belloni 	/* Get Tx stats */
756a556c76aSAlexandre Belloni 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
757a556c76aSAlexandre Belloni 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
758a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
759a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
760a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
761a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
762a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
763a556c76aSAlexandre Belloni 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
764a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
765a556c76aSAlexandre Belloni 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
766a556c76aSAlexandre Belloni }
767a556c76aSAlexandre Belloni 
768a556c76aSAlexandre Belloni static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
769a556c76aSAlexandre Belloni 			  struct net_device *dev, const unsigned char *addr,
77087b0984eSPetr Machata 			  u16 vid, u16 flags,
77187b0984eSPetr Machata 			  struct netlink_ext_ack *extack)
772a556c76aSAlexandre Belloni {
773a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
774a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
775a556c76aSAlexandre Belloni 
7767142529fSAntoine Tenart 	if (!vid) {
7777142529fSAntoine Tenart 		if (!port->vlan_aware)
7787142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
7797142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
7807142529fSAntoine Tenart 			 * matches incoming untagged packets
7817142529fSAntoine Tenart 			 */
7827142529fSAntoine Tenart 			vid = port->pvid;
7837142529fSAntoine Tenart 		else
7847142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
7857142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
7867142529fSAntoine Tenart 			 */
7877142529fSAntoine Tenart 			return -EINVAL;
7887142529fSAntoine Tenart 	}
7897142529fSAntoine Tenart 
790a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
7918fd1a4afSAllan W. Nielsen 				 ENTRYTYPE_LOCKED);
792a556c76aSAlexandre Belloni }
793a556c76aSAlexandre Belloni 
794a556c76aSAlexandre Belloni static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
795a556c76aSAlexandre Belloni 			  struct net_device *dev,
796a556c76aSAlexandre Belloni 			  const unsigned char *addr, u16 vid)
797a556c76aSAlexandre Belloni {
798a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
799a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
800a556c76aSAlexandre Belloni 
801a556c76aSAlexandre Belloni 	return ocelot_mact_forget(ocelot, addr, vid);
802a556c76aSAlexandre Belloni }
803a556c76aSAlexandre Belloni 
804a556c76aSAlexandre Belloni struct ocelot_dump_ctx {
805a556c76aSAlexandre Belloni 	struct net_device *dev;
806a556c76aSAlexandre Belloni 	struct sk_buff *skb;
807a556c76aSAlexandre Belloni 	struct netlink_callback *cb;
808a556c76aSAlexandre Belloni 	int idx;
809a556c76aSAlexandre Belloni };
810a556c76aSAlexandre Belloni 
811a556c76aSAlexandre Belloni static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
812a556c76aSAlexandre Belloni 			      struct ocelot_dump_ctx *dump)
813a556c76aSAlexandre Belloni {
814a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
815a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
816a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
817a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
818a556c76aSAlexandre Belloni 
819a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
820a556c76aSAlexandre Belloni 		goto skip;
821a556c76aSAlexandre Belloni 
822a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
823a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
824a556c76aSAlexandre Belloni 	if (!nlh)
825a556c76aSAlexandre Belloni 		return -EMSGSIZE;
826a556c76aSAlexandre Belloni 
827a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
828a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
829a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
830a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
831a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
832a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
833a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
834a556c76aSAlexandre Belloni 	ndm->ndm_state   = NUD_REACHABLE;
835a556c76aSAlexandre Belloni 
836a556c76aSAlexandre Belloni 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
837a556c76aSAlexandre Belloni 		goto nla_put_failure;
838a556c76aSAlexandre Belloni 
839a556c76aSAlexandre Belloni 	if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
840a556c76aSAlexandre Belloni 		goto nla_put_failure;
841a556c76aSAlexandre Belloni 
842a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
843a556c76aSAlexandre Belloni 
844a556c76aSAlexandre Belloni skip:
845a556c76aSAlexandre Belloni 	dump->idx++;
846a556c76aSAlexandre Belloni 	return 0;
847a556c76aSAlexandre Belloni 
848a556c76aSAlexandre Belloni nla_put_failure:
849a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
850a556c76aSAlexandre Belloni 	return -EMSGSIZE;
851a556c76aSAlexandre Belloni }
852a556c76aSAlexandre Belloni 
853a556c76aSAlexandre Belloni static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
854a556c76aSAlexandre Belloni 				   struct ocelot_mact_entry *entry)
855a556c76aSAlexandre Belloni {
856a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
857a556c76aSAlexandre Belloni 	char mac[ETH_ALEN];
858a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
859a556c76aSAlexandre Belloni 
860a556c76aSAlexandre Belloni 	/* Set row and column to read from */
861a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
862a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
863a556c76aSAlexandre Belloni 
864a556c76aSAlexandre Belloni 	/* Issue a read command */
865a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
866a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
867a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
868a556c76aSAlexandre Belloni 
869a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
870a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
871a556c76aSAlexandre Belloni 
872a556c76aSAlexandre Belloni 	/* Read the entry flags */
873a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
874a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
875a556c76aSAlexandre Belloni 		return -EINVAL;
876a556c76aSAlexandre Belloni 
877a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
878a556c76aSAlexandre Belloni 	 * do not report it.
879a556c76aSAlexandre Belloni 	 */
880a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
881a556c76aSAlexandre Belloni 	if (dst != port->chip_port)
882a556c76aSAlexandre Belloni 		return -EINVAL;
883a556c76aSAlexandre Belloni 
884a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
885a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
886a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
887a556c76aSAlexandre Belloni 
888a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
889a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
890a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
891a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
892a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
893a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
894a556c76aSAlexandre Belloni 
895a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
896a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
897a556c76aSAlexandre Belloni 
898a556c76aSAlexandre Belloni 	return 0;
899a556c76aSAlexandre Belloni }
900a556c76aSAlexandre Belloni 
901a556c76aSAlexandre Belloni static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
902a556c76aSAlexandre Belloni 			   struct net_device *dev,
903a556c76aSAlexandre Belloni 			   struct net_device *filter_dev, int *idx)
904a556c76aSAlexandre Belloni {
905a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
906a556c76aSAlexandre Belloni 	int i, j, ret = 0;
907a556c76aSAlexandre Belloni 	struct ocelot_dump_ctx dump = {
908a556c76aSAlexandre Belloni 		.dev = dev,
909a556c76aSAlexandre Belloni 		.skb = skb,
910a556c76aSAlexandre Belloni 		.cb = cb,
911a556c76aSAlexandre Belloni 		.idx = *idx,
912a556c76aSAlexandre Belloni 	};
913a556c76aSAlexandre Belloni 
914a556c76aSAlexandre Belloni 	struct ocelot_mact_entry entry;
915a556c76aSAlexandre Belloni 
916a556c76aSAlexandre Belloni 	/* Loop through all the mac tables entries. There are 1024 rows of 4
917a556c76aSAlexandre Belloni 	 * entries.
918a556c76aSAlexandre Belloni 	 */
919a556c76aSAlexandre Belloni 	for (i = 0; i < 1024; i++) {
920a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
921a556c76aSAlexandre Belloni 			ret = ocelot_mact_read(port, i, j, &entry);
922a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
923a556c76aSAlexandre Belloni 			 * skip it.
924a556c76aSAlexandre Belloni 			 */
925a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
926a556c76aSAlexandre Belloni 				continue;
927a556c76aSAlexandre Belloni 			else if (ret)
928a556c76aSAlexandre Belloni 				goto end;
929a556c76aSAlexandre Belloni 
930a556c76aSAlexandre Belloni 			ret = ocelot_fdb_do_dump(&entry, &dump);
931a556c76aSAlexandre Belloni 			if (ret)
932a556c76aSAlexandre Belloni 				goto end;
933a556c76aSAlexandre Belloni 		}
934a556c76aSAlexandre Belloni 	}
935a556c76aSAlexandre Belloni 
936a556c76aSAlexandre Belloni end:
937a556c76aSAlexandre Belloni 	*idx = dump.idx;
938a556c76aSAlexandre Belloni 	return ret;
939a556c76aSAlexandre Belloni }
940a556c76aSAlexandre Belloni 
9417142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
9427142529fSAntoine Tenart 				  u16 vid)
9437142529fSAntoine Tenart {
9441c44ce56SVladimir Oltean 	return ocelot_vlan_vid_add(dev, vid, false, false);
9457142529fSAntoine Tenart }
9467142529fSAntoine Tenart 
9477142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
9487142529fSAntoine Tenart 				   u16 vid)
9497142529fSAntoine Tenart {
9507142529fSAntoine Tenart 	return ocelot_vlan_vid_del(dev, vid);
9517142529fSAntoine Tenart }
9527142529fSAntoine Tenart 
9537142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev,
9547142529fSAntoine Tenart 			       netdev_features_t features)
9557142529fSAntoine Tenart {
9567142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
9577142529fSAntoine Tenart 	netdev_features_t changed = dev->features ^ features;
9587142529fSAntoine Tenart 
9592c1d029aSJoergen Andreasen 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
9602c1d029aSJoergen Andreasen 	    port->tc.offload_cnt) {
9612c1d029aSJoergen Andreasen 		netdev_err(dev,
9622c1d029aSJoergen Andreasen 			   "Cannot disable HW TC offload while offloads active\n");
9632c1d029aSJoergen Andreasen 		return -EBUSY;
9642c1d029aSJoergen Andreasen 	}
9652c1d029aSJoergen Andreasen 
9667142529fSAntoine Tenart 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
9677142529fSAntoine Tenart 		ocelot_vlan_mode(port, features);
9687142529fSAntoine Tenart 
9697142529fSAntoine Tenart 	return 0;
9707142529fSAntoine Tenart }
9717142529fSAntoine Tenart 
972751302c3SFlorian Fainelli static int ocelot_get_port_parent_id(struct net_device *dev,
973751302c3SFlorian Fainelli 				     struct netdev_phys_item_id *ppid)
974751302c3SFlorian Fainelli {
975751302c3SFlorian Fainelli 	struct ocelot_port *ocelot_port = netdev_priv(dev);
976751302c3SFlorian Fainelli 	struct ocelot *ocelot = ocelot_port->ocelot;
977751302c3SFlorian Fainelli 
978751302c3SFlorian Fainelli 	ppid->id_len = sizeof(ocelot->base_mac);
979751302c3SFlorian Fainelli 	memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
980751302c3SFlorian Fainelli 
981751302c3SFlorian Fainelli 	return 0;
982751302c3SFlorian Fainelli }
983751302c3SFlorian Fainelli 
9844e3b0468SAntoine Tenart static int ocelot_hwstamp_get(struct ocelot_port *port, struct ifreq *ifr)
9854e3b0468SAntoine Tenart {
9864e3b0468SAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
9874e3b0468SAntoine Tenart 
9884e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
9894e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
9904e3b0468SAntoine Tenart }
9914e3b0468SAntoine Tenart 
9924e3b0468SAntoine Tenart static int ocelot_hwstamp_set(struct ocelot_port *port, struct ifreq *ifr)
9934e3b0468SAntoine Tenart {
9944e3b0468SAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
9954e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
9964e3b0468SAntoine Tenart 
9974e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
9984e3b0468SAntoine Tenart 		return -EFAULT;
9994e3b0468SAntoine Tenart 
10004e3b0468SAntoine Tenart 	/* reserved for future extensions */
10014e3b0468SAntoine Tenart 	if (cfg.flags)
10024e3b0468SAntoine Tenart 		return -EINVAL;
10034e3b0468SAntoine Tenart 
10044e3b0468SAntoine Tenart 	/* Tx type sanity check */
10054e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
10064e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
10074e3b0468SAntoine Tenart 		port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
10084e3b0468SAntoine Tenart 		break;
10094e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
10104e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
10114e3b0468SAntoine Tenart 		 * need to update the origin time.
10124e3b0468SAntoine Tenart 		 */
10134e3b0468SAntoine Tenart 		port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
10144e3b0468SAntoine Tenart 		break;
10154e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
10164e3b0468SAntoine Tenart 		port->ptp_cmd = 0;
10174e3b0468SAntoine Tenart 		break;
10184e3b0468SAntoine Tenart 	default:
10194e3b0468SAntoine Tenart 		return -ERANGE;
10204e3b0468SAntoine Tenart 	}
10214e3b0468SAntoine Tenart 
10224e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
10234e3b0468SAntoine Tenart 
10244e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
10254e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
10264e3b0468SAntoine Tenart 		break;
10274e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
10284e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
10294e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
10304e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
10314e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
10324e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
10334e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
10344e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
10354e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
10364e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
10374e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
10384e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
10394e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
10404e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
10414e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
10424e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
10434e3b0468SAntoine Tenart 		break;
10444e3b0468SAntoine Tenart 	default:
10454e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
10464e3b0468SAntoine Tenart 		return -ERANGE;
10474e3b0468SAntoine Tenart 	}
10484e3b0468SAntoine Tenart 
10494e3b0468SAntoine Tenart 	/* Commit back the result & save it */
10504e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
10514e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
10524e3b0468SAntoine Tenart 
10534e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
10544e3b0468SAntoine Tenart }
10554e3b0468SAntoine Tenart 
10564e3b0468SAntoine Tenart static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
10574e3b0468SAntoine Tenart {
10584e3b0468SAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
10594e3b0468SAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
10604e3b0468SAntoine Tenart 
10614e3b0468SAntoine Tenart 	/* The function is only used for PTP operations for now */
10624e3b0468SAntoine Tenart 	if (!ocelot->ptp)
10634e3b0468SAntoine Tenart 		return -EOPNOTSUPP;
10644e3b0468SAntoine Tenart 
10654e3b0468SAntoine Tenart 	switch (cmd) {
10664e3b0468SAntoine Tenart 	case SIOCSHWTSTAMP:
10674e3b0468SAntoine Tenart 		return ocelot_hwstamp_set(port, ifr);
10684e3b0468SAntoine Tenart 	case SIOCGHWTSTAMP:
10694e3b0468SAntoine Tenart 		return ocelot_hwstamp_get(port, ifr);
10704e3b0468SAntoine Tenart 	default:
10714e3b0468SAntoine Tenart 		return -EOPNOTSUPP;
10724e3b0468SAntoine Tenart 	}
10734e3b0468SAntoine Tenart }
10744e3b0468SAntoine Tenart 
1075a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = {
1076a556c76aSAlexandre Belloni 	.ndo_open			= ocelot_port_open,
1077a556c76aSAlexandre Belloni 	.ndo_stop			= ocelot_port_stop,
1078a556c76aSAlexandre Belloni 	.ndo_start_xmit			= ocelot_port_xmit,
1079a556c76aSAlexandre Belloni 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
1080a556c76aSAlexandre Belloni 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
1081a556c76aSAlexandre Belloni 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
1082a556c76aSAlexandre Belloni 	.ndo_get_stats64		= ocelot_get_stats64,
1083a556c76aSAlexandre Belloni 	.ndo_fdb_add			= ocelot_fdb_add,
1084a556c76aSAlexandre Belloni 	.ndo_fdb_del			= ocelot_fdb_del,
1085a556c76aSAlexandre Belloni 	.ndo_fdb_dump			= ocelot_fdb_dump,
10867142529fSAntoine Tenart 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
10877142529fSAntoine Tenart 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
10887142529fSAntoine Tenart 	.ndo_set_features		= ocelot_set_features,
1089751302c3SFlorian Fainelli 	.ndo_get_port_parent_id		= ocelot_get_port_parent_id,
10902c1d029aSJoergen Andreasen 	.ndo_setup_tc			= ocelot_setup_tc,
10914e3b0468SAntoine Tenart 	.ndo_do_ioctl			= ocelot_ioctl,
1092a556c76aSAlexandre Belloni };
1093a556c76aSAlexandre Belloni 
1094a556c76aSAlexandre Belloni static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
1095a556c76aSAlexandre Belloni {
1096a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(netdev);
1097a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1098a556c76aSAlexandre Belloni 	int i;
1099a556c76aSAlexandre Belloni 
1100a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1101a556c76aSAlexandre Belloni 		return;
1102a556c76aSAlexandre Belloni 
1103a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1104a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1105a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1106a556c76aSAlexandre Belloni }
1107a556c76aSAlexandre Belloni 
11081e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1109a556c76aSAlexandre Belloni {
1110a556c76aSAlexandre Belloni 	int i, j;
1111a556c76aSAlexandre Belloni 
1112a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1113a556c76aSAlexandre Belloni 
1114a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1115a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1116a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1117a556c76aSAlexandre Belloni 
1118a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1119a556c76aSAlexandre Belloni 			u32 val;
1120a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1121a556c76aSAlexandre Belloni 
1122a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1123a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1124a556c76aSAlexandre Belloni 
1125a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1126a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1127a556c76aSAlexandre Belloni 
1128a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1129a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1130a556c76aSAlexandre Belloni 		}
1131a556c76aSAlexandre Belloni 	}
1132a556c76aSAlexandre Belloni 
11331e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
11341e1caa97SClaudiu Manoil }
11351e1caa97SClaudiu Manoil 
11361e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
11371e1caa97SClaudiu Manoil {
11381e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
11391e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
11401e1caa97SClaudiu Manoil 					     stats_work);
11411e1caa97SClaudiu Manoil 
11421e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
11431e1caa97SClaudiu Manoil 
1144a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1145a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1146a556c76aSAlexandre Belloni }
1147a556c76aSAlexandre Belloni 
1148a556c76aSAlexandre Belloni static void ocelot_get_ethtool_stats(struct net_device *dev,
1149a556c76aSAlexandre Belloni 				     struct ethtool_stats *stats, u64 *data)
1150a556c76aSAlexandre Belloni {
1151a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1152a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1153a556c76aSAlexandre Belloni 	int i;
1154a556c76aSAlexandre Belloni 
1155a556c76aSAlexandre Belloni 	/* check and update now */
11561e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1157a556c76aSAlexandre Belloni 
1158a556c76aSAlexandre Belloni 	/* Copy all counters */
1159a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1160a556c76aSAlexandre Belloni 		*data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
1161a556c76aSAlexandre Belloni }
1162a556c76aSAlexandre Belloni 
1163a556c76aSAlexandre Belloni static int ocelot_get_sset_count(struct net_device *dev, int sset)
1164a556c76aSAlexandre Belloni {
1165a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1166a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1167a556c76aSAlexandre Belloni 
1168a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1169a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1170a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1171a556c76aSAlexandre Belloni }
1172a556c76aSAlexandre Belloni 
11734e3b0468SAntoine Tenart static int ocelot_get_ts_info(struct net_device *dev,
11744e3b0468SAntoine Tenart 			      struct ethtool_ts_info *info)
11754e3b0468SAntoine Tenart {
11764e3b0468SAntoine Tenart 	struct ocelot_port *ocelot_port = netdev_priv(dev);
11774e3b0468SAntoine Tenart 	struct ocelot *ocelot = ocelot_port->ocelot;
11784e3b0468SAntoine Tenart 
11794e3b0468SAntoine Tenart 	if (!ocelot->ptp)
11804e3b0468SAntoine Tenart 		return ethtool_op_get_ts_info(dev, info);
11814e3b0468SAntoine Tenart 
11824e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
11834e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
11844e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
11854e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
11864e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
11874e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
11884e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
11894e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
11904e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
11914e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
11924e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
11934e3b0468SAntoine Tenart 
11944e3b0468SAntoine Tenart 	return 0;
11954e3b0468SAntoine Tenart }
11964e3b0468SAntoine Tenart 
1197a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = {
1198a556c76aSAlexandre Belloni 	.get_strings		= ocelot_get_strings,
1199a556c76aSAlexandre Belloni 	.get_ethtool_stats	= ocelot_get_ethtool_stats,
1200a556c76aSAlexandre Belloni 	.get_sset_count		= ocelot_get_sset_count,
1201dc96ee37SAlexandre Belloni 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1202dc96ee37SAlexandre Belloni 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
12034e3b0468SAntoine Tenart 	.get_ts_info		= ocelot_get_ts_info,
1204a556c76aSAlexandre Belloni };
1205a556c76aSAlexandre Belloni 
1206a556c76aSAlexandre Belloni static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1207a556c76aSAlexandre Belloni 					  struct switchdev_trans *trans,
1208a556c76aSAlexandre Belloni 					  u8 state)
1209a556c76aSAlexandre Belloni {
1210a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1211a556c76aSAlexandre Belloni 	u32 port_cfg;
1212a556c76aSAlexandre Belloni 	int port, i;
1213a556c76aSAlexandre Belloni 
1214a556c76aSAlexandre Belloni 	if (switchdev_trans_ph_prepare(trans))
1215a556c76aSAlexandre Belloni 		return 0;
1216a556c76aSAlexandre Belloni 
1217a556c76aSAlexandre Belloni 	if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1218a556c76aSAlexandre Belloni 		return 0;
1219a556c76aSAlexandre Belloni 
1220a556c76aSAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1221a556c76aSAlexandre Belloni 				   ocelot_port->chip_port);
1222a556c76aSAlexandre Belloni 
1223a556c76aSAlexandre Belloni 	switch (state) {
1224a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
1225a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1226a556c76aSAlexandre Belloni 		/* Fallthrough */
1227a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1228a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1229a556c76aSAlexandre Belloni 		break;
1230a556c76aSAlexandre Belloni 
1231a556c76aSAlexandre Belloni 	default:
1232a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1233a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1234a556c76aSAlexandre Belloni 		break;
1235a556c76aSAlexandre Belloni 	}
1236a556c76aSAlexandre Belloni 
1237a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1238a556c76aSAlexandre Belloni 			 ocelot_port->chip_port);
1239a556c76aSAlexandre Belloni 
1240a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1241a556c76aSAlexandre Belloni 	 * a source for the other ports.
1242a556c76aSAlexandre Belloni 	 */
1243a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1244a556c76aSAlexandre Belloni 		if (ocelot->bridge_fwd_mask & BIT(port)) {
1245a556c76aSAlexandre Belloni 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1246a556c76aSAlexandre Belloni 
1247a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1248a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
1249a556c76aSAlexandre Belloni 
1250a556c76aSAlexandre Belloni 				if (!bond_mask)
1251a556c76aSAlexandre Belloni 					continue;
1252a556c76aSAlexandre Belloni 
1253a556c76aSAlexandre Belloni 				if (bond_mask & BIT(port)) {
1254a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
1255a556c76aSAlexandre Belloni 					break;
1256a556c76aSAlexandre Belloni 				}
1257a556c76aSAlexandre Belloni 			}
1258a556c76aSAlexandre Belloni 
1259a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1260a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports) | mask,
1261a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1262a556c76aSAlexandre Belloni 		} else {
1263a556c76aSAlexandre Belloni 			/* Only the CPU port, this is compatible with link
1264a556c76aSAlexandre Belloni 			 * aggregation.
1265a556c76aSAlexandre Belloni 			 */
1266a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1267a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports),
1268a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1269a556c76aSAlexandre Belloni 		}
1270a556c76aSAlexandre Belloni 	}
1271a556c76aSAlexandre Belloni 
1272a556c76aSAlexandre Belloni 	return 0;
1273a556c76aSAlexandre Belloni }
1274a556c76aSAlexandre Belloni 
1275a556c76aSAlexandre Belloni static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1276a556c76aSAlexandre Belloni 					unsigned long ageing_clock_t)
1277a556c76aSAlexandre Belloni {
1278a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1279a556c76aSAlexandre Belloni 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1280a556c76aSAlexandre Belloni 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1281a556c76aSAlexandre Belloni 
1282a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1283a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1284a556c76aSAlexandre Belloni }
1285a556c76aSAlexandre Belloni 
1286a556c76aSAlexandre Belloni static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1287a556c76aSAlexandre Belloni {
1288a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1289a556c76aSAlexandre Belloni 	u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1290a556c76aSAlexandre Belloni 				  port->chip_port);
1291a556c76aSAlexandre Belloni 
1292a556c76aSAlexandre Belloni 	if (mc)
1293a556c76aSAlexandre Belloni 		val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1294a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1295a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1296a556c76aSAlexandre Belloni 	else
1297a556c76aSAlexandre Belloni 		val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1298a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1299a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1300a556c76aSAlexandre Belloni 
1301a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1302a556c76aSAlexandre Belloni }
1303a556c76aSAlexandre Belloni 
1304a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev,
1305a556c76aSAlexandre Belloni 				const struct switchdev_attr *attr,
1306a556c76aSAlexandre Belloni 				struct switchdev_trans *trans)
1307a556c76aSAlexandre Belloni {
1308a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1309a556c76aSAlexandre Belloni 	int err = 0;
1310a556c76aSAlexandre Belloni 
1311a556c76aSAlexandre Belloni 	switch (attr->id) {
1312a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1313a556c76aSAlexandre Belloni 		ocelot_port_attr_stp_state_set(ocelot_port, trans,
1314a556c76aSAlexandre Belloni 					       attr->u.stp_state);
1315a556c76aSAlexandre Belloni 		break;
1316a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1317a556c76aSAlexandre Belloni 		ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1318a556c76aSAlexandre Belloni 		break;
13197142529fSAntoine Tenart 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
13207142529fSAntoine Tenart 		ocelot_port->vlan_aware = attr->u.vlan_filtering;
13217142529fSAntoine Tenart 		ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port);
13227142529fSAntoine Tenart 		break;
1323a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1324a556c76aSAlexandre Belloni 		ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1325a556c76aSAlexandre Belloni 		break;
1326a556c76aSAlexandre Belloni 	default:
1327a556c76aSAlexandre Belloni 		err = -EOPNOTSUPP;
1328a556c76aSAlexandre Belloni 		break;
1329a556c76aSAlexandre Belloni 	}
1330a556c76aSAlexandre Belloni 
1331a556c76aSAlexandre Belloni 	return err;
1332a556c76aSAlexandre Belloni }
1333a556c76aSAlexandre Belloni 
13347142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev,
13357142529fSAntoine Tenart 				    const struct switchdev_obj_port_vlan *vlan,
13367142529fSAntoine Tenart 				    struct switchdev_trans *trans)
13377142529fSAntoine Tenart {
13387142529fSAntoine Tenart 	int ret;
13397142529fSAntoine Tenart 	u16 vid;
13407142529fSAntoine Tenart 
13417142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
13427142529fSAntoine Tenart 		ret = ocelot_vlan_vid_add(dev, vid,
13437142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
13447142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
13457142529fSAntoine Tenart 		if (ret)
13467142529fSAntoine Tenart 			return ret;
13477142529fSAntoine Tenart 	}
13487142529fSAntoine Tenart 
13497142529fSAntoine Tenart 	return 0;
13507142529fSAntoine Tenart }
13517142529fSAntoine Tenart 
13527142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev,
13537142529fSAntoine Tenart 				     const struct switchdev_obj_port_vlan *vlan)
13547142529fSAntoine Tenart {
13557142529fSAntoine Tenart 	int ret;
13567142529fSAntoine Tenart 	u16 vid;
13577142529fSAntoine Tenart 
13587142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
13597142529fSAntoine Tenart 		ret = ocelot_vlan_vid_del(dev, vid);
13607142529fSAntoine Tenart 
13617142529fSAntoine Tenart 		if (ret)
13627142529fSAntoine Tenart 			return ret;
13637142529fSAntoine Tenart 	}
13647142529fSAntoine Tenart 
13657142529fSAntoine Tenart 	return 0;
13667142529fSAntoine Tenart }
13677142529fSAntoine Tenart 
1368a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1369a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1370a556c76aSAlexandre Belloni 						     u16 vid)
1371a556c76aSAlexandre Belloni {
1372a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1373a556c76aSAlexandre Belloni 
1374a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1375a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1376a556c76aSAlexandre Belloni 			return mc;
1377a556c76aSAlexandre Belloni 	}
1378a556c76aSAlexandre Belloni 
1379a556c76aSAlexandre Belloni 	return NULL;
1380a556c76aSAlexandre Belloni }
1381a556c76aSAlexandre Belloni 
1382a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev,
1383a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb,
1384a556c76aSAlexandre Belloni 				   struct switchdev_trans *trans)
1385a556c76aSAlexandre Belloni {
1386a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1387a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1388a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1389a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1390a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1391a556c76aSAlexandre Belloni 	bool new = false;
1392a556c76aSAlexandre Belloni 
1393a556c76aSAlexandre Belloni 	if (!vid)
13947142529fSAntoine Tenart 		vid = port->pvid;
1395a556c76aSAlexandre Belloni 
1396a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1397a556c76aSAlexandre Belloni 	if (!mc) {
1398a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1399a556c76aSAlexandre Belloni 		if (!mc)
1400a556c76aSAlexandre Belloni 			return -ENOMEM;
1401a556c76aSAlexandre Belloni 
1402a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1403a556c76aSAlexandre Belloni 		mc->vid = vid;
1404a556c76aSAlexandre Belloni 
1405a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1406a556c76aSAlexandre Belloni 		new = true;
1407a556c76aSAlexandre Belloni 	}
1408a556c76aSAlexandre Belloni 
1409a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1410a556c76aSAlexandre Belloni 	addr[0] = 0;
1411a556c76aSAlexandre Belloni 
1412a556c76aSAlexandre Belloni 	if (!new) {
1413a556c76aSAlexandre Belloni 		addr[2] = mc->ports << 0;
1414a556c76aSAlexandre Belloni 		addr[1] = mc->ports << 8;
1415a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1416a556c76aSAlexandre Belloni 	}
1417a556c76aSAlexandre Belloni 
1418a556c76aSAlexandre Belloni 	mc->ports |= BIT(port->chip_port);
1419a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1420a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1421a556c76aSAlexandre Belloni 
1422a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1423a556c76aSAlexandre Belloni }
1424a556c76aSAlexandre Belloni 
1425a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev,
1426a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb)
1427a556c76aSAlexandre Belloni {
1428a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1429a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1430a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1431a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1432a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1433a556c76aSAlexandre Belloni 
1434a556c76aSAlexandre Belloni 	if (!vid)
14357142529fSAntoine Tenart 		vid = port->pvid;
1436a556c76aSAlexandre Belloni 
1437a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1438a556c76aSAlexandre Belloni 	if (!mc)
1439a556c76aSAlexandre Belloni 		return -ENOENT;
1440a556c76aSAlexandre Belloni 
1441a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1442a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1443a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1444a556c76aSAlexandre Belloni 	addr[0] = 0;
1445a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1446a556c76aSAlexandre Belloni 
1447a556c76aSAlexandre Belloni 	mc->ports &= ~BIT(port->chip_port);
1448a556c76aSAlexandre Belloni 	if (!mc->ports) {
1449a556c76aSAlexandre Belloni 		list_del(&mc->list);
1450a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1451a556c76aSAlexandre Belloni 		return 0;
1452a556c76aSAlexandre Belloni 	}
1453a556c76aSAlexandre Belloni 
1454a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1455a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1456a556c76aSAlexandre Belloni 
1457a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1458a556c76aSAlexandre Belloni }
1459a556c76aSAlexandre Belloni 
1460a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev,
1461a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj,
146269213513SPetr Machata 			       struct switchdev_trans *trans,
146369213513SPetr Machata 			       struct netlink_ext_ack *extack)
1464a556c76aSAlexandre Belloni {
1465a556c76aSAlexandre Belloni 	int ret = 0;
1466a556c76aSAlexandre Belloni 
1467a556c76aSAlexandre Belloni 	switch (obj->id) {
14687142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
14697142529fSAntoine Tenart 		ret = ocelot_port_obj_add_vlan(dev,
14707142529fSAntoine Tenart 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
14717142529fSAntoine Tenart 					       trans);
14727142529fSAntoine Tenart 		break;
1473a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1474a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1475a556c76aSAlexandre Belloni 					      trans);
1476a556c76aSAlexandre Belloni 		break;
1477a556c76aSAlexandre Belloni 	default:
1478a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1479a556c76aSAlexandre Belloni 	}
1480a556c76aSAlexandre Belloni 
1481a556c76aSAlexandre Belloni 	return ret;
1482a556c76aSAlexandre Belloni }
1483a556c76aSAlexandre Belloni 
1484a556c76aSAlexandre Belloni static int ocelot_port_obj_del(struct net_device *dev,
1485a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj)
1486a556c76aSAlexandre Belloni {
1487a556c76aSAlexandre Belloni 	int ret = 0;
1488a556c76aSAlexandre Belloni 
1489a556c76aSAlexandre Belloni 	switch (obj->id) {
14907142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
14917142529fSAntoine Tenart 		ret = ocelot_port_vlan_del_vlan(dev,
14927142529fSAntoine Tenart 						SWITCHDEV_OBJ_PORT_VLAN(obj));
14937142529fSAntoine Tenart 		break;
1494a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1495a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1496a556c76aSAlexandre Belloni 		break;
1497a556c76aSAlexandre Belloni 	default:
1498a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1499a556c76aSAlexandre Belloni 	}
1500a556c76aSAlexandre Belloni 
1501a556c76aSAlexandre Belloni 	return ret;
1502a556c76aSAlexandre Belloni }
1503a556c76aSAlexandre Belloni 
1504a556c76aSAlexandre Belloni static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1505a556c76aSAlexandre Belloni 				   struct net_device *bridge)
1506a556c76aSAlexandre Belloni {
1507a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1508a556c76aSAlexandre Belloni 
1509a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1510a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1511a556c76aSAlexandre Belloni 	} else {
1512a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1513a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1514a556c76aSAlexandre Belloni 			 * unsupported */
1515a556c76aSAlexandre Belloni 			return -ENODEV;
1516a556c76aSAlexandre Belloni 	}
1517a556c76aSAlexandre Belloni 
1518a556c76aSAlexandre Belloni 	ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1519a556c76aSAlexandre Belloni 
1520a556c76aSAlexandre Belloni 	return 0;
1521a556c76aSAlexandre Belloni }
1522a556c76aSAlexandre Belloni 
1523a556c76aSAlexandre Belloni static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1524a556c76aSAlexandre Belloni 				     struct net_device *bridge)
1525a556c76aSAlexandre Belloni {
1526a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1527a556c76aSAlexandre Belloni 
1528a556c76aSAlexandre Belloni 	ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
1529a556c76aSAlexandre Belloni 
1530a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1531a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
15327142529fSAntoine Tenart 
15337142529fSAntoine Tenart 	/* Clear bridge vlan settings before calling ocelot_vlan_port_apply */
15347142529fSAntoine Tenart 	ocelot_port->vlan_aware = 0;
15357142529fSAntoine Tenart 	ocelot_port->pvid = 0;
15367142529fSAntoine Tenart 	ocelot_port->vid = 0;
1537a556c76aSAlexandre Belloni }
1538a556c76aSAlexandre Belloni 
1539dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1540dc96ee37SAlexandre Belloni {
1541dc96ee37SAlexandre Belloni 	int i, port, lag;
1542dc96ee37SAlexandre Belloni 
1543dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1544dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1545dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1546dc96ee37SAlexandre Belloni 
1547dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1548dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1549dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1550dc96ee37SAlexandre Belloni 
1551dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1552dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1553dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1554dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1555dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1556dc96ee37SAlexandre Belloni 
1557dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1558dc96ee37SAlexandre Belloni 		if (!bond_mask)
1559dc96ee37SAlexandre Belloni 			continue;
1560dc96ee37SAlexandre Belloni 
1561dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1562dc96ee37SAlexandre Belloni 			// Destination mask
1563dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1564dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1565dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1566dc96ee37SAlexandre Belloni 			aggr_count++;
1567dc96ee37SAlexandre Belloni 		}
1568dc96ee37SAlexandre Belloni 
1569dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1570dc96ee37SAlexandre Belloni 			u32 ac;
1571dc96ee37SAlexandre Belloni 
1572dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1573dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1574dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1575dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1576dc96ee37SAlexandre Belloni 		}
1577dc96ee37SAlexandre Belloni 	}
1578dc96ee37SAlexandre Belloni }
1579dc96ee37SAlexandre Belloni 
1580dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1581dc96ee37SAlexandre Belloni {
1582dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1583dc96ee37SAlexandre Belloni 	unsigned int p;
1584dc96ee37SAlexandre Belloni 
1585dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1586dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1587dc96ee37SAlexandre Belloni 
1588dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1589dc96ee37SAlexandre Belloni 
1590dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1591dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1592dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1593dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1594dc96ee37SAlexandre Belloni 	}
1595dc96ee37SAlexandre Belloni }
1596dc96ee37SAlexandre Belloni 
1597dc96ee37SAlexandre Belloni static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1598dc96ee37SAlexandre Belloni 				struct net_device *bond)
1599dc96ee37SAlexandre Belloni {
1600dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1601dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1602dc96ee37SAlexandre Belloni 	int lag, lp;
1603dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1604dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1605dc96ee37SAlexandre Belloni 
1606dc96ee37SAlexandre Belloni 	rcu_read_lock();
1607dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1608dc96ee37SAlexandre Belloni 		struct ocelot_port *port = netdev_priv(ndev);
1609dc96ee37SAlexandre Belloni 
1610dc96ee37SAlexandre Belloni 		bond_mask |= BIT(port->chip_port);
1611dc96ee37SAlexandre Belloni 	}
1612dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1613dc96ee37SAlexandre Belloni 
1614dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1615dc96ee37SAlexandre Belloni 
1616dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1617dc96ee37SAlexandre Belloni 	 * now on
1618dc96ee37SAlexandre Belloni 	 */
1619dc96ee37SAlexandre Belloni 	if (p == lp) {
1620dc96ee37SAlexandre Belloni 		lag = p;
1621dc96ee37SAlexandre Belloni 		ocelot->lags[p] = bond_mask;
1622dc96ee37SAlexandre Belloni 		bond_mask &= ~BIT(p);
1623dc96ee37SAlexandre Belloni 		if (bond_mask) {
1624dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1625dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1626dc96ee37SAlexandre Belloni 		}
1627dc96ee37SAlexandre Belloni 	} else {
1628dc96ee37SAlexandre Belloni 		lag = lp;
1629dc96ee37SAlexandre Belloni 		ocelot->lags[lp] |= BIT(p);
1630dc96ee37SAlexandre Belloni 	}
1631dc96ee37SAlexandre Belloni 
1632dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1633dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1634dc96ee37SAlexandre Belloni 
1635dc96ee37SAlexandre Belloni 	return 0;
1636dc96ee37SAlexandre Belloni }
1637dc96ee37SAlexandre Belloni 
1638dc96ee37SAlexandre Belloni static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1639dc96ee37SAlexandre Belloni 				  struct net_device *bond)
1640dc96ee37SAlexandre Belloni {
1641dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1642dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1643dc96ee37SAlexandre Belloni 	u32 port_cfg;
1644dc96ee37SAlexandre Belloni 	int i;
1645dc96ee37SAlexandre Belloni 
1646dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1647dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1648dc96ee37SAlexandre Belloni 		ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1649dc96ee37SAlexandre Belloni 
1650dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1651dc96ee37SAlexandre Belloni 	 * next port
1652dc96ee37SAlexandre Belloni 	 */
1653dc96ee37SAlexandre Belloni 	if (ocelot->lags[p]) {
1654dc96ee37SAlexandre Belloni 		int n = __ffs(ocelot->lags[p]);
1655dc96ee37SAlexandre Belloni 
1656dc96ee37SAlexandre Belloni 		ocelot->lags[n] = ocelot->lags[p];
1657dc96ee37SAlexandre Belloni 		ocelot->lags[p] = 0;
1658dc96ee37SAlexandre Belloni 
1659dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1660dc96ee37SAlexandre Belloni 	}
1661dc96ee37SAlexandre Belloni 
1662dc96ee37SAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1663dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1664dc96ee37SAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1665dc96ee37SAlexandre Belloni 			 ANA_PORT_PORT_CFG, p);
1666dc96ee37SAlexandre Belloni 
1667dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1668dc96ee37SAlexandre Belloni }
1669dc96ee37SAlexandre Belloni 
1670a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */
1671a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1672a556c76aSAlexandre Belloni {
1673a556c76aSAlexandre Belloni 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1674a556c76aSAlexandre Belloni }
1675a556c76aSAlexandre Belloni 
1676a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev,
1677a556c76aSAlexandre Belloni 				       unsigned long event,
1678a556c76aSAlexandre Belloni 				       struct netdev_notifier_changeupper_info *info)
1679a556c76aSAlexandre Belloni {
1680a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1681a556c76aSAlexandre Belloni 	int err = 0;
1682a556c76aSAlexandre Belloni 
1683a556c76aSAlexandre Belloni 	switch (event) {
1684a556c76aSAlexandre Belloni 	case NETDEV_CHANGEUPPER:
1685a556c76aSAlexandre Belloni 		if (netif_is_bridge_master(info->upper_dev)) {
1686a556c76aSAlexandre Belloni 			if (info->linking)
1687a556c76aSAlexandre Belloni 				err = ocelot_port_bridge_join(ocelot_port,
1688a556c76aSAlexandre Belloni 							      info->upper_dev);
1689a556c76aSAlexandre Belloni 			else
1690a556c76aSAlexandre Belloni 				ocelot_port_bridge_leave(ocelot_port,
1691a556c76aSAlexandre Belloni 							 info->upper_dev);
16927142529fSAntoine Tenart 
16937142529fSAntoine Tenart 			ocelot_vlan_port_apply(ocelot_port->ocelot,
16947142529fSAntoine Tenart 					       ocelot_port);
1695a556c76aSAlexandre Belloni 		}
1696dc96ee37SAlexandre Belloni 		if (netif_is_lag_master(info->upper_dev)) {
1697dc96ee37SAlexandre Belloni 			if (info->linking)
1698dc96ee37SAlexandre Belloni 				err = ocelot_port_lag_join(ocelot_port,
1699dc96ee37SAlexandre Belloni 							   info->upper_dev);
1700dc96ee37SAlexandre Belloni 			else
1701dc96ee37SAlexandre Belloni 				ocelot_port_lag_leave(ocelot_port,
1702dc96ee37SAlexandre Belloni 						      info->upper_dev);
1703dc96ee37SAlexandre Belloni 		}
1704a556c76aSAlexandre Belloni 		break;
1705a556c76aSAlexandre Belloni 	default:
1706a556c76aSAlexandre Belloni 		break;
1707a556c76aSAlexandre Belloni 	}
1708a556c76aSAlexandre Belloni 
1709a556c76aSAlexandre Belloni 	return err;
1710a556c76aSAlexandre Belloni }
1711a556c76aSAlexandre Belloni 
1712a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused,
1713a556c76aSAlexandre Belloni 				  unsigned long event, void *ptr)
1714a556c76aSAlexandre Belloni {
1715a556c76aSAlexandre Belloni 	struct netdev_notifier_changeupper_info *info = ptr;
1716a556c76aSAlexandre Belloni 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
17172ac0e152SGeert Uytterhoeven 	int ret = 0;
1718a556c76aSAlexandre Belloni 
1719*7afb3e57SClaudiu Manoil 	if (!ocelot_netdevice_dev_check(dev))
1720*7afb3e57SClaudiu Manoil 		return 0;
1721*7afb3e57SClaudiu Manoil 
1722dc96ee37SAlexandre Belloni 	if (event == NETDEV_PRECHANGEUPPER &&
1723dc96ee37SAlexandre Belloni 	    netif_is_lag_master(info->upper_dev)) {
1724dc96ee37SAlexandre Belloni 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1725dc96ee37SAlexandre Belloni 		struct netlink_ext_ack *extack;
1726dc96ee37SAlexandre Belloni 
1727dc96ee37SAlexandre Belloni 		if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1728dc96ee37SAlexandre Belloni 			extack = netdev_notifier_info_to_extack(&info->info);
1729dc96ee37SAlexandre Belloni 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1730dc96ee37SAlexandre Belloni 
1731dc96ee37SAlexandre Belloni 			ret = -EINVAL;
1732dc96ee37SAlexandre Belloni 			goto notify;
1733dc96ee37SAlexandre Belloni 		}
1734dc96ee37SAlexandre Belloni 	}
1735dc96ee37SAlexandre Belloni 
1736a556c76aSAlexandre Belloni 	if (netif_is_lag_master(dev)) {
1737a556c76aSAlexandre Belloni 		struct net_device *slave;
1738a556c76aSAlexandre Belloni 		struct list_head *iter;
1739a556c76aSAlexandre Belloni 
1740a556c76aSAlexandre Belloni 		netdev_for_each_lower_dev(dev, slave, iter) {
1741a556c76aSAlexandre Belloni 			ret = ocelot_netdevice_port_event(slave, event, info);
1742a556c76aSAlexandre Belloni 			if (ret)
1743a556c76aSAlexandre Belloni 				goto notify;
1744a556c76aSAlexandre Belloni 		}
1745a556c76aSAlexandre Belloni 	} else {
1746a556c76aSAlexandre Belloni 		ret = ocelot_netdevice_port_event(dev, event, info);
1747a556c76aSAlexandre Belloni 	}
1748a556c76aSAlexandre Belloni 
1749a556c76aSAlexandre Belloni notify:
1750a556c76aSAlexandre Belloni 	return notifier_from_errno(ret);
1751a556c76aSAlexandre Belloni }
1752a556c76aSAlexandre Belloni 
1753a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = {
1754a556c76aSAlexandre Belloni 	.notifier_call = ocelot_netdevice_event,
1755a556c76aSAlexandre Belloni };
1756a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb);
1757a556c76aSAlexandre Belloni 
175856da64bcSFlorian Fainelli static int ocelot_switchdev_event(struct notifier_block *unused,
175956da64bcSFlorian Fainelli 				  unsigned long event, void *ptr)
176056da64bcSFlorian Fainelli {
176156da64bcSFlorian Fainelli 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
176256da64bcSFlorian Fainelli 	int err;
176356da64bcSFlorian Fainelli 
176456da64bcSFlorian Fainelli 	switch (event) {
176556da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
176656da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
176756da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
176856da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
176956da64bcSFlorian Fainelli 		return notifier_from_errno(err);
177056da64bcSFlorian Fainelli 	}
177156da64bcSFlorian Fainelli 
177256da64bcSFlorian Fainelli 	return NOTIFY_DONE;
177356da64bcSFlorian Fainelli }
177456da64bcSFlorian Fainelli 
177556da64bcSFlorian Fainelli struct notifier_block ocelot_switchdev_nb __read_mostly = {
177656da64bcSFlorian Fainelli 	.notifier_call = ocelot_switchdev_event,
177756da64bcSFlorian Fainelli };
177856da64bcSFlorian Fainelli EXPORT_SYMBOL(ocelot_switchdev_nb);
177956da64bcSFlorian Fainelli 
17800e332c85SPetr Machata static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
17810e332c85SPetr Machata 					   unsigned long event, void *ptr)
17820e332c85SPetr Machata {
17830e332c85SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
17840e332c85SPetr Machata 	int err;
17850e332c85SPetr Machata 
17860e332c85SPetr Machata 	switch (event) {
17870e332c85SPetr Machata 		/* Blocking events. */
17880e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
17890e332c85SPetr Machata 		err = switchdev_handle_port_obj_add(dev, ptr,
17900e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
17910e332c85SPetr Machata 						    ocelot_port_obj_add);
17920e332c85SPetr Machata 		return notifier_from_errno(err);
17930e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
17940e332c85SPetr Machata 		err = switchdev_handle_port_obj_del(dev, ptr,
17950e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
17960e332c85SPetr Machata 						    ocelot_port_obj_del);
17970e332c85SPetr Machata 		return notifier_from_errno(err);
179856da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
179956da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
180056da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
180156da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
180256da64bcSFlorian Fainelli 		return notifier_from_errno(err);
18030e332c85SPetr Machata 	}
18040e332c85SPetr Machata 
18050e332c85SPetr Machata 	return NOTIFY_DONE;
18060e332c85SPetr Machata }
18070e332c85SPetr Machata 
18080e332c85SPetr Machata struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
18090e332c85SPetr Machata 	.notifier_call = ocelot_switchdev_blocking_event,
18100e332c85SPetr Machata };
18110e332c85SPetr Machata EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
18120e332c85SPetr Machata 
18134e3b0468SAntoine Tenart int ocelot_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
18144e3b0468SAntoine Tenart {
18154e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
18164e3b0468SAntoine Tenart 	unsigned long flags;
18174e3b0468SAntoine Tenart 	time64_t s;
18184e3b0468SAntoine Tenart 	u32 val;
18194e3b0468SAntoine Tenart 	s64 ns;
18204e3b0468SAntoine Tenart 
18214e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
18224e3b0468SAntoine Tenart 
18234e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18244e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18254e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
18264e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18274e3b0468SAntoine Tenart 
18284e3b0468SAntoine Tenart 	s = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN) & 0xffff;
18294e3b0468SAntoine Tenart 	s <<= 32;
18304e3b0468SAntoine Tenart 	s += ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
18314e3b0468SAntoine Tenart 	ns = ocelot_read_rix(ocelot, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
18324e3b0468SAntoine Tenart 
18334e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
18344e3b0468SAntoine Tenart 
18354e3b0468SAntoine Tenart 	/* Deal with negative values */
18364e3b0468SAntoine Tenart 	if (ns >= 0x3ffffff0 && ns <= 0x3fffffff) {
18374e3b0468SAntoine Tenart 		s--;
18384e3b0468SAntoine Tenart 		ns &= 0xf;
18394e3b0468SAntoine Tenart 		ns += 999999984;
18404e3b0468SAntoine Tenart 	}
18414e3b0468SAntoine Tenart 
18424e3b0468SAntoine Tenart 	set_normalized_timespec64(ts, s, ns);
18434e3b0468SAntoine Tenart 	return 0;
18444e3b0468SAntoine Tenart }
18454e3b0468SAntoine Tenart EXPORT_SYMBOL(ocelot_ptp_gettime64);
18464e3b0468SAntoine Tenart 
18474e3b0468SAntoine Tenart static int ocelot_ptp_settime64(struct ptp_clock_info *ptp,
18484e3b0468SAntoine Tenart 				const struct timespec64 *ts)
18494e3b0468SAntoine Tenart {
18504e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
18514e3b0468SAntoine Tenart 	unsigned long flags;
18524e3b0468SAntoine Tenart 	u32 val;
18534e3b0468SAntoine Tenart 
18544e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
18554e3b0468SAntoine Tenart 
18564e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18574e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18584e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
18594e3b0468SAntoine Tenart 
18604e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18614e3b0468SAntoine Tenart 
18624e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, lower_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_LSB,
18634e3b0468SAntoine Tenart 			 TOD_ACC_PIN);
18644e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, upper_32_bits(ts->tv_sec), PTP_PIN_TOD_SEC_MSB,
18654e3b0468SAntoine Tenart 			 TOD_ACC_PIN);
18664e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, ts->tv_nsec, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
18674e3b0468SAntoine Tenart 
18684e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18694e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18704e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_LOAD);
18714e3b0468SAntoine Tenart 
18724e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18734e3b0468SAntoine Tenart 
18744e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
18754e3b0468SAntoine Tenart 	return 0;
18764e3b0468SAntoine Tenart }
18774e3b0468SAntoine Tenart 
18784e3b0468SAntoine Tenart static int ocelot_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
18794e3b0468SAntoine Tenart {
18804e3b0468SAntoine Tenart 	if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
18814e3b0468SAntoine Tenart 		struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
18824e3b0468SAntoine Tenart 		unsigned long flags;
18834e3b0468SAntoine Tenart 		u32 val;
18844e3b0468SAntoine Tenart 
18854e3b0468SAntoine Tenart 		spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
18864e3b0468SAntoine Tenart 
18874e3b0468SAntoine Tenart 		val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18884e3b0468SAntoine Tenart 		val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18894e3b0468SAntoine Tenart 		val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_IDLE);
18904e3b0468SAntoine Tenart 
18914e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
18924e3b0468SAntoine Tenart 
18934e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
18944e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, 0, PTP_PIN_TOD_SEC_MSB, TOD_ACC_PIN);
18954e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, delta, PTP_PIN_TOD_NSEC, TOD_ACC_PIN);
18964e3b0468SAntoine Tenart 
18974e3b0468SAntoine Tenart 		val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
18984e3b0468SAntoine Tenart 		val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
18994e3b0468SAntoine Tenart 		val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_DELTA);
19004e3b0468SAntoine Tenart 
19014e3b0468SAntoine Tenart 		ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
19024e3b0468SAntoine Tenart 
19034e3b0468SAntoine Tenart 		spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19044e3b0468SAntoine Tenart 	} else {
19054e3b0468SAntoine Tenart 		/* Fall back using ocelot_ptp_settime64 which is not exact. */
19064e3b0468SAntoine Tenart 		struct timespec64 ts;
19074e3b0468SAntoine Tenart 		u64 now;
19084e3b0468SAntoine Tenart 
19094e3b0468SAntoine Tenart 		ocelot_ptp_gettime64(ptp, &ts);
19104e3b0468SAntoine Tenart 
19114e3b0468SAntoine Tenart 		now = ktime_to_ns(timespec64_to_ktime(ts));
19124e3b0468SAntoine Tenart 		ts = ns_to_timespec64(now + delta);
19134e3b0468SAntoine Tenart 
19144e3b0468SAntoine Tenart 		ocelot_ptp_settime64(ptp, &ts);
19154e3b0468SAntoine Tenart 	}
19164e3b0468SAntoine Tenart 	return 0;
19174e3b0468SAntoine Tenart }
19184e3b0468SAntoine Tenart 
19194e3b0468SAntoine Tenart static int ocelot_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
19204e3b0468SAntoine Tenart {
19214e3b0468SAntoine Tenart 	struct ocelot *ocelot = container_of(ptp, struct ocelot, ptp_info);
19224e3b0468SAntoine Tenart 	u32 unit = 0, direction = 0;
19234e3b0468SAntoine Tenart 	unsigned long flags;
19244e3b0468SAntoine Tenart 	u64 adj = 0;
19254e3b0468SAntoine Tenart 
19264e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
19274e3b0468SAntoine Tenart 
19284e3b0468SAntoine Tenart 	if (!scaled_ppm)
19294e3b0468SAntoine Tenart 		goto disable_adj;
19304e3b0468SAntoine Tenart 
19314e3b0468SAntoine Tenart 	if (scaled_ppm < 0) {
19324e3b0468SAntoine Tenart 		direction = PTP_CFG_CLK_ADJ_CFG_DIR;
19334e3b0468SAntoine Tenart 		scaled_ppm = -scaled_ppm;
19344e3b0468SAntoine Tenart 	}
19354e3b0468SAntoine Tenart 
19364e3b0468SAntoine Tenart 	adj = PSEC_PER_SEC << 16;
19374e3b0468SAntoine Tenart 	do_div(adj, scaled_ppm);
19384e3b0468SAntoine Tenart 	do_div(adj, 1000);
19394e3b0468SAntoine Tenart 
19404e3b0468SAntoine Tenart 	/* If the adjustment value is too large, use ns instead */
19414e3b0468SAntoine Tenart 	if (adj >= (1L << 30)) {
19424e3b0468SAntoine Tenart 		unit = PTP_CFG_CLK_ADJ_FREQ_NS;
19434e3b0468SAntoine Tenart 		do_div(adj, 1000);
19444e3b0468SAntoine Tenart 	}
19454e3b0468SAntoine Tenart 
19464e3b0468SAntoine Tenart 	/* Still too big */
19474e3b0468SAntoine Tenart 	if (adj >= (1L << 30))
19484e3b0468SAntoine Tenart 		goto disable_adj;
19494e3b0468SAntoine Tenart 
19504e3b0468SAntoine Tenart 	ocelot_write(ocelot, unit | adj, PTP_CLK_CFG_ADJ_FREQ);
19514e3b0468SAntoine Tenart 	ocelot_write(ocelot, PTP_CFG_CLK_ADJ_CFG_ENA | direction,
19524e3b0468SAntoine Tenart 		     PTP_CLK_CFG_ADJ_CFG);
19534e3b0468SAntoine Tenart 
19544e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19554e3b0468SAntoine Tenart 	return 0;
19564e3b0468SAntoine Tenart 
19574e3b0468SAntoine Tenart disable_adj:
19584e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0, PTP_CLK_CFG_ADJ_CFG);
19594e3b0468SAntoine Tenart 
19604e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
19614e3b0468SAntoine Tenart 	return 0;
19624e3b0468SAntoine Tenart }
19634e3b0468SAntoine Tenart 
19644e3b0468SAntoine Tenart static struct ptp_clock_info ocelot_ptp_clock_info = {
19654e3b0468SAntoine Tenart 	.owner		= THIS_MODULE,
19664e3b0468SAntoine Tenart 	.name		= "ocelot ptp",
19674e3b0468SAntoine Tenart 	.max_adj	= 0x7fffffff,
19684e3b0468SAntoine Tenart 	.n_alarm	= 0,
19694e3b0468SAntoine Tenart 	.n_ext_ts	= 0,
19704e3b0468SAntoine Tenart 	.n_per_out	= 0,
19714e3b0468SAntoine Tenart 	.n_pins		= 0,
19724e3b0468SAntoine Tenart 	.pps		= 0,
19734e3b0468SAntoine Tenart 	.gettime64	= ocelot_ptp_gettime64,
19744e3b0468SAntoine Tenart 	.settime64	= ocelot_ptp_settime64,
19754e3b0468SAntoine Tenart 	.adjtime	= ocelot_ptp_adjtime,
19764e3b0468SAntoine Tenart 	.adjfine	= ocelot_ptp_adjfine,
19774e3b0468SAntoine Tenart };
19784e3b0468SAntoine Tenart 
19794e3b0468SAntoine Tenart static int ocelot_init_timestamp(struct ocelot *ocelot)
19804e3b0468SAntoine Tenart {
19814e3b0468SAntoine Tenart 	ocelot->ptp_info = ocelot_ptp_clock_info;
19824e3b0468SAntoine Tenart 	ocelot->ptp_clock = ptp_clock_register(&ocelot->ptp_info, ocelot->dev);
19834e3b0468SAntoine Tenart 	if (IS_ERR(ocelot->ptp_clock))
19844e3b0468SAntoine Tenart 		return PTR_ERR(ocelot->ptp_clock);
19854e3b0468SAntoine Tenart 	/* Check if PHC support is missing at the configuration level */
19864e3b0468SAntoine Tenart 	if (!ocelot->ptp_clock)
19874e3b0468SAntoine Tenart 		return 0;
19884e3b0468SAntoine Tenart 
19894e3b0468SAntoine Tenart 	ocelot_write(ocelot, SYS_PTP_CFG_PTP_STAMP_WID(30), SYS_PTP_CFG);
19904e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_LOW);
19914e3b0468SAntoine Tenart 	ocelot_write(ocelot, 0xffffffff, ANA_TABLES_PTP_ID_HIGH);
19924e3b0468SAntoine Tenart 
19934e3b0468SAntoine Tenart 	ocelot_write(ocelot, PTP_CFG_MISC_PTP_EN, PTP_CFG_MISC);
19944e3b0468SAntoine Tenart 
19954e3b0468SAntoine Tenart 	/* There is no device reconfiguration, PTP Rx stamping is always
19964e3b0468SAntoine Tenart 	 * enabled.
19974e3b0468SAntoine Tenart 	 */
19984e3b0468SAntoine Tenart 	ocelot->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
19994e3b0468SAntoine Tenart 
20004e3b0468SAntoine Tenart 	return 0;
20014e3b0468SAntoine Tenart }
20024e3b0468SAntoine Tenart 
2003a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2004a556c76aSAlexandre Belloni 		      void __iomem *regs,
2005a556c76aSAlexandre Belloni 		      struct phy_device *phy)
2006a556c76aSAlexandre Belloni {
2007a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port;
2008a556c76aSAlexandre Belloni 	struct net_device *dev;
2009a556c76aSAlexandre Belloni 	int err;
2010a556c76aSAlexandre Belloni 
2011a556c76aSAlexandre Belloni 	dev = alloc_etherdev(sizeof(struct ocelot_port));
2012a556c76aSAlexandre Belloni 	if (!dev)
2013a556c76aSAlexandre Belloni 		return -ENOMEM;
2014a556c76aSAlexandre Belloni 	SET_NETDEV_DEV(dev, ocelot->dev);
2015a556c76aSAlexandre Belloni 	ocelot_port = netdev_priv(dev);
2016a556c76aSAlexandre Belloni 	ocelot_port->dev = dev;
2017a556c76aSAlexandre Belloni 	ocelot_port->ocelot = ocelot;
2018a556c76aSAlexandre Belloni 	ocelot_port->regs = regs;
2019a556c76aSAlexandre Belloni 	ocelot_port->chip_port = port;
2020a556c76aSAlexandre Belloni 	ocelot_port->phy = phy;
2021a556c76aSAlexandre Belloni 	ocelot->ports[port] = ocelot_port;
2022a556c76aSAlexandre Belloni 
2023a556c76aSAlexandre Belloni 	dev->netdev_ops = &ocelot_port_netdev_ops;
2024a556c76aSAlexandre Belloni 	dev->ethtool_ops = &ocelot_ethtool_ops;
2025a556c76aSAlexandre Belloni 
20262c1d029aSJoergen Andreasen 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
20272c1d029aSJoergen Andreasen 		NETIF_F_HW_TC;
20282c1d029aSJoergen Andreasen 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
20297142529fSAntoine Tenart 
2030a556c76aSAlexandre Belloni 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2031a556c76aSAlexandre Belloni 	dev->dev_addr[ETH_ALEN - 1] += port;
2032a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2033a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
2034a556c76aSAlexandre Belloni 
20354e3b0468SAntoine Tenart 	INIT_LIST_HEAD(&ocelot_port->skbs);
20364e3b0468SAntoine Tenart 
2037a556c76aSAlexandre Belloni 	err = register_netdev(dev);
2038a556c76aSAlexandre Belloni 	if (err) {
2039a556c76aSAlexandre Belloni 		dev_err(ocelot->dev, "register_netdev failed\n");
2040a556c76aSAlexandre Belloni 		goto err_register_netdev;
2041a556c76aSAlexandre Belloni 	}
2042a556c76aSAlexandre Belloni 
20437142529fSAntoine Tenart 	/* Basic L2 initialization */
20447142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, ocelot_port);
20457142529fSAntoine Tenart 
2046b5962294SHoratiu Vultur 	/* Enable vcap lookups */
2047b5962294SHoratiu Vultur 	ocelot_vcap_enable(ocelot, ocelot_port);
2048b5962294SHoratiu Vultur 
2049a556c76aSAlexandre Belloni 	return 0;
2050a556c76aSAlexandre Belloni 
2051a556c76aSAlexandre Belloni err_register_netdev:
2052a556c76aSAlexandre Belloni 	free_netdev(dev);
2053a556c76aSAlexandre Belloni 	return err;
2054a556c76aSAlexandre Belloni }
2055a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port);
2056a556c76aSAlexandre Belloni 
2057a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2058a556c76aSAlexandre Belloni {
2059a556c76aSAlexandre Belloni 	u32 port;
20604e3b0468SAntoine Tenart 	int i, ret, cpu = ocelot->num_phys_ports;
2061a556c76aSAlexandre Belloni 	char queue_name[32];
2062a556c76aSAlexandre Belloni 
2063dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2064dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
2065dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
2066dc96ee37SAlexandre Belloni 		return -ENOMEM;
2067dc96ee37SAlexandre Belloni 
2068a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2069a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2070a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2071a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2072a556c76aSAlexandre Belloni 		return -ENOMEM;
2073a556c76aSAlexandre Belloni 
2074a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
20754e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
20764e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
2077a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2078a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2079a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2080a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2081a556c76aSAlexandre Belloni 		return -ENOMEM;
2082a556c76aSAlexandre Belloni 
2083a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2084a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2085b5962294SHoratiu Vultur 	ocelot_ace_init(ocelot);
2086a556c76aSAlexandre Belloni 
2087a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2088a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2089a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2090a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2091a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2092a556c76aSAlexandre Belloni 	}
2093a556c76aSAlexandre Belloni 
2094a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2095a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2096a556c76aSAlexandre Belloni 
2097a556c76aSAlexandre Belloni 	/* Aggregation mode */
2098a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2099a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2100a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2101a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2102a556c76aSAlexandre Belloni 
2103a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2104a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2105a556c76aSAlexandre Belloni 	 */
2106a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2107a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2108a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2109a556c76aSAlexandre Belloni 
2110a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2111a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2112a556c76aSAlexandre Belloni 
2113a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2114a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2115a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2116a556c76aSAlexandre Belloni 
2117a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2118a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2119a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2120a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2121a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
2122a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2123a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2124a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2125a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2126a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2127a556c76aSAlexandre Belloni 
2128a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2129a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2130a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2131a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2132a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2133a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2134a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2135a556c76aSAlexandre Belloni 				 port);
2136a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2137a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2138a556c76aSAlexandre Belloni 	}
2139a556c76aSAlexandre Belloni 
2140a556c76aSAlexandre Belloni 	/* Configure and enable the CPU port. */
2141a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2142a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2143a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2144a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2145a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, cpu);
2146a556c76aSAlexandre Belloni 
2147a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
2148a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2149a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2150a556c76aSAlexandre Belloni 
2151a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2152a556c76aSAlexandre Belloni 	}
2153a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
2154a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2155a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
2156a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2157a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2158a556c76aSAlexandre Belloni 
2159a556c76aSAlexandre Belloni 	/* CPU port Injection/Extraction configuration */
2160a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2161a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2162a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2163a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, cpu);
2164a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
2165a556c76aSAlexandre Belloni 			 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
2166a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2167a556c76aSAlexandre Belloni 	 * registers endianness.
2168a556c76aSAlexandre Belloni 	 */
2169a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2170a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2171a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2172a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2173a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2174a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2175a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2176a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2177a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2178a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2179a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2180a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2181a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2182a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2183a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2184a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2185a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2186a556c76aSAlexandre Belloni 
21871e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2188a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2189a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
21904e3b0468SAntoine Tenart 
21914e3b0468SAntoine Tenart 	if (ocelot->ptp) {
21924e3b0468SAntoine Tenart 		ret = ocelot_init_timestamp(ocelot);
21934e3b0468SAntoine Tenart 		if (ret) {
21944e3b0468SAntoine Tenart 			dev_err(ocelot->dev,
21954e3b0468SAntoine Tenart 				"Timestamp initialization failed\n");
21964e3b0468SAntoine Tenart 			return ret;
21974e3b0468SAntoine Tenart 		}
21984e3b0468SAntoine Tenart 	}
21994e3b0468SAntoine Tenart 
2200a556c76aSAlexandre Belloni 	return 0;
2201a556c76aSAlexandre Belloni }
2202a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2203a556c76aSAlexandre Belloni 
2204a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2205a556c76aSAlexandre Belloni {
22064e3b0468SAntoine Tenart 	struct list_head *pos, *tmp;
22074e3b0468SAntoine Tenart 	struct ocelot_port *port;
22084e3b0468SAntoine Tenart 	struct ocelot_skb *entry;
22094e3b0468SAntoine Tenart 	int i;
22104e3b0468SAntoine Tenart 
2211c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2212a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2213a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
2214b5962294SHoratiu Vultur 	ocelot_ace_deinit();
22154e3b0468SAntoine Tenart 
22164e3b0468SAntoine Tenart 	for (i = 0; i < ocelot->num_phys_ports; i++) {
22174e3b0468SAntoine Tenart 		port = ocelot->ports[i];
22184e3b0468SAntoine Tenart 
22194e3b0468SAntoine Tenart 		list_for_each_safe(pos, tmp, &port->skbs) {
22204e3b0468SAntoine Tenart 			entry = list_entry(pos, struct ocelot_skb, head);
22214e3b0468SAntoine Tenart 
22224e3b0468SAntoine Tenart 			list_del(pos);
22234e3b0468SAntoine Tenart 			dev_kfree_skb_any(entry->skb);
22244e3b0468SAntoine Tenart 			kfree(entry);
22254e3b0468SAntoine Tenart 		}
22264e3b0468SAntoine Tenart 	}
2227a556c76aSAlexandre Belloni }
2228a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2229a556c76aSAlexandre Belloni 
2230a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2231