xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 842ed298)
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/if_bridge.h>
8 #include <soc/mscc/ocelot_vcap.h>
9 #include "ocelot.h"
10 #include "ocelot_vcap.h"
11 
12 #define TABLE_UPDATE_SLEEP_US 10
13 #define TABLE_UPDATE_TIMEOUT_US 100000
14 
15 struct ocelot_mact_entry {
16 	u8 mac[ETH_ALEN];
17 	u16 vid;
18 	enum macaccess_entry_type type;
19 };
20 
21 static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
22 {
23 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
24 }
25 
26 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
27 {
28 	u32 val;
29 
30 	return readx_poll_timeout(ocelot_mact_read_macaccess,
31 		ocelot, val,
32 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
33 		MACACCESS_CMD_IDLE,
34 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
35 }
36 
37 static void ocelot_mact_select(struct ocelot *ocelot,
38 			       const unsigned char mac[ETH_ALEN],
39 			       unsigned int vid)
40 {
41 	u32 macl = 0, mach = 0;
42 
43 	/* Set the MAC address to handle and the vlan associated in a format
44 	 * understood by the hardware.
45 	 */
46 	mach |= vid    << 16;
47 	mach |= mac[0] << 8;
48 	mach |= mac[1] << 0;
49 	macl |= mac[2] << 24;
50 	macl |= mac[3] << 16;
51 	macl |= mac[4] << 8;
52 	macl |= mac[5] << 0;
53 
54 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
55 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
56 
57 }
58 
59 int ocelot_mact_learn(struct ocelot *ocelot, int port,
60 		      const unsigned char mac[ETH_ALEN],
61 		      unsigned int vid, enum macaccess_entry_type type)
62 {
63 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
64 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
65 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
66 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
67 	unsigned int mc_ports;
68 
69 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
70 	if (type == ENTRYTYPE_MACv4)
71 		mc_ports = (mac[1] << 8) | mac[2];
72 	else if (type == ENTRYTYPE_MACv6)
73 		mc_ports = (mac[0] << 8) | mac[1];
74 	else
75 		mc_ports = 0;
76 
77 	if (mc_ports & BIT(ocelot->num_phys_ports))
78 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
79 
80 	ocelot_mact_select(ocelot, mac, vid);
81 
82 	/* Issue a write command */
83 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
84 
85 	return ocelot_mact_wait_for_completion(ocelot);
86 }
87 EXPORT_SYMBOL(ocelot_mact_learn);
88 
89 int ocelot_mact_forget(struct ocelot *ocelot,
90 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
91 {
92 	ocelot_mact_select(ocelot, mac, vid);
93 
94 	/* Issue a forget command */
95 	ocelot_write(ocelot,
96 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
97 		     ANA_TABLES_MACACCESS);
98 
99 	return ocelot_mact_wait_for_completion(ocelot);
100 }
101 EXPORT_SYMBOL(ocelot_mact_forget);
102 
103 static void ocelot_mact_init(struct ocelot *ocelot)
104 {
105 	/* Configure the learning mode entries attributes:
106 	 * - Do not copy the frame to the CPU extraction queues.
107 	 * - Use the vlan and mac_cpoy for dmac lookup.
108 	 */
109 	ocelot_rmw(ocelot, 0,
110 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
111 		   | ANA_AGENCTRL_LEARN_FWD_KILL
112 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
113 		   ANA_AGENCTRL);
114 
115 	/* Clear the MAC table */
116 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
117 }
118 
119 static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
120 {
121 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
122 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
123 			 ANA_PORT_VCAP_S2_CFG, port);
124 
125 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
126 			 ANA_PORT_VCAP_CFG, port);
127 
128 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
129 		       REW_PORT_CFG_ES0_EN,
130 		       REW_PORT_CFG, port);
131 }
132 
133 static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
134 {
135 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
136 }
137 
138 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
139 {
140 	u32 val;
141 
142 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
143 		ocelot,
144 		val,
145 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
146 		ANA_TABLES_VLANACCESS_CMD_IDLE,
147 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
148 }
149 
150 static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
151 {
152 	/* Select the VID to configure */
153 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
154 		     ANA_TABLES_VLANTIDX);
155 	/* Set the vlan port members mask and issue a write command */
156 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
157 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
158 		     ANA_TABLES_VLANACCESS);
159 
160 	return ocelot_vlant_wait_for_completion(ocelot);
161 }
162 
163 static void ocelot_port_set_native_vlan(struct ocelot *ocelot, int port,
164 					struct ocelot_vlan native_vlan)
165 {
166 	struct ocelot_port *ocelot_port = ocelot->ports[port];
167 	u32 val = 0;
168 
169 	ocelot_port->native_vlan = native_vlan;
170 
171 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_VID(native_vlan.vid),
172 		       REW_PORT_VLAN_CFG_PORT_VID_M,
173 		       REW_PORT_VLAN_CFG, port);
174 
175 	if (ocelot_port->vlan_aware) {
176 		if (native_vlan.valid)
177 			/* Tag all frames except when VID == DEFAULT_VLAN */
178 			val = REW_TAG_CFG_TAG_CFG(1);
179 		else
180 			/* Tag all frames */
181 			val = REW_TAG_CFG_TAG_CFG(3);
182 	} else {
183 		/* Port tagging disabled. */
184 		val = REW_TAG_CFG_TAG_CFG(0);
185 	}
186 	ocelot_rmw_gix(ocelot, val,
187 		       REW_TAG_CFG_TAG_CFG_M,
188 		       REW_TAG_CFG, port);
189 }
190 
191 /* Default vlan to clasify for untagged frames (may be zero) */
192 static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
193 				 struct ocelot_vlan pvid_vlan)
194 {
195 	struct ocelot_port *ocelot_port = ocelot->ports[port];
196 	u32 val = 0;
197 
198 	ocelot_port->pvid_vlan = pvid_vlan;
199 
200 	if (!ocelot_port->vlan_aware)
201 		pvid_vlan.vid = 0;
202 
203 	ocelot_rmw_gix(ocelot,
204 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid_vlan.vid),
205 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
206 		       ANA_PORT_VLAN_CFG, port);
207 
208 	/* If there's no pvid, we should drop not only untagged traffic (which
209 	 * happens automatically), but also 802.1p traffic which gets
210 	 * classified to VLAN 0, but that is always in our RX filter, so it
211 	 * would get accepted were it not for this setting.
212 	 */
213 	if (!pvid_vlan.valid && ocelot_port->vlan_aware)
214 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
215 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
216 
217 	ocelot_rmw_gix(ocelot, val,
218 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
219 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
220 		       ANA_PORT_DROP_CFG, port);
221 }
222 
223 int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
224 			       bool vlan_aware, struct switchdev_trans *trans)
225 {
226 	struct ocelot_port *ocelot_port = ocelot->ports[port];
227 	u32 val;
228 
229 	if (switchdev_trans_ph_prepare(trans)) {
230 		struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
231 		struct ocelot_vcap_filter *filter;
232 
233 		list_for_each_entry(filter, &block->rules, list) {
234 			if (filter->ingress_port_mask & BIT(port) &&
235 			    filter->action.vid_replace_ena) {
236 				dev_err(ocelot->dev,
237 					"Cannot change VLAN state with vlan modify rules active\n");
238 				return -EBUSY;
239 			}
240 		}
241 
242 		return 0;
243 	}
244 
245 	ocelot_port->vlan_aware = vlan_aware;
246 
247 	if (vlan_aware)
248 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
249 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
250 	else
251 		val = 0;
252 	ocelot_rmw_gix(ocelot, val,
253 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
254 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
255 		       ANA_PORT_VLAN_CFG, port);
256 
257 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
258 	ocelot_port_set_native_vlan(ocelot, port, ocelot_port->native_vlan);
259 
260 	return 0;
261 }
262 EXPORT_SYMBOL(ocelot_port_vlan_filtering);
263 
264 int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
265 			bool untagged)
266 {
267 	struct ocelot_port *ocelot_port = ocelot->ports[port];
268 
269 	/* Deny changing the native VLAN, but always permit deleting it */
270 	if (untagged && ocelot_port->native_vlan.vid != vid &&
271 	    ocelot_port->native_vlan.valid) {
272 		dev_err(ocelot->dev,
273 			"Port already has a native VLAN: %d\n",
274 			ocelot_port->native_vlan.vid);
275 		return -EBUSY;
276 	}
277 
278 	return 0;
279 }
280 EXPORT_SYMBOL(ocelot_vlan_prepare);
281 
282 int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
283 		    bool untagged)
284 {
285 	int ret;
286 
287 	/* Make the port a member of the VLAN */
288 	ocelot->vlan_mask[vid] |= BIT(port);
289 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
290 	if (ret)
291 		return ret;
292 
293 	/* Default ingress vlan classification */
294 	if (pvid) {
295 		struct ocelot_vlan pvid_vlan;
296 
297 		pvid_vlan.vid = vid;
298 		pvid_vlan.valid = true;
299 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
300 	}
301 
302 	/* Untagged egress vlan clasification */
303 	if (untagged) {
304 		struct ocelot_vlan native_vlan;
305 
306 		native_vlan.vid = vid;
307 		native_vlan.valid = true;
308 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
309 	}
310 
311 	return 0;
312 }
313 EXPORT_SYMBOL(ocelot_vlan_add);
314 
315 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
316 {
317 	struct ocelot_port *ocelot_port = ocelot->ports[port];
318 	int ret;
319 
320 	/* Stop the port from being a member of the vlan */
321 	ocelot->vlan_mask[vid] &= ~BIT(port);
322 	ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
323 	if (ret)
324 		return ret;
325 
326 	/* Ingress */
327 	if (ocelot_port->pvid_vlan.vid == vid) {
328 		struct ocelot_vlan pvid_vlan = {0};
329 
330 		ocelot_port_set_pvid(ocelot, port, pvid_vlan);
331 	}
332 
333 	/* Egress */
334 	if (ocelot_port->native_vlan.vid == vid) {
335 		struct ocelot_vlan native_vlan = {0};
336 
337 		ocelot_port_set_native_vlan(ocelot, port, native_vlan);
338 	}
339 
340 	return 0;
341 }
342 EXPORT_SYMBOL(ocelot_vlan_del);
343 
344 static void ocelot_vlan_init(struct ocelot *ocelot)
345 {
346 	u16 port, vid;
347 
348 	/* Clear VLAN table, by default all ports are members of all VLANs */
349 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
350 		     ANA_TABLES_VLANACCESS);
351 	ocelot_vlant_wait_for_completion(ocelot);
352 
353 	/* Configure the port VLAN memberships */
354 	for (vid = 1; vid < VLAN_N_VID; vid++) {
355 		ocelot->vlan_mask[vid] = 0;
356 		ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
357 	}
358 
359 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
360 	 * traffic.  It is added automatically if 8021q module is loaded, but
361 	 * we can't rely on it since module may be not loaded.
362 	 */
363 	ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
364 	ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
365 
366 	/* Set vlan ingress filter mask to all ports but the CPU port by
367 	 * default.
368 	 */
369 	ocelot_write(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
370 		     ANA_VLANMASK);
371 
372 	for (port = 0; port < ocelot->num_phys_ports; port++) {
373 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
374 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
375 	}
376 }
377 
378 static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
379 {
380 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
381 }
382 
383 int ocelot_port_flush(struct ocelot *ocelot, int port)
384 {
385 	int err, val;
386 
387 	/* Disable dequeuing from the egress queues */
388 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
389 		       QSYS_PORT_MODE_DEQUEUE_DIS,
390 		       QSYS_PORT_MODE, port);
391 
392 	/* Disable flow control */
393 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
394 
395 	/* Disable priority flow control */
396 	ocelot_fields_write(ocelot, port,
397 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
398 
399 	/* Wait at least the time it takes to receive a frame of maximum length
400 	 * at the port.
401 	 * Worst-case delays for 10 kilobyte jumbo frames are:
402 	 * 8 ms on a 10M port
403 	 * 800 μs on a 100M port
404 	 * 80 μs on a 1G port
405 	 * 32 μs on a 2.5G port
406 	 */
407 	usleep_range(8000, 10000);
408 
409 	/* Disable half duplex backpressure. */
410 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
411 		       SYS_FRONT_PORT_MODE, port);
412 
413 	/* Flush the queues associated with the port. */
414 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
415 		       REW_PORT_CFG, port);
416 
417 	/* Enable dequeuing from the egress queues. */
418 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
419 		       port);
420 
421 	/* Wait until flushing is complete. */
422 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
423 				100, 2000000, false, ocelot, port);
424 
425 	/* Clear flushing again. */
426 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
427 
428 	return err;
429 }
430 EXPORT_SYMBOL(ocelot_port_flush);
431 
432 void ocelot_adjust_link(struct ocelot *ocelot, int port,
433 			struct phy_device *phydev)
434 {
435 	struct ocelot_port *ocelot_port = ocelot->ports[port];
436 	int speed, mode = 0;
437 
438 	switch (phydev->speed) {
439 	case SPEED_10:
440 		speed = OCELOT_SPEED_10;
441 		break;
442 	case SPEED_100:
443 		speed = OCELOT_SPEED_100;
444 		break;
445 	case SPEED_1000:
446 		speed = OCELOT_SPEED_1000;
447 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
448 		break;
449 	case SPEED_2500:
450 		speed = OCELOT_SPEED_2500;
451 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
452 		break;
453 	default:
454 		dev_err(ocelot->dev, "Unsupported PHY speed on port %d: %d\n",
455 			port, phydev->speed);
456 		return;
457 	}
458 
459 	phy_print_status(phydev);
460 
461 	if (!phydev->link)
462 		return;
463 
464 	/* Only full duplex supported for now */
465 	ocelot_port_writel(ocelot_port, DEV_MAC_MODE_CFG_FDX_ENA |
466 			   mode, DEV_MAC_MODE_CFG);
467 
468 	/* Disable HDX fast control */
469 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
470 			   DEV_PORT_MISC);
471 
472 	/* SGMII only for now */
473 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
474 			   PCS1G_MODE_CFG);
475 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
476 
477 	/* Enable PCS */
478 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
479 
480 	/* No aneg on SGMII */
481 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
482 
483 	/* No loopback */
484 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
485 
486 	/* Enable MAC module */
487 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
488 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
489 
490 	/* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
491 	 * reset */
492 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(speed),
493 			   DEV_CLOCK_CFG);
494 
495 	/* No PFC */
496 	ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
497 			 ANA_PFC_PFC_CFG, port);
498 
499 	/* Core: Enable port for frame transfer */
500 	ocelot_fields_write(ocelot, port,
501 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
502 
503 	/* Flow control */
504 	ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
505 			 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
506 			 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
507 			 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
508 			 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
509 			 SYS_MAC_FC_CFG, port);
510 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
511 }
512 EXPORT_SYMBOL(ocelot_adjust_link);
513 
514 void ocelot_port_enable(struct ocelot *ocelot, int port,
515 			struct phy_device *phy)
516 {
517 	/* Enable receiving frames on the port, and activate auto-learning of
518 	 * MAC addresses.
519 	 */
520 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
521 			 ANA_PORT_PORT_CFG_RECV_ENA |
522 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
523 			 ANA_PORT_PORT_CFG, port);
524 }
525 EXPORT_SYMBOL(ocelot_port_enable);
526 
527 void ocelot_port_disable(struct ocelot *ocelot, int port)
528 {
529 	struct ocelot_port *ocelot_port = ocelot->ports[port];
530 
531 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
532 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
533 }
534 EXPORT_SYMBOL(ocelot_port_disable);
535 
536 void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
537 				  struct sk_buff *clone)
538 {
539 	struct ocelot_port *ocelot_port = ocelot->ports[port];
540 
541 	spin_lock(&ocelot_port->ts_id_lock);
542 
543 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
544 	/* Store timestamp ID in cb[0] of sk_buff */
545 	clone->cb[0] = ocelot_port->ts_id;
546 	ocelot_port->ts_id = (ocelot_port->ts_id + 1) % 4;
547 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
548 
549 	spin_unlock(&ocelot_port->ts_id_lock);
550 }
551 EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
552 
553 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
554 				   struct timespec64 *ts)
555 {
556 	unsigned long flags;
557 	u32 val;
558 
559 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
560 
561 	/* Read current PTP time to get seconds */
562 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
563 
564 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
565 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
566 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
567 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
568 
569 	/* Read packet HW timestamp from FIFO */
570 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
571 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
572 
573 	/* Sec has incremented since the ts was registered */
574 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
575 		ts->tv_sec--;
576 
577 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
578 }
579 
580 void ocelot_get_txtstamp(struct ocelot *ocelot)
581 {
582 	int budget = OCELOT_PTP_QUEUE_SZ;
583 
584 	while (budget--) {
585 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
586 		struct skb_shared_hwtstamps shhwtstamps;
587 		struct ocelot_port *port;
588 		struct timespec64 ts;
589 		unsigned long flags;
590 		u32 val, id, txport;
591 
592 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
593 
594 		/* Check if a timestamp can be retrieved */
595 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
596 			break;
597 
598 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
599 
600 		/* Retrieve the ts ID and Tx port */
601 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
602 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
603 
604 		/* Retrieve its associated skb */
605 		port = ocelot->ports[txport];
606 
607 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
608 
609 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
610 			if (skb->cb[0] != id)
611 				continue;
612 			__skb_unlink(skb, &port->tx_skbs);
613 			skb_match = skb;
614 			break;
615 		}
616 
617 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
618 
619 		/* Get the h/w timestamp */
620 		ocelot_get_hwtimestamp(ocelot, &ts);
621 
622 		if (unlikely(!skb_match))
623 			continue;
624 
625 		/* Set the timestamp into the skb */
626 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
627 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
628 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
629 
630 		/* Next ts */
631 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
632 	}
633 }
634 EXPORT_SYMBOL(ocelot_get_txtstamp);
635 
636 int ocelot_fdb_add(struct ocelot *ocelot, int port,
637 		   const unsigned char *addr, u16 vid)
638 {
639 	int pgid = port;
640 
641 	if (port == ocelot->npi)
642 		pgid = PGID_CPU;
643 
644 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
645 }
646 EXPORT_SYMBOL(ocelot_fdb_add);
647 
648 int ocelot_fdb_del(struct ocelot *ocelot, int port,
649 		   const unsigned char *addr, u16 vid)
650 {
651 	return ocelot_mact_forget(ocelot, addr, vid);
652 }
653 EXPORT_SYMBOL(ocelot_fdb_del);
654 
655 int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
656 			    bool is_static, void *data)
657 {
658 	struct ocelot_dump_ctx *dump = data;
659 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
660 	u32 seq = dump->cb->nlh->nlmsg_seq;
661 	struct nlmsghdr *nlh;
662 	struct ndmsg *ndm;
663 
664 	if (dump->idx < dump->cb->args[2])
665 		goto skip;
666 
667 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
668 			sizeof(*ndm), NLM_F_MULTI);
669 	if (!nlh)
670 		return -EMSGSIZE;
671 
672 	ndm = nlmsg_data(nlh);
673 	ndm->ndm_family  = AF_BRIDGE;
674 	ndm->ndm_pad1    = 0;
675 	ndm->ndm_pad2    = 0;
676 	ndm->ndm_flags   = NTF_SELF;
677 	ndm->ndm_type    = 0;
678 	ndm->ndm_ifindex = dump->dev->ifindex;
679 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
680 
681 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
682 		goto nla_put_failure;
683 
684 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
685 		goto nla_put_failure;
686 
687 	nlmsg_end(dump->skb, nlh);
688 
689 skip:
690 	dump->idx++;
691 	return 0;
692 
693 nla_put_failure:
694 	nlmsg_cancel(dump->skb, nlh);
695 	return -EMSGSIZE;
696 }
697 EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
698 
699 static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
700 			    struct ocelot_mact_entry *entry)
701 {
702 	u32 val, dst, macl, mach;
703 	char mac[ETH_ALEN];
704 
705 	/* Set row and column to read from */
706 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
707 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
708 
709 	/* Issue a read command */
710 	ocelot_write(ocelot,
711 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
712 		     ANA_TABLES_MACACCESS);
713 
714 	if (ocelot_mact_wait_for_completion(ocelot))
715 		return -ETIMEDOUT;
716 
717 	/* Read the entry flags */
718 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
719 	if (!(val & ANA_TABLES_MACACCESS_VALID))
720 		return -EINVAL;
721 
722 	/* If the entry read has another port configured as its destination,
723 	 * do not report it.
724 	 */
725 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
726 	if (dst != port)
727 		return -EINVAL;
728 
729 	/* Get the entry's MAC address and VLAN id */
730 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
731 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
732 
733 	mac[0] = (mach >> 8)  & 0xff;
734 	mac[1] = (mach >> 0)  & 0xff;
735 	mac[2] = (macl >> 24) & 0xff;
736 	mac[3] = (macl >> 16) & 0xff;
737 	mac[4] = (macl >> 8)  & 0xff;
738 	mac[5] = (macl >> 0)  & 0xff;
739 
740 	entry->vid = (mach >> 16) & 0xfff;
741 	ether_addr_copy(entry->mac, mac);
742 
743 	return 0;
744 }
745 
746 int ocelot_fdb_dump(struct ocelot *ocelot, int port,
747 		    dsa_fdb_dump_cb_t *cb, void *data)
748 {
749 	int i, j;
750 
751 	/* Loop through all the mac tables entries. */
752 	for (i = 0; i < ocelot->num_mact_rows; i++) {
753 		for (j = 0; j < 4; j++) {
754 			struct ocelot_mact_entry entry;
755 			bool is_static;
756 			int ret;
757 
758 			ret = ocelot_mact_read(ocelot, port, i, j, &entry);
759 			/* If the entry is invalid (wrong port, invalid...),
760 			 * skip it.
761 			 */
762 			if (ret == -EINVAL)
763 				continue;
764 			else if (ret)
765 				return ret;
766 
767 			is_static = (entry.type == ENTRYTYPE_LOCKED);
768 
769 			ret = cb(entry.mac, entry.vid, is_static, data);
770 			if (ret)
771 				return ret;
772 		}
773 	}
774 
775 	return 0;
776 }
777 EXPORT_SYMBOL(ocelot_fdb_dump);
778 
779 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
780 {
781 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
782 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
783 }
784 EXPORT_SYMBOL(ocelot_hwstamp_get);
785 
786 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
787 {
788 	struct ocelot_port *ocelot_port = ocelot->ports[port];
789 	struct hwtstamp_config cfg;
790 
791 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
792 		return -EFAULT;
793 
794 	/* reserved for future extensions */
795 	if (cfg.flags)
796 		return -EINVAL;
797 
798 	/* Tx type sanity check */
799 	switch (cfg.tx_type) {
800 	case HWTSTAMP_TX_ON:
801 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
802 		break;
803 	case HWTSTAMP_TX_ONESTEP_SYNC:
804 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
805 		 * need to update the origin time.
806 		 */
807 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
808 		break;
809 	case HWTSTAMP_TX_OFF:
810 		ocelot_port->ptp_cmd = 0;
811 		break;
812 	default:
813 		return -ERANGE;
814 	}
815 
816 	mutex_lock(&ocelot->ptp_lock);
817 
818 	switch (cfg.rx_filter) {
819 	case HWTSTAMP_FILTER_NONE:
820 		break;
821 	case HWTSTAMP_FILTER_ALL:
822 	case HWTSTAMP_FILTER_SOME:
823 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
824 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
825 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
826 	case HWTSTAMP_FILTER_NTP_ALL:
827 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
828 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
829 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
830 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
831 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
832 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
833 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
834 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
835 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
836 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
837 		break;
838 	default:
839 		mutex_unlock(&ocelot->ptp_lock);
840 		return -ERANGE;
841 	}
842 
843 	/* Commit back the result & save it */
844 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
845 	mutex_unlock(&ocelot->ptp_lock);
846 
847 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
848 }
849 EXPORT_SYMBOL(ocelot_hwstamp_set);
850 
851 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
852 {
853 	int i;
854 
855 	if (sset != ETH_SS_STATS)
856 		return;
857 
858 	for (i = 0; i < ocelot->num_stats; i++)
859 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
860 		       ETH_GSTRING_LEN);
861 }
862 EXPORT_SYMBOL(ocelot_get_strings);
863 
864 static void ocelot_update_stats(struct ocelot *ocelot)
865 {
866 	int i, j;
867 
868 	mutex_lock(&ocelot->stats_lock);
869 
870 	for (i = 0; i < ocelot->num_phys_ports; i++) {
871 		/* Configure the port to read the stats from */
872 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
873 
874 		for (j = 0; j < ocelot->num_stats; j++) {
875 			u32 val;
876 			unsigned int idx = i * ocelot->num_stats + j;
877 
878 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
879 					      ocelot->stats_layout[j].offset);
880 
881 			if (val < (ocelot->stats[idx] & U32_MAX))
882 				ocelot->stats[idx] += (u64)1 << 32;
883 
884 			ocelot->stats[idx] = (ocelot->stats[idx] &
885 					      ~(u64)U32_MAX) + val;
886 		}
887 	}
888 
889 	mutex_unlock(&ocelot->stats_lock);
890 }
891 
892 static void ocelot_check_stats_work(struct work_struct *work)
893 {
894 	struct delayed_work *del_work = to_delayed_work(work);
895 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
896 					     stats_work);
897 
898 	ocelot_update_stats(ocelot);
899 
900 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
901 			   OCELOT_STATS_CHECK_DELAY);
902 }
903 
904 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
905 {
906 	int i;
907 
908 	/* check and update now */
909 	ocelot_update_stats(ocelot);
910 
911 	/* Copy all counters */
912 	for (i = 0; i < ocelot->num_stats; i++)
913 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
914 }
915 EXPORT_SYMBOL(ocelot_get_ethtool_stats);
916 
917 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
918 {
919 	if (sset != ETH_SS_STATS)
920 		return -EOPNOTSUPP;
921 
922 	return ocelot->num_stats;
923 }
924 EXPORT_SYMBOL(ocelot_get_sset_count);
925 
926 int ocelot_get_ts_info(struct ocelot *ocelot, int port,
927 		       struct ethtool_ts_info *info)
928 {
929 	info->phc_index = ocelot->ptp_clock ?
930 			  ptp_clock_index(ocelot->ptp_clock) : -1;
931 	if (info->phc_index == -1) {
932 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
933 					 SOF_TIMESTAMPING_RX_SOFTWARE |
934 					 SOF_TIMESTAMPING_SOFTWARE;
935 		return 0;
936 	}
937 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
938 				 SOF_TIMESTAMPING_RX_SOFTWARE |
939 				 SOF_TIMESTAMPING_SOFTWARE |
940 				 SOF_TIMESTAMPING_TX_HARDWARE |
941 				 SOF_TIMESTAMPING_RX_HARDWARE |
942 				 SOF_TIMESTAMPING_RAW_HARDWARE;
943 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
944 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
945 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
946 
947 	return 0;
948 }
949 EXPORT_SYMBOL(ocelot_get_ts_info);
950 
951 void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
952 {
953 	u32 port_cfg;
954 	int p, i;
955 
956 	if (!(BIT(port) & ocelot->bridge_mask))
957 		return;
958 
959 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
960 
961 	switch (state) {
962 	case BR_STATE_FORWARDING:
963 		ocelot->bridge_fwd_mask |= BIT(port);
964 		fallthrough;
965 	case BR_STATE_LEARNING:
966 		port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
967 		break;
968 
969 	default:
970 		port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
971 		ocelot->bridge_fwd_mask &= ~BIT(port);
972 		break;
973 	}
974 
975 	ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG, port);
976 
977 	/* Apply FWD mask. The loop is needed to add/remove the current port as
978 	 * a source for the other ports.
979 	 */
980 	for (p = 0; p < ocelot->num_phys_ports; p++) {
981 		if (ocelot->bridge_fwd_mask & BIT(p)) {
982 			unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(p);
983 
984 			for (i = 0; i < ocelot->num_phys_ports; i++) {
985 				unsigned long bond_mask = ocelot->lags[i];
986 
987 				if (!bond_mask)
988 					continue;
989 
990 				if (bond_mask & BIT(p)) {
991 					mask &= ~bond_mask;
992 					break;
993 				}
994 			}
995 
996 			ocelot_write_rix(ocelot, mask,
997 					 ANA_PGID_PGID, PGID_SRC + p);
998 		} else {
999 			ocelot_write_rix(ocelot, 0,
1000 					 ANA_PGID_PGID, PGID_SRC + p);
1001 		}
1002 	}
1003 }
1004 EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1005 
1006 void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
1007 {
1008 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1009 
1010 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1011 	 * which is clearly not what our intention is. So avoid that.
1012 	 */
1013 	if (!age_period)
1014 		age_period = 1;
1015 
1016 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
1017 }
1018 EXPORT_SYMBOL(ocelot_set_ageing_time);
1019 
1020 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1021 						     const unsigned char *addr,
1022 						     u16 vid)
1023 {
1024 	struct ocelot_multicast *mc;
1025 
1026 	list_for_each_entry(mc, &ocelot->multicast, list) {
1027 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1028 			return mc;
1029 	}
1030 
1031 	return NULL;
1032 }
1033 
1034 static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
1035 {
1036 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
1037 		return ENTRYTYPE_MACv4;
1038 	if (addr[0] == 0x33 && addr[1] == 0x33)
1039 		return ENTRYTYPE_MACv6;
1040 	return ENTRYTYPE_LOCKED;
1041 }
1042 
1043 static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
1044 					     unsigned long ports)
1045 {
1046 	struct ocelot_pgid *pgid;
1047 
1048 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
1049 	if (!pgid)
1050 		return ERR_PTR(-ENOMEM);
1051 
1052 	pgid->ports = ports;
1053 	pgid->index = index;
1054 	refcount_set(&pgid->refcount, 1);
1055 	list_add_tail(&pgid->list, &ocelot->pgids);
1056 
1057 	return pgid;
1058 }
1059 
1060 static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
1061 {
1062 	if (!refcount_dec_and_test(&pgid->refcount))
1063 		return;
1064 
1065 	list_del(&pgid->list);
1066 	kfree(pgid);
1067 }
1068 
1069 static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
1070 					       const struct ocelot_multicast *mc)
1071 {
1072 	struct ocelot_pgid *pgid;
1073 	int index;
1074 
1075 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
1076 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
1077 	 * destination mask table (PGID), the destination set is programmed as
1078 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
1079 	 */
1080 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
1081 	    mc->entry_type == ENTRYTYPE_MACv6)
1082 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
1083 
1084 	list_for_each_entry(pgid, &ocelot->pgids, list) {
1085 		/* When searching for a nonreserved multicast PGID, ignore the
1086 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
1087 		 */
1088 		if (pgid->index && pgid->ports == mc->ports) {
1089 			refcount_inc(&pgid->refcount);
1090 			return pgid;
1091 		}
1092 	}
1093 
1094 	/* Search for a free index in the nonreserved multicast PGID area */
1095 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
1096 		bool used = false;
1097 
1098 		list_for_each_entry(pgid, &ocelot->pgids, list) {
1099 			if (pgid->index == index) {
1100 				used = true;
1101 				break;
1102 			}
1103 		}
1104 
1105 		if (!used)
1106 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
1107 	}
1108 
1109 	return ERR_PTR(-ENOSPC);
1110 }
1111 
1112 static void ocelot_encode_ports_to_mdb(unsigned char *addr,
1113 				       struct ocelot_multicast *mc)
1114 {
1115 	ether_addr_copy(addr, mc->addr);
1116 
1117 	if (mc->entry_type == ENTRYTYPE_MACv4) {
1118 		addr[0] = 0;
1119 		addr[1] = mc->ports >> 8;
1120 		addr[2] = mc->ports & 0xff;
1121 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
1122 		addr[0] = mc->ports >> 8;
1123 		addr[1] = mc->ports & 0xff;
1124 	}
1125 }
1126 
1127 int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
1128 			const struct switchdev_obj_port_mdb *mdb)
1129 {
1130 	unsigned char addr[ETH_ALEN];
1131 	struct ocelot_multicast *mc;
1132 	struct ocelot_pgid *pgid;
1133 	u16 vid = mdb->vid;
1134 
1135 	if (port == ocelot->npi)
1136 		port = ocelot->num_phys_ports;
1137 
1138 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1139 	if (!mc) {
1140 		/* New entry */
1141 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1142 		if (!mc)
1143 			return -ENOMEM;
1144 
1145 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
1146 		ether_addr_copy(mc->addr, mdb->addr);
1147 		mc->vid = vid;
1148 
1149 		list_add_tail(&mc->list, &ocelot->multicast);
1150 	} else {
1151 		/* Existing entry. Clean up the current port mask from
1152 		 * hardware now, because we'll be modifying it.
1153 		 */
1154 		ocelot_pgid_free(ocelot, mc->pgid);
1155 		ocelot_encode_ports_to_mdb(addr, mc);
1156 		ocelot_mact_forget(ocelot, addr, vid);
1157 	}
1158 
1159 	mc->ports |= BIT(port);
1160 
1161 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1162 	if (IS_ERR(pgid)) {
1163 		dev_err(ocelot->dev,
1164 			"Cannot allocate PGID for mdb %pM vid %d\n",
1165 			mc->addr, mc->vid);
1166 		devm_kfree(ocelot->dev, mc);
1167 		return PTR_ERR(pgid);
1168 	}
1169 	mc->pgid = pgid;
1170 
1171 	ocelot_encode_ports_to_mdb(addr, mc);
1172 
1173 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1174 	    mc->entry_type != ENTRYTYPE_MACv6)
1175 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1176 				 pgid->index);
1177 
1178 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1179 				 mc->entry_type);
1180 }
1181 EXPORT_SYMBOL(ocelot_port_mdb_add);
1182 
1183 int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
1184 			const struct switchdev_obj_port_mdb *mdb)
1185 {
1186 	unsigned char addr[ETH_ALEN];
1187 	struct ocelot_multicast *mc;
1188 	struct ocelot_pgid *pgid;
1189 	u16 vid = mdb->vid;
1190 
1191 	if (port == ocelot->npi)
1192 		port = ocelot->num_phys_ports;
1193 
1194 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1195 	if (!mc)
1196 		return -ENOENT;
1197 
1198 	ocelot_encode_ports_to_mdb(addr, mc);
1199 	ocelot_mact_forget(ocelot, addr, vid);
1200 
1201 	ocelot_pgid_free(ocelot, mc->pgid);
1202 	mc->ports &= ~BIT(port);
1203 	if (!mc->ports) {
1204 		list_del(&mc->list);
1205 		devm_kfree(ocelot->dev, mc);
1206 		return 0;
1207 	}
1208 
1209 	/* We have a PGID with fewer ports now */
1210 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
1211 	if (IS_ERR(pgid))
1212 		return PTR_ERR(pgid);
1213 	mc->pgid = pgid;
1214 
1215 	ocelot_encode_ports_to_mdb(addr, mc);
1216 
1217 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
1218 	    mc->entry_type != ENTRYTYPE_MACv6)
1219 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
1220 				 pgid->index);
1221 
1222 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
1223 				 mc->entry_type);
1224 }
1225 EXPORT_SYMBOL(ocelot_port_mdb_del);
1226 
1227 int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
1228 			    struct net_device *bridge)
1229 {
1230 	if (!ocelot->bridge_mask) {
1231 		ocelot->hw_bridge_dev = bridge;
1232 	} else {
1233 		if (ocelot->hw_bridge_dev != bridge)
1234 			/* This is adding the port to a second bridge, this is
1235 			 * unsupported */
1236 			return -ENODEV;
1237 	}
1238 
1239 	ocelot->bridge_mask |= BIT(port);
1240 
1241 	return 0;
1242 }
1243 EXPORT_SYMBOL(ocelot_port_bridge_join);
1244 
1245 int ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
1246 			     struct net_device *bridge)
1247 {
1248 	struct ocelot_vlan pvid = {0}, native_vlan = {0};
1249 	struct switchdev_trans trans;
1250 	int ret;
1251 
1252 	ocelot->bridge_mask &= ~BIT(port);
1253 
1254 	if (!ocelot->bridge_mask)
1255 		ocelot->hw_bridge_dev = NULL;
1256 
1257 	trans.ph_prepare = true;
1258 	ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
1259 	if (ret)
1260 		return ret;
1261 
1262 	trans.ph_prepare = false;
1263 	ret = ocelot_port_vlan_filtering(ocelot, port, false, &trans);
1264 	if (ret)
1265 		return ret;
1266 
1267 	ocelot_port_set_pvid(ocelot, port, pvid);
1268 	ocelot_port_set_native_vlan(ocelot, port, native_vlan);
1269 
1270 	return 0;
1271 }
1272 EXPORT_SYMBOL(ocelot_port_bridge_leave);
1273 
1274 static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1275 {
1276 	int i, port, lag;
1277 
1278 	/* Reset destination and aggregation PGIDS */
1279 	for_each_unicast_dest_pgid(ocelot, port)
1280 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1281 
1282 	for_each_aggr_pgid(ocelot, i)
1283 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1284 				 ANA_PGID_PGID, i);
1285 
1286 	/* Now, set PGIDs for each LAG */
1287 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1288 		unsigned long bond_mask;
1289 		int aggr_count = 0;
1290 		u8 aggr_idx[16];
1291 
1292 		bond_mask = ocelot->lags[lag];
1293 		if (!bond_mask)
1294 			continue;
1295 
1296 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1297 			// Destination mask
1298 			ocelot_write_rix(ocelot, bond_mask,
1299 					 ANA_PGID_PGID, port);
1300 			aggr_idx[aggr_count] = port;
1301 			aggr_count++;
1302 		}
1303 
1304 		for_each_aggr_pgid(ocelot, i) {
1305 			u32 ac;
1306 
1307 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1308 			ac &= ~bond_mask;
1309 			ac |= BIT(aggr_idx[i % aggr_count]);
1310 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1311 		}
1312 	}
1313 }
1314 
1315 static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1316 {
1317 	unsigned long bond_mask = ocelot->lags[lag];
1318 	unsigned int p;
1319 
1320 	for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1321 		u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1322 
1323 		port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1324 
1325 		/* Use lag port as logical port for port i */
1326 		ocelot_write_gix(ocelot, port_cfg |
1327 				 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1328 				 ANA_PORT_PORT_CFG, p);
1329 	}
1330 }
1331 
1332 int ocelot_port_lag_join(struct ocelot *ocelot, int port,
1333 			 struct net_device *bond)
1334 {
1335 	struct net_device *ndev;
1336 	u32 bond_mask = 0;
1337 	int lag, lp;
1338 
1339 	rcu_read_lock();
1340 	for_each_netdev_in_bond_rcu(bond, ndev) {
1341 		struct ocelot_port_private *priv = netdev_priv(ndev);
1342 
1343 		bond_mask |= BIT(priv->chip_port);
1344 	}
1345 	rcu_read_unlock();
1346 
1347 	lp = __ffs(bond_mask);
1348 
1349 	/* If the new port is the lowest one, use it as the logical port from
1350 	 * now on
1351 	 */
1352 	if (port == lp) {
1353 		lag = port;
1354 		ocelot->lags[port] = bond_mask;
1355 		bond_mask &= ~BIT(port);
1356 		if (bond_mask) {
1357 			lp = __ffs(bond_mask);
1358 			ocelot->lags[lp] = 0;
1359 		}
1360 	} else {
1361 		lag = lp;
1362 		ocelot->lags[lp] |= BIT(port);
1363 	}
1364 
1365 	ocelot_setup_lag(ocelot, lag);
1366 	ocelot_set_aggr_pgids(ocelot);
1367 
1368 	return 0;
1369 }
1370 EXPORT_SYMBOL(ocelot_port_lag_join);
1371 
1372 void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
1373 			   struct net_device *bond)
1374 {
1375 	u32 port_cfg;
1376 	int i;
1377 
1378 	/* Remove port from any lag */
1379 	for (i = 0; i < ocelot->num_phys_ports; i++)
1380 		ocelot->lags[i] &= ~BIT(port);
1381 
1382 	/* if it was the logical port of the lag, move the lag config to the
1383 	 * next port
1384 	 */
1385 	if (ocelot->lags[port]) {
1386 		int n = __ffs(ocelot->lags[port]);
1387 
1388 		ocelot->lags[n] = ocelot->lags[port];
1389 		ocelot->lags[port] = 0;
1390 
1391 		ocelot_setup_lag(ocelot, n);
1392 	}
1393 
1394 	port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, port);
1395 	port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1396 	ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(port),
1397 			 ANA_PORT_PORT_CFG, port);
1398 
1399 	ocelot_set_aggr_pgids(ocelot);
1400 }
1401 EXPORT_SYMBOL(ocelot_port_lag_leave);
1402 
1403 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
1404  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
1405  * In the special case that it's the NPI port that we're configuring, the
1406  * length of the tag and optional prefix needs to be accounted for privately,
1407  * in order to be able to sustain communication at the requested @sdu.
1408  */
1409 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
1410 {
1411 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1412 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
1413 	int pause_start, pause_stop;
1414 	int atop, atop_tot;
1415 
1416 	if (port == ocelot->npi) {
1417 		maxlen += OCELOT_TAG_LEN;
1418 
1419 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1420 			maxlen += OCELOT_SHORT_PREFIX_LEN;
1421 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
1422 			maxlen += OCELOT_LONG_PREFIX_LEN;
1423 	}
1424 
1425 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
1426 
1427 	/* Set Pause watermark hysteresis */
1428 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
1429 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
1430 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
1431 			    pause_start);
1432 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
1433 			    pause_stop);
1434 
1435 	/* Tail dropping watermarks */
1436 	atop_tot = (ocelot->shared_queue_sz - 9 * maxlen) /
1437 		   OCELOT_BUFFER_CELL_SZ;
1438 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
1439 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
1440 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
1441 }
1442 EXPORT_SYMBOL(ocelot_port_set_maxlen);
1443 
1444 int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
1445 {
1446 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
1447 
1448 	if (port == ocelot->npi) {
1449 		max_mtu -= OCELOT_TAG_LEN;
1450 
1451 		if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_SHORT)
1452 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
1453 		else if (ocelot->inj_prefix == OCELOT_TAG_PREFIX_LONG)
1454 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
1455 	}
1456 
1457 	return max_mtu;
1458 }
1459 EXPORT_SYMBOL(ocelot_get_max_mtu);
1460 
1461 void ocelot_init_port(struct ocelot *ocelot, int port)
1462 {
1463 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1464 
1465 	skb_queue_head_init(&ocelot_port->tx_skbs);
1466 	spin_lock_init(&ocelot_port->ts_id_lock);
1467 
1468 	/* Basic L2 initialization */
1469 
1470 	/* Set MAC IFG Gaps
1471 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
1472 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
1473 	 */
1474 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
1475 			   DEV_MAC_IFG_CFG);
1476 
1477 	/* Load seed (0) and set MAC HDX late collision  */
1478 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
1479 			   DEV_MAC_HDX_CFG_SEED_LOAD,
1480 			   DEV_MAC_HDX_CFG);
1481 	mdelay(1);
1482 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
1483 			   DEV_MAC_HDX_CFG);
1484 
1485 	/* Set Max Length and maximum tags allowed */
1486 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
1487 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
1488 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
1489 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
1490 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
1491 			   DEV_MAC_TAGS_CFG);
1492 
1493 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
1494 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
1495 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
1496 
1497 	/* Enable transmission of pause frames */
1498 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
1499 
1500 	/* Drop frames with multicast source address */
1501 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1502 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
1503 		       ANA_PORT_DROP_CFG, port);
1504 
1505 	/* Set default VLAN and tag type to 8021Q. */
1506 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
1507 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
1508 		       REW_PORT_VLAN_CFG, port);
1509 
1510 	/* Enable vcap lookups */
1511 	ocelot_vcap_enable(ocelot, port);
1512 }
1513 EXPORT_SYMBOL(ocelot_init_port);
1514 
1515 /* Configure and enable the CPU port module, which is a set of queues
1516  * accessible through register MMIO, frame DMA or Ethernet (in case
1517  * NPI mode is used).
1518  */
1519 static void ocelot_cpu_port_init(struct ocelot *ocelot)
1520 {
1521 	int cpu = ocelot->num_phys_ports;
1522 
1523 	/* The unicast destination PGID for the CPU port module is unused */
1524 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
1525 	/* Instead set up a multicast destination PGID for traffic copied to
1526 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
1527 	 * addresses will be copied to the CPU via this PGID.
1528 	 */
1529 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1530 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1531 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1532 			 ANA_PORT_PORT_CFG, cpu);
1533 
1534 	/* Enable CPU port module */
1535 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
1536 	/* CPU port Injection/Extraction configuration */
1537 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
1538 			    ocelot->xtr_prefix);
1539 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
1540 			    ocelot->inj_prefix);
1541 
1542 	/* Configure the CPU port to be VLAN aware */
1543 	ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
1544 				 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
1545 				 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
1546 			 ANA_PORT_VLAN_CFG, cpu);
1547 }
1548 
1549 int ocelot_init(struct ocelot *ocelot)
1550 {
1551 	char queue_name[32];
1552 	int i, ret;
1553 	u32 port;
1554 
1555 	if (ocelot->ops->reset) {
1556 		ret = ocelot->ops->reset(ocelot);
1557 		if (ret) {
1558 			dev_err(ocelot->dev, "Switch reset failed\n");
1559 			return ret;
1560 		}
1561 	}
1562 
1563 	ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1564 				    sizeof(u32), GFP_KERNEL);
1565 	if (!ocelot->lags)
1566 		return -ENOMEM;
1567 
1568 	ocelot->stats = devm_kcalloc(ocelot->dev,
1569 				     ocelot->num_phys_ports * ocelot->num_stats,
1570 				     sizeof(u64), GFP_KERNEL);
1571 	if (!ocelot->stats)
1572 		return -ENOMEM;
1573 
1574 	mutex_init(&ocelot->stats_lock);
1575 	mutex_init(&ocelot->ptp_lock);
1576 	spin_lock_init(&ocelot->ptp_clock_lock);
1577 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
1578 		 dev_name(ocelot->dev));
1579 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1580 	if (!ocelot->stats_queue)
1581 		return -ENOMEM;
1582 
1583 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
1584 	if (!ocelot->owq) {
1585 		destroy_workqueue(ocelot->stats_queue);
1586 		return -ENOMEM;
1587 	}
1588 
1589 	INIT_LIST_HEAD(&ocelot->multicast);
1590 	INIT_LIST_HEAD(&ocelot->pgids);
1591 	ocelot_mact_init(ocelot);
1592 	ocelot_vlan_init(ocelot);
1593 	ocelot_vcap_init(ocelot);
1594 	ocelot_cpu_port_init(ocelot);
1595 
1596 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1597 		/* Clear all counters (5 groups) */
1598 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1599 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1600 			     SYS_STAT_CFG);
1601 	}
1602 
1603 	/* Only use S-Tag */
1604 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1605 
1606 	/* Aggregation mode */
1607 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1608 			     ANA_AGGR_CFG_AC_DMAC_ENA |
1609 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1610 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1611 
1612 	/* Set MAC age time to default value. The entry is aged after
1613 	 * 2*AGE_PERIOD
1614 	 */
1615 	ocelot_write(ocelot,
1616 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1617 		     ANA_AUTOAGE);
1618 
1619 	/* Disable learning for frames discarded by VLAN ingress filtering */
1620 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1621 
1622 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1623 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1624 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1625 
1626 	/* Setup flooding PGIDs */
1627 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
1628 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1629 				 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1630 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1631 				 ANA_FLOODING, i);
1632 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1633 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1634 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1635 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1636 		     ANA_FLOODING_IPMC);
1637 
1638 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1639 		/* Transmit the frame to the local port. */
1640 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1641 		/* Do not forward BPDU frames to the front ports. */
1642 		ocelot_write_gix(ocelot,
1643 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1644 				 ANA_PORT_CPU_FWD_BPDU_CFG,
1645 				 port);
1646 		/* Ensure bridging is disabled */
1647 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1648 	}
1649 
1650 	/* Allow broadcast MAC frames. */
1651 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
1652 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1653 
1654 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1655 	}
1656 	ocelot_write_rix(ocelot,
1657 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1658 			 ANA_PGID_PGID, PGID_MC);
1659 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1660 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1661 
1662 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
1663 	 * registers endianness.
1664 	 */
1665 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1666 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1667 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1668 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1669 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1670 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
1671 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1672 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1673 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1674 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1675 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1676 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1677 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1678 	for (i = 0; i < 16; i++)
1679 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1680 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1681 				 ANA_CPUQ_8021_CFG, i);
1682 
1683 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
1684 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1685 			   OCELOT_STATS_CHECK_DELAY);
1686 
1687 	return 0;
1688 }
1689 EXPORT_SYMBOL(ocelot_init);
1690 
1691 void ocelot_deinit(struct ocelot *ocelot)
1692 {
1693 	cancel_delayed_work(&ocelot->stats_work);
1694 	destroy_workqueue(ocelot->stats_queue);
1695 	destroy_workqueue(ocelot->owq);
1696 	mutex_destroy(&ocelot->stats_lock);
1697 }
1698 EXPORT_SYMBOL(ocelot_deinit);
1699 
1700 void ocelot_deinit_port(struct ocelot *ocelot, int port)
1701 {
1702 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1703 
1704 	skb_queue_purge(&ocelot_port->tx_skbs);
1705 }
1706 EXPORT_SYMBOL(ocelot_deinit_port);
1707 
1708 MODULE_LICENSE("Dual MIT/GPL");
1709