1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Handling of a single switch port 4 * 5 * Copyright (c) 2017 Savoir-faire Linux Inc. 6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com> 7 */ 8 9 #include <linux/if_bridge.h> 10 #include <linux/notifier.h> 11 #include <linux/of_mdio.h> 12 #include <linux/of_net.h> 13 14 #include "dsa_priv.h" 15 16 /** 17 * dsa_port_notify - Notify the switching fabric of changes to a port 18 * @dp: port on which change occurred 19 * @e: event, must be of type DSA_NOTIFIER_* 20 * @v: event-specific value. 21 * 22 * Notify all switches in the DSA tree that this port's switch belongs to, 23 * including this switch itself, of an event. Allows the other switches to 24 * reconfigure themselves for cross-chip operations. Can also be used to 25 * reconfigure ports without net_devices (CPU ports, DSA links) whenever 26 * a user port's state changes. 27 */ 28 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v) 29 { 30 return dsa_tree_notify(dp->ds->dst, e, v); 31 } 32 33 static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp) 34 { 35 struct net_device *brport_dev = dsa_port_to_bridge_port(dp); 36 struct switchdev_notifier_fdb_info info = { 37 /* flush all VLANs */ 38 .vid = 0, 39 }; 40 41 /* When the port becomes standalone it has already left the bridge. 42 * Don't notify the bridge in that case. 43 */ 44 if (!brport_dev) 45 return; 46 47 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE, 48 brport_dev, &info.info, NULL); 49 } 50 51 static void dsa_port_fast_age(const struct dsa_port *dp) 52 { 53 struct dsa_switch *ds = dp->ds; 54 55 if (!ds->ops->port_fast_age) 56 return; 57 58 ds->ops->port_fast_age(ds, dp->index); 59 60 dsa_port_notify_bridge_fdb_flush(dp); 61 } 62 63 static bool dsa_port_can_configure_learning(struct dsa_port *dp) 64 { 65 struct switchdev_brport_flags flags = { 66 .mask = BR_LEARNING, 67 }; 68 struct dsa_switch *ds = dp->ds; 69 int err; 70 71 if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags) 72 return false; 73 74 err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL); 75 return !err; 76 } 77 78 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age) 79 { 80 struct dsa_switch *ds = dp->ds; 81 int port = dp->index; 82 83 if (!ds->ops->port_stp_state_set) 84 return -EOPNOTSUPP; 85 86 ds->ops->port_stp_state_set(ds, port, state); 87 88 if (!dsa_port_can_configure_learning(dp) || 89 (do_fast_age && dp->learning)) { 90 /* Fast age FDB entries or flush appropriate forwarding database 91 * for the given port, if we are moving it from Learning or 92 * Forwarding state, to Disabled or Blocking or Listening state. 93 * Ports that were standalone before the STP state change don't 94 * need to fast age the FDB, since address learning is off in 95 * standalone mode. 96 */ 97 98 if ((dp->stp_state == BR_STATE_LEARNING || 99 dp->stp_state == BR_STATE_FORWARDING) && 100 (state == BR_STATE_DISABLED || 101 state == BR_STATE_BLOCKING || 102 state == BR_STATE_LISTENING)) 103 dsa_port_fast_age(dp); 104 } 105 106 dp->stp_state = state; 107 108 return 0; 109 } 110 111 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state, 112 bool do_fast_age) 113 { 114 int err; 115 116 err = dsa_port_set_state(dp, state, do_fast_age); 117 if (err) 118 pr_err("DSA: failed to set STP state %u (%d)\n", state, err); 119 } 120 121 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy) 122 { 123 struct dsa_switch *ds = dp->ds; 124 int port = dp->index; 125 int err; 126 127 if (ds->ops->port_enable) { 128 err = ds->ops->port_enable(ds, port, phy); 129 if (err) 130 return err; 131 } 132 133 if (!dp->bridge_dev) 134 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false); 135 136 if (dp->pl) 137 phylink_start(dp->pl); 138 139 return 0; 140 } 141 142 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy) 143 { 144 int err; 145 146 rtnl_lock(); 147 err = dsa_port_enable_rt(dp, phy); 148 rtnl_unlock(); 149 150 return err; 151 } 152 153 void dsa_port_disable_rt(struct dsa_port *dp) 154 { 155 struct dsa_switch *ds = dp->ds; 156 int port = dp->index; 157 158 if (dp->pl) 159 phylink_stop(dp->pl); 160 161 if (!dp->bridge_dev) 162 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false); 163 164 if (ds->ops->port_disable) 165 ds->ops->port_disable(ds, port); 166 } 167 168 void dsa_port_disable(struct dsa_port *dp) 169 { 170 rtnl_lock(); 171 dsa_port_disable_rt(dp); 172 rtnl_unlock(); 173 } 174 175 static int dsa_port_inherit_brport_flags(struct dsa_port *dp, 176 struct netlink_ext_ack *extack) 177 { 178 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 179 BR_BCAST_FLOOD; 180 struct net_device *brport_dev = dsa_port_to_bridge_port(dp); 181 int flag, err; 182 183 for_each_set_bit(flag, &mask, 32) { 184 struct switchdev_brport_flags flags = {0}; 185 186 flags.mask = BIT(flag); 187 188 if (br_port_flag_is_set(brport_dev, BIT(flag))) 189 flags.val = BIT(flag); 190 191 err = dsa_port_bridge_flags(dp, flags, extack); 192 if (err && err != -EOPNOTSUPP) 193 return err; 194 } 195 196 return 0; 197 } 198 199 static void dsa_port_clear_brport_flags(struct dsa_port *dp) 200 { 201 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD; 202 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 203 BR_BCAST_FLOOD; 204 int flag, err; 205 206 for_each_set_bit(flag, &mask, 32) { 207 struct switchdev_brport_flags flags = {0}; 208 209 flags.mask = BIT(flag); 210 flags.val = val & BIT(flag); 211 212 err = dsa_port_bridge_flags(dp, flags, NULL); 213 if (err && err != -EOPNOTSUPP) 214 dev_err(dp->ds->dev, 215 "failed to clear bridge port flag %lu: %pe\n", 216 flags.val, ERR_PTR(err)); 217 } 218 } 219 220 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp, 221 struct netlink_ext_ack *extack) 222 { 223 struct net_device *brport_dev = dsa_port_to_bridge_port(dp); 224 struct net_device *br = dp->bridge_dev; 225 int err; 226 227 err = dsa_port_inherit_brport_flags(dp, extack); 228 if (err) 229 return err; 230 231 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false); 232 if (err && err != -EOPNOTSUPP) 233 return err; 234 235 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack); 236 if (err && err != -EOPNOTSUPP) 237 return err; 238 239 err = dsa_port_ageing_time(dp, br_get_ageing_time(br)); 240 if (err && err != -EOPNOTSUPP) 241 return err; 242 243 return 0; 244 } 245 246 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp) 247 { 248 /* Configure the port for standalone mode (no address learning, 249 * flood everything). 250 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events 251 * when the user requests it through netlink or sysfs, but not 252 * automatically at port join or leave, so we need to handle resetting 253 * the brport flags ourselves. But we even prefer it that way, because 254 * otherwise, some setups might never get the notification they need, 255 * for example, when a port leaves a LAG that offloads the bridge, 256 * it becomes standalone, but as far as the bridge is concerned, no 257 * port ever left. 258 */ 259 dsa_port_clear_brport_flags(dp); 260 261 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer, 262 * so allow it to be in BR_STATE_FORWARDING to be kept functional 263 */ 264 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true); 265 266 /* VLAN filtering is handled by dsa_switch_bridge_leave */ 267 268 /* Ageing time may be global to the switch chip, so don't change it 269 * here because we have no good reason (or value) to change it to. 270 */ 271 } 272 273 static void dsa_port_bridge_tx_fwd_unoffload(struct dsa_port *dp, 274 struct net_device *bridge_dev) 275 { 276 int bridge_num = dp->bridge_num; 277 struct dsa_switch *ds = dp->ds; 278 279 /* No bridge TX forwarding offload => do nothing */ 280 if (!ds->ops->port_bridge_tx_fwd_unoffload || dp->bridge_num == -1) 281 return; 282 283 dp->bridge_num = -1; 284 285 dsa_bridge_num_put(bridge_dev, bridge_num); 286 287 /* Notify the chips only once the offload has been deactivated, so 288 * that they can update their configuration accordingly. 289 */ 290 ds->ops->port_bridge_tx_fwd_unoffload(ds, dp->index, bridge_dev, 291 bridge_num); 292 } 293 294 static bool dsa_port_bridge_tx_fwd_offload(struct dsa_port *dp, 295 struct net_device *bridge_dev) 296 { 297 struct dsa_switch *ds = dp->ds; 298 int bridge_num, err; 299 300 if (!ds->ops->port_bridge_tx_fwd_offload) 301 return false; 302 303 bridge_num = dsa_bridge_num_get(bridge_dev, 304 ds->num_fwd_offloading_bridges); 305 if (bridge_num < 0) 306 return false; 307 308 dp->bridge_num = bridge_num; 309 310 /* Notify the driver */ 311 err = ds->ops->port_bridge_tx_fwd_offload(ds, dp->index, bridge_dev, 312 bridge_num); 313 if (err) { 314 dsa_port_bridge_tx_fwd_unoffload(dp, bridge_dev); 315 return false; 316 } 317 318 return true; 319 } 320 321 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br, 322 struct netlink_ext_ack *extack) 323 { 324 struct dsa_notifier_bridge_info info = { 325 .tree_index = dp->ds->dst->index, 326 .sw_index = dp->ds->index, 327 .port = dp->index, 328 .br = br, 329 }; 330 struct net_device *dev = dp->slave; 331 struct net_device *brport_dev; 332 bool tx_fwd_offload; 333 int err; 334 335 /* Here the interface is already bridged. Reflect the current 336 * configuration so that drivers can program their chips accordingly. 337 */ 338 dp->bridge_dev = br; 339 340 brport_dev = dsa_port_to_bridge_port(dp); 341 342 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info); 343 if (err) 344 goto out_rollback; 345 346 tx_fwd_offload = dsa_port_bridge_tx_fwd_offload(dp, br); 347 348 err = switchdev_bridge_port_offload(brport_dev, dev, dp, 349 &dsa_slave_switchdev_notifier, 350 &dsa_slave_switchdev_blocking_notifier, 351 tx_fwd_offload, extack); 352 if (err) 353 goto out_rollback_unbridge; 354 355 err = dsa_port_switchdev_sync_attrs(dp, extack); 356 if (err) 357 goto out_rollback_unoffload; 358 359 return 0; 360 361 out_rollback_unoffload: 362 switchdev_bridge_port_unoffload(brport_dev, dp, 363 &dsa_slave_switchdev_notifier, 364 &dsa_slave_switchdev_blocking_notifier); 365 out_rollback_unbridge: 366 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); 367 out_rollback: 368 dp->bridge_dev = NULL; 369 return err; 370 } 371 372 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br) 373 { 374 struct net_device *brport_dev = dsa_port_to_bridge_port(dp); 375 376 /* Don't try to unoffload something that is not offloaded */ 377 if (!brport_dev) 378 return; 379 380 switchdev_bridge_port_unoffload(brport_dev, dp, 381 &dsa_slave_switchdev_notifier, 382 &dsa_slave_switchdev_blocking_notifier); 383 384 dsa_flush_workqueue(); 385 } 386 387 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) 388 { 389 struct dsa_notifier_bridge_info info = { 390 .tree_index = dp->ds->dst->index, 391 .sw_index = dp->ds->index, 392 .port = dp->index, 393 .br = br, 394 }; 395 int err; 396 397 /* Here the port is already unbridged. Reflect the current configuration 398 * so that drivers can program their chips accordingly. 399 */ 400 dp->bridge_dev = NULL; 401 402 dsa_port_bridge_tx_fwd_unoffload(dp, br); 403 404 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); 405 if (err) 406 dev_err(dp->ds->dev, 407 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n", 408 dp->index, ERR_PTR(err)); 409 410 dsa_port_switchdev_unsync_attrs(dp); 411 } 412 413 int dsa_port_lag_change(struct dsa_port *dp, 414 struct netdev_lag_lower_state_info *linfo) 415 { 416 struct dsa_notifier_lag_info info = { 417 .sw_index = dp->ds->index, 418 .port = dp->index, 419 }; 420 bool tx_enabled; 421 422 if (!dp->lag_dev) 423 return 0; 424 425 /* On statically configured aggregates (e.g. loadbalance 426 * without LACP) ports will always be tx_enabled, even if the 427 * link is down. Thus we require both link_up and tx_enabled 428 * in order to include it in the tx set. 429 */ 430 tx_enabled = linfo->link_up && linfo->tx_enabled; 431 432 if (tx_enabled == dp->lag_tx_enabled) 433 return 0; 434 435 dp->lag_tx_enabled = tx_enabled; 436 437 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info); 438 } 439 440 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag, 441 struct netdev_lag_upper_info *uinfo, 442 struct netlink_ext_ack *extack) 443 { 444 struct dsa_notifier_lag_info info = { 445 .sw_index = dp->ds->index, 446 .port = dp->index, 447 .lag = lag, 448 .info = uinfo, 449 }; 450 struct net_device *bridge_dev; 451 int err; 452 453 dsa_lag_map(dp->ds->dst, lag); 454 dp->lag_dev = lag; 455 456 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info); 457 if (err) 458 goto err_lag_join; 459 460 bridge_dev = netdev_master_upper_dev_get(lag); 461 if (!bridge_dev || !netif_is_bridge_master(bridge_dev)) 462 return 0; 463 464 err = dsa_port_bridge_join(dp, bridge_dev, extack); 465 if (err) 466 goto err_bridge_join; 467 468 return 0; 469 470 err_bridge_join: 471 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); 472 err_lag_join: 473 dp->lag_dev = NULL; 474 dsa_lag_unmap(dp->ds->dst, lag); 475 return err; 476 } 477 478 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag) 479 { 480 if (dp->bridge_dev) 481 dsa_port_pre_bridge_leave(dp, dp->bridge_dev); 482 } 483 484 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag) 485 { 486 struct dsa_notifier_lag_info info = { 487 .sw_index = dp->ds->index, 488 .port = dp->index, 489 .lag = lag, 490 }; 491 int err; 492 493 if (!dp->lag_dev) 494 return; 495 496 /* Port might have been part of a LAG that in turn was 497 * attached to a bridge. 498 */ 499 if (dp->bridge_dev) 500 dsa_port_bridge_leave(dp, dp->bridge_dev); 501 502 dp->lag_tx_enabled = false; 503 dp->lag_dev = NULL; 504 505 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); 506 if (err) 507 dev_err(dp->ds->dev, 508 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n", 509 dp->index, ERR_PTR(err)); 510 511 dsa_lag_unmap(dp->ds->dst, lag); 512 } 513 514 /* Must be called under rcu_read_lock() */ 515 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp, 516 bool vlan_filtering, 517 struct netlink_ext_ack *extack) 518 { 519 struct dsa_switch *ds = dp->ds; 520 struct dsa_port *other_dp; 521 int err; 522 523 /* VLAN awareness was off, so the question is "can we turn it on". 524 * We may have had 8021q uppers, those need to go. Make sure we don't 525 * enter an inconsistent state: deny changing the VLAN awareness state 526 * as long as we have 8021q uppers. 527 */ 528 if (vlan_filtering && dsa_port_is_user(dp)) { 529 struct net_device *upper_dev, *slave = dp->slave; 530 struct net_device *br = dp->bridge_dev; 531 struct list_head *iter; 532 533 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { 534 struct bridge_vlan_info br_info; 535 u16 vid; 536 537 if (!is_vlan_dev(upper_dev)) 538 continue; 539 540 vid = vlan_dev_vlan_id(upper_dev); 541 542 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 543 * device, respectively the VID is not found, returning 544 * 0 means success, which is a failure for us here. 545 */ 546 err = br_vlan_get_info(br, vid, &br_info); 547 if (err == 0) { 548 NL_SET_ERR_MSG_MOD(extack, 549 "Must first remove VLAN uppers having VIDs also present in bridge"); 550 return false; 551 } 552 } 553 } 554 555 if (!ds->vlan_filtering_is_global) 556 return true; 557 558 /* For cases where enabling/disabling VLAN awareness is global to the 559 * switch, we need to handle the case where multiple bridges span 560 * different ports of the same switch device and one of them has a 561 * different setting than what is being requested. 562 */ 563 dsa_switch_for_each_port(other_dp, ds) { 564 struct net_device *other_bridge; 565 566 other_bridge = other_dp->bridge_dev; 567 if (!other_bridge) 568 continue; 569 /* If it's the same bridge, it also has same 570 * vlan_filtering setting => no need to check 571 */ 572 if (other_bridge == dp->bridge_dev) 573 continue; 574 if (br_vlan_enabled(other_bridge) != vlan_filtering) { 575 NL_SET_ERR_MSG_MOD(extack, 576 "VLAN filtering is a global setting"); 577 return false; 578 } 579 } 580 return true; 581 } 582 583 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering, 584 struct netlink_ext_ack *extack) 585 { 586 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp); 587 struct dsa_switch *ds = dp->ds; 588 bool apply; 589 int err; 590 591 if (!ds->ops->port_vlan_filtering) 592 return -EOPNOTSUPP; 593 594 /* We are called from dsa_slave_switchdev_blocking_event(), 595 * which is not under rcu_read_lock(), unlike 596 * dsa_slave_switchdev_event(). 597 */ 598 rcu_read_lock(); 599 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack); 600 rcu_read_unlock(); 601 if (!apply) 602 return -EINVAL; 603 604 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering) 605 return 0; 606 607 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering, 608 extack); 609 if (err) 610 return err; 611 612 if (ds->vlan_filtering_is_global) { 613 struct dsa_port *other_dp; 614 615 ds->vlan_filtering = vlan_filtering; 616 617 dsa_switch_for_each_user_port(other_dp, ds) { 618 struct net_device *slave = dp->slave; 619 620 /* We might be called in the unbind path, so not 621 * all slave devices might still be registered. 622 */ 623 if (!slave) 624 continue; 625 626 err = dsa_slave_manage_vlan_filtering(slave, 627 vlan_filtering); 628 if (err) 629 goto restore; 630 } 631 } else { 632 dp->vlan_filtering = vlan_filtering; 633 634 err = dsa_slave_manage_vlan_filtering(dp->slave, 635 vlan_filtering); 636 if (err) 637 goto restore; 638 } 639 640 return 0; 641 642 restore: 643 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL); 644 645 if (ds->vlan_filtering_is_global) 646 ds->vlan_filtering = old_vlan_filtering; 647 else 648 dp->vlan_filtering = old_vlan_filtering; 649 650 return err; 651 } 652 653 /* This enforces legacy behavior for switch drivers which assume they can't 654 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0 655 */ 656 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp) 657 { 658 struct dsa_switch *ds = dp->ds; 659 660 if (!dp->bridge_dev) 661 return false; 662 663 return (!ds->configure_vlan_while_not_filtering && 664 !br_vlan_enabled(dp->bridge_dev)); 665 } 666 667 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock) 668 { 669 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); 670 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); 671 struct dsa_notifier_ageing_time_info info; 672 int err; 673 674 info.ageing_time = ageing_time; 675 676 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); 677 if (err) 678 return err; 679 680 dp->ageing_time = ageing_time; 681 682 return 0; 683 } 684 685 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, 686 struct switchdev_brport_flags flags, 687 struct netlink_ext_ack *extack) 688 { 689 struct dsa_switch *ds = dp->ds; 690 691 if (!ds->ops->port_pre_bridge_flags) 692 return -EINVAL; 693 694 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack); 695 } 696 697 int dsa_port_bridge_flags(struct dsa_port *dp, 698 struct switchdev_brport_flags flags, 699 struct netlink_ext_ack *extack) 700 { 701 struct dsa_switch *ds = dp->ds; 702 int err; 703 704 if (!ds->ops->port_bridge_flags) 705 return -EOPNOTSUPP; 706 707 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack); 708 if (err) 709 return err; 710 711 if (flags.mask & BR_LEARNING) { 712 bool learning = flags.val & BR_LEARNING; 713 714 if (learning == dp->learning) 715 return 0; 716 717 if ((dp->learning && !learning) && 718 (dp->stp_state == BR_STATE_LEARNING || 719 dp->stp_state == BR_STATE_FORWARDING)) 720 dsa_port_fast_age(dp); 721 722 dp->learning = learning; 723 } 724 725 return 0; 726 } 727 728 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu, 729 bool targeted_match) 730 { 731 struct dsa_notifier_mtu_info info = { 732 .sw_index = dp->ds->index, 733 .targeted_match = targeted_match, 734 .port = dp->index, 735 .mtu = new_mtu, 736 }; 737 738 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info); 739 } 740 741 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, 742 u16 vid) 743 { 744 struct dsa_notifier_fdb_info info = { 745 .sw_index = dp->ds->index, 746 .port = dp->index, 747 .addr = addr, 748 .vid = vid, 749 }; 750 751 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); 752 } 753 754 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, 755 u16 vid) 756 { 757 struct dsa_notifier_fdb_info info = { 758 .sw_index = dp->ds->index, 759 .port = dp->index, 760 .addr = addr, 761 .vid = vid, 762 763 }; 764 765 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); 766 } 767 768 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr, 769 u16 vid) 770 { 771 struct dsa_notifier_fdb_info info = { 772 .sw_index = dp->ds->index, 773 .port = dp->index, 774 .addr = addr, 775 .vid = vid, 776 }; 777 struct dsa_port *cpu_dp = dp->cpu_dp; 778 int err; 779 780 err = dev_uc_add(cpu_dp->master, addr); 781 if (err) 782 return err; 783 784 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info); 785 } 786 787 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr, 788 u16 vid) 789 { 790 struct dsa_notifier_fdb_info info = { 791 .sw_index = dp->ds->index, 792 .port = dp->index, 793 .addr = addr, 794 .vid = vid, 795 }; 796 struct dsa_port *cpu_dp = dp->cpu_dp; 797 int err; 798 799 err = dev_uc_del(cpu_dp->master, addr); 800 if (err) 801 return err; 802 803 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info); 804 } 805 806 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) 807 { 808 struct dsa_switch *ds = dp->ds; 809 int port = dp->index; 810 811 if (!ds->ops->port_fdb_dump) 812 return -EOPNOTSUPP; 813 814 return ds->ops->port_fdb_dump(ds, port, cb, data); 815 } 816 817 int dsa_port_mdb_add(const struct dsa_port *dp, 818 const struct switchdev_obj_port_mdb *mdb) 819 { 820 struct dsa_notifier_mdb_info info = { 821 .sw_index = dp->ds->index, 822 .port = dp->index, 823 .mdb = mdb, 824 }; 825 826 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); 827 } 828 829 int dsa_port_mdb_del(const struct dsa_port *dp, 830 const struct switchdev_obj_port_mdb *mdb) 831 { 832 struct dsa_notifier_mdb_info info = { 833 .sw_index = dp->ds->index, 834 .port = dp->index, 835 .mdb = mdb, 836 }; 837 838 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); 839 } 840 841 int dsa_port_host_mdb_add(const struct dsa_port *dp, 842 const struct switchdev_obj_port_mdb *mdb) 843 { 844 struct dsa_notifier_mdb_info info = { 845 .sw_index = dp->ds->index, 846 .port = dp->index, 847 .mdb = mdb, 848 }; 849 struct dsa_port *cpu_dp = dp->cpu_dp; 850 int err; 851 852 err = dev_mc_add(cpu_dp->master, mdb->addr); 853 if (err) 854 return err; 855 856 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info); 857 } 858 859 int dsa_port_host_mdb_del(const struct dsa_port *dp, 860 const struct switchdev_obj_port_mdb *mdb) 861 { 862 struct dsa_notifier_mdb_info info = { 863 .sw_index = dp->ds->index, 864 .port = dp->index, 865 .mdb = mdb, 866 }; 867 struct dsa_port *cpu_dp = dp->cpu_dp; 868 int err; 869 870 err = dev_mc_del(cpu_dp->master, mdb->addr); 871 if (err) 872 return err; 873 874 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info); 875 } 876 877 int dsa_port_vlan_add(struct dsa_port *dp, 878 const struct switchdev_obj_port_vlan *vlan, 879 struct netlink_ext_ack *extack) 880 { 881 struct dsa_notifier_vlan_info info = { 882 .sw_index = dp->ds->index, 883 .port = dp->index, 884 .vlan = vlan, 885 .extack = extack, 886 }; 887 888 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); 889 } 890 891 int dsa_port_vlan_del(struct dsa_port *dp, 892 const struct switchdev_obj_port_vlan *vlan) 893 { 894 struct dsa_notifier_vlan_info info = { 895 .sw_index = dp->ds->index, 896 .port = dp->index, 897 .vlan = vlan, 898 }; 899 900 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); 901 } 902 903 int dsa_port_mrp_add(const struct dsa_port *dp, 904 const struct switchdev_obj_mrp *mrp) 905 { 906 struct dsa_notifier_mrp_info info = { 907 .sw_index = dp->ds->index, 908 .port = dp->index, 909 .mrp = mrp, 910 }; 911 912 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info); 913 } 914 915 int dsa_port_mrp_del(const struct dsa_port *dp, 916 const struct switchdev_obj_mrp *mrp) 917 { 918 struct dsa_notifier_mrp_info info = { 919 .sw_index = dp->ds->index, 920 .port = dp->index, 921 .mrp = mrp, 922 }; 923 924 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info); 925 } 926 927 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp, 928 const struct switchdev_obj_ring_role_mrp *mrp) 929 { 930 struct dsa_notifier_mrp_ring_role_info info = { 931 .sw_index = dp->ds->index, 932 .port = dp->index, 933 .mrp = mrp, 934 }; 935 936 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info); 937 } 938 939 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp, 940 const struct switchdev_obj_ring_role_mrp *mrp) 941 { 942 struct dsa_notifier_mrp_ring_role_info info = { 943 .sw_index = dp->ds->index, 944 .port = dp->index, 945 .mrp = mrp, 946 }; 947 948 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info); 949 } 950 951 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp, 952 const struct dsa_device_ops *tag_ops) 953 { 954 cpu_dp->rcv = tag_ops->rcv; 955 cpu_dp->tag_ops = tag_ops; 956 } 957 958 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp) 959 { 960 struct device_node *phy_dn; 961 struct phy_device *phydev; 962 963 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0); 964 if (!phy_dn) 965 return NULL; 966 967 phydev = of_phy_find_device(phy_dn); 968 if (!phydev) { 969 of_node_put(phy_dn); 970 return ERR_PTR(-EPROBE_DEFER); 971 } 972 973 of_node_put(phy_dn); 974 return phydev; 975 } 976 977 static void dsa_port_phylink_validate(struct phylink_config *config, 978 unsigned long *supported, 979 struct phylink_link_state *state) 980 { 981 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 982 struct dsa_switch *ds = dp->ds; 983 984 if (!ds->ops->phylink_validate) { 985 if (config->mac_capabilities) 986 phylink_generic_validate(config, supported, state); 987 return; 988 } 989 990 ds->ops->phylink_validate(ds, dp->index, supported, state); 991 } 992 993 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config, 994 struct phylink_link_state *state) 995 { 996 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 997 struct dsa_switch *ds = dp->ds; 998 int err; 999 1000 /* Only called for inband modes */ 1001 if (!ds->ops->phylink_mac_link_state) { 1002 state->link = 0; 1003 return; 1004 } 1005 1006 err = ds->ops->phylink_mac_link_state(ds, dp->index, state); 1007 if (err < 0) { 1008 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n", 1009 dp->index, err); 1010 state->link = 0; 1011 } 1012 } 1013 1014 static void dsa_port_phylink_mac_config(struct phylink_config *config, 1015 unsigned int mode, 1016 const struct phylink_link_state *state) 1017 { 1018 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1019 struct dsa_switch *ds = dp->ds; 1020 1021 if (!ds->ops->phylink_mac_config) 1022 return; 1023 1024 ds->ops->phylink_mac_config(ds, dp->index, mode, state); 1025 } 1026 1027 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config) 1028 { 1029 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1030 struct dsa_switch *ds = dp->ds; 1031 1032 if (!ds->ops->phylink_mac_an_restart) 1033 return; 1034 1035 ds->ops->phylink_mac_an_restart(ds, dp->index); 1036 } 1037 1038 static void dsa_port_phylink_mac_link_down(struct phylink_config *config, 1039 unsigned int mode, 1040 phy_interface_t interface) 1041 { 1042 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1043 struct phy_device *phydev = NULL; 1044 struct dsa_switch *ds = dp->ds; 1045 1046 if (dsa_port_is_user(dp)) 1047 phydev = dp->slave->phydev; 1048 1049 if (!ds->ops->phylink_mac_link_down) { 1050 if (ds->ops->adjust_link && phydev) 1051 ds->ops->adjust_link(ds, dp->index, phydev); 1052 return; 1053 } 1054 1055 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); 1056 } 1057 1058 static void dsa_port_phylink_mac_link_up(struct phylink_config *config, 1059 struct phy_device *phydev, 1060 unsigned int mode, 1061 phy_interface_t interface, 1062 int speed, int duplex, 1063 bool tx_pause, bool rx_pause) 1064 { 1065 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1066 struct dsa_switch *ds = dp->ds; 1067 1068 if (!ds->ops->phylink_mac_link_up) { 1069 if (ds->ops->adjust_link && phydev) 1070 ds->ops->adjust_link(ds, dp->index, phydev); 1071 return; 1072 } 1073 1074 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev, 1075 speed, duplex, tx_pause, rx_pause); 1076 } 1077 1078 static const struct phylink_mac_ops dsa_port_phylink_mac_ops = { 1079 .validate = dsa_port_phylink_validate, 1080 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state, 1081 .mac_config = dsa_port_phylink_mac_config, 1082 .mac_an_restart = dsa_port_phylink_mac_an_restart, 1083 .mac_link_down = dsa_port_phylink_mac_link_down, 1084 .mac_link_up = dsa_port_phylink_mac_link_up, 1085 }; 1086 1087 int dsa_port_phylink_create(struct dsa_port *dp) 1088 { 1089 struct dsa_switch *ds = dp->ds; 1090 phy_interface_t mode; 1091 int err; 1092 1093 err = of_get_phy_mode(dp->dn, &mode); 1094 if (err) 1095 mode = PHY_INTERFACE_MODE_NA; 1096 1097 if (ds->ops->phylink_get_caps) 1098 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config); 1099 1100 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn), 1101 mode, &dsa_port_phylink_mac_ops); 1102 if (IS_ERR(dp->pl)) { 1103 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); 1104 return PTR_ERR(dp->pl); 1105 } 1106 1107 return 0; 1108 } 1109 1110 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) 1111 { 1112 struct dsa_switch *ds = dp->ds; 1113 struct phy_device *phydev; 1114 int port = dp->index; 1115 int err = 0; 1116 1117 phydev = dsa_port_get_phy_device(dp); 1118 if (!phydev) 1119 return 0; 1120 1121 if (IS_ERR(phydev)) 1122 return PTR_ERR(phydev); 1123 1124 if (enable) { 1125 err = genphy_resume(phydev); 1126 if (err < 0) 1127 goto err_put_dev; 1128 1129 err = genphy_read_status(phydev); 1130 if (err < 0) 1131 goto err_put_dev; 1132 } else { 1133 err = genphy_suspend(phydev); 1134 if (err < 0) 1135 goto err_put_dev; 1136 } 1137 1138 if (ds->ops->adjust_link) 1139 ds->ops->adjust_link(ds, port, phydev); 1140 1141 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev)); 1142 1143 err_put_dev: 1144 put_device(&phydev->mdio.dev); 1145 return err; 1146 } 1147 1148 static int dsa_port_fixed_link_register_of(struct dsa_port *dp) 1149 { 1150 struct device_node *dn = dp->dn; 1151 struct dsa_switch *ds = dp->ds; 1152 struct phy_device *phydev; 1153 int port = dp->index; 1154 phy_interface_t mode; 1155 int err; 1156 1157 err = of_phy_register_fixed_link(dn); 1158 if (err) { 1159 dev_err(ds->dev, 1160 "failed to register the fixed PHY of port %d\n", 1161 port); 1162 return err; 1163 } 1164 1165 phydev = of_phy_find_device(dn); 1166 1167 err = of_get_phy_mode(dn, &mode); 1168 if (err) 1169 mode = PHY_INTERFACE_MODE_NA; 1170 phydev->interface = mode; 1171 1172 genphy_read_status(phydev); 1173 1174 if (ds->ops->adjust_link) 1175 ds->ops->adjust_link(ds, port, phydev); 1176 1177 put_device(&phydev->mdio.dev); 1178 1179 return 0; 1180 } 1181 1182 static int dsa_port_phylink_register(struct dsa_port *dp) 1183 { 1184 struct dsa_switch *ds = dp->ds; 1185 struct device_node *port_dn = dp->dn; 1186 int err; 1187 1188 dp->pl_config.dev = ds->dev; 1189 dp->pl_config.type = PHYLINK_DEV; 1190 dp->pl_config.pcs_poll = ds->pcs_poll; 1191 1192 err = dsa_port_phylink_create(dp); 1193 if (err) 1194 return err; 1195 1196 err = phylink_of_phy_connect(dp->pl, port_dn, 0); 1197 if (err && err != -ENODEV) { 1198 pr_err("could not attach to PHY: %d\n", err); 1199 goto err_phy_connect; 1200 } 1201 1202 return 0; 1203 1204 err_phy_connect: 1205 phylink_destroy(dp->pl); 1206 return err; 1207 } 1208 1209 int dsa_port_link_register_of(struct dsa_port *dp) 1210 { 1211 struct dsa_switch *ds = dp->ds; 1212 struct device_node *phy_np; 1213 int port = dp->index; 1214 1215 if (!ds->ops->adjust_link) { 1216 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0); 1217 if (of_phy_is_fixed_link(dp->dn) || phy_np) { 1218 if (ds->ops->phylink_mac_link_down) 1219 ds->ops->phylink_mac_link_down(ds, port, 1220 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA); 1221 return dsa_port_phylink_register(dp); 1222 } 1223 return 0; 1224 } 1225 1226 dev_warn(ds->dev, 1227 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); 1228 1229 if (of_phy_is_fixed_link(dp->dn)) 1230 return dsa_port_fixed_link_register_of(dp); 1231 else 1232 return dsa_port_setup_phy_of(dp, true); 1233 } 1234 1235 void dsa_port_link_unregister_of(struct dsa_port *dp) 1236 { 1237 struct dsa_switch *ds = dp->ds; 1238 1239 if (!ds->ops->adjust_link && dp->pl) { 1240 rtnl_lock(); 1241 phylink_disconnect_phy(dp->pl); 1242 rtnl_unlock(); 1243 phylink_destroy(dp->pl); 1244 dp->pl = NULL; 1245 return; 1246 } 1247 1248 if (of_phy_is_fixed_link(dp->dn)) 1249 of_phy_deregister_fixed_link(dp->dn); 1250 else 1251 dsa_port_setup_phy_of(dp, false); 1252 } 1253 1254 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data) 1255 { 1256 struct phy_device *phydev; 1257 int ret = -EOPNOTSUPP; 1258 1259 if (of_phy_is_fixed_link(dp->dn)) 1260 return ret; 1261 1262 phydev = dsa_port_get_phy_device(dp); 1263 if (IS_ERR_OR_NULL(phydev)) 1264 return ret; 1265 1266 ret = phy_ethtool_get_strings(phydev, data); 1267 put_device(&phydev->mdio.dev); 1268 1269 return ret; 1270 } 1271 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings); 1272 1273 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data) 1274 { 1275 struct phy_device *phydev; 1276 int ret = -EOPNOTSUPP; 1277 1278 if (of_phy_is_fixed_link(dp->dn)) 1279 return ret; 1280 1281 phydev = dsa_port_get_phy_device(dp); 1282 if (IS_ERR_OR_NULL(phydev)) 1283 return ret; 1284 1285 ret = phy_ethtool_get_stats(phydev, NULL, data); 1286 put_device(&phydev->mdio.dev); 1287 1288 return ret; 1289 } 1290 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats); 1291 1292 int dsa_port_get_phy_sset_count(struct dsa_port *dp) 1293 { 1294 struct phy_device *phydev; 1295 int ret = -EOPNOTSUPP; 1296 1297 if (of_phy_is_fixed_link(dp->dn)) 1298 return ret; 1299 1300 phydev = dsa_port_get_phy_device(dp); 1301 if (IS_ERR_OR_NULL(phydev)) 1302 return ret; 1303 1304 ret = phy_ethtool_get_sset_count(phydev); 1305 put_device(&phydev->mdio.dev); 1306 1307 return ret; 1308 } 1309 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count); 1310 1311 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr) 1312 { 1313 struct dsa_notifier_hsr_info info = { 1314 .sw_index = dp->ds->index, 1315 .port = dp->index, 1316 .hsr = hsr, 1317 }; 1318 int err; 1319 1320 dp->hsr_dev = hsr; 1321 1322 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info); 1323 if (err) 1324 dp->hsr_dev = NULL; 1325 1326 return err; 1327 } 1328 1329 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr) 1330 { 1331 struct dsa_notifier_hsr_info info = { 1332 .sw_index = dp->ds->index, 1333 .port = dp->index, 1334 .hsr = hsr, 1335 }; 1336 int err; 1337 1338 dp->hsr_dev = NULL; 1339 1340 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info); 1341 if (err) 1342 dev_err(dp->ds->dev, 1343 "port %d failed to notify DSA_NOTIFIER_HSR_LEAVE: %pe\n", 1344 dp->index, ERR_PTR(err)); 1345 } 1346 1347 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast) 1348 { 1349 struct dsa_notifier_tag_8021q_vlan_info info = { 1350 .tree_index = dp->ds->dst->index, 1351 .sw_index = dp->ds->index, 1352 .port = dp->index, 1353 .vid = vid, 1354 }; 1355 1356 if (broadcast) 1357 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info); 1358 1359 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info); 1360 } 1361 1362 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast) 1363 { 1364 struct dsa_notifier_tag_8021q_vlan_info info = { 1365 .tree_index = dp->ds->dst->index, 1366 .sw_index = dp->ds->index, 1367 .port = dp->index, 1368 .vid = vid, 1369 }; 1370 int err; 1371 1372 if (broadcast) 1373 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info); 1374 else 1375 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info); 1376 if (err) 1377 dev_err(dp->ds->dev, 1378 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n", 1379 dp->index, vid, ERR_PTR(err)); 1380 } 1381