1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/module.h>
4 #include <linux/if_bridge.h>
5 #include <linux/if_vlan.h>
6 #include <linux/iopoll.h>
7 #include <linux/ip.h>
8 #include <linux/of_platform.h>
9 #include <linux/of_net.h>
10 #include <linux/packing.h>
11 #include <linux/phy/phy.h>
12 #include <linux/reset.h>
13 #include <net/addrconf.h>
14 
15 #include "lan966x_main.h"
16 
17 #define XTR_EOF_0			0x00000080U
18 #define XTR_EOF_1			0x01000080U
19 #define XTR_EOF_2			0x02000080U
20 #define XTR_EOF_3			0x03000080U
21 #define XTR_PRUNED			0x04000080U
22 #define XTR_ABORT			0x05000080U
23 #define XTR_ESCAPE			0x06000080U
24 #define XTR_NOT_READY			0x07000080U
25 #define XTR_VALID_BYTES(x)		(4 - (((x) >> 24) & 3))
26 
27 #define IO_RANGES 2
28 
29 static const struct of_device_id lan966x_match[] = {
30 	{ .compatible = "microchip,lan966x-switch" },
31 	{ }
32 };
33 MODULE_DEVICE_TABLE(of, lan966x_match);
34 
35 struct lan966x_main_io_resource {
36 	enum lan966x_target id;
37 	phys_addr_t offset;
38 	int range;
39 };
40 
41 static const struct lan966x_main_io_resource lan966x_main_iomap[] =  {
42 	{ TARGET_CPU,                   0xc0000, 0 }, /* 0xe00c0000 */
43 	{ TARGET_FDMA,                  0xc0400, 0 }, /* 0xe00c0400 */
44 	{ TARGET_ORG,                         0, 1 }, /* 0xe2000000 */
45 	{ TARGET_GCB,                    0x4000, 1 }, /* 0xe2004000 */
46 	{ TARGET_QS,                     0x8000, 1 }, /* 0xe2008000 */
47 	{ TARGET_PTP,                    0xc000, 1 }, /* 0xe200c000 */
48 	{ TARGET_CHIP_TOP,              0x10000, 1 }, /* 0xe2010000 */
49 	{ TARGET_REW,                   0x14000, 1 }, /* 0xe2014000 */
50 	{ TARGET_SYS,                   0x28000, 1 }, /* 0xe2028000 */
51 	{ TARGET_DEV,                   0x34000, 1 }, /* 0xe2034000 */
52 	{ TARGET_DEV +  1,              0x38000, 1 }, /* 0xe2038000 */
53 	{ TARGET_DEV +  2,              0x3c000, 1 }, /* 0xe203c000 */
54 	{ TARGET_DEV +  3,              0x40000, 1 }, /* 0xe2040000 */
55 	{ TARGET_DEV +  4,              0x44000, 1 }, /* 0xe2044000 */
56 	{ TARGET_DEV +  5,              0x48000, 1 }, /* 0xe2048000 */
57 	{ TARGET_DEV +  6,              0x4c000, 1 }, /* 0xe204c000 */
58 	{ TARGET_DEV +  7,              0x50000, 1 }, /* 0xe2050000 */
59 	{ TARGET_QSYS,                 0x100000, 1 }, /* 0xe2100000 */
60 	{ TARGET_AFI,                  0x120000, 1 }, /* 0xe2120000 */
61 	{ TARGET_ANA,                  0x140000, 1 }, /* 0xe2140000 */
62 };
63 
lan966x_create_targets(struct platform_device * pdev,struct lan966x * lan966x)64 static int lan966x_create_targets(struct platform_device *pdev,
65 				  struct lan966x *lan966x)
66 {
67 	struct resource *iores[IO_RANGES];
68 	void __iomem *begin[IO_RANGES];
69 	int idx;
70 
71 	/* Initially map the entire range and after that update each target to
72 	 * point inside the region at the correct offset. It is possible that
73 	 * other devices access the same region so don't add any checks about
74 	 * this.
75 	 */
76 	for (idx = 0; idx < IO_RANGES; idx++) {
77 		iores[idx] = platform_get_resource(pdev, IORESOURCE_MEM,
78 						   idx);
79 		if (!iores[idx]) {
80 			dev_err(&pdev->dev, "Invalid resource\n");
81 			return -EINVAL;
82 		}
83 
84 		begin[idx] = devm_ioremap(&pdev->dev,
85 					  iores[idx]->start,
86 					  resource_size(iores[idx]));
87 		if (!begin[idx]) {
88 			dev_err(&pdev->dev, "Unable to get registers: %s\n",
89 				iores[idx]->name);
90 			return -ENOMEM;
91 		}
92 	}
93 
94 	for (idx = 0; idx < ARRAY_SIZE(lan966x_main_iomap); idx++) {
95 		const struct lan966x_main_io_resource *iomap =
96 			&lan966x_main_iomap[idx];
97 
98 		lan966x->regs[iomap->id] = begin[iomap->range] + iomap->offset;
99 	}
100 
101 	return 0;
102 }
103 
lan966x_port_unique_address(struct net_device * dev)104 static bool lan966x_port_unique_address(struct net_device *dev)
105 {
106 	struct lan966x_port *port = netdev_priv(dev);
107 	struct lan966x *lan966x = port->lan966x;
108 	int p;
109 
110 	for (p = 0; p < lan966x->num_phys_ports; ++p) {
111 		port = lan966x->ports[p];
112 		if (!port || port->dev == dev)
113 			continue;
114 
115 		if (ether_addr_equal(dev->dev_addr, port->dev->dev_addr))
116 			return false;
117 	}
118 
119 	return true;
120 }
121 
lan966x_port_set_mac_address(struct net_device * dev,void * p)122 static int lan966x_port_set_mac_address(struct net_device *dev, void *p)
123 {
124 	struct lan966x_port *port = netdev_priv(dev);
125 	struct lan966x *lan966x = port->lan966x;
126 	const struct sockaddr *addr = p;
127 	int ret;
128 
129 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
130 		return 0;
131 
132 	/* Learn the new net device MAC address in the mac table. */
133 	ret = lan966x_mac_cpu_learn(lan966x, addr->sa_data, HOST_PVID);
134 	if (ret)
135 		return ret;
136 
137 	/* If there is another port with the same address as the dev, then don't
138 	 * delete it from the MAC table
139 	 */
140 	if (!lan966x_port_unique_address(dev))
141 		goto out;
142 
143 	/* Then forget the previous one. */
144 	ret = lan966x_mac_cpu_forget(lan966x, dev->dev_addr, HOST_PVID);
145 	if (ret)
146 		return ret;
147 
148 out:
149 	eth_hw_addr_set(dev, addr->sa_data);
150 	return ret;
151 }
152 
lan966x_port_get_phys_port_name(struct net_device * dev,char * buf,size_t len)153 static int lan966x_port_get_phys_port_name(struct net_device *dev,
154 					   char *buf, size_t len)
155 {
156 	struct lan966x_port *port = netdev_priv(dev);
157 	int ret;
158 
159 	ret = snprintf(buf, len, "p%d", port->chip_port);
160 	if (ret >= len)
161 		return -EINVAL;
162 
163 	return 0;
164 }
165 
lan966x_port_open(struct net_device * dev)166 static int lan966x_port_open(struct net_device *dev)
167 {
168 	struct lan966x_port *port = netdev_priv(dev);
169 	struct lan966x *lan966x = port->lan966x;
170 	int err;
171 
172 	/* Enable receiving frames on the port, and activate auto-learning of
173 	 * MAC addresses.
174 	 */
175 	lan_rmw(ANA_PORT_CFG_LEARNAUTO_SET(1) |
176 		ANA_PORT_CFG_RECV_ENA_SET(1) |
177 		ANA_PORT_CFG_PORTID_VAL_SET(port->chip_port),
178 		ANA_PORT_CFG_LEARNAUTO |
179 		ANA_PORT_CFG_RECV_ENA |
180 		ANA_PORT_CFG_PORTID_VAL,
181 		lan966x, ANA_PORT_CFG(port->chip_port));
182 
183 	err = phylink_fwnode_phy_connect(port->phylink, port->fwnode, 0);
184 	if (err) {
185 		netdev_err(dev, "Could not attach to PHY\n");
186 		return err;
187 	}
188 
189 	phylink_start(port->phylink);
190 
191 	return 0;
192 }
193 
lan966x_port_stop(struct net_device * dev)194 static int lan966x_port_stop(struct net_device *dev)
195 {
196 	struct lan966x_port *port = netdev_priv(dev);
197 
198 	lan966x_port_config_down(port);
199 	phylink_stop(port->phylink);
200 	phylink_disconnect_phy(port->phylink);
201 
202 	return 0;
203 }
204 
lan966x_port_inj_status(struct lan966x * lan966x)205 static int lan966x_port_inj_status(struct lan966x *lan966x)
206 {
207 	return lan_rd(lan966x, QS_INJ_STATUS);
208 }
209 
lan966x_port_inj_ready(struct lan966x * lan966x,u8 grp)210 static int lan966x_port_inj_ready(struct lan966x *lan966x, u8 grp)
211 {
212 	u32 val;
213 
214 	if (lan_rd(lan966x, QS_INJ_STATUS) & QS_INJ_STATUS_FIFO_RDY_SET(BIT(grp)))
215 		return 0;
216 
217 	return readx_poll_timeout_atomic(lan966x_port_inj_status, lan966x, val,
218 					 QS_INJ_STATUS_FIFO_RDY_GET(val) & BIT(grp),
219 					 READL_SLEEP_US, READL_TIMEOUT_US);
220 }
221 
lan966x_port_ifh_xmit(struct sk_buff * skb,__be32 * ifh,struct net_device * dev)222 static int lan966x_port_ifh_xmit(struct sk_buff *skb,
223 				 __be32 *ifh,
224 				 struct net_device *dev)
225 {
226 	struct lan966x_port *port = netdev_priv(dev);
227 	struct lan966x *lan966x = port->lan966x;
228 	u32 i, count, last;
229 	u8 grp = 0;
230 	u32 val;
231 	int err;
232 
233 	val = lan_rd(lan966x, QS_INJ_STATUS);
234 	if (!(QS_INJ_STATUS_FIFO_RDY_GET(val) & BIT(grp)) ||
235 	    (QS_INJ_STATUS_WMARK_REACHED_GET(val) & BIT(grp)))
236 		goto err;
237 
238 	/* Write start of frame */
239 	lan_wr(QS_INJ_CTRL_GAP_SIZE_SET(1) |
240 	       QS_INJ_CTRL_SOF_SET(1),
241 	       lan966x, QS_INJ_CTRL(grp));
242 
243 	/* Write IFH header */
244 	for (i = 0; i < IFH_LEN; ++i) {
245 		/* Wait until the fifo is ready */
246 		err = lan966x_port_inj_ready(lan966x, grp);
247 		if (err)
248 			goto err;
249 
250 		lan_wr((__force u32)ifh[i], lan966x, QS_INJ_WR(grp));
251 	}
252 
253 	/* Write frame */
254 	count = DIV_ROUND_UP(skb->len, 4);
255 	last = skb->len % 4;
256 	for (i = 0; i < count; ++i) {
257 		/* Wait until the fifo is ready */
258 		err = lan966x_port_inj_ready(lan966x, grp);
259 		if (err)
260 			goto err;
261 
262 		lan_wr(((u32 *)skb->data)[i], lan966x, QS_INJ_WR(grp));
263 	}
264 
265 	/* Add padding */
266 	while (i < (LAN966X_BUFFER_MIN_SZ / 4)) {
267 		/* Wait until the fifo is ready */
268 		err = lan966x_port_inj_ready(lan966x, grp);
269 		if (err)
270 			goto err;
271 
272 		lan_wr(0, lan966x, QS_INJ_WR(grp));
273 		++i;
274 	}
275 
276 	/* Inidcate EOF and valid bytes in the last word */
277 	lan_wr(QS_INJ_CTRL_GAP_SIZE_SET(1) |
278 	       QS_INJ_CTRL_VLD_BYTES_SET(skb->len < LAN966X_BUFFER_MIN_SZ ?
279 				     0 : last) |
280 	       QS_INJ_CTRL_EOF_SET(1),
281 	       lan966x, QS_INJ_CTRL(grp));
282 
283 	/* Add dummy CRC */
284 	lan_wr(0, lan966x, QS_INJ_WR(grp));
285 	skb_tx_timestamp(skb);
286 
287 	dev->stats.tx_packets++;
288 	dev->stats.tx_bytes += skb->len;
289 
290 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
291 	    LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
292 		return NETDEV_TX_OK;
293 
294 	dev_consume_skb_any(skb);
295 	return NETDEV_TX_OK;
296 
297 err:
298 	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
299 	    LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
300 		lan966x_ptp_txtstamp_release(port, skb);
301 
302 	return NETDEV_TX_BUSY;
303 }
304 
lan966x_ifh_set_bypass(void * ifh,u64 bypass)305 static void lan966x_ifh_set_bypass(void *ifh, u64 bypass)
306 {
307 	packing(ifh, &bypass, IFH_POS_BYPASS + IFH_WID_BYPASS - 1,
308 		IFH_POS_BYPASS, IFH_LEN * 4, PACK, 0);
309 }
310 
lan966x_ifh_set_port(void * ifh,u64 bypass)311 static void lan966x_ifh_set_port(void *ifh, u64 bypass)
312 {
313 	packing(ifh, &bypass, IFH_POS_DSTS + IFH_WID_DSTS - 1,
314 		IFH_POS_DSTS, IFH_LEN * 4, PACK, 0);
315 }
316 
lan966x_ifh_set_qos_class(void * ifh,u64 bypass)317 static void lan966x_ifh_set_qos_class(void *ifh, u64 bypass)
318 {
319 	packing(ifh, &bypass, IFH_POS_QOS_CLASS + IFH_WID_QOS_CLASS - 1,
320 		IFH_POS_QOS_CLASS, IFH_LEN * 4, PACK, 0);
321 }
322 
lan966x_ifh_set_ipv(void * ifh,u64 bypass)323 static void lan966x_ifh_set_ipv(void *ifh, u64 bypass)
324 {
325 	packing(ifh, &bypass, IFH_POS_IPV + IFH_WID_IPV - 1,
326 		IFH_POS_IPV, IFH_LEN * 4, PACK, 0);
327 }
328 
lan966x_ifh_set_vid(void * ifh,u64 vid)329 static void lan966x_ifh_set_vid(void *ifh, u64 vid)
330 {
331 	packing(ifh, &vid, IFH_POS_TCI + IFH_WID_TCI - 1,
332 		IFH_POS_TCI, IFH_LEN * 4, PACK, 0);
333 }
334 
lan966x_ifh_set_rew_op(void * ifh,u64 rew_op)335 static void lan966x_ifh_set_rew_op(void *ifh, u64 rew_op)
336 {
337 	packing(ifh, &rew_op, IFH_POS_REW_CMD + IFH_WID_REW_CMD - 1,
338 		IFH_POS_REW_CMD, IFH_LEN * 4, PACK, 0);
339 }
340 
lan966x_ifh_set_timestamp(void * ifh,u64 timestamp)341 static void lan966x_ifh_set_timestamp(void *ifh, u64 timestamp)
342 {
343 	packing(ifh, &timestamp, IFH_POS_TIMESTAMP + IFH_WID_TIMESTAMP - 1,
344 		IFH_POS_TIMESTAMP, IFH_LEN * 4, PACK, 0);
345 }
346 
lan966x_port_xmit(struct sk_buff * skb,struct net_device * dev)347 static netdev_tx_t lan966x_port_xmit(struct sk_buff *skb,
348 				     struct net_device *dev)
349 {
350 	struct lan966x_port *port = netdev_priv(dev);
351 	struct lan966x *lan966x = port->lan966x;
352 	__be32 ifh[IFH_LEN];
353 	int err;
354 
355 	memset(ifh, 0x0, sizeof(__be32) * IFH_LEN);
356 
357 	lan966x_ifh_set_bypass(ifh, 1);
358 	lan966x_ifh_set_port(ifh, BIT_ULL(port->chip_port));
359 	lan966x_ifh_set_qos_class(ifh, skb->priority >= 7 ? 0x7 : skb->priority);
360 	lan966x_ifh_set_ipv(ifh, skb->priority >= 7 ? 0x7 : skb->priority);
361 	lan966x_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
362 
363 	if (port->lan966x->ptp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
364 		err = lan966x_ptp_txtstamp_request(port, skb);
365 		if (err)
366 			return err;
367 
368 		lan966x_ifh_set_rew_op(ifh, LAN966X_SKB_CB(skb)->rew_op);
369 		lan966x_ifh_set_timestamp(ifh, LAN966X_SKB_CB(skb)->ts_id);
370 	}
371 
372 	spin_lock(&lan966x->tx_lock);
373 	if (port->lan966x->fdma)
374 		err = lan966x_fdma_xmit(skb, ifh, dev);
375 	else
376 		err = lan966x_port_ifh_xmit(skb, ifh, dev);
377 	spin_unlock(&lan966x->tx_lock);
378 
379 	return err;
380 }
381 
lan966x_port_change_mtu(struct net_device * dev,int new_mtu)382 static int lan966x_port_change_mtu(struct net_device *dev, int new_mtu)
383 {
384 	struct lan966x_port *port = netdev_priv(dev);
385 	struct lan966x *lan966x = port->lan966x;
386 	int old_mtu = dev->mtu;
387 	int err;
388 
389 	lan_wr(DEV_MAC_MAXLEN_CFG_MAX_LEN_SET(LAN966X_HW_MTU(new_mtu)),
390 	       lan966x, DEV_MAC_MAXLEN_CFG(port->chip_port));
391 	dev->mtu = new_mtu;
392 
393 	if (!lan966x->fdma)
394 		return 0;
395 
396 	err = lan966x_fdma_change_mtu(lan966x);
397 	if (err) {
398 		lan_wr(DEV_MAC_MAXLEN_CFG_MAX_LEN_SET(LAN966X_HW_MTU(old_mtu)),
399 		       lan966x, DEV_MAC_MAXLEN_CFG(port->chip_port));
400 		dev->mtu = old_mtu;
401 	}
402 
403 	return err;
404 }
405 
lan966x_mc_unsync(struct net_device * dev,const unsigned char * addr)406 static int lan966x_mc_unsync(struct net_device *dev, const unsigned char *addr)
407 {
408 	struct lan966x_port *port = netdev_priv(dev);
409 	struct lan966x *lan966x = port->lan966x;
410 
411 	return lan966x_mac_forget(lan966x, addr, HOST_PVID, ENTRYTYPE_LOCKED);
412 }
413 
lan966x_mc_sync(struct net_device * dev,const unsigned char * addr)414 static int lan966x_mc_sync(struct net_device *dev, const unsigned char *addr)
415 {
416 	struct lan966x_port *port = netdev_priv(dev);
417 	struct lan966x *lan966x = port->lan966x;
418 
419 	return lan966x_mac_cpu_learn(lan966x, addr, HOST_PVID);
420 }
421 
lan966x_port_set_rx_mode(struct net_device * dev)422 static void lan966x_port_set_rx_mode(struct net_device *dev)
423 {
424 	__dev_mc_sync(dev, lan966x_mc_sync, lan966x_mc_unsync);
425 }
426 
lan966x_port_get_parent_id(struct net_device * dev,struct netdev_phys_item_id * ppid)427 static int lan966x_port_get_parent_id(struct net_device *dev,
428 				      struct netdev_phys_item_id *ppid)
429 {
430 	struct lan966x_port *port = netdev_priv(dev);
431 	struct lan966x *lan966x = port->lan966x;
432 
433 	ppid->id_len = sizeof(lan966x->base_mac);
434 	memcpy(&ppid->id, &lan966x->base_mac, ppid->id_len);
435 
436 	return 0;
437 }
438 
lan966x_port_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)439 static int lan966x_port_ioctl(struct net_device *dev, struct ifreq *ifr,
440 			      int cmd)
441 {
442 	struct lan966x_port *port = netdev_priv(dev);
443 
444 	if (!phy_has_hwtstamp(dev->phydev) && port->lan966x->ptp) {
445 		switch (cmd) {
446 		case SIOCSHWTSTAMP:
447 			return lan966x_ptp_hwtstamp_set(port, ifr);
448 		case SIOCGHWTSTAMP:
449 			return lan966x_ptp_hwtstamp_get(port, ifr);
450 		}
451 	}
452 
453 	if (!dev->phydev)
454 		return -ENODEV;
455 
456 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
457 }
458 
459 static const struct net_device_ops lan966x_port_netdev_ops = {
460 	.ndo_open			= lan966x_port_open,
461 	.ndo_stop			= lan966x_port_stop,
462 	.ndo_start_xmit			= lan966x_port_xmit,
463 	.ndo_change_mtu			= lan966x_port_change_mtu,
464 	.ndo_set_rx_mode		= lan966x_port_set_rx_mode,
465 	.ndo_get_phys_port_name		= lan966x_port_get_phys_port_name,
466 	.ndo_get_stats64		= lan966x_stats_get,
467 	.ndo_set_mac_address		= lan966x_port_set_mac_address,
468 	.ndo_get_port_parent_id		= lan966x_port_get_parent_id,
469 	.ndo_eth_ioctl			= lan966x_port_ioctl,
470 	.ndo_setup_tc			= lan966x_tc_setup,
471 };
472 
lan966x_netdevice_check(const struct net_device * dev)473 bool lan966x_netdevice_check(const struct net_device *dev)
474 {
475 	return dev->netdev_ops == &lan966x_port_netdev_ops;
476 }
477 
lan966x_hw_offload(struct lan966x * lan966x,u32 port,struct sk_buff * skb)478 bool lan966x_hw_offload(struct lan966x *lan966x, u32 port, struct sk_buff *skb)
479 {
480 	u32 val;
481 
482 	/* The IGMP and MLD frames are not forward by the HW if
483 	 * multicast snooping is enabled, therefor don't mark as
484 	 * offload to allow the SW to forward the frames accordingly.
485 	 */
486 	val = lan_rd(lan966x, ANA_CPU_FWD_CFG(port));
487 	if (!(val & (ANA_CPU_FWD_CFG_IGMP_REDIR_ENA |
488 		     ANA_CPU_FWD_CFG_MLD_REDIR_ENA)))
489 		return true;
490 
491 	if (eth_type_vlan(skb->protocol)) {
492 		skb = skb_vlan_untag(skb);
493 		if (unlikely(!skb))
494 			return false;
495 	}
496 
497 	if (skb->protocol == htons(ETH_P_IP) &&
498 	    ip_hdr(skb)->protocol == IPPROTO_IGMP)
499 		return false;
500 
501 	if (IS_ENABLED(CONFIG_IPV6) &&
502 	    skb->protocol == htons(ETH_P_IPV6) &&
503 	    ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
504 	    !ipv6_mc_check_mld(skb))
505 		return false;
506 
507 	return true;
508 }
509 
lan966x_port_xtr_status(struct lan966x * lan966x,u8 grp)510 static int lan966x_port_xtr_status(struct lan966x *lan966x, u8 grp)
511 {
512 	return lan_rd(lan966x, QS_XTR_RD(grp));
513 }
514 
lan966x_port_xtr_ready(struct lan966x * lan966x,u8 grp)515 static int lan966x_port_xtr_ready(struct lan966x *lan966x, u8 grp)
516 {
517 	u32 val;
518 
519 	return read_poll_timeout(lan966x_port_xtr_status, val,
520 				 val != XTR_NOT_READY,
521 				 READL_SLEEP_US, READL_TIMEOUT_US, false,
522 				 lan966x, grp);
523 }
524 
lan966x_rx_frame_word(struct lan966x * lan966x,u8 grp,u32 * rval)525 static int lan966x_rx_frame_word(struct lan966x *lan966x, u8 grp, u32 *rval)
526 {
527 	u32 bytes_valid;
528 	u32 val;
529 	int err;
530 
531 	val = lan_rd(lan966x, QS_XTR_RD(grp));
532 	if (val == XTR_NOT_READY) {
533 		err = lan966x_port_xtr_ready(lan966x, grp);
534 		if (err)
535 			return -EIO;
536 	}
537 
538 	switch (val) {
539 	case XTR_ABORT:
540 		return -EIO;
541 	case XTR_EOF_0:
542 	case XTR_EOF_1:
543 	case XTR_EOF_2:
544 	case XTR_EOF_3:
545 	case XTR_PRUNED:
546 		bytes_valid = XTR_VALID_BYTES(val);
547 		val = lan_rd(lan966x, QS_XTR_RD(grp));
548 		if (val == XTR_ESCAPE)
549 			*rval = lan_rd(lan966x, QS_XTR_RD(grp));
550 		else
551 			*rval = val;
552 
553 		return bytes_valid;
554 	case XTR_ESCAPE:
555 		*rval = lan_rd(lan966x, QS_XTR_RD(grp));
556 
557 		return 4;
558 	default:
559 		*rval = val;
560 
561 		return 4;
562 	}
563 }
564 
lan966x_ifh_get_src_port(void * ifh,u64 * src_port)565 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port)
566 {
567 	packing(ifh, src_port, IFH_POS_SRCPORT + IFH_WID_SRCPORT - 1,
568 		IFH_POS_SRCPORT, IFH_LEN * 4, UNPACK, 0);
569 }
570 
lan966x_ifh_get_len(void * ifh,u64 * len)571 static void lan966x_ifh_get_len(void *ifh, u64 *len)
572 {
573 	packing(ifh, len, IFH_POS_LEN + IFH_WID_LEN - 1,
574 		IFH_POS_LEN, IFH_LEN * 4, UNPACK, 0);
575 }
576 
lan966x_ifh_get_timestamp(void * ifh,u64 * timestamp)577 void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp)
578 {
579 	packing(ifh, timestamp, IFH_POS_TIMESTAMP + IFH_WID_TIMESTAMP - 1,
580 		IFH_POS_TIMESTAMP, IFH_LEN * 4, UNPACK, 0);
581 }
582 
lan966x_xtr_irq_handler(int irq,void * args)583 static irqreturn_t lan966x_xtr_irq_handler(int irq, void *args)
584 {
585 	struct lan966x *lan966x = args;
586 	int i, grp = 0, err = 0;
587 
588 	if (!(lan_rd(lan966x, QS_XTR_DATA_PRESENT) & BIT(grp)))
589 		return IRQ_NONE;
590 
591 	do {
592 		u64 src_port, len, timestamp;
593 		struct net_device *dev;
594 		struct sk_buff *skb;
595 		int sz = 0, buf_len;
596 		u32 ifh[IFH_LEN];
597 		u32 *buf;
598 		u32 val;
599 
600 		for (i = 0; i < IFH_LEN; i++) {
601 			err = lan966x_rx_frame_word(lan966x, grp, &ifh[i]);
602 			if (err != 4)
603 				goto recover;
604 		}
605 
606 		err = 0;
607 
608 		lan966x_ifh_get_src_port(ifh, &src_port);
609 		lan966x_ifh_get_len(ifh, &len);
610 		lan966x_ifh_get_timestamp(ifh, &timestamp);
611 
612 		WARN_ON(src_port >= lan966x->num_phys_ports);
613 
614 		dev = lan966x->ports[src_port]->dev;
615 		skb = netdev_alloc_skb(dev, len);
616 		if (unlikely(!skb)) {
617 			netdev_err(dev, "Unable to allocate sk_buff\n");
618 			err = -ENOMEM;
619 			break;
620 		}
621 		buf_len = len - ETH_FCS_LEN;
622 		buf = (u32 *)skb_put(skb, buf_len);
623 
624 		len = 0;
625 		do {
626 			sz = lan966x_rx_frame_word(lan966x, grp, &val);
627 			if (sz < 0) {
628 				kfree_skb(skb);
629 				goto recover;
630 			}
631 
632 			*buf++ = val;
633 			len += sz;
634 		} while (len < buf_len);
635 
636 		/* Read the FCS */
637 		sz = lan966x_rx_frame_word(lan966x, grp, &val);
638 		if (sz < 0) {
639 			kfree_skb(skb);
640 			goto recover;
641 		}
642 
643 		/* Update the statistics if part of the FCS was read before */
644 		len -= ETH_FCS_LEN - sz;
645 
646 		if (unlikely(dev->features & NETIF_F_RXFCS)) {
647 			buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
648 			*buf = val;
649 		}
650 
651 		lan966x_ptp_rxtstamp(lan966x, skb, timestamp);
652 		skb->protocol = eth_type_trans(skb, dev);
653 
654 		if (lan966x->bridge_mask & BIT(src_port)) {
655 			skb->offload_fwd_mark = 1;
656 
657 			skb_reset_network_header(skb);
658 			if (!lan966x_hw_offload(lan966x, src_port, skb))
659 				skb->offload_fwd_mark = 0;
660 		}
661 
662 		if (!skb_defer_rx_timestamp(skb))
663 			netif_rx(skb);
664 
665 		dev->stats.rx_bytes += len;
666 		dev->stats.rx_packets++;
667 
668 recover:
669 		if (sz < 0 || err)
670 			lan_rd(lan966x, QS_XTR_RD(grp));
671 
672 	} while (lan_rd(lan966x, QS_XTR_DATA_PRESENT) & BIT(grp));
673 
674 	return IRQ_HANDLED;
675 }
676 
lan966x_ana_irq_handler(int irq,void * args)677 static irqreturn_t lan966x_ana_irq_handler(int irq, void *args)
678 {
679 	struct lan966x *lan966x = args;
680 
681 	return lan966x_mac_irq_handler(lan966x);
682 }
683 
lan966x_cleanup_ports(struct lan966x * lan966x)684 static void lan966x_cleanup_ports(struct lan966x *lan966x)
685 {
686 	struct lan966x_port *port;
687 	int p;
688 
689 	for (p = 0; p < lan966x->num_phys_ports; p++) {
690 		port = lan966x->ports[p];
691 		if (!port)
692 			continue;
693 
694 		if (port->dev)
695 			unregister_netdev(port->dev);
696 
697 		if (lan966x->fdma && lan966x->fdma_ndev == port->dev)
698 			lan966x_fdma_netdev_deinit(lan966x, port->dev);
699 
700 		if (port->phylink) {
701 			rtnl_lock();
702 			lan966x_port_stop(port->dev);
703 			rtnl_unlock();
704 			phylink_destroy(port->phylink);
705 			port->phylink = NULL;
706 		}
707 
708 		if (port->fwnode)
709 			fwnode_handle_put(port->fwnode);
710 	}
711 
712 	disable_irq(lan966x->xtr_irq);
713 	lan966x->xtr_irq = -ENXIO;
714 
715 	if (lan966x->ana_irq > 0) {
716 		disable_irq(lan966x->ana_irq);
717 		lan966x->ana_irq = -ENXIO;
718 	}
719 
720 	if (lan966x->fdma)
721 		devm_free_irq(lan966x->dev, lan966x->fdma_irq, lan966x);
722 
723 	if (lan966x->ptp_irq > 0)
724 		devm_free_irq(lan966x->dev, lan966x->ptp_irq, lan966x);
725 
726 	if (lan966x->ptp_ext_irq > 0)
727 		devm_free_irq(lan966x->dev, lan966x->ptp_ext_irq, lan966x);
728 }
729 
lan966x_probe_port(struct lan966x * lan966x,u32 p,phy_interface_t phy_mode,struct fwnode_handle * portnp)730 static int lan966x_probe_port(struct lan966x *lan966x, u32 p,
731 			      phy_interface_t phy_mode,
732 			      struct fwnode_handle *portnp)
733 {
734 	struct lan966x_port *port;
735 	struct phylink *phylink;
736 	struct net_device *dev;
737 	int err;
738 
739 	if (p >= lan966x->num_phys_ports)
740 		return -EINVAL;
741 
742 	dev = devm_alloc_etherdev_mqs(lan966x->dev,
743 				      sizeof(struct lan966x_port),
744 				      NUM_PRIO_QUEUES, 1);
745 	if (!dev)
746 		return -ENOMEM;
747 
748 	SET_NETDEV_DEV(dev, lan966x->dev);
749 	port = netdev_priv(dev);
750 	port->dev = dev;
751 	port->lan966x = lan966x;
752 	port->chip_port = p;
753 	lan966x->ports[p] = port;
754 
755 	dev->max_mtu = ETH_MAX_MTU;
756 
757 	dev->netdev_ops = &lan966x_port_netdev_ops;
758 	dev->ethtool_ops = &lan966x_ethtool_ops;
759 	dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
760 			 NETIF_F_HW_VLAN_STAG_TX |
761 			 NETIF_F_HW_TC;
762 	dev->hw_features |= NETIF_F_HW_TC;
763 	dev->needed_headroom = IFH_LEN * sizeof(u32);
764 
765 	eth_hw_addr_gen(dev, lan966x->base_mac, p + 1);
766 
767 	lan966x_mac_learn(lan966x, PGID_CPU, dev->dev_addr, HOST_PVID,
768 			  ENTRYTYPE_LOCKED);
769 
770 	port->phylink_config.dev = &port->dev->dev;
771 	port->phylink_config.type = PHYLINK_NETDEV;
772 	port->phylink_pcs.poll = true;
773 	port->phylink_pcs.ops = &lan966x_phylink_pcs_ops;
774 
775 	port->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
776 		MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD;
777 
778 	phy_interface_set_rgmii(port->phylink_config.supported_interfaces);
779 	__set_bit(PHY_INTERFACE_MODE_MII,
780 		  port->phylink_config.supported_interfaces);
781 	__set_bit(PHY_INTERFACE_MODE_GMII,
782 		  port->phylink_config.supported_interfaces);
783 	__set_bit(PHY_INTERFACE_MODE_SGMII,
784 		  port->phylink_config.supported_interfaces);
785 	__set_bit(PHY_INTERFACE_MODE_QSGMII,
786 		  port->phylink_config.supported_interfaces);
787 	__set_bit(PHY_INTERFACE_MODE_QUSGMII,
788 		  port->phylink_config.supported_interfaces);
789 	__set_bit(PHY_INTERFACE_MODE_1000BASEX,
790 		  port->phylink_config.supported_interfaces);
791 	__set_bit(PHY_INTERFACE_MODE_2500BASEX,
792 		  port->phylink_config.supported_interfaces);
793 
794 	phylink = phylink_create(&port->phylink_config,
795 				 portnp,
796 				 phy_mode,
797 				 &lan966x_phylink_mac_ops);
798 	if (IS_ERR(phylink)) {
799 		port->dev = NULL;
800 		return PTR_ERR(phylink);
801 	}
802 
803 	port->phylink = phylink;
804 
805 	err = register_netdev(dev);
806 	if (err) {
807 		dev_err(lan966x->dev, "register_netdev failed\n");
808 		return err;
809 	}
810 
811 	lan966x_vlan_port_set_vlan_aware(port, 0);
812 	lan966x_vlan_port_set_vid(port, HOST_PVID, false, false);
813 	lan966x_vlan_port_apply(port);
814 
815 	return 0;
816 }
817 
lan966x_init(struct lan966x * lan966x)818 static void lan966x_init(struct lan966x *lan966x)
819 {
820 	u32 p, i;
821 
822 	/* MAC table initialization */
823 	lan966x_mac_init(lan966x);
824 
825 	lan966x_vlan_init(lan966x);
826 
827 	/* Flush queues */
828 	lan_wr(lan_rd(lan966x, QS_XTR_FLUSH) |
829 	       GENMASK(1, 0),
830 	       lan966x, QS_XTR_FLUSH);
831 
832 	/* Allow to drain */
833 	mdelay(1);
834 
835 	/* All Queues normal */
836 	lan_wr(lan_rd(lan966x, QS_XTR_FLUSH) &
837 	       ~(GENMASK(1, 0)),
838 	       lan966x, QS_XTR_FLUSH);
839 
840 	/* Set MAC age time to default value, the entry is aged after
841 	 * 2 * AGE_PERIOD
842 	 */
843 	lan_wr(ANA_AUTOAGE_AGE_PERIOD_SET(BR_DEFAULT_AGEING_TIME / 2 / HZ),
844 	       lan966x, ANA_AUTOAGE);
845 
846 	/* Disable learning for frames discarded by VLAN ingress filtering */
847 	lan_rmw(ANA_ADVLEARN_VLAN_CHK_SET(1),
848 		ANA_ADVLEARN_VLAN_CHK,
849 		lan966x, ANA_ADVLEARN);
850 
851 	/* Setup frame ageing - "2 sec" - The unit is 6.5 us on lan966x */
852 	lan_wr(SYS_FRM_AGING_AGE_TX_ENA_SET(1) |
853 	       (20000000 / 65),
854 	       lan966x,  SYS_FRM_AGING);
855 
856 	/* Map the 8 CPU extraction queues to CPU port */
857 	lan_wr(0, lan966x, QSYS_CPU_GROUP_MAP);
858 
859 	/* Do byte-swap and expect status after last data word
860 	 * Extraction: Mode: manual extraction) | Byte_swap
861 	 */
862 	lan_wr(QS_XTR_GRP_CFG_MODE_SET(lan966x->fdma ? 2 : 1) |
863 	       QS_XTR_GRP_CFG_BYTE_SWAP_SET(1),
864 	       lan966x, QS_XTR_GRP_CFG(0));
865 
866 	/* Injection: Mode: manual injection | Byte_swap */
867 	lan_wr(QS_INJ_GRP_CFG_MODE_SET(lan966x->fdma ? 2 : 1) |
868 	       QS_INJ_GRP_CFG_BYTE_SWAP_SET(1),
869 	       lan966x, QS_INJ_GRP_CFG(0));
870 
871 	lan_rmw(QS_INJ_CTRL_GAP_SIZE_SET(0),
872 		QS_INJ_CTRL_GAP_SIZE,
873 		lan966x, QS_INJ_CTRL(0));
874 
875 	/* Enable IFH insertion/parsing on CPU ports */
876 	lan_wr(SYS_PORT_MODE_INCL_INJ_HDR_SET(1) |
877 	       SYS_PORT_MODE_INCL_XTR_HDR_SET(1),
878 	       lan966x, SYS_PORT_MODE(CPU_PORT));
879 
880 	/* Setup flooding PGIDs */
881 	lan_wr(ANA_FLOODING_IPMC_FLD_MC4_DATA_SET(PGID_MCIPV4) |
882 	       ANA_FLOODING_IPMC_FLD_MC4_CTRL_SET(PGID_MC) |
883 	       ANA_FLOODING_IPMC_FLD_MC6_DATA_SET(PGID_MCIPV6) |
884 	       ANA_FLOODING_IPMC_FLD_MC6_CTRL_SET(PGID_MC),
885 	       lan966x, ANA_FLOODING_IPMC);
886 
887 	/* There are 8 priorities */
888 	for (i = 0; i < 8; ++i)
889 		lan_rmw(ANA_FLOODING_FLD_MULTICAST_SET(PGID_MC) |
890 			ANA_FLOODING_FLD_UNICAST_SET(PGID_UC) |
891 			ANA_FLOODING_FLD_BROADCAST_SET(PGID_BC),
892 			ANA_FLOODING_FLD_MULTICAST |
893 			ANA_FLOODING_FLD_UNICAST |
894 			ANA_FLOODING_FLD_BROADCAST,
895 			lan966x, ANA_FLOODING(i));
896 
897 	for (i = 0; i < PGID_ENTRIES; ++i)
898 		/* Set all the entries to obey VLAN_VLAN */
899 		lan_rmw(ANA_PGID_CFG_OBEY_VLAN_SET(1),
900 			ANA_PGID_CFG_OBEY_VLAN,
901 			lan966x, ANA_PGID_CFG(i));
902 
903 	for (p = 0; p < lan966x->num_phys_ports; p++) {
904 		/* Disable bridging by default */
905 		lan_rmw(ANA_PGID_PGID_SET(0x0),
906 			ANA_PGID_PGID,
907 			lan966x, ANA_PGID(p + PGID_SRC));
908 
909 		/* Do not forward BPDU frames to the front ports and copy them
910 		 * to CPU
911 		 */
912 		lan_wr(0xffff, lan966x, ANA_CPU_FWD_BPDU_CFG(p));
913 	}
914 
915 	/* Set source buffer size for each priority and each port to 1500 bytes */
916 	for (i = 0; i <= QSYS_Q_RSRV; ++i) {
917 		lan_wr(1500 / 64, lan966x, QSYS_RES_CFG(i));
918 		lan_wr(1500 / 64, lan966x, QSYS_RES_CFG(512 + i));
919 	}
920 
921 	/* Enable switching to/from cpu port */
922 	lan_wr(QSYS_SW_PORT_MODE_PORT_ENA_SET(1) |
923 	       QSYS_SW_PORT_MODE_SCH_NEXT_CFG_SET(1) |
924 	       QSYS_SW_PORT_MODE_INGRESS_DROP_MODE_SET(1),
925 	       lan966x,  QSYS_SW_PORT_MODE(CPU_PORT));
926 
927 	/* Configure and enable the CPU port */
928 	lan_rmw(ANA_PGID_PGID_SET(0),
929 		ANA_PGID_PGID,
930 		lan966x, ANA_PGID(CPU_PORT));
931 	lan_rmw(ANA_PGID_PGID_SET(BIT(CPU_PORT)),
932 		ANA_PGID_PGID,
933 		lan966x, ANA_PGID(PGID_CPU));
934 
935 	/* Multicast to all other ports */
936 	lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
937 		ANA_PGID_PGID,
938 		lan966x, ANA_PGID(PGID_MC));
939 
940 	/* This will be controlled by mrouter ports */
941 	lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
942 		ANA_PGID_PGID,
943 		lan966x, ANA_PGID(PGID_MCIPV4));
944 
945 	lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
946 		ANA_PGID_PGID,
947 		lan966x, ANA_PGID(PGID_MCIPV6));
948 
949 	/* Unicast to all other ports */
950 	lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
951 		ANA_PGID_PGID,
952 		lan966x, ANA_PGID(PGID_UC));
953 
954 	/* Broadcast to the CPU port and to other ports */
955 	lan_rmw(ANA_PGID_PGID_SET(BIT(CPU_PORT) | GENMASK(lan966x->num_phys_ports - 1, 0)),
956 		ANA_PGID_PGID,
957 		lan966x, ANA_PGID(PGID_BC));
958 
959 	lan_wr(REW_PORT_CFG_NO_REWRITE_SET(1),
960 	       lan966x, REW_PORT_CFG(CPU_PORT));
961 
962 	lan_rmw(ANA_ANAINTR_INTR_ENA_SET(1),
963 		ANA_ANAINTR_INTR_ENA,
964 		lan966x, ANA_ANAINTR);
965 
966 	spin_lock_init(&lan966x->tx_lock);
967 
968 	lan966x_taprio_init(lan966x);
969 }
970 
lan966x_ram_init(struct lan966x * lan966x)971 static int lan966x_ram_init(struct lan966x *lan966x)
972 {
973 	return lan_rd(lan966x, SYS_RAM_INIT);
974 }
975 
lan966x_reset_switch(struct lan966x * lan966x)976 static int lan966x_reset_switch(struct lan966x *lan966x)
977 {
978 	struct reset_control *switch_reset;
979 	int val = 0;
980 	int ret;
981 
982 	switch_reset = devm_reset_control_get_optional_shared(lan966x->dev,
983 							      "switch");
984 	if (IS_ERR(switch_reset))
985 		return dev_err_probe(lan966x->dev, PTR_ERR(switch_reset),
986 				     "Could not obtain switch reset");
987 
988 	reset_control_reset(switch_reset);
989 
990 	lan_wr(SYS_RESET_CFG_CORE_ENA_SET(0), lan966x, SYS_RESET_CFG);
991 	lan_wr(SYS_RAM_INIT_RAM_INIT_SET(1), lan966x, SYS_RAM_INIT);
992 	ret = readx_poll_timeout(lan966x_ram_init, lan966x,
993 				 val, (val & BIT(1)) == 0, READL_SLEEP_US,
994 				 READL_TIMEOUT_US);
995 	if (ret)
996 		return ret;
997 
998 	lan_wr(SYS_RESET_CFG_CORE_ENA_SET(1), lan966x, SYS_RESET_CFG);
999 
1000 	return 0;
1001 }
1002 
lan966x_probe(struct platform_device * pdev)1003 static int lan966x_probe(struct platform_device *pdev)
1004 {
1005 	struct fwnode_handle *ports, *portnp;
1006 	struct lan966x *lan966x;
1007 	u8 mac_addr[ETH_ALEN];
1008 	int err;
1009 
1010 	lan966x = devm_kzalloc(&pdev->dev, sizeof(*lan966x), GFP_KERNEL);
1011 	if (!lan966x)
1012 		return -ENOMEM;
1013 
1014 	platform_set_drvdata(pdev, lan966x);
1015 	lan966x->dev = &pdev->dev;
1016 
1017 	if (!device_get_mac_address(&pdev->dev, mac_addr)) {
1018 		ether_addr_copy(lan966x->base_mac, mac_addr);
1019 	} else {
1020 		pr_info("MAC addr was not set, use random MAC\n");
1021 		eth_random_addr(lan966x->base_mac);
1022 		lan966x->base_mac[5] &= 0xf0;
1023 	}
1024 
1025 	err = lan966x_create_targets(pdev, lan966x);
1026 	if (err)
1027 		return dev_err_probe(&pdev->dev, err,
1028 				     "Failed to create targets");
1029 
1030 	err = lan966x_reset_switch(lan966x);
1031 	if (err)
1032 		return dev_err_probe(&pdev->dev, err, "Reset failed");
1033 
1034 	lan966x->num_phys_ports = NUM_PHYS_PORTS;
1035 	lan966x->ports = devm_kcalloc(&pdev->dev, lan966x->num_phys_ports,
1036 				      sizeof(struct lan966x_port *),
1037 				      GFP_KERNEL);
1038 	if (!lan966x->ports)
1039 		return -ENOMEM;
1040 
1041 	/* There QS system has 32KB of memory */
1042 	lan966x->shared_queue_sz = LAN966X_BUFFER_MEMORY;
1043 
1044 	/* set irq */
1045 	lan966x->xtr_irq = platform_get_irq_byname(pdev, "xtr");
1046 	if (lan966x->xtr_irq <= 0)
1047 		return -EINVAL;
1048 
1049 	err = devm_request_threaded_irq(&pdev->dev, lan966x->xtr_irq, NULL,
1050 					lan966x_xtr_irq_handler, IRQF_ONESHOT,
1051 					"frame extraction", lan966x);
1052 	if (err) {
1053 		pr_err("Unable to use xtr irq");
1054 		return -ENODEV;
1055 	}
1056 
1057 	lan966x->ana_irq = platform_get_irq_byname(pdev, "ana");
1058 	if (lan966x->ana_irq > 0) {
1059 		err = devm_request_threaded_irq(&pdev->dev, lan966x->ana_irq, NULL,
1060 						lan966x_ana_irq_handler, IRQF_ONESHOT,
1061 						"ana irq", lan966x);
1062 		if (err)
1063 			return dev_err_probe(&pdev->dev, err, "Unable to use ana irq");
1064 	}
1065 
1066 	lan966x->ptp_irq = platform_get_irq_byname(pdev, "ptp");
1067 	if (lan966x->ptp_irq > 0) {
1068 		err = devm_request_threaded_irq(&pdev->dev, lan966x->ptp_irq, NULL,
1069 						lan966x_ptp_irq_handler, IRQF_ONESHOT,
1070 						"ptp irq", lan966x);
1071 		if (err)
1072 			return dev_err_probe(&pdev->dev, err, "Unable to use ptp irq");
1073 
1074 		lan966x->ptp = 1;
1075 	}
1076 
1077 	lan966x->fdma_irq = platform_get_irq_byname(pdev, "fdma");
1078 	if (lan966x->fdma_irq > 0) {
1079 		err = devm_request_irq(&pdev->dev, lan966x->fdma_irq,
1080 				       lan966x_fdma_irq_handler, 0,
1081 				       "fdma irq", lan966x);
1082 		if (err)
1083 			return dev_err_probe(&pdev->dev, err, "Unable to use fdma irq");
1084 
1085 		lan966x->fdma = true;
1086 	}
1087 
1088 	if (lan966x->ptp) {
1089 		lan966x->ptp_ext_irq = platform_get_irq_byname(pdev, "ptp-ext");
1090 		if (lan966x->ptp_ext_irq > 0) {
1091 			err = devm_request_threaded_irq(&pdev->dev,
1092 							lan966x->ptp_ext_irq, NULL,
1093 							lan966x_ptp_ext_irq_handler,
1094 							IRQF_ONESHOT,
1095 							"ptp-ext irq", lan966x);
1096 			if (err)
1097 				return dev_err_probe(&pdev->dev, err,
1098 						     "Unable to use ptp-ext irq");
1099 		}
1100 	}
1101 
1102 	ports = device_get_named_child_node(&pdev->dev, "ethernet-ports");
1103 	if (!ports)
1104 		return dev_err_probe(&pdev->dev, -ENODEV,
1105 				     "no ethernet-ports child found\n");
1106 
1107 	/* init switch */
1108 	lan966x_init(lan966x);
1109 	lan966x_stats_init(lan966x);
1110 
1111 	/* go over the child nodes */
1112 	fwnode_for_each_available_child_node(ports, portnp) {
1113 		phy_interface_t phy_mode;
1114 		struct phy *serdes;
1115 		u32 p;
1116 
1117 		if (fwnode_property_read_u32(portnp, "reg", &p))
1118 			continue;
1119 
1120 		phy_mode = fwnode_get_phy_mode(portnp);
1121 		err = lan966x_probe_port(lan966x, p, phy_mode, portnp);
1122 		if (err)
1123 			goto cleanup_ports;
1124 
1125 		/* Read needed configuration */
1126 		lan966x->ports[p]->config.portmode = phy_mode;
1127 		lan966x->ports[p]->fwnode = fwnode_handle_get(portnp);
1128 
1129 		serdes = devm_of_phy_get(lan966x->dev, to_of_node(portnp), NULL);
1130 		if (PTR_ERR(serdes) == -ENODEV)
1131 			serdes = NULL;
1132 		if (IS_ERR(serdes)) {
1133 			err = PTR_ERR(serdes);
1134 			goto cleanup_ports;
1135 		}
1136 		lan966x->ports[p]->serdes = serdes;
1137 
1138 		lan966x_port_init(lan966x->ports[p]);
1139 	}
1140 
1141 	fwnode_handle_put(ports);
1142 
1143 	lan966x_mdb_init(lan966x);
1144 	err = lan966x_fdb_init(lan966x);
1145 	if (err)
1146 		goto cleanup_ports;
1147 
1148 	err = lan966x_ptp_init(lan966x);
1149 	if (err)
1150 		goto cleanup_fdb;
1151 
1152 	err = lan966x_fdma_init(lan966x);
1153 	if (err)
1154 		goto cleanup_ptp;
1155 
1156 	return 0;
1157 
1158 cleanup_ptp:
1159 	lan966x_ptp_deinit(lan966x);
1160 
1161 cleanup_fdb:
1162 	lan966x_fdb_deinit(lan966x);
1163 
1164 cleanup_ports:
1165 	fwnode_handle_put(ports);
1166 	fwnode_handle_put(portnp);
1167 
1168 	lan966x_cleanup_ports(lan966x);
1169 
1170 	cancel_delayed_work_sync(&lan966x->stats_work);
1171 	destroy_workqueue(lan966x->stats_queue);
1172 	mutex_destroy(&lan966x->stats_lock);
1173 
1174 	return err;
1175 }
1176 
lan966x_remove(struct platform_device * pdev)1177 static int lan966x_remove(struct platform_device *pdev)
1178 {
1179 	struct lan966x *lan966x = platform_get_drvdata(pdev);
1180 
1181 	lan966x_taprio_deinit(lan966x);
1182 	lan966x_fdma_deinit(lan966x);
1183 	lan966x_cleanup_ports(lan966x);
1184 
1185 	cancel_delayed_work_sync(&lan966x->stats_work);
1186 	destroy_workqueue(lan966x->stats_queue);
1187 	mutex_destroy(&lan966x->stats_lock);
1188 
1189 	lan966x_mac_purge_entries(lan966x);
1190 	lan966x_mdb_deinit(lan966x);
1191 	lan966x_fdb_deinit(lan966x);
1192 	lan966x_ptp_deinit(lan966x);
1193 
1194 	return 0;
1195 }
1196 
1197 static struct platform_driver lan966x_driver = {
1198 	.probe = lan966x_probe,
1199 	.remove = lan966x_remove,
1200 	.driver = {
1201 		.name = "lan966x-switch",
1202 		.of_match_table = lan966x_match,
1203 	},
1204 };
1205 
lan966x_switch_driver_init(void)1206 static int __init lan966x_switch_driver_init(void)
1207 {
1208 	int ret;
1209 
1210 	lan966x_register_notifier_blocks();
1211 
1212 	ret = platform_driver_register(&lan966x_driver);
1213 	if (ret)
1214 		goto err;
1215 
1216 	return 0;
1217 
1218 err:
1219 	lan966x_unregister_notifier_blocks();
1220 	return ret;
1221 }
1222 
lan966x_switch_driver_exit(void)1223 static void __exit lan966x_switch_driver_exit(void)
1224 {
1225 	platform_driver_unregister(&lan966x_driver);
1226 	lan966x_unregister_notifier_blocks();
1227 }
1228 
1229 module_init(lan966x_switch_driver_init);
1230 module_exit(lan966x_switch_driver_exit);
1231 
1232 MODULE_DESCRIPTION("Microchip LAN966X switch driver");
1233 MODULE_AUTHOR("Horatiu Vultur <horatiu.vultur@microchip.com>");
1234 MODULE_LICENSE("Dual MIT/GPL");
1235