xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision b596229448dd2a263cdc4906e60b1b2249777ee4)
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>
17a556c76aSAlexandre Belloni #include <linux/skbuff.h>
18639c1b26SSteen Hegelund #include <linux/iopoll.h>
19a556c76aSAlexandre Belloni #include <net/arp.h>
20a556c76aSAlexandre Belloni #include <net/netevent.h>
21a556c76aSAlexandre Belloni #include <net/rtnetlink.h>
22a556c76aSAlexandre Belloni #include <net/switchdev.h>
23a556c76aSAlexandre Belloni 
24a556c76aSAlexandre Belloni #include "ocelot.h"
25*b5962294SHoratiu Vultur #include "ocelot_ace.h"
26a556c76aSAlexandre Belloni 
27639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
28639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
29639c1b26SSteen Hegelund 
30a556c76aSAlexandre Belloni /* MAC table entry types.
31a556c76aSAlexandre Belloni  * ENTRYTYPE_NORMAL is subject to aging.
32a556c76aSAlexandre Belloni  * ENTRYTYPE_LOCKED is not subject to aging.
33a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
34a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
35a556c76aSAlexandre Belloni  */
36a556c76aSAlexandre Belloni enum macaccess_entry_type {
37a556c76aSAlexandre Belloni 	ENTRYTYPE_NORMAL = 0,
38a556c76aSAlexandre Belloni 	ENTRYTYPE_LOCKED,
39a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv4,
40a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv6,
41a556c76aSAlexandre Belloni };
42a556c76aSAlexandre Belloni 
43a556c76aSAlexandre Belloni struct ocelot_mact_entry {
44a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
45a556c76aSAlexandre Belloni 	u16 vid;
46a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
47a556c76aSAlexandre Belloni };
48a556c76aSAlexandre Belloni 
49639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
50639c1b26SSteen Hegelund {
51639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
52639c1b26SSteen Hegelund }
53639c1b26SSteen Hegelund 
54a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
55a556c76aSAlexandre Belloni {
56639c1b26SSteen Hegelund 	u32 val;
57a556c76aSAlexandre Belloni 
58639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
59639c1b26SSteen Hegelund 		ocelot, val,
60639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
61639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
62639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
63a556c76aSAlexandre Belloni }
64a556c76aSAlexandre Belloni 
65a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
66a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
67a556c76aSAlexandre Belloni 			       unsigned int vid)
68a556c76aSAlexandre Belloni {
69a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
70a556c76aSAlexandre Belloni 
71a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
72a556c76aSAlexandre Belloni 	 * understood by the hardware.
73a556c76aSAlexandre Belloni 	 */
74a556c76aSAlexandre Belloni 	mach |= vid    << 16;
75a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
76a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
77a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
78a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
79a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
80a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
81a556c76aSAlexandre Belloni 
82a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
83a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
84a556c76aSAlexandre Belloni 
85a556c76aSAlexandre Belloni }
86a556c76aSAlexandre Belloni 
87a556c76aSAlexandre Belloni static int ocelot_mact_learn(struct ocelot *ocelot, int port,
88a556c76aSAlexandre Belloni 			     const unsigned char mac[ETH_ALEN],
89a556c76aSAlexandre Belloni 			     unsigned int vid,
90a556c76aSAlexandre Belloni 			     enum macaccess_entry_type type)
91a556c76aSAlexandre Belloni {
92a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
93a556c76aSAlexandre Belloni 
94a556c76aSAlexandre Belloni 	/* Issue a write command */
95a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
96a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
97a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
98a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
99a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
100a556c76aSAlexandre Belloni 
101a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
102a556c76aSAlexandre Belloni }
103a556c76aSAlexandre Belloni 
104a556c76aSAlexandre Belloni static int ocelot_mact_forget(struct ocelot *ocelot,
105a556c76aSAlexandre Belloni 			      const unsigned char mac[ETH_ALEN],
106a556c76aSAlexandre Belloni 			      unsigned int vid)
107a556c76aSAlexandre Belloni {
108a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
109a556c76aSAlexandre Belloni 
110a556c76aSAlexandre Belloni 	/* Issue a forget command */
111a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
112a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
113a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
114a556c76aSAlexandre Belloni 
115a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
116a556c76aSAlexandre Belloni }
117a556c76aSAlexandre Belloni 
118a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
119a556c76aSAlexandre Belloni {
120a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
121a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
122a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
123a556c76aSAlexandre Belloni 	 */
124a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
125a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
126a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
127a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
128a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
129a556c76aSAlexandre Belloni 
130a556c76aSAlexandre Belloni 	/* Clear the MAC table */
131a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
132a556c76aSAlexandre Belloni }
133a556c76aSAlexandre Belloni 
134*b5962294SHoratiu Vultur static void ocelot_vcap_enable(struct ocelot *ocelot, struct ocelot_port *port)
135*b5962294SHoratiu Vultur {
136*b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
137*b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
138*b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG, port->chip_port);
139*b5962294SHoratiu Vultur }
140*b5962294SHoratiu Vultur 
141639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
142639c1b26SSteen Hegelund {
143639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
144639c1b26SSteen Hegelund }
145639c1b26SSteen Hegelund 
146a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
147a556c76aSAlexandre Belloni {
148639c1b26SSteen Hegelund 	u32 val;
149a556c76aSAlexandre Belloni 
150639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
151639c1b26SSteen Hegelund 		ocelot,
152639c1b26SSteen Hegelund 		val,
153639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
154639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
155639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
156a556c76aSAlexandre Belloni }
157a556c76aSAlexandre Belloni 
1587142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1597142529fSAntoine Tenart {
1607142529fSAntoine Tenart 	/* Select the VID to configure */
1617142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1627142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1637142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1647142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1657142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1667142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1677142529fSAntoine Tenart 
1687142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1697142529fSAntoine Tenart }
1707142529fSAntoine Tenart 
1717142529fSAntoine Tenart static void ocelot_vlan_mode(struct ocelot_port *port,
1727142529fSAntoine Tenart 			     netdev_features_t features)
1737142529fSAntoine Tenart {
1747142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
1757142529fSAntoine Tenart 	u8 p = port->chip_port;
1767142529fSAntoine Tenart 	u32 val;
1777142529fSAntoine Tenart 
1787142529fSAntoine Tenart 	/* Filtering */
1797142529fSAntoine Tenart 	val = ocelot_read(ocelot, ANA_VLANMASK);
1807142529fSAntoine Tenart 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1817142529fSAntoine Tenart 		val |= BIT(p);
1827142529fSAntoine Tenart 	else
1837142529fSAntoine Tenart 		val &= ~BIT(p);
1847142529fSAntoine Tenart 	ocelot_write(ocelot, val, ANA_VLANMASK);
1857142529fSAntoine Tenart }
1867142529fSAntoine Tenart 
1877142529fSAntoine Tenart static void ocelot_vlan_port_apply(struct ocelot *ocelot,
1887142529fSAntoine Tenart 				   struct ocelot_port *port)
1897142529fSAntoine Tenart {
1907142529fSAntoine Tenart 	u32 val;
1917142529fSAntoine Tenart 
1927142529fSAntoine Tenart 	/* Ingress clasification (ANA_PORT_VLAN_CFG) */
1937142529fSAntoine Tenart 	/* Default vlan to clasify for untagged frames (may be zero) */
1947142529fSAntoine Tenart 	val = ANA_PORT_VLAN_CFG_VLAN_VID(port->pvid);
1957142529fSAntoine Tenart 	if (port->vlan_aware)
1967142529fSAntoine Tenart 		val |= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1977142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
1987142529fSAntoine Tenart 
1997142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2007142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_VID_M |
2017142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2027142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
2037142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG, port->chip_port);
2047142529fSAntoine Tenart 
2057142529fSAntoine Tenart 	/* Drop frames with multicast source address */
2067142529fSAntoine Tenart 	val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
2077142529fSAntoine Tenart 	if (port->vlan_aware && !port->vid)
2087142529fSAntoine Tenart 		/* If port is vlan-aware and tagged, drop untagged and priority
2097142529fSAntoine Tenart 		 * tagged frames.
2107142529fSAntoine Tenart 		 */
2117142529fSAntoine Tenart 		val |= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
2127142529fSAntoine Tenart 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
2137142529fSAntoine Tenart 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
2147142529fSAntoine Tenart 	ocelot_write_gix(ocelot, val, ANA_PORT_DROP_CFG, port->chip_port);
2157142529fSAntoine Tenart 
2167142529fSAntoine Tenart 	/* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q. */
2177142529fSAntoine Tenart 	val = REW_TAG_CFG_TAG_TPID_CFG(0);
2187142529fSAntoine Tenart 
2197142529fSAntoine Tenart 	if (port->vlan_aware) {
2207142529fSAntoine Tenart 		if (port->vid)
2217142529fSAntoine Tenart 			/* Tag all frames except when VID == DEFAULT_VLAN */
2227142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(1);
2237142529fSAntoine Tenart 		else
2247142529fSAntoine Tenart 			/* Tag all frames */
2257142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(3);
2267142529fSAntoine Tenart 	}
2277142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2287142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_TPID_CFG_M |
2297142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_CFG_M,
2307142529fSAntoine Tenart 		       REW_TAG_CFG, port->chip_port);
2317142529fSAntoine Tenart 
2327142529fSAntoine Tenart 	/* Set default VLAN and tag type to 8021Q. */
2337142529fSAntoine Tenart 	val = REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q) |
2347142529fSAntoine Tenart 	      REW_PORT_VLAN_CFG_PORT_VID(port->vid);
2357142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2367142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_TPID_M |
2377142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
2387142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG, port->chip_port);
2397142529fSAntoine Tenart }
2407142529fSAntoine Tenart 
2417142529fSAntoine Tenart static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
2427142529fSAntoine Tenart 			       bool untagged)
2437142529fSAntoine Tenart {
2447142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
2457142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
2467142529fSAntoine Tenart 	int ret;
2477142529fSAntoine Tenart 
2487142529fSAntoine Tenart 	/* Add the port MAC address to with the right VLAN information */
2497142529fSAntoine Tenart 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
2507142529fSAntoine Tenart 			  ENTRYTYPE_LOCKED);
2517142529fSAntoine Tenart 
2527142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
2537142529fSAntoine Tenart 	ocelot->vlan_mask[vid] |= BIT(port->chip_port);
2547142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2557142529fSAntoine Tenart 	if (ret)
2567142529fSAntoine Tenart 		return ret;
2577142529fSAntoine Tenart 
2587142529fSAntoine Tenart 	/* Default ingress vlan classification */
2597142529fSAntoine Tenart 	if (pvid)
2607142529fSAntoine Tenart 		port->pvid = vid;
2617142529fSAntoine Tenart 
2627142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
2637142529fSAntoine Tenart 	if (untagged)
2647142529fSAntoine Tenart 		port->vid = vid;
2657142529fSAntoine Tenart 
2667142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
2677142529fSAntoine Tenart 
2687142529fSAntoine Tenart 	return 0;
2697142529fSAntoine Tenart }
2707142529fSAntoine Tenart 
2717142529fSAntoine Tenart static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
2727142529fSAntoine Tenart {
2737142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
2747142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
2757142529fSAntoine Tenart 	int ret;
2767142529fSAntoine Tenart 
2777142529fSAntoine Tenart 	/* 8021q removes VID 0 on module unload for all interfaces
2787142529fSAntoine Tenart 	 * with VLAN filtering feature. We need to keep it to receive
2797142529fSAntoine Tenart 	 * untagged traffic.
2807142529fSAntoine Tenart 	 */
2817142529fSAntoine Tenart 	if (vid == 0)
2827142529fSAntoine Tenart 		return 0;
2837142529fSAntoine Tenart 
2847142529fSAntoine Tenart 	/* Del the port MAC address to with the right VLAN information */
2857142529fSAntoine Tenart 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
2867142529fSAntoine Tenart 
2877142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
2887142529fSAntoine Tenart 	ocelot->vlan_mask[vid] &= ~BIT(port->chip_port);
2897142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2907142529fSAntoine Tenart 	if (ret)
2917142529fSAntoine Tenart 		return ret;
2927142529fSAntoine Tenart 
2937142529fSAntoine Tenart 	/* Ingress */
2947142529fSAntoine Tenart 	if (port->pvid == vid)
2957142529fSAntoine Tenart 		port->pvid = 0;
2967142529fSAntoine Tenart 
2977142529fSAntoine Tenart 	/* Egress */
2987142529fSAntoine Tenart 	if (port->vid == vid)
2997142529fSAntoine Tenart 		port->vid = 0;
3007142529fSAntoine Tenart 
3017142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
3027142529fSAntoine Tenart 
3037142529fSAntoine Tenart 	return 0;
3047142529fSAntoine Tenart }
3057142529fSAntoine Tenart 
306a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
307a556c76aSAlexandre Belloni {
3087142529fSAntoine Tenart 	u16 port, vid;
3097142529fSAntoine Tenart 
310a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
311a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
312a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
313a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3147142529fSAntoine Tenart 
3157142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3167142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3177142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3187142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3197142529fSAntoine Tenart 	}
3207142529fSAntoine Tenart 
3217142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3227142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3237142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3247142529fSAntoine Tenart 	 */
3257142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3267142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3277142529fSAntoine Tenart 
3287142529fSAntoine Tenart 	/* Configure the CPU port to be VLAN aware */
3297142529fSAntoine Tenart 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
3307142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
3317142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
3327142529fSAntoine Tenart 			 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
3337142529fSAntoine Tenart 
3347142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3357142529fSAntoine Tenart 	 * default.
3367142529fSAntoine Tenart 	 */
3377142529fSAntoine Tenart 	ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
3387142529fSAntoine Tenart 
3397142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3407142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3417142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3427142529fSAntoine Tenart 	}
343a556c76aSAlexandre Belloni }
344a556c76aSAlexandre Belloni 
345a556c76aSAlexandre Belloni /* Watermark encode
346a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
347a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
348a556c76aSAlexandre Belloni  */
349a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
350a556c76aSAlexandre Belloni {
351a556c76aSAlexandre Belloni 	if (value >= BIT(8))
352a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
353a556c76aSAlexandre Belloni 
354a556c76aSAlexandre Belloni 	return value;
355a556c76aSAlexandre Belloni }
356a556c76aSAlexandre Belloni 
357a556c76aSAlexandre Belloni static void ocelot_port_adjust_link(struct net_device *dev)
358a556c76aSAlexandre Belloni {
359a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
360a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
361a556c76aSAlexandre Belloni 	u8 p = port->chip_port;
362a556c76aSAlexandre Belloni 	int speed, atop_wm, mode = 0;
363a556c76aSAlexandre Belloni 
364a556c76aSAlexandre Belloni 	switch (dev->phydev->speed) {
365a556c76aSAlexandre Belloni 	case SPEED_10:
366a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
367a556c76aSAlexandre Belloni 		break;
368a556c76aSAlexandre Belloni 	case SPEED_100:
369a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
370a556c76aSAlexandre Belloni 		break;
371a556c76aSAlexandre Belloni 	case SPEED_1000:
372a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
373a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
374a556c76aSAlexandre Belloni 		break;
375a556c76aSAlexandre Belloni 	case SPEED_2500:
376a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
377a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
378a556c76aSAlexandre Belloni 		break;
379a556c76aSAlexandre Belloni 	default:
380a556c76aSAlexandre Belloni 		netdev_err(dev, "Unsupported PHY speed: %d\n",
381a556c76aSAlexandre Belloni 			   dev->phydev->speed);
382a556c76aSAlexandre Belloni 		return;
383a556c76aSAlexandre Belloni 	}
384a556c76aSAlexandre Belloni 
385a556c76aSAlexandre Belloni 	phy_print_status(dev->phydev);
386a556c76aSAlexandre Belloni 
387a556c76aSAlexandre Belloni 	if (!dev->phydev->link)
388a556c76aSAlexandre Belloni 		return;
389a556c76aSAlexandre Belloni 
390a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
391a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
392a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
393a556c76aSAlexandre Belloni 
394a556c76aSAlexandre Belloni 	/* Set MAC IFG Gaps
395a556c76aSAlexandre Belloni 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
396a556c76aSAlexandre Belloni 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
397a556c76aSAlexandre Belloni 	 */
398a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
399a556c76aSAlexandre Belloni 
400a556c76aSAlexandre Belloni 	/* Load seed (0) and set MAC HDX late collision  */
401a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
402a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG_SEED_LOAD,
403a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
404a556c76aSAlexandre Belloni 	mdelay(1);
405a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
406a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
407a556c76aSAlexandre Belloni 
408a556c76aSAlexandre Belloni 	/* Disable HDX fast control */
409a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
410a556c76aSAlexandre Belloni 
411a556c76aSAlexandre Belloni 	/* SGMII only for now */
412a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
413a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
414a556c76aSAlexandre Belloni 
415a556c76aSAlexandre Belloni 	/* Enable PCS */
416a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
417a556c76aSAlexandre Belloni 
418a556c76aSAlexandre Belloni 	/* No aneg on SGMII */
419a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
420a556c76aSAlexandre Belloni 
421a556c76aSAlexandre Belloni 	/* No loopback */
422a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_LB_CFG);
423a556c76aSAlexandre Belloni 
424a556c76aSAlexandre Belloni 	/* Set Max Length and maximum tags allowed */
425a556c76aSAlexandre Belloni 	ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
426a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
427a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
428a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
429a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG);
430a556c76aSAlexandre Belloni 
431a556c76aSAlexandre Belloni 	/* Enable MAC module */
432a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
433a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
434a556c76aSAlexandre Belloni 
435a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
436a556c76aSAlexandre Belloni 	 * reset */
437a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
438a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
439a556c76aSAlexandre Belloni 
440a556c76aSAlexandre Belloni 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
441a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
442a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
443a556c76aSAlexandre Belloni 
444a556c76aSAlexandre Belloni 	/* No PFC */
445a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
446a556c76aSAlexandre Belloni 			 ANA_PFC_PFC_CFG, p);
447a556c76aSAlexandre Belloni 
448a556c76aSAlexandre Belloni 	/* Set Pause WM hysteresis
449a556c76aSAlexandre Belloni 	 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
450a556c76aSAlexandre Belloni 	 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
451a556c76aSAlexandre Belloni 	 */
452a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
453a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
454a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
455a556c76aSAlexandre Belloni 
456a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
457a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
458a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
459a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
460a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, p);
461a556c76aSAlexandre Belloni 
462a556c76aSAlexandre Belloni 	/* Flow control */
463a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
464a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
465a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
466a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
467a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
468a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG, p);
469a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
470a556c76aSAlexandre Belloni 
471a556c76aSAlexandre Belloni 	/* Tail dropping watermark */
472a556c76aSAlexandre Belloni 	atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
473a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
474a556c76aSAlexandre Belloni 			 SYS_ATOP, p);
475a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
476a556c76aSAlexandre Belloni }
477a556c76aSAlexandre Belloni 
478a556c76aSAlexandre Belloni static int ocelot_port_open(struct net_device *dev)
479a556c76aSAlexandre Belloni {
480a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
481a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
482a556c76aSAlexandre Belloni 	int err;
483a556c76aSAlexandre Belloni 
484a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
485a556c76aSAlexandre Belloni 	 * MAC addresses.
486a556c76aSAlexandre Belloni 	 */
487a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
488a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
489a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
490a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, port->chip_port);
491a556c76aSAlexandre Belloni 
49271e32a20SQuentin Schulz 	if (port->serdes) {
493c8fe6d7fSGrygorii Strashko 		err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
494c8fe6d7fSGrygorii Strashko 				       port->phy_mode);
49571e32a20SQuentin Schulz 		if (err) {
49671e32a20SQuentin Schulz 			netdev_err(dev, "Could not set mode of SerDes\n");
49771e32a20SQuentin Schulz 			return err;
49871e32a20SQuentin Schulz 		}
49971e32a20SQuentin Schulz 	}
50071e32a20SQuentin Schulz 
501a556c76aSAlexandre Belloni 	err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
50271e32a20SQuentin Schulz 				 port->phy_mode);
503a556c76aSAlexandre Belloni 	if (err) {
504a556c76aSAlexandre Belloni 		netdev_err(dev, "Could not attach to PHY\n");
505a556c76aSAlexandre Belloni 		return err;
506a556c76aSAlexandre Belloni 	}
507a556c76aSAlexandre Belloni 
508a556c76aSAlexandre Belloni 	dev->phydev = port->phy;
509a556c76aSAlexandre Belloni 
510a556c76aSAlexandre Belloni 	phy_attached_info(port->phy);
511a556c76aSAlexandre Belloni 	phy_start(port->phy);
512a556c76aSAlexandre Belloni 	return 0;
513a556c76aSAlexandre Belloni }
514a556c76aSAlexandre Belloni 
515a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev)
516a556c76aSAlexandre Belloni {
517a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
518a556c76aSAlexandre Belloni 
519a556c76aSAlexandre Belloni 	phy_disconnect(port->phy);
520a556c76aSAlexandre Belloni 
521a556c76aSAlexandre Belloni 	dev->phydev = NULL;
522a556c76aSAlexandre Belloni 
523a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
524a556c76aSAlexandre Belloni 	ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
525a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, port->chip_port);
526a556c76aSAlexandre Belloni 	return 0;
527a556c76aSAlexandre Belloni }
528a556c76aSAlexandre Belloni 
529a556c76aSAlexandre Belloni /* Generate the IFH for frame injection
530a556c76aSAlexandre Belloni  *
531a556c76aSAlexandre Belloni  * The IFH is a 128bit-value
532a556c76aSAlexandre Belloni  * bit 127: bypass the analyzer processing
533a556c76aSAlexandre Belloni  * bit 56-67: destination mask
534a556c76aSAlexandre Belloni  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
535a556c76aSAlexandre Belloni  * bit 20-27: cpu extraction queue mask
536a556c76aSAlexandre Belloni  * bit 16: tag type 0: C-tag, 1: S-tag
537a556c76aSAlexandre Belloni  * bit 0-11: VID
538a556c76aSAlexandre Belloni  */
539a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
540a556c76aSAlexandre Belloni {
541a556c76aSAlexandre Belloni 	ifh[0] = IFH_INJ_BYPASS;
54208d02364SAntoine Tenart 	ifh[1] = (0xf00 & info->port) >> 8;
543a556c76aSAlexandre Belloni 	ifh[2] = (0xff & info->port) << 24;
54408d02364SAntoine Tenart 	ifh[3] = (info->tag_type << 16) | info->vid;
545a556c76aSAlexandre Belloni 
546a556c76aSAlexandre Belloni 	return 0;
547a556c76aSAlexandre Belloni }
548a556c76aSAlexandre Belloni 
549a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
550a556c76aSAlexandre Belloni {
551a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
552a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
553a556c76aSAlexandre Belloni 	u32 val, ifh[IFH_LEN];
554a556c76aSAlexandre Belloni 	struct frame_info info = {};
555a556c76aSAlexandre Belloni 	u8 grp = 0; /* Send everything on CPU group 0 */
556a556c76aSAlexandre Belloni 	unsigned int i, count, last;
557a556c76aSAlexandre Belloni 
558a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, QS_INJ_STATUS);
559a556c76aSAlexandre Belloni 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
560a556c76aSAlexandre Belloni 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
561a556c76aSAlexandre Belloni 		return NETDEV_TX_BUSY;
562a556c76aSAlexandre Belloni 
563a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
564a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
565a556c76aSAlexandre Belloni 
566a556c76aSAlexandre Belloni 	info.port = BIT(port->chip_port);
56708d02364SAntoine Tenart 	info.tag_type = IFH_TAG_TYPE_C;
56808d02364SAntoine Tenart 	info.vid = skb_vlan_tag_get(skb);
569a556c76aSAlexandre Belloni 	ocelot_gen_ifh(ifh, &info);
570a556c76aSAlexandre Belloni 
571a556c76aSAlexandre Belloni 	for (i = 0; i < IFH_LEN; i++)
572c2cd650bSAntoine Tenart 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
573c2cd650bSAntoine Tenart 				 QS_INJ_WR, grp);
574a556c76aSAlexandre Belloni 
575a556c76aSAlexandre Belloni 	count = (skb->len + 3) / 4;
576a556c76aSAlexandre Belloni 	last = skb->len % 4;
577a556c76aSAlexandre Belloni 	for (i = 0; i < count; i++) {
578a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
579a556c76aSAlexandre Belloni 	}
580a556c76aSAlexandre Belloni 
581a556c76aSAlexandre Belloni 	/* Add padding */
582a556c76aSAlexandre Belloni 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
583a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
584a556c76aSAlexandre Belloni 		i++;
585a556c76aSAlexandre Belloni 	}
586a556c76aSAlexandre Belloni 
587a556c76aSAlexandre Belloni 	/* Indicate EOF and valid bytes in last word */
588a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
589a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
590a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_EOF,
591a556c76aSAlexandre Belloni 			 QS_INJ_CTRL, grp);
592a556c76aSAlexandre Belloni 
593a556c76aSAlexandre Belloni 	/* Add dummy CRC */
594a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
595a556c76aSAlexandre Belloni 	skb_tx_timestamp(skb);
596a556c76aSAlexandre Belloni 
597a556c76aSAlexandre Belloni 	dev->stats.tx_packets++;
598a556c76aSAlexandre Belloni 	dev->stats.tx_bytes += skb->len;
599a556c76aSAlexandre Belloni 	dev_kfree_skb_any(skb);
600a556c76aSAlexandre Belloni 
601a556c76aSAlexandre Belloni 	return NETDEV_TX_OK;
602a556c76aSAlexandre Belloni }
603a556c76aSAlexandre Belloni 
60440a1578dSClaudiu Manoil static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
605a556c76aSAlexandre Belloni {
60640a1578dSClaudiu Manoil 	struct ocelot_port *port = netdev_priv(dev);
607a556c76aSAlexandre Belloni 
60840a1578dSClaudiu Manoil 	return ocelot_mact_forget(port->ocelot, addr, port->pvid);
609a556c76aSAlexandre Belloni }
610a556c76aSAlexandre Belloni 
61140a1578dSClaudiu Manoil static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
612a556c76aSAlexandre Belloni {
61340a1578dSClaudiu Manoil 	struct ocelot_port *port = netdev_priv(dev);
614a556c76aSAlexandre Belloni 
61540a1578dSClaudiu Manoil 	return ocelot_mact_learn(port->ocelot, PGID_CPU, addr, port->pvid,
616a556c76aSAlexandre Belloni 				 ENTRYTYPE_LOCKED);
617a556c76aSAlexandre Belloni }
618a556c76aSAlexandre Belloni 
619a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev)
620a556c76aSAlexandre Belloni {
621a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
622a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
623a556c76aSAlexandre Belloni 	int i;
624a556c76aSAlexandre Belloni 	u32 val;
625a556c76aSAlexandre Belloni 
626a556c76aSAlexandre Belloni 	/* This doesn't handle promiscuous mode because the bridge core is
627a556c76aSAlexandre Belloni 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
628a556c76aSAlexandre Belloni 	 * forwarded to the CPU port.
629a556c76aSAlexandre Belloni 	 */
630a556c76aSAlexandre Belloni 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
631a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
632a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
633a556c76aSAlexandre Belloni 
63440a1578dSClaudiu Manoil 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
635a556c76aSAlexandre Belloni }
636a556c76aSAlexandre Belloni 
637a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev,
638a556c76aSAlexandre Belloni 					  char *buf, size_t len)
639a556c76aSAlexandre Belloni {
640a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
641a556c76aSAlexandre Belloni 	int ret;
642a556c76aSAlexandre Belloni 
643a556c76aSAlexandre Belloni 	ret = snprintf(buf, len, "p%d", port->chip_port);
644a556c76aSAlexandre Belloni 	if (ret >= len)
645a556c76aSAlexandre Belloni 		return -EINVAL;
646a556c76aSAlexandre Belloni 
647a556c76aSAlexandre Belloni 	return 0;
648a556c76aSAlexandre Belloni }
649a556c76aSAlexandre Belloni 
650a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
651a556c76aSAlexandre Belloni {
652a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
653a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
654a556c76aSAlexandre Belloni 	const struct sockaddr *addr = p;
655a556c76aSAlexandre Belloni 
656a556c76aSAlexandre Belloni 	/* Learn the new net device MAC address in the mac table. */
657a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
658a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
659a556c76aSAlexandre Belloni 	/* Then forget the previous one. */
660a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
661a556c76aSAlexandre Belloni 
662a556c76aSAlexandre Belloni 	ether_addr_copy(dev->dev_addr, addr->sa_data);
663a556c76aSAlexandre Belloni 	return 0;
664a556c76aSAlexandre Belloni }
665a556c76aSAlexandre Belloni 
666a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev,
667a556c76aSAlexandre Belloni 			       struct rtnl_link_stats64 *stats)
668a556c76aSAlexandre Belloni {
669a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
670a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
671a556c76aSAlexandre Belloni 
672a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
673a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
674a556c76aSAlexandre Belloni 		     SYS_STAT_CFG);
675a556c76aSAlexandre Belloni 
676a556c76aSAlexandre Belloni 	/* Get Rx stats */
677a556c76aSAlexandre Belloni 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
678a556c76aSAlexandre Belloni 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
679a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
680a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
681a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
682a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
683a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
684a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
685a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
686a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
687a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
688a556c76aSAlexandre Belloni 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
689a556c76aSAlexandre Belloni 	stats->rx_dropped = dev->stats.rx_dropped;
690a556c76aSAlexandre Belloni 
691a556c76aSAlexandre Belloni 	/* Get Tx stats */
692a556c76aSAlexandre Belloni 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
693a556c76aSAlexandre Belloni 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
694a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
695a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
696a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
697a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
698a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
699a556c76aSAlexandre Belloni 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
700a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
701a556c76aSAlexandre Belloni 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
702a556c76aSAlexandre Belloni }
703a556c76aSAlexandre Belloni 
704a556c76aSAlexandre Belloni static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
705a556c76aSAlexandre Belloni 			  struct net_device *dev, const unsigned char *addr,
70687b0984eSPetr Machata 			  u16 vid, u16 flags,
70787b0984eSPetr Machata 			  struct netlink_ext_ack *extack)
708a556c76aSAlexandre Belloni {
709a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
710a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
711a556c76aSAlexandre Belloni 
7127142529fSAntoine Tenart 	if (!vid) {
7137142529fSAntoine Tenart 		if (!port->vlan_aware)
7147142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
7157142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
7167142529fSAntoine Tenart 			 * matches incoming untagged packets
7177142529fSAntoine Tenart 			 */
7187142529fSAntoine Tenart 			vid = port->pvid;
7197142529fSAntoine Tenart 		else
7207142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
7217142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
7227142529fSAntoine Tenart 			 */
7237142529fSAntoine Tenart 			return -EINVAL;
7247142529fSAntoine Tenart 	}
7257142529fSAntoine Tenart 
726a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
7278fd1a4afSAllan W. Nielsen 				 ENTRYTYPE_LOCKED);
728a556c76aSAlexandre Belloni }
729a556c76aSAlexandre Belloni 
730a556c76aSAlexandre Belloni static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
731a556c76aSAlexandre Belloni 			  struct net_device *dev,
732a556c76aSAlexandre Belloni 			  const unsigned char *addr, u16 vid)
733a556c76aSAlexandre Belloni {
734a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
735a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
736a556c76aSAlexandre Belloni 
737a556c76aSAlexandre Belloni 	return ocelot_mact_forget(ocelot, addr, vid);
738a556c76aSAlexandre Belloni }
739a556c76aSAlexandre Belloni 
740a556c76aSAlexandre Belloni struct ocelot_dump_ctx {
741a556c76aSAlexandre Belloni 	struct net_device *dev;
742a556c76aSAlexandre Belloni 	struct sk_buff *skb;
743a556c76aSAlexandre Belloni 	struct netlink_callback *cb;
744a556c76aSAlexandre Belloni 	int idx;
745a556c76aSAlexandre Belloni };
746a556c76aSAlexandre Belloni 
747a556c76aSAlexandre Belloni static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
748a556c76aSAlexandre Belloni 			      struct ocelot_dump_ctx *dump)
749a556c76aSAlexandre Belloni {
750a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
751a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
752a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
753a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
754a556c76aSAlexandre Belloni 
755a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
756a556c76aSAlexandre Belloni 		goto skip;
757a556c76aSAlexandre Belloni 
758a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
759a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
760a556c76aSAlexandre Belloni 	if (!nlh)
761a556c76aSAlexandre Belloni 		return -EMSGSIZE;
762a556c76aSAlexandre Belloni 
763a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
764a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
765a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
766a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
767a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
768a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
769a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
770a556c76aSAlexandre Belloni 	ndm->ndm_state   = NUD_REACHABLE;
771a556c76aSAlexandre Belloni 
772a556c76aSAlexandre Belloni 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
773a556c76aSAlexandre Belloni 		goto nla_put_failure;
774a556c76aSAlexandre Belloni 
775a556c76aSAlexandre Belloni 	if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
776a556c76aSAlexandre Belloni 		goto nla_put_failure;
777a556c76aSAlexandre Belloni 
778a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
779a556c76aSAlexandre Belloni 
780a556c76aSAlexandre Belloni skip:
781a556c76aSAlexandre Belloni 	dump->idx++;
782a556c76aSAlexandre Belloni 	return 0;
783a556c76aSAlexandre Belloni 
784a556c76aSAlexandre Belloni nla_put_failure:
785a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
786a556c76aSAlexandre Belloni 	return -EMSGSIZE;
787a556c76aSAlexandre Belloni }
788a556c76aSAlexandre Belloni 
789a556c76aSAlexandre Belloni static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
790a556c76aSAlexandre Belloni 				   struct ocelot_mact_entry *entry)
791a556c76aSAlexandre Belloni {
792a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
793a556c76aSAlexandre Belloni 	char mac[ETH_ALEN];
794a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
795a556c76aSAlexandre Belloni 
796a556c76aSAlexandre Belloni 	/* Set row and column to read from */
797a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
798a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
799a556c76aSAlexandre Belloni 
800a556c76aSAlexandre Belloni 	/* Issue a read command */
801a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
802a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
803a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
804a556c76aSAlexandre Belloni 
805a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
806a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
807a556c76aSAlexandre Belloni 
808a556c76aSAlexandre Belloni 	/* Read the entry flags */
809a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
810a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
811a556c76aSAlexandre Belloni 		return -EINVAL;
812a556c76aSAlexandre Belloni 
813a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
814a556c76aSAlexandre Belloni 	 * do not report it.
815a556c76aSAlexandre Belloni 	 */
816a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
817a556c76aSAlexandre Belloni 	if (dst != port->chip_port)
818a556c76aSAlexandre Belloni 		return -EINVAL;
819a556c76aSAlexandre Belloni 
820a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
821a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
822a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
823a556c76aSAlexandre Belloni 
824a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
825a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
826a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
827a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
828a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
829a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
830a556c76aSAlexandre Belloni 
831a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
832a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
833a556c76aSAlexandre Belloni 
834a556c76aSAlexandre Belloni 	return 0;
835a556c76aSAlexandre Belloni }
836a556c76aSAlexandre Belloni 
837a556c76aSAlexandre Belloni static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
838a556c76aSAlexandre Belloni 			   struct net_device *dev,
839a556c76aSAlexandre Belloni 			   struct net_device *filter_dev, int *idx)
840a556c76aSAlexandre Belloni {
841a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
842a556c76aSAlexandre Belloni 	int i, j, ret = 0;
843a556c76aSAlexandre Belloni 	struct ocelot_dump_ctx dump = {
844a556c76aSAlexandre Belloni 		.dev = dev,
845a556c76aSAlexandre Belloni 		.skb = skb,
846a556c76aSAlexandre Belloni 		.cb = cb,
847a556c76aSAlexandre Belloni 		.idx = *idx,
848a556c76aSAlexandre Belloni 	};
849a556c76aSAlexandre Belloni 
850a556c76aSAlexandre Belloni 	struct ocelot_mact_entry entry;
851a556c76aSAlexandre Belloni 
852a556c76aSAlexandre Belloni 	/* Loop through all the mac tables entries. There are 1024 rows of 4
853a556c76aSAlexandre Belloni 	 * entries.
854a556c76aSAlexandre Belloni 	 */
855a556c76aSAlexandre Belloni 	for (i = 0; i < 1024; i++) {
856a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
857a556c76aSAlexandre Belloni 			ret = ocelot_mact_read(port, i, j, &entry);
858a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
859a556c76aSAlexandre Belloni 			 * skip it.
860a556c76aSAlexandre Belloni 			 */
861a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
862a556c76aSAlexandre Belloni 				continue;
863a556c76aSAlexandre Belloni 			else if (ret)
864a556c76aSAlexandre Belloni 				goto end;
865a556c76aSAlexandre Belloni 
866a556c76aSAlexandre Belloni 			ret = ocelot_fdb_do_dump(&entry, &dump);
867a556c76aSAlexandre Belloni 			if (ret)
868a556c76aSAlexandre Belloni 				goto end;
869a556c76aSAlexandre Belloni 		}
870a556c76aSAlexandre Belloni 	}
871a556c76aSAlexandre Belloni 
872a556c76aSAlexandre Belloni end:
873a556c76aSAlexandre Belloni 	*idx = dump.idx;
874a556c76aSAlexandre Belloni 	return ret;
875a556c76aSAlexandre Belloni }
876a556c76aSAlexandre Belloni 
8777142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
8787142529fSAntoine Tenart 				  u16 vid)
8797142529fSAntoine Tenart {
8807142529fSAntoine Tenart 	return ocelot_vlan_vid_add(dev, vid, false, true);
8817142529fSAntoine Tenart }
8827142529fSAntoine Tenart 
8837142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
8847142529fSAntoine Tenart 				   u16 vid)
8857142529fSAntoine Tenart {
8867142529fSAntoine Tenart 	return ocelot_vlan_vid_del(dev, vid);
8877142529fSAntoine Tenart }
8887142529fSAntoine Tenart 
8897142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev,
8907142529fSAntoine Tenart 			       netdev_features_t features)
8917142529fSAntoine Tenart {
8927142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
8937142529fSAntoine Tenart 	netdev_features_t changed = dev->features ^ features;
8947142529fSAntoine Tenart 
8952c1d029aSJoergen Andreasen 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
8962c1d029aSJoergen Andreasen 	    port->tc.offload_cnt) {
8972c1d029aSJoergen Andreasen 		netdev_err(dev,
8982c1d029aSJoergen Andreasen 			   "Cannot disable HW TC offload while offloads active\n");
8992c1d029aSJoergen Andreasen 		return -EBUSY;
9002c1d029aSJoergen Andreasen 	}
9012c1d029aSJoergen Andreasen 
9027142529fSAntoine Tenart 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
9037142529fSAntoine Tenart 		ocelot_vlan_mode(port, features);
9047142529fSAntoine Tenart 
9057142529fSAntoine Tenart 	return 0;
9067142529fSAntoine Tenart }
9077142529fSAntoine Tenart 
908751302c3SFlorian Fainelli static int ocelot_get_port_parent_id(struct net_device *dev,
909751302c3SFlorian Fainelli 				     struct netdev_phys_item_id *ppid)
910751302c3SFlorian Fainelli {
911751302c3SFlorian Fainelli 	struct ocelot_port *ocelot_port = netdev_priv(dev);
912751302c3SFlorian Fainelli 	struct ocelot *ocelot = ocelot_port->ocelot;
913751302c3SFlorian Fainelli 
914751302c3SFlorian Fainelli 	ppid->id_len = sizeof(ocelot->base_mac);
915751302c3SFlorian Fainelli 	memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
916751302c3SFlorian Fainelli 
917751302c3SFlorian Fainelli 	return 0;
918751302c3SFlorian Fainelli }
919751302c3SFlorian Fainelli 
920a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = {
921a556c76aSAlexandre Belloni 	.ndo_open			= ocelot_port_open,
922a556c76aSAlexandre Belloni 	.ndo_stop			= ocelot_port_stop,
923a556c76aSAlexandre Belloni 	.ndo_start_xmit			= ocelot_port_xmit,
924a556c76aSAlexandre Belloni 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
925a556c76aSAlexandre Belloni 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
926a556c76aSAlexandre Belloni 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
927a556c76aSAlexandre Belloni 	.ndo_get_stats64		= ocelot_get_stats64,
928a556c76aSAlexandre Belloni 	.ndo_fdb_add			= ocelot_fdb_add,
929a556c76aSAlexandre Belloni 	.ndo_fdb_del			= ocelot_fdb_del,
930a556c76aSAlexandre Belloni 	.ndo_fdb_dump			= ocelot_fdb_dump,
9317142529fSAntoine Tenart 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
9327142529fSAntoine Tenart 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
9337142529fSAntoine Tenart 	.ndo_set_features		= ocelot_set_features,
934751302c3SFlorian Fainelli 	.ndo_get_port_parent_id		= ocelot_get_port_parent_id,
9352c1d029aSJoergen Andreasen 	.ndo_setup_tc			= ocelot_setup_tc,
936a556c76aSAlexandre Belloni };
937a556c76aSAlexandre Belloni 
938a556c76aSAlexandre Belloni static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
939a556c76aSAlexandre Belloni {
940a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(netdev);
941a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
942a556c76aSAlexandre Belloni 	int i;
943a556c76aSAlexandre Belloni 
944a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
945a556c76aSAlexandre Belloni 		return;
946a556c76aSAlexandre Belloni 
947a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
948a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
949a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
950a556c76aSAlexandre Belloni }
951a556c76aSAlexandre Belloni 
9521e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
953a556c76aSAlexandre Belloni {
954a556c76aSAlexandre Belloni 	int i, j;
955a556c76aSAlexandre Belloni 
956a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
957a556c76aSAlexandre Belloni 
958a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
959a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
960a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
961a556c76aSAlexandre Belloni 
962a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
963a556c76aSAlexandre Belloni 			u32 val;
964a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
965a556c76aSAlexandre Belloni 
966a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
967a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
968a556c76aSAlexandre Belloni 
969a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
970a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
971a556c76aSAlexandre Belloni 
972a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
973a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
974a556c76aSAlexandre Belloni 		}
975a556c76aSAlexandre Belloni 	}
976a556c76aSAlexandre Belloni 
9771e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
9781e1caa97SClaudiu Manoil }
9791e1caa97SClaudiu Manoil 
9801e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
9811e1caa97SClaudiu Manoil {
9821e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
9831e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
9841e1caa97SClaudiu Manoil 					     stats_work);
9851e1caa97SClaudiu Manoil 
9861e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
9871e1caa97SClaudiu Manoil 
988a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
989a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
990a556c76aSAlexandre Belloni }
991a556c76aSAlexandre Belloni 
992a556c76aSAlexandre Belloni static void ocelot_get_ethtool_stats(struct net_device *dev,
993a556c76aSAlexandre Belloni 				     struct ethtool_stats *stats, u64 *data)
994a556c76aSAlexandre Belloni {
995a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
996a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
997a556c76aSAlexandre Belloni 	int i;
998a556c76aSAlexandre Belloni 
999a556c76aSAlexandre Belloni 	/* check and update now */
10001e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1001a556c76aSAlexandre Belloni 
1002a556c76aSAlexandre Belloni 	/* Copy all counters */
1003a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1004a556c76aSAlexandre Belloni 		*data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
1005a556c76aSAlexandre Belloni }
1006a556c76aSAlexandre Belloni 
1007a556c76aSAlexandre Belloni static int ocelot_get_sset_count(struct net_device *dev, int sset)
1008a556c76aSAlexandre Belloni {
1009a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1010a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1011a556c76aSAlexandre Belloni 
1012a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1013a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1014a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1015a556c76aSAlexandre Belloni }
1016a556c76aSAlexandre Belloni 
1017a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = {
1018a556c76aSAlexandre Belloni 	.get_strings		= ocelot_get_strings,
1019a556c76aSAlexandre Belloni 	.get_ethtool_stats	= ocelot_get_ethtool_stats,
1020a556c76aSAlexandre Belloni 	.get_sset_count		= ocelot_get_sset_count,
1021dc96ee37SAlexandre Belloni 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1022dc96ee37SAlexandre Belloni 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1023a556c76aSAlexandre Belloni };
1024a556c76aSAlexandre Belloni 
1025a556c76aSAlexandre Belloni static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1026a556c76aSAlexandre Belloni 					  struct switchdev_trans *trans,
1027a556c76aSAlexandre Belloni 					  u8 state)
1028a556c76aSAlexandre Belloni {
1029a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1030a556c76aSAlexandre Belloni 	u32 port_cfg;
1031a556c76aSAlexandre Belloni 	int port, i;
1032a556c76aSAlexandre Belloni 
1033a556c76aSAlexandre Belloni 	if (switchdev_trans_ph_prepare(trans))
1034a556c76aSAlexandre Belloni 		return 0;
1035a556c76aSAlexandre Belloni 
1036a556c76aSAlexandre Belloni 	if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1037a556c76aSAlexandre Belloni 		return 0;
1038a556c76aSAlexandre Belloni 
1039a556c76aSAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1040a556c76aSAlexandre Belloni 				   ocelot_port->chip_port);
1041a556c76aSAlexandre Belloni 
1042a556c76aSAlexandre Belloni 	switch (state) {
1043a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
1044a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1045a556c76aSAlexandre Belloni 		/* Fallthrough */
1046a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1047a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1048a556c76aSAlexandre Belloni 		break;
1049a556c76aSAlexandre Belloni 
1050a556c76aSAlexandre Belloni 	default:
1051a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1052a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1053a556c76aSAlexandre Belloni 		break;
1054a556c76aSAlexandre Belloni 	}
1055a556c76aSAlexandre Belloni 
1056a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1057a556c76aSAlexandre Belloni 			 ocelot_port->chip_port);
1058a556c76aSAlexandre Belloni 
1059a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1060a556c76aSAlexandre Belloni 	 * a source for the other ports.
1061a556c76aSAlexandre Belloni 	 */
1062a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1063a556c76aSAlexandre Belloni 		if (ocelot->bridge_fwd_mask & BIT(port)) {
1064a556c76aSAlexandre Belloni 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1065a556c76aSAlexandre Belloni 
1066a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1067a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
1068a556c76aSAlexandre Belloni 
1069a556c76aSAlexandre Belloni 				if (!bond_mask)
1070a556c76aSAlexandre Belloni 					continue;
1071a556c76aSAlexandre Belloni 
1072a556c76aSAlexandre Belloni 				if (bond_mask & BIT(port)) {
1073a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
1074a556c76aSAlexandre Belloni 					break;
1075a556c76aSAlexandre Belloni 				}
1076a556c76aSAlexandre Belloni 			}
1077a556c76aSAlexandre Belloni 
1078a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1079a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports) | mask,
1080a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1081a556c76aSAlexandre Belloni 		} else {
1082a556c76aSAlexandre Belloni 			/* Only the CPU port, this is compatible with link
1083a556c76aSAlexandre Belloni 			 * aggregation.
1084a556c76aSAlexandre Belloni 			 */
1085a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1086a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports),
1087a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1088a556c76aSAlexandre Belloni 		}
1089a556c76aSAlexandre Belloni 	}
1090a556c76aSAlexandre Belloni 
1091a556c76aSAlexandre Belloni 	return 0;
1092a556c76aSAlexandre Belloni }
1093a556c76aSAlexandre Belloni 
1094a556c76aSAlexandre Belloni static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1095a556c76aSAlexandre Belloni 					unsigned long ageing_clock_t)
1096a556c76aSAlexandre Belloni {
1097a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1098a556c76aSAlexandre Belloni 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1099a556c76aSAlexandre Belloni 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1100a556c76aSAlexandre Belloni 
1101a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1102a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1103a556c76aSAlexandre Belloni }
1104a556c76aSAlexandre Belloni 
1105a556c76aSAlexandre Belloni static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1106a556c76aSAlexandre Belloni {
1107a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1108a556c76aSAlexandre Belloni 	u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1109a556c76aSAlexandre Belloni 				  port->chip_port);
1110a556c76aSAlexandre Belloni 
1111a556c76aSAlexandre Belloni 	if (mc)
1112a556c76aSAlexandre Belloni 		val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1113a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1114a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1115a556c76aSAlexandre Belloni 	else
1116a556c76aSAlexandre Belloni 		val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1117a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1118a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1119a556c76aSAlexandre Belloni 
1120a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1121a556c76aSAlexandre Belloni }
1122a556c76aSAlexandre Belloni 
1123a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev,
1124a556c76aSAlexandre Belloni 				const struct switchdev_attr *attr,
1125a556c76aSAlexandre Belloni 				struct switchdev_trans *trans)
1126a556c76aSAlexandre Belloni {
1127a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1128a556c76aSAlexandre Belloni 	int err = 0;
1129a556c76aSAlexandre Belloni 
1130a556c76aSAlexandre Belloni 	switch (attr->id) {
1131a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1132a556c76aSAlexandre Belloni 		ocelot_port_attr_stp_state_set(ocelot_port, trans,
1133a556c76aSAlexandre Belloni 					       attr->u.stp_state);
1134a556c76aSAlexandre Belloni 		break;
1135a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1136a556c76aSAlexandre Belloni 		ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1137a556c76aSAlexandre Belloni 		break;
11387142529fSAntoine Tenart 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
11397142529fSAntoine Tenart 		ocelot_port->vlan_aware = attr->u.vlan_filtering;
11407142529fSAntoine Tenart 		ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port);
11417142529fSAntoine Tenart 		break;
1142a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1143a556c76aSAlexandre Belloni 		ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1144a556c76aSAlexandre Belloni 		break;
1145a556c76aSAlexandre Belloni 	default:
1146a556c76aSAlexandre Belloni 		err = -EOPNOTSUPP;
1147a556c76aSAlexandre Belloni 		break;
1148a556c76aSAlexandre Belloni 	}
1149a556c76aSAlexandre Belloni 
1150a556c76aSAlexandre Belloni 	return err;
1151a556c76aSAlexandre Belloni }
1152a556c76aSAlexandre Belloni 
11537142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev,
11547142529fSAntoine Tenart 				    const struct switchdev_obj_port_vlan *vlan,
11557142529fSAntoine Tenart 				    struct switchdev_trans *trans)
11567142529fSAntoine Tenart {
11577142529fSAntoine Tenart 	int ret;
11587142529fSAntoine Tenart 	u16 vid;
11597142529fSAntoine Tenart 
11607142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
11617142529fSAntoine Tenart 		ret = ocelot_vlan_vid_add(dev, vid,
11627142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
11637142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
11647142529fSAntoine Tenart 		if (ret)
11657142529fSAntoine Tenart 			return ret;
11667142529fSAntoine Tenart 	}
11677142529fSAntoine Tenart 
11687142529fSAntoine Tenart 	return 0;
11697142529fSAntoine Tenart }
11707142529fSAntoine Tenart 
11717142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev,
11727142529fSAntoine Tenart 				     const struct switchdev_obj_port_vlan *vlan)
11737142529fSAntoine Tenart {
11747142529fSAntoine Tenart 	int ret;
11757142529fSAntoine Tenart 	u16 vid;
11767142529fSAntoine Tenart 
11777142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
11787142529fSAntoine Tenart 		ret = ocelot_vlan_vid_del(dev, vid);
11797142529fSAntoine Tenart 
11807142529fSAntoine Tenart 		if (ret)
11817142529fSAntoine Tenart 			return ret;
11827142529fSAntoine Tenart 	}
11837142529fSAntoine Tenart 
11847142529fSAntoine Tenart 	return 0;
11857142529fSAntoine Tenart }
11867142529fSAntoine Tenart 
1187a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1188a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1189a556c76aSAlexandre Belloni 						     u16 vid)
1190a556c76aSAlexandre Belloni {
1191a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1192a556c76aSAlexandre Belloni 
1193a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1194a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1195a556c76aSAlexandre Belloni 			return mc;
1196a556c76aSAlexandre Belloni 	}
1197a556c76aSAlexandre Belloni 
1198a556c76aSAlexandre Belloni 	return NULL;
1199a556c76aSAlexandre Belloni }
1200a556c76aSAlexandre Belloni 
1201a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev,
1202a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb,
1203a556c76aSAlexandre Belloni 				   struct switchdev_trans *trans)
1204a556c76aSAlexandre Belloni {
1205a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1206a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1207a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1208a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1209a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1210a556c76aSAlexandre Belloni 	bool new = false;
1211a556c76aSAlexandre Belloni 
1212a556c76aSAlexandre Belloni 	if (!vid)
12137142529fSAntoine Tenart 		vid = port->pvid;
1214a556c76aSAlexandre Belloni 
1215a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1216a556c76aSAlexandre Belloni 	if (!mc) {
1217a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1218a556c76aSAlexandre Belloni 		if (!mc)
1219a556c76aSAlexandre Belloni 			return -ENOMEM;
1220a556c76aSAlexandre Belloni 
1221a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1222a556c76aSAlexandre Belloni 		mc->vid = vid;
1223a556c76aSAlexandre Belloni 
1224a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1225a556c76aSAlexandre Belloni 		new = true;
1226a556c76aSAlexandre Belloni 	}
1227a556c76aSAlexandre Belloni 
1228a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1229a556c76aSAlexandre Belloni 	addr[0] = 0;
1230a556c76aSAlexandre Belloni 
1231a556c76aSAlexandre Belloni 	if (!new) {
1232a556c76aSAlexandre Belloni 		addr[2] = mc->ports << 0;
1233a556c76aSAlexandre Belloni 		addr[1] = mc->ports << 8;
1234a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1235a556c76aSAlexandre Belloni 	}
1236a556c76aSAlexandre Belloni 
1237a556c76aSAlexandre Belloni 	mc->ports |= BIT(port->chip_port);
1238a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1239a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1240a556c76aSAlexandre Belloni 
1241a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1242a556c76aSAlexandre Belloni }
1243a556c76aSAlexandre Belloni 
1244a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev,
1245a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb)
1246a556c76aSAlexandre Belloni {
1247a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1248a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1249a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1250a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1251a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1252a556c76aSAlexandre Belloni 
1253a556c76aSAlexandre Belloni 	if (!vid)
12547142529fSAntoine Tenart 		vid = port->pvid;
1255a556c76aSAlexandre Belloni 
1256a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1257a556c76aSAlexandre Belloni 	if (!mc)
1258a556c76aSAlexandre Belloni 		return -ENOENT;
1259a556c76aSAlexandre Belloni 
1260a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1261a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1262a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1263a556c76aSAlexandre Belloni 	addr[0] = 0;
1264a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1265a556c76aSAlexandre Belloni 
1266a556c76aSAlexandre Belloni 	mc->ports &= ~BIT(port->chip_port);
1267a556c76aSAlexandre Belloni 	if (!mc->ports) {
1268a556c76aSAlexandre Belloni 		list_del(&mc->list);
1269a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1270a556c76aSAlexandre Belloni 		return 0;
1271a556c76aSAlexandre Belloni 	}
1272a556c76aSAlexandre Belloni 
1273a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1274a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1275a556c76aSAlexandre Belloni 
1276a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1277a556c76aSAlexandre Belloni }
1278a556c76aSAlexandre Belloni 
1279a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev,
1280a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj,
128169213513SPetr Machata 			       struct switchdev_trans *trans,
128269213513SPetr Machata 			       struct netlink_ext_ack *extack)
1283a556c76aSAlexandre Belloni {
1284a556c76aSAlexandre Belloni 	int ret = 0;
1285a556c76aSAlexandre Belloni 
1286a556c76aSAlexandre Belloni 	switch (obj->id) {
12877142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
12887142529fSAntoine Tenart 		ret = ocelot_port_obj_add_vlan(dev,
12897142529fSAntoine Tenart 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
12907142529fSAntoine Tenart 					       trans);
12917142529fSAntoine Tenart 		break;
1292a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1293a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1294a556c76aSAlexandre Belloni 					      trans);
1295a556c76aSAlexandre Belloni 		break;
1296a556c76aSAlexandre Belloni 	default:
1297a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1298a556c76aSAlexandre Belloni 	}
1299a556c76aSAlexandre Belloni 
1300a556c76aSAlexandre Belloni 	return ret;
1301a556c76aSAlexandre Belloni }
1302a556c76aSAlexandre Belloni 
1303a556c76aSAlexandre Belloni static int ocelot_port_obj_del(struct net_device *dev,
1304a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj)
1305a556c76aSAlexandre Belloni {
1306a556c76aSAlexandre Belloni 	int ret = 0;
1307a556c76aSAlexandre Belloni 
1308a556c76aSAlexandre Belloni 	switch (obj->id) {
13097142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
13107142529fSAntoine Tenart 		ret = ocelot_port_vlan_del_vlan(dev,
13117142529fSAntoine Tenart 						SWITCHDEV_OBJ_PORT_VLAN(obj));
13127142529fSAntoine Tenart 		break;
1313a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1314a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1315a556c76aSAlexandre Belloni 		break;
1316a556c76aSAlexandre Belloni 	default:
1317a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1318a556c76aSAlexandre Belloni 	}
1319a556c76aSAlexandre Belloni 
1320a556c76aSAlexandre Belloni 	return ret;
1321a556c76aSAlexandre Belloni }
1322a556c76aSAlexandre Belloni 
1323a556c76aSAlexandre Belloni static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1324a556c76aSAlexandre Belloni 				   struct net_device *bridge)
1325a556c76aSAlexandre Belloni {
1326a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1327a556c76aSAlexandre Belloni 
1328a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1329a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1330a556c76aSAlexandre Belloni 	} else {
1331a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1332a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1333a556c76aSAlexandre Belloni 			 * unsupported */
1334a556c76aSAlexandre Belloni 			return -ENODEV;
1335a556c76aSAlexandre Belloni 	}
1336a556c76aSAlexandre Belloni 
1337a556c76aSAlexandre Belloni 	ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1338a556c76aSAlexandre Belloni 
1339a556c76aSAlexandre Belloni 	return 0;
1340a556c76aSAlexandre Belloni }
1341a556c76aSAlexandre Belloni 
1342a556c76aSAlexandre Belloni static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1343a556c76aSAlexandre Belloni 				     struct net_device *bridge)
1344a556c76aSAlexandre Belloni {
1345a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1346a556c76aSAlexandre Belloni 
1347a556c76aSAlexandre Belloni 	ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
1348a556c76aSAlexandre Belloni 
1349a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1350a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
13517142529fSAntoine Tenart 
13527142529fSAntoine Tenart 	/* Clear bridge vlan settings before calling ocelot_vlan_port_apply */
13537142529fSAntoine Tenart 	ocelot_port->vlan_aware = 0;
13547142529fSAntoine Tenart 	ocelot_port->pvid = 0;
13557142529fSAntoine Tenart 	ocelot_port->vid = 0;
1356a556c76aSAlexandre Belloni }
1357a556c76aSAlexandre Belloni 
1358dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1359dc96ee37SAlexandre Belloni {
1360dc96ee37SAlexandre Belloni 	int i, port, lag;
1361dc96ee37SAlexandre Belloni 
1362dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1363dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1364dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1365dc96ee37SAlexandre Belloni 
1366dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1367dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1368dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1369dc96ee37SAlexandre Belloni 
1370dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1371dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1372dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1373dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1374dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1375dc96ee37SAlexandre Belloni 
1376dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1377dc96ee37SAlexandre Belloni 		if (!bond_mask)
1378dc96ee37SAlexandre Belloni 			continue;
1379dc96ee37SAlexandre Belloni 
1380dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1381dc96ee37SAlexandre Belloni 			// Destination mask
1382dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1383dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1384dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1385dc96ee37SAlexandre Belloni 			aggr_count++;
1386dc96ee37SAlexandre Belloni 		}
1387dc96ee37SAlexandre Belloni 
1388dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1389dc96ee37SAlexandre Belloni 			u32 ac;
1390dc96ee37SAlexandre Belloni 
1391dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1392dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1393dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1394dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1395dc96ee37SAlexandre Belloni 		}
1396dc96ee37SAlexandre Belloni 	}
1397dc96ee37SAlexandre Belloni }
1398dc96ee37SAlexandre Belloni 
1399dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1400dc96ee37SAlexandre Belloni {
1401dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1402dc96ee37SAlexandre Belloni 	unsigned int p;
1403dc96ee37SAlexandre Belloni 
1404dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1405dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1406dc96ee37SAlexandre Belloni 
1407dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1408dc96ee37SAlexandre Belloni 
1409dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1410dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1411dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1412dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1413dc96ee37SAlexandre Belloni 	}
1414dc96ee37SAlexandre Belloni }
1415dc96ee37SAlexandre Belloni 
1416dc96ee37SAlexandre Belloni static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1417dc96ee37SAlexandre Belloni 				struct net_device *bond)
1418dc96ee37SAlexandre Belloni {
1419dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1420dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1421dc96ee37SAlexandre Belloni 	int lag, lp;
1422dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1423dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1424dc96ee37SAlexandre Belloni 
1425dc96ee37SAlexandre Belloni 	rcu_read_lock();
1426dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1427dc96ee37SAlexandre Belloni 		struct ocelot_port *port = netdev_priv(ndev);
1428dc96ee37SAlexandre Belloni 
1429dc96ee37SAlexandre Belloni 		bond_mask |= BIT(port->chip_port);
1430dc96ee37SAlexandre Belloni 	}
1431dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1432dc96ee37SAlexandre Belloni 
1433dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1434dc96ee37SAlexandre Belloni 
1435dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1436dc96ee37SAlexandre Belloni 	 * now on
1437dc96ee37SAlexandre Belloni 	 */
1438dc96ee37SAlexandre Belloni 	if (p == lp) {
1439dc96ee37SAlexandre Belloni 		lag = p;
1440dc96ee37SAlexandre Belloni 		ocelot->lags[p] = bond_mask;
1441dc96ee37SAlexandre Belloni 		bond_mask &= ~BIT(p);
1442dc96ee37SAlexandre Belloni 		if (bond_mask) {
1443dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1444dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1445dc96ee37SAlexandre Belloni 		}
1446dc96ee37SAlexandre Belloni 	} else {
1447dc96ee37SAlexandre Belloni 		lag = lp;
1448dc96ee37SAlexandre Belloni 		ocelot->lags[lp] |= BIT(p);
1449dc96ee37SAlexandre Belloni 	}
1450dc96ee37SAlexandre Belloni 
1451dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1452dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1453dc96ee37SAlexandre Belloni 
1454dc96ee37SAlexandre Belloni 	return 0;
1455dc96ee37SAlexandre Belloni }
1456dc96ee37SAlexandre Belloni 
1457dc96ee37SAlexandre Belloni static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1458dc96ee37SAlexandre Belloni 				  struct net_device *bond)
1459dc96ee37SAlexandre Belloni {
1460dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1461dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1462dc96ee37SAlexandre Belloni 	u32 port_cfg;
1463dc96ee37SAlexandre Belloni 	int i;
1464dc96ee37SAlexandre Belloni 
1465dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1466dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1467dc96ee37SAlexandre Belloni 		ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1468dc96ee37SAlexandre Belloni 
1469dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1470dc96ee37SAlexandre Belloni 	 * next port
1471dc96ee37SAlexandre Belloni 	 */
1472dc96ee37SAlexandre Belloni 	if (ocelot->lags[p]) {
1473dc96ee37SAlexandre Belloni 		int n = __ffs(ocelot->lags[p]);
1474dc96ee37SAlexandre Belloni 
1475dc96ee37SAlexandre Belloni 		ocelot->lags[n] = ocelot->lags[p];
1476dc96ee37SAlexandre Belloni 		ocelot->lags[p] = 0;
1477dc96ee37SAlexandre Belloni 
1478dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1479dc96ee37SAlexandre Belloni 	}
1480dc96ee37SAlexandre Belloni 
1481dc96ee37SAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1482dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1483dc96ee37SAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1484dc96ee37SAlexandre Belloni 			 ANA_PORT_PORT_CFG, p);
1485dc96ee37SAlexandre Belloni 
1486dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1487dc96ee37SAlexandre Belloni }
1488dc96ee37SAlexandre Belloni 
1489a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */
1490a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1491a556c76aSAlexandre Belloni {
1492a556c76aSAlexandre Belloni 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1493a556c76aSAlexandre Belloni }
1494a556c76aSAlexandre Belloni 
1495a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev,
1496a556c76aSAlexandre Belloni 				       unsigned long event,
1497a556c76aSAlexandre Belloni 				       struct netdev_notifier_changeupper_info *info)
1498a556c76aSAlexandre Belloni {
1499a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1500a556c76aSAlexandre Belloni 	int err = 0;
1501a556c76aSAlexandre Belloni 
1502a556c76aSAlexandre Belloni 	if (!ocelot_netdevice_dev_check(dev))
1503a556c76aSAlexandre Belloni 		return 0;
1504a556c76aSAlexandre Belloni 
1505a556c76aSAlexandre Belloni 	switch (event) {
1506a556c76aSAlexandre Belloni 	case NETDEV_CHANGEUPPER:
1507a556c76aSAlexandre Belloni 		if (netif_is_bridge_master(info->upper_dev)) {
1508a556c76aSAlexandre Belloni 			if (info->linking)
1509a556c76aSAlexandre Belloni 				err = ocelot_port_bridge_join(ocelot_port,
1510a556c76aSAlexandre Belloni 							      info->upper_dev);
1511a556c76aSAlexandre Belloni 			else
1512a556c76aSAlexandre Belloni 				ocelot_port_bridge_leave(ocelot_port,
1513a556c76aSAlexandre Belloni 							 info->upper_dev);
15147142529fSAntoine Tenart 
15157142529fSAntoine Tenart 			ocelot_vlan_port_apply(ocelot_port->ocelot,
15167142529fSAntoine Tenart 					       ocelot_port);
1517a556c76aSAlexandre Belloni 		}
1518dc96ee37SAlexandre Belloni 		if (netif_is_lag_master(info->upper_dev)) {
1519dc96ee37SAlexandre Belloni 			if (info->linking)
1520dc96ee37SAlexandre Belloni 				err = ocelot_port_lag_join(ocelot_port,
1521dc96ee37SAlexandre Belloni 							   info->upper_dev);
1522dc96ee37SAlexandre Belloni 			else
1523dc96ee37SAlexandre Belloni 				ocelot_port_lag_leave(ocelot_port,
1524dc96ee37SAlexandre Belloni 						      info->upper_dev);
1525dc96ee37SAlexandre Belloni 		}
1526a556c76aSAlexandre Belloni 		break;
1527a556c76aSAlexandre Belloni 	default:
1528a556c76aSAlexandre Belloni 		break;
1529a556c76aSAlexandre Belloni 	}
1530a556c76aSAlexandre Belloni 
1531a556c76aSAlexandre Belloni 	return err;
1532a556c76aSAlexandre Belloni }
1533a556c76aSAlexandre Belloni 
1534a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused,
1535a556c76aSAlexandre Belloni 				  unsigned long event, void *ptr)
1536a556c76aSAlexandre Belloni {
1537a556c76aSAlexandre Belloni 	struct netdev_notifier_changeupper_info *info = ptr;
1538a556c76aSAlexandre Belloni 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
15392ac0e152SGeert Uytterhoeven 	int ret = 0;
1540a556c76aSAlexandre Belloni 
1541dc96ee37SAlexandre Belloni 	if (event == NETDEV_PRECHANGEUPPER &&
1542dc96ee37SAlexandre Belloni 	    netif_is_lag_master(info->upper_dev)) {
1543dc96ee37SAlexandre Belloni 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1544dc96ee37SAlexandre Belloni 		struct netlink_ext_ack *extack;
1545dc96ee37SAlexandre Belloni 
1546dc96ee37SAlexandre Belloni 		if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1547dc96ee37SAlexandre Belloni 			extack = netdev_notifier_info_to_extack(&info->info);
1548dc96ee37SAlexandre Belloni 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1549dc96ee37SAlexandre Belloni 
1550dc96ee37SAlexandre Belloni 			ret = -EINVAL;
1551dc96ee37SAlexandre Belloni 			goto notify;
1552dc96ee37SAlexandre Belloni 		}
1553dc96ee37SAlexandre Belloni 	}
1554dc96ee37SAlexandre Belloni 
1555a556c76aSAlexandre Belloni 	if (netif_is_lag_master(dev)) {
1556a556c76aSAlexandre Belloni 		struct net_device *slave;
1557a556c76aSAlexandre Belloni 		struct list_head *iter;
1558a556c76aSAlexandre Belloni 
1559a556c76aSAlexandre Belloni 		netdev_for_each_lower_dev(dev, slave, iter) {
1560a556c76aSAlexandre Belloni 			ret = ocelot_netdevice_port_event(slave, event, info);
1561a556c76aSAlexandre Belloni 			if (ret)
1562a556c76aSAlexandre Belloni 				goto notify;
1563a556c76aSAlexandre Belloni 		}
1564a556c76aSAlexandre Belloni 	} else {
1565a556c76aSAlexandre Belloni 		ret = ocelot_netdevice_port_event(dev, event, info);
1566a556c76aSAlexandre Belloni 	}
1567a556c76aSAlexandre Belloni 
1568a556c76aSAlexandre Belloni notify:
1569a556c76aSAlexandre Belloni 	return notifier_from_errno(ret);
1570a556c76aSAlexandre Belloni }
1571a556c76aSAlexandre Belloni 
1572a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = {
1573a556c76aSAlexandre Belloni 	.notifier_call = ocelot_netdevice_event,
1574a556c76aSAlexandre Belloni };
1575a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb);
1576a556c76aSAlexandre Belloni 
157756da64bcSFlorian Fainelli static int ocelot_switchdev_event(struct notifier_block *unused,
157856da64bcSFlorian Fainelli 				  unsigned long event, void *ptr)
157956da64bcSFlorian Fainelli {
158056da64bcSFlorian Fainelli 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
158156da64bcSFlorian Fainelli 	int err;
158256da64bcSFlorian Fainelli 
158356da64bcSFlorian Fainelli 	switch (event) {
158456da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
158556da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
158656da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
158756da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
158856da64bcSFlorian Fainelli 		return notifier_from_errno(err);
158956da64bcSFlorian Fainelli 	}
159056da64bcSFlorian Fainelli 
159156da64bcSFlorian Fainelli 	return NOTIFY_DONE;
159256da64bcSFlorian Fainelli }
159356da64bcSFlorian Fainelli 
159456da64bcSFlorian Fainelli struct notifier_block ocelot_switchdev_nb __read_mostly = {
159556da64bcSFlorian Fainelli 	.notifier_call = ocelot_switchdev_event,
159656da64bcSFlorian Fainelli };
159756da64bcSFlorian Fainelli EXPORT_SYMBOL(ocelot_switchdev_nb);
159856da64bcSFlorian Fainelli 
15990e332c85SPetr Machata static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
16000e332c85SPetr Machata 					   unsigned long event, void *ptr)
16010e332c85SPetr Machata {
16020e332c85SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
16030e332c85SPetr Machata 	int err;
16040e332c85SPetr Machata 
16050e332c85SPetr Machata 	switch (event) {
16060e332c85SPetr Machata 		/* Blocking events. */
16070e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
16080e332c85SPetr Machata 		err = switchdev_handle_port_obj_add(dev, ptr,
16090e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
16100e332c85SPetr Machata 						    ocelot_port_obj_add);
16110e332c85SPetr Machata 		return notifier_from_errno(err);
16120e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
16130e332c85SPetr Machata 		err = switchdev_handle_port_obj_del(dev, ptr,
16140e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
16150e332c85SPetr Machata 						    ocelot_port_obj_del);
16160e332c85SPetr Machata 		return notifier_from_errno(err);
161756da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
161856da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
161956da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
162056da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
162156da64bcSFlorian Fainelli 		return notifier_from_errno(err);
16220e332c85SPetr Machata 	}
16230e332c85SPetr Machata 
16240e332c85SPetr Machata 	return NOTIFY_DONE;
16250e332c85SPetr Machata }
16260e332c85SPetr Machata 
16270e332c85SPetr Machata struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
16280e332c85SPetr Machata 	.notifier_call = ocelot_switchdev_blocking_event,
16290e332c85SPetr Machata };
16300e332c85SPetr Machata EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
16310e332c85SPetr Machata 
1632a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port,
1633a556c76aSAlexandre Belloni 		      void __iomem *regs,
1634a556c76aSAlexandre Belloni 		      struct phy_device *phy)
1635a556c76aSAlexandre Belloni {
1636a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port;
1637a556c76aSAlexandre Belloni 	struct net_device *dev;
1638a556c76aSAlexandre Belloni 	int err;
1639a556c76aSAlexandre Belloni 
1640a556c76aSAlexandre Belloni 	dev = alloc_etherdev(sizeof(struct ocelot_port));
1641a556c76aSAlexandre Belloni 	if (!dev)
1642a556c76aSAlexandre Belloni 		return -ENOMEM;
1643a556c76aSAlexandre Belloni 	SET_NETDEV_DEV(dev, ocelot->dev);
1644a556c76aSAlexandre Belloni 	ocelot_port = netdev_priv(dev);
1645a556c76aSAlexandre Belloni 	ocelot_port->dev = dev;
1646a556c76aSAlexandre Belloni 	ocelot_port->ocelot = ocelot;
1647a556c76aSAlexandre Belloni 	ocelot_port->regs = regs;
1648a556c76aSAlexandre Belloni 	ocelot_port->chip_port = port;
1649a556c76aSAlexandre Belloni 	ocelot_port->phy = phy;
1650a556c76aSAlexandre Belloni 	ocelot->ports[port] = ocelot_port;
1651a556c76aSAlexandre Belloni 
1652a556c76aSAlexandre Belloni 	dev->netdev_ops = &ocelot_port_netdev_ops;
1653a556c76aSAlexandre Belloni 	dev->ethtool_ops = &ocelot_ethtool_ops;
1654a556c76aSAlexandre Belloni 
16552c1d029aSJoergen Andreasen 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
16562c1d029aSJoergen Andreasen 		NETIF_F_HW_TC;
16572c1d029aSJoergen Andreasen 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
16587142529fSAntoine Tenart 
1659a556c76aSAlexandre Belloni 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1660a556c76aSAlexandre Belloni 	dev->dev_addr[ETH_ALEN - 1] += port;
1661a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
1662a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
1663a556c76aSAlexandre Belloni 
1664a556c76aSAlexandre Belloni 	err = register_netdev(dev);
1665a556c76aSAlexandre Belloni 	if (err) {
1666a556c76aSAlexandre Belloni 		dev_err(ocelot->dev, "register_netdev failed\n");
1667a556c76aSAlexandre Belloni 		goto err_register_netdev;
1668a556c76aSAlexandre Belloni 	}
1669a556c76aSAlexandre Belloni 
16707142529fSAntoine Tenart 	/* Basic L2 initialization */
16717142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, ocelot_port);
16727142529fSAntoine Tenart 
1673*b5962294SHoratiu Vultur 	/* Enable vcap lookups */
1674*b5962294SHoratiu Vultur 	ocelot_vcap_enable(ocelot, ocelot_port);
1675*b5962294SHoratiu Vultur 
1676a556c76aSAlexandre Belloni 	return 0;
1677a556c76aSAlexandre Belloni 
1678a556c76aSAlexandre Belloni err_register_netdev:
1679a556c76aSAlexandre Belloni 	free_netdev(dev);
1680a556c76aSAlexandre Belloni 	return err;
1681a556c76aSAlexandre Belloni }
1682a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port);
1683a556c76aSAlexandre Belloni 
1684a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1685a556c76aSAlexandre Belloni {
1686a556c76aSAlexandre Belloni 	u32 port;
1687a556c76aSAlexandre Belloni 	int i, cpu = ocelot->num_phys_ports;
1688a556c76aSAlexandre Belloni 	char queue_name[32];
1689a556c76aSAlexandre Belloni 
1690dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1691dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
1692dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
1693dc96ee37SAlexandre Belloni 		return -ENOMEM;
1694dc96ee37SAlexandre Belloni 
1695a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1696a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1697a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1698a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1699a556c76aSAlexandre Belloni 		return -ENOMEM;
1700a556c76aSAlexandre Belloni 
1701a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
1702a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1703a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1704a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1705a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1706a556c76aSAlexandre Belloni 		return -ENOMEM;
1707a556c76aSAlexandre Belloni 
1708a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1709a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1710*b5962294SHoratiu Vultur 	ocelot_ace_init(ocelot);
1711a556c76aSAlexandre Belloni 
1712a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1713a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1714a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1715a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1716a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1717a556c76aSAlexandre Belloni 	}
1718a556c76aSAlexandre Belloni 
1719a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1720a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1721a556c76aSAlexandre Belloni 
1722a556c76aSAlexandre Belloni 	/* Aggregation mode */
1723a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1724a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1725a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1726a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1727a556c76aSAlexandre Belloni 
1728a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1729a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1730a556c76aSAlexandre Belloni 	 */
1731a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1732a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1733a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1734a556c76aSAlexandre Belloni 
1735a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1736a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1737a556c76aSAlexandre Belloni 
1738a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1739a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1740a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1741a556c76aSAlexandre Belloni 
1742a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1743a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1744a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1745a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1746a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
1747a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1748a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1749a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1750a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1751a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1752a556c76aSAlexandre Belloni 
1753a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1754a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1755a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1756a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1757a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1758a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1759a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1760a556c76aSAlexandre Belloni 				 port);
1761a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1762a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1763a556c76aSAlexandre Belloni 	}
1764a556c76aSAlexandre Belloni 
1765a556c76aSAlexandre Belloni 	/* Configure and enable the CPU port. */
1766a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
1767a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1768a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1769a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1770a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, cpu);
1771a556c76aSAlexandre Belloni 
1772a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
1773a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
1774a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1775a556c76aSAlexandre Belloni 
1776a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1777a556c76aSAlexandre Belloni 	}
1778a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
1779a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1780a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
1781a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1782a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1783a556c76aSAlexandre Belloni 
1784a556c76aSAlexandre Belloni 	/* CPU port Injection/Extraction configuration */
1785a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
1786a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
1787a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
1788a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, cpu);
1789a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
1790a556c76aSAlexandre Belloni 			 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
1791a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1792a556c76aSAlexandre Belloni 	 * registers endianness.
1793a556c76aSAlexandre Belloni 	 */
1794a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1795a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1796a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1797a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1798a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1799a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1800a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1801a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1802a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1803a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1804a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1805a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1806a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1807a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1808a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1809a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1810a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1811a556c76aSAlexandre Belloni 
18121e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1813a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1814a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1815a556c76aSAlexandre Belloni 	return 0;
1816a556c76aSAlexandre Belloni }
1817a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1818a556c76aSAlexandre Belloni 
1819a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1820a556c76aSAlexandre Belloni {
1821a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1822a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
1823*b5962294SHoratiu Vultur 	ocelot_ace_deinit();
1824a556c76aSAlexandre Belloni }
1825a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1826a556c76aSAlexandre Belloni 
1827a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1828