1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) 2 /* 3 * NXP NETC switch driver 4 * Copyright 2025-2026 NXP 5 */ 6 7 #include <linux/clk.h> 8 #include <linux/etherdevice.h> 9 #include <linux/fsl/enetc_mdio.h> 10 #include <linux/if_bridge.h> 11 #include <linux/if_vlan.h> 12 #include <linux/of_mdio.h> 13 14 #include "netc_switch.h" 15 16 static struct netc_fdb_entry * 17 netc_lookup_fdb_entry(struct netc_switch *priv, 18 const unsigned char *addr, 19 u16 vid) 20 { 21 struct netc_fdb_entry *entry; 22 23 hlist_for_each_entry(entry, &priv->fdb_list, node) 24 if (ether_addr_equal(entry->keye.mac_addr, addr) && 25 le16_to_cpu(entry->keye.fid) == vid) 26 return entry; 27 28 return NULL; 29 } 30 31 static void netc_destroy_fdb_list(struct netc_switch *priv) 32 { 33 struct netc_fdb_entry *entry; 34 struct hlist_node *tmp; 35 36 hlist_for_each_entry_safe(entry, tmp, &priv->fdb_list, node) 37 netc_del_fdb_entry(entry); 38 } 39 40 static struct netc_vlan_entry * 41 netc_lookup_vlan_entry(struct netc_switch *priv, u16 vid) 42 { 43 struct netc_vlan_entry *entry; 44 45 hlist_for_each_entry(entry, &priv->vlan_list, node) 46 if (entry->vid == vid) 47 return entry; 48 49 return NULL; 50 } 51 52 static void netc_destroy_vlan_list(struct netc_switch *priv) 53 { 54 struct netc_vlan_entry *entry; 55 struct hlist_node *tmp; 56 57 hlist_for_each_entry_safe(entry, tmp, &priv->vlan_list, node) 58 netc_del_vlan_entry(entry); 59 } 60 61 static enum dsa_tag_protocol 62 netc_get_tag_protocol(struct dsa_switch *ds, int port, 63 enum dsa_tag_protocol mprot) 64 { 65 return DSA_TAG_PROTO_NETC; 66 } 67 68 static void netc_port_rmw(struct netc_port *np, u32 reg, 69 u32 mask, u32 val) 70 { 71 u32 old, new; 72 73 WARN_ON((mask | val) != mask); 74 75 old = netc_port_rd(np, reg); 76 new = (old & ~mask) | val; 77 if (new == old) 78 return; 79 80 netc_port_wr(np, reg, new); 81 } 82 83 static void netc_mac_port_wr(struct netc_port *np, u32 reg, u32 val) 84 { 85 if (is_netc_pseudo_port(np)) 86 return; 87 88 netc_port_wr(np, reg, val); 89 if (np->caps.pmac) 90 netc_port_wr(np, reg + NETC_PMAC_OFFSET, val); 91 } 92 93 /* netc_mac_port_rmw() is used to synchronize the configurations of eMAC 94 * and pMAC to maintain consistency. This function should not be used if 95 * differentiated settings are required. 96 */ 97 static void netc_mac_port_rmw(struct netc_port *np, u32 reg, 98 u32 mask, u32 val) 99 { 100 u32 old, new; 101 102 if (is_netc_pseudo_port(np)) 103 return; 104 105 WARN_ON((mask | val) != mask); 106 107 old = netc_port_rd(np, reg); 108 new = (old & ~mask) | val; 109 if (new == old) 110 return; 111 112 netc_port_wr(np, reg, new); 113 if (np->caps.pmac) 114 netc_port_wr(np, reg + NETC_PMAC_OFFSET, new); 115 } 116 117 static void netc_port_get_capability(struct netc_port *np) 118 { 119 u32 val; 120 121 val = netc_port_rd(np, NETC_PMCAPR); 122 if (val & PMCAPR_HD) 123 np->caps.half_duplex = true; 124 125 if (FIELD_GET(PMCAPR_FP, val) == FP_SUPPORT) 126 np->caps.pmac = true; 127 128 val = netc_port_rd(np, NETC_PCAPR); 129 if (val & PCAPR_LINK_TYPE) 130 np->caps.pseudo_link = true; 131 } 132 133 static int netc_port_get_info_from_dt(struct netc_port *np, 134 struct device_node *node, 135 struct device *dev) 136 { 137 if (of_find_property(node, "clock-names", NULL)) { 138 np->ref_clk = devm_get_clk_from_child(dev, node, "ref"); 139 if (IS_ERR(np->ref_clk)) { 140 dev_err(dev, "Port %d cannot get reference clock\n", 141 np->dp->index); 142 return PTR_ERR(np->ref_clk); 143 } 144 } 145 146 return 0; 147 } 148 149 static int netc_port_create_emdio_bus(struct netc_port *np, 150 struct device_node *node) 151 { 152 struct netc_switch *priv = np->switch_priv; 153 struct enetc_mdio_priv *mdio_priv; 154 struct device *dev = priv->dev; 155 struct enetc_hw *hw; 156 struct mii_bus *bus; 157 int err; 158 159 hw = enetc_hw_alloc(dev, np->iobase); 160 if (IS_ERR(hw)) 161 return dev_err_probe(dev, PTR_ERR(hw), 162 "Failed to allocate enetc_hw\n"); 163 164 bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv)); 165 if (!bus) 166 return -ENOMEM; 167 168 bus->name = "NXP NETC switch external MDIO Bus"; 169 bus->read = enetc_mdio_read_c22; 170 bus->write = enetc_mdio_write_c22; 171 bus->read_c45 = enetc_mdio_read_c45; 172 bus->write_c45 = enetc_mdio_write_c45; 173 bus->parent = dev; 174 mdio_priv = bus->priv; 175 mdio_priv->hw = hw; 176 mdio_priv->mdio_base = NETC_EMDIO_BASE; 177 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-p%d-emdio", 178 dev_name(dev), np->dp->index); 179 180 err = devm_of_mdiobus_register(dev, bus, node); 181 if (err) 182 return dev_err_probe(dev, err, 183 "Cannot register EMDIO bus\n"); 184 185 np->emdio = bus; 186 187 return 0; 188 } 189 190 static int netc_port_create_mdio_bus(struct netc_port *np, 191 struct device_node *node) 192 { 193 struct device_node *mdio_node; 194 int err; 195 196 mdio_node = of_get_child_by_name(node, "mdio"); 197 if (mdio_node) { 198 err = netc_port_create_emdio_bus(np, mdio_node); 199 of_node_put(mdio_node); 200 if (err) 201 return err; 202 } 203 204 return 0; 205 } 206 207 static int netc_init_switch_id(struct netc_switch *priv) 208 { 209 struct netc_switch_regs *regs = &priv->regs; 210 struct dsa_switch *ds = priv->ds; 211 212 /* The value of 0 is reserved for the VEPA switch and cannot 213 * be used. So 'dsa,member' is a required property for NETC 214 * switch, the member is used to specify the switch ID, which 215 * cannot be zero. This way, the hardware switch ID and the 216 * software switch ID are consistent. 217 */ 218 if (ds->index > FIELD_MAX(SWCR_SWID) || !ds->index) { 219 dev_err(priv->dev, "Switch index %d out of range\n", 220 ds->index); 221 return -ERANGE; 222 } 223 224 netc_base_wr(regs, NETC_SWCR, ds->index); 225 226 return 0; 227 } 228 229 static void netc_get_switch_capabilities(struct netc_switch *priv) 230 { 231 struct netc_switch_regs *regs = &priv->regs; 232 u32 val; 233 234 val = netc_base_rd(regs, NETC_HTMCAPR); 235 priv->htmcapr_num_words = FIELD_GET(HTMCAPR_NUM_WORDS, val); 236 237 val = netc_base_rd(regs, NETC_BPCAPR); 238 priv->num_bp = FIELD_GET(BPCAPR_NUM_BP, val); 239 } 240 241 static int netc_init_all_ports(struct netc_switch *priv) 242 { 243 struct device *dev = priv->dev; 244 struct netc_port *np; 245 struct dsa_port *dp; 246 int ett_offset = 0; 247 int err; 248 249 priv->ports = devm_kcalloc(dev, priv->info->num_ports, 250 sizeof(struct netc_port *), 251 GFP_KERNEL); 252 if (!priv->ports) 253 return -ENOMEM; 254 255 /* Some DSA interfaces may set the port even it is disabled, such 256 * as .port_disable(), .port_stp_state_set() and so on. To avoid 257 * crash caused by accessing NULL port pointer, each port is 258 * allocated its own memory. Otherwise, we need to check whether 259 * the port pointer is NULL in these interfaces. The latter is 260 * difficult for us to cover. 261 */ 262 for (int i = 0; i < priv->info->num_ports; i++) { 263 np = devm_kzalloc(dev, sizeof(*np), GFP_KERNEL); 264 if (!np) 265 return -ENOMEM; 266 267 np->switch_priv = priv; 268 np->iobase = priv->regs.port + PORT_IOBASE(i); 269 netc_port_get_capability(np); 270 priv->ports[i] = np; 271 } 272 273 dsa_switch_for_each_available_port(dp, priv->ds) { 274 np = priv->ports[dp->index]; 275 np->dp = dp; 276 np->ett_offset = ett_offset++; 277 priv->port_bitmap |= BIT(dp->index); 278 279 err = netc_port_get_info_from_dt(np, dp->dn, dev); 280 if (err) 281 return err; 282 283 if (dsa_port_is_user(dp)) { 284 err = netc_port_create_mdio_bus(np, dp->dn); 285 if (err) { 286 dev_err(dev, "Failed to create MDIO bus\n"); 287 return err; 288 } 289 } 290 } 291 292 return 0; 293 } 294 295 static void netc_init_ntmp_tbl_versions(struct netc_switch *priv) 296 { 297 struct ntmp_user *ntmp = &priv->ntmp; 298 299 /* All tables default to version 0 */ 300 memset(&ntmp->tbl, 0, sizeof(ntmp->tbl)); 301 } 302 303 static int netc_init_all_cbdrs(struct netc_switch *priv) 304 { 305 struct netc_switch_regs *regs = &priv->regs; 306 struct ntmp_user *ntmp = &priv->ntmp; 307 int i, err; 308 309 ntmp->cbdr_num = NETC_CBDR_NUM; 310 ntmp->dev = priv->dev; 311 ntmp->ring = devm_kcalloc(ntmp->dev, ntmp->cbdr_num, 312 sizeof(struct netc_cbdr), 313 GFP_KERNEL); 314 if (!ntmp->ring) 315 return -ENOMEM; 316 317 for (i = 0; i < ntmp->cbdr_num; i++) { 318 struct netc_cbdr *cbdr = &ntmp->ring[i]; 319 struct netc_cbdr_regs cbdr_regs; 320 321 cbdr_regs.pir = regs->base + NETC_CBDRPIR(i); 322 cbdr_regs.cir = regs->base + NETC_CBDRCIR(i); 323 cbdr_regs.mr = regs->base + NETC_CBDRMR(i); 324 cbdr_regs.bar0 = regs->base + NETC_CBDRBAR0(i); 325 cbdr_regs.bar1 = regs->base + NETC_CBDRBAR1(i); 326 cbdr_regs.lenr = regs->base + NETC_CBDRLENR(i); 327 328 err = ntmp_init_cbdr(cbdr, ntmp->dev, &cbdr_regs); 329 if (err) 330 goto free_cbdrs; 331 } 332 333 return 0; 334 335 free_cbdrs: 336 for (i--; i >= 0; i--) 337 ntmp_free_cbdr(&ntmp->ring[i]); 338 339 return err; 340 } 341 342 static void netc_remove_all_cbdrs(struct netc_switch *priv) 343 { 344 struct ntmp_user *ntmp = &priv->ntmp; 345 346 for (int i = 0; i < NETC_CBDR_NUM; i++) 347 ntmp_free_cbdr(&ntmp->ring[i]); 348 } 349 350 static u32 netc_num_available_ports(struct netc_switch *priv) 351 { 352 struct dsa_port *dp; 353 u32 num_ports = 0; 354 355 dsa_switch_for_each_available_port(dp, priv->ds) 356 num_ports++; 357 358 return num_ports; 359 } 360 361 static int netc_init_ntmp_bitmap_sizes(struct netc_switch *priv) 362 { 363 u32 num_ports = netc_num_available_ports(priv); 364 struct netc_switch_regs *regs = &priv->regs; 365 struct ntmp_user *ntmp = &priv->ntmp; 366 u32 val; 367 368 if (!num_ports) 369 return -EINVAL; 370 371 val = netc_base_rd(regs, NETC_ETTCAPR); 372 ntmp->ett_bitmap_size = NETC_GET_NUM_ENTRIES(val) / num_ports; 373 if (!ntmp->ett_bitmap_size) 374 return -EINVAL; 375 376 val = netc_base_rd(regs, NETC_ECTCAPR); 377 ntmp->ect_bitmap_size = NETC_GET_NUM_ENTRIES(val) / num_ports; 378 if (!ntmp->ect_bitmap_size) 379 return -EINVAL; 380 381 return 0; 382 } 383 384 static int netc_init_ntmp_bitmaps(struct netc_switch *priv) 385 { 386 struct ntmp_user *ntmp = &priv->ntmp; 387 388 ntmp->ett_gid_bitmap = bitmap_zalloc(ntmp->ett_bitmap_size, 389 GFP_KERNEL); 390 if (!ntmp->ett_gid_bitmap) 391 return -ENOMEM; 392 393 ntmp->ect_gid_bitmap = bitmap_zalloc(ntmp->ect_bitmap_size, 394 GFP_KERNEL); 395 if (!ntmp->ect_gid_bitmap) 396 goto free_ett_gid_bitmap; 397 398 return 0; 399 400 free_ett_gid_bitmap: 401 bitmap_free(ntmp->ett_gid_bitmap); 402 ntmp->ett_gid_bitmap = NULL; 403 404 return -ENOMEM; 405 } 406 407 static void netc_free_ntmp_bitmaps(struct netc_switch *priv) 408 { 409 struct ntmp_user *ntmp = &priv->ntmp; 410 411 bitmap_free(ntmp->ect_gid_bitmap); 412 ntmp->ect_gid_bitmap = NULL; 413 414 bitmap_free(ntmp->ett_gid_bitmap); 415 ntmp->ett_gid_bitmap = NULL; 416 } 417 418 static int netc_init_ntmp_user(struct netc_switch *priv) 419 { 420 int err; 421 422 netc_init_ntmp_tbl_versions(priv); 423 424 err = netc_init_ntmp_bitmap_sizes(priv); 425 if (err) 426 return err; 427 428 err = netc_init_ntmp_bitmaps(priv); 429 if (err) 430 return err; 431 432 err = netc_init_all_cbdrs(priv); 433 if (err) 434 goto free_ntmp_bitmaps; 435 436 return 0; 437 438 free_ntmp_bitmaps: 439 netc_free_ntmp_bitmaps(priv); 440 441 return err; 442 } 443 444 static void netc_free_ntmp_user(struct netc_switch *priv) 445 { 446 netc_remove_all_cbdrs(priv); 447 netc_free_ntmp_bitmaps(priv); 448 } 449 450 static void netc_clean_fdbt_ageing_entries(struct work_struct *work) 451 { 452 struct delayed_work *dwork = to_delayed_work(work); 453 struct netc_switch *priv; 454 455 priv = container_of(dwork, struct netc_switch, fdbt_ageing_work); 456 457 /* Update the activity element in FDB table */ 458 mutex_lock(&priv->fdbt_lock); 459 ntmp_fdbt_update_activity_element(&priv->ntmp); 460 /* Delete the ageing entries after the activity element is updated */ 461 ntmp_fdbt_delete_ageing_entries(&priv->ntmp, NETC_FDBT_AGEING_THRESH); 462 mutex_unlock(&priv->fdbt_lock); 463 464 if (atomic_read(&priv->br_cnt)) 465 schedule_delayed_work(&priv->fdbt_ageing_work, 466 READ_ONCE(priv->fdbt_ageing_delay)); 467 } 468 469 static void netc_switch_dos_default_config(struct netc_switch *priv) 470 { 471 struct netc_switch_regs *regs = &priv->regs; 472 u32 val; 473 474 val = DOSL2CR_SAMEADDR | DOSL2CR_MSAMCC; 475 netc_base_wr(regs, NETC_DOSL2CR, val); 476 477 val = DOSL3CR_SAMEADDR | DOSL3CR_IPSAMCC; 478 netc_base_wr(regs, NETC_DOSL3CR, val); 479 } 480 481 static void netc_switch_vfht_default_config(struct netc_switch *priv) 482 { 483 struct netc_switch_regs *regs = &priv->regs; 484 u32 val; 485 486 val = netc_base_rd(regs, NETC_VFHTDECR2); 487 488 /* If no match is found in the VLAN Filter table, then VFHTDECR2[MLO] 489 * will take effect. VFHTDECR2[MLO] is set to "Software MAC learning 490 * secure" by default. Notice BPCR[MLO] will override VFHTDECR2[MLO] 491 * if its value is not zero. 492 */ 493 val = u32_replace_bits(val, MLO_SW_SEC, VFHTDECR2_MLO); 494 val = u32_replace_bits(val, MFO_NO_MATCH_DISCARD, VFHTDECR2_MFO); 495 netc_base_wr(regs, NETC_VFHTDECR2, val); 496 } 497 498 static void netc_port_set_max_frame_size(struct netc_port *np, 499 u32 max_frame_size) 500 { 501 netc_mac_port_wr(np, NETC_PM_MAXFRM(0), 502 max_frame_size & PM_MAXFRAM); 503 } 504 505 static void netc_switch_fixed_config(struct netc_switch *priv) 506 { 507 netc_switch_dos_default_config(priv); 508 netc_switch_vfht_default_config(priv); 509 } 510 511 static void netc_port_set_tc_max_sdu(struct netc_port *np, 512 int tc, u32 max_sdu) 513 { 514 u32 val = FIELD_PREP(PTCTMSDUR_MAXSDU, max_sdu) | 515 FIELD_PREP(PTCTMSDUR_SDU_TYPE, SDU_TYPE_MPDU); 516 517 netc_port_wr(np, NETC_PTCTMSDUR(tc), val); 518 } 519 520 static void netc_port_set_all_tc_msdu(struct netc_port *np) 521 { 522 for (int tc = 0; tc < NETC_TC_NUM; tc++) 523 netc_port_set_tc_max_sdu(np, tc, NETC_MAX_FRAME_LEN); 524 } 525 526 static void netc_port_set_mlo(struct netc_port *np, enum netc_mlo mlo) 527 { 528 netc_port_rmw(np, NETC_BPCR, BPCR_MLO, FIELD_PREP(BPCR_MLO, mlo)); 529 } 530 531 static void netc_port_set_pvid(struct netc_port *np, u16 pvid) 532 { 533 netc_port_rmw(np, NETC_BPDVR, BPDVR_VID, pvid); 534 } 535 536 static void netc_port_set_vlan_aware(struct netc_port *np, bool aware) 537 { 538 netc_port_rmw(np, NETC_BPDVR, BPDVR_RXVAM, 539 aware ? 0 : BPDVR_RXVAM); 540 } 541 542 static void netc_port_fixed_config(struct netc_port *np) 543 { 544 /* Default IPV and DR setting */ 545 netc_port_rmw(np, NETC_PQOSMR, PQOSMR_VS | PQOSMR_VE, 546 PQOSMR_VS | PQOSMR_VE); 547 548 /* Enable L2 and L3 DOS */ 549 netc_port_rmw(np, NETC_PCR, PCR_L2DOSE | PCR_L3DOSE, 550 PCR_L2DOSE | PCR_L3DOSE); 551 552 /* Set the quanta value of TX PAUSE frame */ 553 netc_mac_port_wr(np, NETC_PM_PAUSE_QUANTA(0), NETC_PAUSE_QUANTA); 554 555 /* When a quanta timer counts down and reaches this value, 556 * the MAC sends a refresh PAUSE frame with the programmed 557 * full quanta value if a pause condition still exists. 558 */ 559 netc_mac_port_wr(np, NETC_PM_PAUSE_THRESH(0), NETC_PAUSE_THRESH); 560 } 561 562 static void netc_port_default_config(struct netc_port *np) 563 { 564 netc_port_fixed_config(np); 565 566 /* Default VLAN unaware */ 567 netc_port_set_vlan_aware(np, false); 568 569 if (dsa_port_is_cpu(np->dp)) 570 /* For CPU port, source port pruning is disabled */ 571 netc_port_rmw(np, NETC_BPCR, BPCR_SRCPRND, BPCR_SRCPRND); 572 else 573 netc_port_set_mlo(np, MLO_DISABLE); 574 575 netc_port_set_max_frame_size(np, NETC_MAX_FRAME_LEN); 576 netc_port_set_all_tc_msdu(np); 577 } 578 579 static u32 netc_available_port_bitmap(struct netc_switch *priv) 580 { 581 struct dsa_port *dp; 582 u32 bitmap = 0; 583 584 dsa_switch_for_each_available_port(dp, priv->ds) 585 bitmap |= BIT(dp->index); 586 587 return bitmap; 588 } 589 590 static int netc_add_standalone_vlan_entry(struct netc_switch *priv) 591 { 592 u32 bitmap_stg = VFT_STG_ID(0) | netc_available_port_bitmap(priv); 593 struct vft_cfge_data *cfge; 594 u16 cfg; 595 int err; 596 597 cfge = kzalloc_obj(*cfge); 598 if (!cfge) 599 return -ENOMEM; 600 601 cfge->bitmap_stg = cpu_to_le32(bitmap_stg); 602 cfge->et_eid = cpu_to_le32(NTMP_NULL_ENTRY_ID); 603 cfge->fid = cpu_to_le16(NETC_STANDALONE_PVID); 604 605 /* For standalone ports, MAC learning needs to be disabled, so frames 606 * from other user ports will not be forwarded to the standalone ports, 607 * because there are no FDB entries on the standalone ports. Also, the 608 * frames received by the standalone ports cannot be flooded to other 609 * ports, so MAC forwarding option needs to be set to 610 * MFO_NO_MATCH_DISCARD, so the frames will be discarded rather than 611 * flooding to other ports. 612 */ 613 cfg = FIELD_PREP(VFT_MLO, MLO_DISABLE) | 614 FIELD_PREP(VFT_MFO, MFO_NO_MATCH_DISCARD); 615 cfge->cfg = cpu_to_le16(cfg); 616 617 err = ntmp_vft_add_entry(&priv->ntmp, NETC_STANDALONE_PVID, cfge); 618 if (err) 619 dev_err(priv->dev, 620 "Failed to add standalone VLAN entry\n"); 621 622 kfree(cfge); 623 624 return err; 625 } 626 627 static int netc_port_add_fdb_entry(struct netc_port *np, 628 const unsigned char *addr, u16 vid) 629 { 630 struct netc_switch *priv = np->switch_priv; 631 struct netc_fdb_entry *entry; 632 struct fdbt_keye_data *keye; 633 struct fdbt_cfge_data *cfge; 634 int port = np->dp->index; 635 u32 cfg = 0; 636 int err; 637 638 entry = kzalloc_obj(*entry); 639 if (!entry) 640 return -ENOMEM; 641 642 keye = &entry->keye; 643 cfge = &entry->cfge; 644 ether_addr_copy(keye->mac_addr, addr); 645 keye->fid = cpu_to_le16(vid); 646 647 cfge->port_bitmap = cpu_to_le32(BIT(port)); 648 cfge->cfg = cpu_to_le32(cfg); 649 cfge->et_eid = cpu_to_le32(NTMP_NULL_ENTRY_ID); 650 651 err = ntmp_fdbt_add_entry(&priv->ntmp, &entry->entry_id, keye, cfge); 652 if (err) { 653 kfree(entry); 654 655 return err; 656 } 657 658 netc_add_fdb_entry(priv, entry); 659 660 return 0; 661 } 662 663 static int netc_port_set_fdb_entry(struct netc_port *np, 664 const unsigned char *addr, u16 vid) 665 { 666 struct netc_switch *priv = np->switch_priv; 667 struct netc_fdb_entry *entry; 668 struct fdbt_cfge_data *cfge; 669 int port = np->dp->index; 670 __le32 old_port_bitmap; 671 int err = 0; 672 673 mutex_lock(&priv->fdbt_lock); 674 675 entry = netc_lookup_fdb_entry(priv, addr, vid); 676 if (!entry) { 677 err = netc_port_add_fdb_entry(np, addr, vid); 678 if (err) 679 dev_err(priv->dev, 680 "Failed to add FDB entry on port %d\n", 681 port); 682 683 goto unlock_fdbt; 684 } 685 686 cfge = &entry->cfge; 687 /* If the entry already exists on the port, return 0 directly */ 688 if (unlikely(cfge->port_bitmap & cpu_to_le32(BIT(port)))) 689 goto unlock_fdbt; 690 691 /* If the entry already exists, but not on this port, we need to 692 * update the port bitmap. In general, it should only be valid 693 * for multicast or broadcast address. 694 */ 695 old_port_bitmap = cfge->port_bitmap; 696 if (is_multicast_ether_addr(addr)) 697 cfge->port_bitmap |= cpu_to_le32(BIT(port)); 698 else 699 cfge->port_bitmap = cpu_to_le32(BIT(port)); 700 701 err = ntmp_fdbt_update_entry(&priv->ntmp, entry->entry_id, cfge); 702 if (err) { 703 cfge->port_bitmap = old_port_bitmap; 704 dev_err(priv->dev, "Failed to set FDB entry on port %d\n", 705 port); 706 } 707 708 unlock_fdbt: 709 mutex_unlock(&priv->fdbt_lock); 710 711 return err; 712 } 713 714 static int netc_port_del_fdb_entry(struct netc_port *np, 715 const unsigned char *addr, u16 vid) 716 { 717 struct netc_switch *priv = np->switch_priv; 718 struct ntmp_user *ntmp = &priv->ntmp; 719 struct netc_fdb_entry *entry; 720 struct fdbt_cfge_data *cfge; 721 int port = np->dp->index; 722 int err = 0; 723 724 mutex_lock(&priv->fdbt_lock); 725 726 entry = netc_lookup_fdb_entry(priv, addr, vid); 727 if (unlikely(!entry)) 728 /* The hardware-learned dynamic FDB entries cannot be deleted 729 * through .port_fdb_del() interface. 730 * For NTF_MASTER path: Since hardware-learned dynamic FDB 731 * entries are never synchronized back to the bridge software 732 * database. br_fdb_delete() -> br_fdb_find() cannot find the 733 * FDB entry, so .port_fdb_del() will not be called. 734 * For NTF_SELF path: dsa_user_netdev_ops does not implement 735 * ndo_fdb_del(), so rtnl_fdb_del() falls back to 736 * ndo_dflt_fdb_del(), which only supports NUD_PERMANENT static 737 * entries and rejects all others with -EINVAL. 738 */ 739 goto unlock_fdbt; 740 741 cfge = &entry->cfge; 742 if (unlikely(!(cfge->port_bitmap & cpu_to_le32(BIT(port))))) 743 goto unlock_fdbt; 744 745 if (cfge->port_bitmap != cpu_to_le32(BIT(port))) { 746 /* If the entry also exists on other ports, we need to 747 * update the entry in the FDB table. 748 */ 749 cfge->port_bitmap &= cpu_to_le32(~BIT(port)); 750 err = ntmp_fdbt_update_entry(ntmp, entry->entry_id, cfge); 751 if (err) { 752 cfge->port_bitmap |= cpu_to_le32(BIT(port)); 753 goto unlock_fdbt; 754 } 755 } else { 756 /* If the entry only exists on this port, just delete 757 * it from the FDB table. 758 */ 759 err = ntmp_fdbt_delete_entry(ntmp, entry->entry_id); 760 if (err) 761 goto unlock_fdbt; 762 763 netc_del_fdb_entry(entry); 764 } 765 766 unlock_fdbt: 767 mutex_unlock(&priv->fdbt_lock); 768 769 return err; 770 } 771 772 static int netc_add_standalone_fdb_bcast_entry(struct netc_switch *priv) 773 { 774 const u8 bcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 775 struct dsa_port *dp, *cpu_dp = NULL; 776 777 dsa_switch_for_each_cpu_port(dp, priv->ds) { 778 /* The switch has only one CPU port, so only need to find 779 * the first CPU port to break out of the loop. 780 */ 781 cpu_dp = dp; 782 break; 783 } 784 785 if (!cpu_dp) 786 return -ENODEV; 787 788 /* If the user port acts as a standalone port, then its PVID is 0, 789 * MLO is set to "disable MAC learning" and MFO is set to "discard 790 * frames if no matching entry found in FDB table". Therefore, we 791 * need to add a broadcast FDB entry on the CPU port so that the 792 * broadcast frames received on the user port can be forwarded to 793 * the CPU port. 794 */ 795 return netc_port_set_fdb_entry(NETC_PORT(priv->ds, cpu_dp->index), 796 bcast, NETC_STANDALONE_PVID); 797 } 798 799 static void netc_port_set_pbpmcr(struct netc_port *np, u64 mapping) 800 { 801 u32 pbpmcr0 = lower_32_bits(mapping); 802 u32 pbpmcr1 = upper_32_bits(mapping); 803 804 netc_port_wr(np, NETC_PBPMCR0, pbpmcr0); 805 netc_port_wr(np, NETC_PBPMCR1, pbpmcr1); 806 } 807 808 static void netc_ipv_to_buffer_pool_mapping(struct netc_switch *priv) 809 { 810 int bp_per_port = priv->num_bp / priv->info->num_ports; 811 int q = NETC_IPV_NUM / bp_per_port; 812 int r = NETC_IPV_NUM % bp_per_port; 813 int num = q + r; 814 815 /* IPV-to-buffer-pool mapping per port: 816 * Each port is allocated 'bp_per_port' buffer pools and supports 8 817 * IPVs, where a higher IPV indicates a higher frame priority. Each 818 * IPV can be mapped to only one buffer pool, from hardware design 819 * perspective, bp_per_port will not be greater than 8. So 'q' will 820 * not be 0. 821 * 822 * The mapping rule is as follows: 823 * - The first 'num' IPVs share the port's first buffer pool (index 824 * 'base_id'). 825 * - After that, every 'q' IPVs share one buffer pool, with pool 826 * indices increasing sequentially. 827 */ 828 for (int i = 0; i < priv->info->num_ports; i++) { 829 u32 base_id = i * bp_per_port; 830 u32 bp_id = base_id; 831 u64 mapping = 0; 832 833 for (int ipv = 0; ipv < NETC_IPV_NUM; ipv++) { 834 /* Update the buffer pool index */ 835 if (ipv >= num) 836 bp_id = base_id + ((ipv - num) / q) + 1; 837 838 mapping |= (u64)bp_id << (ipv * 8); 839 } 840 841 netc_port_set_pbpmcr(priv->ports[i], mapping); 842 } 843 } 844 845 static int netc_switch_bpt_default_config(struct netc_switch *priv) 846 { 847 if (priv->num_bp < priv->info->num_ports) 848 return -EINVAL; 849 850 priv->bpt_list = devm_kcalloc(priv->dev, priv->num_bp, 851 sizeof(struct bpt_cfge_data), 852 GFP_KERNEL); 853 if (!priv->bpt_list) 854 return -ENOMEM; 855 856 /* Initialize the maximum threshold of each buffer pool entry */ 857 for (int i = 0; i < priv->num_bp; i++) { 858 struct bpt_cfge_data *cfge = &priv->bpt_list[i]; 859 int err; 860 861 cfge->max_thresh = cpu_to_le16(NETC_BP_THRESH); 862 err = ntmp_bpt_update_entry(&priv->ntmp, i, cfge); 863 if (err) 864 return err; 865 } 866 867 netc_ipv_to_buffer_pool_mapping(priv); 868 869 return 0; 870 } 871 872 static int netc_setup(struct dsa_switch *ds) 873 { 874 struct netc_switch *priv = ds->priv; 875 struct dsa_port *dp; 876 int err; 877 878 err = netc_init_switch_id(priv); 879 if (err) 880 return err; 881 882 netc_get_switch_capabilities(priv); 883 884 err = netc_init_all_ports(priv); 885 if (err) 886 return err; 887 888 err = netc_init_ntmp_user(priv); 889 if (err) 890 return err; 891 892 INIT_HLIST_HEAD(&priv->fdb_list); 893 mutex_init(&priv->fdbt_lock); 894 priv->fdbt_ageing_delay = NETC_FDBT_AGEING_DELAY; 895 atomic_set(&priv->br_cnt, 0); 896 INIT_DELAYED_WORK(&priv->fdbt_ageing_work, 897 netc_clean_fdbt_ageing_entries); 898 INIT_HLIST_HEAD(&priv->vlan_list); 899 mutex_init(&priv->vft_lock); 900 901 netc_switch_fixed_config(priv); 902 903 /* default setting for ports */ 904 dsa_switch_for_each_available_port(dp, ds) 905 netc_port_default_config(priv->ports[dp->index]); 906 907 err = netc_switch_bpt_default_config(priv); 908 if (err) 909 goto free_lock_and_ntmp_user; 910 911 err = netc_add_standalone_vlan_entry(priv); 912 if (err) 913 goto free_lock_and_ntmp_user; 914 915 err = netc_add_standalone_fdb_bcast_entry(priv); 916 if (err) 917 goto free_lock_and_ntmp_user; 918 919 return 0; 920 921 free_lock_and_ntmp_user: 922 /* No need to clear the hardware state, netc_setup() is only called 923 * when the driver is bound, and FLR will be performed to reset the 924 * hardware state. 925 */ 926 mutex_destroy(&priv->fdbt_lock); 927 mutex_destroy(&priv->vft_lock); 928 netc_free_ntmp_user(priv); 929 930 return err; 931 } 932 933 static void netc_destroy_all_lists(struct netc_switch *priv) 934 { 935 netc_destroy_fdb_list(priv); 936 mutex_destroy(&priv->fdbt_lock); 937 netc_destroy_vlan_list(priv); 938 mutex_destroy(&priv->vft_lock); 939 } 940 941 static void netc_free_host_flood_rules(struct netc_switch *priv) 942 { 943 struct dsa_port *dp; 944 945 dsa_switch_for_each_user_port(dp, priv->ds) { 946 struct netc_port *np = priv->ports[dp->index]; 947 948 /* No need to clear the hardware IPFT entry. Because PCIe 949 * FLR will be performed when the switch is re-registered, 950 * it will reset hardware state. So only need to free the 951 * memory to avoid memory leak. 952 */ 953 kfree(np->host_flood); 954 np->host_flood = NULL; 955 } 956 } 957 958 static void netc_teardown(struct dsa_switch *ds) 959 { 960 struct netc_switch *priv = ds->priv; 961 962 disable_delayed_work_sync(&priv->fdbt_ageing_work); 963 netc_destroy_all_lists(priv); 964 netc_free_host_flood_rules(priv); 965 netc_free_ntmp_user(priv); 966 } 967 968 static bool netc_port_is_emdio_consumer(struct device_node *node) 969 { 970 struct device_node *mdio_node; 971 972 /* If the port node has phy-handle property and it does 973 * not contain a mdio child node, then the port is the 974 * EMDIO consumer. 975 */ 976 mdio_node = of_get_child_by_name(node, "mdio"); 977 if (!mdio_node) 978 return true; 979 980 of_node_put(mdio_node); 981 982 return false; 983 } 984 985 /* Currently, phylink_of_phy_connect() is called by dsa_user_create(), 986 * so if the switch uses the external MDIO controller (like the EMDIO 987 * function) to manage the external PHYs. The MDIO bus may not be 988 * created when phylink_of_phy_connect() is called, so it will return 989 * an error and cause the switch driver to fail to probe. 990 * This workaround can be removed when DSA phylink_of_phy_connect() 991 * calls are moved from probe() to ndo_open(). 992 */ 993 static int netc_switch_check_emdio_is_ready(struct device *dev) 994 { 995 struct device_node *ports, *phy_node; 996 struct phy_device *phydev; 997 int err = 0; 998 999 ports = of_get_child_by_name(dev->of_node, "ethernet-ports"); 1000 if (!ports) { 1001 dev_err(dev, "Cannot find the ethernet-ports node\n"); 1002 return -EINVAL; 1003 } 1004 1005 for_each_available_child_of_node_scoped(ports, child) { 1006 /* If the node does not have phy-handle property, then the 1007 * port does not connect to a PHY, so the port is not the 1008 * EMDIO consumer. 1009 */ 1010 phy_node = of_parse_phandle(child, "phy-handle", 0); 1011 if (!phy_node) 1012 continue; 1013 1014 /* Note that from the hardware perspective, the switch ports 1015 * do not support sharing the MDIO bus defined under one port. 1016 * Each port can only access its own external PHY through its 1017 * port MDIO bus. 1018 */ 1019 if (!netc_port_is_emdio_consumer(child)) { 1020 of_node_put(phy_node); 1021 continue; 1022 } 1023 1024 phydev = of_phy_find_device(phy_node); 1025 of_node_put(phy_node); 1026 if (!phydev) { 1027 err = -EPROBE_DEFER; 1028 goto out; 1029 } 1030 1031 put_device(&phydev->mdio.dev); 1032 } 1033 1034 out: 1035 of_node_put(ports); 1036 1037 return err; 1038 } 1039 1040 static int netc_switch_pci_init(struct pci_dev *pdev) 1041 { 1042 struct device *dev = &pdev->dev; 1043 struct netc_switch_regs *regs; 1044 struct netc_switch *priv; 1045 void __iomem *base; 1046 int err; 1047 1048 pcie_flr(pdev); 1049 err = pcim_enable_device(pdev); 1050 if (err) 1051 return dev_err_probe(dev, err, "Failed to enable device\n"); 1052 1053 err = pcim_request_all_regions(pdev, KBUILD_MODNAME); 1054 if (err) 1055 return dev_err_probe(dev, err, "Failed to request regions\n"); 1056 1057 /* The command BD rings and NTMP tables need DMA. No need to check 1058 * the return value, because it never returns fail when the mask is 1059 * DMA_BIT_MASK(64), see dma-api-howto.rst. 1060 */ 1061 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 1062 1063 if (pci_resource_len(pdev, NETC_REGS_BAR) < NETC_REGS_SIZE) { 1064 return dev_err_probe(dev, -EINVAL, 1065 "Invalid register space size\n"); 1066 } 1067 1068 base = pcim_iomap(pdev, NETC_REGS_BAR, 0); 1069 if (!base) 1070 return dev_err_probe(dev, -ENXIO, "pcim_iomap() failed\n"); 1071 1072 pci_set_master(pdev); 1073 1074 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 1075 if (!priv) 1076 return -ENOMEM; 1077 1078 priv->pdev = pdev; 1079 priv->dev = dev; 1080 1081 regs = &priv->regs; 1082 regs->base = base; 1083 regs->port = regs->base + NETC_REGS_PORT_BASE; 1084 regs->global = regs->base + NETC_REGS_GLOBAL_BASE; 1085 pci_set_drvdata(pdev, priv); 1086 1087 return 0; 1088 } 1089 1090 static void netc_switch_get_ip_revision(struct netc_switch *priv) 1091 { 1092 struct netc_switch_regs *regs = &priv->regs; 1093 u32 val = netc_glb_rd(regs, NETC_IPBRR0); 1094 1095 priv->revision = FIELD_GET(IPBRR0_IP_REV, val); 1096 } 1097 1098 static void netc_init_ett_cfge(struct ett_cfge_data *cfge, 1099 bool untagged, u32 ect_eid) 1100 { 1101 u32 vuda_sqta = FMTEID_VUDA_SQTA; 1102 u16 efm_cfg = 0; 1103 1104 if (ect_eid != NTMP_NULL_ENTRY_ID) { 1105 /* Increase egress frame counter */ 1106 efm_cfg |= FIELD_PREP(ETT_ECA, ETT_ECA_INC); 1107 cfge->ec_eid = cpu_to_le32(ect_eid); 1108 } 1109 1110 /* If egress rule is VLAN untagged */ 1111 if (untagged) { 1112 /* delete outer VLAN tag */ 1113 vuda_sqta |= FIELD_PREP(FMTEID_VUDA, FMTEID_VUDA_DEL_OTAG); 1114 /* length change: twos-complement notation */ 1115 efm_cfg |= FIELD_PREP(ETT_EFM_LEN_CHANGE, 1116 ETT_FRM_LEN_DEL_VLAN); 1117 } 1118 1119 cfge->efm_eid = cpu_to_le32(vuda_sqta); 1120 cfge->efm_cfg = cpu_to_le16(efm_cfg); 1121 } 1122 1123 static int netc_add_ett_entry(struct netc_switch *priv, bool untagged, 1124 u32 ett_eid, u32 ect_eid) 1125 { 1126 struct ntmp_user *ntmp = &priv->ntmp; 1127 struct ett_cfge_data cfge = {}; 1128 1129 netc_init_ett_cfge(&cfge, untagged, ect_eid); 1130 1131 return ntmp_ett_add_entry(ntmp, ett_eid, &cfge); 1132 } 1133 1134 static int netc_update_ett_entry(struct netc_switch *priv, bool untagged, 1135 u32 ett_eid, u32 ect_eid) 1136 { 1137 struct ntmp_user *ntmp = &priv->ntmp; 1138 struct ett_cfge_data cfge = {}; 1139 1140 netc_init_ett_cfge(&cfge, untagged, ect_eid); 1141 1142 return ntmp_ett_update_entry(ntmp, ett_eid, &cfge); 1143 } 1144 1145 static int netc_add_ett_group_entries(struct netc_switch *priv, 1146 u32 untagged_port_bitmap, 1147 u32 ett_base_eid, 1148 u32 ect_base_eid) 1149 { 1150 struct netc_port **ports = priv->ports; 1151 u32 ett_eid, ect_eid; 1152 bool untagged; 1153 int i, err; 1154 1155 for (i = 0; i < priv->info->num_ports; i++) { 1156 if (!ports[i]->dp) 1157 continue; 1158 1159 untagged = !!(untagged_port_bitmap & BIT(i)); 1160 ett_eid = ett_base_eid + ports[i]->ett_offset; 1161 ect_eid = NTMP_NULL_ENTRY_ID; 1162 if (ect_base_eid != NTMP_NULL_ENTRY_ID) 1163 ect_eid = ect_base_eid + ports[i]->ett_offset; 1164 1165 err = netc_add_ett_entry(priv, untagged, ett_eid, ect_eid); 1166 if (err) 1167 goto clear_ett_entries; 1168 } 1169 1170 return 0; 1171 1172 clear_ett_entries: 1173 while (--i >= 0) { 1174 if (!ports[i]->dp) 1175 continue; 1176 1177 ett_eid = ett_base_eid + ports[i]->ett_offset; 1178 ntmp_ett_delete_entry(&priv->ntmp, ett_eid); 1179 } 1180 1181 return err; 1182 } 1183 1184 static int netc_add_vlan_egress_rule(struct netc_switch *priv, 1185 struct netc_vlan_entry *entry) 1186 { 1187 u32 num_ports = netc_num_available_ports(priv); 1188 struct ntmp_user *ntmp = &priv->ntmp; 1189 u32 ect_eid = NTMP_NULL_ENTRY_ID; 1190 u32 ett_eid, ett_gid, ect_gid; 1191 int err; 1192 1193 /* Step 1: Find available egress counter table entries and update 1194 * these entries. 1195 */ 1196 ect_gid = ntmp_lookup_free_eid(ntmp->ect_gid_bitmap, 1197 ntmp->ect_bitmap_size); 1198 if (ect_gid == NTMP_NULL_ENTRY_ID) { 1199 dev_info(priv->dev, 1200 "No egress counter table entries available\n"); 1201 } else { 1202 ect_eid = ect_gid * num_ports; 1203 for (int i = 0; i < num_ports; i++) 1204 /* There is no need to check the return value, the only 1205 * issue is that the entry's counter might be inaccurate, 1206 * but it will not affect the functionality, it is only 1207 * for future debugging. 1208 */ 1209 ntmp_ect_update_entry(ntmp, ect_eid + i); 1210 } 1211 1212 /* Step 2: Find available egress treatment table entries and add 1213 * these entries. 1214 */ 1215 ett_gid = ntmp_lookup_free_eid(ntmp->ett_gid_bitmap, 1216 ntmp->ett_bitmap_size); 1217 if (ett_gid == NTMP_NULL_ENTRY_ID) { 1218 dev_err(priv->dev, 1219 "No egress treatment table entries available\n"); 1220 err = -ENOSPC; 1221 goto clear_ect_gid; 1222 } 1223 1224 ett_eid = ett_gid * num_ports; 1225 err = netc_add_ett_group_entries(priv, entry->untagged_port_bitmap, 1226 ett_eid, ect_eid); 1227 if (err) 1228 goto clear_ett_gid; 1229 1230 entry->cfge.et_eid = cpu_to_le32(ett_eid); 1231 entry->ect_gid = ect_gid; 1232 1233 return 0; 1234 1235 clear_ett_gid: 1236 ntmp_clear_eid_bitmap(ntmp->ett_gid_bitmap, ett_gid); 1237 1238 clear_ect_gid: 1239 if (ect_gid != NTMP_NULL_ENTRY_ID) 1240 ntmp_clear_eid_bitmap(ntmp->ect_gid_bitmap, ect_gid); 1241 1242 return err; 1243 } 1244 1245 static void netc_delete_vlan_egress_rule(struct netc_switch *priv, 1246 struct netc_vlan_entry *entry) 1247 { 1248 u32 num_ports = netc_num_available_ports(priv); 1249 struct ntmp_user *ntmp = &priv->ntmp; 1250 u32 ett_eid, ett_gid; 1251 1252 ett_eid = le32_to_cpu(entry->cfge.et_eid); 1253 if (ett_eid == NTMP_NULL_ENTRY_ID) 1254 return; 1255 1256 ett_gid = ett_eid / num_ports; 1257 ntmp_clear_eid_bitmap(ntmp->ett_gid_bitmap, ett_gid); 1258 for (int i = 0; i < num_ports; i++) 1259 ntmp_ett_delete_entry(ntmp, ett_eid + i); 1260 1261 if (entry->ect_gid == NTMP_NULL_ENTRY_ID) 1262 return; 1263 1264 ntmp_clear_eid_bitmap(ntmp->ect_gid_bitmap, entry->ect_gid); 1265 } 1266 1267 static int netc_port_update_vlan_egress_rule(struct netc_port *np, 1268 struct netc_vlan_entry *entry) 1269 { 1270 bool untagged = !!(entry->untagged_port_bitmap & BIT(np->dp->index)); 1271 u32 num_ports = netc_num_available_ports(np->switch_priv); 1272 u32 ett_eid = le32_to_cpu(entry->cfge.et_eid); 1273 struct netc_switch *priv = np->switch_priv; 1274 u32 ect_eid = NTMP_NULL_ENTRY_ID; 1275 int err; 1276 1277 if (ett_eid == NTMP_NULL_ENTRY_ID) 1278 return 0; 1279 1280 if (entry->ect_gid != NTMP_NULL_ENTRY_ID) 1281 /* Each ETT entry maps to an ECT entry if ect_gid is not NULL 1282 * entry ID. The offset of the ECT entry corresponding to the 1283 * port in the group is equal to ett_offset. 1284 */ 1285 ect_eid = entry->ect_gid * num_ports + np->ett_offset; 1286 1287 ett_eid += np->ett_offset; 1288 err = netc_update_ett_entry(priv, untagged, ett_eid, ect_eid); 1289 if (err) { 1290 dev_err(priv->dev, 1291 "Failed to update VLAN %u egress rule on port %d\n", 1292 entry->vid, np->dp->index); 1293 return err; 1294 } 1295 1296 if (ect_eid != NTMP_NULL_ENTRY_ID) 1297 ntmp_ect_update_entry(&priv->ntmp, ect_eid); 1298 1299 return 0; 1300 } 1301 1302 static int netc_port_add_vlan_entry(struct netc_port *np, u16 vid, 1303 bool untagged) 1304 { 1305 struct netc_switch *priv = np->switch_priv; 1306 struct netc_vlan_entry *entry; 1307 struct vft_cfge_data *cfge; 1308 u32 index = np->dp->index; 1309 u32 bitmap_stg; 1310 int err; 1311 u16 cfg; 1312 1313 entry = kzalloc_obj(*entry); 1314 if (!entry) 1315 return -ENOMEM; 1316 1317 entry->vid = vid; 1318 entry->ect_gid = NTMP_NULL_ENTRY_ID; 1319 1320 bitmap_stg = BIT(index) | VFT_STG_ID(0); 1321 /* If the VID is a VLAN-unaware PVID, the CPU port needs to be 1322 * a member of this VLAN. 1323 */ 1324 if (dsa_port_is_user(np->dp) && 1325 vid >= NETC_VLAN_UNAWARE_PVID(priv->ds->max_num_bridges)) { 1326 struct dsa_port *cpu_dp = np->dp->cpu_dp; 1327 1328 bitmap_stg |= BIT(cpu_dp->index); 1329 } 1330 1331 cfg = FIELD_PREP(VFT_MLO, MLO_HW) | 1332 FIELD_PREP(VFT_MFO, MFO_NO_MATCH_FLOOD); 1333 1334 cfge = &entry->cfge; 1335 cfge->et_eid = cpu_to_le32(NTMP_NULL_ENTRY_ID); 1336 cfge->bitmap_stg = cpu_to_le32(bitmap_stg); 1337 cfge->fid = cpu_to_le16(vid); 1338 cfge->cfg = cpu_to_le16(cfg); 1339 cfge->eta_port_bitmap = cpu_to_le32(priv->port_bitmap); 1340 1341 if (untagged) 1342 entry->untagged_port_bitmap = BIT(index); 1343 1344 err = netc_add_vlan_egress_rule(priv, entry); 1345 if (err) 1346 goto free_vlan_entry; 1347 1348 err = ntmp_vft_add_entry(&priv->ntmp, vid, cfge); 1349 if (err) { 1350 dev_err(priv->dev, 1351 "Failed to add VLAN %u entry on port %d\n", 1352 vid, index); 1353 goto delete_vlan_egress_rule; 1354 } 1355 1356 netc_add_vlan_entry(priv, entry); 1357 1358 return 0; 1359 1360 delete_vlan_egress_rule: 1361 netc_delete_vlan_egress_rule(priv, entry); 1362 free_vlan_entry: 1363 kfree(entry); 1364 1365 return err; 1366 } 1367 1368 static bool netc_port_vlan_egress_rule_changed(struct netc_switch *priv, 1369 struct netc_vlan_entry *entry, 1370 int port, bool untagged) 1371 { 1372 bool old_untagged = !!(entry->untagged_port_bitmap & BIT(port)); 1373 1374 /* VLAN-unaware VIDs have no egress rules, so return 'false' */ 1375 if (entry->vid >= NETC_VLAN_UNAWARE_PVID(priv->ds->max_num_bridges)) 1376 return false; 1377 1378 return old_untagged != untagged; 1379 } 1380 1381 static int netc_port_set_vlan_entry(struct netc_port *np, u16 vid, 1382 bool untagged) 1383 { 1384 struct netc_switch *priv = np->switch_priv; 1385 struct netc_vlan_entry *entry; 1386 struct vft_cfge_data *cfge; 1387 int port = np->dp->index; 1388 bool changed; 1389 int err = 0; 1390 1391 mutex_lock(&priv->vft_lock); 1392 1393 entry = netc_lookup_vlan_entry(priv, vid); 1394 if (!entry) { 1395 err = netc_port_add_vlan_entry(np, vid, untagged); 1396 goto unlock_vft; 1397 } 1398 1399 /* Check whether the egress VLAN rule is changed */ 1400 changed = netc_port_vlan_egress_rule_changed(priv, entry, port, 1401 untagged); 1402 if (changed) { 1403 entry->untagged_port_bitmap ^= BIT(port); 1404 err = netc_port_update_vlan_egress_rule(np, entry); 1405 if (err) { 1406 entry->untagged_port_bitmap ^= BIT(port); 1407 goto unlock_vft; 1408 } 1409 } 1410 1411 cfge = &entry->cfge; 1412 if (cfge->bitmap_stg & cpu_to_le32(BIT(port))) 1413 goto unlock_vft; 1414 1415 cfge->bitmap_stg |= cpu_to_le32(BIT(port)); 1416 err = ntmp_vft_update_entry(&priv->ntmp, vid, cfge); 1417 if (err) { 1418 dev_err(priv->dev, 1419 "Failed to update VLAN %u entry on port %d\n", 1420 vid, port); 1421 1422 goto restore_bitmap_stg; 1423 } 1424 1425 mutex_unlock(&priv->vft_lock); 1426 1427 return 0; 1428 1429 restore_bitmap_stg: 1430 cfge->bitmap_stg &= cpu_to_le32(~BIT(port)); 1431 if (changed) { 1432 entry->untagged_port_bitmap ^= BIT(port); 1433 /* Recover the corresponding ETT entry. It doesn't matter 1434 * if it fails because the bit corresponding to the port 1435 * in the port bitmap of the VFT entry is not set. so the 1436 * frame will not match that ETT entry. 1437 */ 1438 if (netc_port_update_vlan_egress_rule(np, entry)) 1439 entry->untagged_port_bitmap ^= BIT(port); 1440 } 1441 unlock_vft: 1442 mutex_unlock(&priv->vft_lock); 1443 1444 return err; 1445 } 1446 1447 static int netc_port_del_vlan_entry(struct netc_port *np, u16 vid) 1448 { 1449 struct netc_switch *priv = np->switch_priv; 1450 struct netc_vlan_entry *entry; 1451 struct vft_cfge_data *cfge; 1452 int port = np->dp->index; 1453 u32 vlan_port_bitmap; 1454 int err = 0; 1455 1456 mutex_lock(&priv->vft_lock); 1457 1458 entry = netc_lookup_vlan_entry(priv, vid); 1459 if (!entry) 1460 goto unlock_vft; 1461 1462 cfge = &entry->cfge; 1463 vlan_port_bitmap = FIELD_GET(VFT_PORT_MEMBERSHIP, 1464 le32_to_cpu(cfge->bitmap_stg)); 1465 /* If the VID is a VLAN-unaware PVID, we need to clear the CPU 1466 * port bit of vlan_port_bitmap, so that the VLAN entry can be 1467 * deleted if no user ports use this VLAN. 1468 */ 1469 if (dsa_port_is_user(np->dp) && 1470 vid >= NETC_VLAN_UNAWARE_PVID(priv->ds->max_num_bridges)) { 1471 struct dsa_port *cpu_dp = np->dp->cpu_dp; 1472 1473 vlan_port_bitmap &= ~BIT(cpu_dp->index); 1474 } 1475 1476 /* If the VLAN only belongs to the current port */ 1477 if (vlan_port_bitmap == BIT(port)) { 1478 err = ntmp_vft_delete_entry(&priv->ntmp, vid); 1479 if (err) 1480 goto unlock_vft; 1481 1482 netc_delete_vlan_egress_rule(priv, entry); 1483 netc_del_vlan_entry(entry); 1484 1485 goto unlock_vft; 1486 } 1487 1488 if (!(vlan_port_bitmap & BIT(port))) 1489 goto unlock_vft; 1490 1491 cfge->bitmap_stg &= cpu_to_le32(~BIT(port)); 1492 err = ntmp_vft_update_entry(&priv->ntmp, vid, cfge); 1493 if (err) { 1494 cfge->bitmap_stg |= cpu_to_le32(BIT(port)); 1495 goto unlock_vft; 1496 } 1497 1498 unlock_vft: 1499 mutex_unlock(&priv->vft_lock); 1500 1501 return err; 1502 } 1503 1504 static int netc_port_enable(struct dsa_switch *ds, int port, 1505 struct phy_device *phy) 1506 { 1507 struct netc_port *np = NETC_PORT(ds, port); 1508 int err; 1509 1510 if (np->enable) 1511 return 0; 1512 1513 err = clk_prepare_enable(np->ref_clk); 1514 if (err) { 1515 dev_err(ds->dev, 1516 "Failed to enable enet_ref_clk of port %d\n", port); 1517 return err; 1518 } 1519 1520 np->enable = true; 1521 1522 return 0; 1523 } 1524 1525 static void netc_port_disable(struct dsa_switch *ds, int port) 1526 { 1527 struct netc_port *np = NETC_PORT(ds, port); 1528 1529 /* When .port_disable() is called, .port_enable() may not have been 1530 * called. In this case, both the prepare_count and enable_count of 1531 * clock are 0. Calling clk_disable_unprepare() at this time will 1532 * cause warnings. 1533 */ 1534 if (!np->enable) 1535 return; 1536 1537 clk_disable_unprepare(np->ref_clk); 1538 np->enable = false; 1539 } 1540 1541 static void netc_port_stp_state_set(struct dsa_switch *ds, 1542 int port, u8 state) 1543 { 1544 struct netc_port *np = NETC_PORT(ds, port); 1545 u32 val; 1546 1547 switch (state) { 1548 case BR_STATE_DISABLED: 1549 case BR_STATE_LISTENING: 1550 case BR_STATE_BLOCKING: 1551 val = NETC_STG_STATE_DISABLED; 1552 break; 1553 case BR_STATE_LEARNING: 1554 val = NETC_STG_STATE_LEARNING; 1555 break; 1556 case BR_STATE_FORWARDING: 1557 val = NETC_STG_STATE_FORWARDING; 1558 break; 1559 default: 1560 return; 1561 } 1562 1563 netc_port_wr(np, NETC_BPSTGSR, val); 1564 } 1565 1566 static int netc_port_change_mtu(struct dsa_switch *ds, 1567 int port, int mtu) 1568 { 1569 u32 max_frame_size = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; 1570 1571 netc_port_set_max_frame_size(NETC_PORT(ds, port), max_frame_size); 1572 1573 return 0; 1574 } 1575 1576 static int netc_port_max_mtu(struct dsa_switch *ds, int port) 1577 { 1578 return NETC_MAX_FRAME_LEN - VLAN_ETH_HLEN - ETH_FCS_LEN; 1579 } 1580 1581 static struct net_device *netc_classify_db(struct dsa_db db) 1582 { 1583 switch (db.type) { 1584 case DSA_DB_PORT: 1585 return NULL; 1586 case DSA_DB_BRIDGE: 1587 return db.bridge.dev; 1588 default: 1589 return ERR_PTR(-EOPNOTSUPP); 1590 } 1591 } 1592 1593 static u16 netc_vlan_unaware_pvid(struct dsa_bridge *bridge) 1594 { 1595 u32 br_num; 1596 1597 if (!bridge) 1598 return NETC_STANDALONE_PVID; 1599 1600 br_num = bridge->num; 1601 1602 /* The br_num is supposed to be 1 ~ ds->max_num_bridges, see 1603 * dsa_bridge_num_get(). Since max_num_bridges is non-zero, 1604 * so dsa_port_bridge_create() will return an error if 1605 * dsa_bridge_num_get() returns 0. 1606 */ 1607 if (WARN_ON(!br_num)) 1608 return NETC_STANDALONE_PVID; 1609 1610 return NETC_VLAN_UNAWARE_PVID(br_num); 1611 } 1612 1613 static int netc_port_fdb_add(struct dsa_switch *ds, int port, 1614 const unsigned char *addr, u16 vid, 1615 struct dsa_db db) 1616 { 1617 struct net_device *br_ndev = netc_classify_db(db); 1618 struct netc_port *np = NETC_PORT(ds, port); 1619 1620 if (IS_ERR(br_ndev)) 1621 return PTR_ERR(br_ndev); 1622 1623 if (!vid) 1624 vid = netc_vlan_unaware_pvid(br_ndev ? &db.bridge : NULL); 1625 1626 return netc_port_set_fdb_entry(np, addr, vid); 1627 } 1628 1629 static int netc_port_fdb_del(struct dsa_switch *ds, int port, 1630 const unsigned char *addr, u16 vid, 1631 struct dsa_db db) 1632 { 1633 struct net_device *br_ndev = netc_classify_db(db); 1634 struct netc_port *np = NETC_PORT(ds, port); 1635 1636 if (IS_ERR(br_ndev)) 1637 return PTR_ERR(br_ndev); 1638 1639 if (!vid) 1640 vid = netc_vlan_unaware_pvid(br_ndev ? &db.bridge : NULL); 1641 1642 return netc_port_del_fdb_entry(np, addr, vid); 1643 } 1644 1645 static int netc_port_fdb_dump(struct dsa_switch *ds, int port, 1646 dsa_fdb_dump_cb_t *cb, void *data) 1647 { 1648 struct netc_switch *priv = ds->priv; 1649 u32 resume_eid = NTMP_NULL_ENTRY_ID; 1650 struct fdbt_entry_data *entry; 1651 struct fdbt_keye_data *keye; 1652 struct fdbt_cfge_data *cfge; 1653 u32 cfg, cnt = 0; 1654 bool is_static; 1655 int err; 1656 u16 vid; 1657 1658 entry = kmalloc_obj(*entry); 1659 if (!entry) 1660 return -ENOMEM; 1661 1662 keye = &entry->keye; 1663 cfge = &entry->cfge; 1664 mutex_lock(&priv->fdbt_lock); 1665 1666 do { 1667 memset(entry, 0, sizeof(*entry)); 1668 err = ntmp_fdbt_search_port_entry(&priv->ntmp, port, 1669 &resume_eid, entry); 1670 if (err || entry->entry_id == NTMP_NULL_ENTRY_ID) 1671 break; 1672 1673 cfg = le32_to_cpu(cfge->cfg); 1674 is_static = (cfg & FDBT_DYNAMIC) ? false : true; 1675 vid = le16_to_cpu(keye->fid); 1676 if (vid >= NETC_VLAN_UNAWARE_PVID(ds->max_num_bridges)) 1677 vid = 0; 1678 1679 err = cb(keye->mac_addr, vid, is_static, data); 1680 if (err) 1681 break; 1682 1683 /* To prevent hardware malfunctions from causing an 1684 * infinite loop. 1685 */ 1686 if (++cnt >= priv->htmcapr_num_words) 1687 break; 1688 } while (resume_eid != NTMP_NULL_ENTRY_ID); 1689 1690 mutex_unlock(&priv->fdbt_lock); 1691 kfree(entry); 1692 1693 return err; 1694 } 1695 1696 static int netc_port_mdb_add(struct dsa_switch *ds, int port, 1697 const struct switchdev_obj_port_mdb *mdb, 1698 struct dsa_db db) 1699 { 1700 return netc_port_fdb_add(ds, port, mdb->addr, mdb->vid, db); 1701 } 1702 1703 static int netc_port_mdb_del(struct dsa_switch *ds, int port, 1704 const struct switchdev_obj_port_mdb *mdb, 1705 struct dsa_db db) 1706 { 1707 return netc_port_fdb_del(ds, port, mdb->addr, mdb->vid, db); 1708 } 1709 1710 static int netc_port_add_host_flood_rule(struct netc_port *np, 1711 bool uc, bool mc) 1712 { 1713 const u8 dmac_mask[ETH_ALEN] = {0x1, 0, 0, 0, 0, 0}; 1714 struct netc_switch *priv = np->switch_priv; 1715 struct ipft_entry_data *host_flood; 1716 struct ipft_keye_data *keye; 1717 struct ipft_cfge_data *cfge; 1718 u16 src_port; 1719 u32 cfg; 1720 int err; 1721 1722 if (!uc && !mc) { 1723 /* Disable ingress port filter table lookup */ 1724 netc_port_wr(np, NETC_PIPFCR, 0); 1725 np->uc = false; 1726 np->mc = false; 1727 1728 return 0; 1729 } 1730 1731 host_flood = kzalloc_obj(*host_flood); 1732 if (!host_flood) 1733 return -ENOMEM; 1734 1735 keye = &host_flood->keye; 1736 cfge = &host_flood->cfge; 1737 1738 src_port = FIELD_PREP(IPFT_SRC_PORT, np->dp->index); 1739 src_port |= IPFT_SRC_PORT_MASK; 1740 keye->src_port = cpu_to_le16(src_port); 1741 1742 /* If either only unicast or only multicast need to be flooded 1743 * to the host, we always set the mask that tests the first MAC 1744 * DA octet. The value should be 0 for the first bit (if unicast 1745 * has to be flooded) or 1 (if multicast). If both unicast and 1746 * multicast have to be flooded, we leave the key mask empty, so 1747 * it matches everything. 1748 */ 1749 if (uc && !mc) 1750 ether_addr_copy(keye->dmac_mask, dmac_mask); 1751 1752 if (!uc && mc) { 1753 ether_addr_copy(keye->dmac, dmac_mask); 1754 ether_addr_copy(keye->dmac_mask, dmac_mask); 1755 } 1756 1757 cfg = FIELD_PREP(IPFT_FLTFA, IPFT_FLTFA_REDIRECT); 1758 cfg |= FIELD_PREP(IPFT_HR, NETC_HR_HOST_FLOOD); 1759 cfge->cfg = cpu_to_le32(cfg); 1760 1761 err = ntmp_ipft_add_entry(&priv->ntmp, host_flood); 1762 if (err) { 1763 kfree(host_flood); 1764 return err; 1765 } 1766 1767 np->uc = uc; 1768 np->mc = mc; 1769 np->host_flood = host_flood; 1770 /* Enable ingress port filter table lookup */ 1771 netc_port_wr(np, NETC_PIPFCR, PIPFCR_EN); 1772 1773 return 0; 1774 } 1775 1776 static void netc_port_remove_host_flood(struct netc_port *np, 1777 struct ipft_entry_data *host_flood) 1778 { 1779 struct netc_switch *priv = np->switch_priv; 1780 bool disable_host_flood = false; 1781 1782 if (!host_flood) 1783 return; 1784 1785 if (np->host_flood == host_flood) 1786 disable_host_flood = true; 1787 1788 ntmp_ipft_delete_entry(&priv->ntmp, host_flood->entry_id); 1789 kfree(host_flood); 1790 1791 if (disable_host_flood) { 1792 np->host_flood = NULL; 1793 np->uc = false; 1794 np->mc = false; 1795 netc_port_wr(np, NETC_PIPFCR, 0); 1796 } 1797 } 1798 1799 static void netc_port_set_host_flood(struct dsa_switch *ds, int port, 1800 bool uc, bool mc) 1801 { 1802 struct netc_port *np = NETC_PORT(ds, port); 1803 struct ipft_entry_data *old_host_flood; 1804 1805 /* Do not add host flood rule to ingress port filter table when 1806 * the port has joined a bridge. Otherwise, the ingress frames 1807 * will bypass FDB table lookup and MAC learning, so the frames 1808 * will be redirected directly to the CPU port. 1809 */ 1810 if (dsa_port_bridge_dev_get(np->dp)) { 1811 netc_port_remove_host_flood(np, np->host_flood); 1812 1813 return; 1814 } 1815 1816 if (np->uc == uc && np->mc == mc) 1817 return; 1818 1819 /* IPFT does not support in-place updates to the KEYE element, 1820 * we need to add a new entry and then delete the old one. So 1821 * save the old entry first. 1822 */ 1823 old_host_flood = np->host_flood; 1824 np->host_flood = NULL; 1825 1826 if (netc_port_add_host_flood_rule(np, uc, mc)) { 1827 np->host_flood = old_host_flood; 1828 dev_err(ds->dev, "Failed to add host flood rule on port %d\n", 1829 port); 1830 return; 1831 } 1832 1833 /* Remove the old host flood entry */ 1834 netc_port_remove_host_flood(np, old_host_flood); 1835 } 1836 1837 static int netc_single_vlan_aware_bridge(struct dsa_switch *ds, 1838 struct netlink_ext_ack *extack) 1839 { 1840 struct net_device *br_ndev = NULL; 1841 struct dsa_port *dp; 1842 1843 dsa_switch_for_each_available_port(dp, ds) { 1844 struct net_device *port_br = dsa_port_bridge_dev_get(dp); 1845 1846 if (!port_br || !br_vlan_enabled(port_br)) 1847 continue; 1848 1849 if (!br_ndev) { 1850 br_ndev = port_br; 1851 continue; 1852 } 1853 1854 if (br_ndev == port_br) 1855 continue; 1856 1857 NL_SET_ERR_MSG_MOD(extack, 1858 "Only one VLAN-aware bridge is supported"); 1859 1860 return -EBUSY; 1861 } 1862 1863 return 0; 1864 } 1865 1866 static int netc_port_vlan_filtering(struct dsa_switch *ds, 1867 int port, bool vlan_aware, 1868 struct netlink_ext_ack *extack) 1869 { 1870 struct netc_port *np = NETC_PORT(ds, port); 1871 u16 pvid; 1872 int err; 1873 1874 /* Before calling port_vlan_filtering(), br_vlan_filter_toggle() has 1875 * already updated the BROPT_VLAN_ENABLED bit of br->options. So the 1876 * VLAN filtering status of the switch ports can be checked by the 1877 * br_vlan_enabled() function. 1878 */ 1879 err = netc_single_vlan_aware_bridge(ds, extack); 1880 if (err) 1881 return err; 1882 1883 pvid = netc_vlan_unaware_pvid(np->dp->bridge); 1884 if (pvid == NETC_STANDALONE_PVID) { 1885 vlan_aware = false; 1886 goto bpdvr_config; 1887 } 1888 1889 if (vlan_aware) { 1890 /* The FDB entries associated with unaware_pvid do not need 1891 * to be deleted, so that when switching from VLAN-aware to 1892 * VLAN-unaware mode, these FDB entries do not need to be 1893 * re-added. 1894 */ 1895 err = netc_port_del_vlan_entry(np, pvid); 1896 if (err) 1897 return err; 1898 1899 pvid = np->pvid; 1900 } else { 1901 err = netc_port_set_vlan_entry(np, pvid, false); 1902 if (err) 1903 return err; 1904 } 1905 1906 bpdvr_config: 1907 netc_port_set_vlan_aware(np, vlan_aware); 1908 netc_port_set_pvid(np, pvid); 1909 1910 return 0; 1911 } 1912 1913 static int netc_port_vlan_add(struct dsa_switch *ds, int port, 1914 const struct switchdev_obj_port_vlan *vlan, 1915 struct netlink_ext_ack *extack) 1916 { 1917 struct netc_port *np = NETC_PORT(ds, port); 1918 struct dsa_port *dp = np->dp; 1919 bool untagged; 1920 int err; 1921 1922 /* The 8021q layer may attempt to change NETC_STANDALONE_PVID 1923 * (VID 0), so we need to ignore it. 1924 */ 1925 if (vlan->vid == NETC_STANDALONE_PVID) 1926 return 0; 1927 1928 if (vlan->vid >= NETC_VLAN_UNAWARE_PVID(ds->max_num_bridges)) { 1929 NL_SET_ERR_MSG_FMT_MOD(extack, 1930 "VID %d~4095 reserved for VLAN-unaware bridge", 1931 NETC_VLAN_UNAWARE_PVID(ds->max_num_bridges)); 1932 return -EINVAL; 1933 } 1934 1935 untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED); 1936 err = netc_port_set_vlan_entry(np, vlan->vid, untagged); 1937 if (err) 1938 return err; 1939 1940 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) { 1941 np->pvid = vlan->vid; 1942 if (dsa_port_is_vlan_filtering(dp)) 1943 netc_port_set_pvid(np, vlan->vid); 1944 1945 return 0; 1946 } 1947 1948 if (np->pvid != vlan->vid) 1949 return 0; 1950 1951 /* Delete PVID */ 1952 np->pvid = NETC_STANDALONE_PVID; 1953 if (dsa_port_is_vlan_filtering(dp)) 1954 netc_port_set_pvid(np, NETC_STANDALONE_PVID); 1955 1956 return 0; 1957 } 1958 1959 static int netc_port_vlan_del(struct dsa_switch *ds, int port, 1960 const struct switchdev_obj_port_vlan *vlan) 1961 { 1962 struct netc_port *np = NETC_PORT(ds, port); 1963 int err; 1964 1965 if (vlan->vid == NETC_STANDALONE_PVID) 1966 return 0; 1967 1968 if (vlan->vid >= NETC_VLAN_UNAWARE_PVID(ds->max_num_bridges)) 1969 return -EINVAL; 1970 1971 err = netc_port_del_vlan_entry(np, vlan->vid); 1972 if (err) 1973 return err; 1974 1975 if (np->pvid == vlan->vid) { 1976 np->pvid = NETC_STANDALONE_PVID; 1977 1978 /* Set the port PVID to NETC_STANDALONE_PVID if the VLAN-aware 1979 * bridge port has no PVID. The untagged frames will not be 1980 * forwarded to other user ports, as NETC_STANDALONE_PVID VLAN 1981 * entry has disabled MAC learning and flooding, and other user 1982 * ports do not have FDB entries with NETC_STANDALONE_PVID. 1983 */ 1984 if (dsa_port_is_vlan_filtering(np->dp)) 1985 netc_port_set_pvid(np, NETC_STANDALONE_PVID); 1986 } 1987 1988 return 0; 1989 } 1990 1991 static int netc_port_bridge_join(struct dsa_switch *ds, int port, 1992 struct dsa_bridge bridge, 1993 bool *tx_fwd_offload, 1994 struct netlink_ext_ack *extack) 1995 { 1996 struct netc_port *np = NETC_PORT(ds, port); 1997 struct netc_switch *priv = ds->priv; 1998 u16 vlan_unaware_pvid; 1999 int err; 2000 2001 if (!bridge.num) { 2002 NL_SET_ERR_MSG_MOD(extack, "Bridge number 0 is unsupported"); 2003 return -EINVAL; 2004 } 2005 2006 err = netc_single_vlan_aware_bridge(ds, extack); 2007 if (err) 2008 return err; 2009 2010 netc_port_set_mlo(np, MLO_NOT_OVERRIDE); 2011 2012 if (br_vlan_enabled(bridge.dev)) 2013 goto out; 2014 2015 vlan_unaware_pvid = NETC_VLAN_UNAWARE_PVID(bridge.num); 2016 err = netc_port_set_vlan_entry(np, vlan_unaware_pvid, false); 2017 if (err) 2018 goto disable_mlo; 2019 2020 netc_port_set_pvid(np, vlan_unaware_pvid); 2021 2022 out: 2023 netc_port_remove_host_flood(np, np->host_flood); 2024 2025 if (atomic_inc_return(&priv->br_cnt) == 1) 2026 schedule_delayed_work(&priv->fdbt_ageing_work, 2027 READ_ONCE(priv->fdbt_ageing_delay)); 2028 2029 return 0; 2030 2031 disable_mlo: 2032 netc_port_set_mlo(np, MLO_DISABLE); 2033 2034 return err; 2035 } 2036 2037 static void netc_port_remove_dynamic_entries(struct netc_port *np) 2038 { 2039 struct netc_switch *priv = np->switch_priv; 2040 2041 /* Return if the port is not available */ 2042 if (!np->dp) 2043 return; 2044 2045 mutex_lock(&priv->fdbt_lock); 2046 ntmp_fdbt_delete_port_dynamic_entries(&priv->ntmp, np->dp->index); 2047 mutex_unlock(&priv->fdbt_lock); 2048 } 2049 2050 static void netc_port_bridge_leave(struct dsa_switch *ds, int port, 2051 struct dsa_bridge bridge) 2052 { 2053 struct netc_port *np = NETC_PORT(ds, port); 2054 struct net_device *ndev = np->dp->user; 2055 struct netc_switch *priv = ds->priv; 2056 u16 vlan_unaware_pvid; 2057 bool mc, uc; 2058 2059 netc_port_set_mlo(np, MLO_DISABLE); 2060 netc_port_set_pvid(np, NETC_STANDALONE_PVID); 2061 np->pvid = NETC_STANDALONE_PVID; 2062 2063 if (atomic_dec_and_test(&priv->br_cnt)) 2064 cancel_delayed_work_sync(&priv->fdbt_ageing_work); 2065 2066 netc_port_remove_dynamic_entries(np); 2067 uc = ndev->flags & IFF_PROMISC; 2068 mc = ndev->flags & (IFF_PROMISC | IFF_ALLMULTI); 2069 2070 if (netc_port_add_host_flood_rule(np, uc, mc)) 2071 dev_warn(ds->dev, 2072 "Failed to restore host flood rule on port %d\n", 2073 port); 2074 2075 /* When a port leaves a VLAN-aware bridge, dsa_port_bridge_leave() 2076 * follows the sequence below: 2077 * 2078 * 1. dsa_port_bridge_destroy() is called to set dp->bridge to NULL. 2079 * 2. dsa_broadcast() is called, which eventually invokes 2080 * ds->ops->port_bridge_leave() 2081 * 3. dsa_port_switchdev_unsync_attrs() is called, which triggers 2082 * dsa_port_reset_vlan_filtering() and ultimately calls 2083 * ds->ops->port_vlan_filtering() to transition the port from 2084 * VLAN-aware mode to VLAN-unaware mode. 2085 * 2086 * At step 3, since dp->bridge has already been set to NULL in step 1, 2087 * netc_port_vlan_filtering() will detect this and skip the creation 2088 * of an unaware PVID entry in the VLAN filter table. Therefore, it is 2089 * safe to return directly here. 2090 */ 2091 if (br_vlan_enabled(bridge.dev)) 2092 return; 2093 2094 vlan_unaware_pvid = NETC_VLAN_UNAWARE_PVID(bridge.num); 2095 /* There is no need to check the return value even if it fails. 2096 * Because the PVID has been set to NETC_STANDALONE_PVID, the 2097 * frames will not match this VLAN entry. 2098 */ 2099 netc_port_del_vlan_entry(np, vlan_unaware_pvid); 2100 } 2101 2102 static int netc_set_ageing_time(struct dsa_switch *ds, unsigned int msecs) 2103 { 2104 struct netc_switch *priv = ds->priv; 2105 unsigned long delay_jiffies; 2106 2107 /* The dynamic FDB entry is deleted when its activity counter reaches 2108 * NETC_FDBT_AGEING_THRESH (100). Each delayed_work tick increments 2109 * the counter by 1 if the entry is inactive. 2110 * 2111 * Therefore: 2112 * msecs (ms) = NETC_FDBT_AGEING_THRESH * delay_ms (ms) 2113 * delay_ms = msecs / NETC_FDBT_AGEING_THRESH 2114 * delay_jiffies = (delay_ms / 1000) * HZ 2115 * = (msecs * HZ) / (1000 * NETC_FDBT_AGEING_THRESH) 2116 * 2117 * Use DIV_ROUND_CLOSEST_ULL to perform a single nearest-jiffy 2118 * rounding, avoiding the two-step rounding error of the intermediate 2119 * delay_ms approach. 2120 * Maximum error = +/-0.5 jiffy * 100 = +/-50000/HZ ms. 2121 */ 2122 delay_jiffies = DIV_ROUND_CLOSEST_ULL((u64)msecs * HZ, 2123 1000 * NETC_FDBT_AGEING_THRESH); 2124 WRITE_ONCE(priv->fdbt_ageing_delay, delay_jiffies); 2125 2126 if (atomic_read(&priv->br_cnt)) 2127 mod_delayed_work(system_percpu_wq, &priv->fdbt_ageing_work, 2128 READ_ONCE(priv->fdbt_ageing_delay)); 2129 2130 return 0; 2131 } 2132 2133 static void netc_port_fast_age(struct dsa_switch *ds, int port) 2134 { 2135 struct netc_port *np = NETC_PORT(ds, port); 2136 2137 netc_port_remove_dynamic_entries(np); 2138 } 2139 2140 static void netc_phylink_get_caps(struct dsa_switch *ds, int port, 2141 struct phylink_config *config) 2142 { 2143 struct netc_switch *priv = ds->priv; 2144 2145 priv->info->phylink_get_caps(port, config); 2146 } 2147 2148 static void netc_port_set_mac_mode(struct netc_port *np, 2149 unsigned int mode, 2150 phy_interface_t phy_mode) 2151 { 2152 u32 mask = PM_IF_MODE_IFMODE | PM_IF_MODE_REVMII; 2153 u32 val = 0; 2154 2155 switch (phy_mode) { 2156 case PHY_INTERFACE_MODE_RGMII: 2157 case PHY_INTERFACE_MODE_RGMII_ID: 2158 case PHY_INTERFACE_MODE_RGMII_RXID: 2159 case PHY_INTERFACE_MODE_RGMII_TXID: 2160 val |= IFMODE_RGMII; 2161 break; 2162 case PHY_INTERFACE_MODE_RMII: 2163 val |= IFMODE_RMII; 2164 break; 2165 case PHY_INTERFACE_MODE_REVMII: 2166 val |= PM_IF_MODE_REVMII; 2167 fallthrough; 2168 case PHY_INTERFACE_MODE_MII: 2169 val |= IFMODE_MII; 2170 break; 2171 case PHY_INTERFACE_MODE_SGMII: 2172 case PHY_INTERFACE_MODE_2500BASEX: 2173 val |= IFMODE_SGMII; 2174 break; 2175 default: 2176 break; 2177 } 2178 2179 netc_mac_port_rmw(np, NETC_PM_IF_MODE(0), mask, val); 2180 } 2181 2182 static void netc_mac_config(struct phylink_config *config, unsigned int mode, 2183 const struct phylink_link_state *state) 2184 { 2185 struct dsa_port *dp = dsa_phylink_to_port(config); 2186 2187 netc_port_set_mac_mode(NETC_PORT(dp->ds, dp->index), mode, 2188 state->interface); 2189 } 2190 2191 static void netc_port_set_speed(struct netc_port *np, int speed) 2192 { 2193 netc_port_rmw(np, NETC_PCR, PCR_PSPEED, PSPEED_SET_VAL(speed)); 2194 } 2195 2196 static void netc_port_set_rgmii_mac(struct netc_port *np, 2197 int speed, int duplex) 2198 { 2199 u32 mask, val; 2200 2201 mask = PM_IF_MODE_SSP | PM_IF_MODE_HD | PM_IF_MODE_M10; 2202 2203 switch (speed) { 2204 default: 2205 case SPEED_1000: 2206 val = FIELD_PREP(PM_IF_MODE_SSP, SSP_1G); 2207 break; 2208 case SPEED_100: 2209 val = FIELD_PREP(PM_IF_MODE_SSP, SSP_100M); 2210 break; 2211 case SPEED_10: 2212 val = FIELD_PREP(PM_IF_MODE_SSP, SSP_10M); 2213 break; 2214 } 2215 2216 if (duplex != DUPLEX_FULL) 2217 val |= PM_IF_MODE_HD; 2218 2219 netc_mac_port_rmw(np, NETC_PM_IF_MODE(0), mask, val); 2220 } 2221 2222 static void netc_port_set_rmii_mii_mac(struct netc_port *np, 2223 int speed, int duplex) 2224 { 2225 u32 mask, val = 0; 2226 2227 mask = PM_IF_MODE_SSP | PM_IF_MODE_HD | PM_IF_MODE_M10; 2228 2229 if (speed == SPEED_10) 2230 val |= PM_IF_MODE_M10; 2231 2232 if (duplex != DUPLEX_FULL) 2233 val |= PM_IF_MODE_HD; 2234 2235 netc_mac_port_rmw(np, NETC_PM_IF_MODE(0), mask, val); 2236 } 2237 2238 static void netc_port_set_tx_pause(struct netc_port *np, bool tx_pause) 2239 { 2240 struct netc_switch *priv = np->switch_priv; 2241 int port = np->dp->index; 2242 int i, j, num_bp; 2243 2244 num_bp = priv->num_bp / priv->info->num_ports; 2245 for (i = 0, j = port * num_bp; i < num_bp; i++, j++) { 2246 struct bpt_cfge_data *cfge = &priv->bpt_list[j]; 2247 struct bpt_cfge_data old_cfge = *cfge; 2248 2249 if (tx_pause) { 2250 cfge->fc_on_thresh = cpu_to_le16(NETC_FC_THRESH_ON); 2251 cfge->fc_off_thresh = cpu_to_le16(NETC_FC_THRESH_OFF); 2252 cfge->fccfg_sbpen = FIELD_PREP(BPT_FC_CFG, 2253 BPT_FC_CFG_EN_BPFC); 2254 cfge->fc_ports = cpu_to_le32(BIT(port)); 2255 } else { 2256 cfge->fc_on_thresh = cpu_to_le16(0); 2257 cfge->fc_off_thresh = cpu_to_le16(0); 2258 cfge->fccfg_sbpen = 0; 2259 cfge->fc_ports = cpu_to_le32(0); 2260 } 2261 2262 if (ntmp_bpt_update_entry(&priv->ntmp, j, cfge)) { 2263 *cfge = old_cfge; 2264 dev_warn(priv->dev, 2265 "Failed to %s TX pause of buffer pool %d (swp%d)\n", 2266 tx_pause ? "enable" : "disable", j, port); 2267 } 2268 } 2269 } 2270 2271 static void netc_port_set_rx_pause(struct netc_port *np, bool rx_pause) 2272 { 2273 netc_mac_port_rmw(np, NETC_PM_CMD_CFG(0), PM_CMD_CFG_PAUSE_IGN, 2274 rx_pause ? 0 : PM_CMD_CFG_PAUSE_IGN); 2275 } 2276 2277 static void netc_port_mac_rx_enable(struct netc_port *np) 2278 { 2279 netc_port_rmw(np, NETC_POR, POR_RXDIS, 0); 2280 netc_mac_port_rmw(np, NETC_PM_CMD_CFG(0), PM_CMD_CFG_RX_EN, 2281 PM_CMD_CFG_RX_EN); 2282 } 2283 2284 static void netc_port_wait_rx_empty(struct netc_port *np, int mac) 2285 { 2286 u32 val; 2287 2288 /* PM_IEVENT_RX_EMPTY is a read-only bit, it is automatically set by 2289 * hardware if RX FIFO is empty and no RX packet receive in process. 2290 * And it is automatically cleared if RX FIFO is not empty or RX 2291 * packet receive in process. 2292 */ 2293 if (read_poll_timeout(netc_port_rd, val, val & PM_IEVENT_RX_EMPTY, 2294 100, 10000, false, np, NETC_PM_IEVENT(mac))) 2295 dev_warn(np->switch_priv->dev, 2296 "swp%d MAC%d: RX is not idle\n", np->dp->index, mac); 2297 } 2298 2299 static void netc_port_mac_rx_graceful_stop(struct netc_port *np) 2300 { 2301 u32 val; 2302 2303 if (is_netc_pseudo_port(np)) 2304 goto rx_disable; 2305 2306 if (np->caps.pmac) { 2307 netc_port_rmw(np, NETC_PM_CMD_CFG(1), PM_CMD_CFG_RX_EN, 0); 2308 netc_port_wait_rx_empty(np, 1); 2309 } 2310 2311 netc_port_rmw(np, NETC_PM_CMD_CFG(0), PM_CMD_CFG_RX_EN, 0); 2312 netc_port_wait_rx_empty(np, 0); 2313 2314 if (read_poll_timeout(netc_port_rd, val, !(val & PSR_RX_BUSY), 2315 100, 10000, false, np, NETC_PSR)) 2316 dev_warn(np->switch_priv->dev, "swp%d RX is busy\n", 2317 np->dp->index); 2318 2319 rx_disable: 2320 netc_port_rmw(np, NETC_POR, POR_RXDIS, POR_RXDIS); 2321 } 2322 2323 static void netc_port_mac_tx_enable(struct netc_port *np) 2324 { 2325 netc_mac_port_rmw(np, NETC_PM_CMD_CFG(0), PM_CMD_CFG_TX_EN, 2326 PM_CMD_CFG_TX_EN); 2327 netc_port_rmw(np, NETC_POR, POR_TXDIS, 0); 2328 } 2329 2330 static void netc_port_wait_tx_empty(struct netc_port *np, int mac) 2331 { 2332 u32 val; 2333 2334 /* PM_IEVENT_TX_EMPTY is a read-only bit, it is automatically set by 2335 * hardware if TX FIFO is empty. And it is automatically cleared if 2336 * TX FIFO is not empty. 2337 */ 2338 if (read_poll_timeout(netc_port_rd, val, val & PM_IEVENT_TX_EMPTY, 2339 100, 10000, false, np, NETC_PM_IEVENT(mac))) 2340 dev_warn(np->switch_priv->dev, 2341 "swp%d MAC%d: TX FIFO is not empty\n", 2342 np->dp->index, mac); 2343 } 2344 2345 static void netc_port_mac_tx_graceful_stop(struct netc_port *np) 2346 { 2347 netc_port_rmw(np, NETC_POR, POR_TXDIS, POR_TXDIS); 2348 2349 if (is_netc_pseudo_port(np)) 2350 return; 2351 2352 netc_port_wait_tx_empty(np, 0); 2353 if (np->caps.pmac) 2354 netc_port_wait_tx_empty(np, 1); 2355 2356 netc_mac_port_rmw(np, NETC_PM_CMD_CFG(0), PM_CMD_CFG_TX_EN, 0); 2357 } 2358 2359 static void netc_mac_link_up(struct phylink_config *config, 2360 struct phy_device *phy, unsigned int mode, 2361 phy_interface_t interface, int speed, 2362 int duplex, bool tx_pause, bool rx_pause) 2363 { 2364 struct dsa_port *dp = dsa_phylink_to_port(config); 2365 struct netc_port *np; 2366 2367 np = NETC_PORT(dp->ds, dp->index); 2368 netc_port_set_speed(np, speed); 2369 2370 if (phy_interface_mode_is_rgmii(interface)) 2371 netc_port_set_rgmii_mac(np, speed, duplex); 2372 2373 if (interface == PHY_INTERFACE_MODE_RMII || 2374 interface == PHY_INTERFACE_MODE_REVMII || 2375 interface == PHY_INTERFACE_MODE_MII) 2376 netc_port_set_rmii_mii_mac(np, speed, duplex); 2377 2378 netc_port_set_tx_pause(np, tx_pause); 2379 netc_port_set_rx_pause(np, rx_pause); 2380 netc_port_mac_tx_enable(np); 2381 netc_port_mac_rx_enable(np); 2382 } 2383 2384 static void netc_mac_link_down(struct phylink_config *config, 2385 unsigned int mode, 2386 phy_interface_t interface) 2387 { 2388 struct dsa_port *dp = dsa_phylink_to_port(config); 2389 struct netc_port *np; 2390 2391 np = NETC_PORT(dp->ds, dp->index); 2392 netc_port_mac_rx_graceful_stop(np); 2393 netc_port_mac_tx_graceful_stop(np); 2394 netc_port_remove_dynamic_entries(np); 2395 } 2396 2397 static const struct phylink_mac_ops netc_phylink_mac_ops = { 2398 .mac_config = netc_mac_config, 2399 .mac_link_up = netc_mac_link_up, 2400 .mac_link_down = netc_mac_link_down, 2401 }; 2402 2403 static const struct dsa_switch_ops netc_switch_ops = { 2404 .get_tag_protocol = netc_get_tag_protocol, 2405 .setup = netc_setup, 2406 .teardown = netc_teardown, 2407 .phylink_get_caps = netc_phylink_get_caps, 2408 .port_enable = netc_port_enable, 2409 .port_disable = netc_port_disable, 2410 .port_stp_state_set = netc_port_stp_state_set, 2411 .port_change_mtu = netc_port_change_mtu, 2412 .port_max_mtu = netc_port_max_mtu, 2413 .port_fdb_add = netc_port_fdb_add, 2414 .port_fdb_del = netc_port_fdb_del, 2415 .port_fdb_dump = netc_port_fdb_dump, 2416 .port_mdb_add = netc_port_mdb_add, 2417 .port_mdb_del = netc_port_mdb_del, 2418 .port_set_host_flood = netc_port_set_host_flood, 2419 .port_vlan_filtering = netc_port_vlan_filtering, 2420 .port_vlan_add = netc_port_vlan_add, 2421 .port_vlan_del = netc_port_vlan_del, 2422 .port_bridge_join = netc_port_bridge_join, 2423 .port_bridge_leave = netc_port_bridge_leave, 2424 .set_ageing_time = netc_set_ageing_time, 2425 .port_fast_age = netc_port_fast_age, 2426 .get_pause_stats = netc_port_get_pause_stats, 2427 .get_rmon_stats = netc_port_get_rmon_stats, 2428 .get_eth_ctrl_stats = netc_port_get_eth_ctrl_stats, 2429 .get_eth_mac_stats = netc_port_get_eth_mac_stats, 2430 .get_sset_count = netc_port_get_sset_count, 2431 .get_strings = netc_port_get_strings, 2432 .get_ethtool_stats = netc_port_get_ethtool_stats, 2433 }; 2434 2435 static int netc_switch_probe(struct pci_dev *pdev, 2436 const struct pci_device_id *id) 2437 { 2438 struct device_node *node = dev_of_node(&pdev->dev); 2439 struct device *dev = &pdev->dev; 2440 struct netc_switch *priv; 2441 struct dsa_switch *ds; 2442 int err; 2443 2444 if (!node) 2445 return dev_err_probe(dev, -ENODEV, 2446 "No DT bindings, skipping\n"); 2447 2448 err = netc_switch_check_emdio_is_ready(dev); 2449 if (err) 2450 return err; 2451 2452 err = netc_switch_pci_init(pdev); 2453 if (err) 2454 return err; 2455 2456 priv = pci_get_drvdata(pdev); 2457 netc_switch_get_ip_revision(priv); 2458 2459 err = netc_switch_platform_probe(priv); 2460 if (err) 2461 return err; 2462 2463 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 2464 if (!ds) 2465 return -ENOMEM; 2466 2467 ds->dev = dev; 2468 ds->num_ports = priv->info->num_ports; 2469 ds->num_tx_queues = NETC_TC_NUM; 2470 ds->ops = &netc_switch_ops; 2471 ds->phylink_mac_ops = &netc_phylink_mac_ops; 2472 ds->fdb_isolation = true; 2473 ds->max_num_bridges = priv->info->num_ports - 1; 2474 ds->ageing_time_min = 1000; 2475 ds->ageing_time_max = U32_MAX; 2476 ds->priv = priv; 2477 priv->ds = ds; 2478 2479 err = dsa_register_switch(ds); 2480 if (err) 2481 return dev_err_probe(dev, err, 2482 "Failed to register DSA switch\n"); 2483 2484 return 0; 2485 } 2486 2487 static void netc_switch_remove(struct pci_dev *pdev) 2488 { 2489 struct netc_switch *priv = pci_get_drvdata(pdev); 2490 2491 if (!priv) 2492 return; 2493 2494 dsa_unregister_switch(priv->ds); 2495 } 2496 2497 static void netc_switch_shutdown(struct pci_dev *pdev) 2498 { 2499 struct netc_switch *priv = pci_get_drvdata(pdev); 2500 2501 if (!priv) 2502 return; 2503 2504 dsa_switch_shutdown(priv->ds); 2505 pci_set_drvdata(pdev, NULL); 2506 } 2507 2508 static const struct pci_device_id netc_switch_ids[] = { 2509 { PCI_DEVICE(NETC_SWITCH_VENDOR_ID, NETC_SWITCH_DEVICE_ID) }, 2510 { } 2511 }; 2512 MODULE_DEVICE_TABLE(pci, netc_switch_ids); 2513 2514 static struct pci_driver netc_switch_driver = { 2515 .name = KBUILD_MODNAME, 2516 .id_table = netc_switch_ids, 2517 .probe = netc_switch_probe, 2518 .remove = netc_switch_remove, 2519 .shutdown = netc_switch_shutdown, 2520 }; 2521 module_pci_driver(netc_switch_driver); 2522 2523 MODULE_DESCRIPTION("NXP NETC Switch driver"); 2524 MODULE_LICENSE("Dual BSD/GPL"); 2525