File Index
Files and directories
Sign in
FILE

lan966x_main.c

File preview with syntax highlighting.

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