xref: /linux/drivers/net/ethernet/mscc/ocelot.c (revision 40e79150c1686263e6a031d7702aec63aff31332)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3  * Microsemi Ocelot Switch driver
4  *
5  * Copyright (c) 2017 Microsemi Corporation
6  */
7 #include <linux/etherdevice.h>
8 #include <linux/ethtool.h>
9 #include <linux/if_bridge.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_vlan.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/phy.h>
17 #include <linux/skbuff.h>
18 #include <linux/iopoll.h>
19 #include <net/arp.h>
20 #include <net/netevent.h>
21 #include <net/rtnetlink.h>
22 #include <net/switchdev.h>
23 
24 #include "ocelot.h"
25 #include "ocelot_ace.h"
26 
27 #define TABLE_UPDATE_SLEEP_US 10
28 #define TABLE_UPDATE_TIMEOUT_US 100000
29 
30 /* MAC table entry types.
31  * ENTRYTYPE_NORMAL is subject to aging.
32  * ENTRYTYPE_LOCKED is not subject to aging.
33  * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
34  * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
35  */
36 enum macaccess_entry_type {
37 	ENTRYTYPE_NORMAL = 0,
38 	ENTRYTYPE_LOCKED,
39 	ENTRYTYPE_MACv4,
40 	ENTRYTYPE_MACv6,
41 };
42 
43 struct ocelot_mact_entry {
44 	u8 mac[ETH_ALEN];
45 	u16 vid;
46 	enum macaccess_entry_type type;
47 };
48 
49 static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
50 {
51 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
52 }
53 
54 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
55 {
56 	u32 val;
57 
58 	return readx_poll_timeout(ocelot_mact_read_macaccess,
59 		ocelot, val,
60 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
61 		MACACCESS_CMD_IDLE,
62 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
63 }
64 
65 static void ocelot_mact_select(struct ocelot *ocelot,
66 			       const unsigned char mac[ETH_ALEN],
67 			       unsigned int vid)
68 {
69 	u32 macl = 0, mach = 0;
70 
71 	/* Set the MAC address to handle and the vlan associated in a format
72 	 * understood by the hardware.
73 	 */
74 	mach |= vid    << 16;
75 	mach |= mac[0] << 8;
76 	mach |= mac[1] << 0;
77 	macl |= mac[2] << 24;
78 	macl |= mac[3] << 16;
79 	macl |= mac[4] << 8;
80 	macl |= mac[5] << 0;
81 
82 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
83 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
84 
85 }
86 
87 static int ocelot_mact_learn(struct ocelot *ocelot, int port,
88 			     const unsigned char mac[ETH_ALEN],
89 			     unsigned int vid,
90 			     enum macaccess_entry_type type)
91 {
92 	ocelot_mact_select(ocelot, mac, vid);
93 
94 	/* Issue a write command */
95 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
96 			     ANA_TABLES_MACACCESS_DEST_IDX(port) |
97 			     ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
98 			     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
99 			     ANA_TABLES_MACACCESS);
100 
101 	return ocelot_mact_wait_for_completion(ocelot);
102 }
103 
104 static int ocelot_mact_forget(struct ocelot *ocelot,
105 			      const unsigned char mac[ETH_ALEN],
106 			      unsigned int vid)
107 {
108 	ocelot_mact_select(ocelot, mac, vid);
109 
110 	/* Issue a forget command */
111 	ocelot_write(ocelot,
112 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
113 		     ANA_TABLES_MACACCESS);
114 
115 	return ocelot_mact_wait_for_completion(ocelot);
116 }
117 
118 static void ocelot_mact_init(struct ocelot *ocelot)
119 {
120 	/* Configure the learning mode entries attributes:
121 	 * - Do not copy the frame to the CPU extraction queues.
122 	 * - Use the vlan and mac_cpoy for dmac lookup.
123 	 */
124 	ocelot_rmw(ocelot, 0,
125 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
126 		   | ANA_AGENCTRL_LEARN_FWD_KILL
127 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
128 		   ANA_AGENCTRL);
129 
130 	/* Clear the MAC table */
131 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
132 }
133 
134 static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
135 {
136 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
137 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
138 			 ANA_PORT_VCAP_S2_CFG, port);
139 }
140 
141 static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
142 {
143 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
144 }
145 
146 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
147 {
148 	u32 val;
149 
150 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
151 		ocelot,
152 		val,
153 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
154 		ANA_TABLES_VLANACCESS_CMD_IDLE,
155 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
156 }
157 
158 static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
159 {
160 	/* Select the VID to configure */
161 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
162 		     ANA_TABLES_VLANTIDX);
163 	/* Set the vlan port members mask and issue a write command */
164 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
165 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
166 		     ANA_TABLES_VLANACCESS);
167 
168 	return ocelot_vlant_wait_for_completion(ocelot);
169 }
170 
171 static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
172 			     netdev_features_t features)
173 {
174 	u32 val;
175 
176 	/* Filtering */
177 	val = ocelot_read(ocelot, ANA_VLANMASK);
178 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
179 		val |= BIT(port);
180 	else
181 		val &= ~BIT(port);
182 	ocelot_write(ocelot, val, ANA_VLANMASK);
183 }
184 
185 static int ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
186 				       u16 vid)
187 {
188 	struct ocelot_port *ocelot_port = ocelot->ports[port];
189 	u32 val = 0;
190 
191 	if (ocelot_port->vid != vid) {
192 		/* Always permit deleting the native VLAN (vid = 0) */
193 		if (ocelot_port->vid && vid) {
194 			dev_err(ocelot->dev,
195 				"Port already has a native VLAN: %d\n",
196 				ocelot_port->vid);
197 			return -EBUSY;
198 		}
199 		ocelot_port->vid = vid;
200 	}
201 
202 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(vid),
203 		       REW_PORT_VLAN_CFG_PORT_VID_M,
204 		       REW_PORT_VLAN_CFG, port);
205 
206 	if (ocelot_port->vlan_aware && !ocelot_port->vid)
207 		/* If port is vlan-aware and tagged, drop untagged and priority
208 		 * tagged frames.
209 		 */
210 		val = ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
211 		      ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
212 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
213 	ocelot_rmw_gix(ocelot, val,
214 		       ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
215 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
216 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
217 		       ANA_PORT_DROP_CFG, port);
218 
219 	if (ocelot_port->vlan_aware) {
220 		if (ocelot_port->vid)
221 			/* Tag all frames except when VID == DEFAULT_VLAN */
222 			val = REW_TAG_CFG_TAG_CFG(1);
223 		else
224 			/* Tag all frames */
225 			val = REW_TAG_CFG_TAG_CFG(3);
226 	} else {
227 		/* Port tagging disabled. */
228 		val = REW_TAG_CFG_TAG_CFG(0);
229 	}
230 	ocelot_rmw_gix(ocelot, val,
231 		       REW_TAG_CFG_TAG_CFG_M,
232 		       REW_TAG_CFG, port);
233 
234 	return 0;
235 }
236 
237 void ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
238 				bool vlan_aware)
239 {
240 	struct ocelot_port *ocelot_port = ocelot->ports[port];
241 	u32 val;
242 
243 	ocelot_port->vlan_aware = vlan_aware;
244 
245 	if (vlan_aware)
246 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
247 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
248 	else
249 		val = 0;
250 	ocelot_rmw_gix(ocelot, val,
251 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
252 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
253 		       ANA_PORT_VLAN_CFG, port);
254 
255 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->vid);
256 }
257 EXPORT_SYMBOL(ocelot_port_vlan_filtering);
258 
259 /* Default vlan to clasify for untagged frames (may be zero) */
260 static void ocelot_port_set_pvid(struct ocelot *ocelot, int port, u16 pvid)
261 {
262 	struct ocelot_port *ocelot_port = ocelot->ports[port];
263 
264 	ocelot_rmw_gix(ocelot,
265 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
266 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
267 		       ANA_PORT_VLAN_CFG, port);
268 
269 	ocelot_port->pvid = pvid;
270 }
271 
272 int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
273 		    bool untagged)
274 {
275 	int ret;
276 
277 	/* Make the port a member of the VLAN */
278 	ocelot->vlan_mask[vid] |= BIT(port);
279 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
280 	if (ret)
281 		return ret;
282 
283 	/* Default ingress vlan classification */
284 	if (pvid)
285 		ocelot_port_set_pvid(ocelot, port, vid);
286 
287 	/* Untagged egress vlan clasification */
288 	if (untagged) {
289 		ret = ocelot_port_set_native_vlan(ocelot, port, vid);
290 		if (ret)
291 			return ret;
292 	}
293 
294 	return 0;
295 }
296 EXPORT_SYMBOL(ocelot_vlan_add);
297 
298 static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
299 			       bool untagged)
300 {
301 	struct ocelot_port_private *priv = netdev_priv(dev);
302 	struct ocelot_port *ocelot_port = &priv->port;
303 	struct ocelot *ocelot = ocelot_port->ocelot;
304 	int port = priv->chip_port;
305 	int ret;
306 
307 	ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
308 	if (ret)
309 		return ret;
310 
311 	/* Add the port MAC address to with the right VLAN information */
312 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
313 			  ENTRYTYPE_LOCKED);
314 
315 	return 0;
316 }
317 
318 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
319 {
320 	struct ocelot_port *ocelot_port = ocelot->ports[port];
321 	int ret;
322 
323 	/* Stop the port from being a member of the vlan */
324 	ocelot->vlan_mask[vid] &= ~BIT(port);
325 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
326 	if (ret)
327 		return ret;
328 
329 	/* Ingress */
330 	if (ocelot_port->pvid == vid)
331 		ocelot_port_set_pvid(ocelot, port, 0);
332 
333 	/* Egress */
334 	if (ocelot_port->vid == vid)
335 		ocelot_port_set_native_vlan(ocelot, port, 0);
336 
337 	return 0;
338 }
339 EXPORT_SYMBOL(ocelot_vlan_del);
340 
341 static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
342 {
343 	struct ocelot_port_private *priv = netdev_priv(dev);
344 	struct ocelot *ocelot = priv->port.ocelot;
345 	int port = priv->chip_port;
346 	int ret;
347 
348 	/* 8021q removes VID 0 on module unload for all interfaces
349 	 * with VLAN filtering feature. We need to keep it to receive
350 	 * untagged traffic.
351 	 */
352 	if (vid == 0)
353 		return 0;
354 
355 	ret = ocelot_vlan_del(ocelot, port, vid);
356 	if (ret)
357 		return ret;
358 
359 	/* Del the port MAC address to with the right VLAN information */
360 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
361 
362 	return 0;
363 }
364 
365 static void ocelot_vlan_init(struct ocelot *ocelot)
366 {
367 	u16 port, vid;
368 
369 	/* Clear VLAN table, by default all ports are members of all VLANs */
370 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
371 		     ANA_TABLES_VLANACCESS);
372 	ocelot_vlant_wait_for_completion(ocelot);
373 
374 	/* Configure the port VLAN memberships */
375 	for (vid = 1; vid < VLAN_N_VID; vid++) {
376 		ocelot->vlan_mask[vid] = 0;
377 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
378 	}
379 
380 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
381 	 * traffic.  It is added automatically if 8021q module is loaded, but
382 	 * we can't rely on it since module may be not loaded.
383 	 */
384 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
385 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
386 
387 	/* Set vlan ingress filter mask to all ports but the CPU port by
388 	 * default.
389 	 */
390 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
391 		     ANA_VLANMASK);
392 
393 	for (port = 0; port < ocelot->num_phys_ports; port++) {
394 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
395 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
396 	}
397 }
398 
399 /* Watermark encode
400  * Bit 8:   Unit; 0:1, 1:16
401  * Bit 7-0: Value to be multiplied with unit
402  */
403 static u16 ocelot_wm_enc(u16 value)
404 {
405 	if (value >= BIT(8))
406 		return BIT(8) | (value / 16);
407 
408 	return value;
409 }
410 
411 void ocelot_adjust_link(struct ocelot *ocelot, int port,
412 			struct phy_device *phydev)
413 {
414 	struct ocelot_port *ocelot_port = ocelot->ports[port];
415 	int speed, mode = 0;
416 
417 	switch (phydev->speed) {
418 	case SPEED_10:
419 		speed = OCELOT_SPEED_10;
420 		break;
421 	case SPEED_100:
422 		speed = OCELOT_SPEED_100;
423 		break;
424 	case SPEED_1000:
425 		speed = OCELOT_SPEED_1000;
426 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
427 		break;
428 	case SPEED_2500:
429 		speed = OCELOT_SPEED_2500;
430 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
431 		break;
432 	default:
433 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
434 			port, phydev->speed);
435 		return;
436 	}
437 
438 	phy_print_status(phydev);
439 
440 	if (!phydev->link)
441 		return;
442 
443 	/* Only full duplex supported for now */
444 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
445 			   mode, DEV_MAC_MODE_CFG);
446 
447 	/* Disable HDX fast control */
448 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
449 			   DEV_PORT_MISC);
450 
451 	/* SGMII only for now */
452 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
453 			   PCS1G_MODE_CFG);
454 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
455 
456 	/* Enable PCS */
457 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
458 
459 	/* No aneg on SGMII */
460 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
461 
462 	/* No loopback */
463 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
464 
465 	/* Enable MAC module */
466 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
467 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
468 
469 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
470 	 * reset */
471 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
472 			   DEV_CLOCK_CFG);
473 
474 	/* No PFC */
475 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
476 			 ANA_PFC_PFC_CFG, port);
477 
478 	/* Core: Enable port for frame transfer */
479 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
480 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
481 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
482 			 QSYS_SWITCH_PORT_MODE, port);
483 
484 	/* Flow control */
485 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
486 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
487 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
488 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
489 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
490 			 SYS_MAC_FC_CFG, port);
491 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
492 }
493 EXPORT_SYMBOL(ocelot_adjust_link);
494 
495 static void ocelot_port_adjust_link(struct net_device *dev)
496 {
497 	struct ocelot_port_private *priv = netdev_priv(dev);
498 	struct ocelot *ocelot = priv->port.ocelot;
499 	int port = priv->chip_port;
500 
501 	ocelot_adjust_link(ocelot, port, dev->phydev);
502 }
503 
504 void ocelot_port_enable(struct ocelot *ocelot, int port,
505 			struct phy_device *phy)
506 {
507 	/* Enable receiving frames on the port, and activate auto-learning of
508 	 * MAC addresses.
509 	 */
510 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
511 			 ANA_PORT_PORT_CFG_RECV_ENA |
512 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
513 			 ANA_PORT_PORT_CFG, port);
514 }
515 EXPORT_SYMBOL(ocelot_port_enable);
516 
517 static int ocelot_port_open(struct net_device *dev)
518 {
519 	struct ocelot_port_private *priv = netdev_priv(dev);
520 	struct ocelot_port *ocelot_port = &priv->port;
521 	struct ocelot *ocelot = ocelot_port->ocelot;
522 	int port = priv->chip_port;
523 	int err;
524 
525 	if (priv->serdes) {
526 		err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
527 				       ocelot_port->phy_mode);
528 		if (err) {
529 			netdev_err(dev, "Could not set mode of SerDes\n");
530 			return err;
531 		}
532 	}
533 
534 	err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
535 				 ocelot_port->phy_mode);
536 	if (err) {
537 		netdev_err(dev, "Could not attach to PHY\n");
538 		return err;
539 	}
540 
541 	dev->phydev = priv->phy;
542 
543 	phy_attached_info(priv->phy);
544 	phy_start(priv->phy);
545 
546 	ocelot_port_enable(ocelot, port, priv->phy);
547 
548 	return 0;
549 }
550 
551 void ocelot_port_disable(struct ocelot *ocelot, int port)
552 {
553 	struct ocelot_port *ocelot_port = ocelot->ports[port];
554 
555 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
556 	ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
557 		       QSYS_SWITCH_PORT_MODE, port);
558 }
559 EXPORT_SYMBOL(ocelot_port_disable);
560 
561 static int ocelot_port_stop(struct net_device *dev)
562 {
563 	struct ocelot_port_private *priv = netdev_priv(dev);
564 	struct ocelot *ocelot = priv->port.ocelot;
565 	int port = priv->chip_port;
566 
567 	phy_disconnect(priv->phy);
568 
569 	dev->phydev = NULL;
570 
571 	ocelot_port_disable(ocelot, port);
572 
573 	return 0;
574 }
575 
576 /* Generate the IFH for frame injection
577  *
578  * The IFH is a 128bit-value
579  * bit 127: bypass the analyzer processing
580  * bit 56-67: destination mask
581  * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
582  * bit 20-27: cpu extraction queue mask
583  * bit 16: tag type 0: C-tag, 1: S-tag
584  * bit 0-11: VID
585  */
586 static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
587 {
588 	ifh[0] = IFH_INJ_BYPASS | ((0x1ff & info->rew_op) << 21);
589 	ifh[1] = (0xf00 & info->port) >> 8;
590 	ifh[2] = (0xff & info->port) << 24;
591 	ifh[3] = (info->tag_type << 16) | info->vid;
592 
593 	return 0;
594 }
595 
596 int ocelot_port_add_txtstamp_skb(struct ocelot_port *ocelot_port,
597 				 struct sk_buff *skb)
598 {
599 	struct skb_shared_info *shinfo = skb_shinfo(skb);
600 	struct ocelot *ocelot = ocelot_port->ocelot;
601 
602 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP &&
603 	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
604 		shinfo->tx_flags |= SKBTX_IN_PROGRESS;
605 		/* Store timestamp ID in cb[0] of sk_buff */
606 		skb->cb[0] = ocelot_port->ts_id % 4;
607 		skb_queue_tail(&ocelot_port->tx_skbs, skb);
608 		return 0;
609 	}
610 	return -ENODATA;
611 }
612 EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
613 
614 static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
615 {
616 	struct ocelot_port_private *priv = netdev_priv(dev);
617 	struct skb_shared_info *shinfo = skb_shinfo(skb);
618 	struct ocelot_port *ocelot_port = &priv->port;
619 	struct ocelot *ocelot = ocelot_port->ocelot;
620 	u32 val, ifh[OCELOT_TAG_LEN / 4];
621 	struct frame_info info = {};
622 	u8 grp = 0; /* Send everything on CPU group 0 */
623 	unsigned int i, count, last;
624 	int port = priv->chip_port;
625 
626 	val = ocelot_read(ocelot, QS_INJ_STATUS);
627 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
628 	    (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
629 		return NETDEV_TX_BUSY;
630 
631 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
632 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
633 
634 	info.port = BIT(port);
635 	info.tag_type = IFH_TAG_TYPE_C;
636 	info.vid = skb_vlan_tag_get(skb);
637 
638 	/* Check if timestamping is needed */
639 	if (ocelot->ptp && shinfo->tx_flags & SKBTX_HW_TSTAMP) {
640 		info.rew_op = ocelot_port->ptp_cmd;
641 		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
642 			info.rew_op |= (ocelot_port->ts_id  % 4) << 3;
643 	}
644 
645 	ocelot_gen_ifh(ifh, &info);
646 
647 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
648 		ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
649 				 QS_INJ_WR, grp);
650 
651 	count = (skb->len + 3) / 4;
652 	last = skb->len % 4;
653 	for (i = 0; i < count; i++) {
654 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
655 	}
656 
657 	/* Add padding */
658 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
659 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
660 		i++;
661 	}
662 
663 	/* Indicate EOF and valid bytes in last word */
664 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
665 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
666 			 QS_INJ_CTRL_EOF,
667 			 QS_INJ_CTRL, grp);
668 
669 	/* Add dummy CRC */
670 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
671 	skb_tx_timestamp(skb);
672 
673 	dev->stats.tx_packets++;
674 	dev->stats.tx_bytes += skb->len;
675 
676 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, skb)) {
677 		ocelot_port->ts_id++;
678 		return NETDEV_TX_OK;
679 	}
680 
681 	dev_kfree_skb_any(skb);
682 	return NETDEV_TX_OK;
683 }
684 
685 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
686 				   struct timespec64 *ts)
687 {
688 	unsigned long flags;
689 	u32 val;
690 
691 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
692 
693 	/* Read current PTP time to get seconds */
694 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
695 
696 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
697 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
698 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
699 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
700 
701 	/* Read packet HW timestamp from FIFO */
702 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
703 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
704 
705 	/* Sec has incremented since the ts was registered */
706 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
707 		ts->tv_sec--;
708 
709 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
710 }
711 
712 void ocelot_get_txtstamp(struct ocelot *ocelot)
713 {
714 	int budget = OCELOT_PTP_QUEUE_SZ;
715 
716 	while (budget--) {
717 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
718 		struct skb_shared_hwtstamps shhwtstamps;
719 		struct ocelot_port *port;
720 		struct timespec64 ts;
721 		unsigned long flags;
722 		u32 val, id, txport;
723 
724 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
725 
726 		/* Check if a timestamp can be retrieved */
727 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
728 			break;
729 
730 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
731 
732 		/* Retrieve the ts ID and Tx port */
733 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
734 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
735 
736 		/* Retrieve its associated skb */
737 		port = ocelot->ports[txport];
738 
739 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
740 
741 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
742 			if (skb->cb[0] != id)
743 				continue;
744 			__skb_unlink(skb, &port->tx_skbs);
745 			skb_match = skb;
746 			break;
747 		}
748 
749 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
750 
751 		/* Next ts */
752 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
753 
754 		if (unlikely(!skb_match))
755 			continue;
756 
757 		/* Get the h/w timestamp */
758 		ocelot_get_hwtimestamp(ocelot, &ts);
759 
760 		/* Set the timestamp into the skb */
761 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
762 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
763 		skb_tstamp_tx(skb_match, &shhwtstamps);
764 
765 		dev_kfree_skb_any(skb_match);
766 	}
767 }
768 EXPORT_SYMBOL(ocelot_get_txtstamp);
769 
770 static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
771 {
772 	struct ocelot_port_private *priv = netdev_priv(dev);
773 	struct ocelot_port *ocelot_port = &priv->port;
774 	struct ocelot *ocelot = ocelot_port->ocelot;
775 
776 	return ocelot_mact_forget(ocelot, addr, ocelot_port->pvid);
777 }
778 
779 static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
780 {
781 	struct ocelot_port_private *priv = netdev_priv(dev);
782 	struct ocelot_port *ocelot_port = &priv->port;
783 	struct ocelot *ocelot = ocelot_port->ocelot;
784 
785 	return ocelot_mact_learn(ocelot, PGID_CPU, addr, ocelot_port->pvid,
786 				 ENTRYTYPE_LOCKED);
787 }
788 
789 static void ocelot_set_rx_mode(struct net_device *dev)
790 {
791 	struct ocelot_port_private *priv = netdev_priv(dev);
792 	struct ocelot *ocelot = priv->port.ocelot;
793 	u32 val;
794 	int i;
795 
796 	/* This doesn't handle promiscuous mode because the bridge core is
797 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
798 	 * forwarded to the CPU port.
799 	 */
800 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
801 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
802 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
803 
804 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
805 }
806 
807 static int ocelot_port_get_phys_port_name(struct net_device *dev,
808 					  char *buf, size_t len)
809 {
810 	struct ocelot_port_private *priv = netdev_priv(dev);
811 	int port = priv->chip_port;
812 	int ret;
813 
814 	ret = snprintf(buf, len, "p%d", port);
815 	if (ret >= len)
816 		return -EINVAL;
817 
818 	return 0;
819 }
820 
821 static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
822 {
823 	struct ocelot_port_private *priv = netdev_priv(dev);
824 	struct ocelot_port *ocelot_port = &priv->port;
825 	struct ocelot *ocelot = ocelot_port->ocelot;
826 	const struct sockaddr *addr = p;
827 
828 	/* Learn the new net device MAC address in the mac table. */
829 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, ocelot_port->pvid,
830 			  ENTRYTYPE_LOCKED);
831 	/* Then forget the previous one. */
832 	ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid);
833 
834 	ether_addr_copy(dev->dev_addr, addr->sa_data);
835 	return 0;
836 }
837 
838 static void ocelot_get_stats64(struct net_device *dev,
839 			       struct rtnl_link_stats64 *stats)
840 {
841 	struct ocelot_port_private *priv = netdev_priv(dev);
842 	struct ocelot *ocelot = priv->port.ocelot;
843 	int port = priv->chip_port;
844 
845 	/* Configure the port to read the stats from */
846 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
847 		     SYS_STAT_CFG);
848 
849 	/* Get Rx stats */
850 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
851 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
852 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
853 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
854 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
855 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
856 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
857 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
858 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
859 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
860 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
861 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
862 	stats->rx_dropped = dev->stats.rx_dropped;
863 
864 	/* Get Tx stats */
865 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
866 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
867 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
868 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
869 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
870 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
871 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
872 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
873 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
874 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
875 }
876 
877 int ocelot_fdb_add(struct ocelot *ocelot, int port,
878 		   const unsigned char *addr, u16 vid)
879 {
880 	struct ocelot_port *ocelot_port = ocelot->ports[port];
881 
882 	if (!vid) {
883 		if (!ocelot_port->vlan_aware)
884 			/* If the bridge is not VLAN aware and no VID was
885 			 * provided, set it to pvid to ensure the MAC entry
886 			 * matches incoming untagged packets
887 			 */
888 			vid = ocelot_port->pvid;
889 		else
890 			/* If the bridge is VLAN aware a VID must be provided as
891 			 * otherwise the learnt entry wouldn't match any frame.
892 			 */
893 			return -EINVAL;
894 	}
895 
896 	return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
897 }
898 EXPORT_SYMBOL(ocelot_fdb_add);
899 
900 static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
901 			       struct net_device *dev,
902 			       const unsigned char *addr,
903 			       u16 vid, u16 flags,
904 			       struct netlink_ext_ack *extack)
905 {
906 	struct ocelot_port_private *priv = netdev_priv(dev);
907 	struct ocelot *ocelot = priv->port.ocelot;
908 	int port = priv->chip_port;
909 
910 	return ocelot_fdb_add(ocelot, port, addr, vid);
911 }
912 
913 int ocelot_fdb_del(struct ocelot *ocelot, int port,
914 		   const unsigned char *addr, u16 vid)
915 {
916 	return ocelot_mact_forget(ocelot, addr, vid);
917 }
918 EXPORT_SYMBOL(ocelot_fdb_del);
919 
920 static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
921 			       struct net_device *dev,
922 			       const unsigned char *addr, u16 vid)
923 {
924 	struct ocelot_port_private *priv = netdev_priv(dev);
925 	struct ocelot *ocelot = priv->port.ocelot;
926 	int port = priv->chip_port;
927 
928 	return ocelot_fdb_del(ocelot, port, addr, vid);
929 }
930 
931 struct ocelot_dump_ctx {
932 	struct net_device *dev;
933 	struct sk_buff *skb;
934 	struct netlink_callback *cb;
935 	int idx;
936 };
937 
938 static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
939 				   bool is_static, void *data)
940 {
941 	struct ocelot_dump_ctx *dump = data;
942 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
943 	u32 seq = dump->cb->nlh->nlmsg_seq;
944 	struct nlmsghdr *nlh;
945 	struct ndmsg *ndm;
946 
947 	if (dump->idx < dump->cb->args[2])
948 		goto skip;
949 
950 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
951 			sizeof(*ndm), NLM_F_MULTI);
952 	if (!nlh)
953 		return -EMSGSIZE;
954 
955 	ndm = nlmsg_data(nlh);
956 	ndm->ndm_family  = AF_BRIDGE;
957 	ndm->ndm_pad1    = 0;
958 	ndm->ndm_pad2    = 0;
959 	ndm->ndm_flags   = NTF_SELF;
960 	ndm->ndm_type    = 0;
961 	ndm->ndm_ifindex = dump->dev->ifindex;
962 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
963 
964 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
965 		goto nla_put_failure;
966 
967 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
968 		goto nla_put_failure;
969 
970 	nlmsg_end(dump->skb, nlh);
971 
972 skip:
973 	dump->idx++;
974 	return 0;
975 
976 nla_put_failure:
977 	nlmsg_cancel(dump->skb, nlh);
978 	return -EMSGSIZE;
979 }
980 
981 static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
982 			    struct ocelot_mact_entry *entry)
983 {
984 	u32 val, dst, macl, mach;
985 	char mac[ETH_ALEN];
986 
987 	/* Set row and column to read from */
988 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
989 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
990 
991 	/* Issue a read command */
992 	ocelot_write(ocelot,
993 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
994 		     ANA_TABLES_MACACCESS);
995 
996 	if (ocelot_mact_wait_for_completion(ocelot))
997 		return -ETIMEDOUT;
998 
999 	/* Read the entry flags */
1000 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1001 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1002 		return -EINVAL;
1003 
1004 	/* If the entry read has another port configured as its destination,
1005 	 * do not report it.
1006 	 */
1007 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1008 	if (dst != port)
1009 		return -EINVAL;
1010 
1011 	/* Get the entry's MAC address and VLAN id */
1012 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1013 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1014 
1015 	mac[0] = (mach >> 8)  & 0xff;
1016 	mac[1] = (mach >> 0)  & 0xff;
1017 	mac[2] = (macl >> 24) & 0xff;
1018 	mac[3] = (macl >> 16) & 0xff;
1019 	mac[4] = (macl >> 8)  & 0xff;
1020 	mac[5] = (macl >> 0)  & 0xff;
1021 
1022 	entry->vid = (mach >> 16) & 0xfff;
1023 	ether_addr_copy(entry->mac, mac);
1024 
1025 	return 0;
1026 }
1027 
1028 int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1029 		    dsa_fdb_dump_cb_t *cb, void *data)
1030 {
1031 	int i, j;
1032 
1033 	/* Loop through all the mac tables entries. There are 1024 rows of 4
1034 	 * entries.
1035 	 */
1036 	for (i = 0; i < 1024; i++) {
1037 		for (j = 0; j < 4; j++) {
1038 			struct ocelot_mact_entry entry;
1039 			bool is_static;
1040 			int ret;
1041 
1042 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
1043 			/* If the entry is invalid (wrong port, invalid...),
1044 			 * skip it.
1045 			 */
1046 			if (ret == -EINVAL)
1047 				continue;
1048 			else if (ret)
1049 				return ret;
1050 
1051 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1052 
1053 			ret = cb(entry.mac, entry.vid, is_static, data);
1054 			if (ret)
1055 				return ret;
1056 		}
1057 	}
1058 
1059 	return 0;
1060 }
1061 EXPORT_SYMBOL(ocelot_fdb_dump);
1062 
1063 static int ocelot_port_fdb_dump(struct sk_buff *skb,
1064 				struct netlink_callback *cb,
1065 				struct net_device *dev,
1066 				struct net_device *filter_dev, int *idx)
1067 {
1068 	struct ocelot_port_private *priv = netdev_priv(dev);
1069 	struct ocelot *ocelot = priv->port.ocelot;
1070 	struct ocelot_dump_ctx dump = {
1071 		.dev = dev,
1072 		.skb = skb,
1073 		.cb = cb,
1074 		.idx = *idx,
1075 	};
1076 	int port = priv->chip_port;
1077 	int ret;
1078 
1079 	ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
1080 
1081 	*idx = dump.idx;
1082 
1083 	return ret;
1084 }
1085 
1086 static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1087 				  u16 vid)
1088 {
1089 	return ocelot_vlan_vid_add(dev, vid, false, false);
1090 }
1091 
1092 static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1093 				   u16 vid)
1094 {
1095 	return ocelot_vlan_vid_del(dev, vid);
1096 }
1097 
1098 static int ocelot_set_features(struct net_device *dev,
1099 			       netdev_features_t features)
1100 {
1101 	netdev_features_t changed = dev->features ^ features;
1102 	struct ocelot_port_private *priv = netdev_priv(dev);
1103 	struct ocelot *ocelot = priv->port.ocelot;
1104 	int port = priv->chip_port;
1105 
1106 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
1107 	    priv->tc.offload_cnt) {
1108 		netdev_err(dev,
1109 			   "Cannot disable HW TC offload while offloads active\n");
1110 		return -EBUSY;
1111 	}
1112 
1113 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
1114 		ocelot_vlan_mode(ocelot, port, features);
1115 
1116 	return 0;
1117 }
1118 
1119 static int ocelot_get_port_parent_id(struct net_device *dev,
1120 				     struct netdev_phys_item_id *ppid)
1121 {
1122 	struct ocelot_port_private *priv = netdev_priv(dev);
1123 	struct ocelot *ocelot = priv->port.ocelot;
1124 
1125 	ppid->id_len = sizeof(ocelot->base_mac);
1126 	memcpy(&ppid->id, &ocelot->base_mac, ppid->id_len);
1127 
1128 	return 0;
1129 }
1130 
1131 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
1132 {
1133 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1134 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1135 }
1136 EXPORT_SYMBOL(ocelot_hwstamp_get);
1137 
1138 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
1139 {
1140 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1141 	struct hwtstamp_config cfg;
1142 
1143 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1144 		return -EFAULT;
1145 
1146 	/* reserved for future extensions */
1147 	if (cfg.flags)
1148 		return -EINVAL;
1149 
1150 	/* Tx type sanity check */
1151 	switch (cfg.tx_type) {
1152 	case HWTSTAMP_TX_ON:
1153 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
1154 		break;
1155 	case HWTSTAMP_TX_ONESTEP_SYNC:
1156 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1157 		 * need to update the origin time.
1158 		 */
1159 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
1160 		break;
1161 	case HWTSTAMP_TX_OFF:
1162 		ocelot_port->ptp_cmd = 0;
1163 		break;
1164 	default:
1165 		return -ERANGE;
1166 	}
1167 
1168 	mutex_lock(&ocelot->ptp_lock);
1169 
1170 	switch (cfg.rx_filter) {
1171 	case HWTSTAMP_FILTER_NONE:
1172 		break;
1173 	case HWTSTAMP_FILTER_ALL:
1174 	case HWTSTAMP_FILTER_SOME:
1175 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1176 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1177 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1178 	case HWTSTAMP_FILTER_NTP_ALL:
1179 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1180 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1181 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1182 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1183 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1184 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1185 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
1186 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1187 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1188 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1189 		break;
1190 	default:
1191 		mutex_unlock(&ocelot->ptp_lock);
1192 		return -ERANGE;
1193 	}
1194 
1195 	/* Commit back the result & save it */
1196 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1197 	mutex_unlock(&ocelot->ptp_lock);
1198 
1199 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1200 }
1201 EXPORT_SYMBOL(ocelot_hwstamp_set);
1202 
1203 static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1204 {
1205 	struct ocelot_port_private *priv = netdev_priv(dev);
1206 	struct ocelot *ocelot = priv->port.ocelot;
1207 	int port = priv->chip_port;
1208 
1209 	/* The function is only used for PTP operations for now */
1210 	if (!ocelot->ptp)
1211 		return -EOPNOTSUPP;
1212 
1213 	switch (cmd) {
1214 	case SIOCSHWTSTAMP:
1215 		return ocelot_hwstamp_set(ocelot, port, ifr);
1216 	case SIOCGHWTSTAMP:
1217 		return ocelot_hwstamp_get(ocelot, port, ifr);
1218 	default:
1219 		return -EOPNOTSUPP;
1220 	}
1221 }
1222 
1223 static const struct net_device_ops ocelot_port_netdev_ops = {
1224 	.ndo_open			= ocelot_port_open,
1225 	.ndo_stop			= ocelot_port_stop,
1226 	.ndo_start_xmit			= ocelot_port_xmit,
1227 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
1228 	.ndo_get_phys_port_name		= ocelot_port_get_phys_port_name,
1229 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
1230 	.ndo_get_stats64		= ocelot_get_stats64,
1231 	.ndo_fdb_add			= ocelot_port_fdb_add,
1232 	.ndo_fdb_del			= ocelot_port_fdb_del,
1233 	.ndo_fdb_dump			= ocelot_port_fdb_dump,
1234 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
1235 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
1236 	.ndo_set_features		= ocelot_set_features,
1237 	.ndo_get_port_parent_id		= ocelot_get_port_parent_id,
1238 	.ndo_setup_tc			= ocelot_setup_tc,
1239 	.ndo_do_ioctl			= ocelot_ioctl,
1240 };
1241 
1242 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1243 {
1244 	int i;
1245 
1246 	if (sset != ETH_SS_STATS)
1247 		return;
1248 
1249 	for (i = 0; i < ocelot->num_stats; i++)
1250 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1251 		       ETH_GSTRING_LEN);
1252 }
1253 EXPORT_SYMBOL(ocelot_get_strings);
1254 
1255 static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
1256 				    u8 *data)
1257 {
1258 	struct ocelot_port_private *priv = netdev_priv(netdev);
1259 	struct ocelot *ocelot = priv->port.ocelot;
1260 	int port = priv->chip_port;
1261 
1262 	ocelot_get_strings(ocelot, port, sset, data);
1263 }
1264 
1265 static void ocelot_update_stats(struct ocelot *ocelot)
1266 {
1267 	int i, j;
1268 
1269 	mutex_lock(&ocelot->stats_lock);
1270 
1271 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1272 		/* Configure the port to read the stats from */
1273 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1274 
1275 		for (j = 0; j < ocelot->num_stats; j++) {
1276 			u32 val;
1277 			unsigned int idx = i * ocelot->num_stats + j;
1278 
1279 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1280 					      ocelot->stats_layout[j].offset);
1281 
1282 			if (val < (ocelot->stats[idx] & U32_MAX))
1283 				ocelot->stats[idx] += (u64)1 << 32;
1284 
1285 			ocelot->stats[idx] = (ocelot->stats[idx] &
1286 					      ~(u64)U32_MAX) + val;
1287 		}
1288 	}
1289 
1290 	mutex_unlock(&ocelot->stats_lock);
1291 }
1292 
1293 static void ocelot_check_stats_work(struct work_struct *work)
1294 {
1295 	struct delayed_work *del_work = to_delayed_work(work);
1296 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
1297 					     stats_work);
1298 
1299 	ocelot_update_stats(ocelot);
1300 
1301 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1302 			   OCELOT_STATS_CHECK_DELAY);
1303 }
1304 
1305 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1306 {
1307 	int i;
1308 
1309 	/* check and update now */
1310 	ocelot_update_stats(ocelot);
1311 
1312 	/* Copy all counters */
1313 	for (i = 0; i < ocelot->num_stats; i++)
1314 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1315 }
1316 EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1317 
1318 static void ocelot_port_get_ethtool_stats(struct net_device *dev,
1319 					  struct ethtool_stats *stats,
1320 					  u64 *data)
1321 {
1322 	struct ocelot_port_private *priv = netdev_priv(dev);
1323 	struct ocelot *ocelot = priv->port.ocelot;
1324 	int port = priv->chip_port;
1325 
1326 	ocelot_get_ethtool_stats(ocelot, port, data);
1327 }
1328 
1329 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1330 {
1331 	if (sset != ETH_SS_STATS)
1332 		return -EOPNOTSUPP;
1333 
1334 	return ocelot->num_stats;
1335 }
1336 EXPORT_SYMBOL(ocelot_get_sset_count);
1337 
1338 static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
1339 {
1340 	struct ocelot_port_private *priv = netdev_priv(dev);
1341 	struct ocelot *ocelot = priv->port.ocelot;
1342 	int port = priv->chip_port;
1343 
1344 	return ocelot_get_sset_count(ocelot, port, sset);
1345 }
1346 
1347 int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1348 		       struct ethtool_ts_info *info)
1349 {
1350 	info->phc_index = ocelot->ptp_clock ?
1351 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1352 	if (info->phc_index == -1) {
1353 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1354 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1355 					 SOF_TIMESTAMPING_SOFTWARE;
1356 		return 0;
1357 	}
1358 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1359 				 SOF_TIMESTAMPING_RX_SOFTWARE |
1360 				 SOF_TIMESTAMPING_SOFTWARE |
1361 				 SOF_TIMESTAMPING_TX_HARDWARE |
1362 				 SOF_TIMESTAMPING_RX_HARDWARE |
1363 				 SOF_TIMESTAMPING_RAW_HARDWARE;
1364 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1365 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1366 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
1367 
1368 	return 0;
1369 }
1370 EXPORT_SYMBOL(ocelot_get_ts_info);
1371 
1372 static int ocelot_port_get_ts_info(struct net_device *dev,
1373 				   struct ethtool_ts_info *info)
1374 {
1375 	struct ocelot_port_private *priv = netdev_priv(dev);
1376 	struct ocelot *ocelot = priv->port.ocelot;
1377 	int port = priv->chip_port;
1378 
1379 	if (!ocelot->ptp)
1380 		return ethtool_op_get_ts_info(dev, info);
1381 
1382 	return ocelot_get_ts_info(ocelot, port, info);
1383 }
1384 
1385 static const struct ethtool_ops ocelot_ethtool_ops = {
1386 	.get_strings		= ocelot_port_get_strings,
1387 	.get_ethtool_stats	= ocelot_port_get_ethtool_stats,
1388 	.get_sset_count		= ocelot_port_get_sset_count,
1389 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1390 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1391 	.get_ts_info		= ocelot_port_get_ts_info,
1392 };
1393 
1394 void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1395 {
1396 	u32 port_cfg;
1397 	int p, i;
1398 
1399 	if (!(BIT(port) & ocelot->bridge_mask))
1400 		return;
1401 
1402 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1403 
1404 	switch (state) {
1405 	case BR_STATE_FORWARDING:
1406 		ocelot->bridge_fwd_mask |= BIT(port);
1407 		/* Fallthrough */
1408 	case BR_STATE_LEARNING:
1409 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1410 		break;
1411 
1412 	default:
1413 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1414 		ocelot->bridge_fwd_mask &= ~BIT(port);
1415 		break;
1416 	}
1417 
1418 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
1419 
1420 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1421 	 * a source for the other ports.
1422 	 */
1423 	for (p = 0; p < ocelot->num_phys_ports; p++) {
1424 		if (ocelot->bridge_fwd_mask & BIT(p)) {
1425 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
1426 
1427 			for (i = 0; i < ocelot->num_phys_ports; i++) {
1428 				unsigned long bond_mask = ocelot->lags[i];
1429 
1430 				if (!bond_mask)
1431 					continue;
1432 
1433 				if (bond_mask & BIT(p)) {
1434 					mask &= ~bond_mask;
1435 					break;
1436 				}
1437 			}
1438 
1439 			ocelot_write_rix(ocelot, mask,
1440 					 ANA_PGID_PGID, PGID_SRC + p);
1441 		} else {
1442 			ocelot_write_rix(ocelot, 0,
1443 					 ANA_PGID_PGID, PGID_SRC + p);
1444 		}
1445 	}
1446 }
1447 EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1448 
1449 static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
1450 					   struct switchdev_trans *trans,
1451 					   u8 state)
1452 {
1453 	if (switchdev_trans_ph_prepare(trans))
1454 		return;
1455 
1456 	ocelot_bridge_stp_state_set(ocelot, port, state);
1457 }
1458 
1459 void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
1460 {
1461 	ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(msecs / 2),
1462 		     ANA_AUTOAGE);
1463 }
1464 EXPORT_SYMBOL(ocelot_set_ageing_time);
1465 
1466 static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
1467 					unsigned long ageing_clock_t)
1468 {
1469 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1470 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1471 
1472 	ocelot_set_ageing_time(ocelot, ageing_time);
1473 }
1474 
1475 static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
1476 {
1477 	u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1478 			    ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1479 			    ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1480 	u32 val = 0;
1481 
1482 	if (mc)
1483 		val = cpu_fwd_mcast;
1484 
1485 	ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
1486 		       ANA_PORT_CPU_FWD_CFG, port);
1487 }
1488 
1489 static int ocelot_port_attr_set(struct net_device *dev,
1490 				const struct switchdev_attr *attr,
1491 				struct switchdev_trans *trans)
1492 {
1493 	struct ocelot_port_private *priv = netdev_priv(dev);
1494 	struct ocelot *ocelot = priv->port.ocelot;
1495 	int port = priv->chip_port;
1496 	int err = 0;
1497 
1498 	switch (attr->id) {
1499 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1500 		ocelot_port_attr_stp_state_set(ocelot, port, trans,
1501 					       attr->u.stp_state);
1502 		break;
1503 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1504 		ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
1505 		break;
1506 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1507 		ocelot_port_vlan_filtering(ocelot, port,
1508 					   attr->u.vlan_filtering);
1509 		break;
1510 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1511 		ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
1512 		break;
1513 	default:
1514 		err = -EOPNOTSUPP;
1515 		break;
1516 	}
1517 
1518 	return err;
1519 }
1520 
1521 static int ocelot_port_obj_add_vlan(struct net_device *dev,
1522 				    const struct switchdev_obj_port_vlan *vlan,
1523 				    struct switchdev_trans *trans)
1524 {
1525 	int ret;
1526 	u16 vid;
1527 
1528 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1529 		ret = ocelot_vlan_vid_add(dev, vid,
1530 					  vlan->flags & BRIDGE_VLAN_INFO_PVID,
1531 					  vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
1532 		if (ret)
1533 			return ret;
1534 	}
1535 
1536 	return 0;
1537 }
1538 
1539 static int ocelot_port_vlan_del_vlan(struct net_device *dev,
1540 				     const struct switchdev_obj_port_vlan *vlan)
1541 {
1542 	int ret;
1543 	u16 vid;
1544 
1545 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1546 		ret = ocelot_vlan_vid_del(dev, vid);
1547 
1548 		if (ret)
1549 			return ret;
1550 	}
1551 
1552 	return 0;
1553 }
1554 
1555 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1556 						     const unsigned char *addr,
1557 						     u16 vid)
1558 {
1559 	struct ocelot_multicast *mc;
1560 
1561 	list_for_each_entry(mc, &ocelot->multicast, list) {
1562 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1563 			return mc;
1564 	}
1565 
1566 	return NULL;
1567 }
1568 
1569 static int ocelot_port_obj_add_mdb(struct net_device *dev,
1570 				   const struct switchdev_obj_port_mdb *mdb,
1571 				   struct switchdev_trans *trans)
1572 {
1573 	struct ocelot_port_private *priv = netdev_priv(dev);
1574 	struct ocelot_port *ocelot_port = &priv->port;
1575 	struct ocelot *ocelot = ocelot_port->ocelot;
1576 	unsigned char addr[ETH_ALEN];
1577 	struct ocelot_multicast *mc;
1578 	int port = priv->chip_port;
1579 	u16 vid = mdb->vid;
1580 	bool new = false;
1581 
1582 	if (!vid)
1583 		vid = ocelot_port->pvid;
1584 
1585 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1586 	if (!mc) {
1587 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1588 		if (!mc)
1589 			return -ENOMEM;
1590 
1591 		memcpy(mc->addr, mdb->addr, ETH_ALEN);
1592 		mc->vid = vid;
1593 
1594 		list_add_tail(&mc->list, &ocelot->multicast);
1595 		new = true;
1596 	}
1597 
1598 	memcpy(addr, mc->addr, ETH_ALEN);
1599 	addr[0] = 0;
1600 
1601 	if (!new) {
1602 		addr[2] = mc->ports << 0;
1603 		addr[1] = mc->ports << 8;
1604 		ocelot_mact_forget(ocelot, addr, vid);
1605 	}
1606 
1607 	mc->ports |= BIT(port);
1608 	addr[2] = mc->ports << 0;
1609 	addr[1] = mc->ports << 8;
1610 
1611 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1612 }
1613 
1614 static int ocelot_port_obj_del_mdb(struct net_device *dev,
1615 				   const struct switchdev_obj_port_mdb *mdb)
1616 {
1617 	struct ocelot_port_private *priv = netdev_priv(dev);
1618 	struct ocelot_port *ocelot_port = &priv->port;
1619 	struct ocelot *ocelot = ocelot_port->ocelot;
1620 	unsigned char addr[ETH_ALEN];
1621 	struct ocelot_multicast *mc;
1622 	int port = priv->chip_port;
1623 	u16 vid = mdb->vid;
1624 
1625 	if (!vid)
1626 		vid = ocelot_port->pvid;
1627 
1628 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1629 	if (!mc)
1630 		return -ENOENT;
1631 
1632 	memcpy(addr, mc->addr, ETH_ALEN);
1633 	addr[2] = mc->ports << 0;
1634 	addr[1] = mc->ports << 8;
1635 	addr[0] = 0;
1636 	ocelot_mact_forget(ocelot, addr, vid);
1637 
1638 	mc->ports &= ~BIT(port);
1639 	if (!mc->ports) {
1640 		list_del(&mc->list);
1641 		devm_kfree(ocelot->dev, mc);
1642 		return 0;
1643 	}
1644 
1645 	addr[2] = mc->ports << 0;
1646 	addr[1] = mc->ports << 8;
1647 
1648 	return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1649 }
1650 
1651 static int ocelot_port_obj_add(struct net_device *dev,
1652 			       const struct switchdev_obj *obj,
1653 			       struct switchdev_trans *trans,
1654 			       struct netlink_ext_ack *extack)
1655 {
1656 	int ret = 0;
1657 
1658 	switch (obj->id) {
1659 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1660 		ret = ocelot_port_obj_add_vlan(dev,
1661 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
1662 					       trans);
1663 		break;
1664 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1665 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1666 					      trans);
1667 		break;
1668 	default:
1669 		return -EOPNOTSUPP;
1670 	}
1671 
1672 	return ret;
1673 }
1674 
1675 static int ocelot_port_obj_del(struct net_device *dev,
1676 			       const struct switchdev_obj *obj)
1677 {
1678 	int ret = 0;
1679 
1680 	switch (obj->id) {
1681 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1682 		ret = ocelot_port_vlan_del_vlan(dev,
1683 						SWITCHDEV_OBJ_PORT_VLAN(obj));
1684 		break;
1685 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1686 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1687 		break;
1688 	default:
1689 		return -EOPNOTSUPP;
1690 	}
1691 
1692 	return ret;
1693 }
1694 
1695 int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1696 			    struct net_device *bridge)
1697 {
1698 	if (!ocelot->bridge_mask) {
1699 		ocelot->hw_bridge_dev = bridge;
1700 	} else {
1701 		if (ocelot->hw_bridge_dev != bridge)
1702 			/* This is adding the port to a second bridge, this is
1703 			 * unsupported */
1704 			return -ENODEV;
1705 	}
1706 
1707 	ocelot->bridge_mask |= BIT(port);
1708 
1709 	return 0;
1710 }
1711 EXPORT_SYMBOL(ocelot_port_bridge_join);
1712 
1713 int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1714 			     struct net_device *bridge)
1715 {
1716 	ocelot->bridge_mask &= ~BIT(port);
1717 
1718 	if (!ocelot->bridge_mask)
1719 		ocelot->hw_bridge_dev = NULL;
1720 
1721 	ocelot_port_vlan_filtering(ocelot, port, 0);
1722 	ocelot_port_set_pvid(ocelot, port, 0);
1723 	return ocelot_port_set_native_vlan(ocelot, port, 0);
1724 }
1725 EXPORT_SYMBOL(ocelot_port_bridge_leave);
1726 
1727 static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1728 {
1729 	int i, port, lag;
1730 
1731 	/* Reset destination and aggregation PGIDS */
1732 	for (port = 0; port < ocelot->num_phys_ports; port++)
1733 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1734 
1735 	for (i = PGID_AGGR; i < PGID_SRC; i++)
1736 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1737 				 ANA_PGID_PGID, i);
1738 
1739 	/* Now, set PGIDs for each LAG */
1740 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1741 		unsigned long bond_mask;
1742 		int aggr_count = 0;
1743 		u8 aggr_idx[16];
1744 
1745 		bond_mask = ocelot->lags[lag];
1746 		if (!bond_mask)
1747 			continue;
1748 
1749 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1750 			// Destination mask
1751 			ocelot_write_rix(ocelot, bond_mask,
1752 					 ANA_PGID_PGID, port);
1753 			aggr_idx[aggr_count] = port;
1754 			aggr_count++;
1755 		}
1756 
1757 		for (i = PGID_AGGR; i < PGID_SRC; i++) {
1758 			u32 ac;
1759 
1760 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1761 			ac &= ~bond_mask;
1762 			ac |= BIT(aggr_idx[i % aggr_count]);
1763 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1764 		}
1765 	}
1766 }
1767 
1768 static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1769 {
1770 	unsigned long bond_mask = ocelot->lags[lag];
1771 	unsigned int p;
1772 
1773 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1774 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1775 
1776 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1777 
1778 		/* Use lag port as logical port for port i */
1779 		ocelot_write_gix(ocelot, port_cfg |
1780 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1781 				 ANA_PORT_PORT_CFG, p);
1782 	}
1783 }
1784 
1785 static int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1786 				struct net_device *bond)
1787 {
1788 	struct net_device *ndev;
1789 	u32 bond_mask = 0;
1790 	int lag, lp;
1791 
1792 	rcu_read_lock();
1793 	for_each_netdev_in_bond_rcu(bond, ndev) {
1794 		struct ocelot_port_private *priv = netdev_priv(ndev);
1795 
1796 		bond_mask |= BIT(priv->chip_port);
1797 	}
1798 	rcu_read_unlock();
1799 
1800 	lp = __ffs(bond_mask);
1801 
1802 	/* If the new port is the lowest one, use it as the logical port from
1803 	 * now on
1804 	 */
1805 	if (port == lp) {
1806 		lag = port;
1807 		ocelot->lags[port] = bond_mask;
1808 		bond_mask &= ~BIT(port);
1809 		if (bond_mask) {
1810 			lp = __ffs(bond_mask);
1811 			ocelot->lags[lp] = 0;
1812 		}
1813 	} else {
1814 		lag = lp;
1815 		ocelot->lags[lp] |= BIT(port);
1816 	}
1817 
1818 	ocelot_setup_lag(ocelot, lag);
1819 	ocelot_set_aggr_pgids(ocelot);
1820 
1821 	return 0;
1822 }
1823 
1824 static void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1825 				  struct net_device *bond)
1826 {
1827 	u32 port_cfg;
1828 	int i;
1829 
1830 	/* Remove port from any lag */
1831 	for (i = 0; i < ocelot->num_phys_ports; i++)
1832 		ocelot->lags[i] &= ~BIT(port);
1833 
1834 	/* if it was the logical port of the lag, move the lag config to the
1835 	 * next port
1836 	 */
1837 	if (ocelot->lags[port]) {
1838 		int n = __ffs(ocelot->lags[port]);
1839 
1840 		ocelot->lags[n] = ocelot->lags[port];
1841 		ocelot->lags[port] = 0;
1842 
1843 		ocelot_setup_lag(ocelot, n);
1844 	}
1845 
1846 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1847 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1848 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1849 			 ANA_PORT_PORT_CFG, port);
1850 
1851 	ocelot_set_aggr_pgids(ocelot);
1852 }
1853 
1854 /* Checks if the net_device instance given to us originate from our driver. */
1855 static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1856 {
1857 	return dev->netdev_ops == &ocelot_port_netdev_ops;
1858 }
1859 
1860 static int ocelot_netdevice_port_event(struct net_device *dev,
1861 				       unsigned long event,
1862 				       struct netdev_notifier_changeupper_info *info)
1863 {
1864 	struct ocelot_port_private *priv = netdev_priv(dev);
1865 	struct ocelot_port *ocelot_port = &priv->port;
1866 	struct ocelot *ocelot = ocelot_port->ocelot;
1867 	int port = priv->chip_port;
1868 	int err = 0;
1869 
1870 	switch (event) {
1871 	case NETDEV_CHANGEUPPER:
1872 		if (netif_is_bridge_master(info->upper_dev)) {
1873 			if (info->linking) {
1874 				err = ocelot_port_bridge_join(ocelot, port,
1875 							      info->upper_dev);
1876 			} else {
1877 				err = ocelot_port_bridge_leave(ocelot, port,
1878 							       info->upper_dev);
1879 			}
1880 		}
1881 		if (netif_is_lag_master(info->upper_dev)) {
1882 			if (info->linking)
1883 				err = ocelot_port_lag_join(ocelot, port,
1884 							   info->upper_dev);
1885 			else
1886 				ocelot_port_lag_leave(ocelot, port,
1887 						      info->upper_dev);
1888 		}
1889 		break;
1890 	default:
1891 		break;
1892 	}
1893 
1894 	return err;
1895 }
1896 
1897 static int ocelot_netdevice_event(struct notifier_block *unused,
1898 				  unsigned long event, void *ptr)
1899 {
1900 	struct netdev_notifier_changeupper_info *info = ptr;
1901 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1902 	int ret = 0;
1903 
1904 	if (!ocelot_netdevice_dev_check(dev))
1905 		return 0;
1906 
1907 	if (event == NETDEV_PRECHANGEUPPER &&
1908 	    netif_is_lag_master(info->upper_dev)) {
1909 		struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1910 		struct netlink_ext_ack *extack;
1911 
1912 		if (lag_upper_info &&
1913 		    lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1914 			extack = netdev_notifier_info_to_extack(&info->info);
1915 			NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1916 
1917 			ret = -EINVAL;
1918 			goto notify;
1919 		}
1920 	}
1921 
1922 	if (netif_is_lag_master(dev)) {
1923 		struct net_device *slave;
1924 		struct list_head *iter;
1925 
1926 		netdev_for_each_lower_dev(dev, slave, iter) {
1927 			ret = ocelot_netdevice_port_event(slave, event, info);
1928 			if (ret)
1929 				goto notify;
1930 		}
1931 	} else {
1932 		ret = ocelot_netdevice_port_event(dev, event, info);
1933 	}
1934 
1935 notify:
1936 	return notifier_from_errno(ret);
1937 }
1938 
1939 struct notifier_block ocelot_netdevice_nb __read_mostly = {
1940 	.notifier_call = ocelot_netdevice_event,
1941 };
1942 EXPORT_SYMBOL(ocelot_netdevice_nb);
1943 
1944 static int ocelot_switchdev_event(struct notifier_block *unused,
1945 				  unsigned long event, void *ptr)
1946 {
1947 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1948 	int err;
1949 
1950 	switch (event) {
1951 	case SWITCHDEV_PORT_ATTR_SET:
1952 		err = switchdev_handle_port_attr_set(dev, ptr,
1953 						     ocelot_netdevice_dev_check,
1954 						     ocelot_port_attr_set);
1955 		return notifier_from_errno(err);
1956 	}
1957 
1958 	return NOTIFY_DONE;
1959 }
1960 
1961 struct notifier_block ocelot_switchdev_nb __read_mostly = {
1962 	.notifier_call = ocelot_switchdev_event,
1963 };
1964 EXPORT_SYMBOL(ocelot_switchdev_nb);
1965 
1966 static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1967 					   unsigned long event, void *ptr)
1968 {
1969 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1970 	int err;
1971 
1972 	switch (event) {
1973 		/* Blocking events. */
1974 	case SWITCHDEV_PORT_OBJ_ADD:
1975 		err = switchdev_handle_port_obj_add(dev, ptr,
1976 						    ocelot_netdevice_dev_check,
1977 						    ocelot_port_obj_add);
1978 		return notifier_from_errno(err);
1979 	case SWITCHDEV_PORT_OBJ_DEL:
1980 		err = switchdev_handle_port_obj_del(dev, ptr,
1981 						    ocelot_netdevice_dev_check,
1982 						    ocelot_port_obj_del);
1983 		return notifier_from_errno(err);
1984 	case SWITCHDEV_PORT_ATTR_SET:
1985 		err = switchdev_handle_port_attr_set(dev, ptr,
1986 						     ocelot_netdevice_dev_check,
1987 						     ocelot_port_attr_set);
1988 		return notifier_from_errno(err);
1989 	}
1990 
1991 	return NOTIFY_DONE;
1992 }
1993 
1994 struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1995 	.notifier_call = ocelot_switchdev_blocking_event,
1996 };
1997 EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
1998 
1999 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2000  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
2001  * In the special case that it's the NPI port that we're configuring, the
2002  * length of the tag and optional prefix needs to be accounted for privately,
2003  * in order to be able to sustain communication at the requested @sdu.
2004  */
2005 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
2006 {
2007 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2008 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2009 	int atop_wm;
2010 
2011 	if (port == ocelot->npi) {
2012 		maxlen += OCELOT_TAG_LEN;
2013 
2014 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
2015 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2016 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
2017 			maxlen += OCELOT_LONG_PREFIX_LEN;
2018 	}
2019 
2020 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2021 
2022 	/* Set Pause WM hysteresis
2023 	 * 152 = 6 * maxlen / OCELOT_BUFFER_CELL_SZ
2024 	 * 101 = 4 * maxlen / OCELOT_BUFFER_CELL_SZ
2025 	 */
2026 	ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
2027 			 SYS_PAUSE_CFG_PAUSE_STOP(101) |
2028 			 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, port);
2029 
2030 	/* Tail dropping watermark */
2031 	atop_wm = (ocelot->shared_queue_sz - 9 * maxlen) /
2032 		   OCELOT_BUFFER_CELL_SZ;
2033 	ocelot_write_rix(ocelot, ocelot_wm_enc(9 * maxlen),
2034 			 SYS_ATOP, port);
2035 	ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
2036 }
2037 EXPORT_SYMBOL(ocelot_port_set_maxlen);
2038 
2039 int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
2040 {
2041 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
2042 
2043 	if (port == ocelot->npi) {
2044 		max_mtu -= OCELOT_TAG_LEN;
2045 
2046 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
2047 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2048 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
2049 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
2050 	}
2051 
2052 	return max_mtu;
2053 }
2054 EXPORT_SYMBOL(ocelot_get_max_mtu);
2055 
2056 void ocelot_init_port(struct ocelot *ocelot, int port)
2057 {
2058 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2059 
2060 	skb_queue_head_init(&ocelot_port->tx_skbs);
2061 
2062 	/* Basic L2 initialization */
2063 
2064 	/* Set MAC IFG Gaps
2065 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
2066 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
2067 	 */
2068 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
2069 			   DEV_MAC_IFG_CFG);
2070 
2071 	/* Load seed (0) and set MAC HDX late collision  */
2072 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
2073 			   DEV_MAC_HDX_CFG_SEED_LOAD,
2074 			   DEV_MAC_HDX_CFG);
2075 	mdelay(1);
2076 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
2077 			   DEV_MAC_HDX_CFG);
2078 
2079 	/* Set Max Length and maximum tags allowed */
2080 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
2081 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
2082 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2083 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
2084 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
2085 			   DEV_MAC_TAGS_CFG);
2086 
2087 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
2088 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
2089 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
2090 
2091 	/* Drop frames with multicast source address */
2092 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2093 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2094 		       ANA_PORT_DROP_CFG, port);
2095 
2096 	/* Set default VLAN and tag type to 8021Q. */
2097 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2098 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
2099 		       REW_PORT_VLAN_CFG, port);
2100 
2101 	/* Enable vcap lookups */
2102 	ocelot_vcap_enable(ocelot, port);
2103 }
2104 EXPORT_SYMBOL(ocelot_init_port);
2105 
2106 int ocelot_probe_port(struct ocelot *ocelot, u8 port,
2107 		      void __iomem *regs,
2108 		      struct phy_device *phy)
2109 {
2110 	struct ocelot_port_private *priv;
2111 	struct ocelot_port *ocelot_port;
2112 	struct net_device *dev;
2113 	int err;
2114 
2115 	dev = alloc_etherdev(sizeof(struct ocelot_port_private));
2116 	if (!dev)
2117 		return -ENOMEM;
2118 	SET_NETDEV_DEV(dev, ocelot->dev);
2119 	priv = netdev_priv(dev);
2120 	priv->dev = dev;
2121 	priv->phy = phy;
2122 	priv->chip_port = port;
2123 	ocelot_port = &priv->port;
2124 	ocelot_port->ocelot = ocelot;
2125 	ocelot_port->regs = regs;
2126 	ocelot->ports[port] = ocelot_port;
2127 
2128 	dev->netdev_ops = &ocelot_port_netdev_ops;
2129 	dev->ethtool_ops = &ocelot_ethtool_ops;
2130 
2131 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
2132 		NETIF_F_HW_TC;
2133 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
2134 
2135 	memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
2136 	dev->dev_addr[ETH_ALEN - 1] += port;
2137 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
2138 			  ENTRYTYPE_LOCKED);
2139 
2140 	ocelot_init_port(ocelot, port);
2141 
2142 	err = register_netdev(dev);
2143 	if (err) {
2144 		dev_err(ocelot->dev, "register_netdev failed\n");
2145 		free_netdev(dev);
2146 	}
2147 
2148 	return err;
2149 }
2150 EXPORT_SYMBOL(ocelot_probe_port);
2151 
2152 /* Configure and enable the CPU port module, which is a set of queues.
2153  * If @npi contains a valid port index, the CPU port module is connected
2154  * to the Node Processor Interface (NPI). This is the mode through which
2155  * frames can be injected from and extracted to an external CPU,
2156  * over Ethernet.
2157  */
2158 void ocelot_configure_cpu(struct ocelot *ocelot, int npi,
2159 			  enum ocelot_tag_prefix injection,
2160 			  enum ocelot_tag_prefix extraction)
2161 {
2162 	int cpu = ocelot->num_phys_ports;
2163 
2164 	ocelot->npi = npi;
2165 	ocelot->inj_prefix = injection;
2166 	ocelot->xtr_prefix = extraction;
2167 
2168 	/* The unicast destination PGID for the CPU port module is unused */
2169 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2170 	/* Instead set up a multicast destination PGID for traffic copied to
2171 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
2172 	 * addresses will be copied to the CPU via this PGID.
2173 	 */
2174 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2175 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2176 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2177 			 ANA_PORT_PORT_CFG, cpu);
2178 
2179 	if (npi >= 0 && npi < ocelot->num_phys_ports) {
2180 		ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
2181 			     QSYS_EXT_CPU_CFG_EXT_CPU_PORT(npi),
2182 			     QSYS_EXT_CPU_CFG);
2183 
2184 		/* Enable NPI port */
2185 		ocelot_write_rix(ocelot,
2186 				 QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2187 				 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2188 				 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2189 				 QSYS_SWITCH_PORT_MODE, npi);
2190 		/* NPI port Injection/Extraction configuration */
2191 		ocelot_write_rix(ocelot,
2192 				 SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
2193 				 SYS_PORT_MODE_INCL_INJ_HDR(injection),
2194 				 SYS_PORT_MODE, npi);
2195 	}
2196 
2197 	/* Enable CPU port module */
2198 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
2199 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
2200 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
2201 			 QSYS_SWITCH_PORT_MODE, cpu);
2202 	/* CPU port Injection/Extraction configuration */
2203 	ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(extraction) |
2204 			 SYS_PORT_MODE_INCL_INJ_HDR(injection),
2205 			 SYS_PORT_MODE, cpu);
2206 
2207 	/* Configure the CPU port to be VLAN aware */
2208 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
2209 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2210 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
2211 			 ANA_PORT_VLAN_CFG, cpu);
2212 }
2213 EXPORT_SYMBOL(ocelot_configure_cpu);
2214 
2215 int ocelot_init(struct ocelot *ocelot)
2216 {
2217 	char queue_name[32];
2218 	int i, ret;
2219 	u32 port;
2220 
2221 	if (ocelot->ops->reset) {
2222 		ret = ocelot->ops->reset(ocelot);
2223 		if (ret) {
2224 			dev_err(ocelot->dev, "Switch reset failed\n");
2225 			return ret;
2226 		}
2227 	}
2228 
2229 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
2230 				    sizeof(u32), GFP_KERNEL);
2231 	if (!ocelot->lags)
2232 		return -ENOMEM;
2233 
2234 	ocelot->stats = devm_kcalloc(ocelot->dev,
2235 				     ocelot->num_phys_ports * ocelot->num_stats,
2236 				     sizeof(u64), GFP_KERNEL);
2237 	if (!ocelot->stats)
2238 		return -ENOMEM;
2239 
2240 	mutex_init(&ocelot->stats_lock);
2241 	mutex_init(&ocelot->ptp_lock);
2242 	spin_lock_init(&ocelot->ptp_clock_lock);
2243 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2244 		 dev_name(ocelot->dev));
2245 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2246 	if (!ocelot->stats_queue)
2247 		return -ENOMEM;
2248 
2249 	INIT_LIST_HEAD(&ocelot->multicast);
2250 	ocelot_mact_init(ocelot);
2251 	ocelot_vlan_init(ocelot);
2252 	ocelot_ace_init(ocelot);
2253 
2254 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2255 		/* Clear all counters (5 groups) */
2256 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2257 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2258 			     SYS_STAT_CFG);
2259 	}
2260 
2261 	/* Only use S-Tag */
2262 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2263 
2264 	/* Aggregation mode */
2265 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2266 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2267 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2268 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
2269 
2270 	/* Set MAC age time to default value. The entry is aged after
2271 	 * 2*AGE_PERIOD
2272 	 */
2273 	ocelot_write(ocelot,
2274 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2275 		     ANA_AUTOAGE);
2276 
2277 	/* Disable learning for frames discarded by VLAN ingress filtering */
2278 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2279 
2280 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2281 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2282 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2283 
2284 	/* Setup flooding PGIDs */
2285 	ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2286 			 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
2287 			 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2288 			 ANA_FLOODING, 0);
2289 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2290 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2291 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2292 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2293 		     ANA_FLOODING_IPMC);
2294 
2295 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2296 		/* Transmit the frame to the local port. */
2297 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2298 		/* Do not forward BPDU frames to the front ports. */
2299 		ocelot_write_gix(ocelot,
2300 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2301 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2302 				 port);
2303 		/* Ensure bridging is disabled */
2304 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2305 	}
2306 
2307 	/* Allow broadcast MAC frames. */
2308 	for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
2309 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2310 
2311 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2312 	}
2313 	ocelot_write_rix(ocelot,
2314 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
2315 			 ANA_PGID_PGID, PGID_MC);
2316 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2317 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2318 
2319 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2320 	 * registers endianness.
2321 	 */
2322 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2323 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2324 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2325 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2326 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2327 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2328 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2329 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2330 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2331 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2332 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2333 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2334 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2335 	for (i = 0; i < 16; i++)
2336 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2337 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2338 				 ANA_CPUQ_8021_CFG, i);
2339 
2340 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2341 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2342 			   OCELOT_STATS_CHECK_DELAY);
2343 
2344 	return 0;
2345 }
2346 EXPORT_SYMBOL(ocelot_init);
2347 
2348 void ocelot_deinit(struct ocelot *ocelot)
2349 {
2350 	struct ocelot_port *port;
2351 	int i;
2352 
2353 	cancel_delayed_work(&ocelot->stats_work);
2354 	destroy_workqueue(ocelot->stats_queue);
2355 	mutex_destroy(&ocelot->stats_lock);
2356 
2357 	for (i = 0; i < ocelot->num_phys_ports; i++) {
2358 		port = ocelot->ports[i];
2359 		skb_queue_purge(&port->tx_skbs);
2360 	}
2361 }
2362 EXPORT_SYMBOL(ocelot_deinit);
2363 
2364 MODULE_LICENSE("Dual MIT/GPL");
2365