xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 5fd82200d870a5dd3e509c98ef2041f580b2c0e1)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
7a556c76aSAlexandre Belloni #include <linux/etherdevice.h>
8a556c76aSAlexandre Belloni #include <linux/ethtool.h>
9a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
10a556c76aSAlexandre Belloni #include <linux/if_ether.h>
11a556c76aSAlexandre Belloni #include <linux/if_vlan.h>
12a556c76aSAlexandre Belloni #include <linux/interrupt.h>
13a556c76aSAlexandre Belloni #include <linux/kernel.h>
14a556c76aSAlexandre Belloni #include <linux/module.h>
15a556c76aSAlexandre Belloni #include <linux/netdevice.h>
16a556c76aSAlexandre Belloni #include <linux/phy.h>
17a556c76aSAlexandre Belloni #include <linux/skbuff.h>
18639c1b26SSteen Hegelund #include <linux/iopoll.h>
19a556c76aSAlexandre Belloni #include <net/arp.h>
20a556c76aSAlexandre Belloni #include <net/netevent.h>
21a556c76aSAlexandre Belloni #include <net/rtnetlink.h>
22a556c76aSAlexandre Belloni #include <net/switchdev.h>
23a556c76aSAlexandre Belloni 
24a556c76aSAlexandre Belloni #include "ocelot.h"
25b5962294SHoratiu Vultur #include "ocelot_ace.h"
26a556c76aSAlexandre Belloni 
27639c1b26SSteen Hegelund #define TABLE_UPDATE_SLEEP_US 10
28639c1b26SSteen Hegelund #define TABLE_UPDATE_TIMEOUT_US 100000
29639c1b26SSteen Hegelund 
30a556c76aSAlexandre Belloni /* MAC table entry types.
31a556c76aSAlexandre Belloni  * ENTRYTYPE_NORMAL is subject to aging.
32a556c76aSAlexandre Belloni  * ENTRYTYPE_LOCKED is not subject to aging.
33a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
34a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
35a556c76aSAlexandre Belloni  */
36a556c76aSAlexandre Belloni enum macaccess_entry_type {
37a556c76aSAlexandre Belloni 	ENTRYTYPE_NORMAL = 0,
38a556c76aSAlexandre Belloni 	ENTRYTYPE_LOCKED,
39a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv4,
40a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv6,
41a556c76aSAlexandre Belloni };
42a556c76aSAlexandre Belloni 
43a556c76aSAlexandre Belloni struct ocelot_mact_entry {
44a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
45a556c76aSAlexandre Belloni 	u16 vid;
46a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
47a556c76aSAlexandre Belloni };
48a556c76aSAlexandre Belloni 
49639c1b26SSteen Hegelund static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
50639c1b26SSteen Hegelund {
51639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
52639c1b26SSteen Hegelund }
53639c1b26SSteen Hegelund 
54a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
55a556c76aSAlexandre Belloni {
56639c1b26SSteen Hegelund 	u32 val;
57a556c76aSAlexandre Belloni 
58639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_mact_read_macaccess,
59639c1b26SSteen Hegelund 		ocelot, val,
60639c1b26SSteen Hegelund 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
61639c1b26SSteen Hegelund 		MACACCESS_CMD_IDLE,
62639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
63a556c76aSAlexandre Belloni }
64a556c76aSAlexandre Belloni 
65a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
66a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
67a556c76aSAlexandre Belloni 			       unsigned int vid)
68a556c76aSAlexandre Belloni {
69a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
70a556c76aSAlexandre Belloni 
71a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
72a556c76aSAlexandre Belloni 	 * understood by the hardware.
73a556c76aSAlexandre Belloni 	 */
74a556c76aSAlexandre Belloni 	mach |= vid    << 16;
75a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
76a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
77a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
78a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
79a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
80a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
81a556c76aSAlexandre Belloni 
82a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
83a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
84a556c76aSAlexandre Belloni 
85a556c76aSAlexandre Belloni }
86a556c76aSAlexandre Belloni 
87a556c76aSAlexandre Belloni static int ocelot_mact_learn(struct ocelot *ocelot, int port,
88a556c76aSAlexandre Belloni 			     const unsigned char mac[ETH_ALEN],
89a556c76aSAlexandre Belloni 			     unsigned int vid,
90a556c76aSAlexandre Belloni 			     enum macaccess_entry_type type)
91a556c76aSAlexandre Belloni {
92a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
93a556c76aSAlexandre Belloni 
94a556c76aSAlexandre Belloni 	/* Issue a write command */
95a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
96a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
97a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
98a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
99a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
100a556c76aSAlexandre Belloni 
101a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
102a556c76aSAlexandre Belloni }
103a556c76aSAlexandre Belloni 
104a556c76aSAlexandre Belloni static int ocelot_mact_forget(struct ocelot *ocelot,
105a556c76aSAlexandre Belloni 			      const unsigned char mac[ETH_ALEN],
106a556c76aSAlexandre Belloni 			      unsigned int vid)
107a556c76aSAlexandre Belloni {
108a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
109a556c76aSAlexandre Belloni 
110a556c76aSAlexandre Belloni 	/* Issue a forget command */
111a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
112a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
113a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
114a556c76aSAlexandre Belloni 
115a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
116a556c76aSAlexandre Belloni }
117a556c76aSAlexandre Belloni 
118a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
119a556c76aSAlexandre Belloni {
120a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
121a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
122a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
123a556c76aSAlexandre Belloni 	 */
124a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
125a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
126a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
127a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
128a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
129a556c76aSAlexandre Belloni 
130a556c76aSAlexandre Belloni 	/* Clear the MAC table */
131a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
132a556c76aSAlexandre Belloni }
133a556c76aSAlexandre Belloni 
134f270dbfaSVladimir Oltean static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
135b5962294SHoratiu Vultur {
136b5962294SHoratiu Vultur 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
137b5962294SHoratiu Vultur 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
138f270dbfaSVladimir Oltean 			 ANA_PORT_VCAP_S2_CFG, port);
139b5962294SHoratiu Vultur }
140b5962294SHoratiu Vultur 
141639c1b26SSteen Hegelund static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
142639c1b26SSteen Hegelund {
143639c1b26SSteen Hegelund 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
144639c1b26SSteen Hegelund }
145639c1b26SSteen Hegelund 
146a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
147a556c76aSAlexandre Belloni {
148639c1b26SSteen Hegelund 	u32 val;
149a556c76aSAlexandre Belloni 
150639c1b26SSteen Hegelund 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
151639c1b26SSteen Hegelund 		ocelot,
152639c1b26SSteen Hegelund 		val,
153639c1b26SSteen Hegelund 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
154639c1b26SSteen Hegelund 		ANA_TABLES_VLANACCESS_CMD_IDLE,
155639c1b26SSteen Hegelund 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
156a556c76aSAlexandre Belloni }
157a556c76aSAlexandre Belloni 
1587142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
1597142529fSAntoine Tenart {
1607142529fSAntoine Tenart 	/* Select the VID to configure */
1617142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
1627142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
1637142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
1647142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
1657142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
1667142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
1677142529fSAntoine Tenart 
1687142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
1697142529fSAntoine Tenart }
1707142529fSAntoine Tenart 
171f270dbfaSVladimir Oltean static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
1727142529fSAntoine Tenart 			     netdev_features_t features)
1737142529fSAntoine Tenart {
1747142529fSAntoine Tenart 	u32 val;
1757142529fSAntoine Tenart 
1767142529fSAntoine Tenart 	/* Filtering */
1777142529fSAntoine Tenart 	val = ocelot_read(ocelot, ANA_VLANMASK);
1787142529fSAntoine Tenart 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
179f270dbfaSVladimir Oltean 		val |= BIT(port);
1807142529fSAntoine Tenart 	else
181f270dbfaSVladimir Oltean 		val &= ~BIT(port);
1827142529fSAntoine Tenart 	ocelot_write(ocelot, val, ANA_VLANMASK);
1837142529fSAntoine Tenart }
1847142529fSAntoine Tenart 
18597bb69e1SVladimir Oltean static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
18697bb69e1SVladimir Oltean 				       u16 vid)
18797bb69e1SVladimir Oltean {
18897bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
18987b0f983SVladimir Oltean 	u32 val = 0;
19097bb69e1SVladimir Oltean 
19197bb69e1SVladimir Oltean 	if (ocelot_port->vid != vid) {
19297bb69e1SVladimir Oltean 		/* Always permit deleting the native VLAN (vid = 0) */
19397bb69e1SVladimir Oltean 		if (ocelot_port->vid && vid) {
19497bb69e1SVladimir Oltean 			dev_err(ocelot->dev,
19597bb69e1SVladimir Oltean 				"Port already has a native VLAN: %d\n",
19697bb69e1SVladimir Oltean 				ocelot_port->vid);
19797bb69e1SVladimir Oltean 			return -EBUSY;
19897bb69e1SVladimir Oltean 		}
19997bb69e1SVladimir Oltean 		ocelot_port->vid = vid;
20097bb69e1SVladimir Oltean 	}
20197bb69e1SVladimir Oltean 
20297bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
2037142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
20497bb69e1SVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
20597bb69e1SVladimir Oltean 
20687b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware && !ocelot_port->vid)
20787b0f983SVladimir Oltean 		/* If port is vlan-aware and tagged, drop untagged and priority
20887b0f983SVladimir Oltean 		 * tagged frames.
20987b0f983SVladimir Oltean 		 */
21087b0f983SVladimir Oltean 		val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
21187b0f983SVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
21287b0f983SVladimir Oltean 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
21387b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
21487b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
21587b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
21687b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
21787b0f983SVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
21887b0f983SVladimir Oltean 
21987b0f983SVladimir Oltean 	if (ocelot_port->vlan_aware) {
22087b0f983SVladimir Oltean 		if (ocelot_port->vid)
22187b0f983SVladimir Oltean 			/* Tag all frames except when VID == DEFAULT_VLAN */
22287b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(1);
22387b0f983SVladimir Oltean 		else
22487b0f983SVladimir Oltean 			/* Tag all frames */
22587b0f983SVladimir Oltean 			val = REW_TAG_CFG_TAG_CFG(3);
22687b0f983SVladimir Oltean 	} else {
22787b0f983SVladimir Oltean 		/* Port tagging disabled. */
22887b0f983SVladimir Oltean 		val = REW_TAG_CFG_TAG_CFG(0);
22987b0f983SVladimir Oltean 	}
23087b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
23187b0f983SVladimir Oltean 		       REW_TAG_CFG_TAG_CFG_M,
23287b0f983SVladimir Oltean 		       REW_TAG_CFG, port);
23387b0f983SVladimir Oltean 
23497bb69e1SVladimir Oltean 	return 0;
23597bb69e1SVladimir Oltean }
23697bb69e1SVladimir Oltean 
23787b0f983SVladimir Oltean void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
23887b0f983SVladimir Oltean 				bool vlan_aware)
23987b0f983SVladimir Oltean {
24087b0f983SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
24187b0f983SVladimir Oltean 	u32 val;
24287b0f983SVladimir Oltean 
24387b0f983SVladimir Oltean 	ocelot_port->vlan_aware = vlan_aware;
24487b0f983SVladimir Oltean 
24587b0f983SVladimir Oltean 	if (vlan_aware)
24687b0f983SVladimir Oltean 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
24787b0f983SVladimir Oltean 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
24887b0f983SVladimir Oltean 	else
24987b0f983SVladimir Oltean 		val = 0;
25087b0f983SVladimir Oltean 	ocelot_rmw_gix(ocelot, val,
25187b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
25287b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
25387b0f983SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
25487b0f983SVladimir Oltean 
25587b0f983SVladimir Oltean 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid);
25687b0f983SVladimir Oltean }
25787b0f983SVladimir Oltean EXPORT_SYMBOL(ocelot_port_vlan_filtering);
25887b0f983SVladimir Oltean 
25997bb69e1SVladimir Oltean /* Default vlan to clasify for untagged frames (may be zero) */
26097bb69e1SVladimir Oltean static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
26197bb69e1SVladimir Oltean {
26297bb69e1SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
26397bb69e1SVladimir Oltean 
26497bb69e1SVladimir Oltean 	ocelot_rmw_gix(ocelot,
26597bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
26697bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
26797bb69e1SVladimir Oltean 		       ANA_PORT_VLAN_CFG, port);
26897bb69e1SVladimir Oltean 
26997bb69e1SVladimir Oltean 	ocelot_port->pvid = pvid;
2707142529fSAntoine Tenart }
2717142529fSAntoine Tenart 
2725e256365SVladimir Oltean int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
2737142529fSAntoine Tenart 		    bool untagged)
2747142529fSAntoine Tenart {
2757142529fSAntoine Tenart 	int ret;
2767142529fSAntoine Tenart 
2777142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
27897bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] |= BIT(port);
2797142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
2807142529fSAntoine Tenart 	if (ret)
2817142529fSAntoine Tenart 		return ret;
2827142529fSAntoine Tenart 
2837142529fSAntoine Tenart 	/* Default ingress vlan classification */
2847142529fSAntoine Tenart 	if (pvid)
28597bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, vid);
2867142529fSAntoine Tenart 
2877142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
28897bb69e1SVladimir Oltean 	if (untagged) {
28997bb69e1SVladimir Oltean 		ret = ocelot_port_set_native_vlan(ocelot, port, vid);
29097bb69e1SVladimir Oltean 		if (ret)
29197bb69e1SVladimir Oltean 			return ret;
292b9cd75e6SVladimir Oltean 	}
2937142529fSAntoine Tenart 
2947142529fSAntoine Tenart 	return 0;
2957142529fSAntoine Tenart }
2965e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_add);
2977142529fSAntoine Tenart 
2989855934cSVladimir Oltean static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
2999855934cSVladimir Oltean 			       bool untagged)
3007142529fSAntoine Tenart {
301004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
302004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
30397bb69e1SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
304004d44f6SVladimir Oltean 	int port = priv->chip_port;
3057142529fSAntoine Tenart 	int ret;
3067142529fSAntoine Tenart 
3079855934cSVladimir Oltean 	ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
3089855934cSVladimir Oltean 	if (ret)
3099855934cSVladimir Oltean 		return ret;
3107142529fSAntoine Tenart 
3119855934cSVladimir Oltean 	/* Add the port MAC address to with the right VLAN information */
3129855934cSVladimir Oltean 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
3139855934cSVladimir Oltean 			  ENTRYTYPE_LOCKED);
3149855934cSVladimir Oltean 
3159855934cSVladimir Oltean 	return 0;
3169855934cSVladimir Oltean }
3179855934cSVladimir Oltean 
3185e256365SVladimir Oltean int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
3199855934cSVladimir Oltean {
3209855934cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
3219855934cSVladimir Oltean 	int ret;
3227142529fSAntoine Tenart 
3237142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
32497bb69e1SVladimir Oltean 	ocelot->vlan_mask[vid] &= ~BIT(port);
3257142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3267142529fSAntoine Tenart 	if (ret)
3277142529fSAntoine Tenart 		return ret;
3287142529fSAntoine Tenart 
3297142529fSAntoine Tenart 	/* Ingress */
33097bb69e1SVladimir Oltean 	if (ocelot_port->pvid == vid)
33197bb69e1SVladimir Oltean 		ocelot_port_set_pvid(ocelot, port, 0);
3327142529fSAntoine Tenart 
3337142529fSAntoine Tenart 	/* Egress */
33497bb69e1SVladimir Oltean 	if (ocelot_port->vid == vid)
33597bb69e1SVladimir Oltean 		ocelot_port_set_native_vlan(ocelot, port, 0);
3367142529fSAntoine Tenart 
3377142529fSAntoine Tenart 	return 0;
3387142529fSAntoine Tenart }
3395e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_vlan_del);
3407142529fSAntoine Tenart 
3419855934cSVladimir Oltean static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
3429855934cSVladimir Oltean {
343004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
344004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
345004d44f6SVladimir Oltean 	int port = priv->chip_port;
3469855934cSVladimir Oltean 	int ret;
3479855934cSVladimir Oltean 
3489855934cSVladimir Oltean 	/* 8021q removes VID 0 on module unload for all interfaces
3499855934cSVladimir Oltean 	 * with VLAN filtering feature. We need to keep it to receive
3509855934cSVladimir Oltean 	 * untagged traffic.
3519855934cSVladimir Oltean 	 */
3529855934cSVladimir Oltean 	if (vid == 0)
3539855934cSVladimir Oltean 		return 0;
3549855934cSVladimir Oltean 
3559855934cSVladimir Oltean 	ret = ocelot_vlan_del(ocelot, port, vid);
3569855934cSVladimir Oltean 	if (ret)
3579855934cSVladimir Oltean 		return ret;
3589855934cSVladimir Oltean 
3599855934cSVladimir Oltean 	/* Del the port MAC address to with the right VLAN information */
3609855934cSVladimir Oltean 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
3619855934cSVladimir Oltean 
3629855934cSVladimir Oltean 	return 0;
3639855934cSVladimir Oltean }
3649855934cSVladimir Oltean 
365a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
366a556c76aSAlexandre Belloni {
3677142529fSAntoine Tenart 	u16 port, vid;
3687142529fSAntoine Tenart 
369a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
370a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
371a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
372a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
3737142529fSAntoine Tenart 
3747142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
3757142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
3767142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
3777142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
3787142529fSAntoine Tenart 	}
3797142529fSAntoine Tenart 
3807142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
3817142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
3827142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
3837142529fSAntoine Tenart 	 */
3847142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
3857142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
3867142529fSAntoine Tenart 
3877142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
3887142529fSAntoine Tenart 	 * default.
3897142529fSAntoine Tenart 	 */
390714d0ffaSVladimir Oltean 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
391714d0ffaSVladimir Oltean 		     ANA_VLANMASK);
3927142529fSAntoine Tenart 
3937142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
3947142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
3957142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
3967142529fSAntoine Tenart 	}
397a556c76aSAlexandre Belloni }
398a556c76aSAlexandre Belloni 
399a556c76aSAlexandre Belloni /* Watermark encode
400a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
401a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
402a556c76aSAlexandre Belloni  */
403a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
404a556c76aSAlexandre Belloni {
405a556c76aSAlexandre Belloni 	if (value >= BIT(8))
406a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
407a556c76aSAlexandre Belloni 
408a556c76aSAlexandre Belloni 	return value;
409a556c76aSAlexandre Belloni }
410a556c76aSAlexandre Belloni 
4115e256365SVladimir Oltean void ocelot_adjust_link(struct ocelot *ocelot, int port,
41226f4dbabSVladimir Oltean 			struct phy_device *phydev)
413a556c76aSAlexandre Belloni {
41426f4dbabSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
4155bc9d2e6SVladimir Oltean 	int speed, mode = 0;
416a556c76aSAlexandre Belloni 
41726f4dbabSVladimir Oltean 	switch (phydev->speed) {
418a556c76aSAlexandre Belloni 	case SPEED_10:
419a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
420a556c76aSAlexandre Belloni 		break;
421a556c76aSAlexandre Belloni 	case SPEED_100:
422a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
423a556c76aSAlexandre Belloni 		break;
424a556c76aSAlexandre Belloni 	case SPEED_1000:
425a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
426a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
427a556c76aSAlexandre Belloni 		break;
428a556c76aSAlexandre Belloni 	case SPEED_2500:
429a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
430a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
431a556c76aSAlexandre Belloni 		break;
432a556c76aSAlexandre Belloni 	default:
43326f4dbabSVladimir Oltean 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
43426f4dbabSVladimir Oltean 			port, phydev->speed);
435a556c76aSAlexandre Belloni 		return;
436a556c76aSAlexandre Belloni 	}
437a556c76aSAlexandre Belloni 
43826f4dbabSVladimir Oltean 	phy_print_status(phydev);
439a556c76aSAlexandre Belloni 
44026f4dbabSVladimir Oltean 	if (!phydev->link)
441a556c76aSAlexandre Belloni 		return;
442a556c76aSAlexandre Belloni 
443a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
444004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
445a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
446a556c76aSAlexandre Belloni 
4471ba8f656SVladimir Oltean 	/* Disable HDX fast control */
4481ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
4491ba8f656SVladimir Oltean 			   DEV_PORT_MISC);
4501ba8f656SVladimir Oltean 
4511ba8f656SVladimir Oltean 	/* SGMII only for now */
4521ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
4531ba8f656SVladimir Oltean 			   PCS1G_MODE_CFG);
4541ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
4551ba8f656SVladimir Oltean 
4561ba8f656SVladimir Oltean 	/* Enable PCS */
4571ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
4581ba8f656SVladimir Oltean 
4591ba8f656SVladimir Oltean 	/* No aneg on SGMII */
4601ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
4611ba8f656SVladimir Oltean 
4621ba8f656SVladimir Oltean 	/* No loopback */
4631ba8f656SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
464a556c76aSAlexandre Belloni 
465a556c76aSAlexandre Belloni 	/* Enable MAC module */
466004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
467a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
468a556c76aSAlexandre Belloni 
469a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
470a556c76aSAlexandre Belloni 	 * reset */
471004d44f6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
472a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
473a556c76aSAlexandre Belloni 
474a556c76aSAlexandre Belloni 	/* No PFC */
475a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
476004d44f6SVladimir Oltean 			 ANA_PFC_PFC_CFG, port);
477a556c76aSAlexandre Belloni 
478a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
479a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
480a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
481a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
482004d44f6SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, port);
483a556c76aSAlexandre Belloni 
484a556c76aSAlexandre Belloni 	/* Flow control */
485a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
486a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
487a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
488a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
489a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
490004d44f6SVladimir Oltean 			 SYS_MAC_FC_CFG, port);
491004d44f6SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
492a556c76aSAlexandre Belloni }
4935e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_adjust_link);
494a556c76aSAlexandre Belloni 
49526f4dbabSVladimir Oltean static void ocelot_port_adjust_link(struct net_device *dev)
49626f4dbabSVladimir Oltean {
49726f4dbabSVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
49826f4dbabSVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
49926f4dbabSVladimir Oltean 	int port = priv->chip_port;
50026f4dbabSVladimir Oltean 
50126f4dbabSVladimir Oltean 	ocelot_adjust_link(ocelot, port, dev->phydev);
50226f4dbabSVladimir Oltean }
50326f4dbabSVladimir Oltean 
5045e256365SVladimir Oltean void ocelot_port_enable(struct ocelot *ocelot, int port,
505889b8950SVladimir Oltean 			struct phy_device *phy)
506a556c76aSAlexandre Belloni {
507a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
508a556c76aSAlexandre Belloni 	 * MAC addresses.
509a556c76aSAlexandre Belloni 	 */
510a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
511a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
512004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
513004d44f6SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
514889b8950SVladimir Oltean }
5155e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_enable);
516889b8950SVladimir Oltean 
517889b8950SVladimir Oltean static int ocelot_port_open(struct net_device *dev)
518889b8950SVladimir Oltean {
519889b8950SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
520ee50d07cSVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
521ee50d07cSVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
522889b8950SVladimir Oltean 	int port = priv->chip_port;
523889b8950SVladimir Oltean 	int err;
524a556c76aSAlexandre Belloni 
525004d44f6SVladimir Oltean 	if (priv->serdes) {
526004d44f6SVladimir Oltean 		err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
527ee50d07cSVladimir Oltean 				       ocelot_port->phy_mode);
52871e32a20SQuentin Schulz 		if (err) {
52971e32a20SQuentin Schulz 			netdev_err(dev, "Could not set mode of SerDes\n");
53071e32a20SQuentin Schulz 			return err;
53171e32a20SQuentin Schulz 		}
53271e32a20SQuentin Schulz 	}
53371e32a20SQuentin Schulz 
534004d44f6SVladimir Oltean 	err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
535ee50d07cSVladimir Oltean 				 ocelot_port->phy_mode);
536a556c76aSAlexandre Belloni 	if (err) {
537a556c76aSAlexandre Belloni 		netdev_err(dev, "Could not attach to PHY\n");
538a556c76aSAlexandre Belloni 		return err;
539a556c76aSAlexandre Belloni 	}
540a556c76aSAlexandre Belloni 
541004d44f6SVladimir Oltean 	dev->phydev = priv->phy;
542a556c76aSAlexandre Belloni 
543004d44f6SVladimir Oltean 	phy_attached_info(priv->phy);
544004d44f6SVladimir Oltean 	phy_start(priv->phy);
545889b8950SVladimir Oltean 
546889b8950SVladimir Oltean 	ocelot_port_enable(ocelot, port, priv->phy);
547889b8950SVladimir Oltean 
548a556c76aSAlexandre Belloni 	return 0;
549a556c76aSAlexandre Belloni }
550a556c76aSAlexandre Belloni 
5515e256365SVladimir Oltean void ocelot_port_disable(struct ocelot *ocelot, int port)
552889b8950SVladimir Oltean {
553889b8950SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
554889b8950SVladimir Oltean 
555889b8950SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
556889b8950SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
557889b8950SVladimir Oltean 		       QSYS_SWITCH_PORT_MODE, port);
558889b8950SVladimir Oltean }
5595e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_disable);
560889b8950SVladimir Oltean 
561a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev)
562a556c76aSAlexandre Belloni {
563004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
564889b8950SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
565889b8950SVladimir Oltean 	int port = priv->chip_port;
566a556c76aSAlexandre Belloni 
567004d44f6SVladimir Oltean 	phy_disconnect(priv->phy);
568a556c76aSAlexandre Belloni 
569a556c76aSAlexandre Belloni 	dev->phydev = NULL;
570a556c76aSAlexandre Belloni 
571889b8950SVladimir Oltean 	ocelot_port_disable(ocelot, port);
572889b8950SVladimir Oltean 
573a556c76aSAlexandre Belloni 	return 0;
574a556c76aSAlexandre Belloni }
575a556c76aSAlexandre Belloni 
576a556c76aSAlexandre Belloni /* Generate the IFH for frame injection
577a556c76aSAlexandre Belloni  *
578a556c76aSAlexandre Belloni  * The IFH is a 128bit-value
579a556c76aSAlexandre Belloni  * bit 127: bypass the analyzer processing
580a556c76aSAlexandre Belloni  * bit 56-67: destination mask
581a556c76aSAlexandre Belloni  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
582a556c76aSAlexandre Belloni  * bit 20-27: cpu extraction queue mask
583a556c76aSAlexandre Belloni  * bit 16: tag type 0: C-tag, 1: S-tag
584a556c76aSAlexandre Belloni  * bit 0-11: VID
585a556c76aSAlexandre Belloni  */
586a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
587a556c76aSAlexandre Belloni {
5884e3b0468SAntoine Tenart 	ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
58908d02364SAntoine Tenart 	ifh[1] = (0xf00 & info->port) >> 8;
590a556c76aSAlexandre Belloni 	ifh[2] = (0xff & info->port) << 24;
59108d02364SAntoine Tenart 	ifh[3] = (info->tag_type << 16) | info->vid;
592a556c76aSAlexandre Belloni 
593a556c76aSAlexandre Belloni 	return 0;
594a556c76aSAlexandre Belloni }
595a556c76aSAlexandre Belloni 
596400928bfSYangbo Lu int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
597400928bfSYangbo Lu 				 struct sk_buff *skb)
598400928bfSYangbo Lu {
599400928bfSYangbo Lu 	struct skb_shared_info *shinfo = skb_shinfo(skb);
600400928bfSYangbo Lu 	struct ocelot *ocelot = ocelot_port->ocelot;
601400928bfSYangbo Lu 
602400928bfSYangbo Lu 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
603400928bfSYangbo Lu 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
604400928bfSYangbo Lu 		shinfo->tx_flags |= SKBTX_IN_PROGRESS;
605b049da13SYangbo Lu 		/* Store timestamp ID in cb[0] of sk_buff */
606b049da13SYangbo Lu 		skb->cb[0] = ocelot_port->ts_id % 4;
607b049da13SYangbo Lu 		skb_queue_tail(&ocelot_port->tx_skbs, skb);
608400928bfSYangbo Lu 		return 0;
609400928bfSYangbo Lu 	}
610400928bfSYangbo Lu 	return -ENODATA;
611400928bfSYangbo Lu }
612400928bfSYangbo Lu EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
613400928bfSYangbo Lu 
614a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
615a556c76aSAlexandre Belloni {
616004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
6174e3b0468SAntoine Tenart 	struct skb_shared_info *shinfo = skb_shinfo(skb);
618004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
619004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
620f24711fdSVladimir Oltean 	u32 val, ifh[OCELOT_TAG_LEN / 4];
621a556c76aSAlexandre Belloni 	struct frame_info info = {};
622a556c76aSAlexandre Belloni 	u8 grp = 0; /* Send everything on CPU group 0 */
623a556c76aSAlexandre Belloni 	unsigned int i, count, last;
624004d44f6SVladimir Oltean 	int port = priv->chip_port;
625a556c76aSAlexandre Belloni 
626a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, QS_INJ_STATUS);
627a556c76aSAlexandre Belloni 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
628a556c76aSAlexandre Belloni 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
629a556c76aSAlexandre Belloni 		return NETDEV_TX_BUSY;
630a556c76aSAlexandre Belloni 
631a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
632a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
633a556c76aSAlexandre Belloni 
634004d44f6SVladimir Oltean 	info.port = BIT(port);
63508d02364SAntoine Tenart 	info.tag_type = IFH_TAG_TYPE_C;
63608d02364SAntoine Tenart 	info.vid = skb_vlan_tag_get(skb);
6374e3b0468SAntoine Tenart 
6384e3b0468SAntoine Tenart 	/* Check if timestamping is needed */
6394e3b0468SAntoine Tenart 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
640004d44f6SVladimir Oltean 		info.rew_op = ocelot_port->ptp_cmd;
641004d44f6SVladimir Oltean 		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
642004d44f6SVladimir Oltean 			info.rew_op |= (ocelot_port->ts_id  % 4) << 3;
6434e3b0468SAntoine Tenart 	}
6444e3b0468SAntoine Tenart 
645a556c76aSAlexandre Belloni 	ocelot_gen_ifh(ifh, &info);
646a556c76aSAlexandre Belloni 
647f24711fdSVladimir Oltean 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
648c2cd650bSAntoine Tenart 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
649c2cd650bSAntoine Tenart 				 QS_INJ_WR, grp);
650a556c76aSAlexandre Belloni 
651a556c76aSAlexandre Belloni 	count = (skb->len + 3) / 4;
652a556c76aSAlexandre Belloni 	last = skb->len % 4;
653a556c76aSAlexandre Belloni 	for (i = 0; i < count; i++) {
654a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
655a556c76aSAlexandre Belloni 	}
656a556c76aSAlexandre Belloni 
657a556c76aSAlexandre Belloni 	/* Add padding */
658a556c76aSAlexandre Belloni 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
659a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
660a556c76aSAlexandre Belloni 		i++;
661a556c76aSAlexandre Belloni 	}
662a556c76aSAlexandre Belloni 
663a556c76aSAlexandre Belloni 	/* Indicate EOF and valid bytes in last word */
664a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
665a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
666a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_EOF,
667a556c76aSAlexandre Belloni 			 QS_INJ_CTRL, grp);
668a556c76aSAlexandre Belloni 
669a556c76aSAlexandre Belloni 	/* Add dummy CRC */
670a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
671a556c76aSAlexandre Belloni 	skb_tx_timestamp(skb);
672a556c76aSAlexandre Belloni 
673a556c76aSAlexandre Belloni 	dev->stats.tx_packets++;
674a556c76aSAlexandre Belloni 	dev->stats.tx_bytes += skb->len;
6754e3b0468SAntoine Tenart 
676400928bfSYangbo Lu 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, skb)) {
677004d44f6SVladimir Oltean 		ocelot_port->ts_id++;
678a556c76aSAlexandre Belloni 		return NETDEV_TX_OK;
679a556c76aSAlexandre Belloni 	}
680a556c76aSAlexandre Belloni 
6814e3b0468SAntoine Tenart 	dev_kfree_skb_any(skb);
6824e3b0468SAntoine Tenart 	return NETDEV_TX_OK;
6834e3b0468SAntoine Tenart }
6844e3b0468SAntoine Tenart 
685e23a7b3eSYangbo Lu static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
686e23a7b3eSYangbo Lu 				   struct timespec64 *ts)
6874e3b0468SAntoine Tenart {
6884e3b0468SAntoine Tenart 	unsigned long flags;
6894e3b0468SAntoine Tenart 	u32 val;
6904e3b0468SAntoine Tenart 
6914e3b0468SAntoine Tenart 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
6924e3b0468SAntoine Tenart 
6934e3b0468SAntoine Tenart 	/* Read current PTP time to get seconds */
6944e3b0468SAntoine Tenart 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
6954e3b0468SAntoine Tenart 
6964e3b0468SAntoine Tenart 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
6974e3b0468SAntoine Tenart 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
6984e3b0468SAntoine Tenart 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
6994e3b0468SAntoine Tenart 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
7004e3b0468SAntoine Tenart 
7014e3b0468SAntoine Tenart 	/* Read packet HW timestamp from FIFO */
7024e3b0468SAntoine Tenart 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
7034e3b0468SAntoine Tenart 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
7044e3b0468SAntoine Tenart 
7054e3b0468SAntoine Tenart 	/* Sec has incremented since the ts was registered */
7064e3b0468SAntoine Tenart 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
7074e3b0468SAntoine Tenart 		ts->tv_sec--;
7084e3b0468SAntoine Tenart 
7094e3b0468SAntoine Tenart 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
7104e3b0468SAntoine Tenart }
711e23a7b3eSYangbo Lu 
712e23a7b3eSYangbo Lu void ocelot_get_txtstamp(struct ocelot *ocelot)
713e23a7b3eSYangbo Lu {
714e23a7b3eSYangbo Lu 	int budget = OCELOT_PTP_QUEUE_SZ;
715e23a7b3eSYangbo Lu 
716e23a7b3eSYangbo Lu 	while (budget--) {
717b049da13SYangbo Lu 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
718e23a7b3eSYangbo Lu 		struct skb_shared_hwtstamps shhwtstamps;
719e23a7b3eSYangbo Lu 		struct ocelot_port *port;
720e23a7b3eSYangbo Lu 		struct timespec64 ts;
721b049da13SYangbo Lu 		unsigned long flags;
722e23a7b3eSYangbo Lu 		u32 val, id, txport;
723e23a7b3eSYangbo Lu 
724e23a7b3eSYangbo Lu 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
725e23a7b3eSYangbo Lu 
726e23a7b3eSYangbo Lu 		/* Check if a timestamp can be retrieved */
727e23a7b3eSYangbo Lu 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
728e23a7b3eSYangbo Lu 			break;
729e23a7b3eSYangbo Lu 
730e23a7b3eSYangbo Lu 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
731e23a7b3eSYangbo Lu 
732e23a7b3eSYangbo Lu 		/* Retrieve the ts ID and Tx port */
733e23a7b3eSYangbo Lu 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
734e23a7b3eSYangbo Lu 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
735e23a7b3eSYangbo Lu 
736e23a7b3eSYangbo Lu 		/* Retrieve its associated skb */
737e23a7b3eSYangbo Lu 		port = ocelot->ports[txport];
738e23a7b3eSYangbo Lu 
739b049da13SYangbo Lu 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
740b049da13SYangbo Lu 
741b049da13SYangbo Lu 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
742b049da13SYangbo Lu 			if (skb->cb[0] != id)
743e23a7b3eSYangbo Lu 				continue;
744b049da13SYangbo Lu 			__skb_unlink(skb, &port->tx_skbs);
745b049da13SYangbo Lu 			skb_match = skb;
746fc62c094SYangbo Lu 			break;
747e23a7b3eSYangbo Lu 		}
748e23a7b3eSYangbo Lu 
749b049da13SYangbo Lu 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
750b049da13SYangbo Lu 
751*5fd82200Slaurent brando 		/* Get the h/w timestamp */
752*5fd82200Slaurent brando 		ocelot_get_hwtimestamp(ocelot, &ts);
753e23a7b3eSYangbo Lu 
754b049da13SYangbo Lu 		if (unlikely(!skb_match))
755e23a7b3eSYangbo Lu 			continue;
756e23a7b3eSYangbo Lu 
757e23a7b3eSYangbo Lu 		/* Set the timestamp into the skb */
758e23a7b3eSYangbo Lu 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
759e23a7b3eSYangbo Lu 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
760b049da13SYangbo Lu 		skb_tstamp_tx(skb_match, &shhwtstamps);
761e23a7b3eSYangbo Lu 
762b049da13SYangbo Lu 		dev_kfree_skb_any(skb_match);
763*5fd82200Slaurent brando 
764*5fd82200Slaurent brando 		/* Next ts */
765*5fd82200Slaurent brando 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
766e23a7b3eSYangbo Lu 	}
767e23a7b3eSYangbo Lu }
768e23a7b3eSYangbo Lu EXPORT_SYMBOL(ocelot_get_txtstamp);
7694e3b0468SAntoine Tenart 
77040a1578dSClaudiu Manoil static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
771a556c76aSAlexandre Belloni {
772004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
773004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
774004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
775a556c76aSAlexandre Belloni 
776004d44f6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, ocelot_port->pvid);
777a556c76aSAlexandre Belloni }
778a556c76aSAlexandre Belloni 
77940a1578dSClaudiu Manoil static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
780a556c76aSAlexandre Belloni {
781004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
782004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
783004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
784a556c76aSAlexandre Belloni 
785004d44f6SVladimir Oltean 	return ocelot_mact_learn(ocelot, PGID_CPU, addr, ocelot_port->pvid,
786a556c76aSAlexandre Belloni 				 ENTRYTYPE_LOCKED);
787a556c76aSAlexandre Belloni }
788a556c76aSAlexandre Belloni 
789a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev)
790a556c76aSAlexandre Belloni {
791004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
792004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
793a556c76aSAlexandre Belloni 	u32 val;
794004d44f6SVladimir Oltean 	int i;
795a556c76aSAlexandre Belloni 
796a556c76aSAlexandre Belloni 	/* This doesn't handle promiscuous mode because the bridge core is
797a556c76aSAlexandre Belloni 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
798a556c76aSAlexandre Belloni 	 * forwarded to the CPU port.
799a556c76aSAlexandre Belloni 	 */
800a556c76aSAlexandre Belloni 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
801a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
802a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
803a556c76aSAlexandre Belloni 
80440a1578dSClaudiu Manoil 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
805a556c76aSAlexandre Belloni }
806a556c76aSAlexandre Belloni 
807a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev,
808a556c76aSAlexandre Belloni 					  char *buf, size_t len)
809a556c76aSAlexandre Belloni {
810004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
811004d44f6SVladimir Oltean 	int port = priv->chip_port;
812a556c76aSAlexandre Belloni 	int ret;
813a556c76aSAlexandre Belloni 
814004d44f6SVladimir Oltean 	ret = snprintf(buf, len, "p%d", port);
815a556c76aSAlexandre Belloni 	if (ret >= len)
816a556c76aSAlexandre Belloni 		return -EINVAL;
817a556c76aSAlexandre Belloni 
818a556c76aSAlexandre Belloni 	return 0;
819a556c76aSAlexandre Belloni }
820a556c76aSAlexandre Belloni 
821a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
822a556c76aSAlexandre Belloni {
823004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
824004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
825004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
826a556c76aSAlexandre Belloni 	const struct sockaddr *addr = p;
827a556c76aSAlexandre Belloni 
828a556c76aSAlexandre Belloni 	/* Learn the new net device MAC address in the mac table. */
829004d44f6SVladimir Oltean 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, ocelot_port->pvid,
830a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
831a556c76aSAlexandre Belloni 	/* Then forget the previous one. */
832004d44f6SVladimir Oltean 	ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid);
833a556c76aSAlexandre Belloni 
834a556c76aSAlexandre Belloni 	ether_addr_copy(dev->dev_addr, addr->sa_data);
835a556c76aSAlexandre Belloni 	return 0;
836a556c76aSAlexandre Belloni }
837a556c76aSAlexandre Belloni 
838a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev,
839a556c76aSAlexandre Belloni 			       struct rtnl_link_stats64 *stats)
840a556c76aSAlexandre Belloni {
841004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
842004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
843004d44f6SVladimir Oltean 	int port = priv->chip_port;
844a556c76aSAlexandre Belloni 
845a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
846004d44f6SVladimir Oltean 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
847a556c76aSAlexandre Belloni 		     SYS_STAT_CFG);
848a556c76aSAlexandre Belloni 
849a556c76aSAlexandre Belloni 	/* Get Rx stats */
850a556c76aSAlexandre Belloni 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
851a556c76aSAlexandre Belloni 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
852a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
853a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
854a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
855a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
856a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
857a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
858a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
859a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
860a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
861a556c76aSAlexandre Belloni 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
862a556c76aSAlexandre Belloni 	stats->rx_dropped = dev->stats.rx_dropped;
863a556c76aSAlexandre Belloni 
864a556c76aSAlexandre Belloni 	/* Get Tx stats */
865a556c76aSAlexandre Belloni 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
866a556c76aSAlexandre Belloni 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
867a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
868a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
869a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
870a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
871a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
872a556c76aSAlexandre Belloni 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
873a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
874a556c76aSAlexandre Belloni 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
875a556c76aSAlexandre Belloni }
876a556c76aSAlexandre Belloni 
8775e256365SVladimir Oltean int ocelot_fdb_add(struct ocelot *ocelot, int port,
87887b0f983SVladimir Oltean 		   const unsigned char *addr, u16 vid)
879a556c76aSAlexandre Belloni {
880531ee1a6SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
881a556c76aSAlexandre Belloni 
8827142529fSAntoine Tenart 	if (!vid) {
88387b0f983SVladimir Oltean 		if (!ocelot_port->vlan_aware)
8847142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
8857142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
8867142529fSAntoine Tenart 			 * matches incoming untagged packets
8877142529fSAntoine Tenart 			 */
888531ee1a6SVladimir Oltean 			vid = ocelot_port->pvid;
8897142529fSAntoine Tenart 		else
8907142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
8917142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
8927142529fSAntoine Tenart 			 */
8937142529fSAntoine Tenart 			return -EINVAL;
8947142529fSAntoine Tenart 	}
8957142529fSAntoine Tenart 
896531ee1a6SVladimir Oltean 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
897a556c76aSAlexandre Belloni }
8985e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_add);
899a556c76aSAlexandre Belloni 
900531ee1a6SVladimir Oltean static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
901531ee1a6SVladimir Oltean 			       struct net_device *dev,
902531ee1a6SVladimir Oltean 			       const unsigned char *addr,
903531ee1a6SVladimir Oltean 			       u16 vid, u16 flags,
904531ee1a6SVladimir Oltean 			       struct netlink_ext_ack *extack)
905531ee1a6SVladimir Oltean {
906004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
907004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
908004d44f6SVladimir Oltean 	int port = priv->chip_port;
909531ee1a6SVladimir Oltean 
91087b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
911531ee1a6SVladimir Oltean }
912531ee1a6SVladimir Oltean 
9135e256365SVladimir Oltean int ocelot_fdb_del(struct ocelot *ocelot, int port,
914531ee1a6SVladimir Oltean 		   const unsigned char *addr, u16 vid)
915531ee1a6SVladimir Oltean {
916531ee1a6SVladimir Oltean 	return ocelot_mact_forget(ocelot, addr, vid);
917531ee1a6SVladimir Oltean }
9185e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_del);
919531ee1a6SVladimir Oltean 
920531ee1a6SVladimir Oltean static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
921a556c76aSAlexandre Belloni 			       struct net_device *dev,
922a556c76aSAlexandre Belloni 			       const unsigned char *addr, u16 vid)
923a556c76aSAlexandre Belloni {
924004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
925004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
926004d44f6SVladimir Oltean 	int port = priv->chip_port;
927a556c76aSAlexandre Belloni 
928004d44f6SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
929a556c76aSAlexandre Belloni }
930a556c76aSAlexandre Belloni 
931a556c76aSAlexandre Belloni struct ocelot_dump_ctx {
932a556c76aSAlexandre Belloni 	struct net_device *dev;
933a556c76aSAlexandre Belloni 	struct sk_buff *skb;
934a556c76aSAlexandre Belloni 	struct netlink_callback *cb;
935a556c76aSAlexandre Belloni 	int idx;
936a556c76aSAlexandre Belloni };
937a556c76aSAlexandre Belloni 
938531ee1a6SVladimir Oltean static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
939531ee1a6SVladimir Oltean 				   bool is_static, void *data)
940a556c76aSAlexandre Belloni {
941531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx *dump = data;
942a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
943a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
944a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
945a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
946a556c76aSAlexandre Belloni 
947a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
948a556c76aSAlexandre Belloni 		goto skip;
949a556c76aSAlexandre Belloni 
950a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
951a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
952a556c76aSAlexandre Belloni 	if (!nlh)
953a556c76aSAlexandre Belloni 		return -EMSGSIZE;
954a556c76aSAlexandre Belloni 
955a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
956a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
957a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
958a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
959a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
960a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
961a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
962531ee1a6SVladimir Oltean 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
963a556c76aSAlexandre Belloni 
964531ee1a6SVladimir Oltean 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
965a556c76aSAlexandre Belloni 		goto nla_put_failure;
966a556c76aSAlexandre Belloni 
967531ee1a6SVladimir Oltean 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
968a556c76aSAlexandre Belloni 		goto nla_put_failure;
969a556c76aSAlexandre Belloni 
970a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
971a556c76aSAlexandre Belloni 
972a556c76aSAlexandre Belloni skip:
973a556c76aSAlexandre Belloni 	dump->idx++;
974a556c76aSAlexandre Belloni 	return 0;
975a556c76aSAlexandre Belloni 
976a556c76aSAlexandre Belloni nla_put_failure:
977a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
978a556c76aSAlexandre Belloni 	return -EMSGSIZE;
979a556c76aSAlexandre Belloni }
980a556c76aSAlexandre Belloni 
981531ee1a6SVladimir Oltean static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
982a556c76aSAlexandre Belloni 			    struct ocelot_mact_entry *entry)
983a556c76aSAlexandre Belloni {
984a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
985531ee1a6SVladimir Oltean 	char mac[ETH_ALEN];
986a556c76aSAlexandre Belloni 
987a556c76aSAlexandre Belloni 	/* Set row and column to read from */
988a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
989a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
990a556c76aSAlexandre Belloni 
991a556c76aSAlexandre Belloni 	/* Issue a read command */
992a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
993a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
994a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
995a556c76aSAlexandre Belloni 
996a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
997a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
998a556c76aSAlexandre Belloni 
999a556c76aSAlexandre Belloni 	/* Read the entry flags */
1000a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1001a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1002a556c76aSAlexandre Belloni 		return -EINVAL;
1003a556c76aSAlexandre Belloni 
1004a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
1005a556c76aSAlexandre Belloni 	 * do not report it.
1006a556c76aSAlexandre Belloni 	 */
1007a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1008531ee1a6SVladimir Oltean 	if (dst != port)
1009a556c76aSAlexandre Belloni 		return -EINVAL;
1010a556c76aSAlexandre Belloni 
1011a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
1012a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1013a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1014a556c76aSAlexandre Belloni 
1015a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
1016a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
1017a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
1018a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
1019a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
1020a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
1021a556c76aSAlexandre Belloni 
1022a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
1023a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
1024a556c76aSAlexandre Belloni 
1025a556c76aSAlexandre Belloni 	return 0;
1026a556c76aSAlexandre Belloni }
1027a556c76aSAlexandre Belloni 
10285e256365SVladimir Oltean int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1029531ee1a6SVladimir Oltean 		    dsa_fdb_dump_cb_t *cb, void *data)
1030a556c76aSAlexandre Belloni {
1031531ee1a6SVladimir Oltean 	int i, j;
1032a556c76aSAlexandre Belloni 
103321ce7f3eSVladimir Oltean 	/* Loop through all the mac tables entries. */
103421ce7f3eSVladimir Oltean 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1035a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
1036531ee1a6SVladimir Oltean 			struct ocelot_mact_entry entry;
1037531ee1a6SVladimir Oltean 			bool is_static;
1038531ee1a6SVladimir Oltean 			int ret;
1039531ee1a6SVladimir Oltean 
1040531ee1a6SVladimir Oltean 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
1041a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
1042a556c76aSAlexandre Belloni 			 * skip it.
1043a556c76aSAlexandre Belloni 			 */
1044a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
1045a556c76aSAlexandre Belloni 				continue;
1046a556c76aSAlexandre Belloni 			else if (ret)
1047531ee1a6SVladimir Oltean 				return ret;
1048a556c76aSAlexandre Belloni 
1049531ee1a6SVladimir Oltean 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1050531ee1a6SVladimir Oltean 
1051531ee1a6SVladimir Oltean 			ret = cb(entry.mac, entry.vid, is_static, data);
1052a556c76aSAlexandre Belloni 			if (ret)
1053531ee1a6SVladimir Oltean 				return ret;
1054a556c76aSAlexandre Belloni 		}
1055a556c76aSAlexandre Belloni 	}
1056a556c76aSAlexandre Belloni 
1057531ee1a6SVladimir Oltean 	return 0;
1058531ee1a6SVladimir Oltean }
10595e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_fdb_dump);
1060531ee1a6SVladimir Oltean 
1061531ee1a6SVladimir Oltean static int ocelot_port_fdb_dump(struct sk_buff *skb,
1062531ee1a6SVladimir Oltean 				struct netlink_callback *cb,
1063531ee1a6SVladimir Oltean 				struct net_device *dev,
1064531ee1a6SVladimir Oltean 				struct net_device *filter_dev, int *idx)
1065531ee1a6SVladimir Oltean {
1066004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1067004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1068531ee1a6SVladimir Oltean 	struct ocelot_dump_ctx dump = {
1069531ee1a6SVladimir Oltean 		.dev = dev,
1070531ee1a6SVladimir Oltean 		.skb = skb,
1071531ee1a6SVladimir Oltean 		.cb = cb,
1072531ee1a6SVladimir Oltean 		.idx = *idx,
1073531ee1a6SVladimir Oltean 	};
1074004d44f6SVladimir Oltean 	int port = priv->chip_port;
1075531ee1a6SVladimir Oltean 	int ret;
1076531ee1a6SVladimir Oltean 
1077004d44f6SVladimir Oltean 	ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
1078531ee1a6SVladimir Oltean 
1079a556c76aSAlexandre Belloni 	*idx = dump.idx;
1080531ee1a6SVladimir Oltean 
1081a556c76aSAlexandre Belloni 	return ret;
1082a556c76aSAlexandre Belloni }
1083a556c76aSAlexandre Belloni 
10847142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
10857142529fSAntoine Tenart 				  u16 vid)
10867142529fSAntoine Tenart {
10871c44ce56SVladimir Oltean 	return ocelot_vlan_vid_add(dev, vid, false, false);
10887142529fSAntoine Tenart }
10897142529fSAntoine Tenart 
10907142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
10917142529fSAntoine Tenart 				   u16 vid)
10927142529fSAntoine Tenart {
10937142529fSAntoine Tenart 	return ocelot_vlan_vid_del(dev, vid);
10947142529fSAntoine Tenart }
10957142529fSAntoine Tenart 
10967142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev,
10977142529fSAntoine Tenart 			       netdev_features_t features)
10987142529fSAntoine Tenart {
10997142529fSAntoine Tenart 	netdev_features_t changed = dev->features ^ features;
1100004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1101004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1102004d44f6SVladimir Oltean 	int port = priv->chip_port;
11037142529fSAntoine Tenart 
11042c1d029aSJoergen Andreasen 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
1105004d44f6SVladimir Oltean 	    priv->tc.offload_cnt) {
11062c1d029aSJoergen Andreasen 		netdev_err(dev,
11072c1d029aSJoergen Andreasen 			   "Cannot disable HW TC offload while offloads active\n");
11082c1d029aSJoergen Andreasen 		return -EBUSY;
11092c1d029aSJoergen Andreasen 	}
11102c1d029aSJoergen Andreasen 
11117142529fSAntoine Tenart 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
1112f270dbfaSVladimir Oltean 		ocelot_vlan_mode(ocelot, port, features);
11137142529fSAntoine Tenart 
11147142529fSAntoine Tenart 	return 0;
11157142529fSAntoine Tenart }
11167142529fSAntoine Tenart 
1117751302c3SFlorian Fainelli static int ocelot_get_port_parent_id(struct net_device *dev,
1118751302c3SFlorian Fainelli 				     struct netdev_phys_item_id *ppid)
1119751302c3SFlorian Fainelli {
1120004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1121004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1122751302c3SFlorian Fainelli 
1123751302c3SFlorian Fainelli 	ppid->id_len = sizeof(ocelot->base_mac);
1124751302c3SFlorian Fainelli 	memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
1125751302c3SFlorian Fainelli 
1126751302c3SFlorian Fainelli 	return 0;
1127751302c3SFlorian Fainelli }
1128751302c3SFlorian Fainelli 
1129f145922dSYangbo Lu int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
11304e3b0468SAntoine Tenart {
11314e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
11324e3b0468SAntoine Tenart 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
11334e3b0468SAntoine Tenart }
1134f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_get);
11354e3b0468SAntoine Tenart 
1136f145922dSYangbo Lu int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
11374e3b0468SAntoine Tenart {
1138306fd44bSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
11394e3b0468SAntoine Tenart 	struct hwtstamp_config cfg;
11404e3b0468SAntoine Tenart 
11414e3b0468SAntoine Tenart 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
11424e3b0468SAntoine Tenart 		return -EFAULT;
11434e3b0468SAntoine Tenart 
11444e3b0468SAntoine Tenart 	/* reserved for future extensions */
11454e3b0468SAntoine Tenart 	if (cfg.flags)
11464e3b0468SAntoine Tenart 		return -EINVAL;
11474e3b0468SAntoine Tenart 
11484e3b0468SAntoine Tenart 	/* Tx type sanity check */
11494e3b0468SAntoine Tenart 	switch (cfg.tx_type) {
11504e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ON:
1151306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
11524e3b0468SAntoine Tenart 		break;
11534e3b0468SAntoine Tenart 	case HWTSTAMP_TX_ONESTEP_SYNC:
11544e3b0468SAntoine Tenart 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
11554e3b0468SAntoine Tenart 		 * need to update the origin time.
11564e3b0468SAntoine Tenart 		 */
1157306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
11584e3b0468SAntoine Tenart 		break;
11594e3b0468SAntoine Tenart 	case HWTSTAMP_TX_OFF:
1160306fd44bSVladimir Oltean 		ocelot_port->ptp_cmd = 0;
11614e3b0468SAntoine Tenart 		break;
11624e3b0468SAntoine Tenart 	default:
11634e3b0468SAntoine Tenart 		return -ERANGE;
11644e3b0468SAntoine Tenart 	}
11654e3b0468SAntoine Tenart 
11664e3b0468SAntoine Tenart 	mutex_lock(&ocelot->ptp_lock);
11674e3b0468SAntoine Tenart 
11684e3b0468SAntoine Tenart 	switch (cfg.rx_filter) {
11694e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NONE:
11704e3b0468SAntoine Tenart 		break;
11714e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_ALL:
11724e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_SOME:
11734e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
11744e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
11754e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
11764e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_NTP_ALL:
11774e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
11784e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
11794e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
11804e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
11814e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
11824e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
11834e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
11844e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
11854e3b0468SAntoine Tenart 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
11864e3b0468SAntoine Tenart 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
11874e3b0468SAntoine Tenart 		break;
11884e3b0468SAntoine Tenart 	default:
11894e3b0468SAntoine Tenart 		mutex_unlock(&ocelot->ptp_lock);
11904e3b0468SAntoine Tenart 		return -ERANGE;
11914e3b0468SAntoine Tenart 	}
11924e3b0468SAntoine Tenart 
11934e3b0468SAntoine Tenart 	/* Commit back the result & save it */
11944e3b0468SAntoine Tenart 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
11954e3b0468SAntoine Tenart 	mutex_unlock(&ocelot->ptp_lock);
11964e3b0468SAntoine Tenart 
11974e3b0468SAntoine Tenart 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
11984e3b0468SAntoine Tenart }
1199f145922dSYangbo Lu EXPORT_SYMBOL(ocelot_hwstamp_set);
12004e3b0468SAntoine Tenart 
12014e3b0468SAntoine Tenart static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12024e3b0468SAntoine Tenart {
1203004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1204004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1205004d44f6SVladimir Oltean 	int port = priv->chip_port;
12064e3b0468SAntoine Tenart 
1207b2e118f6SAntoine Tenart 	/* If the attached PHY device isn't capable of timestamping operations,
1208b2e118f6SAntoine Tenart 	 * use our own (when possible).
1209b2e118f6SAntoine Tenart 	 */
1210b2e118f6SAntoine Tenart 	if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
12114e3b0468SAntoine Tenart 		switch (cmd) {
12124e3b0468SAntoine Tenart 		case SIOCSHWTSTAMP:
1213306fd44bSVladimir Oltean 			return ocelot_hwstamp_set(ocelot, port, ifr);
12144e3b0468SAntoine Tenart 		case SIOCGHWTSTAMP:
1215306fd44bSVladimir Oltean 			return ocelot_hwstamp_get(ocelot, port, ifr);
12164e3b0468SAntoine Tenart 		}
12174e3b0468SAntoine Tenart 	}
12184e3b0468SAntoine Tenart 
12197ff4f3f3SAntoine Tenart 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
12207ff4f3f3SAntoine Tenart }
12217ff4f3f3SAntoine Tenart 
1222a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = {
1223a556c76aSAlexandre Belloni 	.ndo_open			= ocelot_port_open,
1224a556c76aSAlexandre Belloni 	.ndo_stop			= ocelot_port_stop,
1225a556c76aSAlexandre Belloni 	.ndo_start_xmit			= ocelot_port_xmit,
1226a556c76aSAlexandre Belloni 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
1227a556c76aSAlexandre Belloni 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
1228a556c76aSAlexandre Belloni 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
1229a556c76aSAlexandre Belloni 	.ndo_get_stats64		= ocelot_get_stats64,
1230531ee1a6SVladimir Oltean 	.ndo_fdb_add			= ocelot_port_fdb_add,
1231531ee1a6SVladimir Oltean 	.ndo_fdb_del			= ocelot_port_fdb_del,
1232531ee1a6SVladimir Oltean 	.ndo_fdb_dump			= ocelot_port_fdb_dump,
12337142529fSAntoine Tenart 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
12347142529fSAntoine Tenart 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
12357142529fSAntoine Tenart 	.ndo_set_features		= ocelot_set_features,
1236751302c3SFlorian Fainelli 	.ndo_get_port_parent_id		= ocelot_get_port_parent_id,
12372c1d029aSJoergen Andreasen 	.ndo_setup_tc			= ocelot_setup_tc,
12384e3b0468SAntoine Tenart 	.ndo_do_ioctl			= ocelot_ioctl,
1239a556c76aSAlexandre Belloni };
1240a556c76aSAlexandre Belloni 
12415e256365SVladimir Oltean void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1242a556c76aSAlexandre Belloni {
1243a556c76aSAlexandre Belloni 	int i;
1244a556c76aSAlexandre Belloni 
1245a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1246a556c76aSAlexandre Belloni 		return;
1247a556c76aSAlexandre Belloni 
1248a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1249a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1250a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
1251a556c76aSAlexandre Belloni }
12525e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_strings);
1253a556c76aSAlexandre Belloni 
1254c7282d38SVladimir Oltean static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
1255c7282d38SVladimir Oltean 				    u8 *data)
1256c7282d38SVladimir Oltean {
1257c7282d38SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(netdev);
1258c7282d38SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1259c7282d38SVladimir Oltean 	int port = priv->chip_port;
1260c7282d38SVladimir Oltean 
1261c7282d38SVladimir Oltean 	ocelot_get_strings(ocelot, port, sset, data);
1262c7282d38SVladimir Oltean }
1263c7282d38SVladimir Oltean 
12641e1caa97SClaudiu Manoil static void ocelot_update_stats(struct ocelot *ocelot)
1265a556c76aSAlexandre Belloni {
1266a556c76aSAlexandre Belloni 	int i, j;
1267a556c76aSAlexandre Belloni 
1268a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
1269a556c76aSAlexandre Belloni 
1270a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1271a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
1272a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1273a556c76aSAlexandre Belloni 
1274a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
1275a556c76aSAlexandre Belloni 			u32 val;
1276a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
1277a556c76aSAlexandre Belloni 
1278a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1279a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
1280a556c76aSAlexandre Belloni 
1281a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
1282a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
1283a556c76aSAlexandre Belloni 
1284a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
1285a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
1286a556c76aSAlexandre Belloni 		}
1287a556c76aSAlexandre Belloni 	}
1288a556c76aSAlexandre Belloni 
12891e1caa97SClaudiu Manoil 	mutex_unlock(&ocelot->stats_lock);
12901e1caa97SClaudiu Manoil }
12911e1caa97SClaudiu Manoil 
12921e1caa97SClaudiu Manoil static void ocelot_check_stats_work(struct work_struct *work)
12931e1caa97SClaudiu Manoil {
12941e1caa97SClaudiu Manoil 	struct delayed_work *del_work = to_delayed_work(work);
12951e1caa97SClaudiu Manoil 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
12961e1caa97SClaudiu Manoil 					     stats_work);
12971e1caa97SClaudiu Manoil 
12981e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
12991e1caa97SClaudiu Manoil 
1300a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1301a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1302a556c76aSAlexandre Belloni }
1303a556c76aSAlexandre Belloni 
13045e256365SVladimir Oltean void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1305a556c76aSAlexandre Belloni {
1306a556c76aSAlexandre Belloni 	int i;
1307a556c76aSAlexandre Belloni 
1308a556c76aSAlexandre Belloni 	/* check and update now */
13091e1caa97SClaudiu Manoil 	ocelot_update_stats(ocelot);
1310a556c76aSAlexandre Belloni 
1311a556c76aSAlexandre Belloni 	/* Copy all counters */
1312a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
1313004d44f6SVladimir Oltean 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1314a556c76aSAlexandre Belloni }
13155e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1316a556c76aSAlexandre Belloni 
1317c7282d38SVladimir Oltean static void ocelot_port_get_ethtool_stats(struct net_device *dev,
1318c7282d38SVladimir Oltean 					  struct ethtool_stats *stats,
1319c7282d38SVladimir Oltean 					  u64 *data)
1320a556c76aSAlexandre Belloni {
1321004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1322004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1323c7282d38SVladimir Oltean 	int port = priv->chip_port;
1324a556c76aSAlexandre Belloni 
1325c7282d38SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
1326c7282d38SVladimir Oltean }
1327c7282d38SVladimir Oltean 
13285e256365SVladimir Oltean int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1329c7282d38SVladimir Oltean {
1330a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
1331a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1332c7282d38SVladimir Oltean 
1333a556c76aSAlexandre Belloni 	return ocelot->num_stats;
1334a556c76aSAlexandre Belloni }
13355e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_sset_count);
1336a556c76aSAlexandre Belloni 
1337c7282d38SVladimir Oltean static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
13384e3b0468SAntoine Tenart {
1339004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1340004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1341c7282d38SVladimir Oltean 	int port = priv->chip_port;
13424e3b0468SAntoine Tenart 
1343c7282d38SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
1344c7282d38SVladimir Oltean }
13454e3b0468SAntoine Tenart 
13465e256365SVladimir Oltean int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1347c7282d38SVladimir Oltean 		       struct ethtool_ts_info *info)
1348c7282d38SVladimir Oltean {
13494e3b0468SAntoine Tenart 	info->phc_index = ocelot->ptp_clock ?
13504e3b0468SAntoine Tenart 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1351d2b09a8eSYangbo Lu 	if (info->phc_index == -1) {
1352d2b09a8eSYangbo Lu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1353d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1354d2b09a8eSYangbo Lu 					 SOF_TIMESTAMPING_SOFTWARE;
1355d2b09a8eSYangbo Lu 		return 0;
1356d2b09a8eSYangbo Lu 	}
13574e3b0468SAntoine Tenart 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
13584e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_SOFTWARE |
13594e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_SOFTWARE |
13604e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_TX_HARDWARE |
13614e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RX_HARDWARE |
13624e3b0468SAntoine Tenart 				 SOF_TIMESTAMPING_RAW_HARDWARE;
13634e3b0468SAntoine Tenart 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
13644e3b0468SAntoine Tenart 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
13654e3b0468SAntoine Tenart 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
13664e3b0468SAntoine Tenart 
13674e3b0468SAntoine Tenart 	return 0;
13684e3b0468SAntoine Tenart }
13695e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_get_ts_info);
13704e3b0468SAntoine Tenart 
1371c7282d38SVladimir Oltean static int ocelot_port_get_ts_info(struct net_device *dev,
1372c7282d38SVladimir Oltean 				   struct ethtool_ts_info *info)
1373c7282d38SVladimir Oltean {
1374c7282d38SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1375c7282d38SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1376c7282d38SVladimir Oltean 	int port = priv->chip_port;
1377c7282d38SVladimir Oltean 
1378c7282d38SVladimir Oltean 	if (!ocelot->ptp)
1379c7282d38SVladimir Oltean 		return ethtool_op_get_ts_info(dev, info);
1380c7282d38SVladimir Oltean 
1381c7282d38SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
1382c7282d38SVladimir Oltean }
1383c7282d38SVladimir Oltean 
1384a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = {
1385c7282d38SVladimir Oltean 	.get_strings		= ocelot_port_get_strings,
1386c7282d38SVladimir Oltean 	.get_ethtool_stats	= ocelot_port_get_ethtool_stats,
1387c7282d38SVladimir Oltean 	.get_sset_count		= ocelot_port_get_sset_count,
1388dc96ee37SAlexandre Belloni 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1389dc96ee37SAlexandre Belloni 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1390c7282d38SVladimir Oltean 	.get_ts_info		= ocelot_port_get_ts_info,
1391a556c76aSAlexandre Belloni };
1392a556c76aSAlexandre Belloni 
13935e256365SVladimir Oltean void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1394a556c76aSAlexandre Belloni {
1395a556c76aSAlexandre Belloni 	u32 port_cfg;
13964bda1415SVladimir Oltean 	int p, i;
1397a556c76aSAlexandre Belloni 
13984bda1415SVladimir Oltean 	if (!(BIT(port) & ocelot->bridge_mask))
13994bda1415SVladimir Oltean 		return;
1400a556c76aSAlexandre Belloni 
14014bda1415SVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1402a556c76aSAlexandre Belloni 
1403a556c76aSAlexandre Belloni 	switch (state) {
1404a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
14054bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask |= BIT(port);
1406a556c76aSAlexandre Belloni 		/* Fallthrough */
1407a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1408a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1409a556c76aSAlexandre Belloni 		break;
1410a556c76aSAlexandre Belloni 
1411a556c76aSAlexandre Belloni 	default:
1412a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
14134bda1415SVladimir Oltean 		ocelot->bridge_fwd_mask &= ~BIT(port);
1414a556c76aSAlexandre Belloni 		break;
1415a556c76aSAlexandre Belloni 	}
1416a556c76aSAlexandre Belloni 
14174bda1415SVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
1418a556c76aSAlexandre Belloni 
1419a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1420a556c76aSAlexandre Belloni 	 * a source for the other ports.
1421a556c76aSAlexandre Belloni 	 */
14224bda1415SVladimir Oltean 	for (p = 0; p < ocelot->num_phys_ports; p++) {
142369df578cSVladimir Oltean 		if (ocelot->bridge_fwd_mask & BIT(p)) {
14244bda1415SVladimir Oltean 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
1425a556c76aSAlexandre Belloni 
1426a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1427a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
1428a556c76aSAlexandre Belloni 
1429a556c76aSAlexandre Belloni 				if (!bond_mask)
1430a556c76aSAlexandre Belloni 					continue;
1431a556c76aSAlexandre Belloni 
14324bda1415SVladimir Oltean 				if (bond_mask & BIT(p)) {
1433a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
1434a556c76aSAlexandre Belloni 					break;
1435a556c76aSAlexandre Belloni 				}
1436a556c76aSAlexandre Belloni 			}
1437a556c76aSAlexandre Belloni 
1438c9d2203bSVladimir Oltean 			ocelot_write_rix(ocelot, mask,
14394bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
1440a556c76aSAlexandre Belloni 		} else {
144169df578cSVladimir Oltean 			ocelot_write_rix(ocelot, 0,
14424bda1415SVladimir Oltean 					 ANA_PGID_PGID, PGID_SRC + p);
14434bda1415SVladimir Oltean 		}
1444a556c76aSAlexandre Belloni 	}
1445a556c76aSAlexandre Belloni }
14465e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1447a556c76aSAlexandre Belloni 
14484bda1415SVladimir Oltean static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
14494bda1415SVladimir Oltean 					   struct switchdev_trans *trans,
14504bda1415SVladimir Oltean 					   u8 state)
1451a556c76aSAlexandre Belloni {
14524bda1415SVladimir Oltean 	if (switchdev_trans_ph_prepare(trans))
14534bda1415SVladimir Oltean 		return;
1454a556c76aSAlexandre Belloni 
14554bda1415SVladimir Oltean 	ocelot_bridge_stp_state_set(ocelot, port, state);
14564bda1415SVladimir Oltean }
14574bda1415SVladimir Oltean 
14585e256365SVladimir Oltean void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
14594bda1415SVladimir Oltean {
1460c0d7eccbSVladimir Oltean 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1461c0d7eccbSVladimir Oltean 
1462c0d7eccbSVladimir Oltean 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1463c0d7eccbSVladimir Oltean 	 * which is clearly not what our intention is. So avoid that.
1464c0d7eccbSVladimir Oltean 	 */
1465c0d7eccbSVladimir Oltean 	if (!age_period)
1466c0d7eccbSVladimir Oltean 		age_period = 1;
1467c0d7eccbSVladimir Oltean 
1468c0d7eccbSVladimir Oltean 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1469a556c76aSAlexandre Belloni }
14705e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_set_ageing_time);
1471a556c76aSAlexandre Belloni 
14724bda1415SVladimir Oltean static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
14734bda1415SVladimir Oltean 					unsigned long ageing_clock_t)
1474a556c76aSAlexandre Belloni {
14754bda1415SVladimir Oltean 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1476bf655ba2SVladimir Oltean 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
1477a556c76aSAlexandre Belloni 
14784bda1415SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
14794bda1415SVladimir Oltean }
14804bda1415SVladimir Oltean 
14814bda1415SVladimir Oltean static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
14824bda1415SVladimir Oltean {
14834bda1415SVladimir Oltean 	u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1484a556c76aSAlexandre Belloni 			    ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1485a556c76aSAlexandre Belloni 			    ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
14864bda1415SVladimir Oltean 	u32 val = 0;
1487a556c76aSAlexandre Belloni 
14884bda1415SVladimir Oltean 	if (mc)
14894bda1415SVladimir Oltean 		val = cpu_fwd_mcast;
14904bda1415SVladimir Oltean 
14914bda1415SVladimir Oltean 	ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
14924bda1415SVladimir Oltean 		       ANA_PORT_CPU_FWD_CFG, port);
1493a556c76aSAlexandre Belloni }
1494a556c76aSAlexandre Belloni 
1495a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev,
1496a556c76aSAlexandre Belloni 				const struct switchdev_attr *attr,
1497a556c76aSAlexandre Belloni 				struct switchdev_trans *trans)
1498a556c76aSAlexandre Belloni {
1499004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1500004d44f6SVladimir Oltean 	struct ocelot *ocelot = priv->port.ocelot;
1501004d44f6SVladimir Oltean 	int port = priv->chip_port;
1502a556c76aSAlexandre Belloni 	int err = 0;
1503a556c76aSAlexandre Belloni 
1504a556c76aSAlexandre Belloni 	switch (attr->id) {
1505a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
15064bda1415SVladimir Oltean 		ocelot_port_attr_stp_state_set(ocelot, port, trans,
1507a556c76aSAlexandre Belloni 					       attr->u.stp_state);
1508a556c76aSAlexandre Belloni 		break;
1509a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
15104bda1415SVladimir Oltean 		ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
1511a556c76aSAlexandre Belloni 		break;
15127142529fSAntoine Tenart 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
151387b0f983SVladimir Oltean 		ocelot_port_vlan_filtering(ocelot, port,
151487b0f983SVladimir Oltean 					   attr->u.vlan_filtering);
15157142529fSAntoine Tenart 		break;
1516a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
15174bda1415SVladimir Oltean 		ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
1518a556c76aSAlexandre Belloni 		break;
1519a556c76aSAlexandre Belloni 	default:
1520a556c76aSAlexandre Belloni 		err = -EOPNOTSUPP;
1521a556c76aSAlexandre Belloni 		break;
1522a556c76aSAlexandre Belloni 	}
1523a556c76aSAlexandre Belloni 
1524a556c76aSAlexandre Belloni 	return err;
1525a556c76aSAlexandre Belloni }
1526a556c76aSAlexandre Belloni 
15277142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev,
15287142529fSAntoine Tenart 				    const struct switchdev_obj_port_vlan *vlan,
15297142529fSAntoine Tenart 				    struct switchdev_trans *trans)
15307142529fSAntoine Tenart {
15317142529fSAntoine Tenart 	int ret;
15327142529fSAntoine Tenart 	u16 vid;
15337142529fSAntoine Tenart 
15347142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
15357142529fSAntoine Tenart 		ret = ocelot_vlan_vid_add(dev, vid,
15367142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
15377142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
15387142529fSAntoine Tenart 		if (ret)
15397142529fSAntoine Tenart 			return ret;
15407142529fSAntoine Tenart 	}
15417142529fSAntoine Tenart 
15427142529fSAntoine Tenart 	return 0;
15437142529fSAntoine Tenart }
15447142529fSAntoine Tenart 
15457142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev,
15467142529fSAntoine Tenart 				     const struct switchdev_obj_port_vlan *vlan)
15477142529fSAntoine Tenart {
15487142529fSAntoine Tenart 	int ret;
15497142529fSAntoine Tenart 	u16 vid;
15507142529fSAntoine Tenart 
15517142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
15527142529fSAntoine Tenart 		ret = ocelot_vlan_vid_del(dev, vid);
15537142529fSAntoine Tenart 
15547142529fSAntoine Tenart 		if (ret)
15557142529fSAntoine Tenart 			return ret;
15567142529fSAntoine Tenart 	}
15577142529fSAntoine Tenart 
15587142529fSAntoine Tenart 	return 0;
15597142529fSAntoine Tenart }
15607142529fSAntoine Tenart 
1561a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1562a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1563a556c76aSAlexandre Belloni 						     u16 vid)
1564a556c76aSAlexandre Belloni {
1565a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1566a556c76aSAlexandre Belloni 
1567a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1568a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1569a556c76aSAlexandre Belloni 			return mc;
1570a556c76aSAlexandre Belloni 	}
1571a556c76aSAlexandre Belloni 
1572a556c76aSAlexandre Belloni 	return NULL;
1573a556c76aSAlexandre Belloni }
1574a556c76aSAlexandre Belloni 
1575a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev,
1576a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb,
1577a556c76aSAlexandre Belloni 				   struct switchdev_trans *trans)
1578a556c76aSAlexandre Belloni {
1579004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1580004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
1581004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1582a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1583004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1584004d44f6SVladimir Oltean 	int port = priv->chip_port;
1585a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1586a556c76aSAlexandre Belloni 	bool new = false;
1587a556c76aSAlexandre Belloni 
1588a556c76aSAlexandre Belloni 	if (!vid)
1589004d44f6SVladimir Oltean 		vid = ocelot_port->pvid;
1590a556c76aSAlexandre Belloni 
1591a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1592a556c76aSAlexandre Belloni 	if (!mc) {
1593a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1594a556c76aSAlexandre Belloni 		if (!mc)
1595a556c76aSAlexandre Belloni 			return -ENOMEM;
1596a556c76aSAlexandre Belloni 
1597a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1598a556c76aSAlexandre Belloni 		mc->vid = vid;
1599a556c76aSAlexandre Belloni 
1600a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1601a556c76aSAlexandre Belloni 		new = true;
1602a556c76aSAlexandre Belloni 	}
1603a556c76aSAlexandre Belloni 
1604a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1605a556c76aSAlexandre Belloni 	addr[0] = 0;
1606a556c76aSAlexandre Belloni 
1607a556c76aSAlexandre Belloni 	if (!new) {
1608a556c76aSAlexandre Belloni 		addr[2] = mc->ports << 0;
1609a556c76aSAlexandre Belloni 		addr[1] = mc->ports << 8;
1610a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1611a556c76aSAlexandre Belloni 	}
1612a556c76aSAlexandre Belloni 
1613004d44f6SVladimir Oltean 	mc->ports |= BIT(port);
1614a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1615a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1616a556c76aSAlexandre Belloni 
1617a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1618a556c76aSAlexandre Belloni }
1619a556c76aSAlexandre Belloni 
1620a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev,
1621a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb)
1622a556c76aSAlexandre Belloni {
1623004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1624004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
1625004d44f6SVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1626a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1627004d44f6SVladimir Oltean 	struct ocelot_multicast *mc;
1628004d44f6SVladimir Oltean 	int port = priv->chip_port;
1629a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1630a556c76aSAlexandre Belloni 
1631a556c76aSAlexandre Belloni 	if (!vid)
1632004d44f6SVladimir Oltean 		vid = ocelot_port->pvid;
1633a556c76aSAlexandre Belloni 
1634a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1635a556c76aSAlexandre Belloni 	if (!mc)
1636a556c76aSAlexandre Belloni 		return -ENOENT;
1637a556c76aSAlexandre Belloni 
1638a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1639a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1640a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1641a556c76aSAlexandre Belloni 	addr[0] = 0;
1642a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1643a556c76aSAlexandre Belloni 
1644004d44f6SVladimir Oltean 	mc->ports &= ~BIT(port);
1645a556c76aSAlexandre Belloni 	if (!mc->ports) {
1646a556c76aSAlexandre Belloni 		list_del(&mc->list);
1647a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1648a556c76aSAlexandre Belloni 		return 0;
1649a556c76aSAlexandre Belloni 	}
1650a556c76aSAlexandre Belloni 
1651a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1652a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1653a556c76aSAlexandre Belloni 
1654a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1655a556c76aSAlexandre Belloni }
1656a556c76aSAlexandre Belloni 
1657a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev,
1658a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj,
165969213513SPetr Machata 			       struct switchdev_trans *trans,
166069213513SPetr Machata 			       struct netlink_ext_ack *extack)
1661a556c76aSAlexandre Belloni {
1662a556c76aSAlexandre Belloni 	int ret = 0;
1663a556c76aSAlexandre Belloni 
1664a556c76aSAlexandre Belloni 	switch (obj->id) {
16657142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
16667142529fSAntoine Tenart 		ret = ocelot_port_obj_add_vlan(dev,
16677142529fSAntoine Tenart 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
16687142529fSAntoine Tenart 					       trans);
16697142529fSAntoine Tenart 		break;
1670a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1671a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1672a556c76aSAlexandre Belloni 					      trans);
1673a556c76aSAlexandre Belloni 		break;
1674a556c76aSAlexandre Belloni 	default:
1675a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1676a556c76aSAlexandre Belloni 	}
1677a556c76aSAlexandre Belloni 
1678a556c76aSAlexandre Belloni 	return ret;
1679a556c76aSAlexandre Belloni }
1680a556c76aSAlexandre Belloni 
1681a556c76aSAlexandre Belloni static int ocelot_port_obj_del(struct net_device *dev,
1682a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj)
1683a556c76aSAlexandre Belloni {
1684a556c76aSAlexandre Belloni 	int ret = 0;
1685a556c76aSAlexandre Belloni 
1686a556c76aSAlexandre Belloni 	switch (obj->id) {
16877142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
16887142529fSAntoine Tenart 		ret = ocelot_port_vlan_del_vlan(dev,
16897142529fSAntoine Tenart 						SWITCHDEV_OBJ_PORT_VLAN(obj));
16907142529fSAntoine Tenart 		break;
1691a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1692a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1693a556c76aSAlexandre Belloni 		break;
1694a556c76aSAlexandre Belloni 	default:
1695a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1696a556c76aSAlexandre Belloni 	}
1697a556c76aSAlexandre Belloni 
1698a556c76aSAlexandre Belloni 	return ret;
1699a556c76aSAlexandre Belloni }
1700a556c76aSAlexandre Belloni 
17015e256365SVladimir Oltean int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1702a556c76aSAlexandre Belloni 			    struct net_device *bridge)
1703a556c76aSAlexandre Belloni {
1704a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1705a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1706a556c76aSAlexandre Belloni 	} else {
1707a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1708a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1709a556c76aSAlexandre Belloni 			 * unsupported */
1710a556c76aSAlexandre Belloni 			return -ENODEV;
1711a556c76aSAlexandre Belloni 	}
1712a556c76aSAlexandre Belloni 
1713f270dbfaSVladimir Oltean 	ocelot->bridge_mask |= BIT(port);
1714a556c76aSAlexandre Belloni 
1715a556c76aSAlexandre Belloni 	return 0;
1716a556c76aSAlexandre Belloni }
17175e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_join);
1718a556c76aSAlexandre Belloni 
17195e256365SVladimir Oltean int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1720a556c76aSAlexandre Belloni 			     struct net_device *bridge)
1721a556c76aSAlexandre Belloni {
172297bb69e1SVladimir Oltean 	ocelot->bridge_mask &= ~BIT(port);
1723a556c76aSAlexandre Belloni 
1724a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1725a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
17267142529fSAntoine Tenart 
172797bb69e1SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, 0);
172897bb69e1SVladimir Oltean 	ocelot_port_set_pvid(ocelot, port, 0);
172997bb69e1SVladimir Oltean 	return ocelot_port_set_native_vlan(ocelot, port, 0);
1730a556c76aSAlexandre Belloni }
17315e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_port_bridge_leave);
1732a556c76aSAlexandre Belloni 
1733dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1734dc96ee37SAlexandre Belloni {
1735dc96ee37SAlexandre Belloni 	int i, port, lag;
1736dc96ee37SAlexandre Belloni 
1737dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1738dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1739dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1740dc96ee37SAlexandre Belloni 
1741dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1742dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1743dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1744dc96ee37SAlexandre Belloni 
1745dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1746dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1747dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1748dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1749dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1750dc96ee37SAlexandre Belloni 
1751dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1752dc96ee37SAlexandre Belloni 		if (!bond_mask)
1753dc96ee37SAlexandre Belloni 			continue;
1754dc96ee37SAlexandre Belloni 
1755dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1756dc96ee37SAlexandre Belloni 			// Destination mask
1757dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1758dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1759dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1760dc96ee37SAlexandre Belloni 			aggr_count++;
1761dc96ee37SAlexandre Belloni 		}
1762dc96ee37SAlexandre Belloni 
1763dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1764dc96ee37SAlexandre Belloni 			u32 ac;
1765dc96ee37SAlexandre Belloni 
1766dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1767dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1768dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1769dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1770dc96ee37SAlexandre Belloni 		}
1771dc96ee37SAlexandre Belloni 	}
1772dc96ee37SAlexandre Belloni }
1773dc96ee37SAlexandre Belloni 
1774dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1775dc96ee37SAlexandre Belloni {
1776dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1777dc96ee37SAlexandre Belloni 	unsigned int p;
1778dc96ee37SAlexandre Belloni 
1779dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1780dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1781dc96ee37SAlexandre Belloni 
1782dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1783dc96ee37SAlexandre Belloni 
1784dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1785dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1786dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1787dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1788dc96ee37SAlexandre Belloni 	}
1789dc96ee37SAlexandre Belloni }
1790dc96ee37SAlexandre Belloni 
1791f270dbfaSVladimir Oltean static int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1792dc96ee37SAlexandre Belloni 				struct net_device *bond)
1793dc96ee37SAlexandre Belloni {
1794dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1795dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1796f270dbfaSVladimir Oltean 	int lag, lp;
1797dc96ee37SAlexandre Belloni 
1798dc96ee37SAlexandre Belloni 	rcu_read_lock();
1799dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1800004d44f6SVladimir Oltean 		struct ocelot_port_private *priv = netdev_priv(ndev);
1801dc96ee37SAlexandre Belloni 
1802004d44f6SVladimir Oltean 		bond_mask |= BIT(priv->chip_port);
1803dc96ee37SAlexandre Belloni 	}
1804dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1805dc96ee37SAlexandre Belloni 
1806dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1807dc96ee37SAlexandre Belloni 
1808dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1809dc96ee37SAlexandre Belloni 	 * now on
1810dc96ee37SAlexandre Belloni 	 */
1811f270dbfaSVladimir Oltean 	if (port == lp) {
1812f270dbfaSVladimir Oltean 		lag = port;
1813f270dbfaSVladimir Oltean 		ocelot->lags[port] = bond_mask;
1814f270dbfaSVladimir Oltean 		bond_mask &= ~BIT(port);
1815dc96ee37SAlexandre Belloni 		if (bond_mask) {
1816dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1817dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1818dc96ee37SAlexandre Belloni 		}
1819dc96ee37SAlexandre Belloni 	} else {
1820dc96ee37SAlexandre Belloni 		lag = lp;
1821f270dbfaSVladimir Oltean 		ocelot->lags[lp] |= BIT(port);
1822dc96ee37SAlexandre Belloni 	}
1823dc96ee37SAlexandre Belloni 
1824dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1825dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1826dc96ee37SAlexandre Belloni 
1827dc96ee37SAlexandre Belloni 	return 0;
1828dc96ee37SAlexandre Belloni }
1829dc96ee37SAlexandre Belloni 
1830f270dbfaSVladimir Oltean static void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1831dc96ee37SAlexandre Belloni 				  struct net_device *bond)
1832dc96ee37SAlexandre Belloni {
1833dc96ee37SAlexandre Belloni 	u32 port_cfg;
1834dc96ee37SAlexandre Belloni 	int i;
1835dc96ee37SAlexandre Belloni 
1836dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1837dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1838f270dbfaSVladimir Oltean 		ocelot->lags[i] &= ~BIT(port);
1839dc96ee37SAlexandre Belloni 
1840dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1841dc96ee37SAlexandre Belloni 	 * next port
1842dc96ee37SAlexandre Belloni 	 */
1843f270dbfaSVladimir Oltean 	if (ocelot->lags[port]) {
1844f270dbfaSVladimir Oltean 		int n = __ffs(ocelot->lags[port]);
1845dc96ee37SAlexandre Belloni 
1846f270dbfaSVladimir Oltean 		ocelot->lags[n] = ocelot->lags[port];
1847f270dbfaSVladimir Oltean 		ocelot->lags[port] = 0;
1848dc96ee37SAlexandre Belloni 
1849dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1850dc96ee37SAlexandre Belloni 	}
1851dc96ee37SAlexandre Belloni 
1852f270dbfaSVladimir Oltean 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1853dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1854f270dbfaSVladimir Oltean 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1855f270dbfaSVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
1856dc96ee37SAlexandre Belloni 
1857dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1858dc96ee37SAlexandre Belloni }
1859dc96ee37SAlexandre Belloni 
1860a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */
1861a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1862a556c76aSAlexandre Belloni {
1863a556c76aSAlexandre Belloni 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1864a556c76aSAlexandre Belloni }
1865a556c76aSAlexandre Belloni 
1866a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev,
1867a556c76aSAlexandre Belloni 				       unsigned long event,
1868a556c76aSAlexandre Belloni 				       struct netdev_notifier_changeupper_info *info)
1869a556c76aSAlexandre Belloni {
1870004d44f6SVladimir Oltean 	struct ocelot_port_private *priv = netdev_priv(dev);
1871004d44f6SVladimir Oltean 	struct ocelot_port *ocelot_port = &priv->port;
1872f270dbfaSVladimir Oltean 	struct ocelot *ocelot = ocelot_port->ocelot;
1873004d44f6SVladimir Oltean 	int port = priv->chip_port;
1874a556c76aSAlexandre Belloni 	int err = 0;
1875a556c76aSAlexandre Belloni 
1876a556c76aSAlexandre Belloni 	switch (event) {
1877a556c76aSAlexandre Belloni 	case NETDEV_CHANGEUPPER:
1878a556c76aSAlexandre Belloni 		if (netif_is_bridge_master(info->upper_dev)) {
1879004d44f6SVladimir Oltean 			if (info->linking) {
1880f270dbfaSVladimir Oltean 				err = ocelot_port_bridge_join(ocelot, port,
1881a556c76aSAlexandre Belloni 							      info->upper_dev);
1882004d44f6SVladimir Oltean 			} else {
1883f270dbfaSVladimir Oltean 				err = ocelot_port_bridge_leave(ocelot, port,
1884a556c76aSAlexandre Belloni 							       info->upper_dev);
1885004d44f6SVladimir Oltean 			}
1886a556c76aSAlexandre Belloni 		}
1887dc96ee37SAlexandre Belloni 		if (netif_is_lag_master(info->upper_dev)) {
1888dc96ee37SAlexandre Belloni 			if (info->linking)
1889f270dbfaSVladimir Oltean 				err = ocelot_port_lag_join(ocelot, port,
1890dc96ee37SAlexandre Belloni 							   info->upper_dev);
1891dc96ee37SAlexandre Belloni 			else
1892f270dbfaSVladimir Oltean 				ocelot_port_lag_leave(ocelot, port,
1893dc96ee37SAlexandre Belloni 						      info->upper_dev);
1894dc96ee37SAlexandre Belloni 		}
1895a556c76aSAlexandre Belloni 		break;
1896a556c76aSAlexandre Belloni 	default:
1897a556c76aSAlexandre Belloni 		break;
1898a556c76aSAlexandre Belloni 	}
1899a556c76aSAlexandre Belloni 
1900a556c76aSAlexandre Belloni 	return err;
1901a556c76aSAlexandre Belloni }
1902a556c76aSAlexandre Belloni 
1903a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused,
1904a556c76aSAlexandre Belloni 				  unsigned long event, void *ptr)
1905a556c76aSAlexandre Belloni {
1906a556c76aSAlexandre Belloni 	struct netdev_notifier_changeupper_info *info = ptr;
1907a556c76aSAlexandre Belloni 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
19082ac0e152SGeert Uytterhoeven 	int ret = 0;
1909a556c76aSAlexandre Belloni 
19107afb3e57SClaudiu Manoil 	if (!ocelot_netdevice_dev_check(dev))
19117afb3e57SClaudiu Manoil 		return 0;
19127afb3e57SClaudiu Manoil 
1913dc96ee37SAlexandre Belloni 	if (event == NETDEV_PRECHANGEUPPER &&
1914dc96ee37SAlexandre Belloni 	    netif_is_lag_master(info->upper_dev)) {
1915dc96ee37SAlexandre Belloni 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1916dc96ee37SAlexandre Belloni 		struct netlink_ext_ack *extack;
1917dc96ee37SAlexandre Belloni 
19183b3eed8eSClaudiu Manoil 		if (lag_upper_info &&
19193b3eed8eSClaudiu Manoil 		    lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1920dc96ee37SAlexandre Belloni 			extack = netdev_notifier_info_to_extack(&info->info);
1921dc96ee37SAlexandre Belloni 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1922dc96ee37SAlexandre Belloni 
1923dc96ee37SAlexandre Belloni 			ret = -EINVAL;
1924dc96ee37SAlexandre Belloni 			goto notify;
1925dc96ee37SAlexandre Belloni 		}
1926dc96ee37SAlexandre Belloni 	}
1927dc96ee37SAlexandre Belloni 
1928a556c76aSAlexandre Belloni 	if (netif_is_lag_master(dev)) {
1929a556c76aSAlexandre Belloni 		struct net_device *slave;
1930a556c76aSAlexandre Belloni 		struct list_head *iter;
1931a556c76aSAlexandre Belloni 
1932a556c76aSAlexandre Belloni 		netdev_for_each_lower_dev(dev, slave, iter) {
1933a556c76aSAlexandre Belloni 			ret = ocelot_netdevice_port_event(slave, event, info);
1934a556c76aSAlexandre Belloni 			if (ret)
1935a556c76aSAlexandre Belloni 				goto notify;
1936a556c76aSAlexandre Belloni 		}
1937a556c76aSAlexandre Belloni 	} else {
1938a556c76aSAlexandre Belloni 		ret = ocelot_netdevice_port_event(dev, event, info);
1939a556c76aSAlexandre Belloni 	}
1940a556c76aSAlexandre Belloni 
1941a556c76aSAlexandre Belloni notify:
1942a556c76aSAlexandre Belloni 	return notifier_from_errno(ret);
1943a556c76aSAlexandre Belloni }
1944a556c76aSAlexandre Belloni 
1945a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = {
1946a556c76aSAlexandre Belloni 	.notifier_call = ocelot_netdevice_event,
1947a556c76aSAlexandre Belloni };
1948a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb);
1949a556c76aSAlexandre Belloni 
195056da64bcSFlorian Fainelli static int ocelot_switchdev_event(struct notifier_block *unused,
195156da64bcSFlorian Fainelli 				  unsigned long event, void *ptr)
195256da64bcSFlorian Fainelli {
195356da64bcSFlorian Fainelli 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
195456da64bcSFlorian Fainelli 	int err;
195556da64bcSFlorian Fainelli 
195656da64bcSFlorian Fainelli 	switch (event) {
195756da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
195856da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
195956da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
196056da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
196156da64bcSFlorian Fainelli 		return notifier_from_errno(err);
196256da64bcSFlorian Fainelli 	}
196356da64bcSFlorian Fainelli 
196456da64bcSFlorian Fainelli 	return NOTIFY_DONE;
196556da64bcSFlorian Fainelli }
196656da64bcSFlorian Fainelli 
196756da64bcSFlorian Fainelli struct notifier_block ocelot_switchdev_nb __read_mostly = {
196856da64bcSFlorian Fainelli 	.notifier_call = ocelot_switchdev_event,
196956da64bcSFlorian Fainelli };
197056da64bcSFlorian Fainelli EXPORT_SYMBOL(ocelot_switchdev_nb);
197156da64bcSFlorian Fainelli 
19720e332c85SPetr Machata static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
19730e332c85SPetr Machata 					   unsigned long event, void *ptr)
19740e332c85SPetr Machata {
19750e332c85SPetr Machata 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
19760e332c85SPetr Machata 	int err;
19770e332c85SPetr Machata 
19780e332c85SPetr Machata 	switch (event) {
19790e332c85SPetr Machata 		/* Blocking events. */
19800e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_ADD:
19810e332c85SPetr Machata 		err = switchdev_handle_port_obj_add(dev, ptr,
19820e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
19830e332c85SPetr Machata 						    ocelot_port_obj_add);
19840e332c85SPetr Machata 		return notifier_from_errno(err);
19850e332c85SPetr Machata 	case SWITCHDEV_PORT_OBJ_DEL:
19860e332c85SPetr Machata 		err = switchdev_handle_port_obj_del(dev, ptr,
19870e332c85SPetr Machata 						    ocelot_netdevice_dev_check,
19880e332c85SPetr Machata 						    ocelot_port_obj_del);
19890e332c85SPetr Machata 		return notifier_from_errno(err);
199056da64bcSFlorian Fainelli 	case SWITCHDEV_PORT_ATTR_SET:
199156da64bcSFlorian Fainelli 		err = switchdev_handle_port_attr_set(dev, ptr,
199256da64bcSFlorian Fainelli 						     ocelot_netdevice_dev_check,
199356da64bcSFlorian Fainelli 						     ocelot_port_attr_set);
199456da64bcSFlorian Fainelli 		return notifier_from_errno(err);
19950e332c85SPetr Machata 	}
19960e332c85SPetr Machata 
19970e332c85SPetr Machata 	return NOTIFY_DONE;
19980e332c85SPetr Machata }
19990e332c85SPetr Machata 
20000e332c85SPetr Machata struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
20010e332c85SPetr Machata 	.notifier_call = ocelot_switchdev_blocking_event,
20020e332c85SPetr Machata };
20030e332c85SPetr Machata EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
20040e332c85SPetr Machata 
2005a8015dedSVladimir Oltean /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2006a8015dedSVladimir Oltean  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
20070b912fc9SVladimir Oltean  * In the special case that it's the NPI port that we're configuring, the
20080b912fc9SVladimir Oltean  * length of the tag and optional prefix needs to be accounted for privately,
20090b912fc9SVladimir Oltean  * in order to be able to sustain communication at the requested @sdu.
2010a8015dedSVladimir Oltean  */
20110b912fc9SVladimir Oltean void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
201231350d7fSVladimir Oltean {
201331350d7fSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2014a8015dedSVladimir Oltean 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
20155bc9d2e6SVladimir Oltean 	int atop_wm;
201631350d7fSVladimir Oltean 
20170b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
20180b912fc9SVladimir Oltean 		maxlen += OCELOT_TAG_LEN;
20190b912fc9SVladimir Oltean 
20200b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
20210b912fc9SVladimir Oltean 			maxlen += OCELOT_SHORT_PREFIX_LEN;
20220b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
20230b912fc9SVladimir Oltean 			maxlen += OCELOT_LONG_PREFIX_LEN;
20240b912fc9SVladimir Oltean 	}
20250b912fc9SVladimir Oltean 
2026a8015dedSVladimir Oltean 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2027fa914e9cSVladimir Oltean 
2028fa914e9cSVladimir Oltean 	/* Set Pause WM hysteresis
2029a8015dedSVladimir Oltean 	 * 152 = 6 * maxlen / OCELOT_BUFFER_CELL_SZ
2030a8015dedSVladimir Oltean 	 * 101 = 4 * maxlen / OCELOT_BUFFER_CELL_SZ
2031fa914e9cSVladimir Oltean 	 */
2032fa914e9cSVladimir Oltean 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
2033fa914e9cSVladimir Oltean 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
2034fa914e9cSVladimir Oltean 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, port);
2035fa914e9cSVladimir Oltean 
2036fa914e9cSVladimir Oltean 	/* Tail dropping watermark */
2037a8015dedSVladimir Oltean 	atop_wm = (ocelot->shared_queue_sz - 9 * maxlen) /
2038a8015dedSVladimir Oltean 		   OCELOT_BUFFER_CELL_SZ;
2039a8015dedSVladimir Oltean 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * maxlen),
2040fa914e9cSVladimir Oltean 			 SYS_ATOP, port);
2041fa914e9cSVladimir Oltean 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
2042fa914e9cSVladimir Oltean }
20430b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_port_set_maxlen);
20440b912fc9SVladimir Oltean 
20450b912fc9SVladimir Oltean int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
20460b912fc9SVladimir Oltean {
20470b912fc9SVladimir Oltean 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
20480b912fc9SVladimir Oltean 
20490b912fc9SVladimir Oltean 	if (port == ocelot->npi) {
20500b912fc9SVladimir Oltean 		max_mtu -= OCELOT_TAG_LEN;
20510b912fc9SVladimir Oltean 
20520b912fc9SVladimir Oltean 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
20530b912fc9SVladimir Oltean 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
20540b912fc9SVladimir Oltean 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
20550b912fc9SVladimir Oltean 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
20560b912fc9SVladimir Oltean 	}
20570b912fc9SVladimir Oltean 
20580b912fc9SVladimir Oltean 	return max_mtu;
20590b912fc9SVladimir Oltean }
20600b912fc9SVladimir Oltean EXPORT_SYMBOL(ocelot_get_max_mtu);
2061fa914e9cSVladimir Oltean 
20625e256365SVladimir Oltean void ocelot_init_port(struct ocelot *ocelot, int port)
2063fa914e9cSVladimir Oltean {
2064fa914e9cSVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2065fa914e9cSVladimir Oltean 
2066b049da13SYangbo Lu 	skb_queue_head_init(&ocelot_port->tx_skbs);
206731350d7fSVladimir Oltean 
206831350d7fSVladimir Oltean 	/* Basic L2 initialization */
206931350d7fSVladimir Oltean 
20705bc9d2e6SVladimir Oltean 	/* Set MAC IFG Gaps
20715bc9d2e6SVladimir Oltean 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
20725bc9d2e6SVladimir Oltean 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
20735bc9d2e6SVladimir Oltean 	 */
20745bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
20755bc9d2e6SVladimir Oltean 			   DEV_MAC_IFG_CFG);
20765bc9d2e6SVladimir Oltean 
20775bc9d2e6SVladimir Oltean 	/* Load seed (0) and set MAC HDX late collision  */
20785bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
20795bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG_SEED_LOAD,
20805bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
20815bc9d2e6SVladimir Oltean 	mdelay(1);
20825bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
20835bc9d2e6SVladimir Oltean 			   DEV_MAC_HDX_CFG);
20845bc9d2e6SVladimir Oltean 
20855bc9d2e6SVladimir Oltean 	/* Set Max Length and maximum tags allowed */
2086a8015dedSVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
20875bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
20885bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2089a8015dedSVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
20905bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
20915bc9d2e6SVladimir Oltean 			   DEV_MAC_TAGS_CFG);
20925bc9d2e6SVladimir Oltean 
20935bc9d2e6SVladimir Oltean 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
20945bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
20955bc9d2e6SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
20965bc9d2e6SVladimir Oltean 
209731350d7fSVladimir Oltean 	/* Drop frames with multicast source address */
209831350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
209931350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
210031350d7fSVladimir Oltean 		       ANA_PORT_DROP_CFG, port);
210131350d7fSVladimir Oltean 
210231350d7fSVladimir Oltean 	/* Set default VLAN and tag type to 8021Q. */
210331350d7fSVladimir Oltean 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
210431350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
210531350d7fSVladimir Oltean 		       REW_PORT_VLAN_CFG, port);
210631350d7fSVladimir Oltean 
210731350d7fSVladimir Oltean 	/* Enable vcap lookups */
210831350d7fSVladimir Oltean 	ocelot_vcap_enable(ocelot, port);
210931350d7fSVladimir Oltean }
21105e256365SVladimir Oltean EXPORT_SYMBOL(ocelot_init_port);
211131350d7fSVladimir Oltean 
2112a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2113a556c76aSAlexandre Belloni 		      void __iomem *regs,
2114a556c76aSAlexandre Belloni 		      struct phy_device *phy)
2115a556c76aSAlexandre Belloni {
2116004d44f6SVladimir Oltean 	struct ocelot_port_private *priv;
2117a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port;
2118a556c76aSAlexandre Belloni 	struct net_device *dev;
2119a556c76aSAlexandre Belloni 	int err;
2120a556c76aSAlexandre Belloni 
2121004d44f6SVladimir Oltean 	dev = alloc_etherdev(sizeof(struct ocelot_port_private));
2122a556c76aSAlexandre Belloni 	if (!dev)
2123a556c76aSAlexandre Belloni 		return -ENOMEM;
2124a556c76aSAlexandre Belloni 	SET_NETDEV_DEV(dev, ocelot->dev);
2125004d44f6SVladimir Oltean 	priv = netdev_priv(dev);
2126004d44f6SVladimir Oltean 	priv->dev = dev;
2127004d44f6SVladimir Oltean 	priv->phy = phy;
2128004d44f6SVladimir Oltean 	priv->chip_port = port;
2129004d44f6SVladimir Oltean 	ocelot_port = &priv->port;
2130a556c76aSAlexandre Belloni 	ocelot_port->ocelot = ocelot;
2131a556c76aSAlexandre Belloni 	ocelot_port->regs = regs;
2132a556c76aSAlexandre Belloni 	ocelot->ports[port] = ocelot_port;
2133a556c76aSAlexandre Belloni 
2134a556c76aSAlexandre Belloni 	dev->netdev_ops = &ocelot_port_netdev_ops;
2135a556c76aSAlexandre Belloni 	dev->ethtool_ops = &ocelot_ethtool_ops;
2136a556c76aSAlexandre Belloni 
21372c1d029aSJoergen Andreasen 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
21382c1d029aSJoergen Andreasen 		NETIF_F_HW_TC;
21392c1d029aSJoergen Andreasen 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
21407142529fSAntoine Tenart 
2141a556c76aSAlexandre Belloni 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2142a556c76aSAlexandre Belloni 	dev->dev_addr[ETH_ALEN - 1] += port;
2143a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2144a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
2145a556c76aSAlexandre Belloni 
214631350d7fSVladimir Oltean 	ocelot_init_port(ocelot, port);
21474e3b0468SAntoine Tenart 
2148a556c76aSAlexandre Belloni 	err = register_netdev(dev);
2149a556c76aSAlexandre Belloni 	if (err) {
2150a556c76aSAlexandre Belloni 		dev_err(ocelot->dev, "register_netdev failed\n");
215131350d7fSVladimir Oltean 		free_netdev(dev);
2152a556c76aSAlexandre Belloni 	}
2153a556c76aSAlexandre Belloni 
2154a556c76aSAlexandre Belloni 	return err;
2155a556c76aSAlexandre Belloni }
2156a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port);
2157a556c76aSAlexandre Belloni 
215869df578cSVladimir Oltean /* Configure and enable the CPU port module, which is a set of queues.
215969df578cSVladimir Oltean  * If @npi contains a valid port index, the CPU port module is connected
216069df578cSVladimir Oltean  * to the Node Processor Interface (NPI). This is the mode through which
216169df578cSVladimir Oltean  * frames can be injected from and extracted to an external CPU,
216269df578cSVladimir Oltean  * over Ethernet.
216369df578cSVladimir Oltean  */
216469df578cSVladimir Oltean void ocelot_configure_cpu(struct ocelot *ocelot, int npi,
216521468199SVladimir Oltean 			  enum ocelot_tag_prefix injection,
216621468199SVladimir Oltean 			  enum ocelot_tag_prefix extraction)
216721468199SVladimir Oltean {
216869df578cSVladimir Oltean 	int cpu = ocelot->num_phys_ports;
216969df578cSVladimir Oltean 
21700b912fc9SVladimir Oltean 	ocelot->npi = npi;
21710b912fc9SVladimir Oltean 	ocelot->inj_prefix = injection;
21720b912fc9SVladimir Oltean 	ocelot->xtr_prefix = extraction;
21730b912fc9SVladimir Oltean 
217469df578cSVladimir Oltean 	/* The unicast destination PGID for the CPU port module is unused */
217521468199SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
217669df578cSVladimir Oltean 	/* Instead set up a multicast destination PGID for traffic copied to
217769df578cSVladimir Oltean 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
217869df578cSVladimir Oltean 	 * addresses will be copied to the CPU via this PGID.
217969df578cSVladimir Oltean 	 */
218021468199SVladimir Oltean 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
218121468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
218221468199SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
218321468199SVladimir Oltean 			 ANA_PORT_PORT_CFG, cpu);
218421468199SVladimir Oltean 
218569df578cSVladimir Oltean 	if (npi >= 0 && npi < ocelot->num_phys_ports) {
218621468199SVladimir Oltean 		ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
218769df578cSVladimir Oltean 			     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(npi),
218821468199SVladimir Oltean 			     QSYS_EXT_CPU_CFG);
2189ba551bc3SVladimir Oltean 
219069df578cSVladimir Oltean 		/* Enable NPI port */
219169df578cSVladimir Oltean 		ocelot_write_rix(ocelot,
219269df578cSVladimir Oltean 				 QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
219369df578cSVladimir Oltean 				 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
219469df578cSVladimir Oltean 				 QSYS_SWITCH_PORT_MODE_PORT_ENA,
219569df578cSVladimir Oltean 				 QSYS_SWITCH_PORT_MODE, npi);
219669df578cSVladimir Oltean 		/* NPI port Injection/Extraction configuration */
219769df578cSVladimir Oltean 		ocelot_write_rix(ocelot,
219869df578cSVladimir Oltean 				 SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
219969df578cSVladimir Oltean 				 SYS_PORT_MODE_INCL_INJ_HDR(injection),
220069df578cSVladimir Oltean 				 SYS_PORT_MODE, npi);
220121468199SVladimir Oltean 	}
220221468199SVladimir Oltean 
220369df578cSVladimir Oltean 	/* Enable CPU port module */
220421468199SVladimir Oltean 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
220521468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
220621468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
220721468199SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, cpu);
220869df578cSVladimir Oltean 	/* CPU port Injection/Extraction configuration */
220921468199SVladimir Oltean 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
221021468199SVladimir Oltean 			 SYS_PORT_MODE_INCL_INJ_HDR(injection),
221121468199SVladimir Oltean 			 SYS_PORT_MODE, cpu);
221221468199SVladimir Oltean 
221321468199SVladimir Oltean 	/* Configure the CPU port to be VLAN aware */
221421468199SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
221521468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
221621468199SVladimir Oltean 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
221721468199SVladimir Oltean 			 ANA_PORT_VLAN_CFG, cpu);
221821468199SVladimir Oltean }
221969df578cSVladimir Oltean EXPORT_SYMBOL(ocelot_configure_cpu);
222021468199SVladimir Oltean 
2221a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
2222a556c76aSAlexandre Belloni {
2223a556c76aSAlexandre Belloni 	char queue_name[32];
222421468199SVladimir Oltean 	int i, ret;
222521468199SVladimir Oltean 	u32 port;
2226a556c76aSAlexandre Belloni 
22273a77b593SVladimir Oltean 	if (ocelot->ops->reset) {
22283a77b593SVladimir Oltean 		ret = ocelot->ops->reset(ocelot);
22293a77b593SVladimir Oltean 		if (ret) {
22303a77b593SVladimir Oltean 			dev_err(ocelot->dev, "Switch reset failed\n");
22313a77b593SVladimir Oltean 			return ret;
22323a77b593SVladimir Oltean 		}
22333a77b593SVladimir Oltean 	}
22343a77b593SVladimir Oltean 
2235dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2236dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
2237dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
2238dc96ee37SAlexandre Belloni 		return -ENOMEM;
2239dc96ee37SAlexandre Belloni 
2240a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
2241a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
2242a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
2243a556c76aSAlexandre Belloni 	if (!ocelot->stats)
2244a556c76aSAlexandre Belloni 		return -ENOMEM;
2245a556c76aSAlexandre Belloni 
2246a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
22474e3b0468SAntoine Tenart 	mutex_init(&ocelot->ptp_lock);
22484e3b0468SAntoine Tenart 	spin_lock_init(&ocelot->ptp_clock_lock);
2249a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2250a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
2251a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2252a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
2253a556c76aSAlexandre Belloni 		return -ENOMEM;
2254a556c76aSAlexandre Belloni 
22552b120ddeSClaudiu Manoil 	INIT_LIST_HEAD(&ocelot->multicast);
2256a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
2257a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
2258b5962294SHoratiu Vultur 	ocelot_ace_init(ocelot);
2259a556c76aSAlexandre Belloni 
2260a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2261a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
2262a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2263a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2264a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
2265a556c76aSAlexandre Belloni 	}
2266a556c76aSAlexandre Belloni 
2267a556c76aSAlexandre Belloni 	/* Only use S-Tag */
2268a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2269a556c76aSAlexandre Belloni 
2270a556c76aSAlexandre Belloni 	/* Aggregation mode */
2271a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2272a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2273a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2274a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2275a556c76aSAlexandre Belloni 
2276a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
2277a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
2278a556c76aSAlexandre Belloni 	 */
2279a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
2280a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2281a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
2282a556c76aSAlexandre Belloni 
2283a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
2284a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2285a556c76aSAlexandre Belloni 
2286a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2287a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2288a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2289a556c76aSAlexandre Belloni 
2290a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
2291a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2292a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2293a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2294a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
2295a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2296a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2297a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2298a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2299a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
2300a556c76aSAlexandre Belloni 
2301a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2302a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
2303a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2304a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
2305a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
2306a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2307a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2308a556c76aSAlexandre Belloni 				 port);
2309a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
2310a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2311a556c76aSAlexandre Belloni 	}
2312a556c76aSAlexandre Belloni 
2313a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
2314a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2315a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2316a556c76aSAlexandre Belloni 
2317a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2318a556c76aSAlexandre Belloni 	}
2319a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
2320a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2321a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
2322a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2323a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2324a556c76aSAlexandre Belloni 
2325a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2326a556c76aSAlexandre Belloni 	 * registers endianness.
2327a556c76aSAlexandre Belloni 	 */
2328a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2329a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2330a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2331a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2332a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2333a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2334a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2335a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2336a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2337a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2338a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2339a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2340a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2341a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
2342a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2343a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2344a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
2345a556c76aSAlexandre Belloni 
23461e1caa97SClaudiu Manoil 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2347a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2348a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
23494e3b0468SAntoine Tenart 
2350a556c76aSAlexandre Belloni 	return 0;
2351a556c76aSAlexandre Belloni }
2352a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
2353a556c76aSAlexandre Belloni 
2354a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
2355a556c76aSAlexandre Belloni {
23564e3b0468SAntoine Tenart 	struct ocelot_port *port;
23574e3b0468SAntoine Tenart 	int i;
23584e3b0468SAntoine Tenart 
2359c5d13969SClaudiu Manoil 	cancel_delayed_work(&ocelot->stats_work);
2360a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
2361a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
23624e3b0468SAntoine Tenart 
23634e3b0468SAntoine Tenart 	for (i = 0; i < ocelot->num_phys_ports; i++) {
23644e3b0468SAntoine Tenart 		port = ocelot->ports[i];
2365b049da13SYangbo Lu 		skb_queue_purge(&port->tx_skbs);
23664e3b0468SAntoine Tenart 	}
2367a556c76aSAlexandre Belloni }
2368a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
2369a556c76aSAlexandre Belloni 
2370a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
2371