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