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