xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 87b0984ebfabafcfe959e52ca5cdab5eeb2d60c0)
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"
25a556c76aSAlexandre Belloni 
26639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
27639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
28639c1b26SSteen Hegelund 
29a556c76aSAlexandre Belloni /* MAC table entry types.
30a556c76aSAlexandre Belloni  * ENTRYTYPE_NORMAL is subject to aging.
31a556c76aSAlexandre Belloni  * ENTRYTYPE_LOCKED is not subject to aging.
32a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
33a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
34a556c76aSAlexandre Belloni  */
35a556c76aSAlexandre Belloni enum macaccess_entry_type {
36a556c76aSAlexandre Belloni 	ENTRYTYPE_NORMAL = 0,
37a556c76aSAlexandre Belloni 	ENTRYTYPE_LOCKED,
38a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv4,
39a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv6,
40a556c76aSAlexandre Belloni };
41a556c76aSAlexandre Belloni 
42a556c76aSAlexandre Belloni struct ocelot_mact_entry {
43a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
44a556c76aSAlexandre Belloni 	u16 vid;
45a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
46a556c76aSAlexandre Belloni };
47a556c76aSAlexandre Belloni 
48639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
49639c1b26SSteen Hegelund {
50639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
51639c1b26SSteen Hegelund }
52639c1b26SSteen Hegelund 
53a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
54a556c76aSAlexandre Belloni {
55639c1b26SSteen Hegelund 	u32 val;
56a556c76aSAlexandre Belloni 
57639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
58639c1b26SSteen Hegelund 		ocelot, val,
59639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
60639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
61639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
62a556c76aSAlexandre Belloni }
63a556c76aSAlexandre Belloni 
64a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
65a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
66a556c76aSAlexandre Belloni 			       unsigned int vid)
67a556c76aSAlexandre Belloni {
68a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
69a556c76aSAlexandre Belloni 
70a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
71a556c76aSAlexandre Belloni 	 * understood by the hardware.
72a556c76aSAlexandre Belloni 	 */
73a556c76aSAlexandre Belloni 	mach |= vid    << 16;
74a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
75a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
76a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
77a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
78a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
79a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
80a556c76aSAlexandre Belloni 
81a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
82a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
83a556c76aSAlexandre Belloni 
84a556c76aSAlexandre Belloni }
85a556c76aSAlexandre Belloni 
86a556c76aSAlexandre Belloni static int ocelot_mact_learn(struct ocelot *ocelot, int port,
87a556c76aSAlexandre Belloni 			     const unsigned char mac[ETH_ALEN],
88a556c76aSAlexandre Belloni 			     unsigned int vid,
89a556c76aSAlexandre Belloni 			     enum macaccess_entry_type type)
90a556c76aSAlexandre Belloni {
91a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
92a556c76aSAlexandre Belloni 
93a556c76aSAlexandre Belloni 	/* Issue a write command */
94a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
95a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
96a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
97a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
98a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
99a556c76aSAlexandre Belloni 
100a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
101a556c76aSAlexandre Belloni }
102a556c76aSAlexandre Belloni 
103a556c76aSAlexandre Belloni static int ocelot_mact_forget(struct ocelot *ocelot,
104a556c76aSAlexandre Belloni 			      const unsigned char mac[ETH_ALEN],
105a556c76aSAlexandre Belloni 			      unsigned int vid)
106a556c76aSAlexandre Belloni {
107a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
108a556c76aSAlexandre Belloni 
109a556c76aSAlexandre Belloni 	/* Issue a forget command */
110a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
111a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
112a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
113a556c76aSAlexandre Belloni 
114a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
115a556c76aSAlexandre Belloni }
116a556c76aSAlexandre Belloni 
117a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
118a556c76aSAlexandre Belloni {
119a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
120a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
121a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
122a556c76aSAlexandre Belloni 	 */
123a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
124a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
125a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
126a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
127a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
128a556c76aSAlexandre Belloni 
129a556c76aSAlexandre Belloni 	/* Clear the MAC table */
130a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
131a556c76aSAlexandre Belloni }
132a556c76aSAlexandre Belloni 
133639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
134639c1b26SSteen Hegelund {
135639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
136639c1b26SSteen Hegelund }
137639c1b26SSteen Hegelund 
138a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
139a556c76aSAlexandre Belloni {
140639c1b26SSteen Hegelund 	u32 val;
141a556c76aSAlexandre Belloni 
142639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
143639c1b26SSteen Hegelund 		ocelot,
144639c1b26SSteen Hegelund 		val,
145639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
146639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
147639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
148a556c76aSAlexandre Belloni }
149a556c76aSAlexandre Belloni 
1507142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1517142529fSAntoine Tenart {
1527142529fSAntoine Tenart 	/* Select the VID to configure */
1537142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1547142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1557142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1567142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1577142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1587142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1597142529fSAntoine Tenart 
1607142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1617142529fSAntoine Tenart }
1627142529fSAntoine Tenart 
1637142529fSAntoine Tenart static void ocelot_vlan_mode(struct ocelot_port *port,
1647142529fSAntoine Tenart 			     netdev_features_t features)
1657142529fSAntoine Tenart {
1667142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
1677142529fSAntoine Tenart 	u8 p = port->chip_port;
1687142529fSAntoine Tenart 	u32 val;
1697142529fSAntoine Tenart 
1707142529fSAntoine Tenart 	/* Filtering */
1717142529fSAntoine Tenart 	val = ocelot_read(ocelot, ANA_VLANMASK);
1727142529fSAntoine Tenart 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1737142529fSAntoine Tenart 		val |= BIT(p);
1747142529fSAntoine Tenart 	else
1757142529fSAntoine Tenart 		val &= ~BIT(p);
1767142529fSAntoine Tenart 	ocelot_write(ocelot, val, ANA_VLANMASK);
1777142529fSAntoine Tenart }
1787142529fSAntoine Tenart 
1797142529fSAntoine Tenart static void ocelot_vlan_port_apply(struct ocelot *ocelot,
1807142529fSAntoine Tenart 				   struct ocelot_port *port)
1817142529fSAntoine Tenart {
1827142529fSAntoine Tenart 	u32 val;
1837142529fSAntoine Tenart 
1847142529fSAntoine Tenart 	/* Ingress clasification (ANA_PORT_VLAN_CFG) */
1857142529fSAntoine Tenart 	/* Default vlan to clasify for untagged frames (may be zero) */
1867142529fSAntoine Tenart 	val = ANA_PORT_VLAN_CFG_VLAN_VID(port->pvid);
1877142529fSAntoine Tenart 	if (port->vlan_aware)
1887142529fSAntoine Tenart 		val |= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1897142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
1907142529fSAntoine Tenart 
1917142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
1927142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_VID_M |
1937142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1947142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
1957142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG, port->chip_port);
1967142529fSAntoine Tenart 
1977142529fSAntoine Tenart 	/* Drop frames with multicast source address */
1987142529fSAntoine Tenart 	val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
1997142529fSAntoine Tenart 	if (port->vlan_aware && !port->vid)
2007142529fSAntoine Tenart 		/* If port is vlan-aware and tagged, drop untagged and priority
2017142529fSAntoine Tenart 		 * tagged frames.
2027142529fSAntoine Tenart 		 */
2037142529fSAntoine Tenart 		val |= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
2047142529fSAntoine Tenart 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
2057142529fSAntoine Tenart 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
2067142529fSAntoine Tenart 	ocelot_write_gix(ocelot, val, ANA_PORT_DROP_CFG, port->chip_port);
2077142529fSAntoine Tenart 
2087142529fSAntoine Tenart 	/* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q. */
2097142529fSAntoine Tenart 	val = REW_TAG_CFG_TAG_TPID_CFG(0);
2107142529fSAntoine Tenart 
2117142529fSAntoine Tenart 	if (port->vlan_aware) {
2127142529fSAntoine Tenart 		if (port->vid)
2137142529fSAntoine Tenart 			/* Tag all frames except when VID == DEFAULT_VLAN */
2147142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(1);
2157142529fSAntoine Tenart 		else
2167142529fSAntoine Tenart 			/* Tag all frames */
2177142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(3);
2187142529fSAntoine Tenart 	}
2197142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2207142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_TPID_CFG_M |
2217142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_CFG_M,
2227142529fSAntoine Tenart 		       REW_TAG_CFG, port->chip_port);
2237142529fSAntoine Tenart 
2247142529fSAntoine Tenart 	/* Set default VLAN and tag type to 8021Q. */
2257142529fSAntoine Tenart 	val = REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q) |
2267142529fSAntoine Tenart 	      REW_PORT_VLAN_CFG_PORT_VID(port->vid);
2277142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
2287142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_TPID_M |
2297142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
2307142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG, port->chip_port);
2317142529fSAntoine Tenart }
2327142529fSAntoine Tenart 
2337142529fSAntoine Tenart static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
2347142529fSAntoine Tenart 			       bool untagged)
2357142529fSAntoine Tenart {
2367142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
2377142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
2387142529fSAntoine Tenart 	int ret;
2397142529fSAntoine Tenart 
2407142529fSAntoine Tenart 	/* Add the port MAC address to with the right VLAN information */
2417142529fSAntoine Tenart 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
2427142529fSAntoine Tenart 			  ENTRYTYPE_LOCKED);
2437142529fSAntoine Tenart 
2447142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
2457142529fSAntoine Tenart 	ocelot->vlan_mask[vid] |= BIT(port->chip_port);
2467142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2477142529fSAntoine Tenart 	if (ret)
2487142529fSAntoine Tenart 		return ret;
2497142529fSAntoine Tenart 
2507142529fSAntoine Tenart 	/* Default ingress vlan classification */
2517142529fSAntoine Tenart 	if (pvid)
2527142529fSAntoine Tenart 		port->pvid = vid;
2537142529fSAntoine Tenart 
2547142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
2557142529fSAntoine Tenart 	if (untagged)
2567142529fSAntoine Tenart 		port->vid = vid;
2577142529fSAntoine Tenart 
2587142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
2597142529fSAntoine Tenart 
2607142529fSAntoine Tenart 	return 0;
2617142529fSAntoine Tenart }
2627142529fSAntoine Tenart 
2637142529fSAntoine Tenart static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
2647142529fSAntoine Tenart {
2657142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
2667142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
2677142529fSAntoine Tenart 	int ret;
2687142529fSAntoine Tenart 
2697142529fSAntoine Tenart 	/* 8021q removes VID 0 on module unload for all interfaces
2707142529fSAntoine Tenart 	 * with VLAN filtering feature. We need to keep it to receive
2717142529fSAntoine Tenart 	 * untagged traffic.
2727142529fSAntoine Tenart 	 */
2737142529fSAntoine Tenart 	if (vid == 0)
2747142529fSAntoine Tenart 		return 0;
2757142529fSAntoine Tenart 
2767142529fSAntoine Tenart 	/* Del the port MAC address to with the right VLAN information */
2777142529fSAntoine Tenart 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
2787142529fSAntoine Tenart 
2797142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
2807142529fSAntoine Tenart 	ocelot->vlan_mask[vid] &= ~BIT(port->chip_port);
2817142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2827142529fSAntoine Tenart 	if (ret)
2837142529fSAntoine Tenart 		return ret;
2847142529fSAntoine Tenart 
2857142529fSAntoine Tenart 	/* Ingress */
2867142529fSAntoine Tenart 	if (port->pvid == vid)
2877142529fSAntoine Tenart 		port->pvid = 0;
2887142529fSAntoine Tenart 
2897142529fSAntoine Tenart 	/* Egress */
2907142529fSAntoine Tenart 	if (port->vid == vid)
2917142529fSAntoine Tenart 		port->vid = 0;
2927142529fSAntoine Tenart 
2937142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
2947142529fSAntoine Tenart 
2957142529fSAntoine Tenart 	return 0;
2967142529fSAntoine Tenart }
2977142529fSAntoine Tenart 
298a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
299a556c76aSAlexandre Belloni {
3007142529fSAntoine Tenart 	u16 port, vid;
3017142529fSAntoine Tenart 
302a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
303a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
304a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
305a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3067142529fSAntoine Tenart 
3077142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3087142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3097142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3107142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3117142529fSAntoine Tenart 	}
3127142529fSAntoine Tenart 
3137142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3147142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3157142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3167142529fSAntoine Tenart 	 */
3177142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3187142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3197142529fSAntoine Tenart 
3207142529fSAntoine Tenart 	/* Configure the CPU port to be VLAN aware */
3217142529fSAntoine Tenart 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
3227142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
3237142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
3247142529fSAntoine Tenart 			 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
3257142529fSAntoine Tenart 
3267142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3277142529fSAntoine Tenart 	 * default.
3287142529fSAntoine Tenart 	 */
3297142529fSAntoine Tenart 	ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
3307142529fSAntoine Tenart 
3317142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3327142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3337142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3347142529fSAntoine Tenart 	}
335a556c76aSAlexandre Belloni }
336a556c76aSAlexandre Belloni 
337a556c76aSAlexandre Belloni /* Watermark encode
338a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
339a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
340a556c76aSAlexandre Belloni  */
341a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
342a556c76aSAlexandre Belloni {
343a556c76aSAlexandre Belloni 	if (value >= BIT(8))
344a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
345a556c76aSAlexandre Belloni 
346a556c76aSAlexandre Belloni 	return value;
347a556c76aSAlexandre Belloni }
348a556c76aSAlexandre Belloni 
349a556c76aSAlexandre Belloni static void ocelot_port_adjust_link(struct net_device *dev)
350a556c76aSAlexandre Belloni {
351a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
352a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
353a556c76aSAlexandre Belloni 	u8 p = port->chip_port;
354a556c76aSAlexandre Belloni 	int speed, atop_wm, mode = 0;
355a556c76aSAlexandre Belloni 
356a556c76aSAlexandre Belloni 	switch (dev->phydev->speed) {
357a556c76aSAlexandre Belloni 	case SPEED_10:
358a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
359a556c76aSAlexandre Belloni 		break;
360a556c76aSAlexandre Belloni 	case SPEED_100:
361a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
362a556c76aSAlexandre Belloni 		break;
363a556c76aSAlexandre Belloni 	case SPEED_1000:
364a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
365a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
366a556c76aSAlexandre Belloni 		break;
367a556c76aSAlexandre Belloni 	case SPEED_2500:
368a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
369a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
370a556c76aSAlexandre Belloni 		break;
371a556c76aSAlexandre Belloni 	default:
372a556c76aSAlexandre Belloni 		netdev_err(dev, "Unsupported PHY speed: %d\n",
373a556c76aSAlexandre Belloni 			   dev->phydev->speed);
374a556c76aSAlexandre Belloni 		return;
375a556c76aSAlexandre Belloni 	}
376a556c76aSAlexandre Belloni 
377a556c76aSAlexandre Belloni 	phy_print_status(dev->phydev);
378a556c76aSAlexandre Belloni 
379a556c76aSAlexandre Belloni 	if (!dev->phydev->link)
380a556c76aSAlexandre Belloni 		return;
381a556c76aSAlexandre Belloni 
382a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
383a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
384a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
385a556c76aSAlexandre Belloni 
386a556c76aSAlexandre Belloni 	/* Set MAC IFG Gaps
387a556c76aSAlexandre Belloni 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
388a556c76aSAlexandre Belloni 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
389a556c76aSAlexandre Belloni 	 */
390a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
391a556c76aSAlexandre Belloni 
392a556c76aSAlexandre Belloni 	/* Load seed (0) and set MAC HDX late collision  */
393a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
394a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG_SEED_LOAD,
395a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
396a556c76aSAlexandre Belloni 	mdelay(1);
397a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
398a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
399a556c76aSAlexandre Belloni 
400a556c76aSAlexandre Belloni 	/* Disable HDX fast control */
401a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
402a556c76aSAlexandre Belloni 
403a556c76aSAlexandre Belloni 	/* SGMII only for now */
404a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
405a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
406a556c76aSAlexandre Belloni 
407a556c76aSAlexandre Belloni 	/* Enable PCS */
408a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
409a556c76aSAlexandre Belloni 
410a556c76aSAlexandre Belloni 	/* No aneg on SGMII */
411a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
412a556c76aSAlexandre Belloni 
413a556c76aSAlexandre Belloni 	/* No loopback */
414a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_LB_CFG);
415a556c76aSAlexandre Belloni 
416a556c76aSAlexandre Belloni 	/* Set Max Length and maximum tags allowed */
417a556c76aSAlexandre Belloni 	ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
418a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
419a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
420a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
421a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG);
422a556c76aSAlexandre Belloni 
423a556c76aSAlexandre Belloni 	/* Enable MAC module */
424a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
425a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
426a556c76aSAlexandre Belloni 
427a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
428a556c76aSAlexandre Belloni 	 * reset */
429a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
430a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
431a556c76aSAlexandre Belloni 
432a556c76aSAlexandre Belloni 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
433a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
434a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
435a556c76aSAlexandre Belloni 
436a556c76aSAlexandre Belloni 	/* No PFC */
437a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
438a556c76aSAlexandre Belloni 			 ANA_PFC_PFC_CFG, p);
439a556c76aSAlexandre Belloni 
440a556c76aSAlexandre Belloni 	/* Set Pause WM hysteresis
441a556c76aSAlexandre Belloni 	 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
442a556c76aSAlexandre Belloni 	 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
443a556c76aSAlexandre Belloni 	 */
444a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
445a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
446a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
447a556c76aSAlexandre Belloni 
448a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
449a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
450a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
451a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
452a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, p);
453a556c76aSAlexandre Belloni 
454a556c76aSAlexandre Belloni 	/* Flow control */
455a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
456a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
457a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
458a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
459a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
460a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG, p);
461a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
462a556c76aSAlexandre Belloni 
463a556c76aSAlexandre Belloni 	/* Tail dropping watermark */
464a556c76aSAlexandre Belloni 	atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
465a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
466a556c76aSAlexandre Belloni 			 SYS_ATOP, p);
467a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
468a556c76aSAlexandre Belloni }
469a556c76aSAlexandre Belloni 
470a556c76aSAlexandre Belloni static int ocelot_port_open(struct net_device *dev)
471a556c76aSAlexandre Belloni {
472a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
473a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
474a556c76aSAlexandre Belloni 	int err;
475a556c76aSAlexandre Belloni 
476a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
477a556c76aSAlexandre Belloni 	 * MAC addresses.
478a556c76aSAlexandre Belloni 	 */
479a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
480a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
481a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
482a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, port->chip_port);
483a556c76aSAlexandre Belloni 
48471e32a20SQuentin Schulz 	if (port->serdes) {
485c8fe6d7fSGrygorii Strashko 		err = phy_set_mode_ext(port->serdes, PHY_MODE_ETHERNET,
486c8fe6d7fSGrygorii Strashko 				       port->phy_mode);
48771e32a20SQuentin Schulz 		if (err) {
48871e32a20SQuentin Schulz 			netdev_err(dev, "Could not set mode of SerDes\n");
48971e32a20SQuentin Schulz 			return err;
49071e32a20SQuentin Schulz 		}
49171e32a20SQuentin Schulz 	}
49271e32a20SQuentin Schulz 
493a556c76aSAlexandre Belloni 	err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
49471e32a20SQuentin Schulz 				 port->phy_mode);
495a556c76aSAlexandre Belloni 	if (err) {
496a556c76aSAlexandre Belloni 		netdev_err(dev, "Could not attach to PHY\n");
497a556c76aSAlexandre Belloni 		return err;
498a556c76aSAlexandre Belloni 	}
499a556c76aSAlexandre Belloni 
500a556c76aSAlexandre Belloni 	dev->phydev = port->phy;
501a556c76aSAlexandre Belloni 
502a556c76aSAlexandre Belloni 	phy_attached_info(port->phy);
503a556c76aSAlexandre Belloni 	phy_start(port->phy);
504a556c76aSAlexandre Belloni 	return 0;
505a556c76aSAlexandre Belloni }
506a556c76aSAlexandre Belloni 
507a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev)
508a556c76aSAlexandre Belloni {
509a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
510a556c76aSAlexandre Belloni 
511a556c76aSAlexandre Belloni 	phy_disconnect(port->phy);
512a556c76aSAlexandre Belloni 
513a556c76aSAlexandre Belloni 	dev->phydev = NULL;
514a556c76aSAlexandre Belloni 
515a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
516a556c76aSAlexandre Belloni 	ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
517a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, port->chip_port);
518a556c76aSAlexandre Belloni 	return 0;
519a556c76aSAlexandre Belloni }
520a556c76aSAlexandre Belloni 
521a556c76aSAlexandre Belloni /* Generate the IFH for frame injection
522a556c76aSAlexandre Belloni  *
523a556c76aSAlexandre Belloni  * The IFH is a 128bit-value
524a556c76aSAlexandre Belloni  * bit 127: bypass the analyzer processing
525a556c76aSAlexandre Belloni  * bit 56-67: destination mask
526a556c76aSAlexandre Belloni  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
527a556c76aSAlexandre Belloni  * bit 20-27: cpu extraction queue mask
528a556c76aSAlexandre Belloni  * bit 16: tag type 0: C-tag, 1: S-tag
529a556c76aSAlexandre Belloni  * bit 0-11: VID
530a556c76aSAlexandre Belloni  */
531a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
532a556c76aSAlexandre Belloni {
533a556c76aSAlexandre Belloni 	ifh[0] = IFH_INJ_BYPASS;
53408d02364SAntoine Tenart 	ifh[1] = (0xf00 & info->port) >> 8;
535a556c76aSAlexandre Belloni 	ifh[2] = (0xff & info->port) << 24;
53608d02364SAntoine Tenart 	ifh[3] = (info->tag_type << 16) | info->vid;
537a556c76aSAlexandre Belloni 
538a556c76aSAlexandre Belloni 	return 0;
539a556c76aSAlexandre Belloni }
540a556c76aSAlexandre Belloni 
541a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
542a556c76aSAlexandre Belloni {
543a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
544a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
545a556c76aSAlexandre Belloni 	u32 val, ifh[IFH_LEN];
546a556c76aSAlexandre Belloni 	struct frame_info info = {};
547a556c76aSAlexandre Belloni 	u8 grp = 0; /* Send everything on CPU group 0 */
548a556c76aSAlexandre Belloni 	unsigned int i, count, last;
549a556c76aSAlexandre Belloni 
550a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, QS_INJ_STATUS);
551a556c76aSAlexandre Belloni 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
552a556c76aSAlexandre Belloni 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
553a556c76aSAlexandre Belloni 		return NETDEV_TX_BUSY;
554a556c76aSAlexandre Belloni 
555a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
556a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
557a556c76aSAlexandre Belloni 
558a556c76aSAlexandre Belloni 	info.port = BIT(port->chip_port);
55908d02364SAntoine Tenart 	info.tag_type = IFH_TAG_TYPE_C;
56008d02364SAntoine Tenart 	info.vid = skb_vlan_tag_get(skb);
561a556c76aSAlexandre Belloni 	ocelot_gen_ifh(ifh, &info);
562a556c76aSAlexandre Belloni 
563a556c76aSAlexandre Belloni 	for (i = 0; i < IFH_LEN; i++)
564c2cd650bSAntoine Tenart 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
565c2cd650bSAntoine Tenart 				 QS_INJ_WR, grp);
566a556c76aSAlexandre Belloni 
567a556c76aSAlexandre Belloni 	count = (skb->len + 3) / 4;
568a556c76aSAlexandre Belloni 	last = skb->len % 4;
569a556c76aSAlexandre Belloni 	for (i = 0; i < count; i++) {
570a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
571a556c76aSAlexandre Belloni 	}
572a556c76aSAlexandre Belloni 
573a556c76aSAlexandre Belloni 	/* Add padding */
574a556c76aSAlexandre Belloni 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
575a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
576a556c76aSAlexandre Belloni 		i++;
577a556c76aSAlexandre Belloni 	}
578a556c76aSAlexandre Belloni 
579a556c76aSAlexandre Belloni 	/* Indicate EOF and valid bytes in last word */
580a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
581a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
582a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_EOF,
583a556c76aSAlexandre Belloni 			 QS_INJ_CTRL, grp);
584a556c76aSAlexandre Belloni 
585a556c76aSAlexandre Belloni 	/* Add dummy CRC */
586a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
587a556c76aSAlexandre Belloni 	skb_tx_timestamp(skb);
588a556c76aSAlexandre Belloni 
589a556c76aSAlexandre Belloni 	dev->stats.tx_packets++;
590a556c76aSAlexandre Belloni 	dev->stats.tx_bytes += skb->len;
591a556c76aSAlexandre Belloni 	dev_kfree_skb_any(skb);
592a556c76aSAlexandre Belloni 
593a556c76aSAlexandre Belloni 	return NETDEV_TX_OK;
594a556c76aSAlexandre Belloni }
595a556c76aSAlexandre Belloni 
596a556c76aSAlexandre Belloni static void ocelot_mact_mc_reset(struct ocelot_port *port)
597a556c76aSAlexandre Belloni {
598a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
599a556c76aSAlexandre Belloni 	struct netdev_hw_addr *ha, *n;
600a556c76aSAlexandre Belloni 
601a556c76aSAlexandre Belloni 	/* Free and forget all the MAC addresses stored in the port private mc
602a556c76aSAlexandre Belloni 	 * list. These are mc addresses that were previously added by calling
603a556c76aSAlexandre Belloni 	 * ocelot_mact_mc_add().
604a556c76aSAlexandre Belloni 	 */
605a556c76aSAlexandre Belloni 	list_for_each_entry_safe(ha, n, &port->mc, list) {
606a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, ha->addr, port->pvid);
607a556c76aSAlexandre Belloni 		list_del(&ha->list);
608a556c76aSAlexandre Belloni 		kfree(ha);
609a556c76aSAlexandre Belloni 	}
610a556c76aSAlexandre Belloni }
611a556c76aSAlexandre Belloni 
612a556c76aSAlexandre Belloni static int ocelot_mact_mc_add(struct ocelot_port *port,
613a556c76aSAlexandre Belloni 			      struct netdev_hw_addr *hw_addr)
614a556c76aSAlexandre Belloni {
615a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
616a556c76aSAlexandre Belloni 	struct netdev_hw_addr *ha = kzalloc(sizeof(*ha), GFP_KERNEL);
617a556c76aSAlexandre Belloni 
618a556c76aSAlexandre Belloni 	if (!ha)
619a556c76aSAlexandre Belloni 		return -ENOMEM;
620a556c76aSAlexandre Belloni 
621a556c76aSAlexandre Belloni 	memcpy(ha, hw_addr, sizeof(*ha));
622a556c76aSAlexandre Belloni 	list_add_tail(&ha->list, &port->mc);
623a556c76aSAlexandre Belloni 
624a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, ha->addr, port->pvid,
625a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
626a556c76aSAlexandre Belloni 
627a556c76aSAlexandre Belloni 	return 0;
628a556c76aSAlexandre Belloni }
629a556c76aSAlexandre Belloni 
630a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev)
631a556c76aSAlexandre Belloni {
632a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
633a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
634a556c76aSAlexandre Belloni 	struct netdev_hw_addr *ha;
635a556c76aSAlexandre Belloni 	int i;
636a556c76aSAlexandre Belloni 	u32 val;
637a556c76aSAlexandre Belloni 
638a556c76aSAlexandre Belloni 	/* This doesn't handle promiscuous mode because the bridge core is
639a556c76aSAlexandre Belloni 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
640a556c76aSAlexandre Belloni 	 * forwarded to the CPU port.
641a556c76aSAlexandre Belloni 	 */
642a556c76aSAlexandre Belloni 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
643a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
644a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
645a556c76aSAlexandre Belloni 
646a556c76aSAlexandre Belloni 	/* Handle the device multicast addresses. First remove all the
647a556c76aSAlexandre Belloni 	 * previously installed addresses and then add the latest ones to the
648a556c76aSAlexandre Belloni 	 * mac table.
649a556c76aSAlexandre Belloni 	 */
650a556c76aSAlexandre Belloni 	ocelot_mact_mc_reset(port);
651a556c76aSAlexandre Belloni 	netdev_for_each_mc_addr(ha, dev)
652a556c76aSAlexandre Belloni 		ocelot_mact_mc_add(port, ha);
653a556c76aSAlexandre Belloni }
654a556c76aSAlexandre Belloni 
655a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev,
656a556c76aSAlexandre Belloni 					  char *buf, size_t len)
657a556c76aSAlexandre Belloni {
658a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
659a556c76aSAlexandre Belloni 	int ret;
660a556c76aSAlexandre Belloni 
661a556c76aSAlexandre Belloni 	ret = snprintf(buf, len, "p%d", port->chip_port);
662a556c76aSAlexandre Belloni 	if (ret >= len)
663a556c76aSAlexandre Belloni 		return -EINVAL;
664a556c76aSAlexandre Belloni 
665a556c76aSAlexandre Belloni 	return 0;
666a556c76aSAlexandre Belloni }
667a556c76aSAlexandre Belloni 
668a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
669a556c76aSAlexandre Belloni {
670a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
671a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
672a556c76aSAlexandre Belloni 	const struct sockaddr *addr = p;
673a556c76aSAlexandre Belloni 
674a556c76aSAlexandre Belloni 	/* Learn the new net device MAC address in the mac table. */
675a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
676a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
677a556c76aSAlexandre Belloni 	/* Then forget the previous one. */
678a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
679a556c76aSAlexandre Belloni 
680a556c76aSAlexandre Belloni 	ether_addr_copy(dev->dev_addr, addr->sa_data);
681a556c76aSAlexandre Belloni 	return 0;
682a556c76aSAlexandre Belloni }
683a556c76aSAlexandre Belloni 
684a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev,
685a556c76aSAlexandre Belloni 			       struct rtnl_link_stats64 *stats)
686a556c76aSAlexandre Belloni {
687a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
688a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
689a556c76aSAlexandre Belloni 
690a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
691a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
692a556c76aSAlexandre Belloni 		     SYS_STAT_CFG);
693a556c76aSAlexandre Belloni 
694a556c76aSAlexandre Belloni 	/* Get Rx stats */
695a556c76aSAlexandre Belloni 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
696a556c76aSAlexandre Belloni 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
697a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
698a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
699a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
700a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
701a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
702a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
703a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
704a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
705a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
706a556c76aSAlexandre Belloni 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
707a556c76aSAlexandre Belloni 	stats->rx_dropped = dev->stats.rx_dropped;
708a556c76aSAlexandre Belloni 
709a556c76aSAlexandre Belloni 	/* Get Tx stats */
710a556c76aSAlexandre Belloni 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
711a556c76aSAlexandre Belloni 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
712a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
713a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
714a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
715a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
716a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
717a556c76aSAlexandre Belloni 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
718a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
719a556c76aSAlexandre Belloni 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
720a556c76aSAlexandre Belloni }
721a556c76aSAlexandre Belloni 
722a556c76aSAlexandre Belloni static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
723a556c76aSAlexandre Belloni 			  struct net_device *dev, const unsigned char *addr,
724*87b0984eSPetr Machata 			  u16 vid, u16 flags,
725*87b0984eSPetr Machata 			  struct netlink_ext_ack *extack)
726a556c76aSAlexandre Belloni {
727a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
728a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
729a556c76aSAlexandre Belloni 
7307142529fSAntoine Tenart 	if (!vid) {
7317142529fSAntoine Tenart 		if (!port->vlan_aware)
7327142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
7337142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
7347142529fSAntoine Tenart 			 * matches incoming untagged packets
7357142529fSAntoine Tenart 			 */
7367142529fSAntoine Tenart 			vid = port->pvid;
7377142529fSAntoine Tenart 		else
7387142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
7397142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
7407142529fSAntoine Tenart 			 */
7417142529fSAntoine Tenart 			return -EINVAL;
7427142529fSAntoine Tenart 	}
7437142529fSAntoine Tenart 
744a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
7458fd1a4afSAllan W. Nielsen 				 ENTRYTYPE_LOCKED);
746a556c76aSAlexandre Belloni }
747a556c76aSAlexandre Belloni 
748a556c76aSAlexandre Belloni static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
749a556c76aSAlexandre Belloni 			  struct net_device *dev,
750a556c76aSAlexandre Belloni 			  const unsigned char *addr, u16 vid)
751a556c76aSAlexandre Belloni {
752a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
753a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
754a556c76aSAlexandre Belloni 
755a556c76aSAlexandre Belloni 	return ocelot_mact_forget(ocelot, addr, vid);
756a556c76aSAlexandre Belloni }
757a556c76aSAlexandre Belloni 
758a556c76aSAlexandre Belloni struct ocelot_dump_ctx {
759a556c76aSAlexandre Belloni 	struct net_device *dev;
760a556c76aSAlexandre Belloni 	struct sk_buff *skb;
761a556c76aSAlexandre Belloni 	struct netlink_callback *cb;
762a556c76aSAlexandre Belloni 	int idx;
763a556c76aSAlexandre Belloni };
764a556c76aSAlexandre Belloni 
765a556c76aSAlexandre Belloni static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
766a556c76aSAlexandre Belloni 			      struct ocelot_dump_ctx *dump)
767a556c76aSAlexandre Belloni {
768a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
769a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
770a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
771a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
772a556c76aSAlexandre Belloni 
773a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
774a556c76aSAlexandre Belloni 		goto skip;
775a556c76aSAlexandre Belloni 
776a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
777a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
778a556c76aSAlexandre Belloni 	if (!nlh)
779a556c76aSAlexandre Belloni 		return -EMSGSIZE;
780a556c76aSAlexandre Belloni 
781a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
782a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
783a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
784a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
785a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
786a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
787a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
788a556c76aSAlexandre Belloni 	ndm->ndm_state   = NUD_REACHABLE;
789a556c76aSAlexandre Belloni 
790a556c76aSAlexandre Belloni 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
791a556c76aSAlexandre Belloni 		goto nla_put_failure;
792a556c76aSAlexandre Belloni 
793a556c76aSAlexandre Belloni 	if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
794a556c76aSAlexandre Belloni 		goto nla_put_failure;
795a556c76aSAlexandre Belloni 
796a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
797a556c76aSAlexandre Belloni 
798a556c76aSAlexandre Belloni skip:
799a556c76aSAlexandre Belloni 	dump->idx++;
800a556c76aSAlexandre Belloni 	return 0;
801a556c76aSAlexandre Belloni 
802a556c76aSAlexandre Belloni nla_put_failure:
803a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
804a556c76aSAlexandre Belloni 	return -EMSGSIZE;
805a556c76aSAlexandre Belloni }
806a556c76aSAlexandre Belloni 
807a556c76aSAlexandre Belloni static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
808a556c76aSAlexandre Belloni 				   struct ocelot_mact_entry *entry)
809a556c76aSAlexandre Belloni {
810a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
811a556c76aSAlexandre Belloni 	char mac[ETH_ALEN];
812a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
813a556c76aSAlexandre Belloni 
814a556c76aSAlexandre Belloni 	/* Set row and column to read from */
815a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
816a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
817a556c76aSAlexandre Belloni 
818a556c76aSAlexandre Belloni 	/* Issue a read command */
819a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
820a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
821a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
822a556c76aSAlexandre Belloni 
823a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
824a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
825a556c76aSAlexandre Belloni 
826a556c76aSAlexandre Belloni 	/* Read the entry flags */
827a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
828a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
829a556c76aSAlexandre Belloni 		return -EINVAL;
830a556c76aSAlexandre Belloni 
831a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
832a556c76aSAlexandre Belloni 	 * do not report it.
833a556c76aSAlexandre Belloni 	 */
834a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
835a556c76aSAlexandre Belloni 	if (dst != port->chip_port)
836a556c76aSAlexandre Belloni 		return -EINVAL;
837a556c76aSAlexandre Belloni 
838a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
839a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
840a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
841a556c76aSAlexandre Belloni 
842a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
843a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
844a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
845a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
846a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
847a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
848a556c76aSAlexandre Belloni 
849a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
850a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
851a556c76aSAlexandre Belloni 
852a556c76aSAlexandre Belloni 	return 0;
853a556c76aSAlexandre Belloni }
854a556c76aSAlexandre Belloni 
855a556c76aSAlexandre Belloni static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
856a556c76aSAlexandre Belloni 			   struct net_device *dev,
857a556c76aSAlexandre Belloni 			   struct net_device *filter_dev, int *idx)
858a556c76aSAlexandre Belloni {
859a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
860a556c76aSAlexandre Belloni 	int i, j, ret = 0;
861a556c76aSAlexandre Belloni 	struct ocelot_dump_ctx dump = {
862a556c76aSAlexandre Belloni 		.dev = dev,
863a556c76aSAlexandre Belloni 		.skb = skb,
864a556c76aSAlexandre Belloni 		.cb = cb,
865a556c76aSAlexandre Belloni 		.idx = *idx,
866a556c76aSAlexandre Belloni 	};
867a556c76aSAlexandre Belloni 
868a556c76aSAlexandre Belloni 	struct ocelot_mact_entry entry;
869a556c76aSAlexandre Belloni 
870a556c76aSAlexandre Belloni 	/* Loop through all the mac tables entries. There are 1024 rows of 4
871a556c76aSAlexandre Belloni 	 * entries.
872a556c76aSAlexandre Belloni 	 */
873a556c76aSAlexandre Belloni 	for (i = 0; i < 1024; i++) {
874a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
875a556c76aSAlexandre Belloni 			ret = ocelot_mact_read(port, i, j, &entry);
876a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
877a556c76aSAlexandre Belloni 			 * skip it.
878a556c76aSAlexandre Belloni 			 */
879a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
880a556c76aSAlexandre Belloni 				continue;
881a556c76aSAlexandre Belloni 			else if (ret)
882a556c76aSAlexandre Belloni 				goto end;
883a556c76aSAlexandre Belloni 
884a556c76aSAlexandre Belloni 			ret = ocelot_fdb_do_dump(&entry, &dump);
885a556c76aSAlexandre Belloni 			if (ret)
886a556c76aSAlexandre Belloni 				goto end;
887a556c76aSAlexandre Belloni 		}
888a556c76aSAlexandre Belloni 	}
889a556c76aSAlexandre Belloni 
890a556c76aSAlexandre Belloni end:
891a556c76aSAlexandre Belloni 	*idx = dump.idx;
892a556c76aSAlexandre Belloni 	return ret;
893a556c76aSAlexandre Belloni }
894a556c76aSAlexandre Belloni 
8957142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
8967142529fSAntoine Tenart 				  u16 vid)
8977142529fSAntoine Tenart {
8987142529fSAntoine Tenart 	return ocelot_vlan_vid_add(dev, vid, false, true);
8997142529fSAntoine Tenart }
9007142529fSAntoine Tenart 
9017142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
9027142529fSAntoine Tenart 				   u16 vid)
9037142529fSAntoine Tenart {
9047142529fSAntoine Tenart 	return ocelot_vlan_vid_del(dev, vid);
9057142529fSAntoine Tenart }
9067142529fSAntoine Tenart 
9077142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev,
9087142529fSAntoine Tenart 			       netdev_features_t features)
9097142529fSAntoine Tenart {
9107142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
9117142529fSAntoine Tenart 	netdev_features_t changed = dev->features ^ features;
9127142529fSAntoine Tenart 
9137142529fSAntoine Tenart 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
9147142529fSAntoine Tenart 		ocelot_vlan_mode(port, features);
9157142529fSAntoine Tenart 
9167142529fSAntoine Tenart 	return 0;
9177142529fSAntoine Tenart }
9187142529fSAntoine Tenart 
919a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = {
920a556c76aSAlexandre Belloni 	.ndo_open			= ocelot_port_open,
921a556c76aSAlexandre Belloni 	.ndo_stop			= ocelot_port_stop,
922a556c76aSAlexandre Belloni 	.ndo_start_xmit			= ocelot_port_xmit,
923a556c76aSAlexandre Belloni 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
924a556c76aSAlexandre Belloni 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
925a556c76aSAlexandre Belloni 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
926a556c76aSAlexandre Belloni 	.ndo_get_stats64		= ocelot_get_stats64,
927a556c76aSAlexandre Belloni 	.ndo_fdb_add			= ocelot_fdb_add,
928a556c76aSAlexandre Belloni 	.ndo_fdb_del			= ocelot_fdb_del,
929a556c76aSAlexandre Belloni 	.ndo_fdb_dump			= ocelot_fdb_dump,
9307142529fSAntoine Tenart 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
9317142529fSAntoine Tenart 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
9327142529fSAntoine Tenart 	.ndo_set_features		= ocelot_set_features,
933a556c76aSAlexandre Belloni };
934a556c76aSAlexandre Belloni 
935a556c76aSAlexandre Belloni static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
936a556c76aSAlexandre Belloni {
937a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(netdev);
938a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
939a556c76aSAlexandre Belloni 	int i;
940a556c76aSAlexandre Belloni 
941a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
942a556c76aSAlexandre Belloni 		return;
943a556c76aSAlexandre Belloni 
944a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
945a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
946a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
947a556c76aSAlexandre Belloni }
948a556c76aSAlexandre Belloni 
949a556c76aSAlexandre Belloni static void ocelot_check_stats(struct work_struct *work)
950a556c76aSAlexandre Belloni {
951a556c76aSAlexandre Belloni 	struct delayed_work *del_work = to_delayed_work(work);
952a556c76aSAlexandre Belloni 	struct ocelot *ocelot = container_of(del_work, struct ocelot, stats_work);
953a556c76aSAlexandre Belloni 	int i, j;
954a556c76aSAlexandre Belloni 
955a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
956a556c76aSAlexandre Belloni 
957a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
958a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
959a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
960a556c76aSAlexandre Belloni 
961a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
962a556c76aSAlexandre Belloni 			u32 val;
963a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
964a556c76aSAlexandre Belloni 
965a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
966a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
967a556c76aSAlexandre Belloni 
968a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
969a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
970a556c76aSAlexandre Belloni 
971a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
972a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
973a556c76aSAlexandre Belloni 		}
974a556c76aSAlexandre Belloni 	}
975a556c76aSAlexandre Belloni 
976a556c76aSAlexandre Belloni 	cancel_delayed_work(&ocelot->stats_work);
977a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
978a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
979a556c76aSAlexandre Belloni 
980a556c76aSAlexandre Belloni 	mutex_unlock(&ocelot->stats_lock);
981a556c76aSAlexandre Belloni }
982a556c76aSAlexandre Belloni 
983a556c76aSAlexandre Belloni static void ocelot_get_ethtool_stats(struct net_device *dev,
984a556c76aSAlexandre Belloni 				     struct ethtool_stats *stats, u64 *data)
985a556c76aSAlexandre Belloni {
986a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
987a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
988a556c76aSAlexandre Belloni 	int i;
989a556c76aSAlexandre Belloni 
990a556c76aSAlexandre Belloni 	/* check and update now */
991a556c76aSAlexandre Belloni 	ocelot_check_stats(&ocelot->stats_work.work);
992a556c76aSAlexandre Belloni 
993a556c76aSAlexandre Belloni 	/* Copy all counters */
994a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
995a556c76aSAlexandre Belloni 		*data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
996a556c76aSAlexandre Belloni }
997a556c76aSAlexandre Belloni 
998a556c76aSAlexandre Belloni static int ocelot_get_sset_count(struct net_device *dev, int sset)
999a556c76aSAlexandre Belloni {
1000a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1001a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1002a556c76aSAlexandre Belloni 
1003a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1004a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1005a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1006a556c76aSAlexandre Belloni }
1007a556c76aSAlexandre Belloni 
1008a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = {
1009a556c76aSAlexandre Belloni 	.get_strings		= ocelot_get_strings,
1010a556c76aSAlexandre Belloni 	.get_ethtool_stats	= ocelot_get_ethtool_stats,
1011a556c76aSAlexandre Belloni 	.get_sset_count		= ocelot_get_sset_count,
1012dc96ee37SAlexandre Belloni 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1013dc96ee37SAlexandre Belloni 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1014a556c76aSAlexandre Belloni };
1015a556c76aSAlexandre Belloni 
1016a556c76aSAlexandre Belloni static int ocelot_port_attr_get(struct net_device *dev,
1017a556c76aSAlexandre Belloni 				struct switchdev_attr *attr)
1018a556c76aSAlexandre Belloni {
1019a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1020a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1021a556c76aSAlexandre Belloni 
1022a556c76aSAlexandre Belloni 	switch (attr->id) {
1023a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
1024a556c76aSAlexandre Belloni 		attr->u.ppid.id_len = sizeof(ocelot->base_mac);
1025a556c76aSAlexandre Belloni 		memcpy(&attr->u.ppid.id, &ocelot->base_mac,
1026a556c76aSAlexandre Belloni 		       attr->u.ppid.id_len);
1027a556c76aSAlexandre Belloni 		break;
1028a556c76aSAlexandre Belloni 	default:
1029a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1030a556c76aSAlexandre Belloni 	}
1031a556c76aSAlexandre Belloni 
1032a556c76aSAlexandre Belloni 	return 0;
1033a556c76aSAlexandre Belloni }
1034a556c76aSAlexandre Belloni 
1035a556c76aSAlexandre Belloni static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1036a556c76aSAlexandre Belloni 					  struct switchdev_trans *trans,
1037a556c76aSAlexandre Belloni 					  u8 state)
1038a556c76aSAlexandre Belloni {
1039a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1040a556c76aSAlexandre Belloni 	u32 port_cfg;
1041a556c76aSAlexandre Belloni 	int port, i;
1042a556c76aSAlexandre Belloni 
1043a556c76aSAlexandre Belloni 	if (switchdev_trans_ph_prepare(trans))
1044a556c76aSAlexandre Belloni 		return 0;
1045a556c76aSAlexandre Belloni 
1046a556c76aSAlexandre Belloni 	if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1047a556c76aSAlexandre Belloni 		return 0;
1048a556c76aSAlexandre Belloni 
1049a556c76aSAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1050a556c76aSAlexandre Belloni 				   ocelot_port->chip_port);
1051a556c76aSAlexandre Belloni 
1052a556c76aSAlexandre Belloni 	switch (state) {
1053a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
1054a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1055a556c76aSAlexandre Belloni 		/* Fallthrough */
1056a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1057a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1058a556c76aSAlexandre Belloni 		break;
1059a556c76aSAlexandre Belloni 
1060a556c76aSAlexandre Belloni 	default:
1061a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1062a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1063a556c76aSAlexandre Belloni 		break;
1064a556c76aSAlexandre Belloni 	}
1065a556c76aSAlexandre Belloni 
1066a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1067a556c76aSAlexandre Belloni 			 ocelot_port->chip_port);
1068a556c76aSAlexandre Belloni 
1069a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1070a556c76aSAlexandre Belloni 	 * a source for the other ports.
1071a556c76aSAlexandre Belloni 	 */
1072a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1073a556c76aSAlexandre Belloni 		if (ocelot->bridge_fwd_mask & BIT(port)) {
1074a556c76aSAlexandre Belloni 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1075a556c76aSAlexandre Belloni 
1076a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1077a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
1078a556c76aSAlexandre Belloni 
1079a556c76aSAlexandre Belloni 				if (!bond_mask)
1080a556c76aSAlexandre Belloni 					continue;
1081a556c76aSAlexandre Belloni 
1082a556c76aSAlexandre Belloni 				if (bond_mask & BIT(port)) {
1083a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
1084a556c76aSAlexandre Belloni 					break;
1085a556c76aSAlexandre Belloni 				}
1086a556c76aSAlexandre Belloni 			}
1087a556c76aSAlexandre Belloni 
1088a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1089a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports) | mask,
1090a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1091a556c76aSAlexandre Belloni 		} else {
1092a556c76aSAlexandre Belloni 			/* Only the CPU port, this is compatible with link
1093a556c76aSAlexandre Belloni 			 * aggregation.
1094a556c76aSAlexandre Belloni 			 */
1095a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1096a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports),
1097a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1098a556c76aSAlexandre Belloni 		}
1099a556c76aSAlexandre Belloni 	}
1100a556c76aSAlexandre Belloni 
1101a556c76aSAlexandre Belloni 	return 0;
1102a556c76aSAlexandre Belloni }
1103a556c76aSAlexandre Belloni 
1104a556c76aSAlexandre Belloni static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1105a556c76aSAlexandre Belloni 					unsigned long ageing_clock_t)
1106a556c76aSAlexandre Belloni {
1107a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1108a556c76aSAlexandre Belloni 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1109a556c76aSAlexandre Belloni 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1110a556c76aSAlexandre Belloni 
1111a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1112a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1113a556c76aSAlexandre Belloni }
1114a556c76aSAlexandre Belloni 
1115a556c76aSAlexandre Belloni static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1116a556c76aSAlexandre Belloni {
1117a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1118a556c76aSAlexandre Belloni 	u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1119a556c76aSAlexandre Belloni 				  port->chip_port);
1120a556c76aSAlexandre Belloni 
1121a556c76aSAlexandre Belloni 	if (mc)
1122a556c76aSAlexandre Belloni 		val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1123a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1124a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1125a556c76aSAlexandre Belloni 	else
1126a556c76aSAlexandre Belloni 		val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1127a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1128a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1129a556c76aSAlexandre Belloni 
1130a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1131a556c76aSAlexandre Belloni }
1132a556c76aSAlexandre Belloni 
1133a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev,
1134a556c76aSAlexandre Belloni 				const struct switchdev_attr *attr,
1135a556c76aSAlexandre Belloni 				struct switchdev_trans *trans)
1136a556c76aSAlexandre Belloni {
1137a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1138a556c76aSAlexandre Belloni 	int err = 0;
1139a556c76aSAlexandre Belloni 
1140a556c76aSAlexandre Belloni 	switch (attr->id) {
1141a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1142a556c76aSAlexandre Belloni 		ocelot_port_attr_stp_state_set(ocelot_port, trans,
1143a556c76aSAlexandre Belloni 					       attr->u.stp_state);
1144a556c76aSAlexandre Belloni 		break;
1145a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1146a556c76aSAlexandre Belloni 		ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1147a556c76aSAlexandre Belloni 		break;
11487142529fSAntoine Tenart 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
11497142529fSAntoine Tenart 		ocelot_port->vlan_aware = attr->u.vlan_filtering;
11507142529fSAntoine Tenart 		ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port);
11517142529fSAntoine Tenart 		break;
1152a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1153a556c76aSAlexandre Belloni 		ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1154a556c76aSAlexandre Belloni 		break;
1155a556c76aSAlexandre Belloni 	default:
1156a556c76aSAlexandre Belloni 		err = -EOPNOTSUPP;
1157a556c76aSAlexandre Belloni 		break;
1158a556c76aSAlexandre Belloni 	}
1159a556c76aSAlexandre Belloni 
1160a556c76aSAlexandre Belloni 	return err;
1161a556c76aSAlexandre Belloni }
1162a556c76aSAlexandre Belloni 
11637142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev,
11647142529fSAntoine Tenart 				    const struct switchdev_obj_port_vlan *vlan,
11657142529fSAntoine Tenart 				    struct switchdev_trans *trans)
11667142529fSAntoine Tenart {
11677142529fSAntoine Tenart 	int ret;
11687142529fSAntoine Tenart 	u16 vid;
11697142529fSAntoine Tenart 
11707142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
11717142529fSAntoine Tenart 		ret = ocelot_vlan_vid_add(dev, vid,
11727142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
11737142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
11747142529fSAntoine Tenart 		if (ret)
11757142529fSAntoine Tenart 			return ret;
11767142529fSAntoine Tenart 	}
11777142529fSAntoine Tenart 
11787142529fSAntoine Tenart 	return 0;
11797142529fSAntoine Tenart }
11807142529fSAntoine Tenart 
11817142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev,
11827142529fSAntoine Tenart 				     const struct switchdev_obj_port_vlan *vlan)
11837142529fSAntoine Tenart {
11847142529fSAntoine Tenart 	int ret;
11857142529fSAntoine Tenart 	u16 vid;
11867142529fSAntoine Tenart 
11877142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
11887142529fSAntoine Tenart 		ret = ocelot_vlan_vid_del(dev, vid);
11897142529fSAntoine Tenart 
11907142529fSAntoine Tenart 		if (ret)
11917142529fSAntoine Tenart 			return ret;
11927142529fSAntoine Tenart 	}
11937142529fSAntoine Tenart 
11947142529fSAntoine Tenart 	return 0;
11957142529fSAntoine Tenart }
11967142529fSAntoine Tenart 
1197a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1198a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1199a556c76aSAlexandre Belloni 						     u16 vid)
1200a556c76aSAlexandre Belloni {
1201a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1202a556c76aSAlexandre Belloni 
1203a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1204a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1205a556c76aSAlexandre Belloni 			return mc;
1206a556c76aSAlexandre Belloni 	}
1207a556c76aSAlexandre Belloni 
1208a556c76aSAlexandre Belloni 	return NULL;
1209a556c76aSAlexandre Belloni }
1210a556c76aSAlexandre Belloni 
1211a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev,
1212a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb,
1213a556c76aSAlexandre Belloni 				   struct switchdev_trans *trans)
1214a556c76aSAlexandre Belloni {
1215a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1216a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1217a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1218a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1219a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1220a556c76aSAlexandre Belloni 	bool new = false;
1221a556c76aSAlexandre Belloni 
1222a556c76aSAlexandre Belloni 	if (!vid)
12237142529fSAntoine Tenart 		vid = port->pvid;
1224a556c76aSAlexandre Belloni 
1225a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1226a556c76aSAlexandre Belloni 	if (!mc) {
1227a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1228a556c76aSAlexandre Belloni 		if (!mc)
1229a556c76aSAlexandre Belloni 			return -ENOMEM;
1230a556c76aSAlexandre Belloni 
1231a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1232a556c76aSAlexandre Belloni 		mc->vid = vid;
1233a556c76aSAlexandre Belloni 
1234a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1235a556c76aSAlexandre Belloni 		new = true;
1236a556c76aSAlexandre Belloni 	}
1237a556c76aSAlexandre Belloni 
1238a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1239a556c76aSAlexandre Belloni 	addr[0] = 0;
1240a556c76aSAlexandre Belloni 
1241a556c76aSAlexandre Belloni 	if (!new) {
1242a556c76aSAlexandre Belloni 		addr[2] = mc->ports << 0;
1243a556c76aSAlexandre Belloni 		addr[1] = mc->ports << 8;
1244a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1245a556c76aSAlexandre Belloni 	}
1246a556c76aSAlexandre Belloni 
1247a556c76aSAlexandre Belloni 	mc->ports |= BIT(port->chip_port);
1248a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1249a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1250a556c76aSAlexandre Belloni 
1251a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1252a556c76aSAlexandre Belloni }
1253a556c76aSAlexandre Belloni 
1254a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev,
1255a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb)
1256a556c76aSAlexandre Belloni {
1257a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1258a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1259a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1260a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1261a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1262a556c76aSAlexandre Belloni 
1263a556c76aSAlexandre Belloni 	if (!vid)
12647142529fSAntoine Tenart 		vid = port->pvid;
1265a556c76aSAlexandre Belloni 
1266a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1267a556c76aSAlexandre Belloni 	if (!mc)
1268a556c76aSAlexandre Belloni 		return -ENOENT;
1269a556c76aSAlexandre Belloni 
1270a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1271a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1272a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1273a556c76aSAlexandre Belloni 	addr[0] = 0;
1274a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1275a556c76aSAlexandre Belloni 
1276a556c76aSAlexandre Belloni 	mc->ports &= ~BIT(port->chip_port);
1277a556c76aSAlexandre Belloni 	if (!mc->ports) {
1278a556c76aSAlexandre Belloni 		list_del(&mc->list);
1279a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1280a556c76aSAlexandre Belloni 		return 0;
1281a556c76aSAlexandre Belloni 	}
1282a556c76aSAlexandre Belloni 
1283a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1284a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1285a556c76aSAlexandre Belloni 
1286a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1287a556c76aSAlexandre Belloni }
1288a556c76aSAlexandre Belloni 
1289a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev,
1290a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj,
129169213513SPetr Machata 			       struct switchdev_trans *trans,
129269213513SPetr Machata 			       struct netlink_ext_ack *extack)
1293a556c76aSAlexandre Belloni {
1294a556c76aSAlexandre Belloni 	int ret = 0;
1295a556c76aSAlexandre Belloni 
1296a556c76aSAlexandre Belloni 	switch (obj->id) {
12977142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
12987142529fSAntoine Tenart 		ret = ocelot_port_obj_add_vlan(dev,
12997142529fSAntoine Tenart 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
13007142529fSAntoine Tenart 					       trans);
13017142529fSAntoine Tenart 		break;
1302a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1303a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1304a556c76aSAlexandre Belloni 					      trans);
1305a556c76aSAlexandre Belloni 		break;
1306a556c76aSAlexandre Belloni 	default:
1307a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1308a556c76aSAlexandre Belloni 	}
1309a556c76aSAlexandre Belloni 
1310a556c76aSAlexandre Belloni 	return ret;
1311a556c76aSAlexandre Belloni }
1312a556c76aSAlexandre Belloni 
1313a556c76aSAlexandre Belloni static int ocelot_port_obj_del(struct net_device *dev,
1314a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj)
1315a556c76aSAlexandre Belloni {
1316a556c76aSAlexandre Belloni 	int ret = 0;
1317a556c76aSAlexandre Belloni 
1318a556c76aSAlexandre Belloni 	switch (obj->id) {
13197142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
13207142529fSAntoine Tenart 		ret = ocelot_port_vlan_del_vlan(dev,
13217142529fSAntoine Tenart 						SWITCHDEV_OBJ_PORT_VLAN(obj));
13227142529fSAntoine Tenart 		break;
1323a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1324a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1325a556c76aSAlexandre Belloni 		break;
1326a556c76aSAlexandre Belloni 	default:
1327a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1328a556c76aSAlexandre Belloni 	}
1329a556c76aSAlexandre Belloni 
1330a556c76aSAlexandre Belloni 	return ret;
1331a556c76aSAlexandre Belloni }
1332a556c76aSAlexandre Belloni 
1333a556c76aSAlexandre Belloni static const struct switchdev_ops ocelot_port_switchdev_ops = {
1334a556c76aSAlexandre Belloni 	.switchdev_port_attr_get	= ocelot_port_attr_get,
1335a556c76aSAlexandre Belloni 	.switchdev_port_attr_set	= ocelot_port_attr_set,
1336a556c76aSAlexandre Belloni };
1337a556c76aSAlexandre Belloni 
1338a556c76aSAlexandre Belloni static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1339a556c76aSAlexandre Belloni 				   struct net_device *bridge)
1340a556c76aSAlexandre Belloni {
1341a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1342a556c76aSAlexandre Belloni 
1343a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1344a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1345a556c76aSAlexandre Belloni 	} else {
1346a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1347a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1348a556c76aSAlexandre Belloni 			 * unsupported */
1349a556c76aSAlexandre Belloni 			return -ENODEV;
1350a556c76aSAlexandre Belloni 	}
1351a556c76aSAlexandre Belloni 
1352a556c76aSAlexandre Belloni 	ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1353a556c76aSAlexandre Belloni 
1354a556c76aSAlexandre Belloni 	return 0;
1355a556c76aSAlexandre Belloni }
1356a556c76aSAlexandre Belloni 
1357a556c76aSAlexandre Belloni static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1358a556c76aSAlexandre Belloni 				     struct net_device *bridge)
1359a556c76aSAlexandre Belloni {
1360a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1361a556c76aSAlexandre Belloni 
1362a556c76aSAlexandre Belloni 	ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
1363a556c76aSAlexandre Belloni 
1364a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1365a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
13667142529fSAntoine Tenart 
13677142529fSAntoine Tenart 	/* Clear bridge vlan settings before calling ocelot_vlan_port_apply */
13687142529fSAntoine Tenart 	ocelot_port->vlan_aware = 0;
13697142529fSAntoine Tenart 	ocelot_port->pvid = 0;
13707142529fSAntoine Tenart 	ocelot_port->vid = 0;
1371a556c76aSAlexandre Belloni }
1372a556c76aSAlexandre Belloni 
1373dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1374dc96ee37SAlexandre Belloni {
1375dc96ee37SAlexandre Belloni 	int i, port, lag;
1376dc96ee37SAlexandre Belloni 
1377dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1378dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1379dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1380dc96ee37SAlexandre Belloni 
1381dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1382dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1383dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1384dc96ee37SAlexandre Belloni 
1385dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1386dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1387dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1388dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1389dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1390dc96ee37SAlexandre Belloni 
1391dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1392dc96ee37SAlexandre Belloni 		if (!bond_mask)
1393dc96ee37SAlexandre Belloni 			continue;
1394dc96ee37SAlexandre Belloni 
1395dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1396dc96ee37SAlexandre Belloni 			// Destination mask
1397dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1398dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1399dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1400dc96ee37SAlexandre Belloni 			aggr_count++;
1401dc96ee37SAlexandre Belloni 		}
1402dc96ee37SAlexandre Belloni 
1403dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1404dc96ee37SAlexandre Belloni 			u32 ac;
1405dc96ee37SAlexandre Belloni 
1406dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1407dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1408dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1409dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1410dc96ee37SAlexandre Belloni 		}
1411dc96ee37SAlexandre Belloni 	}
1412dc96ee37SAlexandre Belloni }
1413dc96ee37SAlexandre Belloni 
1414dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1415dc96ee37SAlexandre Belloni {
1416dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1417dc96ee37SAlexandre Belloni 	unsigned int p;
1418dc96ee37SAlexandre Belloni 
1419dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1420dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1421dc96ee37SAlexandre Belloni 
1422dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1423dc96ee37SAlexandre Belloni 
1424dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1425dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1426dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1427dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1428dc96ee37SAlexandre Belloni 	}
1429dc96ee37SAlexandre Belloni }
1430dc96ee37SAlexandre Belloni 
1431dc96ee37SAlexandre Belloni static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1432dc96ee37SAlexandre Belloni 				struct net_device *bond)
1433dc96ee37SAlexandre Belloni {
1434dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1435dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1436dc96ee37SAlexandre Belloni 	int lag, lp;
1437dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1438dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1439dc96ee37SAlexandre Belloni 
1440dc96ee37SAlexandre Belloni 	rcu_read_lock();
1441dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1442dc96ee37SAlexandre Belloni 		struct ocelot_port *port = netdev_priv(ndev);
1443dc96ee37SAlexandre Belloni 
1444dc96ee37SAlexandre Belloni 		bond_mask |= BIT(port->chip_port);
1445dc96ee37SAlexandre Belloni 	}
1446dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1447dc96ee37SAlexandre Belloni 
1448dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1449dc96ee37SAlexandre Belloni 
1450dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1451dc96ee37SAlexandre Belloni 	 * now on
1452dc96ee37SAlexandre Belloni 	 */
1453dc96ee37SAlexandre Belloni 	if (p == lp) {
1454dc96ee37SAlexandre Belloni 		lag = p;
1455dc96ee37SAlexandre Belloni 		ocelot->lags[p] = bond_mask;
1456dc96ee37SAlexandre Belloni 		bond_mask &= ~BIT(p);
1457dc96ee37SAlexandre Belloni 		if (bond_mask) {
1458dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1459dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1460dc96ee37SAlexandre Belloni 		}
1461dc96ee37SAlexandre Belloni 	} else {
1462dc96ee37SAlexandre Belloni 		lag = lp;
1463dc96ee37SAlexandre Belloni 		ocelot->lags[lp] |= BIT(p);
1464dc96ee37SAlexandre Belloni 	}
1465dc96ee37SAlexandre Belloni 
1466dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1467dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1468dc96ee37SAlexandre Belloni 
1469dc96ee37SAlexandre Belloni 	return 0;
1470dc96ee37SAlexandre Belloni }
1471dc96ee37SAlexandre Belloni 
1472dc96ee37SAlexandre Belloni static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1473dc96ee37SAlexandre Belloni 				  struct net_device *bond)
1474dc96ee37SAlexandre Belloni {
1475dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1476dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1477dc96ee37SAlexandre Belloni 	u32 port_cfg;
1478dc96ee37SAlexandre Belloni 	int i;
1479dc96ee37SAlexandre Belloni 
1480dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1481dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1482dc96ee37SAlexandre Belloni 		ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1483dc96ee37SAlexandre Belloni 
1484dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1485dc96ee37SAlexandre Belloni 	 * next port
1486dc96ee37SAlexandre Belloni 	 */
1487dc96ee37SAlexandre Belloni 	if (ocelot->lags[p]) {
1488dc96ee37SAlexandre Belloni 		int n = __ffs(ocelot->lags[p]);
1489dc96ee37SAlexandre Belloni 
1490dc96ee37SAlexandre Belloni 		ocelot->lags[n] = ocelot->lags[p];
1491dc96ee37SAlexandre Belloni 		ocelot->lags[p] = 0;
1492dc96ee37SAlexandre Belloni 
1493dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1494dc96ee37SAlexandre Belloni 	}
1495dc96ee37SAlexandre Belloni 
1496dc96ee37SAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1497dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1498dc96ee37SAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1499dc96ee37SAlexandre Belloni 			 ANA_PORT_PORT_CFG, p);
1500dc96ee37SAlexandre Belloni 
1501dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1502dc96ee37SAlexandre Belloni }
1503dc96ee37SAlexandre Belloni 
1504a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */
1505a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1506a556c76aSAlexandre Belloni {
1507a556c76aSAlexandre Belloni 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1508a556c76aSAlexandre Belloni }
1509a556c76aSAlexandre Belloni 
1510a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev,
1511a556c76aSAlexandre Belloni 				       unsigned long event,
1512a556c76aSAlexandre Belloni 				       struct netdev_notifier_changeupper_info *info)
1513a556c76aSAlexandre Belloni {
1514a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1515a556c76aSAlexandre Belloni 	int err = 0;
1516a556c76aSAlexandre Belloni 
1517a556c76aSAlexandre Belloni 	if (!ocelot_netdevice_dev_check(dev))
1518a556c76aSAlexandre Belloni 		return 0;
1519a556c76aSAlexandre Belloni 
1520a556c76aSAlexandre Belloni 	switch (event) {
1521a556c76aSAlexandre Belloni 	case NETDEV_CHANGEUPPER:
1522a556c76aSAlexandre Belloni 		if (netif_is_bridge_master(info->upper_dev)) {
1523a556c76aSAlexandre Belloni 			if (info->linking)
1524a556c76aSAlexandre Belloni 				err = ocelot_port_bridge_join(ocelot_port,
1525a556c76aSAlexandre Belloni 							      info->upper_dev);
1526a556c76aSAlexandre Belloni 			else
1527a556c76aSAlexandre Belloni 				ocelot_port_bridge_leave(ocelot_port,
1528a556c76aSAlexandre Belloni 							 info->upper_dev);
15297142529fSAntoine Tenart 
15307142529fSAntoine Tenart 			ocelot_vlan_port_apply(ocelot_port->ocelot,
15317142529fSAntoine Tenart 					       ocelot_port);
1532a556c76aSAlexandre Belloni 		}
1533dc96ee37SAlexandre Belloni 		if (netif_is_lag_master(info->upper_dev)) {
1534dc96ee37SAlexandre Belloni 			if (info->linking)
1535dc96ee37SAlexandre Belloni 				err = ocelot_port_lag_join(ocelot_port,
1536dc96ee37SAlexandre Belloni 							   info->upper_dev);
1537dc96ee37SAlexandre Belloni 			else
1538dc96ee37SAlexandre Belloni 				ocelot_port_lag_leave(ocelot_port,
1539dc96ee37SAlexandre Belloni 						      info->upper_dev);
1540dc96ee37SAlexandre Belloni 		}
1541a556c76aSAlexandre Belloni 		break;
1542a556c76aSAlexandre Belloni 	default:
1543a556c76aSAlexandre Belloni 		break;
1544a556c76aSAlexandre Belloni 	}
1545a556c76aSAlexandre Belloni 
1546a556c76aSAlexandre Belloni 	return err;
1547a556c76aSAlexandre Belloni }
1548a556c76aSAlexandre Belloni 
1549a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused,
1550a556c76aSAlexandre Belloni 				  unsigned long event, void *ptr)
1551a556c76aSAlexandre Belloni {
1552a556c76aSAlexandre Belloni 	struct netdev_notifier_changeupper_info *info = ptr;
1553a556c76aSAlexandre Belloni 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
15542ac0e152SGeert Uytterhoeven 	int ret = 0;
1555a556c76aSAlexandre Belloni 
1556dc96ee37SAlexandre Belloni 	if (event == NETDEV_PRECHANGEUPPER &&
1557dc96ee37SAlexandre Belloni 	    netif_is_lag_master(info->upper_dev)) {
1558dc96ee37SAlexandre Belloni 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1559dc96ee37SAlexandre Belloni 		struct netlink_ext_ack *extack;
1560dc96ee37SAlexandre Belloni 
1561dc96ee37SAlexandre Belloni 		if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1562dc96ee37SAlexandre Belloni 			extack = netdev_notifier_info_to_extack(&info->info);
1563dc96ee37SAlexandre Belloni 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1564dc96ee37SAlexandre Belloni 
1565dc96ee37SAlexandre Belloni 			ret = -EINVAL;
1566dc96ee37SAlexandre Belloni 			goto notify;
1567dc96ee37SAlexandre Belloni 		}
1568dc96ee37SAlexandre Belloni 	}
1569dc96ee37SAlexandre Belloni 
1570a556c76aSAlexandre Belloni 	if (netif_is_lag_master(dev)) {
1571a556c76aSAlexandre Belloni 		struct net_device *slave;
1572a556c76aSAlexandre Belloni 		struct list_head *iter;
1573a556c76aSAlexandre Belloni 
1574a556c76aSAlexandre Belloni 		netdev_for_each_lower_dev(dev, slave, iter) {
1575a556c76aSAlexandre Belloni 			ret = ocelot_netdevice_port_event(slave, event, info);
1576a556c76aSAlexandre Belloni 			if (ret)
1577a556c76aSAlexandre Belloni 				goto notify;
1578a556c76aSAlexandre Belloni 		}
1579a556c76aSAlexandre Belloni 	} else {
1580a556c76aSAlexandre Belloni 		ret = ocelot_netdevice_port_event(dev, event, info);
1581a556c76aSAlexandre Belloni 	}
1582a556c76aSAlexandre Belloni 
1583a556c76aSAlexandre Belloni notify:
1584a556c76aSAlexandre Belloni 	return notifier_from_errno(ret);
1585a556c76aSAlexandre Belloni }
1586a556c76aSAlexandre Belloni 
1587a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = {
1588a556c76aSAlexandre Belloni 	.notifier_call = ocelot_netdevice_event,
1589a556c76aSAlexandre Belloni };
1590a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb);
1591a556c76aSAlexandre Belloni 
15920e332c85SPetr Machata static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
15930e332c85SPetr Machata 					   unsigned long event, void *ptr)
15940e332c85SPetr Machata {
15950e332c85SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
15960e332c85SPetr Machata 	int err;
15970e332c85SPetr Machata 
15980e332c85SPetr Machata 	switch (event) {
15990e332c85SPetr Machata 		/* Blocking events. */
16000e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
16010e332c85SPetr Machata 		err = switchdev_handle_port_obj_add(dev, ptr,
16020e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
16030e332c85SPetr Machata 						    ocelot_port_obj_add);
16040e332c85SPetr Machata 		return notifier_from_errno(err);
16050e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
16060e332c85SPetr Machata 		err = switchdev_handle_port_obj_del(dev, ptr,
16070e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
16080e332c85SPetr Machata 						    ocelot_port_obj_del);
16090e332c85SPetr Machata 		return notifier_from_errno(err);
16100e332c85SPetr Machata 	}
16110e332c85SPetr Machata 
16120e332c85SPetr Machata 	return NOTIFY_DONE;
16130e332c85SPetr Machata }
16140e332c85SPetr Machata 
16150e332c85SPetr Machata struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
16160e332c85SPetr Machata 	.notifier_call = ocelot_switchdev_blocking_event,
16170e332c85SPetr Machata };
16180e332c85SPetr Machata EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
16190e332c85SPetr Machata 
1620a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port,
1621a556c76aSAlexandre Belloni 		      void __iomem *regs,
1622a556c76aSAlexandre Belloni 		      struct phy_device *phy)
1623a556c76aSAlexandre Belloni {
1624a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port;
1625a556c76aSAlexandre Belloni 	struct net_device *dev;
1626a556c76aSAlexandre Belloni 	int err;
1627a556c76aSAlexandre Belloni 
1628a556c76aSAlexandre Belloni 	dev = alloc_etherdev(sizeof(struct ocelot_port));
1629a556c76aSAlexandre Belloni 	if (!dev)
1630a556c76aSAlexandre Belloni 		return -ENOMEM;
1631a556c76aSAlexandre Belloni 	SET_NETDEV_DEV(dev, ocelot->dev);
1632a556c76aSAlexandre Belloni 	ocelot_port = netdev_priv(dev);
1633a556c76aSAlexandre Belloni 	ocelot_port->dev = dev;
1634a556c76aSAlexandre Belloni 	ocelot_port->ocelot = ocelot;
1635a556c76aSAlexandre Belloni 	ocelot_port->regs = regs;
1636a556c76aSAlexandre Belloni 	ocelot_port->chip_port = port;
1637a556c76aSAlexandre Belloni 	ocelot_port->phy = phy;
1638a556c76aSAlexandre Belloni 	INIT_LIST_HEAD(&ocelot_port->mc);
1639a556c76aSAlexandre Belloni 	ocelot->ports[port] = ocelot_port;
1640a556c76aSAlexandre Belloni 
1641a556c76aSAlexandre Belloni 	dev->netdev_ops = &ocelot_port_netdev_ops;
1642a556c76aSAlexandre Belloni 	dev->ethtool_ops = &ocelot_ethtool_ops;
1643a556c76aSAlexandre Belloni 	dev->switchdev_ops = &ocelot_port_switchdev_ops;
1644a556c76aSAlexandre Belloni 
164560f8e67dSAntoine Tenart 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS;
16467142529fSAntoine Tenart 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
16477142529fSAntoine Tenart 
1648a556c76aSAlexandre Belloni 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1649a556c76aSAlexandre Belloni 	dev->dev_addr[ETH_ALEN - 1] += port;
1650a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
1651a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
1652a556c76aSAlexandre Belloni 
1653a556c76aSAlexandre Belloni 	err = register_netdev(dev);
1654a556c76aSAlexandre Belloni 	if (err) {
1655a556c76aSAlexandre Belloni 		dev_err(ocelot->dev, "register_netdev failed\n");
1656a556c76aSAlexandre Belloni 		goto err_register_netdev;
1657a556c76aSAlexandre Belloni 	}
1658a556c76aSAlexandre Belloni 
16597142529fSAntoine Tenart 	/* Basic L2 initialization */
16607142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, ocelot_port);
16617142529fSAntoine Tenart 
1662a556c76aSAlexandre Belloni 	return 0;
1663a556c76aSAlexandre Belloni 
1664a556c76aSAlexandre Belloni err_register_netdev:
1665a556c76aSAlexandre Belloni 	free_netdev(dev);
1666a556c76aSAlexandre Belloni 	return err;
1667a556c76aSAlexandre Belloni }
1668a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port);
1669a556c76aSAlexandre Belloni 
1670a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1671a556c76aSAlexandre Belloni {
1672a556c76aSAlexandre Belloni 	u32 port;
1673a556c76aSAlexandre Belloni 	int i, cpu = ocelot->num_phys_ports;
1674a556c76aSAlexandre Belloni 	char queue_name[32];
1675a556c76aSAlexandre Belloni 
1676dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1677dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
1678dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
1679dc96ee37SAlexandre Belloni 		return -ENOMEM;
1680dc96ee37SAlexandre Belloni 
1681a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1682a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1683a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1684a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1685a556c76aSAlexandre Belloni 		return -ENOMEM;
1686a556c76aSAlexandre Belloni 
1687a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
1688a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1689a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1690a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1691a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1692a556c76aSAlexandre Belloni 		return -ENOMEM;
1693a556c76aSAlexandre Belloni 
1694a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1695a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1696a556c76aSAlexandre Belloni 
1697a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1698a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1699a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1700a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1701a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1702a556c76aSAlexandre Belloni 	}
1703a556c76aSAlexandre Belloni 
1704a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1705a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1706a556c76aSAlexandre Belloni 
1707a556c76aSAlexandre Belloni 	/* Aggregation mode */
1708a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1709a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1710a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1711a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1712a556c76aSAlexandre Belloni 
1713a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1714a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1715a556c76aSAlexandre Belloni 	 */
1716a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1717a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1718a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1719a556c76aSAlexandre Belloni 
1720a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1721a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1722a556c76aSAlexandre Belloni 
1723a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1724a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1725a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1726a556c76aSAlexandre Belloni 
1727a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1728a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1729a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1730a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1731a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
1732a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1733a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1734a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1735a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1736a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1737a556c76aSAlexandre Belloni 
1738a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1739a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1740a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1741a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1742a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1743a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1744a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1745a556c76aSAlexandre Belloni 				 port);
1746a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1747a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1748a556c76aSAlexandre Belloni 	}
1749a556c76aSAlexandre Belloni 
1750a556c76aSAlexandre Belloni 	/* Configure and enable the CPU port. */
1751a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
1752a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1753a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1754a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1755a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, cpu);
1756a556c76aSAlexandre Belloni 
1757a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
1758a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
1759a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1760a556c76aSAlexandre Belloni 
1761a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1762a556c76aSAlexandre Belloni 	}
1763a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
1764a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1765a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
1766a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1767a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1768a556c76aSAlexandre Belloni 
1769a556c76aSAlexandre Belloni 	/* CPU port Injection/Extraction configuration */
1770a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
1771a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
1772a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
1773a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, cpu);
1774a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
1775a556c76aSAlexandre Belloni 			 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
1776a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1777a556c76aSAlexandre Belloni 	 * registers endianness.
1778a556c76aSAlexandre Belloni 	 */
1779a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1780a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1781a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1782a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1783a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1784a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1785a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1786a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1787a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1788a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1789a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1790a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1791a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1792a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1793a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1794a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1795a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1796a556c76aSAlexandre Belloni 
1797a556c76aSAlexandre Belloni 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats);
1798a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1799a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1800a556c76aSAlexandre Belloni 	return 0;
1801a556c76aSAlexandre Belloni }
1802a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1803a556c76aSAlexandre Belloni 
1804a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1805a556c76aSAlexandre Belloni {
1806a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1807a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
1808a556c76aSAlexandre Belloni }
1809a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1810a556c76aSAlexandre Belloni 
1811a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1812