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