1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH 3 * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com> 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #include <linux/delay.h> 9 #include <linux/module.h> 10 #include <linux/printk.h> 11 #include <linux/spi/spi.h> 12 #include <linux/errno.h> 13 #include <linux/gpio/consumer.h> 14 #include <linux/phylink.h> 15 #include <linux/of.h> 16 #include <linux/of_net.h> 17 #include <linux/of_mdio.h> 18 #include <linux/of_device.h> 19 #include <linux/pcs/pcs-xpcs.h> 20 #include <linux/netdev_features.h> 21 #include <linux/netdevice.h> 22 #include <linux/if_bridge.h> 23 #include <linux/if_ether.h> 24 #include <linux/dsa/8021q.h> 25 #include "sja1105.h" 26 #include "sja1105_tas.h" 27 28 #define SJA1105_UNKNOWN_MULTICAST 0x010000000000ull 29 #define SJA1105_DEFAULT_VLAN (VLAN_N_VID - 1) 30 31 static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, 32 unsigned int startup_delay) 33 { 34 gpiod_set_value_cansleep(gpio, 1); 35 /* Wait for minimum reset pulse length */ 36 msleep(pulse_len); 37 gpiod_set_value_cansleep(gpio, 0); 38 /* Wait until chip is ready after reset */ 39 msleep(startup_delay); 40 } 41 42 static void 43 sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, 44 int from, int to, bool allow) 45 { 46 if (allow) 47 l2_fwd[from].reach_port |= BIT(to); 48 else 49 l2_fwd[from].reach_port &= ~BIT(to); 50 } 51 52 static bool sja1105_can_forward(struct sja1105_l2_forwarding_entry *l2_fwd, 53 int from, int to) 54 { 55 return !!(l2_fwd[from].reach_port & BIT(to)); 56 } 57 58 static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 59 { 60 struct sja1105_vlan_lookup_entry *vlan; 61 int count, i; 62 63 vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 64 count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 65 66 for (i = 0; i < count; i++) 67 if (vlan[i].vlanid == vid) 68 return i; 69 70 /* Return an invalid entry index if not found */ 71 return -1; 72 } 73 74 static int sja1105_drop_untagged(struct dsa_switch *ds, int port, bool drop) 75 { 76 struct sja1105_private *priv = ds->priv; 77 struct sja1105_mac_config_entry *mac; 78 79 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 80 81 if (mac[port].drpuntag == drop) 82 return 0; 83 84 mac[port].drpuntag = drop; 85 86 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 87 &mac[port], true); 88 } 89 90 static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 91 { 92 struct sja1105_mac_config_entry *mac; 93 94 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 95 96 if (mac[port].vlanid == pvid) 97 return 0; 98 99 mac[port].vlanid = pvid; 100 101 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 102 &mac[port], true); 103 } 104 105 static int sja1105_commit_pvid(struct dsa_switch *ds, int port) 106 { 107 struct dsa_port *dp = dsa_to_port(ds, port); 108 struct sja1105_private *priv = ds->priv; 109 struct sja1105_vlan_lookup_entry *vlan; 110 bool drop_untagged = false; 111 int match, rc; 112 u16 pvid; 113 114 if (dp->bridge_dev && br_vlan_enabled(dp->bridge_dev)) 115 pvid = priv->bridge_pvid[port]; 116 else 117 pvid = priv->tag_8021q_pvid[port]; 118 119 rc = sja1105_pvid_apply(priv, port, pvid); 120 if (rc) 121 return rc; 122 123 vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 124 125 match = sja1105_is_vlan_configured(priv, pvid); 126 127 if (match < 0 || !(vlan[match].vmemb_port & BIT(port))) 128 drop_untagged = true; 129 130 return sja1105_drop_untagged(ds, port, drop_untagged); 131 } 132 133 static int sja1105_init_mac_settings(struct sja1105_private *priv) 134 { 135 struct sja1105_mac_config_entry default_mac = { 136 /* Enable all 8 priority queues on egress. 137 * Every queue i holds top[i] - base[i] frames. 138 * Sum of top[i] - base[i] is 511 (max hardware limit). 139 */ 140 .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, 141 .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, 142 .enabled = {true, true, true, true, true, true, true, true}, 143 /* Keep standard IFG of 12 bytes on egress. */ 144 .ifg = 0, 145 /* Always put the MAC speed in automatic mode, where it can be 146 * adjusted at runtime by PHYLINK. 147 */ 148 .speed = priv->info->port_speed[SJA1105_SPEED_AUTO], 149 /* No static correction for 1-step 1588 events */ 150 .tp_delin = 0, 151 .tp_delout = 0, 152 /* Disable aging for critical TTEthernet traffic */ 153 .maxage = 0xFF, 154 /* Internal VLAN (pvid) to apply to untagged ingress */ 155 .vlanprio = 0, 156 .vlanid = 1, 157 .ing_mirr = false, 158 .egr_mirr = false, 159 /* Don't drop traffic with other EtherType than ETH_P_IP */ 160 .drpnona664 = false, 161 /* Don't drop double-tagged traffic */ 162 .drpdtag = false, 163 /* Don't drop untagged traffic */ 164 .drpuntag = false, 165 /* Don't retag 802.1p (VID 0) traffic with the pvid */ 166 .retag = false, 167 /* Disable learning and I/O on user ports by default - 168 * STP will enable it. 169 */ 170 .dyn_learn = false, 171 .egress = false, 172 .ingress = false, 173 }; 174 struct sja1105_mac_config_entry *mac; 175 struct dsa_switch *ds = priv->ds; 176 struct sja1105_table *table; 177 struct dsa_port *dp; 178 179 table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; 180 181 /* Discard previous MAC Configuration Table */ 182 if (table->entry_count) { 183 kfree(table->entries); 184 table->entry_count = 0; 185 } 186 187 table->entries = kcalloc(table->ops->max_entry_count, 188 table->ops->unpacked_entry_size, GFP_KERNEL); 189 if (!table->entries) 190 return -ENOMEM; 191 192 table->entry_count = table->ops->max_entry_count; 193 194 mac = table->entries; 195 196 list_for_each_entry(dp, &ds->dst->ports, list) { 197 if (dp->ds != ds) 198 continue; 199 200 mac[dp->index] = default_mac; 201 202 /* Let sja1105_bridge_stp_state_set() keep address learning 203 * enabled for the DSA ports. CPU ports use software-assisted 204 * learning to ensure that only FDB entries belonging to the 205 * bridge are learned, and that they are learned towards all 206 * CPU ports in a cross-chip topology if multiple CPU ports 207 * exist. 208 */ 209 if (dsa_port_is_dsa(dp)) 210 dp->learning = true; 211 } 212 213 return 0; 214 } 215 216 static int sja1105_init_mii_settings(struct sja1105_private *priv) 217 { 218 struct device *dev = &priv->spidev->dev; 219 struct sja1105_xmii_params_entry *mii; 220 struct dsa_switch *ds = priv->ds; 221 struct sja1105_table *table; 222 int i; 223 224 table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 225 226 /* Discard previous xMII Mode Parameters Table */ 227 if (table->entry_count) { 228 kfree(table->entries); 229 table->entry_count = 0; 230 } 231 232 table->entries = kcalloc(table->ops->max_entry_count, 233 table->ops->unpacked_entry_size, GFP_KERNEL); 234 if (!table->entries) 235 return -ENOMEM; 236 237 /* Override table based on PHYLINK DT bindings */ 238 table->entry_count = table->ops->max_entry_count; 239 240 mii = table->entries; 241 242 for (i = 0; i < ds->num_ports; i++) { 243 sja1105_mii_role_t role = XMII_MAC; 244 245 if (dsa_is_unused_port(priv->ds, i)) 246 continue; 247 248 switch (priv->phy_mode[i]) { 249 case PHY_INTERFACE_MODE_INTERNAL: 250 if (priv->info->internal_phy[i] == SJA1105_NO_PHY) 251 goto unsupported; 252 253 mii->xmii_mode[i] = XMII_MODE_MII; 254 if (priv->info->internal_phy[i] == SJA1105_PHY_BASE_TX) 255 mii->special[i] = true; 256 257 break; 258 case PHY_INTERFACE_MODE_REVMII: 259 role = XMII_PHY; 260 fallthrough; 261 case PHY_INTERFACE_MODE_MII: 262 if (!priv->info->supports_mii[i]) 263 goto unsupported; 264 265 mii->xmii_mode[i] = XMII_MODE_MII; 266 break; 267 case PHY_INTERFACE_MODE_REVRMII: 268 role = XMII_PHY; 269 fallthrough; 270 case PHY_INTERFACE_MODE_RMII: 271 if (!priv->info->supports_rmii[i]) 272 goto unsupported; 273 274 mii->xmii_mode[i] = XMII_MODE_RMII; 275 break; 276 case PHY_INTERFACE_MODE_RGMII: 277 case PHY_INTERFACE_MODE_RGMII_ID: 278 case PHY_INTERFACE_MODE_RGMII_RXID: 279 case PHY_INTERFACE_MODE_RGMII_TXID: 280 if (!priv->info->supports_rgmii[i]) 281 goto unsupported; 282 283 mii->xmii_mode[i] = XMII_MODE_RGMII; 284 break; 285 case PHY_INTERFACE_MODE_SGMII: 286 if (!priv->info->supports_sgmii[i]) 287 goto unsupported; 288 289 mii->xmii_mode[i] = XMII_MODE_SGMII; 290 mii->special[i] = true; 291 break; 292 case PHY_INTERFACE_MODE_2500BASEX: 293 if (!priv->info->supports_2500basex[i]) 294 goto unsupported; 295 296 mii->xmii_mode[i] = XMII_MODE_SGMII; 297 mii->special[i] = true; 298 break; 299 unsupported: 300 default: 301 dev_err(dev, "Unsupported PHY mode %s on port %d!\n", 302 phy_modes(priv->phy_mode[i]), i); 303 return -EINVAL; 304 } 305 306 mii->phy_mac[i] = role; 307 } 308 return 0; 309 } 310 311 static int sja1105_init_static_fdb(struct sja1105_private *priv) 312 { 313 struct sja1105_l2_lookup_entry *l2_lookup; 314 struct sja1105_table *table; 315 int port; 316 317 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 318 319 /* We only populate the FDB table through dynamic L2 Address Lookup 320 * entries, except for a special entry at the end which is a catch-all 321 * for unknown multicast and will be used to control flooding domain. 322 */ 323 if (table->entry_count) { 324 kfree(table->entries); 325 table->entry_count = 0; 326 } 327 328 if (!priv->info->can_limit_mcast_flood) 329 return 0; 330 331 table->entries = kcalloc(1, table->ops->unpacked_entry_size, 332 GFP_KERNEL); 333 if (!table->entries) 334 return -ENOMEM; 335 336 table->entry_count = 1; 337 l2_lookup = table->entries; 338 339 /* All L2 multicast addresses have an odd first octet */ 340 l2_lookup[0].macaddr = SJA1105_UNKNOWN_MULTICAST; 341 l2_lookup[0].mask_macaddr = SJA1105_UNKNOWN_MULTICAST; 342 l2_lookup[0].lockeds = true; 343 l2_lookup[0].index = SJA1105_MAX_L2_LOOKUP_COUNT - 1; 344 345 /* Flood multicast to every port by default */ 346 for (port = 0; port < priv->ds->num_ports; port++) 347 if (!dsa_is_unused_port(priv->ds, port)) 348 l2_lookup[0].destports |= BIT(port); 349 350 return 0; 351 } 352 353 static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 354 { 355 struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 356 /* Learned FDB entries are forgotten after 300 seconds */ 357 .maxage = SJA1105_AGEING_TIME_MS(300000), 358 /* All entries within a FDB bin are available for learning */ 359 .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 360 /* And the P/Q/R/S equivalent setting: */ 361 .start_dynspc = 0, 362 /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 363 .poly = 0x97, 364 /* This selects between Independent VLAN Learning (IVL) and 365 * Shared VLAN Learning (SVL) 366 */ 367 .shared_learn = true, 368 /* Don't discard management traffic based on ENFPORT - 369 * we don't perform SMAC port enforcement anyway, so 370 * what we are setting here doesn't matter. 371 */ 372 .no_enf_hostprt = false, 373 /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 374 * Maybe correlate with no_linklocal_learn from bridge driver? 375 */ 376 .no_mgmt_learn = true, 377 /* P/Q/R/S only */ 378 .use_static = true, 379 /* Dynamically learned FDB entries can overwrite other (older) 380 * dynamic FDB entries 381 */ 382 .owr_dyn = true, 383 .drpnolearn = true, 384 }; 385 struct dsa_switch *ds = priv->ds; 386 int port, num_used_ports = 0; 387 struct sja1105_table *table; 388 u64 max_fdb_entries; 389 390 for (port = 0; port < ds->num_ports; port++) 391 if (!dsa_is_unused_port(ds, port)) 392 num_used_ports++; 393 394 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / num_used_ports; 395 396 for (port = 0; port < ds->num_ports; port++) { 397 if (dsa_is_unused_port(ds, port)) 398 continue; 399 400 default_l2_lookup_params.maxaddrp[port] = max_fdb_entries; 401 } 402 403 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 404 405 if (table->entry_count) { 406 kfree(table->entries); 407 table->entry_count = 0; 408 } 409 410 table->entries = kcalloc(table->ops->max_entry_count, 411 table->ops->unpacked_entry_size, GFP_KERNEL); 412 if (!table->entries) 413 return -ENOMEM; 414 415 table->entry_count = table->ops->max_entry_count; 416 417 /* This table only has a single entry */ 418 ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 419 default_l2_lookup_params; 420 421 return 0; 422 } 423 424 /* Set up a default VLAN for untagged traffic injected from the CPU 425 * using management routes (e.g. STP, PTP) as opposed to tag_8021q. 426 * All DT-defined ports are members of this VLAN, and there are no 427 * restrictions on forwarding (since the CPU selects the destination). 428 * Frames from this VLAN will always be transmitted as untagged, and 429 * neither the bridge nor the 8021q module cannot create this VLAN ID. 430 */ 431 static int sja1105_init_static_vlan(struct sja1105_private *priv) 432 { 433 struct sja1105_table *table; 434 struct sja1105_vlan_lookup_entry pvid = { 435 .type_entry = SJA1110_VLAN_D_TAG, 436 .ving_mirr = 0, 437 .vegr_mirr = 0, 438 .vmemb_port = 0, 439 .vlan_bc = 0, 440 .tag_port = 0, 441 .vlanid = SJA1105_DEFAULT_VLAN, 442 }; 443 struct dsa_switch *ds = priv->ds; 444 int port; 445 446 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 447 448 if (table->entry_count) { 449 kfree(table->entries); 450 table->entry_count = 0; 451 } 452 453 table->entries = kzalloc(table->ops->unpacked_entry_size, 454 GFP_KERNEL); 455 if (!table->entries) 456 return -ENOMEM; 457 458 table->entry_count = 1; 459 460 for (port = 0; port < ds->num_ports; port++) { 461 if (dsa_is_unused_port(ds, port)) 462 continue; 463 464 pvid.vmemb_port |= BIT(port); 465 pvid.vlan_bc |= BIT(port); 466 pvid.tag_port &= ~BIT(port); 467 468 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) { 469 priv->tag_8021q_pvid[port] = SJA1105_DEFAULT_VLAN; 470 priv->bridge_pvid[port] = SJA1105_DEFAULT_VLAN; 471 } 472 } 473 474 ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 475 return 0; 476 } 477 478 static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 479 { 480 struct sja1105_l2_forwarding_entry *l2fwd; 481 struct dsa_switch *ds = priv->ds; 482 struct dsa_switch_tree *dst; 483 struct sja1105_table *table; 484 struct dsa_link *dl; 485 int port, tc; 486 int from, to; 487 488 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 489 490 if (table->entry_count) { 491 kfree(table->entries); 492 table->entry_count = 0; 493 } 494 495 table->entries = kcalloc(table->ops->max_entry_count, 496 table->ops->unpacked_entry_size, GFP_KERNEL); 497 if (!table->entries) 498 return -ENOMEM; 499 500 table->entry_count = table->ops->max_entry_count; 501 502 l2fwd = table->entries; 503 504 /* First 5 entries in the L2 Forwarding Table define the forwarding 505 * rules and the VLAN PCP to ingress queue mapping. 506 * Set up the ingress queue mapping first. 507 */ 508 for (port = 0; port < ds->num_ports; port++) { 509 if (dsa_is_unused_port(ds, port)) 510 continue; 511 512 for (tc = 0; tc < SJA1105_NUM_TC; tc++) 513 l2fwd[port].vlan_pmap[tc] = tc; 514 } 515 516 /* Then manage the forwarding domain for user ports. These can forward 517 * only to the always-on domain (CPU port and DSA links) 518 */ 519 for (from = 0; from < ds->num_ports; from++) { 520 if (!dsa_is_user_port(ds, from)) 521 continue; 522 523 for (to = 0; to < ds->num_ports; to++) { 524 if (!dsa_is_cpu_port(ds, to) && 525 !dsa_is_dsa_port(ds, to)) 526 continue; 527 528 l2fwd[from].bc_domain |= BIT(to); 529 l2fwd[from].fl_domain |= BIT(to); 530 531 sja1105_port_allow_traffic(l2fwd, from, to, true); 532 } 533 } 534 535 /* Then manage the forwarding domain for DSA links and CPU ports (the 536 * always-on domain). These can send packets to any enabled port except 537 * themselves. 538 */ 539 for (from = 0; from < ds->num_ports; from++) { 540 if (!dsa_is_cpu_port(ds, from) && !dsa_is_dsa_port(ds, from)) 541 continue; 542 543 for (to = 0; to < ds->num_ports; to++) { 544 if (dsa_is_unused_port(ds, to)) 545 continue; 546 547 if (from == to) 548 continue; 549 550 l2fwd[from].bc_domain |= BIT(to); 551 l2fwd[from].fl_domain |= BIT(to); 552 553 sja1105_port_allow_traffic(l2fwd, from, to, true); 554 } 555 } 556 557 /* In odd topologies ("H" connections where there is a DSA link to 558 * another switch which also has its own CPU port), TX packets can loop 559 * back into the system (they are flooded from CPU port 1 to the DSA 560 * link, and from there to CPU port 2). Prevent this from happening by 561 * cutting RX from DSA links towards our CPU port, if the remote switch 562 * has its own CPU port and therefore doesn't need ours for network 563 * stack termination. 564 */ 565 dst = ds->dst; 566 567 list_for_each_entry(dl, &dst->rtable, list) { 568 if (dl->dp->ds != ds || dl->link_dp->cpu_dp == dl->dp->cpu_dp) 569 continue; 570 571 from = dl->dp->index; 572 to = dsa_upstream_port(ds, from); 573 574 dev_warn(ds->dev, 575 "H topology detected, cutting RX from DSA link %d to CPU port %d to prevent TX packet loops\n", 576 from, to); 577 578 sja1105_port_allow_traffic(l2fwd, from, to, false); 579 580 l2fwd[from].bc_domain &= ~BIT(to); 581 l2fwd[from].fl_domain &= ~BIT(to); 582 } 583 584 /* Finally, manage the egress flooding domain. All ports start up with 585 * flooding enabled, including the CPU port and DSA links. 586 */ 587 for (port = 0; port < ds->num_ports; port++) { 588 if (dsa_is_unused_port(ds, port)) 589 continue; 590 591 priv->ucast_egress_floods |= BIT(port); 592 priv->bcast_egress_floods |= BIT(port); 593 } 594 595 /* Next 8 entries define VLAN PCP mapping from ingress to egress. 596 * Create a one-to-one mapping. 597 */ 598 for (tc = 0; tc < SJA1105_NUM_TC; tc++) { 599 for (port = 0; port < ds->num_ports; port++) { 600 if (dsa_is_unused_port(ds, port)) 601 continue; 602 603 l2fwd[ds->num_ports + tc].vlan_pmap[port] = tc; 604 } 605 606 l2fwd[ds->num_ports + tc].type_egrpcp2outputq = true; 607 } 608 609 return 0; 610 } 611 612 static int sja1110_init_pcp_remapping(struct sja1105_private *priv) 613 { 614 struct sja1110_pcp_remapping_entry *pcp_remap; 615 struct dsa_switch *ds = priv->ds; 616 struct sja1105_table *table; 617 int port, tc; 618 619 table = &priv->static_config.tables[BLK_IDX_PCP_REMAPPING]; 620 621 /* Nothing to do for SJA1105 */ 622 if (!table->ops->max_entry_count) 623 return 0; 624 625 if (table->entry_count) { 626 kfree(table->entries); 627 table->entry_count = 0; 628 } 629 630 table->entries = kcalloc(table->ops->max_entry_count, 631 table->ops->unpacked_entry_size, GFP_KERNEL); 632 if (!table->entries) 633 return -ENOMEM; 634 635 table->entry_count = table->ops->max_entry_count; 636 637 pcp_remap = table->entries; 638 639 /* Repeat the configuration done for vlan_pmap */ 640 for (port = 0; port < ds->num_ports; port++) { 641 if (dsa_is_unused_port(ds, port)) 642 continue; 643 644 for (tc = 0; tc < SJA1105_NUM_TC; tc++) 645 pcp_remap[port].egrpcp[tc] = tc; 646 } 647 648 return 0; 649 } 650 651 static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 652 { 653 struct sja1105_l2_forwarding_params_entry *l2fwd_params; 654 struct sja1105_table *table; 655 656 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 657 658 if (table->entry_count) { 659 kfree(table->entries); 660 table->entry_count = 0; 661 } 662 663 table->entries = kcalloc(table->ops->max_entry_count, 664 table->ops->unpacked_entry_size, GFP_KERNEL); 665 if (!table->entries) 666 return -ENOMEM; 667 668 table->entry_count = table->ops->max_entry_count; 669 670 /* This table only has a single entry */ 671 l2fwd_params = table->entries; 672 673 /* Disallow dynamic reconfiguration of vlan_pmap */ 674 l2fwd_params->max_dynp = 0; 675 /* Use a single memory partition for all ingress queues */ 676 l2fwd_params->part_spc[0] = priv->info->max_frame_mem; 677 678 return 0; 679 } 680 681 void sja1105_frame_memory_partitioning(struct sja1105_private *priv) 682 { 683 struct sja1105_l2_forwarding_params_entry *l2_fwd_params; 684 struct sja1105_vl_forwarding_params_entry *vl_fwd_params; 685 struct sja1105_table *table; 686 687 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 688 l2_fwd_params = table->entries; 689 l2_fwd_params->part_spc[0] = SJA1105_MAX_FRAME_MEMORY; 690 691 /* If we have any critical-traffic virtual links, we need to reserve 692 * some frame buffer memory for them. At the moment, hardcode the value 693 * at 100 blocks of 128 bytes of memory each. This leaves 829 blocks 694 * remaining for best-effort traffic. TODO: figure out a more flexible 695 * way to perform the frame buffer partitioning. 696 */ 697 if (!priv->static_config.tables[BLK_IDX_VL_FORWARDING].entry_count) 698 return; 699 700 table = &priv->static_config.tables[BLK_IDX_VL_FORWARDING_PARAMS]; 701 vl_fwd_params = table->entries; 702 703 l2_fwd_params->part_spc[0] -= SJA1105_VL_FRAME_MEMORY; 704 vl_fwd_params->partspc[0] = SJA1105_VL_FRAME_MEMORY; 705 } 706 707 /* SJA1110 TDMACONFIGIDX values: 708 * 709 * | 100 Mbps ports | 1Gbps ports | 2.5Gbps ports | Disabled ports 710 * -----+----------------+---------------+---------------+--------------- 711 * 0 | 0, [5:10] | [1:2] | [3:4] | retag 712 * 1 |0, [5:10], retag| [1:2] | [3:4] | - 713 * 2 | 0, [5:10] | [1:3], retag | 4 | - 714 * 3 | 0, [5:10] |[1:2], 4, retag| 3 | - 715 * 4 | 0, 2, [5:10] | 1, retag | [3:4] | - 716 * 5 | 0, 1, [5:10] | 2, retag | [3:4] | - 717 * 14 | 0, [5:10] | [1:4], retag | - | - 718 * 15 | [5:10] | [0:4], retag | - | - 719 */ 720 static void sja1110_select_tdmaconfigidx(struct sja1105_private *priv) 721 { 722 struct sja1105_general_params_entry *general_params; 723 struct sja1105_table *table; 724 bool port_1_is_base_tx; 725 bool port_3_is_2500; 726 bool port_4_is_2500; 727 u64 tdmaconfigidx; 728 729 if (priv->info->device_id != SJA1110_DEVICE_ID) 730 return; 731 732 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 733 general_params = table->entries; 734 735 /* All the settings below are "as opposed to SGMII", which is the 736 * other pinmuxing option. 737 */ 738 port_1_is_base_tx = priv->phy_mode[1] == PHY_INTERFACE_MODE_INTERNAL; 739 port_3_is_2500 = priv->phy_mode[3] == PHY_INTERFACE_MODE_2500BASEX; 740 port_4_is_2500 = priv->phy_mode[4] == PHY_INTERFACE_MODE_2500BASEX; 741 742 if (port_1_is_base_tx) 743 /* Retagging port will operate at 1 Gbps */ 744 tdmaconfigidx = 5; 745 else if (port_3_is_2500 && port_4_is_2500) 746 /* Retagging port will operate at 100 Mbps */ 747 tdmaconfigidx = 1; 748 else if (port_3_is_2500) 749 /* Retagging port will operate at 1 Gbps */ 750 tdmaconfigidx = 3; 751 else if (port_4_is_2500) 752 /* Retagging port will operate at 1 Gbps */ 753 tdmaconfigidx = 2; 754 else 755 /* Retagging port will operate at 1 Gbps */ 756 tdmaconfigidx = 14; 757 758 general_params->tdmaconfigidx = tdmaconfigidx; 759 } 760 761 static int sja1105_init_topology(struct sja1105_private *priv, 762 struct sja1105_general_params_entry *general_params) 763 { 764 struct dsa_switch *ds = priv->ds; 765 int port; 766 767 /* The host port is the destination for traffic matching mac_fltres1 768 * and mac_fltres0 on all ports except itself. Default to an invalid 769 * value. 770 */ 771 general_params->host_port = ds->num_ports; 772 773 /* Link-local traffic received on casc_port will be forwarded 774 * to host_port without embedding the source port and device ID 775 * info in the destination MAC address, and no RX timestamps will be 776 * taken either (presumably because it is a cascaded port and a 777 * downstream SJA switch already did that). 778 * To disable the feature, we need to do different things depending on 779 * switch generation. On SJA1105 we need to set an invalid port, while 780 * on SJA1110 which support multiple cascaded ports, this field is a 781 * bitmask so it must be left zero. 782 */ 783 if (!priv->info->multiple_cascade_ports) 784 general_params->casc_port = ds->num_ports; 785 786 for (port = 0; port < ds->num_ports; port++) { 787 bool is_upstream = dsa_is_upstream_port(ds, port); 788 bool is_dsa_link = dsa_is_dsa_port(ds, port); 789 790 /* Upstream ports can be dedicated CPU ports or 791 * upstream-facing DSA links 792 */ 793 if (is_upstream) { 794 if (general_params->host_port == ds->num_ports) { 795 general_params->host_port = port; 796 } else { 797 dev_err(ds->dev, 798 "Port %llu is already a host port, configuring %d as one too is not supported\n", 799 general_params->host_port, port); 800 return -EINVAL; 801 } 802 } 803 804 /* Cascade ports are downstream-facing DSA links */ 805 if (is_dsa_link && !is_upstream) { 806 if (priv->info->multiple_cascade_ports) { 807 general_params->casc_port |= BIT(port); 808 } else if (general_params->casc_port == ds->num_ports) { 809 general_params->casc_port = port; 810 } else { 811 dev_err(ds->dev, 812 "Port %llu is already a cascade port, configuring %d as one too is not supported\n", 813 general_params->casc_port, port); 814 return -EINVAL; 815 } 816 } 817 } 818 819 if (general_params->host_port == ds->num_ports) { 820 dev_err(ds->dev, "No host port configured\n"); 821 return -EINVAL; 822 } 823 824 return 0; 825 } 826 827 static int sja1105_init_general_params(struct sja1105_private *priv) 828 { 829 struct sja1105_general_params_entry default_general_params = { 830 /* Allow dynamic changing of the mirror port */ 831 .mirr_ptacu = true, 832 .switchid = priv->ds->index, 833 /* Priority queue for link-local management frames 834 * (both ingress to and egress from CPU - PTP, STP etc) 835 */ 836 .hostprio = 7, 837 .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 838 .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 839 .incl_srcpt1 = false, 840 .send_meta1 = false, 841 .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 842 .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 843 .incl_srcpt0 = false, 844 .send_meta0 = false, 845 /* Default to an invalid value */ 846 .mirr_port = priv->ds->num_ports, 847 /* No TTEthernet */ 848 .vllupformat = SJA1105_VL_FORMAT_PSFP, 849 .vlmarker = 0, 850 .vlmask = 0, 851 /* Only update correctionField for 1-step PTP (L2 transport) */ 852 .ignore2stf = 0, 853 /* Forcefully disable VLAN filtering by telling 854 * the switch that VLAN has a different EtherType. 855 */ 856 .tpid = ETH_P_SJA1105, 857 .tpid2 = ETH_P_SJA1105, 858 /* Enable the TTEthernet engine on SJA1110 */ 859 .tte_en = true, 860 /* Set up the EtherType for control packets on SJA1110 */ 861 .header_type = ETH_P_SJA1110, 862 }; 863 struct sja1105_general_params_entry *general_params; 864 struct sja1105_table *table; 865 int rc; 866 867 rc = sja1105_init_topology(priv, &default_general_params); 868 if (rc) 869 return rc; 870 871 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 872 873 if (table->entry_count) { 874 kfree(table->entries); 875 table->entry_count = 0; 876 } 877 878 table->entries = kcalloc(table->ops->max_entry_count, 879 table->ops->unpacked_entry_size, GFP_KERNEL); 880 if (!table->entries) 881 return -ENOMEM; 882 883 table->entry_count = table->ops->max_entry_count; 884 885 general_params = table->entries; 886 887 /* This table only has a single entry */ 888 general_params[0] = default_general_params; 889 890 sja1110_select_tdmaconfigidx(priv); 891 892 return 0; 893 } 894 895 static int sja1105_init_avb_params(struct sja1105_private *priv) 896 { 897 struct sja1105_avb_params_entry *avb; 898 struct sja1105_table *table; 899 900 table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 901 902 /* Discard previous AVB Parameters Table */ 903 if (table->entry_count) { 904 kfree(table->entries); 905 table->entry_count = 0; 906 } 907 908 table->entries = kcalloc(table->ops->max_entry_count, 909 table->ops->unpacked_entry_size, GFP_KERNEL); 910 if (!table->entries) 911 return -ENOMEM; 912 913 table->entry_count = table->ops->max_entry_count; 914 915 avb = table->entries; 916 917 /* Configure the MAC addresses for meta frames */ 918 avb->destmeta = SJA1105_META_DMAC; 919 avb->srcmeta = SJA1105_META_SMAC; 920 /* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by 921 * default. This is because there might be boards with a hardware 922 * layout where enabling the pin as output might cause an electrical 923 * clash. On E/T the pin is always an output, which the board designers 924 * probably already knew, so even if there are going to be electrical 925 * issues, there's nothing we can do. 926 */ 927 avb->cas_master = false; 928 929 return 0; 930 } 931 932 /* The L2 policing table is 2-stage. The table is looked up for each frame 933 * according to the ingress port, whether it was broadcast or not, and the 934 * classified traffic class (given by VLAN PCP). This portion of the lookup is 935 * fixed, and gives access to the SHARINDX, an indirection register pointing 936 * within the policing table itself, which is used to resolve the policer that 937 * will be used for this frame. 938 * 939 * Stage 1 Stage 2 940 * +------------+--------+ +---------------------------------+ 941 * |Port 0 TC 0 |SHARINDX| | Policer 0: Rate, Burst, MTU | 942 * +------------+--------+ +---------------------------------+ 943 * |Port 0 TC 1 |SHARINDX| | Policer 1: Rate, Burst, MTU | 944 * +------------+--------+ +---------------------------------+ 945 * ... | Policer 2: Rate, Burst, MTU | 946 * +------------+--------+ +---------------------------------+ 947 * |Port 0 TC 7 |SHARINDX| | Policer 3: Rate, Burst, MTU | 948 * +------------+--------+ +---------------------------------+ 949 * |Port 1 TC 0 |SHARINDX| | Policer 4: Rate, Burst, MTU | 950 * +------------+--------+ +---------------------------------+ 951 * ... | Policer 5: Rate, Burst, MTU | 952 * +------------+--------+ +---------------------------------+ 953 * |Port 1 TC 7 |SHARINDX| | Policer 6: Rate, Burst, MTU | 954 * +------------+--------+ +---------------------------------+ 955 * ... | Policer 7: Rate, Burst, MTU | 956 * +------------+--------+ +---------------------------------+ 957 * |Port 4 TC 7 |SHARINDX| ... 958 * +------------+--------+ 959 * |Port 0 BCAST|SHARINDX| ... 960 * +------------+--------+ 961 * |Port 1 BCAST|SHARINDX| ... 962 * +------------+--------+ 963 * ... ... 964 * +------------+--------+ +---------------------------------+ 965 * |Port 4 BCAST|SHARINDX| | Policer 44: Rate, Burst, MTU | 966 * +------------+--------+ +---------------------------------+ 967 * 968 * In this driver, we shall use policers 0-4 as statically alocated port 969 * (matchall) policers. So we need to make the SHARINDX for all lookups 970 * corresponding to this ingress port (8 VLAN PCP lookups and 1 broadcast 971 * lookup) equal. 972 * The remaining policers (40) shall be dynamically allocated for flower 973 * policers, where the key is either vlan_prio or dst_mac ff:ff:ff:ff:ff:ff. 974 */ 975 #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 976 977 static int sja1105_init_l2_policing(struct sja1105_private *priv) 978 { 979 struct sja1105_l2_policing_entry *policing; 980 struct dsa_switch *ds = priv->ds; 981 struct sja1105_table *table; 982 int port, tc; 983 984 table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 985 986 /* Discard previous L2 Policing Table */ 987 if (table->entry_count) { 988 kfree(table->entries); 989 table->entry_count = 0; 990 } 991 992 table->entries = kcalloc(table->ops->max_entry_count, 993 table->ops->unpacked_entry_size, GFP_KERNEL); 994 if (!table->entries) 995 return -ENOMEM; 996 997 table->entry_count = table->ops->max_entry_count; 998 999 policing = table->entries; 1000 1001 /* Setup shared indices for the matchall policers */ 1002 for (port = 0; port < ds->num_ports; port++) { 1003 int mcast = (ds->num_ports * (SJA1105_NUM_TC + 1)) + port; 1004 int bcast = (ds->num_ports * SJA1105_NUM_TC) + port; 1005 1006 for (tc = 0; tc < SJA1105_NUM_TC; tc++) 1007 policing[port * SJA1105_NUM_TC + tc].sharindx = port; 1008 1009 policing[bcast].sharindx = port; 1010 /* Only SJA1110 has multicast policers */ 1011 if (mcast <= table->ops->max_entry_count) 1012 policing[mcast].sharindx = port; 1013 } 1014 1015 /* Setup the matchall policer parameters */ 1016 for (port = 0; port < ds->num_ports; port++) { 1017 int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN; 1018 1019 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 1020 mtu += VLAN_HLEN; 1021 1022 policing[port].smax = 65535; /* Burst size in bytes */ 1023 policing[port].rate = SJA1105_RATE_MBPS(1000); 1024 policing[port].maxlen = mtu; 1025 policing[port].partition = 0; 1026 } 1027 1028 return 0; 1029 } 1030 1031 static int sja1105_static_config_load(struct sja1105_private *priv) 1032 { 1033 int rc; 1034 1035 sja1105_static_config_free(&priv->static_config); 1036 rc = sja1105_static_config_init(&priv->static_config, 1037 priv->info->static_ops, 1038 priv->info->device_id); 1039 if (rc) 1040 return rc; 1041 1042 /* Build static configuration */ 1043 rc = sja1105_init_mac_settings(priv); 1044 if (rc < 0) 1045 return rc; 1046 rc = sja1105_init_mii_settings(priv); 1047 if (rc < 0) 1048 return rc; 1049 rc = sja1105_init_static_fdb(priv); 1050 if (rc < 0) 1051 return rc; 1052 rc = sja1105_init_static_vlan(priv); 1053 if (rc < 0) 1054 return rc; 1055 rc = sja1105_init_l2_lookup_params(priv); 1056 if (rc < 0) 1057 return rc; 1058 rc = sja1105_init_l2_forwarding(priv); 1059 if (rc < 0) 1060 return rc; 1061 rc = sja1105_init_l2_forwarding_params(priv); 1062 if (rc < 0) 1063 return rc; 1064 rc = sja1105_init_l2_policing(priv); 1065 if (rc < 0) 1066 return rc; 1067 rc = sja1105_init_general_params(priv); 1068 if (rc < 0) 1069 return rc; 1070 rc = sja1105_init_avb_params(priv); 1071 if (rc < 0) 1072 return rc; 1073 rc = sja1110_init_pcp_remapping(priv); 1074 if (rc < 0) 1075 return rc; 1076 1077 /* Send initial configuration to hardware via SPI */ 1078 return sja1105_static_config_upload(priv); 1079 } 1080 1081 static int sja1105_parse_rgmii_delays(struct sja1105_private *priv) 1082 { 1083 struct dsa_switch *ds = priv->ds; 1084 int port; 1085 1086 for (port = 0; port < ds->num_ports; port++) { 1087 if (!priv->fixed_link[port]) 1088 continue; 1089 1090 if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_RXID || 1091 priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 1092 priv->rgmii_rx_delay[port] = true; 1093 1094 if (priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_TXID || 1095 priv->phy_mode[port] == PHY_INTERFACE_MODE_RGMII_ID) 1096 priv->rgmii_tx_delay[port] = true; 1097 1098 if ((priv->rgmii_rx_delay[port] || priv->rgmii_tx_delay[port]) && 1099 !priv->info->setup_rgmii_delay) 1100 return -EINVAL; 1101 } 1102 return 0; 1103 } 1104 1105 static int sja1105_parse_ports_node(struct sja1105_private *priv, 1106 struct device_node *ports_node) 1107 { 1108 struct device *dev = &priv->spidev->dev; 1109 struct device_node *child; 1110 1111 for_each_available_child_of_node(ports_node, child) { 1112 struct device_node *phy_node; 1113 phy_interface_t phy_mode; 1114 u32 index; 1115 int err; 1116 1117 /* Get switch port number from DT */ 1118 if (of_property_read_u32(child, "reg", &index) < 0) { 1119 dev_err(dev, "Port number not defined in device tree " 1120 "(property \"reg\")\n"); 1121 of_node_put(child); 1122 return -ENODEV; 1123 } 1124 1125 /* Get PHY mode from DT */ 1126 err = of_get_phy_mode(child, &phy_mode); 1127 if (err) { 1128 dev_err(dev, "Failed to read phy-mode or " 1129 "phy-interface-type property for port %d\n", 1130 index); 1131 of_node_put(child); 1132 return -ENODEV; 1133 } 1134 1135 phy_node = of_parse_phandle(child, "phy-handle", 0); 1136 if (!phy_node) { 1137 if (!of_phy_is_fixed_link(child)) { 1138 dev_err(dev, "phy-handle or fixed-link " 1139 "properties missing!\n"); 1140 of_node_put(child); 1141 return -ENODEV; 1142 } 1143 /* phy-handle is missing, but fixed-link isn't. 1144 * So it's a fixed link. Default to PHY role. 1145 */ 1146 priv->fixed_link[index] = true; 1147 } else { 1148 of_node_put(phy_node); 1149 } 1150 1151 priv->phy_mode[index] = phy_mode; 1152 } 1153 1154 return 0; 1155 } 1156 1157 static int sja1105_parse_dt(struct sja1105_private *priv) 1158 { 1159 struct device *dev = &priv->spidev->dev; 1160 struct device_node *switch_node = dev->of_node; 1161 struct device_node *ports_node; 1162 int rc; 1163 1164 ports_node = of_get_child_by_name(switch_node, "ports"); 1165 if (!ports_node) 1166 ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 1167 if (!ports_node) { 1168 dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 1169 return -ENODEV; 1170 } 1171 1172 rc = sja1105_parse_ports_node(priv, ports_node); 1173 of_node_put(ports_node); 1174 1175 return rc; 1176 } 1177 1178 /* Convert link speed from SJA1105 to ethtool encoding */ 1179 static int sja1105_port_speed_to_ethtool(struct sja1105_private *priv, 1180 u64 speed) 1181 { 1182 if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS]) 1183 return SPEED_10; 1184 if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS]) 1185 return SPEED_100; 1186 if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) 1187 return SPEED_1000; 1188 if (speed == priv->info->port_speed[SJA1105_SPEED_2500MBPS]) 1189 return SPEED_2500; 1190 return SPEED_UNKNOWN; 1191 } 1192 1193 /* Set link speed in the MAC configuration for a specific port. */ 1194 static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 1195 int speed_mbps) 1196 { 1197 struct sja1105_mac_config_entry *mac; 1198 struct device *dev = priv->ds->dev; 1199 u64 speed; 1200 int rc; 1201 1202 /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 1203 * tables. On E/T, MAC reconfig tables are not readable, only writable. 1204 * We have to *know* what the MAC looks like. For the sake of keeping 1205 * the code common, we'll use the static configuration tables as a 1206 * reasonable approximation for both E/T and P/Q/R/S. 1207 */ 1208 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1209 1210 switch (speed_mbps) { 1211 case SPEED_UNKNOWN: 1212 /* PHYLINK called sja1105_mac_config() to inform us about 1213 * the state->interface, but AN has not completed and the 1214 * speed is not yet valid. UM10944.pdf says that setting 1215 * SJA1105_SPEED_AUTO at runtime disables the port, so that is 1216 * ok for power consumption in case AN will never complete - 1217 * otherwise PHYLINK should come back with a new update. 1218 */ 1219 speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 1220 break; 1221 case SPEED_10: 1222 speed = priv->info->port_speed[SJA1105_SPEED_10MBPS]; 1223 break; 1224 case SPEED_100: 1225 speed = priv->info->port_speed[SJA1105_SPEED_100MBPS]; 1226 break; 1227 case SPEED_1000: 1228 speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1229 break; 1230 case SPEED_2500: 1231 speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 1232 break; 1233 default: 1234 dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 1235 return -EINVAL; 1236 } 1237 1238 /* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration 1239 * table, since this will be used for the clocking setup, and we no 1240 * longer need to store it in the static config (already told hardware 1241 * we want auto during upload phase). 1242 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and 1243 * we need to configure the PCS only (if even that). 1244 */ 1245 if (priv->phy_mode[port] == PHY_INTERFACE_MODE_SGMII) 1246 mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS]; 1247 else if (priv->phy_mode[port] == PHY_INTERFACE_MODE_2500BASEX) 1248 mac[port].speed = priv->info->port_speed[SJA1105_SPEED_2500MBPS]; 1249 else 1250 mac[port].speed = speed; 1251 1252 /* Write to the dynamic reconfiguration tables */ 1253 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1254 &mac[port], true); 1255 if (rc < 0) { 1256 dev_err(dev, "Failed to write MAC config: %d\n", rc); 1257 return rc; 1258 } 1259 1260 /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 1261 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 1262 * RMII no change of the clock setup is required. Actually, changing 1263 * the clock setup does interrupt the clock signal for a certain time 1264 * which causes trouble for all PHYs relying on this signal. 1265 */ 1266 if (!phy_interface_mode_is_rgmii(priv->phy_mode[port])) 1267 return 0; 1268 1269 return sja1105_clocking_setup_port(priv, port); 1270 } 1271 1272 /* The SJA1105 MAC programming model is through the static config (the xMII 1273 * Mode table cannot be dynamically reconfigured), and we have to program 1274 * that early (earlier than PHYLINK calls us, anyway). 1275 * So just error out in case the connected PHY attempts to change the initial 1276 * system interface MII protocol from what is defined in the DT, at least for 1277 * now. 1278 */ 1279 static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port, 1280 phy_interface_t interface) 1281 { 1282 return priv->phy_mode[port] != interface; 1283 } 1284 1285 static void sja1105_mac_config(struct dsa_switch *ds, int port, 1286 unsigned int mode, 1287 const struct phylink_link_state *state) 1288 { 1289 struct dsa_port *dp = dsa_to_port(ds, port); 1290 struct sja1105_private *priv = ds->priv; 1291 struct dw_xpcs *xpcs; 1292 1293 if (sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1294 dev_err(ds->dev, "Changing PHY mode to %s not supported!\n", 1295 phy_modes(state->interface)); 1296 return; 1297 } 1298 1299 xpcs = priv->xpcs[port]; 1300 1301 if (xpcs) 1302 phylink_set_pcs(dp->pl, &xpcs->pcs); 1303 } 1304 1305 static void sja1105_mac_link_down(struct dsa_switch *ds, int port, 1306 unsigned int mode, 1307 phy_interface_t interface) 1308 { 1309 sja1105_inhibit_tx(ds->priv, BIT(port), true); 1310 } 1311 1312 static void sja1105_mac_link_up(struct dsa_switch *ds, int port, 1313 unsigned int mode, 1314 phy_interface_t interface, 1315 struct phy_device *phydev, 1316 int speed, int duplex, 1317 bool tx_pause, bool rx_pause) 1318 { 1319 struct sja1105_private *priv = ds->priv; 1320 1321 sja1105_adjust_port_config(priv, port, speed); 1322 1323 sja1105_inhibit_tx(priv, BIT(port), false); 1324 } 1325 1326 static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 1327 unsigned long *supported, 1328 struct phylink_link_state *state) 1329 { 1330 /* Construct a new mask which exhaustively contains all link features 1331 * supported by the MAC, and then apply that (logical AND) to what will 1332 * be sent to the PHY for "marketing". 1333 */ 1334 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 1335 struct sja1105_private *priv = ds->priv; 1336 struct sja1105_xmii_params_entry *mii; 1337 1338 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 1339 1340 /* include/linux/phylink.h says: 1341 * When @state->interface is %PHY_INTERFACE_MODE_NA, phylink 1342 * expects the MAC driver to return all supported link modes. 1343 */ 1344 if (state->interface != PHY_INTERFACE_MODE_NA && 1345 sja1105_phy_mode_mismatch(priv, port, state->interface)) { 1346 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 1347 return; 1348 } 1349 1350 /* The MAC does not support pause frames, and also doesn't 1351 * support half-duplex traffic modes. 1352 */ 1353 phylink_set(mask, Autoneg); 1354 phylink_set(mask, MII); 1355 phylink_set(mask, 10baseT_Full); 1356 phylink_set(mask, 100baseT_Full); 1357 phylink_set(mask, 100baseT1_Full); 1358 if (mii->xmii_mode[port] == XMII_MODE_RGMII || 1359 mii->xmii_mode[port] == XMII_MODE_SGMII) 1360 phylink_set(mask, 1000baseT_Full); 1361 if (priv->info->supports_2500basex[port]) { 1362 phylink_set(mask, 2500baseT_Full); 1363 phylink_set(mask, 2500baseX_Full); 1364 } 1365 1366 bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 1367 bitmap_and(state->advertising, state->advertising, mask, 1368 __ETHTOOL_LINK_MODE_MASK_NBITS); 1369 } 1370 1371 static int 1372 sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port, 1373 const struct sja1105_l2_lookup_entry *requested) 1374 { 1375 struct sja1105_l2_lookup_entry *l2_lookup; 1376 struct sja1105_table *table; 1377 int i; 1378 1379 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 1380 l2_lookup = table->entries; 1381 1382 for (i = 0; i < table->entry_count; i++) 1383 if (l2_lookup[i].macaddr == requested->macaddr && 1384 l2_lookup[i].vlanid == requested->vlanid && 1385 l2_lookup[i].destports & BIT(port)) 1386 return i; 1387 1388 return -1; 1389 } 1390 1391 /* We want FDB entries added statically through the bridge command to persist 1392 * across switch resets, which are a common thing during normal SJA1105 1393 * operation. So we have to back them up in the static configuration tables 1394 * and hence apply them on next static config upload... yay! 1395 */ 1396 static int 1397 sja1105_static_fdb_change(struct sja1105_private *priv, int port, 1398 const struct sja1105_l2_lookup_entry *requested, 1399 bool keep) 1400 { 1401 struct sja1105_l2_lookup_entry *l2_lookup; 1402 struct sja1105_table *table; 1403 int rc, match; 1404 1405 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 1406 1407 match = sja1105_find_static_fdb_entry(priv, port, requested); 1408 if (match < 0) { 1409 /* Can't delete a missing entry. */ 1410 if (!keep) 1411 return 0; 1412 1413 /* No match => new entry */ 1414 rc = sja1105_table_resize(table, table->entry_count + 1); 1415 if (rc) 1416 return rc; 1417 1418 match = table->entry_count - 1; 1419 } 1420 1421 /* Assign pointer after the resize (it may be new memory) */ 1422 l2_lookup = table->entries; 1423 1424 /* We have a match. 1425 * If the job was to add this FDB entry, it's already done (mostly 1426 * anyway, since the port forwarding mask may have changed, case in 1427 * which we update it). 1428 * Otherwise we have to delete it. 1429 */ 1430 if (keep) { 1431 l2_lookup[match] = *requested; 1432 return 0; 1433 } 1434 1435 /* To remove, the strategy is to overwrite the element with 1436 * the last one, and then reduce the array size by 1 1437 */ 1438 l2_lookup[match] = l2_lookup[table->entry_count - 1]; 1439 return sja1105_table_resize(table, table->entry_count - 1); 1440 } 1441 1442 /* First-generation switches have a 4-way set associative TCAM that 1443 * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 1444 * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 1445 * For the placement of a newly learnt FDB entry, the switch selects the bin 1446 * based on a hash function, and the way within that bin incrementally. 1447 */ 1448 static int sja1105et_fdb_index(int bin, int way) 1449 { 1450 return bin * SJA1105ET_FDB_BIN_SIZE + way; 1451 } 1452 1453 static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 1454 const u8 *addr, u16 vid, 1455 struct sja1105_l2_lookup_entry *match, 1456 int *last_unused) 1457 { 1458 int way; 1459 1460 for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 1461 struct sja1105_l2_lookup_entry l2_lookup = {0}; 1462 int index = sja1105et_fdb_index(bin, way); 1463 1464 /* Skip unused entries, optionally marking them 1465 * into the return value 1466 */ 1467 if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1468 index, &l2_lookup)) { 1469 if (last_unused) 1470 *last_unused = way; 1471 continue; 1472 } 1473 1474 if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 1475 l2_lookup.vlanid == vid) { 1476 if (match) 1477 *match = l2_lookup; 1478 return way; 1479 } 1480 } 1481 /* Return an invalid entry index if not found */ 1482 return -1; 1483 } 1484 1485 int sja1105et_fdb_add(struct dsa_switch *ds, int port, 1486 const unsigned char *addr, u16 vid) 1487 { 1488 struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp; 1489 struct sja1105_private *priv = ds->priv; 1490 struct device *dev = ds->dev; 1491 int last_unused = -1; 1492 int start, end, i; 1493 int bin, way, rc; 1494 1495 bin = sja1105et_fdb_hash(priv, addr, vid); 1496 1497 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1498 &l2_lookup, &last_unused); 1499 if (way >= 0) { 1500 /* We have an FDB entry. Is our port in the destination 1501 * mask? If yes, we need to do nothing. If not, we need 1502 * to rewrite the entry by adding this port to it. 1503 */ 1504 if ((l2_lookup.destports & BIT(port)) && l2_lookup.lockeds) 1505 return 0; 1506 l2_lookup.destports |= BIT(port); 1507 } else { 1508 int index = sja1105et_fdb_index(bin, way); 1509 1510 /* We don't have an FDB entry. We construct a new one and 1511 * try to find a place for it within the FDB table. 1512 */ 1513 l2_lookup.macaddr = ether_addr_to_u64(addr); 1514 l2_lookup.destports = BIT(port); 1515 l2_lookup.vlanid = vid; 1516 1517 if (last_unused >= 0) { 1518 way = last_unused; 1519 } else { 1520 /* Bin is full, need to evict somebody. 1521 * Choose victim at random. If you get these messages 1522 * often, you may need to consider changing the 1523 * distribution function: 1524 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 1525 */ 1526 get_random_bytes(&way, sizeof(u8)); 1527 way %= SJA1105ET_FDB_BIN_SIZE; 1528 dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 1529 bin, addr, way); 1530 /* Evict entry */ 1531 sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1532 index, NULL, false); 1533 } 1534 } 1535 l2_lookup.lockeds = true; 1536 l2_lookup.index = sja1105et_fdb_index(bin, way); 1537 1538 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1539 l2_lookup.index, &l2_lookup, 1540 true); 1541 if (rc < 0) 1542 return rc; 1543 1544 /* Invalidate a dynamically learned entry if that exists */ 1545 start = sja1105et_fdb_index(bin, 0); 1546 end = sja1105et_fdb_index(bin, way); 1547 1548 for (i = start; i < end; i++) { 1549 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1550 i, &tmp); 1551 if (rc == -ENOENT) 1552 continue; 1553 if (rc) 1554 return rc; 1555 1556 if (tmp.macaddr != ether_addr_to_u64(addr) || tmp.vlanid != vid) 1557 continue; 1558 1559 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1560 i, NULL, false); 1561 if (rc) 1562 return rc; 1563 1564 break; 1565 } 1566 1567 return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1568 } 1569 1570 int sja1105et_fdb_del(struct dsa_switch *ds, int port, 1571 const unsigned char *addr, u16 vid) 1572 { 1573 struct sja1105_l2_lookup_entry l2_lookup = {0}; 1574 struct sja1105_private *priv = ds->priv; 1575 int index, bin, way, rc; 1576 bool keep; 1577 1578 bin = sja1105et_fdb_hash(priv, addr, vid); 1579 way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 1580 &l2_lookup, NULL); 1581 if (way < 0) 1582 return 0; 1583 index = sja1105et_fdb_index(bin, way); 1584 1585 /* We have an FDB entry. Is our port in the destination mask? If yes, 1586 * we need to remove it. If the resulting port mask becomes empty, we 1587 * need to completely evict the FDB entry. 1588 * Otherwise we just write it back. 1589 */ 1590 l2_lookup.destports &= ~BIT(port); 1591 1592 if (l2_lookup.destports) 1593 keep = true; 1594 else 1595 keep = false; 1596 1597 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1598 index, &l2_lookup, keep); 1599 if (rc < 0) 1600 return rc; 1601 1602 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1603 } 1604 1605 int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 1606 const unsigned char *addr, u16 vid) 1607 { 1608 struct sja1105_l2_lookup_entry l2_lookup = {0}, tmp; 1609 struct sja1105_private *priv = ds->priv; 1610 int rc, i; 1611 1612 /* Search for an existing entry in the FDB table */ 1613 l2_lookup.macaddr = ether_addr_to_u64(addr); 1614 l2_lookup.vlanid = vid; 1615 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 1616 l2_lookup.mask_vlanid = VLAN_VID_MASK; 1617 l2_lookup.destports = BIT(port); 1618 1619 tmp = l2_lookup; 1620 1621 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1622 SJA1105_SEARCH, &tmp); 1623 if (rc == 0 && tmp.index != SJA1105_MAX_L2_LOOKUP_COUNT - 1) { 1624 /* Found a static entry and this port is already in the entry's 1625 * port mask => job done 1626 */ 1627 if ((tmp.destports & BIT(port)) && tmp.lockeds) 1628 return 0; 1629 1630 l2_lookup = tmp; 1631 1632 /* l2_lookup.index is populated by the switch in case it 1633 * found something. 1634 */ 1635 l2_lookup.destports |= BIT(port); 1636 goto skip_finding_an_index; 1637 } 1638 1639 /* Not found, so try to find an unused spot in the FDB. 1640 * This is slightly inefficient because the strategy is knock-knock at 1641 * every possible position from 0 to 1023. 1642 */ 1643 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1644 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1645 i, NULL); 1646 if (rc < 0) 1647 break; 1648 } 1649 if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 1650 dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 1651 return -EINVAL; 1652 } 1653 l2_lookup.index = i; 1654 1655 skip_finding_an_index: 1656 l2_lookup.lockeds = true; 1657 1658 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1659 l2_lookup.index, &l2_lookup, 1660 true); 1661 if (rc < 0) 1662 return rc; 1663 1664 /* The switch learns dynamic entries and looks up the FDB left to 1665 * right. It is possible that our addition was concurrent with the 1666 * dynamic learning of the same address, so now that the static entry 1667 * has been installed, we are certain that address learning for this 1668 * particular address has been turned off, so the dynamic entry either 1669 * is in the FDB at an index smaller than the static one, or isn't (it 1670 * can also be at a larger index, but in that case it is inactive 1671 * because the static FDB entry will match first, and the dynamic one 1672 * will eventually age out). Search for a dynamically learned address 1673 * prior to our static one and invalidate it. 1674 */ 1675 tmp = l2_lookup; 1676 1677 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1678 SJA1105_SEARCH, &tmp); 1679 if (rc < 0) { 1680 dev_err(ds->dev, 1681 "port %d failed to read back entry for %pM vid %d: %pe\n", 1682 port, addr, vid, ERR_PTR(rc)); 1683 return rc; 1684 } 1685 1686 if (tmp.index < l2_lookup.index) { 1687 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1688 tmp.index, NULL, false); 1689 if (rc < 0) 1690 return rc; 1691 } 1692 1693 return sja1105_static_fdb_change(priv, port, &l2_lookup, true); 1694 } 1695 1696 int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 1697 const unsigned char *addr, u16 vid) 1698 { 1699 struct sja1105_l2_lookup_entry l2_lookup = {0}; 1700 struct sja1105_private *priv = ds->priv; 1701 bool keep; 1702 int rc; 1703 1704 l2_lookup.macaddr = ether_addr_to_u64(addr); 1705 l2_lookup.vlanid = vid; 1706 l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 1707 l2_lookup.mask_vlanid = VLAN_VID_MASK; 1708 l2_lookup.destports = BIT(port); 1709 1710 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1711 SJA1105_SEARCH, &l2_lookup); 1712 if (rc < 0) 1713 return 0; 1714 1715 l2_lookup.destports &= ~BIT(port); 1716 1717 /* Decide whether we remove just this port from the FDB entry, 1718 * or if we remove it completely. 1719 */ 1720 if (l2_lookup.destports) 1721 keep = true; 1722 else 1723 keep = false; 1724 1725 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 1726 l2_lookup.index, &l2_lookup, keep); 1727 if (rc < 0) 1728 return rc; 1729 1730 return sja1105_static_fdb_change(priv, port, &l2_lookup, keep); 1731 } 1732 1733 static int sja1105_fdb_add(struct dsa_switch *ds, int port, 1734 const unsigned char *addr, u16 vid) 1735 { 1736 struct sja1105_private *priv = ds->priv; 1737 1738 return priv->info->fdb_add_cmd(ds, port, addr, vid); 1739 } 1740 1741 static int sja1105_fdb_del(struct dsa_switch *ds, int port, 1742 const unsigned char *addr, u16 vid) 1743 { 1744 struct sja1105_private *priv = ds->priv; 1745 1746 return priv->info->fdb_del_cmd(ds, port, addr, vid); 1747 } 1748 1749 static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1750 dsa_fdb_dump_cb_t *cb, void *data) 1751 { 1752 struct sja1105_private *priv = ds->priv; 1753 struct device *dev = ds->dev; 1754 int i; 1755 1756 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1757 struct sja1105_l2_lookup_entry l2_lookup = {0}; 1758 u8 macaddr[ETH_ALEN]; 1759 int rc; 1760 1761 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1762 i, &l2_lookup); 1763 /* No fdb entry at i, not an issue */ 1764 if (rc == -ENOENT) 1765 continue; 1766 if (rc) { 1767 dev_err(dev, "Failed to dump FDB: %d\n", rc); 1768 return rc; 1769 } 1770 1771 /* FDB dump callback is per port. This means we have to 1772 * disregard a valid entry if it's not for this port, even if 1773 * only to revisit it later. This is inefficient because the 1774 * 1024-sized FDB table needs to be traversed 4 times through 1775 * SPI during a 'bridge fdb show' command. 1776 */ 1777 if (!(l2_lookup.destports & BIT(port))) 1778 continue; 1779 1780 /* We need to hide the FDB entry for unknown multicast */ 1781 if (l2_lookup.macaddr == SJA1105_UNKNOWN_MULTICAST && 1782 l2_lookup.mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 1783 continue; 1784 1785 u64_to_ether_addr(l2_lookup.macaddr, macaddr); 1786 1787 /* We need to hide the dsa_8021q VLANs from the user. */ 1788 if (!priv->vlan_aware) 1789 l2_lookup.vlanid = 0; 1790 rc = cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data); 1791 if (rc) 1792 return rc; 1793 } 1794 return 0; 1795 } 1796 1797 static void sja1105_fast_age(struct dsa_switch *ds, int port) 1798 { 1799 struct sja1105_private *priv = ds->priv; 1800 int i; 1801 1802 for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1803 struct sja1105_l2_lookup_entry l2_lookup = {0}; 1804 u8 macaddr[ETH_ALEN]; 1805 int rc; 1806 1807 rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1808 i, &l2_lookup); 1809 /* No fdb entry at i, not an issue */ 1810 if (rc == -ENOENT) 1811 continue; 1812 if (rc) { 1813 dev_err(ds->dev, "Failed to read FDB: %pe\n", 1814 ERR_PTR(rc)); 1815 return; 1816 } 1817 1818 if (!(l2_lookup.destports & BIT(port))) 1819 continue; 1820 1821 /* Don't delete static FDB entries */ 1822 if (l2_lookup.lockeds) 1823 continue; 1824 1825 u64_to_ether_addr(l2_lookup.macaddr, macaddr); 1826 1827 rc = sja1105_fdb_del(ds, port, macaddr, l2_lookup.vlanid); 1828 if (rc) { 1829 dev_err(ds->dev, 1830 "Failed to delete FDB entry %pM vid %lld: %pe\n", 1831 macaddr, l2_lookup.vlanid, ERR_PTR(rc)); 1832 return; 1833 } 1834 } 1835 } 1836 1837 static int sja1105_mdb_add(struct dsa_switch *ds, int port, 1838 const struct switchdev_obj_port_mdb *mdb) 1839 { 1840 return sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1841 } 1842 1843 static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1844 const struct switchdev_obj_port_mdb *mdb) 1845 { 1846 return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1847 } 1848 1849 /* Common function for unicast and broadcast flood configuration. 1850 * Flooding is configured between each {ingress, egress} port pair, and since 1851 * the bridge's semantics are those of "egress flooding", it means we must 1852 * enable flooding towards this port from all ingress ports that are in the 1853 * same forwarding domain. 1854 */ 1855 static int sja1105_manage_flood_domains(struct sja1105_private *priv) 1856 { 1857 struct sja1105_l2_forwarding_entry *l2_fwd; 1858 struct dsa_switch *ds = priv->ds; 1859 int from, to, rc; 1860 1861 l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 1862 1863 for (from = 0; from < ds->num_ports; from++) { 1864 u64 fl_domain = 0, bc_domain = 0; 1865 1866 for (to = 0; to < priv->ds->num_ports; to++) { 1867 if (!sja1105_can_forward(l2_fwd, from, to)) 1868 continue; 1869 1870 if (priv->ucast_egress_floods & BIT(to)) 1871 fl_domain |= BIT(to); 1872 if (priv->bcast_egress_floods & BIT(to)) 1873 bc_domain |= BIT(to); 1874 } 1875 1876 /* Nothing changed, nothing to do */ 1877 if (l2_fwd[from].fl_domain == fl_domain && 1878 l2_fwd[from].bc_domain == bc_domain) 1879 continue; 1880 1881 l2_fwd[from].fl_domain = fl_domain; 1882 l2_fwd[from].bc_domain = bc_domain; 1883 1884 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 1885 from, &l2_fwd[from], true); 1886 if (rc < 0) 1887 return rc; 1888 } 1889 1890 return 0; 1891 } 1892 1893 static int sja1105_bridge_member(struct dsa_switch *ds, int port, 1894 struct net_device *br, bool member) 1895 { 1896 struct sja1105_l2_forwarding_entry *l2_fwd; 1897 struct sja1105_private *priv = ds->priv; 1898 int i, rc; 1899 1900 l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 1901 1902 for (i = 0; i < ds->num_ports; i++) { 1903 /* Add this port to the forwarding matrix of the 1904 * other ports in the same bridge, and viceversa. 1905 */ 1906 if (!dsa_is_user_port(ds, i)) 1907 continue; 1908 /* For the ports already under the bridge, only one thing needs 1909 * to be done, and that is to add this port to their 1910 * reachability domain. So we can perform the SPI write for 1911 * them immediately. However, for this port itself (the one 1912 * that is new to the bridge), we need to add all other ports 1913 * to its reachability domain. So we do that incrementally in 1914 * this loop, and perform the SPI write only at the end, once 1915 * the domain contains all other bridge ports. 1916 */ 1917 if (i == port) 1918 continue; 1919 if (dsa_to_port(ds, i)->bridge_dev != br) 1920 continue; 1921 sja1105_port_allow_traffic(l2_fwd, i, port, member); 1922 sja1105_port_allow_traffic(l2_fwd, port, i, member); 1923 1924 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 1925 i, &l2_fwd[i], true); 1926 if (rc < 0) 1927 return rc; 1928 } 1929 1930 rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 1931 port, &l2_fwd[port], true); 1932 if (rc) 1933 return rc; 1934 1935 rc = sja1105_commit_pvid(ds, port); 1936 if (rc) 1937 return rc; 1938 1939 return sja1105_manage_flood_domains(priv); 1940 } 1941 1942 static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1943 u8 state) 1944 { 1945 struct dsa_port *dp = dsa_to_port(ds, port); 1946 struct sja1105_private *priv = ds->priv; 1947 struct sja1105_mac_config_entry *mac; 1948 1949 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1950 1951 switch (state) { 1952 case BR_STATE_DISABLED: 1953 case BR_STATE_BLOCKING: 1954 /* From UM10944 description of DRPDTAG (why put this there?): 1955 * "Management traffic flows to the port regardless of the state 1956 * of the INGRESS flag". So BPDUs are still be allowed to pass. 1957 * At the moment no difference between DISABLED and BLOCKING. 1958 */ 1959 mac[port].ingress = false; 1960 mac[port].egress = false; 1961 mac[port].dyn_learn = false; 1962 break; 1963 case BR_STATE_LISTENING: 1964 mac[port].ingress = true; 1965 mac[port].egress = false; 1966 mac[port].dyn_learn = false; 1967 break; 1968 case BR_STATE_LEARNING: 1969 mac[port].ingress = true; 1970 mac[port].egress = false; 1971 mac[port].dyn_learn = dp->learning; 1972 break; 1973 case BR_STATE_FORWARDING: 1974 mac[port].ingress = true; 1975 mac[port].egress = true; 1976 mac[port].dyn_learn = dp->learning; 1977 break; 1978 default: 1979 dev_err(ds->dev, "invalid STP state: %d\n", state); 1980 return; 1981 } 1982 1983 sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1984 &mac[port], true); 1985 } 1986 1987 static int sja1105_bridge_join(struct dsa_switch *ds, int port, 1988 struct net_device *br) 1989 { 1990 return sja1105_bridge_member(ds, port, br, true); 1991 } 1992 1993 static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 1994 struct net_device *br) 1995 { 1996 sja1105_bridge_member(ds, port, br, false); 1997 } 1998 1999 #define BYTES_PER_KBIT (1000LL / 8) 2000 2001 static int sja1105_find_unused_cbs_shaper(struct sja1105_private *priv) 2002 { 2003 int i; 2004 2005 for (i = 0; i < priv->info->num_cbs_shapers; i++) 2006 if (!priv->cbs[i].idle_slope && !priv->cbs[i].send_slope) 2007 return i; 2008 2009 return -1; 2010 } 2011 2012 static int sja1105_delete_cbs_shaper(struct sja1105_private *priv, int port, 2013 int prio) 2014 { 2015 int i; 2016 2017 for (i = 0; i < priv->info->num_cbs_shapers; i++) { 2018 struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 2019 2020 if (cbs->port == port && cbs->prio == prio) { 2021 memset(cbs, 0, sizeof(*cbs)); 2022 return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, 2023 i, cbs, true); 2024 } 2025 } 2026 2027 return 0; 2028 } 2029 2030 static int sja1105_setup_tc_cbs(struct dsa_switch *ds, int port, 2031 struct tc_cbs_qopt_offload *offload) 2032 { 2033 struct sja1105_private *priv = ds->priv; 2034 struct sja1105_cbs_entry *cbs; 2035 int index; 2036 2037 if (!offload->enable) 2038 return sja1105_delete_cbs_shaper(priv, port, offload->queue); 2039 2040 index = sja1105_find_unused_cbs_shaper(priv); 2041 if (index < 0) 2042 return -ENOSPC; 2043 2044 cbs = &priv->cbs[index]; 2045 cbs->port = port; 2046 cbs->prio = offload->queue; 2047 /* locredit and sendslope are negative by definition. In hardware, 2048 * positive values must be provided, and the negative sign is implicit. 2049 */ 2050 cbs->credit_hi = offload->hicredit; 2051 cbs->credit_lo = abs(offload->locredit); 2052 /* User space is in kbits/sec, hardware in bytes/sec */ 2053 cbs->idle_slope = offload->idleslope * BYTES_PER_KBIT; 2054 cbs->send_slope = abs(offload->sendslope * BYTES_PER_KBIT); 2055 /* Convert the negative values from 64-bit 2's complement 2056 * to 32-bit 2's complement (for the case of 0x80000000 whose 2057 * negative is still negative). 2058 */ 2059 cbs->credit_lo &= GENMASK_ULL(31, 0); 2060 cbs->send_slope &= GENMASK_ULL(31, 0); 2061 2062 return sja1105_dynamic_config_write(priv, BLK_IDX_CBS, index, cbs, 2063 true); 2064 } 2065 2066 static int sja1105_reload_cbs(struct sja1105_private *priv) 2067 { 2068 int rc = 0, i; 2069 2070 /* The credit based shapers are only allocated if 2071 * CONFIG_NET_SCH_CBS is enabled. 2072 */ 2073 if (!priv->cbs) 2074 return 0; 2075 2076 for (i = 0; i < priv->info->num_cbs_shapers; i++) { 2077 struct sja1105_cbs_entry *cbs = &priv->cbs[i]; 2078 2079 if (!cbs->idle_slope && !cbs->send_slope) 2080 continue; 2081 2082 rc = sja1105_dynamic_config_write(priv, BLK_IDX_CBS, i, cbs, 2083 true); 2084 if (rc) 2085 break; 2086 } 2087 2088 return rc; 2089 } 2090 2091 static const char * const sja1105_reset_reasons[] = { 2092 [SJA1105_VLAN_FILTERING] = "VLAN filtering", 2093 [SJA1105_RX_HWTSTAMPING] = "RX timestamping", 2094 [SJA1105_AGEING_TIME] = "Ageing time", 2095 [SJA1105_SCHEDULING] = "Time-aware scheduling", 2096 [SJA1105_BEST_EFFORT_POLICING] = "Best-effort policing", 2097 [SJA1105_VIRTUAL_LINKS] = "Virtual links", 2098 }; 2099 2100 /* For situations where we need to change a setting at runtime that is only 2101 * available through the static configuration, resetting the switch in order 2102 * to upload the new static config is unavoidable. Back up the settings we 2103 * modify at runtime (currently only MAC) and restore them after uploading, 2104 * such that this operation is relatively seamless. 2105 */ 2106 int sja1105_static_config_reload(struct sja1105_private *priv, 2107 enum sja1105_reset_reason reason) 2108 { 2109 struct ptp_system_timestamp ptp_sts_before; 2110 struct ptp_system_timestamp ptp_sts_after; 2111 int speed_mbps[SJA1105_MAX_NUM_PORTS]; 2112 u16 bmcr[SJA1105_MAX_NUM_PORTS] = {0}; 2113 struct sja1105_mac_config_entry *mac; 2114 struct dsa_switch *ds = priv->ds; 2115 s64 t1, t2, t3, t4; 2116 s64 t12, t34; 2117 int rc, i; 2118 s64 now; 2119 2120 mutex_lock(&priv->mgmt_lock); 2121 2122 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 2123 2124 /* Back up the dynamic link speed changed by sja1105_adjust_port_config 2125 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the 2126 * switch wants to see in the static config in order to allow us to 2127 * change it through the dynamic interface later. 2128 */ 2129 for (i = 0; i < ds->num_ports; i++) { 2130 u32 reg_addr = mdiobus_c45_addr(MDIO_MMD_VEND2, MDIO_CTRL1); 2131 2132 speed_mbps[i] = sja1105_port_speed_to_ethtool(priv, 2133 mac[i].speed); 2134 mac[i].speed = priv->info->port_speed[SJA1105_SPEED_AUTO]; 2135 2136 if (priv->xpcs[i]) 2137 bmcr[i] = mdiobus_read(priv->mdio_pcs, i, reg_addr); 2138 } 2139 2140 /* No PTP operations can run right now */ 2141 mutex_lock(&priv->ptp_data.lock); 2142 2143 rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before); 2144 if (rc < 0) { 2145 mutex_unlock(&priv->ptp_data.lock); 2146 goto out; 2147 } 2148 2149 /* Reset switch and send updated static configuration */ 2150 rc = sja1105_static_config_upload(priv); 2151 if (rc < 0) { 2152 mutex_unlock(&priv->ptp_data.lock); 2153 goto out; 2154 } 2155 2156 rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after); 2157 if (rc < 0) { 2158 mutex_unlock(&priv->ptp_data.lock); 2159 goto out; 2160 } 2161 2162 t1 = timespec64_to_ns(&ptp_sts_before.pre_ts); 2163 t2 = timespec64_to_ns(&ptp_sts_before.post_ts); 2164 t3 = timespec64_to_ns(&ptp_sts_after.pre_ts); 2165 t4 = timespec64_to_ns(&ptp_sts_after.post_ts); 2166 /* Mid point, corresponds to pre-reset PTPCLKVAL */ 2167 t12 = t1 + (t2 - t1) / 2; 2168 /* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */ 2169 t34 = t3 + (t4 - t3) / 2; 2170 /* Advance PTPCLKVAL by the time it took since its readout */ 2171 now += (t34 - t12); 2172 2173 __sja1105_ptp_adjtime(ds, now); 2174 2175 mutex_unlock(&priv->ptp_data.lock); 2176 2177 dev_info(priv->ds->dev, 2178 "Reset switch and programmed static config. Reason: %s\n", 2179 sja1105_reset_reasons[reason]); 2180 2181 /* Configure the CGU (PLLs) for MII and RMII PHYs. 2182 * For these interfaces there is no dynamic configuration 2183 * needed, since PLLs have same settings at all speeds. 2184 */ 2185 if (priv->info->clocking_setup) { 2186 rc = priv->info->clocking_setup(priv); 2187 if (rc < 0) 2188 goto out; 2189 } 2190 2191 for (i = 0; i < ds->num_ports; i++) { 2192 struct dw_xpcs *xpcs = priv->xpcs[i]; 2193 unsigned int mode; 2194 2195 rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]); 2196 if (rc < 0) 2197 goto out; 2198 2199 if (!xpcs) 2200 continue; 2201 2202 if (bmcr[i] & BMCR_ANENABLE) 2203 mode = MLO_AN_INBAND; 2204 else if (priv->fixed_link[i]) 2205 mode = MLO_AN_FIXED; 2206 else 2207 mode = MLO_AN_PHY; 2208 2209 rc = xpcs_do_config(xpcs, priv->phy_mode[i], mode); 2210 if (rc < 0) 2211 goto out; 2212 2213 if (!phylink_autoneg_inband(mode)) { 2214 int speed = SPEED_UNKNOWN; 2215 2216 if (priv->phy_mode[i] == PHY_INTERFACE_MODE_2500BASEX) 2217 speed = SPEED_2500; 2218 else if (bmcr[i] & BMCR_SPEED1000) 2219 speed = SPEED_1000; 2220 else if (bmcr[i] & BMCR_SPEED100) 2221 speed = SPEED_100; 2222 else 2223 speed = SPEED_10; 2224 2225 xpcs_link_up(&xpcs->pcs, mode, priv->phy_mode[i], 2226 speed, DUPLEX_FULL); 2227 } 2228 } 2229 2230 rc = sja1105_reload_cbs(priv); 2231 if (rc < 0) 2232 goto out; 2233 out: 2234 mutex_unlock(&priv->mgmt_lock); 2235 2236 return rc; 2237 } 2238 2239 static enum dsa_tag_protocol 2240 sja1105_get_tag_protocol(struct dsa_switch *ds, int port, 2241 enum dsa_tag_protocol mp) 2242 { 2243 struct sja1105_private *priv = ds->priv; 2244 2245 return priv->info->tag_proto; 2246 } 2247 2248 /* The TPID setting belongs to the General Parameters table, 2249 * which can only be partially reconfigured at runtime (and not the TPID). 2250 * So a switch reset is required. 2251 */ 2252 int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 2253 struct netlink_ext_ack *extack) 2254 { 2255 struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2256 struct sja1105_general_params_entry *general_params; 2257 struct sja1105_private *priv = ds->priv; 2258 struct sja1105_table *table; 2259 struct sja1105_rule *rule; 2260 u16 tpid, tpid2; 2261 int rc; 2262 2263 list_for_each_entry(rule, &priv->flow_block.rules, list) { 2264 if (rule->type == SJA1105_RULE_VL) { 2265 NL_SET_ERR_MSG_MOD(extack, 2266 "Cannot change VLAN filtering with active VL rules"); 2267 return -EBUSY; 2268 } 2269 } 2270 2271 if (enabled) { 2272 /* Enable VLAN filtering. */ 2273 tpid = ETH_P_8021Q; 2274 tpid2 = ETH_P_8021AD; 2275 } else { 2276 /* Disable VLAN filtering. */ 2277 tpid = ETH_P_SJA1105; 2278 tpid2 = ETH_P_SJA1105; 2279 } 2280 2281 for (port = 0; port < ds->num_ports; port++) { 2282 struct sja1105_port *sp = &priv->ports[port]; 2283 2284 if (enabled) 2285 sp->xmit_tpid = priv->info->qinq_tpid; 2286 else 2287 sp->xmit_tpid = ETH_P_SJA1105; 2288 } 2289 2290 if (priv->vlan_aware == enabled) 2291 return 0; 2292 2293 priv->vlan_aware = enabled; 2294 2295 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2296 general_params = table->entries; 2297 /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 2298 general_params->tpid = tpid; 2299 /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 2300 general_params->tpid2 = tpid2; 2301 /* When VLAN filtering is on, we need to at least be able to 2302 * decode management traffic through the "backup plan". 2303 */ 2304 general_params->incl_srcpt1 = enabled; 2305 general_params->incl_srcpt0 = enabled; 2306 2307 /* VLAN filtering => independent VLAN learning. 2308 * No VLAN filtering (or best effort) => shared VLAN learning. 2309 * 2310 * In shared VLAN learning mode, untagged traffic still gets 2311 * pvid-tagged, and the FDB table gets populated with entries 2312 * containing the "real" (pvid or from VLAN tag) VLAN ID. 2313 * However the switch performs a masked L2 lookup in the FDB, 2314 * effectively only looking up a frame's DMAC (and not VID) for the 2315 * forwarding decision. 2316 * 2317 * This is extremely convenient for us, because in modes with 2318 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into 2319 * each front panel port. This is good for identification but breaks 2320 * learning badly - the VID of the learnt FDB entry is unique, aka 2321 * no frames coming from any other port are going to have it. So 2322 * for forwarding purposes, this is as though learning was broken 2323 * (all frames get flooded). 2324 */ 2325 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 2326 l2_lookup_params = table->entries; 2327 l2_lookup_params->shared_learn = !priv->vlan_aware; 2328 2329 for (port = 0; port < ds->num_ports; port++) { 2330 if (dsa_is_unused_port(ds, port)) 2331 continue; 2332 2333 rc = sja1105_commit_pvid(ds, port); 2334 if (rc) 2335 return rc; 2336 } 2337 2338 rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING); 2339 if (rc) 2340 NL_SET_ERR_MSG_MOD(extack, "Failed to change VLAN Ethertype"); 2341 2342 return rc; 2343 } 2344 2345 static int sja1105_vlan_add(struct sja1105_private *priv, int port, u16 vid, 2346 u16 flags) 2347 { 2348 struct sja1105_vlan_lookup_entry *vlan; 2349 struct sja1105_table *table; 2350 int match, rc; 2351 2352 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2353 2354 match = sja1105_is_vlan_configured(priv, vid); 2355 if (match < 0) { 2356 rc = sja1105_table_resize(table, table->entry_count + 1); 2357 if (rc) 2358 return rc; 2359 match = table->entry_count - 1; 2360 } 2361 2362 /* Assign pointer after the resize (it's new memory) */ 2363 vlan = table->entries; 2364 2365 vlan[match].type_entry = SJA1110_VLAN_D_TAG; 2366 vlan[match].vlanid = vid; 2367 vlan[match].vlan_bc |= BIT(port); 2368 vlan[match].vmemb_port |= BIT(port); 2369 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) 2370 vlan[match].tag_port &= ~BIT(port); 2371 else 2372 vlan[match].tag_port |= BIT(port); 2373 2374 return sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 2375 &vlan[match], true); 2376 } 2377 2378 static int sja1105_vlan_del(struct sja1105_private *priv, int port, u16 vid) 2379 { 2380 struct sja1105_vlan_lookup_entry *vlan; 2381 struct sja1105_table *table; 2382 bool keep = true; 2383 int match, rc; 2384 2385 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2386 2387 match = sja1105_is_vlan_configured(priv, vid); 2388 /* Can't delete a missing entry. */ 2389 if (match < 0) 2390 return 0; 2391 2392 /* Assign pointer after the resize (it's new memory) */ 2393 vlan = table->entries; 2394 2395 vlan[match].vlanid = vid; 2396 vlan[match].vlan_bc &= ~BIT(port); 2397 vlan[match].vmemb_port &= ~BIT(port); 2398 /* Also unset tag_port, just so we don't have a confusing bitmap 2399 * (no practical purpose). 2400 */ 2401 vlan[match].tag_port &= ~BIT(port); 2402 2403 /* If there's no port left as member of this VLAN, 2404 * it's time for it to go. 2405 */ 2406 if (!vlan[match].vmemb_port) 2407 keep = false; 2408 2409 rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 2410 &vlan[match], keep); 2411 if (rc < 0) 2412 return rc; 2413 2414 if (!keep) 2415 return sja1105_table_delete_entry(table, match); 2416 2417 return 0; 2418 } 2419 2420 static int sja1105_bridge_vlan_add(struct dsa_switch *ds, int port, 2421 const struct switchdev_obj_port_vlan *vlan, 2422 struct netlink_ext_ack *extack) 2423 { 2424 struct sja1105_private *priv = ds->priv; 2425 u16 flags = vlan->flags; 2426 int rc; 2427 2428 /* Be sure to deny alterations to the configuration done by tag_8021q. 2429 */ 2430 if (vid_is_dsa_8021q(vlan->vid)) { 2431 NL_SET_ERR_MSG_MOD(extack, 2432 "Range 1024-3071 reserved for dsa_8021q operation"); 2433 return -EBUSY; 2434 } 2435 2436 /* Always install bridge VLANs as egress-tagged on CPU and DSA ports */ 2437 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 2438 flags = 0; 2439 2440 rc = sja1105_vlan_add(priv, port, vlan->vid, flags); 2441 if (rc) 2442 return rc; 2443 2444 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 2445 priv->bridge_pvid[port] = vlan->vid; 2446 2447 return sja1105_commit_pvid(ds, port); 2448 } 2449 2450 static int sja1105_bridge_vlan_del(struct dsa_switch *ds, int port, 2451 const struct switchdev_obj_port_vlan *vlan) 2452 { 2453 struct sja1105_private *priv = ds->priv; 2454 int rc; 2455 2456 rc = sja1105_vlan_del(priv, port, vlan->vid); 2457 if (rc) 2458 return rc; 2459 2460 /* In case the pvid was deleted, make sure that untagged packets will 2461 * be dropped. 2462 */ 2463 return sja1105_commit_pvid(ds, port); 2464 } 2465 2466 static int sja1105_dsa_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 2467 u16 flags) 2468 { 2469 struct sja1105_private *priv = ds->priv; 2470 int rc; 2471 2472 rc = sja1105_vlan_add(priv, port, vid, flags); 2473 if (rc) 2474 return rc; 2475 2476 if (flags & BRIDGE_VLAN_INFO_PVID) 2477 priv->tag_8021q_pvid[port] = vid; 2478 2479 return sja1105_commit_pvid(ds, port); 2480 } 2481 2482 static int sja1105_dsa_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 2483 { 2484 struct sja1105_private *priv = ds->priv; 2485 2486 return sja1105_vlan_del(priv, port, vid); 2487 } 2488 2489 static int sja1105_prechangeupper(struct dsa_switch *ds, int port, 2490 struct netdev_notifier_changeupper_info *info) 2491 { 2492 struct netlink_ext_ack *extack = info->info.extack; 2493 struct net_device *upper = info->upper_dev; 2494 struct dsa_switch_tree *dst = ds->dst; 2495 struct dsa_port *dp; 2496 2497 if (is_vlan_dev(upper)) { 2498 NL_SET_ERR_MSG_MOD(extack, "8021q uppers are not supported"); 2499 return -EBUSY; 2500 } 2501 2502 if (netif_is_bridge_master(upper)) { 2503 list_for_each_entry(dp, &dst->ports, list) { 2504 if (dp->bridge_dev && dp->bridge_dev != upper && 2505 br_vlan_enabled(dp->bridge_dev)) { 2506 NL_SET_ERR_MSG_MOD(extack, 2507 "Only one VLAN-aware bridge is supported"); 2508 return -EBUSY; 2509 } 2510 } 2511 } 2512 2513 return 0; 2514 } 2515 2516 static void sja1105_port_disable(struct dsa_switch *ds, int port) 2517 { 2518 struct sja1105_private *priv = ds->priv; 2519 struct sja1105_port *sp = &priv->ports[port]; 2520 2521 if (!dsa_is_user_port(ds, port)) 2522 return; 2523 2524 kthread_cancel_work_sync(&sp->xmit_work); 2525 skb_queue_purge(&sp->xmit_queue); 2526 } 2527 2528 static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 2529 struct sk_buff *skb, bool takets) 2530 { 2531 struct sja1105_mgmt_entry mgmt_route = {0}; 2532 struct sja1105_private *priv = ds->priv; 2533 struct ethhdr *hdr; 2534 int timeout = 10; 2535 int rc; 2536 2537 hdr = eth_hdr(skb); 2538 2539 mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 2540 mgmt_route.destports = BIT(port); 2541 mgmt_route.enfport = 1; 2542 mgmt_route.tsreg = 0; 2543 mgmt_route.takets = takets; 2544 2545 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 2546 slot, &mgmt_route, true); 2547 if (rc < 0) { 2548 kfree_skb(skb); 2549 return rc; 2550 } 2551 2552 /* Transfer skb to the host port. */ 2553 dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave); 2554 2555 /* Wait until the switch has processed the frame */ 2556 do { 2557 rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 2558 slot, &mgmt_route); 2559 if (rc < 0) { 2560 dev_err_ratelimited(priv->ds->dev, 2561 "failed to poll for mgmt route\n"); 2562 continue; 2563 } 2564 2565 /* UM10944: The ENFPORT flag of the respective entry is 2566 * cleared when a match is found. The host can use this 2567 * flag as an acknowledgment. 2568 */ 2569 cpu_relax(); 2570 } while (mgmt_route.enfport && --timeout); 2571 2572 if (!timeout) { 2573 /* Clean up the management route so that a follow-up 2574 * frame may not match on it by mistake. 2575 * This is only hardware supported on P/Q/R/S - on E/T it is 2576 * a no-op and we are silently discarding the -EOPNOTSUPP. 2577 */ 2578 sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 2579 slot, &mgmt_route, false); 2580 dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 2581 } 2582 2583 return NETDEV_TX_OK; 2584 } 2585 2586 #define work_to_port(work) \ 2587 container_of((work), struct sja1105_port, xmit_work) 2588 #define tagger_to_sja1105(t) \ 2589 container_of((t), struct sja1105_private, tagger_data) 2590 2591 /* Deferred work is unfortunately necessary because setting up the management 2592 * route cannot be done from atomit context (SPI transfer takes a sleepable 2593 * lock on the bus) 2594 */ 2595 static void sja1105_port_deferred_xmit(struct kthread_work *work) 2596 { 2597 struct sja1105_port *sp = work_to_port(work); 2598 struct sja1105_tagger_data *tagger_data = sp->data; 2599 struct sja1105_private *priv = tagger_to_sja1105(tagger_data); 2600 int port = sp - priv->ports; 2601 struct sk_buff *skb; 2602 2603 while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) { 2604 struct sk_buff *clone = SJA1105_SKB_CB(skb)->clone; 2605 2606 mutex_lock(&priv->mgmt_lock); 2607 2608 sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone); 2609 2610 /* The clone, if there, was made by dsa_skb_tx_timestamp */ 2611 if (clone) 2612 sja1105_ptp_txtstamp_skb(priv->ds, port, clone); 2613 2614 mutex_unlock(&priv->mgmt_lock); 2615 } 2616 } 2617 2618 /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 2619 * which cannot be reconfigured at runtime. So a switch reset is required. 2620 */ 2621 static int sja1105_set_ageing_time(struct dsa_switch *ds, 2622 unsigned int ageing_time) 2623 { 2624 struct sja1105_l2_lookup_params_entry *l2_lookup_params; 2625 struct sja1105_private *priv = ds->priv; 2626 struct sja1105_table *table; 2627 unsigned int maxage; 2628 2629 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 2630 l2_lookup_params = table->entries; 2631 2632 maxage = SJA1105_AGEING_TIME_MS(ageing_time); 2633 2634 if (l2_lookup_params->maxage == maxage) 2635 return 0; 2636 2637 l2_lookup_params->maxage = maxage; 2638 2639 return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME); 2640 } 2641 2642 static int sja1105_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 2643 { 2644 struct sja1105_l2_policing_entry *policing; 2645 struct sja1105_private *priv = ds->priv; 2646 2647 new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN; 2648 2649 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) 2650 new_mtu += VLAN_HLEN; 2651 2652 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2653 2654 if (policing[port].maxlen == new_mtu) 2655 return 0; 2656 2657 policing[port].maxlen = new_mtu; 2658 2659 return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2660 } 2661 2662 static int sja1105_get_max_mtu(struct dsa_switch *ds, int port) 2663 { 2664 return 2043 - VLAN_ETH_HLEN - ETH_FCS_LEN; 2665 } 2666 2667 static int sja1105_port_setup_tc(struct dsa_switch *ds, int port, 2668 enum tc_setup_type type, 2669 void *type_data) 2670 { 2671 switch (type) { 2672 case TC_SETUP_QDISC_TAPRIO: 2673 return sja1105_setup_tc_taprio(ds, port, type_data); 2674 case TC_SETUP_QDISC_CBS: 2675 return sja1105_setup_tc_cbs(ds, port, type_data); 2676 default: 2677 return -EOPNOTSUPP; 2678 } 2679 } 2680 2681 /* We have a single mirror (@to) port, but can configure ingress and egress 2682 * mirroring on all other (@from) ports. 2683 * We need to allow mirroring rules only as long as the @to port is always the 2684 * same, and we need to unset the @to port from mirr_port only when there is no 2685 * mirroring rule that references it. 2686 */ 2687 static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to, 2688 bool ingress, bool enabled) 2689 { 2690 struct sja1105_general_params_entry *general_params; 2691 struct sja1105_mac_config_entry *mac; 2692 struct dsa_switch *ds = priv->ds; 2693 struct sja1105_table *table; 2694 bool already_enabled; 2695 u64 new_mirr_port; 2696 int rc; 2697 2698 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 2699 general_params = table->entries; 2700 2701 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 2702 2703 already_enabled = (general_params->mirr_port != ds->num_ports); 2704 if (already_enabled && enabled && general_params->mirr_port != to) { 2705 dev_err(priv->ds->dev, 2706 "Delete mirroring rules towards port %llu first\n", 2707 general_params->mirr_port); 2708 return -EBUSY; 2709 } 2710 2711 new_mirr_port = to; 2712 if (!enabled) { 2713 bool keep = false; 2714 int port; 2715 2716 /* Anybody still referencing mirr_port? */ 2717 for (port = 0; port < ds->num_ports; port++) { 2718 if (mac[port].ing_mirr || mac[port].egr_mirr) { 2719 keep = true; 2720 break; 2721 } 2722 } 2723 /* Unset already_enabled for next time */ 2724 if (!keep) 2725 new_mirr_port = ds->num_ports; 2726 } 2727 if (new_mirr_port != general_params->mirr_port) { 2728 general_params->mirr_port = new_mirr_port; 2729 2730 rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS, 2731 0, general_params, true); 2732 if (rc < 0) 2733 return rc; 2734 } 2735 2736 if (ingress) 2737 mac[from].ing_mirr = enabled; 2738 else 2739 mac[from].egr_mirr = enabled; 2740 2741 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from, 2742 &mac[from], true); 2743 } 2744 2745 static int sja1105_mirror_add(struct dsa_switch *ds, int port, 2746 struct dsa_mall_mirror_tc_entry *mirror, 2747 bool ingress) 2748 { 2749 return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 2750 ingress, true); 2751 } 2752 2753 static void sja1105_mirror_del(struct dsa_switch *ds, int port, 2754 struct dsa_mall_mirror_tc_entry *mirror) 2755 { 2756 sja1105_mirror_apply(ds->priv, port, mirror->to_local_port, 2757 mirror->ingress, false); 2758 } 2759 2760 static int sja1105_port_policer_add(struct dsa_switch *ds, int port, 2761 struct dsa_mall_policer_tc_entry *policer) 2762 { 2763 struct sja1105_l2_policing_entry *policing; 2764 struct sja1105_private *priv = ds->priv; 2765 2766 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2767 2768 /* In hardware, every 8 microseconds the credit level is incremented by 2769 * the value of RATE bytes divided by 64, up to a maximum of SMAX 2770 * bytes. 2771 */ 2772 policing[port].rate = div_u64(512 * policer->rate_bytes_per_sec, 2773 1000000); 2774 policing[port].smax = policer->burst; 2775 2776 return sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2777 } 2778 2779 static void sja1105_port_policer_del(struct dsa_switch *ds, int port) 2780 { 2781 struct sja1105_l2_policing_entry *policing; 2782 struct sja1105_private *priv = ds->priv; 2783 2784 policing = priv->static_config.tables[BLK_IDX_L2_POLICING].entries; 2785 2786 policing[port].rate = SJA1105_RATE_MBPS(1000); 2787 policing[port].smax = 65535; 2788 2789 sja1105_static_config_reload(priv, SJA1105_BEST_EFFORT_POLICING); 2790 } 2791 2792 static int sja1105_port_set_learning(struct sja1105_private *priv, int port, 2793 bool enabled) 2794 { 2795 struct sja1105_mac_config_entry *mac; 2796 2797 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 2798 2799 mac[port].dyn_learn = enabled; 2800 2801 return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 2802 &mac[port], true); 2803 } 2804 2805 static int sja1105_port_ucast_bcast_flood(struct sja1105_private *priv, int to, 2806 struct switchdev_brport_flags flags) 2807 { 2808 if (flags.mask & BR_FLOOD) { 2809 if (flags.val & BR_FLOOD) 2810 priv->ucast_egress_floods |= BIT(to); 2811 else 2812 priv->ucast_egress_floods &= ~BIT(to); 2813 } 2814 2815 if (flags.mask & BR_BCAST_FLOOD) { 2816 if (flags.val & BR_BCAST_FLOOD) 2817 priv->bcast_egress_floods |= BIT(to); 2818 else 2819 priv->bcast_egress_floods &= ~BIT(to); 2820 } 2821 2822 return sja1105_manage_flood_domains(priv); 2823 } 2824 2825 static int sja1105_port_mcast_flood(struct sja1105_private *priv, int to, 2826 struct switchdev_brport_flags flags, 2827 struct netlink_ext_ack *extack) 2828 { 2829 struct sja1105_l2_lookup_entry *l2_lookup; 2830 struct sja1105_table *table; 2831 int match; 2832 2833 table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 2834 l2_lookup = table->entries; 2835 2836 for (match = 0; match < table->entry_count; match++) 2837 if (l2_lookup[match].macaddr == SJA1105_UNKNOWN_MULTICAST && 2838 l2_lookup[match].mask_macaddr == SJA1105_UNKNOWN_MULTICAST) 2839 break; 2840 2841 if (match == table->entry_count) { 2842 NL_SET_ERR_MSG_MOD(extack, 2843 "Could not find FDB entry for unknown multicast"); 2844 return -ENOSPC; 2845 } 2846 2847 if (flags.val & BR_MCAST_FLOOD) 2848 l2_lookup[match].destports |= BIT(to); 2849 else 2850 l2_lookup[match].destports &= ~BIT(to); 2851 2852 return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 2853 l2_lookup[match].index, 2854 &l2_lookup[match], 2855 true); 2856 } 2857 2858 static int sja1105_port_pre_bridge_flags(struct dsa_switch *ds, int port, 2859 struct switchdev_brport_flags flags, 2860 struct netlink_ext_ack *extack) 2861 { 2862 struct sja1105_private *priv = ds->priv; 2863 2864 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 2865 BR_BCAST_FLOOD)) 2866 return -EINVAL; 2867 2868 if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD) && 2869 !priv->info->can_limit_mcast_flood) { 2870 bool multicast = !!(flags.val & BR_MCAST_FLOOD); 2871 bool unicast = !!(flags.val & BR_FLOOD); 2872 2873 if (unicast != multicast) { 2874 NL_SET_ERR_MSG_MOD(extack, 2875 "This chip cannot configure multicast flooding independently of unicast"); 2876 return -EINVAL; 2877 } 2878 } 2879 2880 return 0; 2881 } 2882 2883 static int sja1105_port_bridge_flags(struct dsa_switch *ds, int port, 2884 struct switchdev_brport_flags flags, 2885 struct netlink_ext_ack *extack) 2886 { 2887 struct sja1105_private *priv = ds->priv; 2888 int rc; 2889 2890 if (flags.mask & BR_LEARNING) { 2891 bool learn_ena = !!(flags.val & BR_LEARNING); 2892 2893 rc = sja1105_port_set_learning(priv, port, learn_ena); 2894 if (rc) 2895 return rc; 2896 } 2897 2898 if (flags.mask & (BR_FLOOD | BR_BCAST_FLOOD)) { 2899 rc = sja1105_port_ucast_bcast_flood(priv, port, flags); 2900 if (rc) 2901 return rc; 2902 } 2903 2904 /* For chips that can't offload BR_MCAST_FLOOD independently, there 2905 * is nothing to do here, we ensured the configuration is in sync by 2906 * offloading BR_FLOOD. 2907 */ 2908 if (flags.mask & BR_MCAST_FLOOD && priv->info->can_limit_mcast_flood) { 2909 rc = sja1105_port_mcast_flood(priv, port, flags, 2910 extack); 2911 if (rc) 2912 return rc; 2913 } 2914 2915 return 0; 2916 } 2917 2918 static void sja1105_teardown_ports(struct sja1105_private *priv) 2919 { 2920 struct dsa_switch *ds = priv->ds; 2921 int port; 2922 2923 for (port = 0; port < ds->num_ports; port++) { 2924 struct sja1105_port *sp = &priv->ports[port]; 2925 2926 if (sp->xmit_worker) 2927 kthread_destroy_worker(sp->xmit_worker); 2928 } 2929 } 2930 2931 static int sja1105_setup_ports(struct sja1105_private *priv) 2932 { 2933 struct sja1105_tagger_data *tagger_data = &priv->tagger_data; 2934 struct dsa_switch *ds = priv->ds; 2935 int port, rc; 2936 2937 /* Connections between dsa_port and sja1105_port */ 2938 for (port = 0; port < ds->num_ports; port++) { 2939 struct sja1105_port *sp = &priv->ports[port]; 2940 struct dsa_port *dp = dsa_to_port(ds, port); 2941 struct kthread_worker *worker; 2942 struct net_device *slave; 2943 2944 if (!dsa_port_is_user(dp)) 2945 continue; 2946 2947 dp->priv = sp; 2948 sp->dp = dp; 2949 sp->data = tagger_data; 2950 slave = dp->slave; 2951 kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit); 2952 worker = kthread_create_worker(0, "%s_xmit", slave->name); 2953 if (IS_ERR(worker)) { 2954 rc = PTR_ERR(worker); 2955 dev_err(ds->dev, 2956 "failed to create deferred xmit thread: %d\n", 2957 rc); 2958 goto out_destroy_workers; 2959 } 2960 sp->xmit_worker = worker; 2961 skb_queue_head_init(&sp->xmit_queue); 2962 sp->xmit_tpid = ETH_P_SJA1105; 2963 } 2964 2965 return 0; 2966 2967 out_destroy_workers: 2968 sja1105_teardown_ports(priv); 2969 return rc; 2970 } 2971 2972 /* The programming model for the SJA1105 switch is "all-at-once" via static 2973 * configuration tables. Some of these can be dynamically modified at runtime, 2974 * but not the xMII mode parameters table. 2975 * Furthermode, some PHYs may not have crystals for generating their clocks 2976 * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 2977 * ref_clk pin. So port clocking needs to be initialized early, before 2978 * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 2979 * Setting correct PHY link speed does not matter now. 2980 * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 2981 * bindings are not yet parsed by DSA core. We need to parse early so that we 2982 * can populate the xMII mode parameters table. 2983 */ 2984 static int sja1105_setup(struct dsa_switch *ds) 2985 { 2986 struct sja1105_private *priv = ds->priv; 2987 int rc; 2988 2989 if (priv->info->disable_microcontroller) { 2990 rc = priv->info->disable_microcontroller(priv); 2991 if (rc < 0) { 2992 dev_err(ds->dev, 2993 "Failed to disable microcontroller: %pe\n", 2994 ERR_PTR(rc)); 2995 return rc; 2996 } 2997 } 2998 2999 /* Create and send configuration down to device */ 3000 rc = sja1105_static_config_load(priv); 3001 if (rc < 0) { 3002 dev_err(ds->dev, "Failed to load static config: %d\n", rc); 3003 return rc; 3004 } 3005 3006 /* Configure the CGU (PHY link modes and speeds) */ 3007 if (priv->info->clocking_setup) { 3008 rc = priv->info->clocking_setup(priv); 3009 if (rc < 0) { 3010 dev_err(ds->dev, 3011 "Failed to configure MII clocking: %pe\n", 3012 ERR_PTR(rc)); 3013 goto out_static_config_free; 3014 } 3015 } 3016 3017 rc = sja1105_setup_ports(priv); 3018 if (rc) 3019 goto out_static_config_free; 3020 3021 sja1105_tas_setup(ds); 3022 sja1105_flower_setup(ds); 3023 3024 rc = sja1105_ptp_clock_register(ds); 3025 if (rc < 0) { 3026 dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 3027 goto out_flower_teardown; 3028 } 3029 3030 rc = sja1105_mdiobus_register(ds); 3031 if (rc < 0) { 3032 dev_err(ds->dev, "Failed to register MDIO bus: %pe\n", 3033 ERR_PTR(rc)); 3034 goto out_ptp_clock_unregister; 3035 } 3036 3037 rc = sja1105_devlink_setup(ds); 3038 if (rc < 0) 3039 goto out_mdiobus_unregister; 3040 3041 rtnl_lock(); 3042 rc = dsa_tag_8021q_register(ds, htons(ETH_P_8021Q)); 3043 rtnl_unlock(); 3044 if (rc) 3045 goto out_devlink_teardown; 3046 3047 /* On SJA1105, VLAN filtering per se is always enabled in hardware. 3048 * The only thing we can do to disable it is lie about what the 802.1Q 3049 * EtherType is. 3050 * So it will still try to apply VLAN filtering, but all ingress 3051 * traffic (except frames received with EtherType of ETH_P_SJA1105) 3052 * will be internally tagged with a distorted VLAN header where the 3053 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 3054 */ 3055 ds->vlan_filtering_is_global = true; 3056 ds->untag_bridge_pvid = true; 3057 /* tag_8021q has 3 bits for the VBID, and the value 0 is reserved */ 3058 ds->num_fwd_offloading_bridges = 7; 3059 3060 /* Advertise the 8 egress queues */ 3061 ds->num_tx_queues = SJA1105_NUM_TC; 3062 3063 ds->mtu_enforcement_ingress = true; 3064 ds->assisted_learning_on_cpu_port = true; 3065 3066 return 0; 3067 3068 out_devlink_teardown: 3069 sja1105_devlink_teardown(ds); 3070 out_mdiobus_unregister: 3071 sja1105_mdiobus_unregister(ds); 3072 out_ptp_clock_unregister: 3073 sja1105_ptp_clock_unregister(ds); 3074 out_flower_teardown: 3075 sja1105_flower_teardown(ds); 3076 sja1105_tas_teardown(ds); 3077 sja1105_teardown_ports(priv); 3078 out_static_config_free: 3079 sja1105_static_config_free(&priv->static_config); 3080 3081 return rc; 3082 } 3083 3084 static void sja1105_teardown(struct dsa_switch *ds) 3085 { 3086 struct sja1105_private *priv = ds->priv; 3087 3088 rtnl_lock(); 3089 dsa_tag_8021q_unregister(ds); 3090 rtnl_unlock(); 3091 3092 sja1105_devlink_teardown(ds); 3093 sja1105_mdiobus_unregister(ds); 3094 sja1105_ptp_clock_unregister(ds); 3095 sja1105_flower_teardown(ds); 3096 sja1105_tas_teardown(ds); 3097 sja1105_teardown_ports(priv); 3098 sja1105_static_config_free(&priv->static_config); 3099 } 3100 3101 const struct dsa_switch_ops sja1105_switch_ops = { 3102 .get_tag_protocol = sja1105_get_tag_protocol, 3103 .setup = sja1105_setup, 3104 .teardown = sja1105_teardown, 3105 .set_ageing_time = sja1105_set_ageing_time, 3106 .port_change_mtu = sja1105_change_mtu, 3107 .port_max_mtu = sja1105_get_max_mtu, 3108 .phylink_validate = sja1105_phylink_validate, 3109 .phylink_mac_config = sja1105_mac_config, 3110 .phylink_mac_link_up = sja1105_mac_link_up, 3111 .phylink_mac_link_down = sja1105_mac_link_down, 3112 .get_strings = sja1105_get_strings, 3113 .get_ethtool_stats = sja1105_get_ethtool_stats, 3114 .get_sset_count = sja1105_get_sset_count, 3115 .get_ts_info = sja1105_get_ts_info, 3116 .port_disable = sja1105_port_disable, 3117 .port_fdb_dump = sja1105_fdb_dump, 3118 .port_fdb_add = sja1105_fdb_add, 3119 .port_fdb_del = sja1105_fdb_del, 3120 .port_fast_age = sja1105_fast_age, 3121 .port_bridge_join = sja1105_bridge_join, 3122 .port_bridge_leave = sja1105_bridge_leave, 3123 .port_pre_bridge_flags = sja1105_port_pre_bridge_flags, 3124 .port_bridge_flags = sja1105_port_bridge_flags, 3125 .port_stp_state_set = sja1105_bridge_stp_state_set, 3126 .port_vlan_filtering = sja1105_vlan_filtering, 3127 .port_vlan_add = sja1105_bridge_vlan_add, 3128 .port_vlan_del = sja1105_bridge_vlan_del, 3129 .port_mdb_add = sja1105_mdb_add, 3130 .port_mdb_del = sja1105_mdb_del, 3131 .port_hwtstamp_get = sja1105_hwtstamp_get, 3132 .port_hwtstamp_set = sja1105_hwtstamp_set, 3133 .port_rxtstamp = sja1105_port_rxtstamp, 3134 .port_txtstamp = sja1105_port_txtstamp, 3135 .port_setup_tc = sja1105_port_setup_tc, 3136 .port_mirror_add = sja1105_mirror_add, 3137 .port_mirror_del = sja1105_mirror_del, 3138 .port_policer_add = sja1105_port_policer_add, 3139 .port_policer_del = sja1105_port_policer_del, 3140 .cls_flower_add = sja1105_cls_flower_add, 3141 .cls_flower_del = sja1105_cls_flower_del, 3142 .cls_flower_stats = sja1105_cls_flower_stats, 3143 .devlink_info_get = sja1105_devlink_info_get, 3144 .tag_8021q_vlan_add = sja1105_dsa_8021q_vlan_add, 3145 .tag_8021q_vlan_del = sja1105_dsa_8021q_vlan_del, 3146 .port_prechangeupper = sja1105_prechangeupper, 3147 .port_bridge_tx_fwd_offload = dsa_tag_8021q_bridge_tx_fwd_offload, 3148 .port_bridge_tx_fwd_unoffload = dsa_tag_8021q_bridge_tx_fwd_unoffload, 3149 }; 3150 EXPORT_SYMBOL_GPL(sja1105_switch_ops); 3151 3152 static const struct of_device_id sja1105_dt_ids[]; 3153 3154 static int sja1105_check_device_id(struct sja1105_private *priv) 3155 { 3156 const struct sja1105_regs *regs = priv->info->regs; 3157 u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 3158 struct device *dev = &priv->spidev->dev; 3159 const struct of_device_id *match; 3160 u32 device_id; 3161 u64 part_no; 3162 int rc; 3163 3164 rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id, 3165 NULL); 3166 if (rc < 0) 3167 return rc; 3168 3169 rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id, 3170 SJA1105_SIZE_DEVICE_ID); 3171 if (rc < 0) 3172 return rc; 3173 3174 sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 3175 3176 for (match = sja1105_dt_ids; match->compatible[0]; match++) { 3177 const struct sja1105_info *info = match->data; 3178 3179 /* Is what's been probed in our match table at all? */ 3180 if (info->device_id != device_id || info->part_no != part_no) 3181 continue; 3182 3183 /* But is it what's in the device tree? */ 3184 if (priv->info->device_id != device_id || 3185 priv->info->part_no != part_no) { 3186 dev_warn(dev, "Device tree specifies chip %s but found %s, please fix it!\n", 3187 priv->info->name, info->name); 3188 /* It isn't. No problem, pick that up. */ 3189 priv->info = info; 3190 } 3191 3192 return 0; 3193 } 3194 3195 dev_err(dev, "Unexpected {device ID, part number}: 0x%x 0x%llx\n", 3196 device_id, part_no); 3197 3198 return -ENODEV; 3199 } 3200 3201 static int sja1105_probe(struct spi_device *spi) 3202 { 3203 struct device *dev = &spi->dev; 3204 struct sja1105_private *priv; 3205 size_t max_xfer, max_msg; 3206 struct dsa_switch *ds; 3207 int rc; 3208 3209 if (!dev->of_node) { 3210 dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 3211 return -EINVAL; 3212 } 3213 3214 priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 3215 if (!priv) 3216 return -ENOMEM; 3217 3218 /* Configure the optional reset pin and bring up switch */ 3219 priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 3220 if (IS_ERR(priv->reset_gpio)) 3221 dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 3222 else 3223 sja1105_hw_reset(priv->reset_gpio, 1, 1); 3224 3225 /* Populate our driver private structure (priv) based on 3226 * the device tree node that was probed (spi) 3227 */ 3228 priv->spidev = spi; 3229 spi_set_drvdata(spi, priv); 3230 3231 /* Configure the SPI bus */ 3232 spi->bits_per_word = 8; 3233 rc = spi_setup(spi); 3234 if (rc < 0) { 3235 dev_err(dev, "Could not init SPI\n"); 3236 return rc; 3237 } 3238 3239 /* In sja1105_xfer, we send spi_messages composed of two spi_transfers: 3240 * a small one for the message header and another one for the current 3241 * chunk of the packed buffer. 3242 * Check that the restrictions imposed by the SPI controller are 3243 * respected: the chunk buffer is smaller than the max transfer size, 3244 * and the total length of the chunk plus its message header is smaller 3245 * than the max message size. 3246 * We do that during probe time since the maximum transfer size is a 3247 * runtime invariant. 3248 */ 3249 max_xfer = spi_max_transfer_size(spi); 3250 max_msg = spi_max_message_size(spi); 3251 3252 /* We need to send at least one 64-bit word of SPI payload per message 3253 * in order to be able to make useful progress. 3254 */ 3255 if (max_msg < SJA1105_SIZE_SPI_MSG_HEADER + 8) { 3256 dev_err(dev, "SPI master cannot send large enough buffers, aborting\n"); 3257 return -EINVAL; 3258 } 3259 3260 priv->max_xfer_len = SJA1105_SIZE_SPI_MSG_MAXLEN; 3261 if (priv->max_xfer_len > max_xfer) 3262 priv->max_xfer_len = max_xfer; 3263 if (priv->max_xfer_len > max_msg - SJA1105_SIZE_SPI_MSG_HEADER) 3264 priv->max_xfer_len = max_msg - SJA1105_SIZE_SPI_MSG_HEADER; 3265 3266 priv->info = of_device_get_match_data(dev); 3267 3268 /* Detect hardware device */ 3269 rc = sja1105_check_device_id(priv); 3270 if (rc < 0) { 3271 dev_err(dev, "Device ID check failed: %d\n", rc); 3272 return rc; 3273 } 3274 3275 dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 3276 3277 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL); 3278 if (!ds) 3279 return -ENOMEM; 3280 3281 ds->dev = dev; 3282 ds->num_ports = priv->info->num_ports; 3283 ds->ops = &sja1105_switch_ops; 3284 ds->priv = priv; 3285 priv->ds = ds; 3286 3287 mutex_init(&priv->ptp_data.lock); 3288 mutex_init(&priv->mgmt_lock); 3289 3290 rc = sja1105_parse_dt(priv); 3291 if (rc < 0) { 3292 dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 3293 return rc; 3294 } 3295 3296 /* Error out early if internal delays are required through DT 3297 * and we can't apply them. 3298 */ 3299 rc = sja1105_parse_rgmii_delays(priv); 3300 if (rc < 0) { 3301 dev_err(ds->dev, "RGMII delay not supported\n"); 3302 return rc; 3303 } 3304 3305 if (IS_ENABLED(CONFIG_NET_SCH_CBS)) { 3306 priv->cbs = devm_kcalloc(dev, priv->info->num_cbs_shapers, 3307 sizeof(struct sja1105_cbs_entry), 3308 GFP_KERNEL); 3309 if (!priv->cbs) 3310 return -ENOMEM; 3311 } 3312 3313 return dsa_register_switch(priv->ds); 3314 } 3315 3316 static int sja1105_remove(struct spi_device *spi) 3317 { 3318 struct sja1105_private *priv = spi_get_drvdata(spi); 3319 struct dsa_switch *ds = priv->ds; 3320 3321 dsa_unregister_switch(ds); 3322 3323 return 0; 3324 } 3325 3326 static const struct of_device_id sja1105_dt_ids[] = { 3327 { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 3328 { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 3329 { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 3330 { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 3331 { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 3332 { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 3333 { .compatible = "nxp,sja1110a", .data = &sja1110a_info }, 3334 { .compatible = "nxp,sja1110b", .data = &sja1110b_info }, 3335 { .compatible = "nxp,sja1110c", .data = &sja1110c_info }, 3336 { .compatible = "nxp,sja1110d", .data = &sja1110d_info }, 3337 { /* sentinel */ }, 3338 }; 3339 MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 3340 3341 static struct spi_driver sja1105_driver = { 3342 .driver = { 3343 .name = "sja1105", 3344 .owner = THIS_MODULE, 3345 .of_match_table = of_match_ptr(sja1105_dt_ids), 3346 }, 3347 .probe = sja1105_probe, 3348 .remove = sja1105_remove, 3349 }; 3350 3351 module_spi_driver(sja1105_driver); 3352 3353 MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 3354 MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 3355 MODULE_DESCRIPTION("SJA1105 Driver"); 3356 MODULE_LICENSE("GPL v2"); 3357