xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 7142529f168846065eaf4e8126e3de7c8ffc292d)
1a556c76aSAlexandre Belloni // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2a556c76aSAlexandre Belloni /*
3a556c76aSAlexandre Belloni  * Microsemi Ocelot Switch driver
4a556c76aSAlexandre Belloni  *
5a556c76aSAlexandre Belloni  * Copyright (c) 2017 Microsemi Corporation
6a556c76aSAlexandre Belloni  */
7a556c76aSAlexandre Belloni #include <linux/etherdevice.h>
8a556c76aSAlexandre Belloni #include <linux/ethtool.h>
9a556c76aSAlexandre Belloni #include <linux/if_bridge.h>
10a556c76aSAlexandre Belloni #include <linux/if_ether.h>
11a556c76aSAlexandre Belloni #include <linux/if_vlan.h>
12a556c76aSAlexandre Belloni #include <linux/interrupt.h>
13a556c76aSAlexandre Belloni #include <linux/kernel.h>
14a556c76aSAlexandre Belloni #include <linux/module.h>
15a556c76aSAlexandre Belloni #include <linux/netdevice.h>
16a556c76aSAlexandre Belloni #include <linux/phy.h>
17a556c76aSAlexandre Belloni #include <linux/skbuff.h>
18a556c76aSAlexandre Belloni #include <net/arp.h>
19a556c76aSAlexandre Belloni #include <net/netevent.h>
20a556c76aSAlexandre Belloni #include <net/rtnetlink.h>
21a556c76aSAlexandre Belloni #include <net/switchdev.h>
22a556c76aSAlexandre Belloni 
23a556c76aSAlexandre Belloni #include "ocelot.h"
24a556c76aSAlexandre Belloni 
25a556c76aSAlexandre Belloni /* MAC table entry types.
26a556c76aSAlexandre Belloni  * ENTRYTYPE_NORMAL is subject to aging.
27a556c76aSAlexandre Belloni  * ENTRYTYPE_LOCKED is not subject to aging.
28a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
29a556c76aSAlexandre Belloni  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
30a556c76aSAlexandre Belloni  */
31a556c76aSAlexandre Belloni enum macaccess_entry_type {
32a556c76aSAlexandre Belloni 	ENTRYTYPE_NORMAL = 0,
33a556c76aSAlexandre Belloni 	ENTRYTYPE_LOCKED,
34a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv4,
35a556c76aSAlexandre Belloni 	ENTRYTYPE_MACv6,
36a556c76aSAlexandre Belloni };
37a556c76aSAlexandre Belloni 
38a556c76aSAlexandre Belloni struct ocelot_mact_entry {
39a556c76aSAlexandre Belloni 	u8 mac[ETH_ALEN];
40a556c76aSAlexandre Belloni 	u16 vid;
41a556c76aSAlexandre Belloni 	enum macaccess_entry_type type;
42a556c76aSAlexandre Belloni };
43a556c76aSAlexandre Belloni 
44a556c76aSAlexandre Belloni static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
45a556c76aSAlexandre Belloni {
46a556c76aSAlexandre Belloni 	unsigned int val, timeout = 10;
47a556c76aSAlexandre Belloni 
48a556c76aSAlexandre Belloni 	/* Wait for the issued mac table command to be completed, or timeout.
49a556c76aSAlexandre Belloni 	 * When the command read from  ANA_TABLES_MACACCESS is
50a556c76aSAlexandre Belloni 	 * MACACCESS_CMD_IDLE, the issued command completed successfully.
51a556c76aSAlexandre Belloni 	 */
52a556c76aSAlexandre Belloni 	do {
53a556c76aSAlexandre Belloni 		val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
54a556c76aSAlexandre Belloni 		val &= ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M;
55a556c76aSAlexandre Belloni 	} while (val != MACACCESS_CMD_IDLE && timeout--);
56a556c76aSAlexandre Belloni 
57a556c76aSAlexandre Belloni 	if (!timeout)
58a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
59a556c76aSAlexandre Belloni 
60a556c76aSAlexandre Belloni 	return 0;
61a556c76aSAlexandre Belloni }
62a556c76aSAlexandre Belloni 
63a556c76aSAlexandre Belloni static void ocelot_mact_select(struct ocelot *ocelot,
64a556c76aSAlexandre Belloni 			       const unsigned char mac[ETH_ALEN],
65a556c76aSAlexandre Belloni 			       unsigned int vid)
66a556c76aSAlexandre Belloni {
67a556c76aSAlexandre Belloni 	u32 macl = 0, mach = 0;
68a556c76aSAlexandre Belloni 
69a556c76aSAlexandre Belloni 	/* Set the MAC address to handle and the vlan associated in a format
70a556c76aSAlexandre Belloni 	 * understood by the hardware.
71a556c76aSAlexandre Belloni 	 */
72a556c76aSAlexandre Belloni 	mach |= vid    << 16;
73a556c76aSAlexandre Belloni 	mach |= mac[0] << 8;
74a556c76aSAlexandre Belloni 	mach |= mac[1] << 0;
75a556c76aSAlexandre Belloni 	macl |= mac[2] << 24;
76a556c76aSAlexandre Belloni 	macl |= mac[3] << 16;
77a556c76aSAlexandre Belloni 	macl |= mac[4] << 8;
78a556c76aSAlexandre Belloni 	macl |= mac[5] << 0;
79a556c76aSAlexandre Belloni 
80a556c76aSAlexandre Belloni 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
81a556c76aSAlexandre Belloni 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
82a556c76aSAlexandre Belloni 
83a556c76aSAlexandre Belloni }
84a556c76aSAlexandre Belloni 
85a556c76aSAlexandre Belloni static int ocelot_mact_learn(struct ocelot *ocelot, int port,
86a556c76aSAlexandre Belloni 			     const unsigned char mac[ETH_ALEN],
87a556c76aSAlexandre Belloni 			     unsigned int vid,
88a556c76aSAlexandre Belloni 			     enum macaccess_entry_type type)
89a556c76aSAlexandre Belloni {
90a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
91a556c76aSAlexandre Belloni 
92a556c76aSAlexandre Belloni 	/* Issue a write command */
93a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
94a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
95a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
96a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
97a556c76aSAlexandre Belloni 			     ANA_TABLES_MACACCESS);
98a556c76aSAlexandre Belloni 
99a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
100a556c76aSAlexandre Belloni }
101a556c76aSAlexandre Belloni 
102a556c76aSAlexandre Belloni static int ocelot_mact_forget(struct ocelot *ocelot,
103a556c76aSAlexandre Belloni 			      const unsigned char mac[ETH_ALEN],
104a556c76aSAlexandre Belloni 			      unsigned int vid)
105a556c76aSAlexandre Belloni {
106a556c76aSAlexandre Belloni 	ocelot_mact_select(ocelot, mac, vid);
107a556c76aSAlexandre Belloni 
108a556c76aSAlexandre Belloni 	/* Issue a forget command */
109a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
110a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
111a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
112a556c76aSAlexandre Belloni 
113a556c76aSAlexandre Belloni 	return ocelot_mact_wait_for_completion(ocelot);
114a556c76aSAlexandre Belloni }
115a556c76aSAlexandre Belloni 
116a556c76aSAlexandre Belloni static void ocelot_mact_init(struct ocelot *ocelot)
117a556c76aSAlexandre Belloni {
118a556c76aSAlexandre Belloni 	/* Configure the learning mode entries attributes:
119a556c76aSAlexandre Belloni 	 * - Do not copy the frame to the CPU extraction queues.
120a556c76aSAlexandre Belloni 	 * - Use the vlan and mac_cpoy for dmac lookup.
121a556c76aSAlexandre Belloni 	 */
122a556c76aSAlexandre Belloni 	ocelot_rmw(ocelot, 0,
123a556c76aSAlexandre Belloni 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
124a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_FWD_KILL
125a556c76aSAlexandre Belloni 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
126a556c76aSAlexandre Belloni 		   ANA_AGENCTRL);
127a556c76aSAlexandre Belloni 
128a556c76aSAlexandre Belloni 	/* Clear the MAC table */
129a556c76aSAlexandre Belloni 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
130a556c76aSAlexandre Belloni }
131a556c76aSAlexandre Belloni 
132a556c76aSAlexandre Belloni static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
133a556c76aSAlexandre Belloni {
134a556c76aSAlexandre Belloni 	unsigned int val, timeout = 10;
135a556c76aSAlexandre Belloni 
136a556c76aSAlexandre Belloni 	/* Wait for the issued mac table command to be completed, or timeout.
137a556c76aSAlexandre Belloni 	 * When the command read from ANA_TABLES_MACACCESS is
138a556c76aSAlexandre Belloni 	 * MACACCESS_CMD_IDLE, the issued command completed successfully.
139a556c76aSAlexandre Belloni 	 */
140a556c76aSAlexandre Belloni 	do {
141a556c76aSAlexandre Belloni 		val = ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
142a556c76aSAlexandre Belloni 		val &= ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M;
143a556c76aSAlexandre Belloni 	} while (val != ANA_TABLES_VLANACCESS_CMD_IDLE && timeout--);
144a556c76aSAlexandre Belloni 
145a556c76aSAlexandre Belloni 	if (!timeout)
146a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
147a556c76aSAlexandre Belloni 
148a556c76aSAlexandre Belloni 	return 0;
149a556c76aSAlexandre Belloni }
150a556c76aSAlexandre Belloni 
151*7142529fSAntoine Tenart static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
152*7142529fSAntoine Tenart {
153*7142529fSAntoine Tenart 	/* Select the VID to configure */
154*7142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
155*7142529fSAntoine Tenart 		     ANA_TABLES_VLANTIDX);
156*7142529fSAntoine Tenart 	/* Set the vlan port members mask and issue a write command */
157*7142529fSAntoine Tenart 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
158*7142529fSAntoine Tenart 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
159*7142529fSAntoine Tenart 		     ANA_TABLES_VLANACCESS);
160*7142529fSAntoine Tenart 
161*7142529fSAntoine Tenart 	return ocelot_vlant_wait_for_completion(ocelot);
162*7142529fSAntoine Tenart }
163*7142529fSAntoine Tenart 
164*7142529fSAntoine Tenart static void ocelot_vlan_mode(struct ocelot_port *port,
165*7142529fSAntoine Tenart 			     netdev_features_t features)
166*7142529fSAntoine Tenart {
167*7142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
168*7142529fSAntoine Tenart 	u8 p = port->chip_port;
169*7142529fSAntoine Tenart 	u32 val;
170*7142529fSAntoine Tenart 
171*7142529fSAntoine Tenart 	/* Filtering */
172*7142529fSAntoine Tenart 	val = ocelot_read(ocelot, ANA_VLANMASK);
173*7142529fSAntoine Tenart 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
174*7142529fSAntoine Tenart 		val |= BIT(p);
175*7142529fSAntoine Tenart 	else
176*7142529fSAntoine Tenart 		val &= ~BIT(p);
177*7142529fSAntoine Tenart 	ocelot_write(ocelot, val, ANA_VLANMASK);
178*7142529fSAntoine Tenart }
179*7142529fSAntoine Tenart 
180*7142529fSAntoine Tenart static void ocelot_vlan_port_apply(struct ocelot *ocelot,
181*7142529fSAntoine Tenart 				   struct ocelot_port *port)
182*7142529fSAntoine Tenart {
183*7142529fSAntoine Tenart 	u32 val;
184*7142529fSAntoine Tenart 
185*7142529fSAntoine Tenart 	/* Ingress clasification (ANA_PORT_VLAN_CFG) */
186*7142529fSAntoine Tenart 	/* Default vlan to clasify for untagged frames (may be zero) */
187*7142529fSAntoine Tenart 	val = ANA_PORT_VLAN_CFG_VLAN_VID(port->pvid);
188*7142529fSAntoine Tenart 	if (port->vlan_aware)
189*7142529fSAntoine Tenart 		val |= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
190*7142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
191*7142529fSAntoine Tenart 
192*7142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
193*7142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_VID_M |
194*7142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
195*7142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
196*7142529fSAntoine Tenart 		       ANA_PORT_VLAN_CFG, port->chip_port);
197*7142529fSAntoine Tenart 
198*7142529fSAntoine Tenart 	/* Drop frames with multicast source address */
199*7142529fSAntoine Tenart 	val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
200*7142529fSAntoine Tenart 	if (port->vlan_aware && !port->vid)
201*7142529fSAntoine Tenart 		/* If port is vlan-aware and tagged, drop untagged and priority
202*7142529fSAntoine Tenart 		 * tagged frames.
203*7142529fSAntoine Tenart 		 */
204*7142529fSAntoine Tenart 		val |= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
205*7142529fSAntoine Tenart 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
206*7142529fSAntoine Tenart 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
207*7142529fSAntoine Tenart 	ocelot_write_gix(ocelot, val, ANA_PORT_DROP_CFG, port->chip_port);
208*7142529fSAntoine Tenart 
209*7142529fSAntoine Tenart 	/* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q. */
210*7142529fSAntoine Tenart 	val = REW_TAG_CFG_TAG_TPID_CFG(0);
211*7142529fSAntoine Tenart 
212*7142529fSAntoine Tenart 	if (port->vlan_aware) {
213*7142529fSAntoine Tenart 		if (port->vid)
214*7142529fSAntoine Tenart 			/* Tag all frames except when VID == DEFAULT_VLAN */
215*7142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(1);
216*7142529fSAntoine Tenart 		else
217*7142529fSAntoine Tenart 			/* Tag all frames */
218*7142529fSAntoine Tenart 			val |= REW_TAG_CFG_TAG_CFG(3);
219*7142529fSAntoine Tenart 	}
220*7142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
221*7142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_TPID_CFG_M |
222*7142529fSAntoine Tenart 		       REW_TAG_CFG_TAG_CFG_M,
223*7142529fSAntoine Tenart 		       REW_TAG_CFG, port->chip_port);
224*7142529fSAntoine Tenart 
225*7142529fSAntoine Tenart 	/* Set default VLAN and tag type to 8021Q. */
226*7142529fSAntoine Tenart 	val = REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q) |
227*7142529fSAntoine Tenart 	      REW_PORT_VLAN_CFG_PORT_VID(port->vid);
228*7142529fSAntoine Tenart 	ocelot_rmw_gix(ocelot, val,
229*7142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_TPID_M |
230*7142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG_PORT_VID_M,
231*7142529fSAntoine Tenart 		       REW_PORT_VLAN_CFG, port->chip_port);
232*7142529fSAntoine Tenart }
233*7142529fSAntoine Tenart 
234*7142529fSAntoine Tenart static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
235*7142529fSAntoine Tenart 			       bool untagged)
236*7142529fSAntoine Tenart {
237*7142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
238*7142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
239*7142529fSAntoine Tenart 	int ret;
240*7142529fSAntoine Tenart 
241*7142529fSAntoine Tenart 	/* Add the port MAC address to with the right VLAN information */
242*7142529fSAntoine Tenart 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
243*7142529fSAntoine Tenart 			  ENTRYTYPE_LOCKED);
244*7142529fSAntoine Tenart 
245*7142529fSAntoine Tenart 	/* Make the port a member of the VLAN */
246*7142529fSAntoine Tenart 	ocelot->vlan_mask[vid] |= BIT(port->chip_port);
247*7142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
248*7142529fSAntoine Tenart 	if (ret)
249*7142529fSAntoine Tenart 		return ret;
250*7142529fSAntoine Tenart 
251*7142529fSAntoine Tenart 	/* Default ingress vlan classification */
252*7142529fSAntoine Tenart 	if (pvid)
253*7142529fSAntoine Tenart 		port->pvid = vid;
254*7142529fSAntoine Tenart 
255*7142529fSAntoine Tenart 	/* Untagged egress vlan clasification */
256*7142529fSAntoine Tenart 	if (untagged)
257*7142529fSAntoine Tenart 		port->vid = vid;
258*7142529fSAntoine Tenart 
259*7142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
260*7142529fSAntoine Tenart 
261*7142529fSAntoine Tenart 	return 0;
262*7142529fSAntoine Tenart }
263*7142529fSAntoine Tenart 
264*7142529fSAntoine Tenart static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
265*7142529fSAntoine Tenart {
266*7142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
267*7142529fSAntoine Tenart 	struct ocelot *ocelot = port->ocelot;
268*7142529fSAntoine Tenart 	int ret;
269*7142529fSAntoine Tenart 
270*7142529fSAntoine Tenart 	/* 8021q removes VID 0 on module unload for all interfaces
271*7142529fSAntoine Tenart 	 * with VLAN filtering feature. We need to keep it to receive
272*7142529fSAntoine Tenart 	 * untagged traffic.
273*7142529fSAntoine Tenart 	 */
274*7142529fSAntoine Tenart 	if (vid == 0)
275*7142529fSAntoine Tenart 		return 0;
276*7142529fSAntoine Tenart 
277*7142529fSAntoine Tenart 	/* Del the port MAC address to with the right VLAN information */
278*7142529fSAntoine Tenart 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
279*7142529fSAntoine Tenart 
280*7142529fSAntoine Tenart 	/* Stop the port from being a member of the vlan */
281*7142529fSAntoine Tenart 	ocelot->vlan_mask[vid] &= ~BIT(port->chip_port);
282*7142529fSAntoine Tenart 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
283*7142529fSAntoine Tenart 	if (ret)
284*7142529fSAntoine Tenart 		return ret;
285*7142529fSAntoine Tenart 
286*7142529fSAntoine Tenart 	/* Ingress */
287*7142529fSAntoine Tenart 	if (port->pvid == vid)
288*7142529fSAntoine Tenart 		port->pvid = 0;
289*7142529fSAntoine Tenart 
290*7142529fSAntoine Tenart 	/* Egress */
291*7142529fSAntoine Tenart 	if (port->vid == vid)
292*7142529fSAntoine Tenart 		port->vid = 0;
293*7142529fSAntoine Tenart 
294*7142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, port);
295*7142529fSAntoine Tenart 
296*7142529fSAntoine Tenart 	return 0;
297*7142529fSAntoine Tenart }
298*7142529fSAntoine Tenart 
299a556c76aSAlexandre Belloni static void ocelot_vlan_init(struct ocelot *ocelot)
300a556c76aSAlexandre Belloni {
301*7142529fSAntoine Tenart 	u16 port, vid;
302*7142529fSAntoine Tenart 
303a556c76aSAlexandre Belloni 	/* Clear VLAN table, by default all ports are members of all VLANs */
304a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
305a556c76aSAlexandre Belloni 		     ANA_TABLES_VLANACCESS);
306a556c76aSAlexandre Belloni 	ocelot_vlant_wait_for_completion(ocelot);
307*7142529fSAntoine Tenart 
308*7142529fSAntoine Tenart 	/* Configure the port VLAN memberships */
309*7142529fSAntoine Tenart 	for (vid = 1; vid < VLAN_N_VID; vid++) {
310*7142529fSAntoine Tenart 		ocelot->vlan_mask[vid] = 0;
311*7142529fSAntoine Tenart 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
312*7142529fSAntoine Tenart 	}
313*7142529fSAntoine Tenart 
314*7142529fSAntoine Tenart 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
315*7142529fSAntoine Tenart 	 * traffic.  It is added automatically if 8021q module is loaded, but
316*7142529fSAntoine Tenart 	 * we can't rely on it since module may be not loaded.
317*7142529fSAntoine Tenart 	 */
318*7142529fSAntoine Tenart 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
319*7142529fSAntoine Tenart 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
320*7142529fSAntoine Tenart 
321*7142529fSAntoine Tenart 	/* Configure the CPU port to be VLAN aware */
322*7142529fSAntoine Tenart 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
323*7142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
324*7142529fSAntoine Tenart 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
325*7142529fSAntoine Tenart 			 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
326*7142529fSAntoine Tenart 
327*7142529fSAntoine Tenart 	/* Set vlan ingress filter mask to all ports but the CPU port by
328*7142529fSAntoine Tenart 	 * default.
329*7142529fSAntoine Tenart 	 */
330*7142529fSAntoine Tenart 	ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
331*7142529fSAntoine Tenart 
332*7142529fSAntoine Tenart 	for (port = 0; port < ocelot->num_phys_ports; port++) {
333*7142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
334*7142529fSAntoine Tenart 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
335*7142529fSAntoine Tenart 	}
336a556c76aSAlexandre Belloni }
337a556c76aSAlexandre Belloni 
338a556c76aSAlexandre Belloni /* Watermark encode
339a556c76aSAlexandre Belloni  * Bit 8:   Unit; 0:1, 1:16
340a556c76aSAlexandre Belloni  * Bit 7-0: Value to be multiplied with unit
341a556c76aSAlexandre Belloni  */
342a556c76aSAlexandre Belloni static u16 ocelot_wm_enc(u16 value)
343a556c76aSAlexandre Belloni {
344a556c76aSAlexandre Belloni 	if (value >= BIT(8))
345a556c76aSAlexandre Belloni 		return BIT(8) | (value / 16);
346a556c76aSAlexandre Belloni 
347a556c76aSAlexandre Belloni 	return value;
348a556c76aSAlexandre Belloni }
349a556c76aSAlexandre Belloni 
350a556c76aSAlexandre Belloni static void ocelot_port_adjust_link(struct net_device *dev)
351a556c76aSAlexandre Belloni {
352a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
353a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
354a556c76aSAlexandre Belloni 	u8 p = port->chip_port;
355a556c76aSAlexandre Belloni 	int speed, atop_wm, mode = 0;
356a556c76aSAlexandre Belloni 
357a556c76aSAlexandre Belloni 	switch (dev->phydev->speed) {
358a556c76aSAlexandre Belloni 	case SPEED_10:
359a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_10;
360a556c76aSAlexandre Belloni 		break;
361a556c76aSAlexandre Belloni 	case SPEED_100:
362a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_100;
363a556c76aSAlexandre Belloni 		break;
364a556c76aSAlexandre Belloni 	case SPEED_1000:
365a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_1000;
366a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
367a556c76aSAlexandre Belloni 		break;
368a556c76aSAlexandre Belloni 	case SPEED_2500:
369a556c76aSAlexandre Belloni 		speed = OCELOT_SPEED_2500;
370a556c76aSAlexandre Belloni 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
371a556c76aSAlexandre Belloni 		break;
372a556c76aSAlexandre Belloni 	default:
373a556c76aSAlexandre Belloni 		netdev_err(dev, "Unsupported PHY speed: %d\n",
374a556c76aSAlexandre Belloni 			   dev->phydev->speed);
375a556c76aSAlexandre Belloni 		return;
376a556c76aSAlexandre Belloni 	}
377a556c76aSAlexandre Belloni 
378a556c76aSAlexandre Belloni 	phy_print_status(dev->phydev);
379a556c76aSAlexandre Belloni 
380a556c76aSAlexandre Belloni 	if (!dev->phydev->link)
381a556c76aSAlexandre Belloni 		return;
382a556c76aSAlexandre Belloni 
383a556c76aSAlexandre Belloni 	/* Only full duplex supported for now */
384a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
385a556c76aSAlexandre Belloni 			   mode, DEV_MAC_MODE_CFG);
386a556c76aSAlexandre Belloni 
387a556c76aSAlexandre Belloni 	/* Set MAC IFG Gaps
388a556c76aSAlexandre Belloni 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
389a556c76aSAlexandre Belloni 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
390a556c76aSAlexandre Belloni 	 */
391a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
392a556c76aSAlexandre Belloni 
393a556c76aSAlexandre Belloni 	/* Load seed (0) and set MAC HDX late collision  */
394a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
395a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG_SEED_LOAD,
396a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
397a556c76aSAlexandre Belloni 	mdelay(1);
398a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
399a556c76aSAlexandre Belloni 			   DEV_MAC_HDX_CFG);
400a556c76aSAlexandre Belloni 
401a556c76aSAlexandre Belloni 	/* Disable HDX fast control */
402a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
403a556c76aSAlexandre Belloni 
404a556c76aSAlexandre Belloni 	/* SGMII only for now */
405a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
406a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
407a556c76aSAlexandre Belloni 
408a556c76aSAlexandre Belloni 	/* Enable PCS */
409a556c76aSAlexandre Belloni 	ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
410a556c76aSAlexandre Belloni 
411a556c76aSAlexandre Belloni 	/* No aneg on SGMII */
412a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
413a556c76aSAlexandre Belloni 
414a556c76aSAlexandre Belloni 	/* No loopback */
415a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, PCS1G_LB_CFG);
416a556c76aSAlexandre Belloni 
417a556c76aSAlexandre Belloni 	/* Set Max Length and maximum tags allowed */
418a556c76aSAlexandre Belloni 	ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
419a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
420a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
421a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
422a556c76aSAlexandre Belloni 			   DEV_MAC_TAGS_CFG);
423a556c76aSAlexandre Belloni 
424a556c76aSAlexandre Belloni 	/* Enable MAC module */
425a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
426a556c76aSAlexandre Belloni 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
427a556c76aSAlexandre Belloni 
428a556c76aSAlexandre Belloni 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
429a556c76aSAlexandre Belloni 	 * reset */
430a556c76aSAlexandre Belloni 	ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
431a556c76aSAlexandre Belloni 			   DEV_CLOCK_CFG);
432a556c76aSAlexandre Belloni 
433a556c76aSAlexandre Belloni 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
434a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
435a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
436a556c76aSAlexandre Belloni 
437a556c76aSAlexandre Belloni 	/* No PFC */
438a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
439a556c76aSAlexandre Belloni 			 ANA_PFC_PFC_CFG, p);
440a556c76aSAlexandre Belloni 
441a556c76aSAlexandre Belloni 	/* Set Pause WM hysteresis
442a556c76aSAlexandre Belloni 	 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
443a556c76aSAlexandre Belloni 	 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
444a556c76aSAlexandre Belloni 	 */
445a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
446a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
447a556c76aSAlexandre Belloni 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
448a556c76aSAlexandre Belloni 
449a556c76aSAlexandre Belloni 	/* Core: Enable port for frame transfer */
450a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
451a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
452a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
453a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, p);
454a556c76aSAlexandre Belloni 
455a556c76aSAlexandre Belloni 	/* Flow control */
456a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
457a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
458a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
459a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
460a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
461a556c76aSAlexandre Belloni 			 SYS_MAC_FC_CFG, p);
462a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
463a556c76aSAlexandre Belloni 
464a556c76aSAlexandre Belloni 	/* Tail dropping watermark */
465a556c76aSAlexandre Belloni 	atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
466a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
467a556c76aSAlexandre Belloni 			 SYS_ATOP, p);
468a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
469a556c76aSAlexandre Belloni }
470a556c76aSAlexandre Belloni 
471a556c76aSAlexandre Belloni static int ocelot_port_open(struct net_device *dev)
472a556c76aSAlexandre Belloni {
473a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
474a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
475a556c76aSAlexandre Belloni 	int err;
476a556c76aSAlexandre Belloni 
477a556c76aSAlexandre Belloni 	/* Enable receiving frames on the port, and activate auto-learning of
478a556c76aSAlexandre Belloni 	 * MAC addresses.
479a556c76aSAlexandre Belloni 	 */
480a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
481a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_RECV_ENA |
482a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
483a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, port->chip_port);
484a556c76aSAlexandre Belloni 
485a556c76aSAlexandre Belloni 	err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
486a556c76aSAlexandre Belloni 				 PHY_INTERFACE_MODE_NA);
487a556c76aSAlexandre Belloni 	if (err) {
488a556c76aSAlexandre Belloni 		netdev_err(dev, "Could not attach to PHY\n");
489a556c76aSAlexandre Belloni 		return err;
490a556c76aSAlexandre Belloni 	}
491a556c76aSAlexandre Belloni 
492a556c76aSAlexandre Belloni 	dev->phydev = port->phy;
493a556c76aSAlexandre Belloni 
494a556c76aSAlexandre Belloni 	phy_attached_info(port->phy);
495a556c76aSAlexandre Belloni 	phy_start(port->phy);
496a556c76aSAlexandre Belloni 	return 0;
497a556c76aSAlexandre Belloni }
498a556c76aSAlexandre Belloni 
499a556c76aSAlexandre Belloni static int ocelot_port_stop(struct net_device *dev)
500a556c76aSAlexandre Belloni {
501a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
502a556c76aSAlexandre Belloni 
503a556c76aSAlexandre Belloni 	phy_disconnect(port->phy);
504a556c76aSAlexandre Belloni 
505a556c76aSAlexandre Belloni 	dev->phydev = NULL;
506a556c76aSAlexandre Belloni 
507a556c76aSAlexandre Belloni 	ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
508a556c76aSAlexandre Belloni 	ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
509a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, port->chip_port);
510a556c76aSAlexandre Belloni 	return 0;
511a556c76aSAlexandre Belloni }
512a556c76aSAlexandre Belloni 
513a556c76aSAlexandre Belloni /* Generate the IFH for frame injection
514a556c76aSAlexandre Belloni  *
515a556c76aSAlexandre Belloni  * The IFH is a 128bit-value
516a556c76aSAlexandre Belloni  * bit 127: bypass the analyzer processing
517a556c76aSAlexandre Belloni  * bit 56-67: destination mask
518a556c76aSAlexandre Belloni  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
519a556c76aSAlexandre Belloni  * bit 20-27: cpu extraction queue mask
520a556c76aSAlexandre Belloni  * bit 16: tag type 0: C-tag, 1: S-tag
521a556c76aSAlexandre Belloni  * bit 0-11: VID
522a556c76aSAlexandre Belloni  */
523a556c76aSAlexandre Belloni static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
524a556c76aSAlexandre Belloni {
525a556c76aSAlexandre Belloni 	ifh[0] = IFH_INJ_BYPASS;
52608d02364SAntoine Tenart 	ifh[1] = (0xf00 & info->port) >> 8;
527a556c76aSAlexandre Belloni 	ifh[2] = (0xff & info->port) << 24;
52808d02364SAntoine Tenart 	ifh[3] = (info->tag_type << 16) | info->vid;
529a556c76aSAlexandre Belloni 
530a556c76aSAlexandre Belloni 	return 0;
531a556c76aSAlexandre Belloni }
532a556c76aSAlexandre Belloni 
533a556c76aSAlexandre Belloni static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
534a556c76aSAlexandre Belloni {
535a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
536a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
537a556c76aSAlexandre Belloni 	u32 val, ifh[IFH_LEN];
538a556c76aSAlexandre Belloni 	struct frame_info info = {};
539a556c76aSAlexandre Belloni 	u8 grp = 0; /* Send everything on CPU group 0 */
540a556c76aSAlexandre Belloni 	unsigned int i, count, last;
541a556c76aSAlexandre Belloni 
542a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, QS_INJ_STATUS);
543a556c76aSAlexandre Belloni 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
544a556c76aSAlexandre Belloni 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
545a556c76aSAlexandre Belloni 		return NETDEV_TX_BUSY;
546a556c76aSAlexandre Belloni 
547a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
548a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
549a556c76aSAlexandre Belloni 
550a556c76aSAlexandre Belloni 	info.port = BIT(port->chip_port);
55108d02364SAntoine Tenart 	info.tag_type = IFH_TAG_TYPE_C;
55208d02364SAntoine Tenart 	info.vid = skb_vlan_tag_get(skb);
553a556c76aSAlexandre Belloni 	ocelot_gen_ifh(ifh, &info);
554a556c76aSAlexandre Belloni 
555a556c76aSAlexandre Belloni 	for (i = 0; i < IFH_LEN; i++)
556c2cd650bSAntoine Tenart 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
557c2cd650bSAntoine Tenart 				 QS_INJ_WR, grp);
558a556c76aSAlexandre Belloni 
559a556c76aSAlexandre Belloni 	count = (skb->len + 3) / 4;
560a556c76aSAlexandre Belloni 	last = skb->len % 4;
561a556c76aSAlexandre Belloni 	for (i = 0; i < count; i++) {
562a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
563a556c76aSAlexandre Belloni 	}
564a556c76aSAlexandre Belloni 
565a556c76aSAlexandre Belloni 	/* Add padding */
566a556c76aSAlexandre Belloni 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
567a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
568a556c76aSAlexandre Belloni 		i++;
569a556c76aSAlexandre Belloni 	}
570a556c76aSAlexandre Belloni 
571a556c76aSAlexandre Belloni 	/* Indicate EOF and valid bytes in last word */
572a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
573a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
574a556c76aSAlexandre Belloni 			 QS_INJ_CTRL_EOF,
575a556c76aSAlexandre Belloni 			 QS_INJ_CTRL, grp);
576a556c76aSAlexandre Belloni 
577a556c76aSAlexandre Belloni 	/* Add dummy CRC */
578a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
579a556c76aSAlexandre Belloni 	skb_tx_timestamp(skb);
580a556c76aSAlexandre Belloni 
581a556c76aSAlexandre Belloni 	dev->stats.tx_packets++;
582a556c76aSAlexandre Belloni 	dev->stats.tx_bytes += skb->len;
583a556c76aSAlexandre Belloni 	dev_kfree_skb_any(skb);
584a556c76aSAlexandre Belloni 
585a556c76aSAlexandre Belloni 	return NETDEV_TX_OK;
586a556c76aSAlexandre Belloni }
587a556c76aSAlexandre Belloni 
588a556c76aSAlexandre Belloni static void ocelot_mact_mc_reset(struct ocelot_port *port)
589a556c76aSAlexandre Belloni {
590a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
591a556c76aSAlexandre Belloni 	struct netdev_hw_addr *ha, *n;
592a556c76aSAlexandre Belloni 
593a556c76aSAlexandre Belloni 	/* Free and forget all the MAC addresses stored in the port private mc
594a556c76aSAlexandre Belloni 	 * list. These are mc addresses that were previously added by calling
595a556c76aSAlexandre Belloni 	 * ocelot_mact_mc_add().
596a556c76aSAlexandre Belloni 	 */
597a556c76aSAlexandre Belloni 	list_for_each_entry_safe(ha, n, &port->mc, list) {
598a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, ha->addr, port->pvid);
599a556c76aSAlexandre Belloni 		list_del(&ha->list);
600a556c76aSAlexandre Belloni 		kfree(ha);
601a556c76aSAlexandre Belloni 	}
602a556c76aSAlexandre Belloni }
603a556c76aSAlexandre Belloni 
604a556c76aSAlexandre Belloni static int ocelot_mact_mc_add(struct ocelot_port *port,
605a556c76aSAlexandre Belloni 			      struct netdev_hw_addr *hw_addr)
606a556c76aSAlexandre Belloni {
607a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
608a556c76aSAlexandre Belloni 	struct netdev_hw_addr *ha = kzalloc(sizeof(*ha), GFP_KERNEL);
609a556c76aSAlexandre Belloni 
610a556c76aSAlexandre Belloni 	if (!ha)
611a556c76aSAlexandre Belloni 		return -ENOMEM;
612a556c76aSAlexandre Belloni 
613a556c76aSAlexandre Belloni 	memcpy(ha, hw_addr, sizeof(*ha));
614a556c76aSAlexandre Belloni 	list_add_tail(&ha->list, &port->mc);
615a556c76aSAlexandre Belloni 
616a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, ha->addr, port->pvid,
617a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
618a556c76aSAlexandre Belloni 
619a556c76aSAlexandre Belloni 	return 0;
620a556c76aSAlexandre Belloni }
621a556c76aSAlexandre Belloni 
622a556c76aSAlexandre Belloni static void ocelot_set_rx_mode(struct net_device *dev)
623a556c76aSAlexandre Belloni {
624a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
625a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
626a556c76aSAlexandre Belloni 	struct netdev_hw_addr *ha;
627a556c76aSAlexandre Belloni 	int i;
628a556c76aSAlexandre Belloni 	u32 val;
629a556c76aSAlexandre Belloni 
630a556c76aSAlexandre Belloni 	/* This doesn't handle promiscuous mode because the bridge core is
631a556c76aSAlexandre Belloni 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
632a556c76aSAlexandre Belloni 	 * forwarded to the CPU port.
633a556c76aSAlexandre Belloni 	 */
634a556c76aSAlexandre Belloni 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
635a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
636a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
637a556c76aSAlexandre Belloni 
638a556c76aSAlexandre Belloni 	/* Handle the device multicast addresses. First remove all the
639a556c76aSAlexandre Belloni 	 * previously installed addresses and then add the latest ones to the
640a556c76aSAlexandre Belloni 	 * mac table.
641a556c76aSAlexandre Belloni 	 */
642a556c76aSAlexandre Belloni 	ocelot_mact_mc_reset(port);
643a556c76aSAlexandre Belloni 	netdev_for_each_mc_addr(ha, dev)
644a556c76aSAlexandre Belloni 		ocelot_mact_mc_add(port, ha);
645a556c76aSAlexandre Belloni }
646a556c76aSAlexandre Belloni 
647a556c76aSAlexandre Belloni static int ocelot_port_get_phys_port_name(struct net_device *dev,
648a556c76aSAlexandre Belloni 					  char *buf, size_t len)
649a556c76aSAlexandre Belloni {
650a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
651a556c76aSAlexandre Belloni 	int ret;
652a556c76aSAlexandre Belloni 
653a556c76aSAlexandre Belloni 	ret = snprintf(buf, len, "p%d", port->chip_port);
654a556c76aSAlexandre Belloni 	if (ret >= len)
655a556c76aSAlexandre Belloni 		return -EINVAL;
656a556c76aSAlexandre Belloni 
657a556c76aSAlexandre Belloni 	return 0;
658a556c76aSAlexandre Belloni }
659a556c76aSAlexandre Belloni 
660a556c76aSAlexandre Belloni static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
661a556c76aSAlexandre Belloni {
662a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
663a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
664a556c76aSAlexandre Belloni 	const struct sockaddr *addr = p;
665a556c76aSAlexandre Belloni 
666a556c76aSAlexandre Belloni 	/* Learn the new net device MAC address in the mac table. */
667a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
668a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
669a556c76aSAlexandre Belloni 	/* Then forget the previous one. */
670a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
671a556c76aSAlexandre Belloni 
672a556c76aSAlexandre Belloni 	ether_addr_copy(dev->dev_addr, addr->sa_data);
673a556c76aSAlexandre Belloni 	return 0;
674a556c76aSAlexandre Belloni }
675a556c76aSAlexandre Belloni 
676a556c76aSAlexandre Belloni static void ocelot_get_stats64(struct net_device *dev,
677a556c76aSAlexandre Belloni 			       struct rtnl_link_stats64 *stats)
678a556c76aSAlexandre Belloni {
679a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
680a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
681a556c76aSAlexandre Belloni 
682a556c76aSAlexandre Belloni 	/* Configure the port to read the stats from */
683a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
684a556c76aSAlexandre Belloni 		     SYS_STAT_CFG);
685a556c76aSAlexandre Belloni 
686a556c76aSAlexandre Belloni 	/* Get Rx stats */
687a556c76aSAlexandre Belloni 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
688a556c76aSAlexandre Belloni 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
689a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
690a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
691a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
692a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
693a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
694a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
695a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
696a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
697a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
698a556c76aSAlexandre Belloni 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
699a556c76aSAlexandre Belloni 	stats->rx_dropped = dev->stats.rx_dropped;
700a556c76aSAlexandre Belloni 
701a556c76aSAlexandre Belloni 	/* Get Tx stats */
702a556c76aSAlexandre Belloni 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
703a556c76aSAlexandre Belloni 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
704a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
705a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
706a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
707a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
708a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
709a556c76aSAlexandre Belloni 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
710a556c76aSAlexandre Belloni 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
711a556c76aSAlexandre Belloni 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
712a556c76aSAlexandre Belloni }
713a556c76aSAlexandre Belloni 
714a556c76aSAlexandre Belloni static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
715a556c76aSAlexandre Belloni 			  struct net_device *dev, const unsigned char *addr,
716a556c76aSAlexandre Belloni 			  u16 vid, u16 flags)
717a556c76aSAlexandre Belloni {
718a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
719a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
720a556c76aSAlexandre Belloni 
721*7142529fSAntoine Tenart 	if (!vid) {
722*7142529fSAntoine Tenart 		if (!port->vlan_aware)
723*7142529fSAntoine Tenart 			/* If the bridge is not VLAN aware and no VID was
724*7142529fSAntoine Tenart 			 * provided, set it to pvid to ensure the MAC entry
725*7142529fSAntoine Tenart 			 * matches incoming untagged packets
726*7142529fSAntoine Tenart 			 */
727*7142529fSAntoine Tenart 			vid = port->pvid;
728*7142529fSAntoine Tenart 		else
729*7142529fSAntoine Tenart 			/* If the bridge is VLAN aware a VID must be provided as
730*7142529fSAntoine Tenart 			 * otherwise the learnt entry wouldn't match any frame.
731*7142529fSAntoine Tenart 			 */
732*7142529fSAntoine Tenart 			return -EINVAL;
733*7142529fSAntoine Tenart 	}
734*7142529fSAntoine Tenart 
735a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
736a556c76aSAlexandre Belloni 				 ENTRYTYPE_NORMAL);
737a556c76aSAlexandre Belloni }
738a556c76aSAlexandre Belloni 
739a556c76aSAlexandre Belloni static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
740a556c76aSAlexandre Belloni 			  struct net_device *dev,
741a556c76aSAlexandre Belloni 			  const unsigned char *addr, u16 vid)
742a556c76aSAlexandre Belloni {
743a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
744a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
745a556c76aSAlexandre Belloni 
746a556c76aSAlexandre Belloni 	return ocelot_mact_forget(ocelot, addr, vid);
747a556c76aSAlexandre Belloni }
748a556c76aSAlexandre Belloni 
749a556c76aSAlexandre Belloni struct ocelot_dump_ctx {
750a556c76aSAlexandre Belloni 	struct net_device *dev;
751a556c76aSAlexandre Belloni 	struct sk_buff *skb;
752a556c76aSAlexandre Belloni 	struct netlink_callback *cb;
753a556c76aSAlexandre Belloni 	int idx;
754a556c76aSAlexandre Belloni };
755a556c76aSAlexandre Belloni 
756a556c76aSAlexandre Belloni static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
757a556c76aSAlexandre Belloni 			      struct ocelot_dump_ctx *dump)
758a556c76aSAlexandre Belloni {
759a556c76aSAlexandre Belloni 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
760a556c76aSAlexandre Belloni 	u32 seq = dump->cb->nlh->nlmsg_seq;
761a556c76aSAlexandre Belloni 	struct nlmsghdr *nlh;
762a556c76aSAlexandre Belloni 	struct ndmsg *ndm;
763a556c76aSAlexandre Belloni 
764a556c76aSAlexandre Belloni 	if (dump->idx < dump->cb->args[2])
765a556c76aSAlexandre Belloni 		goto skip;
766a556c76aSAlexandre Belloni 
767a556c76aSAlexandre Belloni 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
768a556c76aSAlexandre Belloni 			sizeof(*ndm), NLM_F_MULTI);
769a556c76aSAlexandre Belloni 	if (!nlh)
770a556c76aSAlexandre Belloni 		return -EMSGSIZE;
771a556c76aSAlexandre Belloni 
772a556c76aSAlexandre Belloni 	ndm = nlmsg_data(nlh);
773a556c76aSAlexandre Belloni 	ndm->ndm_family  = AF_BRIDGE;
774a556c76aSAlexandre Belloni 	ndm->ndm_pad1    = 0;
775a556c76aSAlexandre Belloni 	ndm->ndm_pad2    = 0;
776a556c76aSAlexandre Belloni 	ndm->ndm_flags   = NTF_SELF;
777a556c76aSAlexandre Belloni 	ndm->ndm_type    = 0;
778a556c76aSAlexandre Belloni 	ndm->ndm_ifindex = dump->dev->ifindex;
779a556c76aSAlexandre Belloni 	ndm->ndm_state   = NUD_REACHABLE;
780a556c76aSAlexandre Belloni 
781a556c76aSAlexandre Belloni 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
782a556c76aSAlexandre Belloni 		goto nla_put_failure;
783a556c76aSAlexandre Belloni 
784a556c76aSAlexandre Belloni 	if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
785a556c76aSAlexandre Belloni 		goto nla_put_failure;
786a556c76aSAlexandre Belloni 
787a556c76aSAlexandre Belloni 	nlmsg_end(dump->skb, nlh);
788a556c76aSAlexandre Belloni 
789a556c76aSAlexandre Belloni skip:
790a556c76aSAlexandre Belloni 	dump->idx++;
791a556c76aSAlexandre Belloni 	return 0;
792a556c76aSAlexandre Belloni 
793a556c76aSAlexandre Belloni nla_put_failure:
794a556c76aSAlexandre Belloni 	nlmsg_cancel(dump->skb, nlh);
795a556c76aSAlexandre Belloni 	return -EMSGSIZE;
796a556c76aSAlexandre Belloni }
797a556c76aSAlexandre Belloni 
798a556c76aSAlexandre Belloni static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
799a556c76aSAlexandre Belloni 				   struct ocelot_mact_entry *entry)
800a556c76aSAlexandre Belloni {
801a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
802a556c76aSAlexandre Belloni 	char mac[ETH_ALEN];
803a556c76aSAlexandre Belloni 	u32 val, dst, macl, mach;
804a556c76aSAlexandre Belloni 
805a556c76aSAlexandre Belloni 	/* Set row and column to read from */
806a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
807a556c76aSAlexandre Belloni 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
808a556c76aSAlexandre Belloni 
809a556c76aSAlexandre Belloni 	/* Issue a read command */
810a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
811a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
812a556c76aSAlexandre Belloni 		     ANA_TABLES_MACACCESS);
813a556c76aSAlexandre Belloni 
814a556c76aSAlexandre Belloni 	if (ocelot_mact_wait_for_completion(ocelot))
815a556c76aSAlexandre Belloni 		return -ETIMEDOUT;
816a556c76aSAlexandre Belloni 
817a556c76aSAlexandre Belloni 	/* Read the entry flags */
818a556c76aSAlexandre Belloni 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
819a556c76aSAlexandre Belloni 	if (!(val & ANA_TABLES_MACACCESS_VALID))
820a556c76aSAlexandre Belloni 		return -EINVAL;
821a556c76aSAlexandre Belloni 
822a556c76aSAlexandre Belloni 	/* If the entry read has another port configured as its destination,
823a556c76aSAlexandre Belloni 	 * do not report it.
824a556c76aSAlexandre Belloni 	 */
825a556c76aSAlexandre Belloni 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
826a556c76aSAlexandre Belloni 	if (dst != port->chip_port)
827a556c76aSAlexandre Belloni 		return -EINVAL;
828a556c76aSAlexandre Belloni 
829a556c76aSAlexandre Belloni 	/* Get the entry's MAC address and VLAN id */
830a556c76aSAlexandre Belloni 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
831a556c76aSAlexandre Belloni 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
832a556c76aSAlexandre Belloni 
833a556c76aSAlexandre Belloni 	mac[0] = (mach >> 8)  & 0xff;
834a556c76aSAlexandre Belloni 	mac[1] = (mach >> 0)  & 0xff;
835a556c76aSAlexandre Belloni 	mac[2] = (macl >> 24) & 0xff;
836a556c76aSAlexandre Belloni 	mac[3] = (macl >> 16) & 0xff;
837a556c76aSAlexandre Belloni 	mac[4] = (macl >> 8)  & 0xff;
838a556c76aSAlexandre Belloni 	mac[5] = (macl >> 0)  & 0xff;
839a556c76aSAlexandre Belloni 
840a556c76aSAlexandre Belloni 	entry->vid = (mach >> 16) & 0xfff;
841a556c76aSAlexandre Belloni 	ether_addr_copy(entry->mac, mac);
842a556c76aSAlexandre Belloni 
843a556c76aSAlexandre Belloni 	return 0;
844a556c76aSAlexandre Belloni }
845a556c76aSAlexandre Belloni 
846a556c76aSAlexandre Belloni static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
847a556c76aSAlexandre Belloni 			   struct net_device *dev,
848a556c76aSAlexandre Belloni 			   struct net_device *filter_dev, int *idx)
849a556c76aSAlexandre Belloni {
850a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
851a556c76aSAlexandre Belloni 	int i, j, ret = 0;
852a556c76aSAlexandre Belloni 	struct ocelot_dump_ctx dump = {
853a556c76aSAlexandre Belloni 		.dev = dev,
854a556c76aSAlexandre Belloni 		.skb = skb,
855a556c76aSAlexandre Belloni 		.cb = cb,
856a556c76aSAlexandre Belloni 		.idx = *idx,
857a556c76aSAlexandre Belloni 	};
858a556c76aSAlexandre Belloni 
859a556c76aSAlexandre Belloni 	struct ocelot_mact_entry entry;
860a556c76aSAlexandre Belloni 
861a556c76aSAlexandre Belloni 	/* Loop through all the mac tables entries. There are 1024 rows of 4
862a556c76aSAlexandre Belloni 	 * entries.
863a556c76aSAlexandre Belloni 	 */
864a556c76aSAlexandre Belloni 	for (i = 0; i < 1024; i++) {
865a556c76aSAlexandre Belloni 		for (j = 0; j < 4; j++) {
866a556c76aSAlexandre Belloni 			ret = ocelot_mact_read(port, i, j, &entry);
867a556c76aSAlexandre Belloni 			/* If the entry is invalid (wrong port, invalid...),
868a556c76aSAlexandre Belloni 			 * skip it.
869a556c76aSAlexandre Belloni 			 */
870a556c76aSAlexandre Belloni 			if (ret == -EINVAL)
871a556c76aSAlexandre Belloni 				continue;
872a556c76aSAlexandre Belloni 			else if (ret)
873a556c76aSAlexandre Belloni 				goto end;
874a556c76aSAlexandre Belloni 
875a556c76aSAlexandre Belloni 			ret = ocelot_fdb_do_dump(&entry, &dump);
876a556c76aSAlexandre Belloni 			if (ret)
877a556c76aSAlexandre Belloni 				goto end;
878a556c76aSAlexandre Belloni 		}
879a556c76aSAlexandre Belloni 	}
880a556c76aSAlexandre Belloni 
881a556c76aSAlexandre Belloni end:
882a556c76aSAlexandre Belloni 	*idx = dump.idx;
883a556c76aSAlexandre Belloni 	return ret;
884a556c76aSAlexandre Belloni }
885a556c76aSAlexandre Belloni 
886*7142529fSAntoine Tenart static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
887*7142529fSAntoine Tenart 				  u16 vid)
888*7142529fSAntoine Tenart {
889*7142529fSAntoine Tenart 	return ocelot_vlan_vid_add(dev, vid, false, true);
890*7142529fSAntoine Tenart }
891*7142529fSAntoine Tenart 
892*7142529fSAntoine Tenart static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
893*7142529fSAntoine Tenart 				   u16 vid)
894*7142529fSAntoine Tenart {
895*7142529fSAntoine Tenart 	return ocelot_vlan_vid_del(dev, vid);
896*7142529fSAntoine Tenart }
897*7142529fSAntoine Tenart 
898*7142529fSAntoine Tenart static int ocelot_set_features(struct net_device *dev,
899*7142529fSAntoine Tenart 			       netdev_features_t features)
900*7142529fSAntoine Tenart {
901*7142529fSAntoine Tenart 	struct ocelot_port *port = netdev_priv(dev);
902*7142529fSAntoine Tenart 	netdev_features_t changed = dev->features ^ features;
903*7142529fSAntoine Tenart 
904*7142529fSAntoine Tenart 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
905*7142529fSAntoine Tenart 		ocelot_vlan_mode(port, features);
906*7142529fSAntoine Tenart 
907*7142529fSAntoine Tenart 	return 0;
908*7142529fSAntoine Tenart }
909*7142529fSAntoine Tenart 
910a556c76aSAlexandre Belloni static const struct net_device_ops ocelot_port_netdev_ops = {
911a556c76aSAlexandre Belloni 	.ndo_open			= ocelot_port_open,
912a556c76aSAlexandre Belloni 	.ndo_stop			= ocelot_port_stop,
913a556c76aSAlexandre Belloni 	.ndo_start_xmit			= ocelot_port_xmit,
914a556c76aSAlexandre Belloni 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
915a556c76aSAlexandre Belloni 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
916a556c76aSAlexandre Belloni 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
917a556c76aSAlexandre Belloni 	.ndo_get_stats64		= ocelot_get_stats64,
918a556c76aSAlexandre Belloni 	.ndo_fdb_add			= ocelot_fdb_add,
919a556c76aSAlexandre Belloni 	.ndo_fdb_del			= ocelot_fdb_del,
920a556c76aSAlexandre Belloni 	.ndo_fdb_dump			= ocelot_fdb_dump,
921*7142529fSAntoine Tenart 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
922*7142529fSAntoine Tenart 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
923*7142529fSAntoine Tenart 	.ndo_set_features		= ocelot_set_features,
924a556c76aSAlexandre Belloni };
925a556c76aSAlexandre Belloni 
926a556c76aSAlexandre Belloni static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
927a556c76aSAlexandre Belloni {
928a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(netdev);
929a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
930a556c76aSAlexandre Belloni 	int i;
931a556c76aSAlexandre Belloni 
932a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
933a556c76aSAlexandre Belloni 		return;
934a556c76aSAlexandre Belloni 
935a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
936a556c76aSAlexandre Belloni 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
937a556c76aSAlexandre Belloni 		       ETH_GSTRING_LEN);
938a556c76aSAlexandre Belloni }
939a556c76aSAlexandre Belloni 
940a556c76aSAlexandre Belloni static void ocelot_check_stats(struct work_struct *work)
941a556c76aSAlexandre Belloni {
942a556c76aSAlexandre Belloni 	struct delayed_work *del_work = to_delayed_work(work);
943a556c76aSAlexandre Belloni 	struct ocelot *ocelot = container_of(del_work, struct ocelot, stats_work);
944a556c76aSAlexandre Belloni 	int i, j;
945a556c76aSAlexandre Belloni 
946a556c76aSAlexandre Belloni 	mutex_lock(&ocelot->stats_lock);
947a556c76aSAlexandre Belloni 
948a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++) {
949a556c76aSAlexandre Belloni 		/* Configure the port to read the stats from */
950a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
951a556c76aSAlexandre Belloni 
952a556c76aSAlexandre Belloni 		for (j = 0; j < ocelot->num_stats; j++) {
953a556c76aSAlexandre Belloni 			u32 val;
954a556c76aSAlexandre Belloni 			unsigned int idx = i * ocelot->num_stats + j;
955a556c76aSAlexandre Belloni 
956a556c76aSAlexandre Belloni 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
957a556c76aSAlexandre Belloni 					      ocelot->stats_layout[j].offset);
958a556c76aSAlexandre Belloni 
959a556c76aSAlexandre Belloni 			if (val < (ocelot->stats[idx] & U32_MAX))
960a556c76aSAlexandre Belloni 				ocelot->stats[idx] += (u64)1 << 32;
961a556c76aSAlexandre Belloni 
962a556c76aSAlexandre Belloni 			ocelot->stats[idx] = (ocelot->stats[idx] &
963a556c76aSAlexandre Belloni 					      ~(u64)U32_MAX) + val;
964a556c76aSAlexandre Belloni 		}
965a556c76aSAlexandre Belloni 	}
966a556c76aSAlexandre Belloni 
967a556c76aSAlexandre Belloni 	cancel_delayed_work(&ocelot->stats_work);
968a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
969a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
970a556c76aSAlexandre Belloni 
971a556c76aSAlexandre Belloni 	mutex_unlock(&ocelot->stats_lock);
972a556c76aSAlexandre Belloni }
973a556c76aSAlexandre Belloni 
974a556c76aSAlexandre Belloni static void ocelot_get_ethtool_stats(struct net_device *dev,
975a556c76aSAlexandre Belloni 				     struct ethtool_stats *stats, u64 *data)
976a556c76aSAlexandre Belloni {
977a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
978a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
979a556c76aSAlexandre Belloni 	int i;
980a556c76aSAlexandre Belloni 
981a556c76aSAlexandre Belloni 	/* check and update now */
982a556c76aSAlexandre Belloni 	ocelot_check_stats(&ocelot->stats_work.work);
983a556c76aSAlexandre Belloni 
984a556c76aSAlexandre Belloni 	/* Copy all counters */
985a556c76aSAlexandre Belloni 	for (i = 0; i < ocelot->num_stats; i++)
986a556c76aSAlexandre Belloni 		*data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
987a556c76aSAlexandre Belloni }
988a556c76aSAlexandre Belloni 
989a556c76aSAlexandre Belloni static int ocelot_get_sset_count(struct net_device *dev, int sset)
990a556c76aSAlexandre Belloni {
991a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
992a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
993a556c76aSAlexandre Belloni 
994a556c76aSAlexandre Belloni 	if (sset != ETH_SS_STATS)
995a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
996a556c76aSAlexandre Belloni 	return ocelot->num_stats;
997a556c76aSAlexandre Belloni }
998a556c76aSAlexandre Belloni 
999a556c76aSAlexandre Belloni static const struct ethtool_ops ocelot_ethtool_ops = {
1000a556c76aSAlexandre Belloni 	.get_strings		= ocelot_get_strings,
1001a556c76aSAlexandre Belloni 	.get_ethtool_stats	= ocelot_get_ethtool_stats,
1002a556c76aSAlexandre Belloni 	.get_sset_count		= ocelot_get_sset_count,
1003dc96ee37SAlexandre Belloni 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1004dc96ee37SAlexandre Belloni 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1005a556c76aSAlexandre Belloni };
1006a556c76aSAlexandre Belloni 
1007a556c76aSAlexandre Belloni static int ocelot_port_attr_get(struct net_device *dev,
1008a556c76aSAlexandre Belloni 				struct switchdev_attr *attr)
1009a556c76aSAlexandre Belloni {
1010a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1011a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1012a556c76aSAlexandre Belloni 
1013a556c76aSAlexandre Belloni 	switch (attr->id) {
1014a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
1015a556c76aSAlexandre Belloni 		attr->u.ppid.id_len = sizeof(ocelot->base_mac);
1016a556c76aSAlexandre Belloni 		memcpy(&attr->u.ppid.id, &ocelot->base_mac,
1017a556c76aSAlexandre Belloni 		       attr->u.ppid.id_len);
1018a556c76aSAlexandre Belloni 		break;
1019a556c76aSAlexandre Belloni 	default:
1020a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1021a556c76aSAlexandre Belloni 	}
1022a556c76aSAlexandre Belloni 
1023a556c76aSAlexandre Belloni 	return 0;
1024a556c76aSAlexandre Belloni }
1025a556c76aSAlexandre Belloni 
1026a556c76aSAlexandre Belloni static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1027a556c76aSAlexandre Belloni 					  struct switchdev_trans *trans,
1028a556c76aSAlexandre Belloni 					  u8 state)
1029a556c76aSAlexandre Belloni {
1030a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1031a556c76aSAlexandre Belloni 	u32 port_cfg;
1032a556c76aSAlexandre Belloni 	int port, i;
1033a556c76aSAlexandre Belloni 
1034a556c76aSAlexandre Belloni 	if (switchdev_trans_ph_prepare(trans))
1035a556c76aSAlexandre Belloni 		return 0;
1036a556c76aSAlexandre Belloni 
1037a556c76aSAlexandre Belloni 	if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1038a556c76aSAlexandre Belloni 		return 0;
1039a556c76aSAlexandre Belloni 
1040a556c76aSAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1041a556c76aSAlexandre Belloni 				   ocelot_port->chip_port);
1042a556c76aSAlexandre Belloni 
1043a556c76aSAlexandre Belloni 	switch (state) {
1044a556c76aSAlexandre Belloni 	case BR_STATE_FORWARDING:
1045a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1046a556c76aSAlexandre Belloni 		/* Fallthrough */
1047a556c76aSAlexandre Belloni 	case BR_STATE_LEARNING:
1048a556c76aSAlexandre Belloni 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1049a556c76aSAlexandre Belloni 		break;
1050a556c76aSAlexandre Belloni 
1051a556c76aSAlexandre Belloni 	default:
1052a556c76aSAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1053a556c76aSAlexandre Belloni 		ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1054a556c76aSAlexandre Belloni 		break;
1055a556c76aSAlexandre Belloni 	}
1056a556c76aSAlexandre Belloni 
1057a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1058a556c76aSAlexandre Belloni 			 ocelot_port->chip_port);
1059a556c76aSAlexandre Belloni 
1060a556c76aSAlexandre Belloni 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1061a556c76aSAlexandre Belloni 	 * a source for the other ports.
1062a556c76aSAlexandre Belloni 	 */
1063a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1064a556c76aSAlexandre Belloni 		if (ocelot->bridge_fwd_mask & BIT(port)) {
1065a556c76aSAlexandre Belloni 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1066a556c76aSAlexandre Belloni 
1067a556c76aSAlexandre Belloni 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1068a556c76aSAlexandre Belloni 				unsigned long bond_mask = ocelot->lags[i];
1069a556c76aSAlexandre Belloni 
1070a556c76aSAlexandre Belloni 				if (!bond_mask)
1071a556c76aSAlexandre Belloni 					continue;
1072a556c76aSAlexandre Belloni 
1073a556c76aSAlexandre Belloni 				if (bond_mask & BIT(port)) {
1074a556c76aSAlexandre Belloni 					mask &= ~bond_mask;
1075a556c76aSAlexandre Belloni 					break;
1076a556c76aSAlexandre Belloni 				}
1077a556c76aSAlexandre Belloni 			}
1078a556c76aSAlexandre Belloni 
1079a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1080a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports) | mask,
1081a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1082a556c76aSAlexandre Belloni 		} else {
1083a556c76aSAlexandre Belloni 			/* Only the CPU port, this is compatible with link
1084a556c76aSAlexandre Belloni 			 * aggregation.
1085a556c76aSAlexandre Belloni 			 */
1086a556c76aSAlexandre Belloni 			ocelot_write_rix(ocelot,
1087a556c76aSAlexandre Belloni 					 BIT(ocelot->num_phys_ports),
1088a556c76aSAlexandre Belloni 					 ANA_PGID_PGID, PGID_SRC + port);
1089a556c76aSAlexandre Belloni 		}
1090a556c76aSAlexandre Belloni 	}
1091a556c76aSAlexandre Belloni 
1092a556c76aSAlexandre Belloni 	return 0;
1093a556c76aSAlexandre Belloni }
1094a556c76aSAlexandre Belloni 
1095a556c76aSAlexandre Belloni static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1096a556c76aSAlexandre Belloni 					unsigned long ageing_clock_t)
1097a556c76aSAlexandre Belloni {
1098a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1099a556c76aSAlexandre Belloni 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1100a556c76aSAlexandre Belloni 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1101a556c76aSAlexandre Belloni 
1102a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1103a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1104a556c76aSAlexandre Belloni }
1105a556c76aSAlexandre Belloni 
1106a556c76aSAlexandre Belloni static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1107a556c76aSAlexandre Belloni {
1108a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1109a556c76aSAlexandre Belloni 	u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1110a556c76aSAlexandre Belloni 				  port->chip_port);
1111a556c76aSAlexandre Belloni 
1112a556c76aSAlexandre Belloni 	if (mc)
1113a556c76aSAlexandre Belloni 		val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1114a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1115a556c76aSAlexandre Belloni 		       ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1116a556c76aSAlexandre Belloni 	else
1117a556c76aSAlexandre Belloni 		val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1118a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1119a556c76aSAlexandre Belloni 			 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1120a556c76aSAlexandre Belloni 
1121a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1122a556c76aSAlexandre Belloni }
1123a556c76aSAlexandre Belloni 
1124a556c76aSAlexandre Belloni static int ocelot_port_attr_set(struct net_device *dev,
1125a556c76aSAlexandre Belloni 				const struct switchdev_attr *attr,
1126a556c76aSAlexandre Belloni 				struct switchdev_trans *trans)
1127a556c76aSAlexandre Belloni {
1128a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1129a556c76aSAlexandre Belloni 	int err = 0;
1130a556c76aSAlexandre Belloni 
1131a556c76aSAlexandre Belloni 	switch (attr->id) {
1132a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1133a556c76aSAlexandre Belloni 		ocelot_port_attr_stp_state_set(ocelot_port, trans,
1134a556c76aSAlexandre Belloni 					       attr->u.stp_state);
1135a556c76aSAlexandre Belloni 		break;
1136a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1137a556c76aSAlexandre Belloni 		ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1138a556c76aSAlexandre Belloni 		break;
1139*7142529fSAntoine Tenart 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1140*7142529fSAntoine Tenart 		ocelot_port->vlan_aware = attr->u.vlan_filtering;
1141*7142529fSAntoine Tenart 		ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port);
1142*7142529fSAntoine Tenart 		break;
1143a556c76aSAlexandre Belloni 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1144a556c76aSAlexandre Belloni 		ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1145a556c76aSAlexandre Belloni 		break;
1146a556c76aSAlexandre Belloni 	default:
1147a556c76aSAlexandre Belloni 		err = -EOPNOTSUPP;
1148a556c76aSAlexandre Belloni 		break;
1149a556c76aSAlexandre Belloni 	}
1150a556c76aSAlexandre Belloni 
1151a556c76aSAlexandre Belloni 	return err;
1152a556c76aSAlexandre Belloni }
1153a556c76aSAlexandre Belloni 
1154*7142529fSAntoine Tenart static int ocelot_port_obj_add_vlan(struct net_device *dev,
1155*7142529fSAntoine Tenart 				    const struct switchdev_obj_port_vlan *vlan,
1156*7142529fSAntoine Tenart 				    struct switchdev_trans *trans)
1157*7142529fSAntoine Tenart {
1158*7142529fSAntoine Tenart 	int ret;
1159*7142529fSAntoine Tenart 	u16 vid;
1160*7142529fSAntoine Tenart 
1161*7142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1162*7142529fSAntoine Tenart 		ret = ocelot_vlan_vid_add(dev, vid,
1163*7142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
1164*7142529fSAntoine Tenart 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
1165*7142529fSAntoine Tenart 		if (ret)
1166*7142529fSAntoine Tenart 			return ret;
1167*7142529fSAntoine Tenart 	}
1168*7142529fSAntoine Tenart 
1169*7142529fSAntoine Tenart 	return 0;
1170*7142529fSAntoine Tenart }
1171*7142529fSAntoine Tenart 
1172*7142529fSAntoine Tenart static int ocelot_port_vlan_del_vlan(struct net_device *dev,
1173*7142529fSAntoine Tenart 				     const struct switchdev_obj_port_vlan *vlan)
1174*7142529fSAntoine Tenart {
1175*7142529fSAntoine Tenart 	int ret;
1176*7142529fSAntoine Tenart 	u16 vid;
1177*7142529fSAntoine Tenart 
1178*7142529fSAntoine Tenart 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1179*7142529fSAntoine Tenart 		ret = ocelot_vlan_vid_del(dev, vid);
1180*7142529fSAntoine Tenart 
1181*7142529fSAntoine Tenart 		if (ret)
1182*7142529fSAntoine Tenart 			return ret;
1183*7142529fSAntoine Tenart 	}
1184*7142529fSAntoine Tenart 
1185*7142529fSAntoine Tenart 	return 0;
1186*7142529fSAntoine Tenart }
1187*7142529fSAntoine Tenart 
1188a556c76aSAlexandre Belloni static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1189a556c76aSAlexandre Belloni 						     const unsigned char *addr,
1190a556c76aSAlexandre Belloni 						     u16 vid)
1191a556c76aSAlexandre Belloni {
1192a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1193a556c76aSAlexandre Belloni 
1194a556c76aSAlexandre Belloni 	list_for_each_entry(mc, &ocelot->multicast, list) {
1195a556c76aSAlexandre Belloni 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1196a556c76aSAlexandre Belloni 			return mc;
1197a556c76aSAlexandre Belloni 	}
1198a556c76aSAlexandre Belloni 
1199a556c76aSAlexandre Belloni 	return NULL;
1200a556c76aSAlexandre Belloni }
1201a556c76aSAlexandre Belloni 
1202a556c76aSAlexandre Belloni static int ocelot_port_obj_add_mdb(struct net_device *dev,
1203a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb,
1204a556c76aSAlexandre Belloni 				   struct switchdev_trans *trans)
1205a556c76aSAlexandre Belloni {
1206a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1207a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1208a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1209a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1210a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1211a556c76aSAlexandre Belloni 	bool new = false;
1212a556c76aSAlexandre Belloni 
1213a556c76aSAlexandre Belloni 	if (!vid)
1214*7142529fSAntoine Tenart 		vid = port->pvid;
1215a556c76aSAlexandre Belloni 
1216a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1217a556c76aSAlexandre Belloni 	if (!mc) {
1218a556c76aSAlexandre Belloni 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1219a556c76aSAlexandre Belloni 		if (!mc)
1220a556c76aSAlexandre Belloni 			return -ENOMEM;
1221a556c76aSAlexandre Belloni 
1222a556c76aSAlexandre Belloni 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1223a556c76aSAlexandre Belloni 		mc->vid = vid;
1224a556c76aSAlexandre Belloni 
1225a556c76aSAlexandre Belloni 		list_add_tail(&mc->list, &ocelot->multicast);
1226a556c76aSAlexandre Belloni 		new = true;
1227a556c76aSAlexandre Belloni 	}
1228a556c76aSAlexandre Belloni 
1229a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1230a556c76aSAlexandre Belloni 	addr[0] = 0;
1231a556c76aSAlexandre Belloni 
1232a556c76aSAlexandre Belloni 	if (!new) {
1233a556c76aSAlexandre Belloni 		addr[2] = mc->ports << 0;
1234a556c76aSAlexandre Belloni 		addr[1] = mc->ports << 8;
1235a556c76aSAlexandre Belloni 		ocelot_mact_forget(ocelot, addr, vid);
1236a556c76aSAlexandre Belloni 	}
1237a556c76aSAlexandre Belloni 
1238a556c76aSAlexandre Belloni 	mc->ports |= BIT(port->chip_port);
1239a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1240a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1241a556c76aSAlexandre Belloni 
1242a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1243a556c76aSAlexandre Belloni }
1244a556c76aSAlexandre Belloni 
1245a556c76aSAlexandre Belloni static int ocelot_port_obj_del_mdb(struct net_device *dev,
1246a556c76aSAlexandre Belloni 				   const struct switchdev_obj_port_mdb *mdb)
1247a556c76aSAlexandre Belloni {
1248a556c76aSAlexandre Belloni 	struct ocelot_port *port = netdev_priv(dev);
1249a556c76aSAlexandre Belloni 	struct ocelot *ocelot = port->ocelot;
1250a556c76aSAlexandre Belloni 	struct ocelot_multicast *mc;
1251a556c76aSAlexandre Belloni 	unsigned char addr[ETH_ALEN];
1252a556c76aSAlexandre Belloni 	u16 vid = mdb->vid;
1253a556c76aSAlexandre Belloni 
1254a556c76aSAlexandre Belloni 	if (!vid)
1255*7142529fSAntoine Tenart 		vid = port->pvid;
1256a556c76aSAlexandre Belloni 
1257a556c76aSAlexandre Belloni 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1258a556c76aSAlexandre Belloni 	if (!mc)
1259a556c76aSAlexandre Belloni 		return -ENOENT;
1260a556c76aSAlexandre Belloni 
1261a556c76aSAlexandre Belloni 	memcpy(addr, mc->addr, ETH_ALEN);
1262a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1263a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1264a556c76aSAlexandre Belloni 	addr[0] = 0;
1265a556c76aSAlexandre Belloni 	ocelot_mact_forget(ocelot, addr, vid);
1266a556c76aSAlexandre Belloni 
1267a556c76aSAlexandre Belloni 	mc->ports &= ~BIT(port->chip_port);
1268a556c76aSAlexandre Belloni 	if (!mc->ports) {
1269a556c76aSAlexandre Belloni 		list_del(&mc->list);
1270a556c76aSAlexandre Belloni 		devm_kfree(ocelot->dev, mc);
1271a556c76aSAlexandre Belloni 		return 0;
1272a556c76aSAlexandre Belloni 	}
1273a556c76aSAlexandre Belloni 
1274a556c76aSAlexandre Belloni 	addr[2] = mc->ports << 0;
1275a556c76aSAlexandre Belloni 	addr[1] = mc->ports << 8;
1276a556c76aSAlexandre Belloni 
1277a556c76aSAlexandre Belloni 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1278a556c76aSAlexandre Belloni }
1279a556c76aSAlexandre Belloni 
1280a556c76aSAlexandre Belloni static int ocelot_port_obj_add(struct net_device *dev,
1281a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj,
1282a556c76aSAlexandre Belloni 			       struct switchdev_trans *trans)
1283a556c76aSAlexandre Belloni {
1284a556c76aSAlexandre Belloni 	int ret = 0;
1285a556c76aSAlexandre Belloni 
1286a556c76aSAlexandre Belloni 	switch (obj->id) {
1287*7142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1288*7142529fSAntoine Tenart 		ret = ocelot_port_obj_add_vlan(dev,
1289*7142529fSAntoine Tenart 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
1290*7142529fSAntoine Tenart 					       trans);
1291*7142529fSAntoine Tenart 		break;
1292a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1293a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1294a556c76aSAlexandre Belloni 					      trans);
1295a556c76aSAlexandre Belloni 		break;
1296a556c76aSAlexandre Belloni 	default:
1297a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1298a556c76aSAlexandre Belloni 	}
1299a556c76aSAlexandre Belloni 
1300a556c76aSAlexandre Belloni 	return ret;
1301a556c76aSAlexandre Belloni }
1302a556c76aSAlexandre Belloni 
1303a556c76aSAlexandre Belloni static int ocelot_port_obj_del(struct net_device *dev,
1304a556c76aSAlexandre Belloni 			       const struct switchdev_obj *obj)
1305a556c76aSAlexandre Belloni {
1306a556c76aSAlexandre Belloni 	int ret = 0;
1307a556c76aSAlexandre Belloni 
1308a556c76aSAlexandre Belloni 	switch (obj->id) {
1309*7142529fSAntoine Tenart 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1310*7142529fSAntoine Tenart 		ret = ocelot_port_vlan_del_vlan(dev,
1311*7142529fSAntoine Tenart 						SWITCHDEV_OBJ_PORT_VLAN(obj));
1312*7142529fSAntoine Tenart 		break;
1313a556c76aSAlexandre Belloni 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1314a556c76aSAlexandre Belloni 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1315a556c76aSAlexandre Belloni 		break;
1316a556c76aSAlexandre Belloni 	default:
1317a556c76aSAlexandre Belloni 		return -EOPNOTSUPP;
1318a556c76aSAlexandre Belloni 	}
1319a556c76aSAlexandre Belloni 
1320a556c76aSAlexandre Belloni 	return ret;
1321a556c76aSAlexandre Belloni }
1322a556c76aSAlexandre Belloni 
1323a556c76aSAlexandre Belloni static const struct switchdev_ops ocelot_port_switchdev_ops = {
1324a556c76aSAlexandre Belloni 	.switchdev_port_attr_get	= ocelot_port_attr_get,
1325a556c76aSAlexandre Belloni 	.switchdev_port_attr_set	= ocelot_port_attr_set,
1326a556c76aSAlexandre Belloni 	.switchdev_port_obj_add		= ocelot_port_obj_add,
1327a556c76aSAlexandre Belloni 	.switchdev_port_obj_del		= ocelot_port_obj_del,
1328a556c76aSAlexandre Belloni };
1329a556c76aSAlexandre Belloni 
1330a556c76aSAlexandre Belloni static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1331a556c76aSAlexandre Belloni 				   struct net_device *bridge)
1332a556c76aSAlexandre Belloni {
1333a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1334a556c76aSAlexandre Belloni 
1335a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask) {
1336a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = bridge;
1337a556c76aSAlexandre Belloni 	} else {
1338a556c76aSAlexandre Belloni 		if (ocelot->hw_bridge_dev != bridge)
1339a556c76aSAlexandre Belloni 			/* This is adding the port to a second bridge, this is
1340a556c76aSAlexandre Belloni 			 * unsupported */
1341a556c76aSAlexandre Belloni 			return -ENODEV;
1342a556c76aSAlexandre Belloni 	}
1343a556c76aSAlexandre Belloni 
1344a556c76aSAlexandre Belloni 	ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1345a556c76aSAlexandre Belloni 
1346a556c76aSAlexandre Belloni 	return 0;
1347a556c76aSAlexandre Belloni }
1348a556c76aSAlexandre Belloni 
1349a556c76aSAlexandre Belloni static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1350a556c76aSAlexandre Belloni 				     struct net_device *bridge)
1351a556c76aSAlexandre Belloni {
1352a556c76aSAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1353a556c76aSAlexandre Belloni 
1354a556c76aSAlexandre Belloni 	ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
1355a556c76aSAlexandre Belloni 
1356a556c76aSAlexandre Belloni 	if (!ocelot->bridge_mask)
1357a556c76aSAlexandre Belloni 		ocelot->hw_bridge_dev = NULL;
1358*7142529fSAntoine Tenart 
1359*7142529fSAntoine Tenart 	/* Clear bridge vlan settings before calling ocelot_vlan_port_apply */
1360*7142529fSAntoine Tenart 	ocelot_port->vlan_aware = 0;
1361*7142529fSAntoine Tenart 	ocelot_port->pvid = 0;
1362*7142529fSAntoine Tenart 	ocelot_port->vid = 0;
1363a556c76aSAlexandre Belloni }
1364a556c76aSAlexandre Belloni 
1365dc96ee37SAlexandre Belloni static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1366dc96ee37SAlexandre Belloni {
1367dc96ee37SAlexandre Belloni 	int i, port, lag;
1368dc96ee37SAlexandre Belloni 
1369dc96ee37SAlexandre Belloni 	/* Reset destination and aggregation PGIDS */
1370dc96ee37SAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++)
1371dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1372dc96ee37SAlexandre Belloni 
1373dc96ee37SAlexandre Belloni 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1374dc96ee37SAlexandre Belloni 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1375dc96ee37SAlexandre Belloni 				 ANA_PGID_PGID, i);
1376dc96ee37SAlexandre Belloni 
1377dc96ee37SAlexandre Belloni 	/* Now, set PGIDs for each LAG */
1378dc96ee37SAlexandre Belloni 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1379dc96ee37SAlexandre Belloni 		unsigned long bond_mask;
1380dc96ee37SAlexandre Belloni 		int aggr_count = 0;
1381dc96ee37SAlexandre Belloni 		u8 aggr_idx[16];
1382dc96ee37SAlexandre Belloni 
1383dc96ee37SAlexandre Belloni 		bond_mask = ocelot->lags[lag];
1384dc96ee37SAlexandre Belloni 		if (!bond_mask)
1385dc96ee37SAlexandre Belloni 			continue;
1386dc96ee37SAlexandre Belloni 
1387dc96ee37SAlexandre Belloni 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1388dc96ee37SAlexandre Belloni 			// Destination mask
1389dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, bond_mask,
1390dc96ee37SAlexandre Belloni 					 ANA_PGID_PGID, port);
1391dc96ee37SAlexandre Belloni 			aggr_idx[aggr_count] = port;
1392dc96ee37SAlexandre Belloni 			aggr_count++;
1393dc96ee37SAlexandre Belloni 		}
1394dc96ee37SAlexandre Belloni 
1395dc96ee37SAlexandre Belloni 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1396dc96ee37SAlexandre Belloni 			u32 ac;
1397dc96ee37SAlexandre Belloni 
1398dc96ee37SAlexandre Belloni 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1399dc96ee37SAlexandre Belloni 			ac &= ~bond_mask;
1400dc96ee37SAlexandre Belloni 			ac |= BIT(aggr_idx[i % aggr_count]);
1401dc96ee37SAlexandre Belloni 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1402dc96ee37SAlexandre Belloni 		}
1403dc96ee37SAlexandre Belloni 	}
1404dc96ee37SAlexandre Belloni }
1405dc96ee37SAlexandre Belloni 
1406dc96ee37SAlexandre Belloni static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1407dc96ee37SAlexandre Belloni {
1408dc96ee37SAlexandre Belloni 	unsigned long bond_mask = ocelot->lags[lag];
1409dc96ee37SAlexandre Belloni 	unsigned int p;
1410dc96ee37SAlexandre Belloni 
1411dc96ee37SAlexandre Belloni 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1412dc96ee37SAlexandre Belloni 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1413dc96ee37SAlexandre Belloni 
1414dc96ee37SAlexandre Belloni 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1415dc96ee37SAlexandre Belloni 
1416dc96ee37SAlexandre Belloni 		/* Use lag port as logical port for port i */
1417dc96ee37SAlexandre Belloni 		ocelot_write_gix(ocelot, port_cfg |
1418dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1419dc96ee37SAlexandre Belloni 				 ANA_PORT_PORT_CFG, p);
1420dc96ee37SAlexandre Belloni 	}
1421dc96ee37SAlexandre Belloni }
1422dc96ee37SAlexandre Belloni 
1423dc96ee37SAlexandre Belloni static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1424dc96ee37SAlexandre Belloni 				struct net_device *bond)
1425dc96ee37SAlexandre Belloni {
1426dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1427dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1428dc96ee37SAlexandre Belloni 	int lag, lp;
1429dc96ee37SAlexandre Belloni 	struct net_device *ndev;
1430dc96ee37SAlexandre Belloni 	u32 bond_mask = 0;
1431dc96ee37SAlexandre Belloni 
1432dc96ee37SAlexandre Belloni 	rcu_read_lock();
1433dc96ee37SAlexandre Belloni 	for_each_netdev_in_bond_rcu(bond, ndev) {
1434dc96ee37SAlexandre Belloni 		struct ocelot_port *port = netdev_priv(ndev);
1435dc96ee37SAlexandre Belloni 
1436dc96ee37SAlexandre Belloni 		bond_mask |= BIT(port->chip_port);
1437dc96ee37SAlexandre Belloni 	}
1438dc96ee37SAlexandre Belloni 	rcu_read_unlock();
1439dc96ee37SAlexandre Belloni 
1440dc96ee37SAlexandre Belloni 	lp = __ffs(bond_mask);
1441dc96ee37SAlexandre Belloni 
1442dc96ee37SAlexandre Belloni 	/* If the new port is the lowest one, use it as the logical port from
1443dc96ee37SAlexandre Belloni 	 * now on
1444dc96ee37SAlexandre Belloni 	 */
1445dc96ee37SAlexandre Belloni 	if (p == lp) {
1446dc96ee37SAlexandre Belloni 		lag = p;
1447dc96ee37SAlexandre Belloni 		ocelot->lags[p] = bond_mask;
1448dc96ee37SAlexandre Belloni 		bond_mask &= ~BIT(p);
1449dc96ee37SAlexandre Belloni 		if (bond_mask) {
1450dc96ee37SAlexandre Belloni 			lp = __ffs(bond_mask);
1451dc96ee37SAlexandre Belloni 			ocelot->lags[lp] = 0;
1452dc96ee37SAlexandre Belloni 		}
1453dc96ee37SAlexandre Belloni 	} else {
1454dc96ee37SAlexandre Belloni 		lag = lp;
1455dc96ee37SAlexandre Belloni 		ocelot->lags[lp] |= BIT(p);
1456dc96ee37SAlexandre Belloni 	}
1457dc96ee37SAlexandre Belloni 
1458dc96ee37SAlexandre Belloni 	ocelot_setup_lag(ocelot, lag);
1459dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1460dc96ee37SAlexandre Belloni 
1461dc96ee37SAlexandre Belloni 	return 0;
1462dc96ee37SAlexandre Belloni }
1463dc96ee37SAlexandre Belloni 
1464dc96ee37SAlexandre Belloni static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1465dc96ee37SAlexandre Belloni 				  struct net_device *bond)
1466dc96ee37SAlexandre Belloni {
1467dc96ee37SAlexandre Belloni 	struct ocelot *ocelot = ocelot_port->ocelot;
1468dc96ee37SAlexandre Belloni 	int p = ocelot_port->chip_port;
1469dc96ee37SAlexandre Belloni 	u32 port_cfg;
1470dc96ee37SAlexandre Belloni 	int i;
1471dc96ee37SAlexandre Belloni 
1472dc96ee37SAlexandre Belloni 	/* Remove port from any lag */
1473dc96ee37SAlexandre Belloni 	for (i = 0; i < ocelot->num_phys_ports; i++)
1474dc96ee37SAlexandre Belloni 		ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1475dc96ee37SAlexandre Belloni 
1476dc96ee37SAlexandre Belloni 	/* if it was the logical port of the lag, move the lag config to the
1477dc96ee37SAlexandre Belloni 	 * next port
1478dc96ee37SAlexandre Belloni 	 */
1479dc96ee37SAlexandre Belloni 	if (ocelot->lags[p]) {
1480dc96ee37SAlexandre Belloni 		int n = __ffs(ocelot->lags[p]);
1481dc96ee37SAlexandre Belloni 
1482dc96ee37SAlexandre Belloni 		ocelot->lags[n] = ocelot->lags[p];
1483dc96ee37SAlexandre Belloni 		ocelot->lags[p] = 0;
1484dc96ee37SAlexandre Belloni 
1485dc96ee37SAlexandre Belloni 		ocelot_setup_lag(ocelot, n);
1486dc96ee37SAlexandre Belloni 	}
1487dc96ee37SAlexandre Belloni 
1488dc96ee37SAlexandre Belloni 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1489dc96ee37SAlexandre Belloni 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1490dc96ee37SAlexandre Belloni 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1491dc96ee37SAlexandre Belloni 			 ANA_PORT_PORT_CFG, p);
1492dc96ee37SAlexandre Belloni 
1493dc96ee37SAlexandre Belloni 	ocelot_set_aggr_pgids(ocelot);
1494dc96ee37SAlexandre Belloni }
1495dc96ee37SAlexandre Belloni 
1496a556c76aSAlexandre Belloni /* Checks if the net_device instance given to us originate from our driver. */
1497a556c76aSAlexandre Belloni static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1498a556c76aSAlexandre Belloni {
1499a556c76aSAlexandre Belloni 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1500a556c76aSAlexandre Belloni }
1501a556c76aSAlexandre Belloni 
1502a556c76aSAlexandre Belloni static int ocelot_netdevice_port_event(struct net_device *dev,
1503a556c76aSAlexandre Belloni 				       unsigned long event,
1504a556c76aSAlexandre Belloni 				       struct netdev_notifier_changeupper_info *info)
1505a556c76aSAlexandre Belloni {
1506a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port = netdev_priv(dev);
1507a556c76aSAlexandre Belloni 	int err = 0;
1508a556c76aSAlexandre Belloni 
1509a556c76aSAlexandre Belloni 	if (!ocelot_netdevice_dev_check(dev))
1510a556c76aSAlexandre Belloni 		return 0;
1511a556c76aSAlexandre Belloni 
1512a556c76aSAlexandre Belloni 	switch (event) {
1513a556c76aSAlexandre Belloni 	case NETDEV_CHANGEUPPER:
1514a556c76aSAlexandre Belloni 		if (netif_is_bridge_master(info->upper_dev)) {
1515a556c76aSAlexandre Belloni 			if (info->linking)
1516a556c76aSAlexandre Belloni 				err = ocelot_port_bridge_join(ocelot_port,
1517a556c76aSAlexandre Belloni 							      info->upper_dev);
1518a556c76aSAlexandre Belloni 			else
1519a556c76aSAlexandre Belloni 				ocelot_port_bridge_leave(ocelot_port,
1520a556c76aSAlexandre Belloni 							 info->upper_dev);
1521*7142529fSAntoine Tenart 
1522*7142529fSAntoine Tenart 			ocelot_vlan_port_apply(ocelot_port->ocelot,
1523*7142529fSAntoine Tenart 					       ocelot_port);
1524a556c76aSAlexandre Belloni 		}
1525dc96ee37SAlexandre Belloni 		if (netif_is_lag_master(info->upper_dev)) {
1526dc96ee37SAlexandre Belloni 			if (info->linking)
1527dc96ee37SAlexandre Belloni 				err = ocelot_port_lag_join(ocelot_port,
1528dc96ee37SAlexandre Belloni 							   info->upper_dev);
1529dc96ee37SAlexandre Belloni 			else
1530dc96ee37SAlexandre Belloni 				ocelot_port_lag_leave(ocelot_port,
1531dc96ee37SAlexandre Belloni 						      info->upper_dev);
1532dc96ee37SAlexandre Belloni 		}
1533a556c76aSAlexandre Belloni 		break;
1534a556c76aSAlexandre Belloni 	default:
1535a556c76aSAlexandre Belloni 		break;
1536a556c76aSAlexandre Belloni 	}
1537a556c76aSAlexandre Belloni 
1538a556c76aSAlexandre Belloni 	return err;
1539a556c76aSAlexandre Belloni }
1540a556c76aSAlexandre Belloni 
1541a556c76aSAlexandre Belloni static int ocelot_netdevice_event(struct notifier_block *unused,
1542a556c76aSAlexandre Belloni 				  unsigned long event, void *ptr)
1543a556c76aSAlexandre Belloni {
1544a556c76aSAlexandre Belloni 	struct netdev_notifier_changeupper_info *info = ptr;
1545a556c76aSAlexandre Belloni 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
15462ac0e152SGeert Uytterhoeven 	int ret = 0;
1547a556c76aSAlexandre Belloni 
1548dc96ee37SAlexandre Belloni 	if (event == NETDEV_PRECHANGEUPPER &&
1549dc96ee37SAlexandre Belloni 	    netif_is_lag_master(info->upper_dev)) {
1550dc96ee37SAlexandre Belloni 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1551dc96ee37SAlexandre Belloni 		struct netlink_ext_ack *extack;
1552dc96ee37SAlexandre Belloni 
1553dc96ee37SAlexandre Belloni 		if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1554dc96ee37SAlexandre Belloni 			extack = netdev_notifier_info_to_extack(&info->info);
1555dc96ee37SAlexandre Belloni 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1556dc96ee37SAlexandre Belloni 
1557dc96ee37SAlexandre Belloni 			ret = -EINVAL;
1558dc96ee37SAlexandre Belloni 			goto notify;
1559dc96ee37SAlexandre Belloni 		}
1560dc96ee37SAlexandre Belloni 	}
1561dc96ee37SAlexandre Belloni 
1562a556c76aSAlexandre Belloni 	if (netif_is_lag_master(dev)) {
1563a556c76aSAlexandre Belloni 		struct net_device *slave;
1564a556c76aSAlexandre Belloni 		struct list_head *iter;
1565a556c76aSAlexandre Belloni 
1566a556c76aSAlexandre Belloni 		netdev_for_each_lower_dev(dev, slave, iter) {
1567a556c76aSAlexandre Belloni 			ret = ocelot_netdevice_port_event(slave, event, info);
1568a556c76aSAlexandre Belloni 			if (ret)
1569a556c76aSAlexandre Belloni 				goto notify;
1570a556c76aSAlexandre Belloni 		}
1571a556c76aSAlexandre Belloni 	} else {
1572a556c76aSAlexandre Belloni 		ret = ocelot_netdevice_port_event(dev, event, info);
1573a556c76aSAlexandre Belloni 	}
1574a556c76aSAlexandre Belloni 
1575a556c76aSAlexandre Belloni notify:
1576a556c76aSAlexandre Belloni 	return notifier_from_errno(ret);
1577a556c76aSAlexandre Belloni }
1578a556c76aSAlexandre Belloni 
1579a556c76aSAlexandre Belloni struct notifier_block ocelot_netdevice_nb __read_mostly = {
1580a556c76aSAlexandre Belloni 	.notifier_call = ocelot_netdevice_event,
1581a556c76aSAlexandre Belloni };
1582a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_netdevice_nb);
1583a556c76aSAlexandre Belloni 
1584a556c76aSAlexandre Belloni int ocelot_probe_port(struct ocelot *ocelot, u8 port,
1585a556c76aSAlexandre Belloni 		      void __iomem *regs,
1586a556c76aSAlexandre Belloni 		      struct phy_device *phy)
1587a556c76aSAlexandre Belloni {
1588a556c76aSAlexandre Belloni 	struct ocelot_port *ocelot_port;
1589a556c76aSAlexandre Belloni 	struct net_device *dev;
1590a556c76aSAlexandre Belloni 	int err;
1591a556c76aSAlexandre Belloni 
1592a556c76aSAlexandre Belloni 	dev = alloc_etherdev(sizeof(struct ocelot_port));
1593a556c76aSAlexandre Belloni 	if (!dev)
1594a556c76aSAlexandre Belloni 		return -ENOMEM;
1595a556c76aSAlexandre Belloni 	SET_NETDEV_DEV(dev, ocelot->dev);
1596a556c76aSAlexandre Belloni 	ocelot_port = netdev_priv(dev);
1597a556c76aSAlexandre Belloni 	ocelot_port->dev = dev;
1598a556c76aSAlexandre Belloni 	ocelot_port->ocelot = ocelot;
1599a556c76aSAlexandre Belloni 	ocelot_port->regs = regs;
1600a556c76aSAlexandre Belloni 	ocelot_port->chip_port = port;
1601a556c76aSAlexandre Belloni 	ocelot_port->phy = phy;
1602a556c76aSAlexandre Belloni 	INIT_LIST_HEAD(&ocelot_port->mc);
1603a556c76aSAlexandre Belloni 	ocelot->ports[port] = ocelot_port;
1604a556c76aSAlexandre Belloni 
1605a556c76aSAlexandre Belloni 	dev->netdev_ops = &ocelot_port_netdev_ops;
1606a556c76aSAlexandre Belloni 	dev->ethtool_ops = &ocelot_ethtool_ops;
1607a556c76aSAlexandre Belloni 	dev->switchdev_ops = &ocelot_port_switchdev_ops;
1608a556c76aSAlexandre Belloni 
1609*7142529fSAntoine Tenart 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1610*7142529fSAntoine Tenart 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1611*7142529fSAntoine Tenart 
1612a556c76aSAlexandre Belloni 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1613a556c76aSAlexandre Belloni 	dev->dev_addr[ETH_ALEN - 1] += port;
1614a556c76aSAlexandre Belloni 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
1615a556c76aSAlexandre Belloni 			  ENTRYTYPE_LOCKED);
1616a556c76aSAlexandre Belloni 
1617a556c76aSAlexandre Belloni 	err = register_netdev(dev);
1618a556c76aSAlexandre Belloni 	if (err) {
1619a556c76aSAlexandre Belloni 		dev_err(ocelot->dev, "register_netdev failed\n");
1620a556c76aSAlexandre Belloni 		goto err_register_netdev;
1621a556c76aSAlexandre Belloni 	}
1622a556c76aSAlexandre Belloni 
1623*7142529fSAntoine Tenart 	/* Basic L2 initialization */
1624*7142529fSAntoine Tenart 	ocelot_vlan_port_apply(ocelot, ocelot_port);
1625*7142529fSAntoine Tenart 
1626a556c76aSAlexandre Belloni 	return 0;
1627a556c76aSAlexandre Belloni 
1628a556c76aSAlexandre Belloni err_register_netdev:
1629a556c76aSAlexandre Belloni 	free_netdev(dev);
1630a556c76aSAlexandre Belloni 	return err;
1631a556c76aSAlexandre Belloni }
1632a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_probe_port);
1633a556c76aSAlexandre Belloni 
1634a556c76aSAlexandre Belloni int ocelot_init(struct ocelot *ocelot)
1635a556c76aSAlexandre Belloni {
1636a556c76aSAlexandre Belloni 	u32 port;
1637a556c76aSAlexandre Belloni 	int i, cpu = ocelot->num_phys_ports;
1638a556c76aSAlexandre Belloni 	char queue_name[32];
1639a556c76aSAlexandre Belloni 
1640dc96ee37SAlexandre Belloni 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1641dc96ee37SAlexandre Belloni 				    sizeof(u32), GFP_KERNEL);
1642dc96ee37SAlexandre Belloni 	if (!ocelot->lags)
1643dc96ee37SAlexandre Belloni 		return -ENOMEM;
1644dc96ee37SAlexandre Belloni 
1645a556c76aSAlexandre Belloni 	ocelot->stats = devm_kcalloc(ocelot->dev,
1646a556c76aSAlexandre Belloni 				     ocelot->num_phys_ports * ocelot->num_stats,
1647a556c76aSAlexandre Belloni 				     sizeof(u64), GFP_KERNEL);
1648a556c76aSAlexandre Belloni 	if (!ocelot->stats)
1649a556c76aSAlexandre Belloni 		return -ENOMEM;
1650a556c76aSAlexandre Belloni 
1651a556c76aSAlexandre Belloni 	mutex_init(&ocelot->stats_lock);
1652a556c76aSAlexandre Belloni 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1653a556c76aSAlexandre Belloni 		 dev_name(ocelot->dev));
1654a556c76aSAlexandre Belloni 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1655a556c76aSAlexandre Belloni 	if (!ocelot->stats_queue)
1656a556c76aSAlexandre Belloni 		return -ENOMEM;
1657a556c76aSAlexandre Belloni 
1658a556c76aSAlexandre Belloni 	ocelot_mact_init(ocelot);
1659a556c76aSAlexandre Belloni 	ocelot_vlan_init(ocelot);
1660a556c76aSAlexandre Belloni 
1661a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1662a556c76aSAlexandre Belloni 		/* Clear all counters (5 groups) */
1663a556c76aSAlexandre Belloni 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1664a556c76aSAlexandre Belloni 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1665a556c76aSAlexandre Belloni 			     SYS_STAT_CFG);
1666a556c76aSAlexandre Belloni 	}
1667a556c76aSAlexandre Belloni 
1668a556c76aSAlexandre Belloni 	/* Only use S-Tag */
1669a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1670a556c76aSAlexandre Belloni 
1671a556c76aSAlexandre Belloni 	/* Aggregation mode */
1672a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1673a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1674a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1675a556c76aSAlexandre Belloni 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1676a556c76aSAlexandre Belloni 
1677a556c76aSAlexandre Belloni 	/* Set MAC age time to default value. The entry is aged after
1678a556c76aSAlexandre Belloni 	 * 2*AGE_PERIOD
1679a556c76aSAlexandre Belloni 	 */
1680a556c76aSAlexandre Belloni 	ocelot_write(ocelot,
1681a556c76aSAlexandre Belloni 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1682a556c76aSAlexandre Belloni 		     ANA_AUTOAGE);
1683a556c76aSAlexandre Belloni 
1684a556c76aSAlexandre Belloni 	/* Disable learning for frames discarded by VLAN ingress filtering */
1685a556c76aSAlexandre Belloni 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1686a556c76aSAlexandre Belloni 
1687a556c76aSAlexandre Belloni 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1688a556c76aSAlexandre Belloni 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1689a556c76aSAlexandre Belloni 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1690a556c76aSAlexandre Belloni 
1691a556c76aSAlexandre Belloni 	/* Setup flooding PGIDs */
1692a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1693a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1694a556c76aSAlexandre Belloni 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1695a556c76aSAlexandre Belloni 			 ANA_FLOODING, 0);
1696a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1697a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1698a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1699a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1700a556c76aSAlexandre Belloni 		     ANA_FLOODING_IPMC);
1701a556c76aSAlexandre Belloni 
1702a556c76aSAlexandre Belloni 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1703a556c76aSAlexandre Belloni 		/* Transmit the frame to the local port. */
1704a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1705a556c76aSAlexandre Belloni 		/* Do not forward BPDU frames to the front ports. */
1706a556c76aSAlexandre Belloni 		ocelot_write_gix(ocelot,
1707a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1708a556c76aSAlexandre Belloni 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1709a556c76aSAlexandre Belloni 				 port);
1710a556c76aSAlexandre Belloni 		/* Ensure bridging is disabled */
1711a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1712a556c76aSAlexandre Belloni 	}
1713a556c76aSAlexandre Belloni 
1714a556c76aSAlexandre Belloni 	/* Configure and enable the CPU port. */
1715a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
1716a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1717a556c76aSAlexandre Belloni 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1718a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1719a556c76aSAlexandre Belloni 			 ANA_PORT_PORT_CFG, cpu);
1720a556c76aSAlexandre Belloni 
1721a556c76aSAlexandre Belloni 	/* Allow broadcast MAC frames. */
1722a556c76aSAlexandre Belloni 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
1723a556c76aSAlexandre Belloni 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1724a556c76aSAlexandre Belloni 
1725a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1726a556c76aSAlexandre Belloni 	}
1727a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot,
1728a556c76aSAlexandre Belloni 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1729a556c76aSAlexandre Belloni 			 ANA_PGID_PGID, PGID_MC);
1730a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1731a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1732a556c76aSAlexandre Belloni 
1733a556c76aSAlexandre Belloni 	/* CPU port Injection/Extraction configuration */
1734a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
1735a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
1736a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
1737a556c76aSAlexandre Belloni 			 QSYS_SWITCH_PORT_MODE, cpu);
1738a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
1739a556c76aSAlexandre Belloni 			 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
1740a556c76aSAlexandre Belloni 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1741a556c76aSAlexandre Belloni 	 * registers endianness.
1742a556c76aSAlexandre Belloni 	 */
1743a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1744a556c76aSAlexandre Belloni 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1745a556c76aSAlexandre Belloni 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1746a556c76aSAlexandre Belloni 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1747a556c76aSAlexandre Belloni 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1748a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1749a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1750a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1751a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1752a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1753a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1754a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1755a556c76aSAlexandre Belloni 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1756a556c76aSAlexandre Belloni 	for (i = 0; i < 16; i++)
1757a556c76aSAlexandre Belloni 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1758a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1759a556c76aSAlexandre Belloni 				 ANA_CPUQ_8021_CFG, i);
1760a556c76aSAlexandre Belloni 
1761a556c76aSAlexandre Belloni 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats);
1762a556c76aSAlexandre Belloni 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1763a556c76aSAlexandre Belloni 			   OCELOT_STATS_CHECK_DELAY);
1764a556c76aSAlexandre Belloni 	return 0;
1765a556c76aSAlexandre Belloni }
1766a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_init);
1767a556c76aSAlexandre Belloni 
1768a556c76aSAlexandre Belloni void ocelot_deinit(struct ocelot *ocelot)
1769a556c76aSAlexandre Belloni {
1770a556c76aSAlexandre Belloni 	destroy_workqueue(ocelot->stats_queue);
1771a556c76aSAlexandre Belloni 	mutex_destroy(&ocelot->stats_lock);
1772a556c76aSAlexandre Belloni }
1773a556c76aSAlexandre Belloni EXPORT_SYMBOL(ocelot_deinit);
1774a556c76aSAlexandre Belloni 
1775a556c76aSAlexandre Belloni MODULE_LICENSE("Dual MIT/GPL");
1776