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