1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/module.h>
4 #include <linux/if_bridge.h>
5 #include <linux/if_vlan.h>
6 #include <linux/iopoll.h>
9 #include <linux/of_net.h>
10 #include <linux/phy/phy.h>
11 #include <linux/platform_device.h>
12 #include <linux/reset.h>
13 #include <net/addrconf.h>
15 #include "lan966x_main.h"
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))
29 static const struct of_device_id lan966x_match[] = {
30 { .compatible = "microchip,lan966x-switch" },
33 MODULE_DEVICE_TABLE(of, lan966x_match);
35 struct lan966x_main_io_resource {
36 enum lan966x_target id;
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_VCAP, 0x18000, 1 }, /* 0xe2018000 */
51 { TARGET_VCAP + 1, 0x20000, 1 }, /* 0xe2020000 */
52 { TARGET_VCAP + 2, 0x24000, 1 }, /* 0xe2024000 */
53 { TARGET_SYS, 0x28000, 1 }, /* 0xe2028000 */
54 { TARGET_DEV, 0x34000, 1 }, /* 0xe2034000 */
55 { TARGET_DEV + 1, 0x38000, 1 }, /* 0xe2038000 */
56 { TARGET_DEV + 2, 0x3c000, 1 }, /* 0xe203c000 */
57 { TARGET_DEV + 3, 0x40000, 1 }, /* 0xe2040000 */
58 { TARGET_DEV + 4, 0x44000, 1 }, /* 0xe2044000 */
59 { TARGET_DEV + 5, 0x48000, 1 }, /* 0xe2048000 */
60 { TARGET_DEV + 6, 0x4c000, 1 }, /* 0xe204c000 */
61 { TARGET_DEV + 7, 0x50000, 1 }, /* 0xe2050000 */
62 { TARGET_QSYS, 0x100000, 1 }, /* 0xe2100000 */
63 { TARGET_AFI, 0x120000, 1 }, /* 0xe2120000 */
64 { TARGET_ANA, 0x140000, 1 }, /* 0xe2140000 */
67 static int lan966x_create_targets(struct platform_device *pdev,
68 struct lan966x *lan966x)
70 struct resource *iores[IO_RANGES];
71 void __iomem *begin[IO_RANGES];
74 /* Initially map the entire range and after that update each target to
75 * point inside the region at the correct offset. It is possible that
76 * other devices access the same region so don't add any checks about
79 for (idx = 0; idx < IO_RANGES; idx++) {
80 iores[idx] = platform_get_resource(pdev, IORESOURCE_MEM,
83 dev_err(&pdev->dev, "Invalid resource\n");
87 begin[idx] = devm_ioremap(&pdev->dev,
89 resource_size(iores[idx]));
91 dev_err(&pdev->dev, "Unable to get registers: %s\n",
97 for (idx = 0; idx < ARRAY_SIZE(lan966x_main_iomap); idx++) {
98 const struct lan966x_main_io_resource *iomap =
99 &lan966x_main_iomap[idx];
101 lan966x->regs[iomap->id] = begin[iomap->range] + iomap->offset;
107 static bool lan966x_port_unique_address(struct net_device *dev)
109 struct lan966x_port *port = netdev_priv(dev);
110 struct lan966x *lan966x = port->lan966x;
113 for (p = 0; p < lan966x->num_phys_ports; ++p) {
114 port = lan966x->ports[p];
115 if (!port || port->dev == dev)
118 if (ether_addr_equal(dev->dev_addr, port->dev->dev_addr))
125 static int lan966x_port_set_mac_address(struct net_device *dev, void *p)
127 struct lan966x_port *port = netdev_priv(dev);
128 struct lan966x *lan966x = port->lan966x;
129 const struct sockaddr *addr = p;
132 if (ether_addr_equal(addr->sa_data, dev->dev_addr))
135 /* Learn the new net device MAC address in the mac table. */
136 ret = lan966x_mac_cpu_learn(lan966x, addr->sa_data, HOST_PVID);
140 /* If there is another port with the same address as the dev, then don't
141 * delete it from the MAC table
143 if (!lan966x_port_unique_address(dev))
146 /* Then forget the previous one. */
147 ret = lan966x_mac_cpu_forget(lan966x, dev->dev_addr, HOST_PVID);
152 eth_hw_addr_set(dev, addr->sa_data);
156 static int lan966x_port_get_phys_port_name(struct net_device *dev,
157 char *buf, size_t len)
159 struct lan966x_port *port = netdev_priv(dev);
162 ret = snprintf(buf, len, "p%d", port->chip_port);
169 static int lan966x_port_open(struct net_device *dev)
171 struct lan966x_port *port = netdev_priv(dev);
172 struct lan966x *lan966x = port->lan966x;
175 /* Enable receiving frames on the port, and activate auto-learning of
178 lan_rmw(ANA_PORT_CFG_LEARNAUTO_SET(1) |
179 ANA_PORT_CFG_RECV_ENA_SET(1) |
180 ANA_PORT_CFG_PORTID_VAL_SET(port->chip_port),
181 ANA_PORT_CFG_LEARNAUTO |
182 ANA_PORT_CFG_RECV_ENA |
183 ANA_PORT_CFG_PORTID_VAL,
184 lan966x, ANA_PORT_CFG(port->chip_port));
186 err = phylink_fwnode_phy_connect(port->phylink, port->fwnode, 0);
188 netdev_err(dev, "Could not attach to PHY\n");
192 phylink_start(port->phylink);
197 static int lan966x_port_stop(struct net_device *dev)
199 struct lan966x_port *port = netdev_priv(dev);
201 lan966x_port_config_down(port);
202 phylink_stop(port->phylink);
203 phylink_disconnect_phy(port->phylink);
208 static int lan966x_port_inj_status(struct lan966x *lan966x)
210 return lan_rd(lan966x, QS_INJ_STATUS);
213 static int lan966x_port_inj_ready(struct lan966x *lan966x, u8 grp)
217 if (lan_rd(lan966x, QS_INJ_STATUS) & QS_INJ_STATUS_FIFO_RDY_SET(BIT(grp)))
220 return readx_poll_timeout_atomic(lan966x_port_inj_status, lan966x, val,
221 QS_INJ_STATUS_FIFO_RDY_GET(val) & BIT(grp),
222 READL_SLEEP_US, READL_TIMEOUT_US);
225 static int lan966x_port_ifh_xmit(struct sk_buff *skb,
227 struct net_device *dev)
229 struct lan966x_port *port = netdev_priv(dev);
230 struct lan966x *lan966x = port->lan966x;
236 val = lan_rd(lan966x, QS_INJ_STATUS);
237 if (!(QS_INJ_STATUS_FIFO_RDY_GET(val) & BIT(grp)) ||
238 (QS_INJ_STATUS_WMARK_REACHED_GET(val) & BIT(grp)))
241 /* Write start of frame */
242 lan_wr(QS_INJ_CTRL_GAP_SIZE_SET(1) |
243 QS_INJ_CTRL_SOF_SET(1),
244 lan966x, QS_INJ_CTRL(grp));
246 /* Write IFH header */
247 for (i = 0; i < IFH_LEN; ++i) {
248 /* Wait until the fifo is ready */
249 err = lan966x_port_inj_ready(lan966x, grp);
253 lan_wr((__force u32)ifh[i], lan966x, QS_INJ_WR(grp));
257 count = DIV_ROUND_UP(skb->len, 4);
259 for (i = 0; i < count; ++i) {
260 /* Wait until the fifo is ready */
261 err = lan966x_port_inj_ready(lan966x, grp);
265 lan_wr(((u32 *)skb->data)[i], lan966x, QS_INJ_WR(grp));
269 while (i < (LAN966X_BUFFER_MIN_SZ / 4)) {
270 /* Wait until the fifo is ready */
271 err = lan966x_port_inj_ready(lan966x, grp);
275 lan_wr(0, lan966x, QS_INJ_WR(grp));
279 /* Indicate EOF and valid bytes in the last word */
280 lan_wr(QS_INJ_CTRL_GAP_SIZE_SET(1) |
281 QS_INJ_CTRL_VLD_BYTES_SET(skb->len < LAN966X_BUFFER_MIN_SZ ?
283 QS_INJ_CTRL_EOF_SET(1),
284 lan966x, QS_INJ_CTRL(grp));
287 lan_wr(0, lan966x, QS_INJ_WR(grp));
288 skb_tx_timestamp(skb);
290 dev->stats.tx_packets++;
291 dev->stats.tx_bytes += skb->len;
293 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
294 LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
297 dev_consume_skb_any(skb);
301 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
302 LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
303 lan966x_ptp_txtstamp_release(port, skb);
305 return NETDEV_TX_BUSY;
308 static void lan966x_ifh_set(u8 *ifh, size_t val, size_t pos, size_t length)
313 u8 p = IFH_LEN_BYTES - (pos + i) / 8 - 1;
314 u8 v = val >> i & 0xff;
316 /* There is no need to check for limits of the array, as these
317 * will never be written
319 ifh[p] |= v << ((pos + i) % 8);
320 ifh[p - 1] |= v >> (8 - (pos + i) % 8);
323 } while (i < length);
326 void lan966x_ifh_set_bypass(void *ifh, u64 bypass)
328 lan966x_ifh_set(ifh, bypass, IFH_POS_BYPASS, IFH_WID_BYPASS);
331 void lan966x_ifh_set_port(void *ifh, u64 port)
333 lan966x_ifh_set(ifh, port, IFH_POS_DSTS, IFH_WID_DSTS);
336 static void lan966x_ifh_set_qos_class(void *ifh, u64 qos)
338 lan966x_ifh_set(ifh, qos, IFH_POS_QOS_CLASS, IFH_WID_QOS_CLASS);
341 static void lan966x_ifh_set_ipv(void *ifh, u64 ipv)
343 lan966x_ifh_set(ifh, ipv, IFH_POS_IPV, IFH_WID_IPV);
346 static void lan966x_ifh_set_vid(void *ifh, u64 vid)
348 lan966x_ifh_set(ifh, vid, IFH_POS_TCI, IFH_WID_TCI);
351 static void lan966x_ifh_set_rew_op(void *ifh, u64 rew_op)
353 lan966x_ifh_set(ifh, rew_op, IFH_POS_REW_CMD, IFH_WID_REW_CMD);
356 static void lan966x_ifh_set_timestamp(void *ifh, u64 timestamp)
358 lan966x_ifh_set(ifh, timestamp, IFH_POS_TIMESTAMP, IFH_WID_TIMESTAMP);
361 static netdev_tx_t lan966x_port_xmit(struct sk_buff *skb,
362 struct net_device *dev)
364 struct lan966x_port *port = netdev_priv(dev);
365 struct lan966x *lan966x = port->lan966x;
369 memset(ifh, 0x0, sizeof(__be32) * IFH_LEN);
371 lan966x_ifh_set_bypass(ifh, 1);
372 lan966x_ifh_set_port(ifh, BIT_ULL(port->chip_port));
373 lan966x_ifh_set_qos_class(ifh, skb->priority >= 7 ? 0x7 : skb->priority);
374 lan966x_ifh_set_ipv(ifh, skb->priority >= 7 ? 0x7 : skb->priority);
375 lan966x_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
377 if (port->lan966x->ptp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
378 err = lan966x_ptp_txtstamp_request(port, skb);
382 lan966x_ifh_set_rew_op(ifh, LAN966X_SKB_CB(skb)->rew_op);
383 lan966x_ifh_set_timestamp(ifh, LAN966X_SKB_CB(skb)->ts_id);
386 spin_lock(&lan966x->tx_lock);
387 if (port->lan966x->fdma)
388 err = lan966x_fdma_xmit(skb, ifh, dev);
390 err = lan966x_port_ifh_xmit(skb, ifh, dev);
391 spin_unlock(&lan966x->tx_lock);
396 static int lan966x_port_change_mtu(struct net_device *dev, int new_mtu)
398 struct lan966x_port *port = netdev_priv(dev);
399 struct lan966x *lan966x = port->lan966x;
400 int old_mtu = dev->mtu;
403 lan_wr(DEV_MAC_MAXLEN_CFG_MAX_LEN_SET(LAN966X_HW_MTU(new_mtu)),
404 lan966x, DEV_MAC_MAXLEN_CFG(port->chip_port));
405 WRITE_ONCE(dev->mtu, new_mtu);
410 err = lan966x_fdma_change_mtu(lan966x);
412 lan_wr(DEV_MAC_MAXLEN_CFG_MAX_LEN_SET(LAN966X_HW_MTU(old_mtu)),
413 lan966x, DEV_MAC_MAXLEN_CFG(port->chip_port));
420 static int lan966x_mc_unsync(struct net_device *dev, const unsigned char *addr)
422 struct lan966x_port *port = netdev_priv(dev);
423 struct lan966x *lan966x = port->lan966x;
425 return lan966x_mac_forget(lan966x, addr, HOST_PVID, ENTRYTYPE_LOCKED);
428 static int lan966x_mc_sync(struct net_device *dev, const unsigned char *addr)
430 struct lan966x_port *port = netdev_priv(dev);
431 struct lan966x *lan966x = port->lan966x;
433 return lan966x_mac_cpu_learn(lan966x, addr, HOST_PVID);
436 static void lan966x_port_set_rx_mode(struct net_device *dev)
438 __dev_mc_sync(dev, lan966x_mc_sync, lan966x_mc_unsync);
441 static int lan966x_port_get_parent_id(struct net_device *dev,
442 struct netdev_phys_item_id *ppid)
444 struct lan966x_port *port = netdev_priv(dev);
445 struct lan966x *lan966x = port->lan966x;
447 ppid->id_len = sizeof(lan966x->base_mac);
448 memcpy(&ppid->id, &lan966x->base_mac, ppid->id_len);
453 static int lan966x_port_hwtstamp_get(struct net_device *dev,
454 struct kernel_hwtstamp_config *cfg)
456 struct lan966x_port *port = netdev_priv(dev);
458 if (!port->lan966x->ptp)
461 lan966x_ptp_hwtstamp_get(port, cfg);
466 static int lan966x_port_hwtstamp_set(struct net_device *dev,
467 struct kernel_hwtstamp_config *cfg,
468 struct netlink_ext_ack *extack)
470 struct lan966x_port *port = netdev_priv(dev);
473 if (cfg->source != HWTSTAMP_SOURCE_NETDEV &&
474 cfg->source != HWTSTAMP_SOURCE_PHYLIB)
477 if (cfg->source == HWTSTAMP_SOURCE_NETDEV && !port->lan966x->ptp)
480 err = lan966x_ptp_setup_traps(port, cfg);
484 if (cfg->source == HWTSTAMP_SOURCE_NETDEV) {
485 err = lan966x_ptp_hwtstamp_set(port, cfg, extack);
487 lan966x_ptp_del_traps(port);
495 static const struct net_device_ops lan966x_port_netdev_ops = {
496 .ndo_open = lan966x_port_open,
497 .ndo_stop = lan966x_port_stop,
498 .ndo_start_xmit = lan966x_port_xmit,
499 .ndo_change_mtu = lan966x_port_change_mtu,
500 .ndo_set_rx_mode = lan966x_port_set_rx_mode,
501 .ndo_get_phys_port_name = lan966x_port_get_phys_port_name,
502 .ndo_get_stats64 = lan966x_stats_get,
503 .ndo_set_mac_address = lan966x_port_set_mac_address,
504 .ndo_get_port_parent_id = lan966x_port_get_parent_id,
505 .ndo_eth_ioctl = phy_do_ioctl,
506 .ndo_setup_tc = lan966x_tc_setup,
507 .ndo_bpf = lan966x_xdp,
508 .ndo_xdp_xmit = lan966x_xdp_xmit,
509 .ndo_hwtstamp_get = lan966x_port_hwtstamp_get,
510 .ndo_hwtstamp_set = lan966x_port_hwtstamp_set,
513 bool lan966x_netdevice_check(const struct net_device *dev)
515 return dev->netdev_ops == &lan966x_port_netdev_ops;
518 bool lan966x_hw_offload(struct lan966x *lan966x, u32 port, struct sk_buff *skb)
522 /* The IGMP and MLD frames are not forward by the HW if
523 * multicast snooping is enabled, therefore don't mark as
524 * offload to allow the SW to forward the frames accordingly.
526 val = lan_rd(lan966x, ANA_CPU_FWD_CFG(port));
527 if (!(val & (ANA_CPU_FWD_CFG_IGMP_REDIR_ENA |
528 ANA_CPU_FWD_CFG_MLD_REDIR_ENA)))
531 if (eth_type_vlan(skb->protocol)) {
532 skb = skb_vlan_untag(skb);
537 if (skb->protocol == htons(ETH_P_IP) &&
538 ip_hdr(skb)->protocol == IPPROTO_IGMP)
541 if (IS_ENABLED(CONFIG_IPV6) &&
542 skb->protocol == htons(ETH_P_IPV6) &&
543 ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
544 !ipv6_mc_check_mld(skb))
550 static int lan966x_port_xtr_status(struct lan966x *lan966x, u8 grp)
552 return lan_rd(lan966x, QS_XTR_RD(grp));
555 static int lan966x_port_xtr_ready(struct lan966x *lan966x, u8 grp)
559 return read_poll_timeout(lan966x_port_xtr_status, val,
560 val != XTR_NOT_READY,
561 READL_SLEEP_US, READL_TIMEOUT_US, false,
565 static int lan966x_rx_frame_word(struct lan966x *lan966x, u8 grp, u32 *rval)
571 val = lan_rd(lan966x, QS_XTR_RD(grp));
572 if (val == XTR_NOT_READY) {
573 err = lan966x_port_xtr_ready(lan966x, grp);
586 bytes_valid = XTR_VALID_BYTES(val);
587 val = lan_rd(lan966x, QS_XTR_RD(grp));
588 if (val == XTR_ESCAPE)
589 *rval = lan_rd(lan966x, QS_XTR_RD(grp));
595 *rval = lan_rd(lan966x, QS_XTR_RD(grp));
605 static u64 lan966x_ifh_get(u8 *ifh, size_t pos, size_t length)
610 for (int i = 0; i < length ; i++) {
614 if (i == 0 || k == 0)
615 v = ifh[IFH_LEN_BYTES - (j / 8) - 1];
624 void lan966x_ifh_get_src_port(void *ifh, u64 *src_port)
626 *src_port = lan966x_ifh_get(ifh, IFH_POS_SRCPORT, IFH_WID_SRCPORT);
629 static void lan966x_ifh_get_len(void *ifh, u64 *len)
631 *len = lan966x_ifh_get(ifh, IFH_POS_LEN, IFH_WID_LEN);
634 void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp)
636 *timestamp = lan966x_ifh_get(ifh, IFH_POS_TIMESTAMP, IFH_WID_TIMESTAMP);
639 static irqreturn_t lan966x_xtr_irq_handler(int irq, void *args)
641 struct lan966x *lan966x = args;
642 int i, grp = 0, err = 0;
644 if (!(lan_rd(lan966x, QS_XTR_DATA_PRESENT) & BIT(grp)))
648 u64 src_port, len, timestamp;
649 struct net_device *dev;
656 for (i = 0; i < IFH_LEN; i++) {
657 err = lan966x_rx_frame_word(lan966x, grp, &ifh[i]);
664 lan966x_ifh_get_src_port(ifh, &src_port);
665 lan966x_ifh_get_len(ifh, &len);
666 lan966x_ifh_get_timestamp(ifh, ×tamp);
668 WARN_ON(src_port >= lan966x->num_phys_ports);
670 dev = lan966x->ports[src_port]->dev;
671 skb = netdev_alloc_skb(dev, len);
672 if (unlikely(!skb)) {
673 netdev_err(dev, "Unable to allocate sk_buff\n");
676 buf_len = len - ETH_FCS_LEN;
677 buf = (u32 *)skb_put(skb, buf_len);
681 sz = lan966x_rx_frame_word(lan966x, grp, &val);
689 } while (len < buf_len);
692 sz = lan966x_rx_frame_word(lan966x, grp, &val);
698 /* Update the statistics if part of the FCS was read before */
699 len -= ETH_FCS_LEN - sz;
701 if (unlikely(dev->features & NETIF_F_RXFCS)) {
702 buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
706 lan966x_ptp_rxtstamp(lan966x, skb, src_port, timestamp);
707 skb->protocol = eth_type_trans(skb, dev);
709 if (lan966x->bridge_mask & BIT(src_port)) {
710 skb->offload_fwd_mark = 1;
712 skb_reset_network_header(skb);
713 if (!lan966x_hw_offload(lan966x, src_port, skb))
714 skb->offload_fwd_mark = 0;
717 if (!skb_defer_rx_timestamp(skb))
720 dev->stats.rx_bytes += len;
721 dev->stats.rx_packets++;
725 lan_rd(lan966x, QS_XTR_RD(grp));
727 } while (lan_rd(lan966x, QS_XTR_DATA_PRESENT) & BIT(grp));
732 static irqreturn_t lan966x_ana_irq_handler(int irq, void *args)
734 struct lan966x *lan966x = args;
736 return lan966x_mac_irq_handler(lan966x);
739 static void lan966x_cleanup_ports(struct lan966x *lan966x)
741 struct lan966x_port *port;
744 for (p = 0; p < lan966x->num_phys_ports; p++) {
745 port = lan966x->ports[p];
750 unregister_netdev(port->dev);
752 lan966x_xdp_port_deinit(port);
753 if (lan966x->fdma && lan966x->fdma_ndev == port->dev)
754 lan966x_fdma_netdev_deinit(lan966x, port->dev);
758 lan966x_port_stop(port->dev);
760 phylink_destroy(port->phylink);
761 port->phylink = NULL;
765 fwnode_handle_put(port->fwnode);
768 disable_irq(lan966x->xtr_irq);
769 lan966x->xtr_irq = -ENXIO;
771 if (lan966x->ana_irq > 0) {
772 disable_irq(lan966x->ana_irq);
773 lan966x->ana_irq = -ENXIO;
777 devm_free_irq(lan966x->dev, lan966x->fdma_irq, lan966x);
779 if (lan966x->ptp_irq > 0)
780 devm_free_irq(lan966x->dev, lan966x->ptp_irq, lan966x);
782 if (lan966x->ptp_ext_irq > 0)
783 devm_free_irq(lan966x->dev, lan966x->ptp_ext_irq, lan966x);
786 static int lan966x_probe_port(struct lan966x *lan966x, u32 p,
787 phy_interface_t phy_mode,
788 struct fwnode_handle *portnp)
790 struct lan966x_port *port;
791 struct phylink *phylink;
792 struct net_device *dev;
795 if (p >= lan966x->num_phys_ports)
798 dev = devm_alloc_etherdev_mqs(lan966x->dev,
799 sizeof(struct lan966x_port),
804 SET_NETDEV_DEV(dev, lan966x->dev);
805 port = netdev_priv(dev);
807 port->lan966x = lan966x;
809 lan966x->ports[p] = port;
811 dev->max_mtu = ETH_MAX_MTU;
813 dev->netdev_ops = &lan966x_port_netdev_ops;
814 dev->ethtool_ops = &lan966x_ethtool_ops;
815 dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
816 NETIF_F_HW_VLAN_STAG_TX |
818 dev->hw_features |= NETIF_F_HW_TC;
819 dev->see_all_hwtstamp_requests = true;
820 dev->needed_headroom = IFH_LEN_BYTES;
822 eth_hw_addr_gen(dev, lan966x->base_mac, p + 1);
824 lan966x_mac_learn(lan966x, PGID_CPU, dev->dev_addr, HOST_PVID,
827 port->phylink_config.dev = &port->dev->dev;
828 port->phylink_config.type = PHYLINK_NETDEV;
829 port->phylink_pcs.poll = true;
830 port->phylink_pcs.ops = &lan966x_phylink_pcs_ops;
831 port->phylink_pcs.neg_mode = true;
833 port->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
834 MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD;
836 phy_interface_set_rgmii(port->phylink_config.supported_interfaces);
837 __set_bit(PHY_INTERFACE_MODE_MII,
838 port->phylink_config.supported_interfaces);
839 __set_bit(PHY_INTERFACE_MODE_GMII,
840 port->phylink_config.supported_interfaces);
841 __set_bit(PHY_INTERFACE_MODE_SGMII,
842 port->phylink_config.supported_interfaces);
843 __set_bit(PHY_INTERFACE_MODE_QSGMII,
844 port->phylink_config.supported_interfaces);
845 __set_bit(PHY_INTERFACE_MODE_QUSGMII,
846 port->phylink_config.supported_interfaces);
847 __set_bit(PHY_INTERFACE_MODE_1000BASEX,
848 port->phylink_config.supported_interfaces);
849 __set_bit(PHY_INTERFACE_MODE_2500BASEX,
850 port->phylink_config.supported_interfaces);
852 phylink = phylink_create(&port->phylink_config,
855 &lan966x_phylink_mac_ops);
856 if (IS_ERR(phylink)) {
858 return PTR_ERR(phylink);
861 port->phylink = phylink;
864 dev->xdp_features = NETDEV_XDP_ACT_BASIC |
865 NETDEV_XDP_ACT_REDIRECT |
866 NETDEV_XDP_ACT_NDO_XMIT;
868 err = register_netdev(dev);
870 dev_err(lan966x->dev, "register_netdev failed\n");
874 lan966x_vlan_port_set_vlan_aware(port, 0);
875 lan966x_vlan_port_set_vid(port, HOST_PVID, false, false);
876 lan966x_vlan_port_apply(port);
881 static void lan966x_init(struct lan966x *lan966x)
885 /* MAC table initialization */
886 lan966x_mac_init(lan966x);
888 lan966x_vlan_init(lan966x);
891 lan_wr(lan_rd(lan966x, QS_XTR_FLUSH) |
893 lan966x, QS_XTR_FLUSH);
898 /* All Queues normal */
899 lan_wr(lan_rd(lan966x, QS_XTR_FLUSH) &
901 lan966x, QS_XTR_FLUSH);
903 /* Set MAC age time to default value, the entry is aged after
906 lan_wr(ANA_AUTOAGE_AGE_PERIOD_SET(BR_DEFAULT_AGEING_TIME / 2 / HZ),
907 lan966x, ANA_AUTOAGE);
909 /* Disable learning for frames discarded by VLAN ingress filtering */
910 lan_rmw(ANA_ADVLEARN_VLAN_CHK_SET(1),
911 ANA_ADVLEARN_VLAN_CHK,
912 lan966x, ANA_ADVLEARN);
914 /* Setup frame ageing - "2 sec" - The unit is 6.5 us on lan966x */
915 lan_wr(SYS_FRM_AGING_AGE_TX_ENA_SET(1) |
917 lan966x, SYS_FRM_AGING);
919 /* Map the 8 CPU extraction queues to CPU port */
920 lan_wr(0, lan966x, QSYS_CPU_GROUP_MAP);
922 /* Do byte-swap and expect status after last data word
923 * Extraction: Mode: manual extraction) | Byte_swap
925 lan_wr(QS_XTR_GRP_CFG_MODE_SET(lan966x->fdma ? 2 : 1) |
926 QS_XTR_GRP_CFG_BYTE_SWAP_SET(1),
927 lan966x, QS_XTR_GRP_CFG(0));
929 /* Injection: Mode: manual injection | Byte_swap */
930 lan_wr(QS_INJ_GRP_CFG_MODE_SET(lan966x->fdma ? 2 : 1) |
931 QS_INJ_GRP_CFG_BYTE_SWAP_SET(1),
932 lan966x, QS_INJ_GRP_CFG(0));
934 lan_rmw(QS_INJ_CTRL_GAP_SIZE_SET(0),
935 QS_INJ_CTRL_GAP_SIZE,
936 lan966x, QS_INJ_CTRL(0));
938 /* Enable IFH insertion/parsing on CPU ports */
939 lan_wr(SYS_PORT_MODE_INCL_INJ_HDR_SET(1) |
940 SYS_PORT_MODE_INCL_XTR_HDR_SET(1),
941 lan966x, SYS_PORT_MODE(CPU_PORT));
943 /* Setup flooding PGIDs */
944 lan_wr(ANA_FLOODING_IPMC_FLD_MC4_DATA_SET(PGID_MCIPV4) |
945 ANA_FLOODING_IPMC_FLD_MC4_CTRL_SET(PGID_MC) |
946 ANA_FLOODING_IPMC_FLD_MC6_DATA_SET(PGID_MCIPV6) |
947 ANA_FLOODING_IPMC_FLD_MC6_CTRL_SET(PGID_MC),
948 lan966x, ANA_FLOODING_IPMC);
950 /* There are 8 priorities */
951 for (i = 0; i < 8; ++i)
952 lan_rmw(ANA_FLOODING_FLD_MULTICAST_SET(PGID_MC) |
953 ANA_FLOODING_FLD_UNICAST_SET(PGID_UC) |
954 ANA_FLOODING_FLD_BROADCAST_SET(PGID_BC),
955 ANA_FLOODING_FLD_MULTICAST |
956 ANA_FLOODING_FLD_UNICAST |
957 ANA_FLOODING_FLD_BROADCAST,
958 lan966x, ANA_FLOODING(i));
960 for (i = 0; i < PGID_ENTRIES; ++i)
961 /* Set all the entries to obey VLAN_VLAN */
962 lan_rmw(ANA_PGID_CFG_OBEY_VLAN_SET(1),
963 ANA_PGID_CFG_OBEY_VLAN,
964 lan966x, ANA_PGID_CFG(i));
966 for (p = 0; p < lan966x->num_phys_ports; p++) {
967 /* Disable bridging by default */
968 lan_rmw(ANA_PGID_PGID_SET(0x0),
970 lan966x, ANA_PGID(p + PGID_SRC));
972 /* Do not forward BPDU frames to the front ports and copy them
975 lan_wr(0xffff, lan966x, ANA_CPU_FWD_BPDU_CFG(p));
978 /* Set source buffer size for each priority and each port to 1500 bytes */
979 for (i = 0; i <= QSYS_Q_RSRV; ++i) {
980 lan_wr(1500 / 64, lan966x, QSYS_RES_CFG(i));
981 lan_wr(1500 / 64, lan966x, QSYS_RES_CFG(512 + i));
984 /* Enable switching to/from cpu port */
985 lan_wr(QSYS_SW_PORT_MODE_PORT_ENA_SET(1) |
986 QSYS_SW_PORT_MODE_SCH_NEXT_CFG_SET(1) |
987 QSYS_SW_PORT_MODE_INGRESS_DROP_MODE_SET(1),
988 lan966x, QSYS_SW_PORT_MODE(CPU_PORT));
990 /* Configure and enable the CPU port */
991 lan_rmw(ANA_PGID_PGID_SET(0),
993 lan966x, ANA_PGID(CPU_PORT));
994 lan_rmw(ANA_PGID_PGID_SET(BIT(CPU_PORT)),
996 lan966x, ANA_PGID(PGID_CPU));
998 /* Multicast to all other ports */
999 lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
1001 lan966x, ANA_PGID(PGID_MC));
1003 /* This will be controlled by mrouter ports */
1004 lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
1006 lan966x, ANA_PGID(PGID_MCIPV4));
1008 lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
1010 lan966x, ANA_PGID(PGID_MCIPV6));
1012 /* Unicast to all other ports */
1013 lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
1015 lan966x, ANA_PGID(PGID_UC));
1017 /* Broadcast to the CPU port and to other ports */
1018 lan_rmw(ANA_PGID_PGID_SET(BIT(CPU_PORT) | GENMASK(lan966x->num_phys_ports - 1, 0)),
1020 lan966x, ANA_PGID(PGID_BC));
1022 lan_wr(REW_PORT_CFG_NO_REWRITE_SET(1),
1023 lan966x, REW_PORT_CFG(CPU_PORT));
1025 lan_rmw(ANA_ANAINTR_INTR_ENA_SET(1),
1026 ANA_ANAINTR_INTR_ENA,
1027 lan966x, ANA_ANAINTR);
1029 spin_lock_init(&lan966x->tx_lock);
1031 lan966x_taprio_init(lan966x);
1034 static int lan966x_ram_init(struct lan966x *lan966x)
1036 return lan_rd(lan966x, SYS_RAM_INIT);
1039 static int lan966x_reset_switch(struct lan966x *lan966x)
1041 struct reset_control *switch_reset;
1045 switch_reset = devm_reset_control_get_optional_shared(lan966x->dev,
1047 if (IS_ERR(switch_reset))
1048 return dev_err_probe(lan966x->dev, PTR_ERR(switch_reset),
1049 "Could not obtain switch reset");
1051 reset_control_reset(switch_reset);
1053 /* Don't reinitialize the switch core, if it is already initialized. In
1054 * case it is initialized twice, some pointers inside the queue system
1055 * in HW will get corrupted and then after a while the queue system gets
1056 * full and no traffic is passing through the switch. The issue is seen
1057 * when loading and unloading the driver and sending traffic through the
1060 if (lan_rd(lan966x, SYS_RESET_CFG) & SYS_RESET_CFG_CORE_ENA)
1063 lan_wr(SYS_RESET_CFG_CORE_ENA_SET(0), lan966x, SYS_RESET_CFG);
1064 lan_wr(SYS_RAM_INIT_RAM_INIT_SET(1), lan966x, SYS_RAM_INIT);
1065 ret = readx_poll_timeout(lan966x_ram_init, lan966x,
1066 val, (val & BIT(1)) == 0, READL_SLEEP_US,
1071 lan_wr(SYS_RESET_CFG_CORE_ENA_SET(1), lan966x, SYS_RESET_CFG);
1076 static int lan966x_probe(struct platform_device *pdev)
1078 struct fwnode_handle *ports, *portnp;
1079 struct lan966x *lan966x;
1080 u8 mac_addr[ETH_ALEN];
1083 lan966x = devm_kzalloc(&pdev->dev, sizeof(*lan966x), GFP_KERNEL);
1087 platform_set_drvdata(pdev, lan966x);
1088 lan966x->dev = &pdev->dev;
1090 if (!device_get_mac_address(&pdev->dev, mac_addr)) {
1091 ether_addr_copy(lan966x->base_mac, mac_addr);
1093 pr_info("MAC addr was not set, use random MAC\n");
1094 eth_random_addr(lan966x->base_mac);
1095 lan966x->base_mac[5] &= 0xf0;
1098 err = lan966x_create_targets(pdev, lan966x);
1100 return dev_err_probe(&pdev->dev, err,
1101 "Failed to create targets");
1103 err = lan966x_reset_switch(lan966x);
1105 return dev_err_probe(&pdev->dev, err, "Reset failed");
1107 lan966x->num_phys_ports = NUM_PHYS_PORTS;
1108 lan966x->ports = devm_kcalloc(&pdev->dev, lan966x->num_phys_ports,
1109 sizeof(struct lan966x_port *),
1111 if (!lan966x->ports)
1114 /* There QS system has 32KB of memory */
1115 lan966x->shared_queue_sz = LAN966X_BUFFER_MEMORY;
1118 lan966x->xtr_irq = platform_get_irq_byname(pdev, "xtr");
1119 if (lan966x->xtr_irq < 0)
1120 return lan966x->xtr_irq;
1122 err = devm_request_threaded_irq(&pdev->dev, lan966x->xtr_irq, NULL,
1123 lan966x_xtr_irq_handler, IRQF_ONESHOT,
1124 "frame extraction", lan966x);
1126 pr_err("Unable to use xtr irq");
1130 lan966x->ana_irq = platform_get_irq_byname(pdev, "ana");
1131 if (lan966x->ana_irq > 0) {
1132 err = devm_request_threaded_irq(&pdev->dev, lan966x->ana_irq, NULL,
1133 lan966x_ana_irq_handler, IRQF_ONESHOT,
1134 "ana irq", lan966x);
1136 return dev_err_probe(&pdev->dev, err, "Unable to use ana irq");
1139 lan966x->ptp_irq = platform_get_irq_byname(pdev, "ptp");
1140 if (lan966x->ptp_irq > 0) {
1141 err = devm_request_threaded_irq(&pdev->dev, lan966x->ptp_irq, NULL,
1142 lan966x_ptp_irq_handler, IRQF_ONESHOT,
1143 "ptp irq", lan966x);
1145 return dev_err_probe(&pdev->dev, err, "Unable to use ptp irq");
1150 lan966x->fdma_irq = platform_get_irq_byname(pdev, "fdma");
1151 if (lan966x->fdma_irq > 0) {
1152 err = devm_request_irq(&pdev->dev, lan966x->fdma_irq,
1153 lan966x_fdma_irq_handler, 0,
1154 "fdma irq", lan966x);
1156 return dev_err_probe(&pdev->dev, err, "Unable to use fdma irq");
1158 lan966x->fdma = true;
1162 lan966x->ptp_ext_irq = platform_get_irq_byname(pdev, "ptp-ext");
1163 if (lan966x->ptp_ext_irq > 0) {
1164 err = devm_request_threaded_irq(&pdev->dev,
1165 lan966x->ptp_ext_irq, NULL,
1166 lan966x_ptp_ext_irq_handler,
1168 "ptp-ext irq", lan966x);
1170 return dev_err_probe(&pdev->dev, err,
1171 "Unable to use ptp-ext irq");
1175 ports = device_get_named_child_node(&pdev->dev, "ethernet-ports");
1177 return dev_err_probe(&pdev->dev, -ENODEV,
1178 "no ethernet-ports child found\n");
1180 lan966x->debugfs_root = debugfs_create_dir("lan966x", NULL);
1183 lan966x_init(lan966x);
1184 lan966x_stats_init(lan966x);
1186 /* go over the child nodes */
1187 fwnode_for_each_available_child_node(ports, portnp) {
1188 phy_interface_t phy_mode;
1192 if (fwnode_property_read_u32(portnp, "reg", &p))
1195 phy_mode = fwnode_get_phy_mode(portnp);
1196 err = lan966x_probe_port(lan966x, p, phy_mode, portnp);
1200 /* Read needed configuration */
1201 lan966x->ports[p]->config.portmode = phy_mode;
1202 lan966x->ports[p]->fwnode = fwnode_handle_get(portnp);
1204 serdes = devm_of_phy_optional_get(lan966x->dev,
1205 to_of_node(portnp), NULL);
1206 if (IS_ERR(serdes)) {
1207 err = PTR_ERR(serdes);
1210 lan966x->ports[p]->serdes = serdes;
1212 lan966x_port_init(lan966x->ports[p]);
1213 err = lan966x_xdp_port_init(lan966x->ports[p]);
1218 fwnode_handle_put(ports);
1220 lan966x_mdb_init(lan966x);
1221 err = lan966x_fdb_init(lan966x);
1225 err = lan966x_ptp_init(lan966x);
1229 err = lan966x_fdma_init(lan966x);
1233 err = lan966x_vcap_init(lan966x);
1237 lan966x_dcb_init(lan966x);
1242 lan966x_fdma_deinit(lan966x);
1245 lan966x_ptp_deinit(lan966x);
1248 lan966x_fdb_deinit(lan966x);
1251 fwnode_handle_put(ports);
1252 fwnode_handle_put(portnp);
1254 lan966x_cleanup_ports(lan966x);
1256 cancel_delayed_work_sync(&lan966x->stats_work);
1257 destroy_workqueue(lan966x->stats_queue);
1258 mutex_destroy(&lan966x->stats_lock);
1260 debugfs_remove_recursive(lan966x->debugfs_root);
1265 static void lan966x_remove(struct platform_device *pdev)
1267 struct lan966x *lan966x = platform_get_drvdata(pdev);
1269 lan966x_taprio_deinit(lan966x);
1270 lan966x_vcap_deinit(lan966x);
1271 lan966x_fdma_deinit(lan966x);
1272 lan966x_cleanup_ports(lan966x);
1274 cancel_delayed_work_sync(&lan966x->stats_work);
1275 destroy_workqueue(lan966x->stats_queue);
1276 mutex_destroy(&lan966x->stats_lock);
1278 lan966x_mac_purge_entries(lan966x);
1279 lan966x_mdb_deinit(lan966x);
1280 lan966x_fdb_deinit(lan966x);
1281 lan966x_ptp_deinit(lan966x);
1283 debugfs_remove_recursive(lan966x->debugfs_root);
1286 static struct platform_driver lan966x_driver = {
1287 .probe = lan966x_probe,
1288 .remove = lan966x_remove,
1290 .name = "lan966x-switch",
1291 .of_match_table = lan966x_match,
1295 static int __init lan966x_switch_driver_init(void)
1299 lan966x_register_notifier_blocks();
1301 ret = platform_driver_register(&lan966x_driver);
1308 lan966x_unregister_notifier_blocks();
1312 static void __exit lan966x_switch_driver_exit(void)
1314 platform_driver_unregister(&lan966x_driver);
1315 lan966x_unregister_notifier_blocks();
1318 module_init(lan966x_switch_driver_init);
1319 module_exit(lan966x_switch_driver_exit);
1321 MODULE_DESCRIPTION("Microchip LAN966X switch driver");
1323 MODULE_LICENSE("Dual MIT/GPL");