xref: /openbmc/linux/drivers/net/ethernet/mscc/ocelot.c (revision 359745d7)
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/dsa/ocelot.h>
8 #include <linux/if_bridge.h>
9 #include <linux/ptp_classify.h>
10 #include <soc/mscc/ocelot_vcap.h>
11 #include "ocelot.h"
12 #include "ocelot_vcap.h"
13 
14 #define TABLE_UPDATE_SLEEP_US 10
15 #define TABLE_UPDATE_TIMEOUT_US 100000
16 
17 struct ocelot_mact_entry {
18 	u8 mac[ETH_ALEN];
19 	u16 vid;
20 	enum macaccess_entry_type type;
21 };
22 
23 /* Caller must hold &ocelot->mact_lock */
24 static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
25 {
26 	return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
27 }
28 
29 /* Caller must hold &ocelot->mact_lock */
30 static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
31 {
32 	u32 val;
33 
34 	return readx_poll_timeout(ocelot_mact_read_macaccess,
35 		ocelot, val,
36 		(val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
37 		MACACCESS_CMD_IDLE,
38 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
39 }
40 
41 /* Caller must hold &ocelot->mact_lock */
42 static void ocelot_mact_select(struct ocelot *ocelot,
43 			       const unsigned char mac[ETH_ALEN],
44 			       unsigned int vid)
45 {
46 	u32 macl = 0, mach = 0;
47 
48 	/* Set the MAC address to handle and the vlan associated in a format
49 	 * understood by the hardware.
50 	 */
51 	mach |= vid    << 16;
52 	mach |= mac[0] << 8;
53 	mach |= mac[1] << 0;
54 	macl |= mac[2] << 24;
55 	macl |= mac[3] << 16;
56 	macl |= mac[4] << 8;
57 	macl |= mac[5] << 0;
58 
59 	ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
60 	ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
61 
62 }
63 
64 static int __ocelot_mact_learn(struct ocelot *ocelot, int port,
65 			       const unsigned char mac[ETH_ALEN],
66 			       unsigned int vid, enum macaccess_entry_type type)
67 {
68 	u32 cmd = ANA_TABLES_MACACCESS_VALID |
69 		ANA_TABLES_MACACCESS_DEST_IDX(port) |
70 		ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
71 		ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
72 	unsigned int mc_ports;
73 	int err;
74 
75 	/* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
76 	if (type == ENTRYTYPE_MACv4)
77 		mc_ports = (mac[1] << 8) | mac[2];
78 	else if (type == ENTRYTYPE_MACv6)
79 		mc_ports = (mac[0] << 8) | mac[1];
80 	else
81 		mc_ports = 0;
82 
83 	if (mc_ports & BIT(ocelot->num_phys_ports))
84 		cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
85 
86 	ocelot_mact_select(ocelot, mac, vid);
87 
88 	/* Issue a write command */
89 	ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
90 
91 	err = ocelot_mact_wait_for_completion(ocelot);
92 
93 	return err;
94 }
95 
96 int ocelot_mact_learn(struct ocelot *ocelot, int port,
97 		      const unsigned char mac[ETH_ALEN],
98 		      unsigned int vid, enum macaccess_entry_type type)
99 {
100 	int ret;
101 
102 	mutex_lock(&ocelot->mact_lock);
103 	ret = __ocelot_mact_learn(ocelot, port, mac, vid, type);
104 	mutex_unlock(&ocelot->mact_lock);
105 
106 	return ret;
107 }
108 EXPORT_SYMBOL(ocelot_mact_learn);
109 
110 int ocelot_mact_forget(struct ocelot *ocelot,
111 		       const unsigned char mac[ETH_ALEN], unsigned int vid)
112 {
113 	int err;
114 
115 	mutex_lock(&ocelot->mact_lock);
116 
117 	ocelot_mact_select(ocelot, mac, vid);
118 
119 	/* Issue a forget command */
120 	ocelot_write(ocelot,
121 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
122 		     ANA_TABLES_MACACCESS);
123 
124 	err = ocelot_mact_wait_for_completion(ocelot);
125 
126 	mutex_unlock(&ocelot->mact_lock);
127 
128 	return err;
129 }
130 EXPORT_SYMBOL(ocelot_mact_forget);
131 
132 int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx,
133 		       const unsigned char mac[ETH_ALEN],
134 		       unsigned int vid, enum macaccess_entry_type *type)
135 {
136 	int val;
137 
138 	mutex_lock(&ocelot->mact_lock);
139 
140 	ocelot_mact_select(ocelot, mac, vid);
141 
142 	/* Issue a read command with MACACCESS_VALID=1. */
143 	ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
144 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
145 		     ANA_TABLES_MACACCESS);
146 
147 	if (ocelot_mact_wait_for_completion(ocelot)) {
148 		mutex_unlock(&ocelot->mact_lock);
149 		return -ETIMEDOUT;
150 	}
151 
152 	/* Read back the entry flags */
153 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
154 
155 	mutex_unlock(&ocelot->mact_lock);
156 
157 	if (!(val & ANA_TABLES_MACACCESS_VALID))
158 		return -ENOENT;
159 
160 	*dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val);
161 	*type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val);
162 
163 	return 0;
164 }
165 EXPORT_SYMBOL(ocelot_mact_lookup);
166 
167 int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx,
168 				 const unsigned char mac[ETH_ALEN],
169 				 unsigned int vid,
170 				 enum macaccess_entry_type type,
171 				 int sfid, int ssid)
172 {
173 	int ret;
174 
175 	mutex_lock(&ocelot->mact_lock);
176 
177 	ocelot_write(ocelot,
178 		     (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) |
179 		     ANA_TABLES_STREAMDATA_SFID(sfid) |
180 		     (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) |
181 		     ANA_TABLES_STREAMDATA_SSID(ssid),
182 		     ANA_TABLES_STREAMDATA);
183 
184 	ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type);
185 
186 	mutex_unlock(&ocelot->mact_lock);
187 
188 	return ret;
189 }
190 EXPORT_SYMBOL(ocelot_mact_learn_streamdata);
191 
192 static void ocelot_mact_init(struct ocelot *ocelot)
193 {
194 	/* Configure the learning mode entries attributes:
195 	 * - Do not copy the frame to the CPU extraction queues.
196 	 * - Use the vlan and mac_cpoy for dmac lookup.
197 	 */
198 	ocelot_rmw(ocelot, 0,
199 		   ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
200 		   | ANA_AGENCTRL_LEARN_FWD_KILL
201 		   | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
202 		   ANA_AGENCTRL);
203 
204 	/* Clear the MAC table. We are not concurrent with anyone, so
205 	 * holding &ocelot->mact_lock is pointless.
206 	 */
207 	ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
208 }
209 
210 static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
211 {
212 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
213 			 ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
214 			 ANA_PORT_VCAP_S2_CFG, port);
215 
216 	ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
217 			 ANA_PORT_VCAP_CFG, port);
218 
219 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
220 		       REW_PORT_CFG_ES0_EN,
221 		       REW_PORT_CFG, port);
222 }
223 
224 static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
225 {
226 	return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
227 }
228 
229 static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
230 {
231 	u32 val;
232 
233 	return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
234 		ocelot,
235 		val,
236 		(val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
237 		ANA_TABLES_VLANACCESS_CMD_IDLE,
238 		TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
239 }
240 
241 static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
242 {
243 	/* Select the VID to configure */
244 	ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
245 		     ANA_TABLES_VLANTIDX);
246 	/* Set the vlan port members mask and issue a write command */
247 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
248 			     ANA_TABLES_VLANACCESS_CMD_WRITE,
249 		     ANA_TABLES_VLANACCESS);
250 
251 	return ocelot_vlant_wait_for_completion(ocelot);
252 }
253 
254 static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
255 {
256 	struct ocelot_bridge_vlan *vlan;
257 	int num_untagged = 0;
258 
259 	list_for_each_entry(vlan, &ocelot->vlans, list) {
260 		if (!(vlan->portmask & BIT(port)))
261 			continue;
262 
263 		if (vlan->untagged & BIT(port))
264 			num_untagged++;
265 	}
266 
267 	return num_untagged;
268 }
269 
270 static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
271 {
272 	struct ocelot_bridge_vlan *vlan;
273 	int num_tagged = 0;
274 
275 	list_for_each_entry(vlan, &ocelot->vlans, list) {
276 		if (!(vlan->portmask & BIT(port)))
277 			continue;
278 
279 		if (!(vlan->untagged & BIT(port)))
280 			num_tagged++;
281 	}
282 
283 	return num_tagged;
284 }
285 
286 /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
287  * _one_ egress-untagged VLAN (_the_ native VLAN)
288  */
289 static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
290 {
291 	return ocelot_port_num_tagged_vlans(ocelot, port) &&
292 	       ocelot_port_num_untagged_vlans(ocelot, port) == 1;
293 }
294 
295 static struct ocelot_bridge_vlan *
296 ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
297 {
298 	struct ocelot_bridge_vlan *vlan;
299 
300 	list_for_each_entry(vlan, &ocelot->vlans, list)
301 		if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
302 			return vlan;
303 
304 	return NULL;
305 }
306 
307 /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
308  * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
309  * state of the port.
310  */
311 static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
312 {
313 	struct ocelot_port *ocelot_port = ocelot->ports[port];
314 	enum ocelot_port_tag_config tag_cfg;
315 	bool uses_native_vlan = false;
316 
317 	if (ocelot_port->vlan_aware) {
318 		uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
319 
320 		if (uses_native_vlan)
321 			tag_cfg = OCELOT_PORT_TAG_NATIVE;
322 		else if (ocelot_port_num_untagged_vlans(ocelot, port))
323 			tag_cfg = OCELOT_PORT_TAG_DISABLED;
324 		else
325 			tag_cfg = OCELOT_PORT_TAG_TRUNK;
326 	} else {
327 		tag_cfg = OCELOT_PORT_TAG_DISABLED;
328 	}
329 
330 	ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
331 		       REW_TAG_CFG_TAG_CFG_M,
332 		       REW_TAG_CFG, port);
333 
334 	if (uses_native_vlan) {
335 		struct ocelot_bridge_vlan *native_vlan;
336 
337 		/* Not having a native VLAN is impossible, because
338 		 * ocelot_port_num_untagged_vlans has returned 1.
339 		 * So there is no use in checking for NULL here.
340 		 */
341 		native_vlan = ocelot_port_find_native_vlan(ocelot, port);
342 
343 		ocelot_rmw_gix(ocelot,
344 			       REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
345 			       REW_PORT_VLAN_CFG_PORT_VID_M,
346 			       REW_PORT_VLAN_CFG, port);
347 	}
348 }
349 
350 /* Default vlan to clasify for untagged frames (may be zero) */
351 static void ocelot_port_set_pvid(struct ocelot *ocelot, int port,
352 				 const struct ocelot_bridge_vlan *pvid_vlan)
353 {
354 	struct ocelot_port *ocelot_port = ocelot->ports[port];
355 	u16 pvid = OCELOT_VLAN_UNAWARE_PVID;
356 	u32 val = 0;
357 
358 	ocelot_port->pvid_vlan = pvid_vlan;
359 
360 	if (ocelot_port->vlan_aware && pvid_vlan)
361 		pvid = pvid_vlan->vid;
362 
363 	ocelot_rmw_gix(ocelot,
364 		       ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
365 		       ANA_PORT_VLAN_CFG_VLAN_VID_M,
366 		       ANA_PORT_VLAN_CFG, port);
367 
368 	/* If there's no pvid, we should drop not only untagged traffic (which
369 	 * happens automatically), but also 802.1p traffic which gets
370 	 * classified to VLAN 0, but that is always in our RX filter, so it
371 	 * would get accepted were it not for this setting.
372 	 */
373 	if (!pvid_vlan && ocelot_port->vlan_aware)
374 		val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
375 		      ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
376 
377 	ocelot_rmw_gix(ocelot, val,
378 		       ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
379 		       ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA,
380 		       ANA_PORT_DROP_CFG, port);
381 }
382 
383 static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
384 							  u16 vid)
385 {
386 	struct ocelot_bridge_vlan *vlan;
387 
388 	list_for_each_entry(vlan, &ocelot->vlans, list)
389 		if (vlan->vid == vid)
390 			return vlan;
391 
392 	return NULL;
393 }
394 
395 static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
396 				  bool untagged)
397 {
398 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
399 	unsigned long portmask;
400 	int err;
401 
402 	if (vlan) {
403 		portmask = vlan->portmask | BIT(port);
404 
405 		err = ocelot_vlant_set_mask(ocelot, vid, portmask);
406 		if (err)
407 			return err;
408 
409 		vlan->portmask = portmask;
410 		/* Bridge VLANs can be overwritten with a different
411 		 * egress-tagging setting, so make sure to override an untagged
412 		 * with a tagged VID if that's going on.
413 		 */
414 		if (untagged)
415 			vlan->untagged |= BIT(port);
416 		else
417 			vlan->untagged &= ~BIT(port);
418 
419 		return 0;
420 	}
421 
422 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
423 	if (!vlan)
424 		return -ENOMEM;
425 
426 	portmask = BIT(port);
427 
428 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
429 	if (err) {
430 		kfree(vlan);
431 		return err;
432 	}
433 
434 	vlan->vid = vid;
435 	vlan->portmask = portmask;
436 	if (untagged)
437 		vlan->untagged = BIT(port);
438 	INIT_LIST_HEAD(&vlan->list);
439 	list_add_tail(&vlan->list, &ocelot->vlans);
440 
441 	return 0;
442 }
443 
444 static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
445 {
446 	struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
447 	unsigned long portmask;
448 	int err;
449 
450 	if (!vlan)
451 		return 0;
452 
453 	portmask = vlan->portmask & ~BIT(port);
454 
455 	err = ocelot_vlant_set_mask(ocelot, vid, portmask);
456 	if (err)
457 		return err;
458 
459 	vlan->portmask = portmask;
460 	if (vlan->portmask)
461 		return 0;
462 
463 	list_del(&vlan->list);
464 	kfree(vlan);
465 
466 	return 0;
467 }
468 
469 int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
470 			       bool vlan_aware, struct netlink_ext_ack *extack)
471 {
472 	struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
473 	struct ocelot_port *ocelot_port = ocelot->ports[port];
474 	struct ocelot_vcap_filter *filter;
475 	u32 val;
476 
477 	list_for_each_entry(filter, &block->rules, list) {
478 		if (filter->ingress_port_mask & BIT(port) &&
479 		    filter->action.vid_replace_ena) {
480 			NL_SET_ERR_MSG_MOD(extack,
481 					   "Cannot change VLAN state with vlan modify rules active");
482 			return -EBUSY;
483 		}
484 	}
485 
486 	ocelot_port->vlan_aware = vlan_aware;
487 
488 	if (vlan_aware)
489 		val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
490 		      ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
491 	else
492 		val = 0;
493 	ocelot_rmw_gix(ocelot, val,
494 		       ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
495 		       ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
496 		       ANA_PORT_VLAN_CFG, port);
497 
498 	ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
499 	ocelot_port_manage_port_tag(ocelot, port);
500 
501 	return 0;
502 }
503 EXPORT_SYMBOL(ocelot_port_vlan_filtering);
504 
505 int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
506 			bool untagged, struct netlink_ext_ack *extack)
507 {
508 	if (untagged) {
509 		/* We are adding an egress-tagged VLAN */
510 		if (ocelot_port_uses_native_vlan(ocelot, port)) {
511 			NL_SET_ERR_MSG_MOD(extack,
512 					   "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
513 			return -EBUSY;
514 		}
515 	} else {
516 		/* We are adding an egress-tagged VLAN */
517 		if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
518 			NL_SET_ERR_MSG_MOD(extack,
519 					   "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
520 			return -EBUSY;
521 		}
522 	}
523 
524 	return 0;
525 }
526 EXPORT_SYMBOL(ocelot_vlan_prepare);
527 
528 int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
529 		    bool untagged)
530 {
531 	int err;
532 
533 	err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
534 	if (err)
535 		return err;
536 
537 	/* Default ingress vlan classification */
538 	if (pvid)
539 		ocelot_port_set_pvid(ocelot, port,
540 				     ocelot_bridge_vlan_find(ocelot, vid));
541 
542 	/* Untagged egress vlan clasification */
543 	ocelot_port_manage_port_tag(ocelot, port);
544 
545 	return 0;
546 }
547 EXPORT_SYMBOL(ocelot_vlan_add);
548 
549 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
550 {
551 	struct ocelot_port *ocelot_port = ocelot->ports[port];
552 	int err;
553 
554 	err = ocelot_vlan_member_del(ocelot, port, vid);
555 	if (err)
556 		return err;
557 
558 	/* Ingress */
559 	if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
560 		ocelot_port_set_pvid(ocelot, port, NULL);
561 
562 	/* Egress */
563 	ocelot_port_manage_port_tag(ocelot, port);
564 
565 	return 0;
566 }
567 EXPORT_SYMBOL(ocelot_vlan_del);
568 
569 static void ocelot_vlan_init(struct ocelot *ocelot)
570 {
571 	unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
572 	u16 port, vid;
573 
574 	/* Clear VLAN table, by default all ports are members of all VLANs */
575 	ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
576 		     ANA_TABLES_VLANACCESS);
577 	ocelot_vlant_wait_for_completion(ocelot);
578 
579 	/* Configure the port VLAN memberships */
580 	for (vid = 1; vid < VLAN_N_VID; vid++)
581 		ocelot_vlant_set_mask(ocelot, vid, 0);
582 
583 	/* Because VLAN filtering is enabled, we need VID 0 to get untagged
584 	 * traffic.  It is added automatically if 8021q module is loaded, but
585 	 * we can't rely on it since module may be not loaded.
586 	 */
587 	ocelot_vlant_set_mask(ocelot, OCELOT_VLAN_UNAWARE_PVID, all_ports);
588 
589 	/* Set vlan ingress filter mask to all ports but the CPU port by
590 	 * default.
591 	 */
592 	ocelot_write(ocelot, all_ports, ANA_VLANMASK);
593 
594 	for (port = 0; port < ocelot->num_phys_ports; port++) {
595 		ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
596 		ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
597 	}
598 }
599 
600 static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
601 {
602 	return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
603 }
604 
605 static int ocelot_port_flush(struct ocelot *ocelot, int port)
606 {
607 	unsigned int pause_ena;
608 	int err, val;
609 
610 	/* Disable dequeuing from the egress queues */
611 	ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
612 		       QSYS_PORT_MODE_DEQUEUE_DIS,
613 		       QSYS_PORT_MODE, port);
614 
615 	/* Disable flow control */
616 	ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
617 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
618 
619 	/* Disable priority flow control */
620 	ocelot_fields_write(ocelot, port,
621 			    QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
622 
623 	/* Wait at least the time it takes to receive a frame of maximum length
624 	 * at the port.
625 	 * Worst-case delays for 10 kilobyte jumbo frames are:
626 	 * 8 ms on a 10M port
627 	 * 800 μs on a 100M port
628 	 * 80 μs on a 1G port
629 	 * 32 μs on a 2.5G port
630 	 */
631 	usleep_range(8000, 10000);
632 
633 	/* Disable half duplex backpressure. */
634 	ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
635 		       SYS_FRONT_PORT_MODE, port);
636 
637 	/* Flush the queues associated with the port. */
638 	ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
639 		       REW_PORT_CFG, port);
640 
641 	/* Enable dequeuing from the egress queues. */
642 	ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
643 		       port);
644 
645 	/* Wait until flushing is complete. */
646 	err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
647 				100, 2000000, false, ocelot, port);
648 
649 	/* Clear flushing again. */
650 	ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
651 
652 	/* Re-enable flow control */
653 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
654 
655 	return err;
656 }
657 
658 void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
659 				  unsigned int link_an_mode,
660 				  phy_interface_t interface,
661 				  unsigned long quirks)
662 {
663 	struct ocelot_port *ocelot_port = ocelot->ports[port];
664 	int err;
665 
666 	ocelot_port->speed = SPEED_UNKNOWN;
667 
668 	ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
669 			 DEV_MAC_ENA_CFG);
670 
671 	if (ocelot->ops->cut_through_fwd) {
672 		mutex_lock(&ocelot->fwd_domain_lock);
673 		ocelot->ops->cut_through_fwd(ocelot);
674 		mutex_unlock(&ocelot->fwd_domain_lock);
675 	}
676 
677 	ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
678 
679 	err = ocelot_port_flush(ocelot, port);
680 	if (err)
681 		dev_err(ocelot->dev, "failed to flush port %d: %d\n",
682 			port, err);
683 
684 	/* Put the port in reset. */
685 	if (interface != PHY_INTERFACE_MODE_QSGMII ||
686 	    !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
687 		ocelot_port_rmwl(ocelot_port,
688 				 DEV_CLOCK_CFG_MAC_TX_RST |
689 				 DEV_CLOCK_CFG_MAC_RX_RST,
690 				 DEV_CLOCK_CFG_MAC_TX_RST |
691 				 DEV_CLOCK_CFG_MAC_RX_RST,
692 				 DEV_CLOCK_CFG);
693 }
694 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
695 
696 void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
697 				struct phy_device *phydev,
698 				unsigned int link_an_mode,
699 				phy_interface_t interface,
700 				int speed, int duplex,
701 				bool tx_pause, bool rx_pause,
702 				unsigned long quirks)
703 {
704 	struct ocelot_port *ocelot_port = ocelot->ports[port];
705 	int mac_speed, mode = 0;
706 	u32 mac_fc_cfg;
707 
708 	ocelot_port->speed = speed;
709 
710 	/* The MAC might be integrated in systems where the MAC speed is fixed
711 	 * and it's the PCS who is performing the rate adaptation, so we have
712 	 * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
713 	 * (which is also its default value).
714 	 */
715 	if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
716 	    speed == SPEED_1000) {
717 		mac_speed = OCELOT_SPEED_1000;
718 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
719 	} else if (speed == SPEED_2500) {
720 		mac_speed = OCELOT_SPEED_2500;
721 		mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
722 	} else if (speed == SPEED_100) {
723 		mac_speed = OCELOT_SPEED_100;
724 	} else {
725 		mac_speed = OCELOT_SPEED_10;
726 	}
727 
728 	if (duplex == DUPLEX_FULL)
729 		mode |= DEV_MAC_MODE_CFG_FDX_ENA;
730 
731 	ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
732 
733 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
734 	 * PORT_RST bits in DEV_CLOCK_CFG.
735 	 */
736 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
737 			   DEV_CLOCK_CFG);
738 
739 	switch (speed) {
740 	case SPEED_10:
741 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
742 		break;
743 	case SPEED_100:
744 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
745 		break;
746 	case SPEED_1000:
747 	case SPEED_2500:
748 		mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
749 		break;
750 	default:
751 		dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
752 			port, speed);
753 		return;
754 	}
755 
756 	/* Handle RX pause in all cases, with 2500base-X this is used for rate
757 	 * adaptation.
758 	 */
759 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
760 
761 	if (tx_pause)
762 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
763 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
764 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
765 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
766 
767 	/* Flow control. Link speed is only used here to evaluate the time
768 	 * specification in incoming pause frames.
769 	 */
770 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
771 
772 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
773 
774 	/* Don't attempt to send PAUSE frames on the NPI port, it's broken */
775 	if (port != ocelot->npi)
776 		ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
777 				    tx_pause);
778 
779 	/* Undo the effects of ocelot_phylink_mac_link_down:
780 	 * enable MAC module
781 	 */
782 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
783 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
784 
785 	/* If the port supports cut-through forwarding, update the masks before
786 	 * enabling forwarding on the port.
787 	 */
788 	if (ocelot->ops->cut_through_fwd) {
789 		mutex_lock(&ocelot->fwd_domain_lock);
790 		ocelot->ops->cut_through_fwd(ocelot);
791 		mutex_unlock(&ocelot->fwd_domain_lock);
792 	}
793 
794 	/* Core: Enable port for frame transfer */
795 	ocelot_fields_write(ocelot, port,
796 			    QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
797 }
798 EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
799 
800 static int ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
801 					struct sk_buff *clone)
802 {
803 	struct ocelot_port *ocelot_port = ocelot->ports[port];
804 	unsigned long flags;
805 
806 	spin_lock_irqsave(&ocelot->ts_id_lock, flags);
807 
808 	if (ocelot_port->ptp_skbs_in_flight == OCELOT_MAX_PTP_ID ||
809 	    ocelot->ptp_skbs_in_flight == OCELOT_PTP_FIFO_SIZE) {
810 		spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
811 		return -EBUSY;
812 	}
813 
814 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
815 	/* Store timestamp ID in OCELOT_SKB_CB(clone)->ts_id */
816 	OCELOT_SKB_CB(clone)->ts_id = ocelot_port->ts_id;
817 
818 	ocelot_port->ts_id++;
819 	if (ocelot_port->ts_id == OCELOT_MAX_PTP_ID)
820 		ocelot_port->ts_id = 0;
821 
822 	ocelot_port->ptp_skbs_in_flight++;
823 	ocelot->ptp_skbs_in_flight++;
824 
825 	skb_queue_tail(&ocelot_port->tx_skbs, clone);
826 
827 	spin_unlock_irqrestore(&ocelot->ts_id_lock, flags);
828 
829 	return 0;
830 }
831 
832 static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb,
833 				       unsigned int ptp_class)
834 {
835 	struct ptp_header *hdr;
836 	u8 msgtype, twostep;
837 
838 	hdr = ptp_parse_header(skb, ptp_class);
839 	if (!hdr)
840 		return false;
841 
842 	msgtype = ptp_get_msgtype(hdr, ptp_class);
843 	twostep = hdr->flag_field[0] & 0x2;
844 
845 	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
846 		return true;
847 
848 	return false;
849 }
850 
851 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
852 				 struct sk_buff *skb,
853 				 struct sk_buff **clone)
854 {
855 	struct ocelot_port *ocelot_port = ocelot->ports[port];
856 	u8 ptp_cmd = ocelot_port->ptp_cmd;
857 	unsigned int ptp_class;
858 	int err;
859 
860 	/* Don't do anything if PTP timestamping not enabled */
861 	if (!ptp_cmd)
862 		return 0;
863 
864 	ptp_class = ptp_classify_raw(skb);
865 	if (ptp_class == PTP_CLASS_NONE)
866 		return -EINVAL;
867 
868 	/* Store ptp_cmd in OCELOT_SKB_CB(skb)->ptp_cmd */
869 	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
870 		if (ocelot_ptp_is_onestep_sync(skb, ptp_class)) {
871 			OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
872 			return 0;
873 		}
874 
875 		/* Fall back to two-step timestamping */
876 		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
877 	}
878 
879 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
880 		*clone = skb_clone_sk(skb);
881 		if (!(*clone))
882 			return -ENOMEM;
883 
884 		err = ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
885 		if (err)
886 			return err;
887 
888 		OCELOT_SKB_CB(skb)->ptp_cmd = ptp_cmd;
889 		OCELOT_SKB_CB(*clone)->ptp_class = ptp_class;
890 	}
891 
892 	return 0;
893 }
894 EXPORT_SYMBOL(ocelot_port_txtstamp_request);
895 
896 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
897 				   struct timespec64 *ts)
898 {
899 	unsigned long flags;
900 	u32 val;
901 
902 	spin_lock_irqsave(&ocelot->ptp_clock_lock, flags);
903 
904 	/* Read current PTP time to get seconds */
905 	val = ocelot_read_rix(ocelot, PTP_PIN_CFG, TOD_ACC_PIN);
906 
907 	val &= ~(PTP_PIN_CFG_SYNC | PTP_PIN_CFG_ACTION_MASK | PTP_PIN_CFG_DOM);
908 	val |= PTP_PIN_CFG_ACTION(PTP_PIN_ACTION_SAVE);
909 	ocelot_write_rix(ocelot, val, PTP_PIN_CFG, TOD_ACC_PIN);
910 	ts->tv_sec = ocelot_read_rix(ocelot, PTP_PIN_TOD_SEC_LSB, TOD_ACC_PIN);
911 
912 	/* Read packet HW timestamp from FIFO */
913 	val = ocelot_read(ocelot, SYS_PTP_TXSTAMP);
914 	ts->tv_nsec = SYS_PTP_TXSTAMP_PTP_TXSTAMP(val);
915 
916 	/* Sec has incremented since the ts was registered */
917 	if ((ts->tv_sec & 0x1) != !!(val & SYS_PTP_TXSTAMP_PTP_TXSTAMP_SEC))
918 		ts->tv_sec--;
919 
920 	spin_unlock_irqrestore(&ocelot->ptp_clock_lock, flags);
921 }
922 
923 static bool ocelot_validate_ptp_skb(struct sk_buff *clone, u16 seqid)
924 {
925 	struct ptp_header *hdr;
926 
927 	hdr = ptp_parse_header(clone, OCELOT_SKB_CB(clone)->ptp_class);
928 	if (WARN_ON(!hdr))
929 		return false;
930 
931 	return seqid == ntohs(hdr->sequence_id);
932 }
933 
934 void ocelot_get_txtstamp(struct ocelot *ocelot)
935 {
936 	int budget = OCELOT_PTP_QUEUE_SZ;
937 
938 	while (budget--) {
939 		struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
940 		struct skb_shared_hwtstamps shhwtstamps;
941 		u32 val, id, seqid, txport;
942 		struct ocelot_port *port;
943 		struct timespec64 ts;
944 		unsigned long flags;
945 
946 		val = ocelot_read(ocelot, SYS_PTP_STATUS);
947 
948 		/* Check if a timestamp can be retrieved */
949 		if (!(val & SYS_PTP_STATUS_PTP_MESS_VLD))
950 			break;
951 
952 		WARN_ON(val & SYS_PTP_STATUS_PTP_OVFL);
953 
954 		/* Retrieve the ts ID and Tx port */
955 		id = SYS_PTP_STATUS_PTP_MESS_ID_X(val);
956 		txport = SYS_PTP_STATUS_PTP_MESS_TXPORT_X(val);
957 		seqid = SYS_PTP_STATUS_PTP_MESS_SEQ_ID(val);
958 
959 		port = ocelot->ports[txport];
960 
961 		spin_lock(&ocelot->ts_id_lock);
962 		port->ptp_skbs_in_flight--;
963 		ocelot->ptp_skbs_in_flight--;
964 		spin_unlock(&ocelot->ts_id_lock);
965 
966 		/* Retrieve its associated skb */
967 try_again:
968 		spin_lock_irqsave(&port->tx_skbs.lock, flags);
969 
970 		skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
971 			if (OCELOT_SKB_CB(skb)->ts_id != id)
972 				continue;
973 			__skb_unlink(skb, &port->tx_skbs);
974 			skb_match = skb;
975 			break;
976 		}
977 
978 		spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
979 
980 		if (WARN_ON(!skb_match))
981 			continue;
982 
983 		if (!ocelot_validate_ptp_skb(skb_match, seqid)) {
984 			dev_err_ratelimited(ocelot->dev,
985 					    "port %d received stale TX timestamp for seqid %d, discarding\n",
986 					    txport, seqid);
987 			dev_kfree_skb_any(skb);
988 			goto try_again;
989 		}
990 
991 		/* Get the h/w timestamp */
992 		ocelot_get_hwtimestamp(ocelot, &ts);
993 
994 		/* Set the timestamp into the skb */
995 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
996 		shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
997 		skb_complete_tx_timestamp(skb_match, &shhwtstamps);
998 
999 		/* Next ts */
1000 		ocelot_write(ocelot, SYS_PTP_NXT_PTP_NXT, SYS_PTP_NXT);
1001 	}
1002 }
1003 EXPORT_SYMBOL(ocelot_get_txtstamp);
1004 
1005 static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
1006 				u32 *rval)
1007 {
1008 	u32 bytes_valid, val;
1009 
1010 	val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1011 	if (val == XTR_NOT_READY) {
1012 		if (ifh)
1013 			return -EIO;
1014 
1015 		do {
1016 			val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1017 		} while (val == XTR_NOT_READY);
1018 	}
1019 
1020 	switch (val) {
1021 	case XTR_ABORT:
1022 		return -EIO;
1023 	case XTR_EOF_0:
1024 	case XTR_EOF_1:
1025 	case XTR_EOF_2:
1026 	case XTR_EOF_3:
1027 	case XTR_PRUNED:
1028 		bytes_valid = XTR_VALID_BYTES(val);
1029 		val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1030 		if (val == XTR_ESCAPE)
1031 			*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1032 		else
1033 			*rval = val;
1034 
1035 		return bytes_valid;
1036 	case XTR_ESCAPE:
1037 		*rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1038 
1039 		return 4;
1040 	default:
1041 		*rval = val;
1042 
1043 		return 4;
1044 	}
1045 }
1046 
1047 static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
1048 {
1049 	int i, err = 0;
1050 
1051 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
1052 		err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
1053 		if (err != 4)
1054 			return (err < 0) ? err : -EIO;
1055 	}
1056 
1057 	return 0;
1058 }
1059 
1060 void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
1061 			     u64 timestamp)
1062 {
1063 	struct skb_shared_hwtstamps *shhwtstamps;
1064 	u64 tod_in_ns, full_ts_in_ns;
1065 	struct timespec64 ts;
1066 
1067 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1068 
1069 	tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
1070 	if ((tod_in_ns & 0xffffffff) < timestamp)
1071 		full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
1072 				timestamp;
1073 	else
1074 		full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
1075 				timestamp;
1076 
1077 	shhwtstamps = skb_hwtstamps(skb);
1078 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
1079 	shhwtstamps->hwtstamp = full_ts_in_ns;
1080 }
1081 EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
1082 
1083 int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
1084 {
1085 	u64 timestamp, src_port, len;
1086 	u32 xfh[OCELOT_TAG_LEN / 4];
1087 	struct net_device *dev;
1088 	struct sk_buff *skb;
1089 	int sz, buf_len;
1090 	u32 val, *buf;
1091 	int err;
1092 
1093 	err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
1094 	if (err)
1095 		return err;
1096 
1097 	ocelot_xfh_get_src_port(xfh, &src_port);
1098 	ocelot_xfh_get_len(xfh, &len);
1099 	ocelot_xfh_get_rew_val(xfh, &timestamp);
1100 
1101 	if (WARN_ON(src_port >= ocelot->num_phys_ports))
1102 		return -EINVAL;
1103 
1104 	dev = ocelot->ops->port_to_netdev(ocelot, src_port);
1105 	if (!dev)
1106 		return -EINVAL;
1107 
1108 	skb = netdev_alloc_skb(dev, len);
1109 	if (unlikely(!skb)) {
1110 		netdev_err(dev, "Unable to allocate sk_buff\n");
1111 		return -ENOMEM;
1112 	}
1113 
1114 	buf_len = len - ETH_FCS_LEN;
1115 	buf = (u32 *)skb_put(skb, buf_len);
1116 
1117 	len = 0;
1118 	do {
1119 		sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1120 		if (sz < 0) {
1121 			err = sz;
1122 			goto out_free_skb;
1123 		}
1124 		*buf++ = val;
1125 		len += sz;
1126 	} while (len < buf_len);
1127 
1128 	/* Read the FCS */
1129 	sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
1130 	if (sz < 0) {
1131 		err = sz;
1132 		goto out_free_skb;
1133 	}
1134 
1135 	/* Update the statistics if part of the FCS was read before */
1136 	len -= ETH_FCS_LEN - sz;
1137 
1138 	if (unlikely(dev->features & NETIF_F_RXFCS)) {
1139 		buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
1140 		*buf = val;
1141 	}
1142 
1143 	if (ocelot->ptp)
1144 		ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
1145 
1146 	/* Everything we see on an interface that is in the HW bridge
1147 	 * has already been forwarded.
1148 	 */
1149 	if (ocelot->ports[src_port]->bridge)
1150 		skb->offload_fwd_mark = 1;
1151 
1152 	skb->protocol = eth_type_trans(skb, dev);
1153 
1154 	*nskb = skb;
1155 
1156 	return 0;
1157 
1158 out_free_skb:
1159 	kfree_skb(skb);
1160 	return err;
1161 }
1162 EXPORT_SYMBOL(ocelot_xtr_poll_frame);
1163 
1164 bool ocelot_can_inject(struct ocelot *ocelot, int grp)
1165 {
1166 	u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
1167 
1168 	if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
1169 		return false;
1170 	if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
1171 		return false;
1172 
1173 	return true;
1174 }
1175 EXPORT_SYMBOL(ocelot_can_inject);
1176 
1177 void ocelot_ifh_port_set(void *ifh, int port, u32 rew_op, u32 vlan_tag)
1178 {
1179 	ocelot_ifh_set_bypass(ifh, 1);
1180 	ocelot_ifh_set_dest(ifh, BIT_ULL(port));
1181 	ocelot_ifh_set_tag_type(ifh, IFH_TAG_TYPE_C);
1182 	if (vlan_tag)
1183 		ocelot_ifh_set_vlan_tci(ifh, vlan_tag);
1184 	if (rew_op)
1185 		ocelot_ifh_set_rew_op(ifh, rew_op);
1186 }
1187 EXPORT_SYMBOL(ocelot_ifh_port_set);
1188 
1189 void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
1190 			      u32 rew_op, struct sk_buff *skb)
1191 {
1192 	u32 ifh[OCELOT_TAG_LEN / 4] = {0};
1193 	unsigned int i, count, last;
1194 
1195 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1196 			 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
1197 
1198 	ocelot_ifh_port_set(ifh, port, rew_op, skb_vlan_tag_get(skb));
1199 
1200 	for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
1201 		ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
1202 
1203 	count = DIV_ROUND_UP(skb->len, 4);
1204 	last = skb->len % 4;
1205 	for (i = 0; i < count; i++)
1206 		ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
1207 
1208 	/* Add padding */
1209 	while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
1210 		ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1211 		i++;
1212 	}
1213 
1214 	/* Indicate EOF and valid bytes in last word */
1215 	ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
1216 			 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
1217 			 QS_INJ_CTRL_EOF,
1218 			 QS_INJ_CTRL, grp);
1219 
1220 	/* Add dummy CRC */
1221 	ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
1222 	skb_tx_timestamp(skb);
1223 
1224 	skb->dev->stats.tx_packets++;
1225 	skb->dev->stats.tx_bytes += skb->len;
1226 }
1227 EXPORT_SYMBOL(ocelot_port_inject_frame);
1228 
1229 void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
1230 {
1231 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
1232 		ocelot_read_rix(ocelot, QS_XTR_RD, grp);
1233 }
1234 EXPORT_SYMBOL(ocelot_drain_cpu_queue);
1235 
1236 int ocelot_fdb_add(struct ocelot *ocelot, int port,
1237 		   const unsigned char *addr, u16 vid)
1238 {
1239 	int pgid = port;
1240 
1241 	if (port == ocelot->npi)
1242 		pgid = PGID_CPU;
1243 
1244 	return ocelot_mact_learn(ocelot, pgid, addr, vid, ENTRYTYPE_LOCKED);
1245 }
1246 EXPORT_SYMBOL(ocelot_fdb_add);
1247 
1248 int ocelot_fdb_del(struct ocelot *ocelot, int port,
1249 		   const unsigned char *addr, u16 vid)
1250 {
1251 	return ocelot_mact_forget(ocelot, addr, vid);
1252 }
1253 EXPORT_SYMBOL(ocelot_fdb_del);
1254 
1255 int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
1256 			    bool is_static, void *data)
1257 {
1258 	struct ocelot_dump_ctx *dump = data;
1259 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
1260 	u32 seq = dump->cb->nlh->nlmsg_seq;
1261 	struct nlmsghdr *nlh;
1262 	struct ndmsg *ndm;
1263 
1264 	if (dump->idx < dump->cb->args[2])
1265 		goto skip;
1266 
1267 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
1268 			sizeof(*ndm), NLM_F_MULTI);
1269 	if (!nlh)
1270 		return -EMSGSIZE;
1271 
1272 	ndm = nlmsg_data(nlh);
1273 	ndm->ndm_family  = AF_BRIDGE;
1274 	ndm->ndm_pad1    = 0;
1275 	ndm->ndm_pad2    = 0;
1276 	ndm->ndm_flags   = NTF_SELF;
1277 	ndm->ndm_type    = 0;
1278 	ndm->ndm_ifindex = dump->dev->ifindex;
1279 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
1280 
1281 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
1282 		goto nla_put_failure;
1283 
1284 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
1285 		goto nla_put_failure;
1286 
1287 	nlmsg_end(dump->skb, nlh);
1288 
1289 skip:
1290 	dump->idx++;
1291 	return 0;
1292 
1293 nla_put_failure:
1294 	nlmsg_cancel(dump->skb, nlh);
1295 	return -EMSGSIZE;
1296 }
1297 EXPORT_SYMBOL(ocelot_port_fdb_do_dump);
1298 
1299 /* Caller must hold &ocelot->mact_lock */
1300 static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
1301 			    struct ocelot_mact_entry *entry)
1302 {
1303 	u32 val, dst, macl, mach;
1304 	char mac[ETH_ALEN];
1305 
1306 	/* Set row and column to read from */
1307 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
1308 	ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
1309 
1310 	/* Issue a read command */
1311 	ocelot_write(ocelot,
1312 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
1313 		     ANA_TABLES_MACACCESS);
1314 
1315 	if (ocelot_mact_wait_for_completion(ocelot))
1316 		return -ETIMEDOUT;
1317 
1318 	/* Read the entry flags */
1319 	val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
1320 	if (!(val & ANA_TABLES_MACACCESS_VALID))
1321 		return -EINVAL;
1322 
1323 	/* If the entry read has another port configured as its destination,
1324 	 * do not report it.
1325 	 */
1326 	dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
1327 	if (dst != port)
1328 		return -EINVAL;
1329 
1330 	/* Get the entry's MAC address and VLAN id */
1331 	macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
1332 	mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
1333 
1334 	mac[0] = (mach >> 8)  & 0xff;
1335 	mac[1] = (mach >> 0)  & 0xff;
1336 	mac[2] = (macl >> 24) & 0xff;
1337 	mac[3] = (macl >> 16) & 0xff;
1338 	mac[4] = (macl >> 8)  & 0xff;
1339 	mac[5] = (macl >> 0)  & 0xff;
1340 
1341 	entry->vid = (mach >> 16) & 0xfff;
1342 	ether_addr_copy(entry->mac, mac);
1343 
1344 	return 0;
1345 }
1346 
1347 int ocelot_mact_flush(struct ocelot *ocelot, int port)
1348 {
1349 	int err;
1350 
1351 	mutex_lock(&ocelot->mact_lock);
1352 
1353 	/* Program ageing filter for a single port */
1354 	ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
1355 		     ANA_ANAGEFIL);
1356 
1357 	/* Flushing dynamic FDB entries requires two successive age scans */
1358 	ocelot_write(ocelot,
1359 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
1360 		     ANA_TABLES_MACACCESS);
1361 
1362 	err = ocelot_mact_wait_for_completion(ocelot);
1363 	if (err) {
1364 		mutex_unlock(&ocelot->mact_lock);
1365 		return err;
1366 	}
1367 
1368 	/* And second... */
1369 	ocelot_write(ocelot,
1370 		     ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
1371 		     ANA_TABLES_MACACCESS);
1372 
1373 	err = ocelot_mact_wait_for_completion(ocelot);
1374 
1375 	/* Restore ageing filter */
1376 	ocelot_write(ocelot, 0, ANA_ANAGEFIL);
1377 
1378 	mutex_unlock(&ocelot->mact_lock);
1379 
1380 	return err;
1381 }
1382 EXPORT_SYMBOL_GPL(ocelot_mact_flush);
1383 
1384 int ocelot_fdb_dump(struct ocelot *ocelot, int port,
1385 		    dsa_fdb_dump_cb_t *cb, void *data)
1386 {
1387 	int err = 0;
1388 	int i, j;
1389 
1390 	/* We could take the lock just around ocelot_mact_read, but doing so
1391 	 * thousands of times in a row seems rather pointless and inefficient.
1392 	 */
1393 	mutex_lock(&ocelot->mact_lock);
1394 
1395 	/* Loop through all the mac tables entries. */
1396 	for (i = 0; i < ocelot->num_mact_rows; i++) {
1397 		for (j = 0; j < 4; j++) {
1398 			struct ocelot_mact_entry entry;
1399 			bool is_static;
1400 
1401 			err = ocelot_mact_read(ocelot, port, i, j, &entry);
1402 			/* If the entry is invalid (wrong port, invalid...),
1403 			 * skip it.
1404 			 */
1405 			if (err == -EINVAL)
1406 				continue;
1407 			else if (err)
1408 				break;
1409 
1410 			is_static = (entry.type == ENTRYTYPE_LOCKED);
1411 
1412 			err = cb(entry.mac, entry.vid, is_static, data);
1413 			if (err)
1414 				break;
1415 		}
1416 	}
1417 
1418 	mutex_unlock(&ocelot->mact_lock);
1419 
1420 	return err;
1421 }
1422 EXPORT_SYMBOL(ocelot_fdb_dump);
1423 
1424 static void ocelot_populate_l2_ptp_trap_key(struct ocelot_vcap_filter *trap)
1425 {
1426 	trap->key_type = OCELOT_VCAP_KEY_ETYPE;
1427 	*(__be16 *)trap->key.etype.etype.value = htons(ETH_P_1588);
1428 	*(__be16 *)trap->key.etype.etype.mask = htons(0xffff);
1429 }
1430 
1431 static void
1432 ocelot_populate_ipv4_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
1433 {
1434 	trap->key_type = OCELOT_VCAP_KEY_IPV4;
1435 	trap->key.ipv4.dport.value = PTP_EV_PORT;
1436 	trap->key.ipv4.dport.mask = 0xffff;
1437 }
1438 
1439 static void
1440 ocelot_populate_ipv6_ptp_event_trap_key(struct ocelot_vcap_filter *trap)
1441 {
1442 	trap->key_type = OCELOT_VCAP_KEY_IPV6;
1443 	trap->key.ipv6.dport.value = PTP_EV_PORT;
1444 	trap->key.ipv6.dport.mask = 0xffff;
1445 }
1446 
1447 static void
1448 ocelot_populate_ipv4_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
1449 {
1450 	trap->key_type = OCELOT_VCAP_KEY_IPV4;
1451 	trap->key.ipv4.dport.value = PTP_GEN_PORT;
1452 	trap->key.ipv4.dport.mask = 0xffff;
1453 }
1454 
1455 static void
1456 ocelot_populate_ipv6_ptp_general_trap_key(struct ocelot_vcap_filter *trap)
1457 {
1458 	trap->key_type = OCELOT_VCAP_KEY_IPV6;
1459 	trap->key.ipv6.dport.value = PTP_GEN_PORT;
1460 	trap->key.ipv6.dport.mask = 0xffff;
1461 }
1462 
1463 static int ocelot_trap_add(struct ocelot *ocelot, int port,
1464 			   unsigned long cookie,
1465 			   void (*populate)(struct ocelot_vcap_filter *f))
1466 {
1467 	struct ocelot_vcap_block *block_vcap_is2;
1468 	struct ocelot_vcap_filter *trap;
1469 	bool new = false;
1470 	int err;
1471 
1472 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
1473 
1474 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
1475 						   false);
1476 	if (!trap) {
1477 		trap = kzalloc(sizeof(*trap), GFP_KERNEL);
1478 		if (!trap)
1479 			return -ENOMEM;
1480 
1481 		populate(trap);
1482 		trap->prio = 1;
1483 		trap->id.cookie = cookie;
1484 		trap->id.tc_offload = false;
1485 		trap->block_id = VCAP_IS2;
1486 		trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
1487 		trap->lookup = 0;
1488 		trap->action.cpu_copy_ena = true;
1489 		trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
1490 		trap->action.port_mask = 0;
1491 		new = true;
1492 	}
1493 
1494 	trap->ingress_port_mask |= BIT(port);
1495 
1496 	if (new)
1497 		err = ocelot_vcap_filter_add(ocelot, trap, NULL);
1498 	else
1499 		err = ocelot_vcap_filter_replace(ocelot, trap);
1500 	if (err) {
1501 		trap->ingress_port_mask &= ~BIT(port);
1502 		if (!trap->ingress_port_mask)
1503 			kfree(trap);
1504 		return err;
1505 	}
1506 
1507 	return 0;
1508 }
1509 
1510 static int ocelot_trap_del(struct ocelot *ocelot, int port,
1511 			   unsigned long cookie)
1512 {
1513 	struct ocelot_vcap_block *block_vcap_is2;
1514 	struct ocelot_vcap_filter *trap;
1515 
1516 	block_vcap_is2 = &ocelot->block[VCAP_IS2];
1517 
1518 	trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
1519 						   false);
1520 	if (!trap)
1521 		return 0;
1522 
1523 	trap->ingress_port_mask &= ~BIT(port);
1524 	if (!trap->ingress_port_mask)
1525 		return ocelot_vcap_filter_del(ocelot, trap);
1526 
1527 	return ocelot_vcap_filter_replace(ocelot, trap);
1528 }
1529 
1530 static int ocelot_l2_ptp_trap_add(struct ocelot *ocelot, int port)
1531 {
1532 	unsigned long l2_cookie = ocelot->num_phys_ports + 1;
1533 
1534 	return ocelot_trap_add(ocelot, port, l2_cookie,
1535 			       ocelot_populate_l2_ptp_trap_key);
1536 }
1537 
1538 static int ocelot_l2_ptp_trap_del(struct ocelot *ocelot, int port)
1539 {
1540 	unsigned long l2_cookie = ocelot->num_phys_ports + 1;
1541 
1542 	return ocelot_trap_del(ocelot, port, l2_cookie);
1543 }
1544 
1545 static int ocelot_ipv4_ptp_trap_add(struct ocelot *ocelot, int port)
1546 {
1547 	unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2;
1548 	unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3;
1549 	int err;
1550 
1551 	err = ocelot_trap_add(ocelot, port, ipv4_ev_cookie,
1552 			      ocelot_populate_ipv4_ptp_event_trap_key);
1553 	if (err)
1554 		return err;
1555 
1556 	err = ocelot_trap_add(ocelot, port, ipv4_gen_cookie,
1557 			      ocelot_populate_ipv4_ptp_general_trap_key);
1558 	if (err)
1559 		ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
1560 
1561 	return err;
1562 }
1563 
1564 static int ocelot_ipv4_ptp_trap_del(struct ocelot *ocelot, int port)
1565 {
1566 	unsigned long ipv4_gen_cookie = ocelot->num_phys_ports + 2;
1567 	unsigned long ipv4_ev_cookie = ocelot->num_phys_ports + 3;
1568 	int err;
1569 
1570 	err = ocelot_trap_del(ocelot, port, ipv4_ev_cookie);
1571 	err |= ocelot_trap_del(ocelot, port, ipv4_gen_cookie);
1572 	return err;
1573 }
1574 
1575 static int ocelot_ipv6_ptp_trap_add(struct ocelot *ocelot, int port)
1576 {
1577 	unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4;
1578 	unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5;
1579 	int err;
1580 
1581 	err = ocelot_trap_add(ocelot, port, ipv6_ev_cookie,
1582 			      ocelot_populate_ipv6_ptp_event_trap_key);
1583 	if (err)
1584 		return err;
1585 
1586 	err = ocelot_trap_add(ocelot, port, ipv6_gen_cookie,
1587 			      ocelot_populate_ipv6_ptp_general_trap_key);
1588 	if (err)
1589 		ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
1590 
1591 	return err;
1592 }
1593 
1594 static int ocelot_ipv6_ptp_trap_del(struct ocelot *ocelot, int port)
1595 {
1596 	unsigned long ipv6_gen_cookie = ocelot->num_phys_ports + 4;
1597 	unsigned long ipv6_ev_cookie = ocelot->num_phys_ports + 5;
1598 	int err;
1599 
1600 	err = ocelot_trap_del(ocelot, port, ipv6_ev_cookie);
1601 	err |= ocelot_trap_del(ocelot, port, ipv6_gen_cookie);
1602 	return err;
1603 }
1604 
1605 static int ocelot_setup_ptp_traps(struct ocelot *ocelot, int port,
1606 				  bool l2, bool l4)
1607 {
1608 	int err;
1609 
1610 	if (l2)
1611 		err = ocelot_l2_ptp_trap_add(ocelot, port);
1612 	else
1613 		err = ocelot_l2_ptp_trap_del(ocelot, port);
1614 	if (err)
1615 		return err;
1616 
1617 	if (l4) {
1618 		err = ocelot_ipv4_ptp_trap_add(ocelot, port);
1619 		if (err)
1620 			goto err_ipv4;
1621 
1622 		err = ocelot_ipv6_ptp_trap_add(ocelot, port);
1623 		if (err)
1624 			goto err_ipv6;
1625 	} else {
1626 		err = ocelot_ipv4_ptp_trap_del(ocelot, port);
1627 
1628 		err |= ocelot_ipv6_ptp_trap_del(ocelot, port);
1629 	}
1630 	if (err)
1631 		return err;
1632 
1633 	return 0;
1634 
1635 err_ipv6:
1636 	ocelot_ipv4_ptp_trap_del(ocelot, port);
1637 err_ipv4:
1638 	if (l2)
1639 		ocelot_l2_ptp_trap_del(ocelot, port);
1640 	return err;
1641 }
1642 
1643 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr)
1644 {
1645 	return copy_to_user(ifr->ifr_data, &ocelot->hwtstamp_config,
1646 			    sizeof(ocelot->hwtstamp_config)) ? -EFAULT : 0;
1647 }
1648 EXPORT_SYMBOL(ocelot_hwstamp_get);
1649 
1650 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr)
1651 {
1652 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1653 	bool l2 = false, l4 = false;
1654 	struct hwtstamp_config cfg;
1655 	int err;
1656 
1657 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1658 		return -EFAULT;
1659 
1660 	/* Tx type sanity check */
1661 	switch (cfg.tx_type) {
1662 	case HWTSTAMP_TX_ON:
1663 		ocelot_port->ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
1664 		break;
1665 	case HWTSTAMP_TX_ONESTEP_SYNC:
1666 		/* IFH_REW_OP_ONE_STEP_PTP updates the correctional field, we
1667 		 * need to update the origin time.
1668 		 */
1669 		ocelot_port->ptp_cmd = IFH_REW_OP_ORIGIN_PTP;
1670 		break;
1671 	case HWTSTAMP_TX_OFF:
1672 		ocelot_port->ptp_cmd = 0;
1673 		break;
1674 	default:
1675 		return -ERANGE;
1676 	}
1677 
1678 	mutex_lock(&ocelot->ptp_lock);
1679 
1680 	switch (cfg.rx_filter) {
1681 	case HWTSTAMP_FILTER_NONE:
1682 		break;
1683 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1684 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1685 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1686 		l4 = true;
1687 		break;
1688 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1689 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1690 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1691 		l2 = true;
1692 		break;
1693 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
1694 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1695 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1696 		l2 = true;
1697 		l4 = true;
1698 		break;
1699 	default:
1700 		mutex_unlock(&ocelot->ptp_lock);
1701 		return -ERANGE;
1702 	}
1703 
1704 	err = ocelot_setup_ptp_traps(ocelot, port, l2, l4);
1705 	if (err) {
1706 		mutex_unlock(&ocelot->ptp_lock);
1707 		return err;
1708 	}
1709 
1710 	if (l2 && l4)
1711 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1712 	else if (l2)
1713 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1714 	else if (l4)
1715 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
1716 	else
1717 		cfg.rx_filter = HWTSTAMP_FILTER_NONE;
1718 
1719 	/* Commit back the result & save it */
1720 	memcpy(&ocelot->hwtstamp_config, &cfg, sizeof(cfg));
1721 	mutex_unlock(&ocelot->ptp_lock);
1722 
1723 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1724 }
1725 EXPORT_SYMBOL(ocelot_hwstamp_set);
1726 
1727 void ocelot_get_strings(struct ocelot *ocelot, int port, u32 sset, u8 *data)
1728 {
1729 	int i;
1730 
1731 	if (sset != ETH_SS_STATS)
1732 		return;
1733 
1734 	for (i = 0; i < ocelot->num_stats; i++)
1735 		memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
1736 		       ETH_GSTRING_LEN);
1737 }
1738 EXPORT_SYMBOL(ocelot_get_strings);
1739 
1740 static void ocelot_update_stats(struct ocelot *ocelot)
1741 {
1742 	int i, j;
1743 
1744 	mutex_lock(&ocelot->stats_lock);
1745 
1746 	for (i = 0; i < ocelot->num_phys_ports; i++) {
1747 		/* Configure the port to read the stats from */
1748 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
1749 
1750 		for (j = 0; j < ocelot->num_stats; j++) {
1751 			u32 val;
1752 			unsigned int idx = i * ocelot->num_stats + j;
1753 
1754 			val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
1755 					      ocelot->stats_layout[j].offset);
1756 
1757 			if (val < (ocelot->stats[idx] & U32_MAX))
1758 				ocelot->stats[idx] += (u64)1 << 32;
1759 
1760 			ocelot->stats[idx] = (ocelot->stats[idx] &
1761 					      ~(u64)U32_MAX) + val;
1762 		}
1763 	}
1764 
1765 	mutex_unlock(&ocelot->stats_lock);
1766 }
1767 
1768 static void ocelot_check_stats_work(struct work_struct *work)
1769 {
1770 	struct delayed_work *del_work = to_delayed_work(work);
1771 	struct ocelot *ocelot = container_of(del_work, struct ocelot,
1772 					     stats_work);
1773 
1774 	ocelot_update_stats(ocelot);
1775 
1776 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1777 			   OCELOT_STATS_CHECK_DELAY);
1778 }
1779 
1780 void ocelot_get_ethtool_stats(struct ocelot *ocelot, int port, u64 *data)
1781 {
1782 	int i;
1783 
1784 	/* check and update now */
1785 	ocelot_update_stats(ocelot);
1786 
1787 	/* Copy all counters */
1788 	for (i = 0; i < ocelot->num_stats; i++)
1789 		*data++ = ocelot->stats[port * ocelot->num_stats + i];
1790 }
1791 EXPORT_SYMBOL(ocelot_get_ethtool_stats);
1792 
1793 int ocelot_get_sset_count(struct ocelot *ocelot, int port, int sset)
1794 {
1795 	if (sset != ETH_SS_STATS)
1796 		return -EOPNOTSUPP;
1797 
1798 	return ocelot->num_stats;
1799 }
1800 EXPORT_SYMBOL(ocelot_get_sset_count);
1801 
1802 int ocelot_get_ts_info(struct ocelot *ocelot, int port,
1803 		       struct ethtool_ts_info *info)
1804 {
1805 	info->phc_index = ocelot->ptp_clock ?
1806 			  ptp_clock_index(ocelot->ptp_clock) : -1;
1807 	if (info->phc_index == -1) {
1808 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1809 					 SOF_TIMESTAMPING_RX_SOFTWARE |
1810 					 SOF_TIMESTAMPING_SOFTWARE;
1811 		return 0;
1812 	}
1813 	info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE |
1814 				 SOF_TIMESTAMPING_RX_SOFTWARE |
1815 				 SOF_TIMESTAMPING_SOFTWARE |
1816 				 SOF_TIMESTAMPING_TX_HARDWARE |
1817 				 SOF_TIMESTAMPING_RX_HARDWARE |
1818 				 SOF_TIMESTAMPING_RAW_HARDWARE;
1819 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON) |
1820 			 BIT(HWTSTAMP_TX_ONESTEP_SYNC);
1821 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
1822 			   BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
1823 			   BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1824 			   BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT);
1825 
1826 	return 0;
1827 }
1828 EXPORT_SYMBOL(ocelot_get_ts_info);
1829 
1830 static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
1831 {
1832 	u32 mask = 0;
1833 	int port;
1834 
1835 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1836 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1837 
1838 		if (!ocelot_port)
1839 			continue;
1840 
1841 		if (ocelot_port->bond == bond)
1842 			mask |= BIT(port);
1843 	}
1844 
1845 	return mask;
1846 }
1847 
1848 u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
1849 {
1850 	struct ocelot_port *ocelot_port = ocelot->ports[src_port];
1851 	const struct net_device *bridge;
1852 	u32 mask = 0;
1853 	int port;
1854 
1855 	if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
1856 		return 0;
1857 
1858 	bridge = ocelot_port->bridge;
1859 	if (!bridge)
1860 		return 0;
1861 
1862 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1863 		ocelot_port = ocelot->ports[port];
1864 
1865 		if (!ocelot_port)
1866 			continue;
1867 
1868 		if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
1869 		    ocelot_port->bridge == bridge)
1870 			mask |= BIT(port);
1871 	}
1872 
1873 	return mask;
1874 }
1875 EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
1876 
1877 u32 ocelot_get_dsa_8021q_cpu_mask(struct ocelot *ocelot)
1878 {
1879 	u32 mask = 0;
1880 	int port;
1881 
1882 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1883 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1884 
1885 		if (!ocelot_port)
1886 			continue;
1887 
1888 		if (ocelot_port->is_dsa_8021q_cpu)
1889 			mask |= BIT(port);
1890 	}
1891 
1892 	return mask;
1893 }
1894 EXPORT_SYMBOL_GPL(ocelot_get_dsa_8021q_cpu_mask);
1895 
1896 void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
1897 {
1898 	unsigned long cpu_fwd_mask;
1899 	int port;
1900 
1901 	lockdep_assert_held(&ocelot->fwd_domain_lock);
1902 
1903 	/* If cut-through forwarding is supported, update the masks before a
1904 	 * port joins the forwarding domain, to avoid potential underruns if it
1905 	 * has the highest speed from the new domain.
1906 	 */
1907 	if (joining && ocelot->ops->cut_through_fwd)
1908 		ocelot->ops->cut_through_fwd(ocelot);
1909 
1910 	/* If a DSA tag_8021q CPU exists, it needs to be included in the
1911 	 * regular forwarding path of the front ports regardless of whether
1912 	 * those are bridged or standalone.
1913 	 * If DSA tag_8021q is not used, this returns 0, which is fine because
1914 	 * the hardware-based CPU port module can be a destination for packets
1915 	 * even if it isn't part of PGID_SRC.
1916 	 */
1917 	cpu_fwd_mask = ocelot_get_dsa_8021q_cpu_mask(ocelot);
1918 
1919 	/* Apply FWD mask. The loop is needed to add/remove the current port as
1920 	 * a source for the other ports.
1921 	 */
1922 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1923 		struct ocelot_port *ocelot_port = ocelot->ports[port];
1924 		unsigned long mask;
1925 
1926 		if (!ocelot_port) {
1927 			/* Unused ports can't send anywhere */
1928 			mask = 0;
1929 		} else if (ocelot_port->is_dsa_8021q_cpu) {
1930 			/* The DSA tag_8021q CPU ports need to be able to
1931 			 * forward packets to all other ports except for
1932 			 * themselves
1933 			 */
1934 			mask = GENMASK(ocelot->num_phys_ports - 1, 0);
1935 			mask &= ~cpu_fwd_mask;
1936 		} else if (ocelot_port->bridge) {
1937 			struct net_device *bond = ocelot_port->bond;
1938 
1939 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
1940 			mask |= cpu_fwd_mask;
1941 			mask &= ~BIT(port);
1942 			if (bond)
1943 				mask &= ~ocelot_get_bond_mask(ocelot, bond);
1944 		} else {
1945 			/* Standalone ports forward only to DSA tag_8021q CPU
1946 			 * ports (if those exist), or to the hardware CPU port
1947 			 * module otherwise.
1948 			 */
1949 			mask = cpu_fwd_mask;
1950 		}
1951 
1952 		ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
1953 	}
1954 
1955 	/* If cut-through forwarding is supported and a port is leaving, there
1956 	 * is a chance that cut-through was disabled on the other ports due to
1957 	 * the port which is leaving (it has a higher link speed). We need to
1958 	 * update the cut-through masks of the remaining ports no earlier than
1959 	 * after the port has left, to prevent underruns from happening between
1960 	 * the cut-through update and the forwarding domain update.
1961 	 */
1962 	if (!joining && ocelot->ops->cut_through_fwd)
1963 		ocelot->ops->cut_through_fwd(ocelot);
1964 }
1965 EXPORT_SYMBOL(ocelot_apply_bridge_fwd_mask);
1966 
1967 void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
1968 {
1969 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1970 	u32 learn_ena = 0;
1971 
1972 	mutex_lock(&ocelot->fwd_domain_lock);
1973 
1974 	ocelot_port->stp_state = state;
1975 
1976 	if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
1977 	    ocelot_port->learn_ena)
1978 		learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
1979 
1980 	ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
1981 		       ANA_PORT_PORT_CFG, port);
1982 
1983 	ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
1984 
1985 	mutex_unlock(&ocelot->fwd_domain_lock);
1986 }
1987 EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
1988 
1989 void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
1990 {
1991 	unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
1992 
1993 	/* Setting AGE_PERIOD to zero effectively disables automatic aging,
1994 	 * which is clearly not what our intention is. So avoid that.
1995 	 */
1996 	if (!age_period)
1997 		age_period = 1;
1998 
1999 	ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
2000 }
2001 EXPORT_SYMBOL(ocelot_set_ageing_time);
2002 
2003 static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
2004 						     const unsigned char *addr,
2005 						     u16 vid)
2006 {
2007 	struct ocelot_multicast *mc;
2008 
2009 	list_for_each_entry(mc, &ocelot->multicast, list) {
2010 		if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
2011 			return mc;
2012 	}
2013 
2014 	return NULL;
2015 }
2016 
2017 static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
2018 {
2019 	if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
2020 		return ENTRYTYPE_MACv4;
2021 	if (addr[0] == 0x33 && addr[1] == 0x33)
2022 		return ENTRYTYPE_MACv6;
2023 	return ENTRYTYPE_LOCKED;
2024 }
2025 
2026 static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
2027 					     unsigned long ports)
2028 {
2029 	struct ocelot_pgid *pgid;
2030 
2031 	pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
2032 	if (!pgid)
2033 		return ERR_PTR(-ENOMEM);
2034 
2035 	pgid->ports = ports;
2036 	pgid->index = index;
2037 	refcount_set(&pgid->refcount, 1);
2038 	list_add_tail(&pgid->list, &ocelot->pgids);
2039 
2040 	return pgid;
2041 }
2042 
2043 static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
2044 {
2045 	if (!refcount_dec_and_test(&pgid->refcount))
2046 		return;
2047 
2048 	list_del(&pgid->list);
2049 	kfree(pgid);
2050 }
2051 
2052 static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
2053 					       const struct ocelot_multicast *mc)
2054 {
2055 	struct ocelot_pgid *pgid;
2056 	int index;
2057 
2058 	/* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
2059 	 * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
2060 	 * destination mask table (PGID), the destination set is programmed as
2061 	 * part of the entry MAC address.", and the DEST_IDX is set to 0.
2062 	 */
2063 	if (mc->entry_type == ENTRYTYPE_MACv4 ||
2064 	    mc->entry_type == ENTRYTYPE_MACv6)
2065 		return ocelot_pgid_alloc(ocelot, 0, mc->ports);
2066 
2067 	list_for_each_entry(pgid, &ocelot->pgids, list) {
2068 		/* When searching for a nonreserved multicast PGID, ignore the
2069 		 * dummy PGID of zero that we have for MACv4/MACv6 entries
2070 		 */
2071 		if (pgid->index && pgid->ports == mc->ports) {
2072 			refcount_inc(&pgid->refcount);
2073 			return pgid;
2074 		}
2075 	}
2076 
2077 	/* Search for a free index in the nonreserved multicast PGID area */
2078 	for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
2079 		bool used = false;
2080 
2081 		list_for_each_entry(pgid, &ocelot->pgids, list) {
2082 			if (pgid->index == index) {
2083 				used = true;
2084 				break;
2085 			}
2086 		}
2087 
2088 		if (!used)
2089 			return ocelot_pgid_alloc(ocelot, index, mc->ports);
2090 	}
2091 
2092 	return ERR_PTR(-ENOSPC);
2093 }
2094 
2095 static void ocelot_encode_ports_to_mdb(unsigned char *addr,
2096 				       struct ocelot_multicast *mc)
2097 {
2098 	ether_addr_copy(addr, mc->addr);
2099 
2100 	if (mc->entry_type == ENTRYTYPE_MACv4) {
2101 		addr[0] = 0;
2102 		addr[1] = mc->ports >> 8;
2103 		addr[2] = mc->ports & 0xff;
2104 	} else if (mc->entry_type == ENTRYTYPE_MACv6) {
2105 		addr[0] = mc->ports >> 8;
2106 		addr[1] = mc->ports & 0xff;
2107 	}
2108 }
2109 
2110 int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
2111 			const struct switchdev_obj_port_mdb *mdb)
2112 {
2113 	unsigned char addr[ETH_ALEN];
2114 	struct ocelot_multicast *mc;
2115 	struct ocelot_pgid *pgid;
2116 	u16 vid = mdb->vid;
2117 
2118 	if (port == ocelot->npi)
2119 		port = ocelot->num_phys_ports;
2120 
2121 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2122 	if (!mc) {
2123 		/* New entry */
2124 		mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
2125 		if (!mc)
2126 			return -ENOMEM;
2127 
2128 		mc->entry_type = ocelot_classify_mdb(mdb->addr);
2129 		ether_addr_copy(mc->addr, mdb->addr);
2130 		mc->vid = vid;
2131 
2132 		list_add_tail(&mc->list, &ocelot->multicast);
2133 	} else {
2134 		/* Existing entry. Clean up the current port mask from
2135 		 * hardware now, because we'll be modifying it.
2136 		 */
2137 		ocelot_pgid_free(ocelot, mc->pgid);
2138 		ocelot_encode_ports_to_mdb(addr, mc);
2139 		ocelot_mact_forget(ocelot, addr, vid);
2140 	}
2141 
2142 	mc->ports |= BIT(port);
2143 
2144 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
2145 	if (IS_ERR(pgid)) {
2146 		dev_err(ocelot->dev,
2147 			"Cannot allocate PGID for mdb %pM vid %d\n",
2148 			mc->addr, mc->vid);
2149 		devm_kfree(ocelot->dev, mc);
2150 		return PTR_ERR(pgid);
2151 	}
2152 	mc->pgid = pgid;
2153 
2154 	ocelot_encode_ports_to_mdb(addr, mc);
2155 
2156 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
2157 	    mc->entry_type != ENTRYTYPE_MACv6)
2158 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2159 				 pgid->index);
2160 
2161 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
2162 				 mc->entry_type);
2163 }
2164 EXPORT_SYMBOL(ocelot_port_mdb_add);
2165 
2166 int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
2167 			const struct switchdev_obj_port_mdb *mdb)
2168 {
2169 	unsigned char addr[ETH_ALEN];
2170 	struct ocelot_multicast *mc;
2171 	struct ocelot_pgid *pgid;
2172 	u16 vid = mdb->vid;
2173 
2174 	if (port == ocelot->npi)
2175 		port = ocelot->num_phys_ports;
2176 
2177 	mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
2178 	if (!mc)
2179 		return -ENOENT;
2180 
2181 	ocelot_encode_ports_to_mdb(addr, mc);
2182 	ocelot_mact_forget(ocelot, addr, vid);
2183 
2184 	ocelot_pgid_free(ocelot, mc->pgid);
2185 	mc->ports &= ~BIT(port);
2186 	if (!mc->ports) {
2187 		list_del(&mc->list);
2188 		devm_kfree(ocelot->dev, mc);
2189 		return 0;
2190 	}
2191 
2192 	/* We have a PGID with fewer ports now */
2193 	pgid = ocelot_mdb_get_pgid(ocelot, mc);
2194 	if (IS_ERR(pgid))
2195 		return PTR_ERR(pgid);
2196 	mc->pgid = pgid;
2197 
2198 	ocelot_encode_ports_to_mdb(addr, mc);
2199 
2200 	if (mc->entry_type != ENTRYTYPE_MACv4 &&
2201 	    mc->entry_type != ENTRYTYPE_MACv6)
2202 		ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
2203 				 pgid->index);
2204 
2205 	return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
2206 				 mc->entry_type);
2207 }
2208 EXPORT_SYMBOL(ocelot_port_mdb_del);
2209 
2210 void ocelot_port_bridge_join(struct ocelot *ocelot, int port,
2211 			     struct net_device *bridge)
2212 {
2213 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2214 
2215 	mutex_lock(&ocelot->fwd_domain_lock);
2216 
2217 	ocelot_port->bridge = bridge;
2218 
2219 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2220 
2221 	mutex_unlock(&ocelot->fwd_domain_lock);
2222 }
2223 EXPORT_SYMBOL(ocelot_port_bridge_join);
2224 
2225 void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
2226 			      struct net_device *bridge)
2227 {
2228 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2229 
2230 	mutex_lock(&ocelot->fwd_domain_lock);
2231 
2232 	ocelot_port->bridge = NULL;
2233 
2234 	ocelot_port_set_pvid(ocelot, port, NULL);
2235 	ocelot_port_manage_port_tag(ocelot, port);
2236 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2237 
2238 	mutex_unlock(&ocelot->fwd_domain_lock);
2239 }
2240 EXPORT_SYMBOL(ocelot_port_bridge_leave);
2241 
2242 static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
2243 {
2244 	unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
2245 	int i, port, lag;
2246 
2247 	/* Reset destination and aggregation PGIDS */
2248 	for_each_unicast_dest_pgid(ocelot, port)
2249 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2250 
2251 	for_each_aggr_pgid(ocelot, i)
2252 		ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
2253 				 ANA_PGID_PGID, i);
2254 
2255 	/* The visited ports bitmask holds the list of ports offloading any
2256 	 * bonding interface. Initially we mark all these ports as unvisited,
2257 	 * then every time we visit a port in this bitmask, we know that it is
2258 	 * the lowest numbered port, i.e. the one whose logical ID == physical
2259 	 * port ID == LAG ID. So we mark as visited all further ports in the
2260 	 * bitmask that are offloading the same bonding interface. This way,
2261 	 * we set up the aggregation PGIDs only once per bonding interface.
2262 	 */
2263 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2264 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2265 
2266 		if (!ocelot_port || !ocelot_port->bond)
2267 			continue;
2268 
2269 		visited &= ~BIT(port);
2270 	}
2271 
2272 	/* Now, set PGIDs for each active LAG */
2273 	for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
2274 		struct net_device *bond = ocelot->ports[lag]->bond;
2275 		int num_active_ports = 0;
2276 		unsigned long bond_mask;
2277 		u8 aggr_idx[16];
2278 
2279 		if (!bond || (visited & BIT(lag)))
2280 			continue;
2281 
2282 		bond_mask = ocelot_get_bond_mask(ocelot, bond);
2283 
2284 		for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
2285 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2286 
2287 			// Destination mask
2288 			ocelot_write_rix(ocelot, bond_mask,
2289 					 ANA_PGID_PGID, port);
2290 
2291 			if (ocelot_port->lag_tx_active)
2292 				aggr_idx[num_active_ports++] = port;
2293 		}
2294 
2295 		for_each_aggr_pgid(ocelot, i) {
2296 			u32 ac;
2297 
2298 			ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
2299 			ac &= ~bond_mask;
2300 			/* Don't do division by zero if there was no active
2301 			 * port. Just make all aggregation codes zero.
2302 			 */
2303 			if (num_active_ports)
2304 				ac |= BIT(aggr_idx[i % num_active_ports]);
2305 			ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
2306 		}
2307 
2308 		/* Mark all ports in the same LAG as visited to avoid applying
2309 		 * the same config again.
2310 		 */
2311 		for (port = lag; port < ocelot->num_phys_ports; port++) {
2312 			struct ocelot_port *ocelot_port = ocelot->ports[port];
2313 
2314 			if (!ocelot_port)
2315 				continue;
2316 
2317 			if (ocelot_port->bond == bond)
2318 				visited |= BIT(port);
2319 		}
2320 	}
2321 }
2322 
2323 /* When offloading a bonding interface, the switch ports configured under the
2324  * same bond must have the same logical port ID, equal to the physical port ID
2325  * of the lowest numbered physical port in that bond. Otherwise, in standalone/
2326  * bridged mode, each port has a logical port ID equal to its physical port ID.
2327  */
2328 static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
2329 {
2330 	int port;
2331 
2332 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2333 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2334 		struct net_device *bond;
2335 
2336 		if (!ocelot_port)
2337 			continue;
2338 
2339 		bond = ocelot_port->bond;
2340 		if (bond) {
2341 			int lag = __ffs(ocelot_get_bond_mask(ocelot, bond));
2342 
2343 			ocelot_rmw_gix(ocelot,
2344 				       ANA_PORT_PORT_CFG_PORTID_VAL(lag),
2345 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
2346 				       ANA_PORT_PORT_CFG, port);
2347 		} else {
2348 			ocelot_rmw_gix(ocelot,
2349 				       ANA_PORT_PORT_CFG_PORTID_VAL(port),
2350 				       ANA_PORT_PORT_CFG_PORTID_VAL_M,
2351 				       ANA_PORT_PORT_CFG, port);
2352 		}
2353 	}
2354 }
2355 
2356 int ocelot_port_lag_join(struct ocelot *ocelot, int port,
2357 			 struct net_device *bond,
2358 			 struct netdev_lag_upper_info *info)
2359 {
2360 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
2361 		return -EOPNOTSUPP;
2362 
2363 	mutex_lock(&ocelot->fwd_domain_lock);
2364 
2365 	ocelot->ports[port]->bond = bond;
2366 
2367 	ocelot_setup_logical_port_ids(ocelot);
2368 	ocelot_apply_bridge_fwd_mask(ocelot, true);
2369 	ocelot_set_aggr_pgids(ocelot);
2370 
2371 	mutex_unlock(&ocelot->fwd_domain_lock);
2372 
2373 	return 0;
2374 }
2375 EXPORT_SYMBOL(ocelot_port_lag_join);
2376 
2377 void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
2378 			   struct net_device *bond)
2379 {
2380 	mutex_lock(&ocelot->fwd_domain_lock);
2381 
2382 	ocelot->ports[port]->bond = NULL;
2383 
2384 	ocelot_setup_logical_port_ids(ocelot);
2385 	ocelot_apply_bridge_fwd_mask(ocelot, false);
2386 	ocelot_set_aggr_pgids(ocelot);
2387 
2388 	mutex_unlock(&ocelot->fwd_domain_lock);
2389 }
2390 EXPORT_SYMBOL(ocelot_port_lag_leave);
2391 
2392 void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
2393 {
2394 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2395 
2396 	ocelot_port->lag_tx_active = lag_tx_active;
2397 
2398 	/* Rebalance the LAGs */
2399 	ocelot_set_aggr_pgids(ocelot);
2400 }
2401 EXPORT_SYMBOL(ocelot_port_lag_change);
2402 
2403 /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
2404  * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
2405  * In the special case that it's the NPI port that we're configuring, the
2406  * length of the tag and optional prefix needs to be accounted for privately,
2407  * in order to be able to sustain communication at the requested @sdu.
2408  */
2409 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
2410 {
2411 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2412 	int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
2413 	int pause_start, pause_stop;
2414 	int atop, atop_tot;
2415 
2416 	if (port == ocelot->npi) {
2417 		maxlen += OCELOT_TAG_LEN;
2418 
2419 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
2420 			maxlen += OCELOT_SHORT_PREFIX_LEN;
2421 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
2422 			maxlen += OCELOT_LONG_PREFIX_LEN;
2423 	}
2424 
2425 	ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
2426 
2427 	/* Set Pause watermark hysteresis */
2428 	pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
2429 	pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
2430 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
2431 			    pause_start);
2432 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
2433 			    pause_stop);
2434 
2435 	/* Tail dropping watermarks */
2436 	atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
2437 		   OCELOT_BUFFER_CELL_SZ;
2438 	atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
2439 	ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
2440 	ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
2441 }
2442 EXPORT_SYMBOL(ocelot_port_set_maxlen);
2443 
2444 int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
2445 {
2446 	int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
2447 
2448 	if (port == ocelot->npi) {
2449 		max_mtu -= OCELOT_TAG_LEN;
2450 
2451 		if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
2452 			max_mtu -= OCELOT_SHORT_PREFIX_LEN;
2453 		else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
2454 			max_mtu -= OCELOT_LONG_PREFIX_LEN;
2455 	}
2456 
2457 	return max_mtu;
2458 }
2459 EXPORT_SYMBOL(ocelot_get_max_mtu);
2460 
2461 static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
2462 				     bool enabled)
2463 {
2464 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2465 	u32 val = 0;
2466 
2467 	if (enabled)
2468 		val = ANA_PORT_PORT_CFG_LEARN_ENA;
2469 
2470 	ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
2471 		       ANA_PORT_PORT_CFG, port);
2472 
2473 	ocelot_port->learn_ena = enabled;
2474 }
2475 
2476 static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
2477 					bool enabled)
2478 {
2479 	u32 val = 0;
2480 
2481 	if (enabled)
2482 		val = BIT(port);
2483 
2484 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
2485 }
2486 
2487 static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
2488 					bool enabled)
2489 {
2490 	u32 val = 0;
2491 
2492 	if (enabled)
2493 		val = BIT(port);
2494 
2495 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
2496 }
2497 
2498 static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
2499 					bool enabled)
2500 {
2501 	u32 val = 0;
2502 
2503 	if (enabled)
2504 		val = BIT(port);
2505 
2506 	ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
2507 }
2508 
2509 int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
2510 				 struct switchdev_brport_flags flags)
2511 {
2512 	if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
2513 			   BR_BCAST_FLOOD))
2514 		return -EINVAL;
2515 
2516 	return 0;
2517 }
2518 EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
2519 
2520 void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
2521 			      struct switchdev_brport_flags flags)
2522 {
2523 	if (flags.mask & BR_LEARNING)
2524 		ocelot_port_set_learning(ocelot, port,
2525 					 !!(flags.val & BR_LEARNING));
2526 
2527 	if (flags.mask & BR_FLOOD)
2528 		ocelot_port_set_ucast_flood(ocelot, port,
2529 					    !!(flags.val & BR_FLOOD));
2530 
2531 	if (flags.mask & BR_MCAST_FLOOD)
2532 		ocelot_port_set_mcast_flood(ocelot, port,
2533 					    !!(flags.val & BR_MCAST_FLOOD));
2534 
2535 	if (flags.mask & BR_BCAST_FLOOD)
2536 		ocelot_port_set_bcast_flood(ocelot, port,
2537 					    !!(flags.val & BR_BCAST_FLOOD));
2538 }
2539 EXPORT_SYMBOL(ocelot_port_bridge_flags);
2540 
2541 void ocelot_init_port(struct ocelot *ocelot, int port)
2542 {
2543 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2544 
2545 	skb_queue_head_init(&ocelot_port->tx_skbs);
2546 
2547 	/* Basic L2 initialization */
2548 
2549 	/* Set MAC IFG Gaps
2550 	 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
2551 	 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
2552 	 */
2553 	ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
2554 			   DEV_MAC_IFG_CFG);
2555 
2556 	/* Load seed (0) and set MAC HDX late collision  */
2557 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
2558 			   DEV_MAC_HDX_CFG_SEED_LOAD,
2559 			   DEV_MAC_HDX_CFG);
2560 	mdelay(1);
2561 	ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
2562 			   DEV_MAC_HDX_CFG);
2563 
2564 	/* Set Max Length and maximum tags allowed */
2565 	ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
2566 	ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
2567 			   DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
2568 			   DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
2569 			   DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
2570 			   DEV_MAC_TAGS_CFG);
2571 
2572 	/* Set SMAC of Pause frame (00:00:00:00:00:00) */
2573 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
2574 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
2575 
2576 	/* Enable transmission of pause frames */
2577 	ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
2578 
2579 	/* Drop frames with multicast source address */
2580 	ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2581 		       ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
2582 		       ANA_PORT_DROP_CFG, port);
2583 
2584 	/* Set default VLAN and tag type to 8021Q. */
2585 	ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
2586 		       REW_PORT_VLAN_CFG_PORT_TPID_M,
2587 		       REW_PORT_VLAN_CFG, port);
2588 
2589 	/* Disable source address learning for standalone mode */
2590 	ocelot_port_set_learning(ocelot, port, false);
2591 
2592 	/* Set the port's initial logical port ID value, enable receiving
2593 	 * frames on it, and configure the MAC address learning type to
2594 	 * automatic.
2595 	 */
2596 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
2597 			 ANA_PORT_PORT_CFG_RECV_ENA |
2598 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
2599 			 ANA_PORT_PORT_CFG, port);
2600 
2601 	/* Enable vcap lookups */
2602 	ocelot_vcap_enable(ocelot, port);
2603 }
2604 EXPORT_SYMBOL(ocelot_init_port);
2605 
2606 /* Configure and enable the CPU port module, which is a set of queues
2607  * accessible through register MMIO, frame DMA or Ethernet (in case
2608  * NPI mode is used).
2609  */
2610 static void ocelot_cpu_port_init(struct ocelot *ocelot)
2611 {
2612 	int cpu = ocelot->num_phys_ports;
2613 
2614 	/* The unicast destination PGID for the CPU port module is unused */
2615 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
2616 	/* Instead set up a multicast destination PGID for traffic copied to
2617 	 * the CPU. Whitelisted MAC addresses like the port netdevice MAC
2618 	 * addresses will be copied to the CPU via this PGID.
2619 	 */
2620 	ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
2621 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
2622 			 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
2623 			 ANA_PORT_PORT_CFG, cpu);
2624 
2625 	/* Enable CPU port module */
2626 	ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
2627 	/* CPU port Injection/Extraction configuration */
2628 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
2629 			    OCELOT_TAG_PREFIX_NONE);
2630 	ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
2631 			    OCELOT_TAG_PREFIX_NONE);
2632 
2633 	/* Configure the CPU port to be VLAN aware */
2634 	ocelot_write_gix(ocelot,
2635 			 ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_VLAN_UNAWARE_PVID) |
2636 			 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
2637 			 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
2638 			 ANA_PORT_VLAN_CFG, cpu);
2639 }
2640 
2641 static void ocelot_detect_features(struct ocelot *ocelot)
2642 {
2643 	int mmgt, eq_ctrl;
2644 
2645 	/* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
2646 	 * the number of 240-byte free memory words (aka 4-cell chunks) and not
2647 	 * 192 bytes as the documentation incorrectly says.
2648 	 */
2649 	mmgt = ocelot_read(ocelot, SYS_MMGT);
2650 	ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
2651 
2652 	eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
2653 	ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
2654 }
2655 
2656 int ocelot_init(struct ocelot *ocelot)
2657 {
2658 	char queue_name[32];
2659 	int i, ret;
2660 	u32 port;
2661 
2662 	if (ocelot->ops->reset) {
2663 		ret = ocelot->ops->reset(ocelot);
2664 		if (ret) {
2665 			dev_err(ocelot->dev, "Switch reset failed\n");
2666 			return ret;
2667 		}
2668 	}
2669 
2670 	ocelot->stats = devm_kcalloc(ocelot->dev,
2671 				     ocelot->num_phys_ports * ocelot->num_stats,
2672 				     sizeof(u64), GFP_KERNEL);
2673 	if (!ocelot->stats)
2674 		return -ENOMEM;
2675 
2676 	mutex_init(&ocelot->stats_lock);
2677 	mutex_init(&ocelot->ptp_lock);
2678 	mutex_init(&ocelot->mact_lock);
2679 	mutex_init(&ocelot->fwd_domain_lock);
2680 	spin_lock_init(&ocelot->ptp_clock_lock);
2681 	spin_lock_init(&ocelot->ts_id_lock);
2682 	snprintf(queue_name, sizeof(queue_name), "%s-stats",
2683 		 dev_name(ocelot->dev));
2684 	ocelot->stats_queue = create_singlethread_workqueue(queue_name);
2685 	if (!ocelot->stats_queue)
2686 		return -ENOMEM;
2687 
2688 	ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
2689 	if (!ocelot->owq) {
2690 		destroy_workqueue(ocelot->stats_queue);
2691 		return -ENOMEM;
2692 	}
2693 
2694 	INIT_LIST_HEAD(&ocelot->multicast);
2695 	INIT_LIST_HEAD(&ocelot->pgids);
2696 	INIT_LIST_HEAD(&ocelot->vlans);
2697 	ocelot_detect_features(ocelot);
2698 	ocelot_mact_init(ocelot);
2699 	ocelot_vlan_init(ocelot);
2700 	ocelot_vcap_init(ocelot);
2701 	ocelot_cpu_port_init(ocelot);
2702 
2703 	if (ocelot->ops->psfp_init)
2704 		ocelot->ops->psfp_init(ocelot);
2705 
2706 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2707 		/* Clear all counters (5 groups) */
2708 		ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
2709 				     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
2710 			     SYS_STAT_CFG);
2711 	}
2712 
2713 	/* Only use S-Tag */
2714 	ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
2715 
2716 	/* Aggregation mode */
2717 	ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
2718 			     ANA_AGGR_CFG_AC_DMAC_ENA |
2719 			     ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
2720 			     ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
2721 			     ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
2722 			     ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
2723 			     ANA_AGGR_CFG);
2724 
2725 	/* Set MAC age time to default value. The entry is aged after
2726 	 * 2*AGE_PERIOD
2727 	 */
2728 	ocelot_write(ocelot,
2729 		     ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
2730 		     ANA_AUTOAGE);
2731 
2732 	/* Disable learning for frames discarded by VLAN ingress filtering */
2733 	regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
2734 
2735 	/* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
2736 	ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
2737 		     SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
2738 
2739 	/* Setup flooding PGIDs */
2740 	for (i = 0; i < ocelot->num_flooding_pgids; i++)
2741 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
2742 				 ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
2743 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
2744 				 ANA_FLOODING, i);
2745 	ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
2746 		     ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
2747 		     ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
2748 		     ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
2749 		     ANA_FLOODING_IPMC);
2750 
2751 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2752 		/* Transmit the frame to the local port. */
2753 		ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
2754 		/* Do not forward BPDU frames to the front ports. */
2755 		ocelot_write_gix(ocelot,
2756 				 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
2757 				 ANA_PORT_CPU_FWD_BPDU_CFG,
2758 				 port);
2759 		/* Ensure bridging is disabled */
2760 		ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
2761 	}
2762 
2763 	for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
2764 		u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
2765 
2766 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
2767 	}
2768 
2769 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
2770 
2771 	/* Allow broadcast and unknown L2 multicast to the CPU. */
2772 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2773 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2774 		       ANA_PGID_PGID, PGID_MC);
2775 	ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2776 		       ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
2777 		       ANA_PGID_PGID, PGID_BC);
2778 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
2779 	ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
2780 
2781 	/* Allow manual injection via DEVCPU_QS registers, and byte swap these
2782 	 * registers endianness.
2783 	 */
2784 	ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
2785 			 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
2786 	ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
2787 			 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
2788 	ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
2789 		     ANA_CPUQ_CFG_CPUQ_LRN(2) |
2790 		     ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
2791 		     ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
2792 		     ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
2793 		     ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
2794 		     ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
2795 		     ANA_CPUQ_CFG_CPUQ_IGMP(6) |
2796 		     ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
2797 	for (i = 0; i < 16; i++)
2798 		ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
2799 				 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
2800 				 ANA_CPUQ_8021_CFG, i);
2801 
2802 	INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats_work);
2803 	queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
2804 			   OCELOT_STATS_CHECK_DELAY);
2805 
2806 	return 0;
2807 }
2808 EXPORT_SYMBOL(ocelot_init);
2809 
2810 void ocelot_deinit(struct ocelot *ocelot)
2811 {
2812 	cancel_delayed_work(&ocelot->stats_work);
2813 	destroy_workqueue(ocelot->stats_queue);
2814 	destroy_workqueue(ocelot->owq);
2815 	mutex_destroy(&ocelot->stats_lock);
2816 }
2817 EXPORT_SYMBOL(ocelot_deinit);
2818 
2819 void ocelot_deinit_port(struct ocelot *ocelot, int port)
2820 {
2821 	struct ocelot_port *ocelot_port = ocelot->ports[port];
2822 
2823 	skb_queue_purge(&ocelot_port->tx_skbs);
2824 }
2825 EXPORT_SYMBOL(ocelot_deinit_port);
2826 
2827 MODULE_LICENSE("Dual MIT/GPL");
2828