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) 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) 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 = dsa_port_bridge_dev_get(dp); 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 int dsa_port_bridge_create(struct dsa_port *dp, 274 struct net_device *br, 275 struct netlink_ext_ack *extack) 276 { 277 struct dsa_switch *ds = dp->ds; 278 struct dsa_bridge *bridge; 279 280 bridge = dsa_tree_bridge_find(ds->dst, br); 281 if (bridge) { 282 refcount_inc(&bridge->refcount); 283 dp->bridge = bridge; 284 return 0; 285 } 286 287 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL); 288 if (!bridge) 289 return -ENOMEM; 290 291 refcount_set(&bridge->refcount, 1); 292 293 bridge->dev = br; 294 295 bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges); 296 if (ds->max_num_bridges && !bridge->num) { 297 NL_SET_ERR_MSG_MOD(extack, 298 "Range of offloadable bridges exceeded"); 299 kfree(bridge); 300 return -EOPNOTSUPP; 301 } 302 303 dp->bridge = bridge; 304 305 return 0; 306 } 307 308 static void dsa_port_bridge_destroy(struct dsa_port *dp, 309 const struct net_device *br) 310 { 311 struct dsa_bridge *bridge = dp->bridge; 312 313 dp->bridge = NULL; 314 315 if (!refcount_dec_and_test(&bridge->refcount)) 316 return; 317 318 if (bridge->num) 319 dsa_bridge_num_put(br, bridge->num); 320 321 kfree(bridge); 322 } 323 324 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br, 325 struct netlink_ext_ack *extack) 326 { 327 struct dsa_notifier_bridge_info info = { 328 .tree_index = dp->ds->dst->index, 329 .sw_index = dp->ds->index, 330 .port = dp->index, 331 }; 332 struct net_device *dev = dp->slave; 333 struct net_device *brport_dev; 334 int err; 335 336 /* Here the interface is already bridged. Reflect the current 337 * configuration so that drivers can program their chips accordingly. 338 */ 339 err = dsa_port_bridge_create(dp, br, extack); 340 if (err) 341 return err; 342 343 brport_dev = dsa_port_to_bridge_port(dp); 344 345 info.bridge = *dp->bridge; 346 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info); 347 if (err) 348 goto out_rollback; 349 350 /* Drivers which support bridge TX forwarding should set this */ 351 dp->bridge->tx_fwd_offload = info.tx_fwd_offload; 352 353 err = switchdev_bridge_port_offload(brport_dev, dev, dp, 354 &dsa_slave_switchdev_notifier, 355 &dsa_slave_switchdev_blocking_notifier, 356 dp->bridge->tx_fwd_offload, extack); 357 if (err) 358 goto out_rollback_unbridge; 359 360 err = dsa_port_switchdev_sync_attrs(dp, extack); 361 if (err) 362 goto out_rollback_unoffload; 363 364 return 0; 365 366 out_rollback_unoffload: 367 switchdev_bridge_port_unoffload(brport_dev, dp, 368 &dsa_slave_switchdev_notifier, 369 &dsa_slave_switchdev_blocking_notifier); 370 out_rollback_unbridge: 371 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); 372 out_rollback: 373 dsa_port_bridge_destroy(dp, br); 374 return err; 375 } 376 377 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br) 378 { 379 struct net_device *brport_dev = dsa_port_to_bridge_port(dp); 380 381 /* Don't try to unoffload something that is not offloaded */ 382 if (!brport_dev) 383 return; 384 385 switchdev_bridge_port_unoffload(brport_dev, dp, 386 &dsa_slave_switchdev_notifier, 387 &dsa_slave_switchdev_blocking_notifier); 388 389 dsa_flush_workqueue(); 390 } 391 392 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br) 393 { 394 struct dsa_notifier_bridge_info info = { 395 .tree_index = dp->ds->dst->index, 396 .sw_index = dp->ds->index, 397 .port = dp->index, 398 }; 399 int err; 400 401 /* If the port could not be offloaded to begin with, then 402 * there is nothing to do. 403 */ 404 if (!dp->bridge) 405 return; 406 407 info.bridge = *dp->bridge; 408 409 /* Here the port is already unbridged. Reflect the current configuration 410 * so that drivers can program their chips accordingly. 411 */ 412 dsa_port_bridge_destroy(dp, br); 413 414 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info); 415 if (err) 416 dev_err(dp->ds->dev, 417 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n", 418 dp->index, ERR_PTR(err)); 419 420 dsa_port_switchdev_unsync_attrs(dp); 421 } 422 423 int dsa_port_lag_change(struct dsa_port *dp, 424 struct netdev_lag_lower_state_info *linfo) 425 { 426 struct dsa_notifier_lag_info info = { 427 .sw_index = dp->ds->index, 428 .port = dp->index, 429 }; 430 bool tx_enabled; 431 432 if (!dp->lag_dev) 433 return 0; 434 435 /* On statically configured aggregates (e.g. loadbalance 436 * without LACP) ports will always be tx_enabled, even if the 437 * link is down. Thus we require both link_up and tx_enabled 438 * in order to include it in the tx set. 439 */ 440 tx_enabled = linfo->link_up && linfo->tx_enabled; 441 442 if (tx_enabled == dp->lag_tx_enabled) 443 return 0; 444 445 dp->lag_tx_enabled = tx_enabled; 446 447 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info); 448 } 449 450 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag, 451 struct netdev_lag_upper_info *uinfo, 452 struct netlink_ext_ack *extack) 453 { 454 struct dsa_notifier_lag_info info = { 455 .sw_index = dp->ds->index, 456 .port = dp->index, 457 .lag = lag, 458 .info = uinfo, 459 }; 460 struct net_device *bridge_dev; 461 int err; 462 463 dsa_lag_map(dp->ds->dst, lag); 464 dp->lag_dev = lag; 465 466 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info); 467 if (err) 468 goto err_lag_join; 469 470 bridge_dev = netdev_master_upper_dev_get(lag); 471 if (!bridge_dev || !netif_is_bridge_master(bridge_dev)) 472 return 0; 473 474 err = dsa_port_bridge_join(dp, bridge_dev, extack); 475 if (err) 476 goto err_bridge_join; 477 478 return 0; 479 480 err_bridge_join: 481 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); 482 err_lag_join: 483 dp->lag_dev = NULL; 484 dsa_lag_unmap(dp->ds->dst, lag); 485 return err; 486 } 487 488 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag) 489 { 490 struct net_device *br = dsa_port_bridge_dev_get(dp); 491 492 if (br) 493 dsa_port_pre_bridge_leave(dp, br); 494 } 495 496 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag) 497 { 498 struct net_device *br = dsa_port_bridge_dev_get(dp); 499 struct dsa_notifier_lag_info info = { 500 .sw_index = dp->ds->index, 501 .port = dp->index, 502 .lag = lag, 503 }; 504 int err; 505 506 if (!dp->lag_dev) 507 return; 508 509 /* Port might have been part of a LAG that in turn was 510 * attached to a bridge. 511 */ 512 if (br) 513 dsa_port_bridge_leave(dp, br); 514 515 dp->lag_tx_enabled = false; 516 dp->lag_dev = NULL; 517 518 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info); 519 if (err) 520 dev_err(dp->ds->dev, 521 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n", 522 dp->index, ERR_PTR(err)); 523 524 dsa_lag_unmap(dp->ds->dst, lag); 525 } 526 527 /* Must be called under rcu_read_lock() */ 528 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp, 529 bool vlan_filtering, 530 struct netlink_ext_ack *extack) 531 { 532 struct dsa_switch *ds = dp->ds; 533 struct dsa_port *other_dp; 534 int err; 535 536 /* VLAN awareness was off, so the question is "can we turn it on". 537 * We may have had 8021q uppers, those need to go. Make sure we don't 538 * enter an inconsistent state: deny changing the VLAN awareness state 539 * as long as we have 8021q uppers. 540 */ 541 if (vlan_filtering && dsa_port_is_user(dp)) { 542 struct net_device *br = dsa_port_bridge_dev_get(dp); 543 struct net_device *upper_dev, *slave = dp->slave; 544 struct list_head *iter; 545 546 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) { 547 struct bridge_vlan_info br_info; 548 u16 vid; 549 550 if (!is_vlan_dev(upper_dev)) 551 continue; 552 553 vid = vlan_dev_vlan_id(upper_dev); 554 555 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the 556 * device, respectively the VID is not found, returning 557 * 0 means success, which is a failure for us here. 558 */ 559 err = br_vlan_get_info(br, vid, &br_info); 560 if (err == 0) { 561 NL_SET_ERR_MSG_MOD(extack, 562 "Must first remove VLAN uppers having VIDs also present in bridge"); 563 return false; 564 } 565 } 566 } 567 568 if (!ds->vlan_filtering_is_global) 569 return true; 570 571 /* For cases where enabling/disabling VLAN awareness is global to the 572 * switch, we need to handle the case where multiple bridges span 573 * different ports of the same switch device and one of them has a 574 * different setting than what is being requested. 575 */ 576 dsa_switch_for_each_port(other_dp, ds) { 577 struct net_device *other_br = dsa_port_bridge_dev_get(other_dp); 578 579 /* If it's the same bridge, it also has same 580 * vlan_filtering setting => no need to check 581 */ 582 if (!other_br || other_br == dsa_port_bridge_dev_get(dp)) 583 continue; 584 585 if (br_vlan_enabled(other_br) != vlan_filtering) { 586 NL_SET_ERR_MSG_MOD(extack, 587 "VLAN filtering is a global setting"); 588 return false; 589 } 590 } 591 return true; 592 } 593 594 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering, 595 struct netlink_ext_ack *extack) 596 { 597 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp); 598 struct dsa_switch *ds = dp->ds; 599 bool apply; 600 int err; 601 602 if (!ds->ops->port_vlan_filtering) 603 return -EOPNOTSUPP; 604 605 /* We are called from dsa_slave_switchdev_blocking_event(), 606 * which is not under rcu_read_lock(), unlike 607 * dsa_slave_switchdev_event(). 608 */ 609 rcu_read_lock(); 610 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack); 611 rcu_read_unlock(); 612 if (!apply) 613 return -EINVAL; 614 615 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering) 616 return 0; 617 618 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering, 619 extack); 620 if (err) 621 return err; 622 623 if (ds->vlan_filtering_is_global) { 624 struct dsa_port *other_dp; 625 626 ds->vlan_filtering = vlan_filtering; 627 628 dsa_switch_for_each_user_port(other_dp, ds) { 629 struct net_device *slave = dp->slave; 630 631 /* We might be called in the unbind path, so not 632 * all slave devices might still be registered. 633 */ 634 if (!slave) 635 continue; 636 637 err = dsa_slave_manage_vlan_filtering(slave, 638 vlan_filtering); 639 if (err) 640 goto restore; 641 } 642 } else { 643 dp->vlan_filtering = vlan_filtering; 644 645 err = dsa_slave_manage_vlan_filtering(dp->slave, 646 vlan_filtering); 647 if (err) 648 goto restore; 649 } 650 651 return 0; 652 653 restore: 654 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL); 655 656 if (ds->vlan_filtering_is_global) 657 ds->vlan_filtering = old_vlan_filtering; 658 else 659 dp->vlan_filtering = old_vlan_filtering; 660 661 return err; 662 } 663 664 /* This enforces legacy behavior for switch drivers which assume they can't 665 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0 666 */ 667 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp) 668 { 669 struct net_device *br = dsa_port_bridge_dev_get(dp); 670 struct dsa_switch *ds = dp->ds; 671 672 if (!br) 673 return false; 674 675 return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br); 676 } 677 678 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock) 679 { 680 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock); 681 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies); 682 struct dsa_notifier_ageing_time_info info; 683 int err; 684 685 info.ageing_time = ageing_time; 686 687 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info); 688 if (err) 689 return err; 690 691 dp->ageing_time = ageing_time; 692 693 return 0; 694 } 695 696 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, 697 struct switchdev_brport_flags flags, 698 struct netlink_ext_ack *extack) 699 { 700 struct dsa_switch *ds = dp->ds; 701 702 if (!ds->ops->port_pre_bridge_flags) 703 return -EINVAL; 704 705 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack); 706 } 707 708 int dsa_port_bridge_flags(struct dsa_port *dp, 709 struct switchdev_brport_flags flags, 710 struct netlink_ext_ack *extack) 711 { 712 struct dsa_switch *ds = dp->ds; 713 int err; 714 715 if (!ds->ops->port_bridge_flags) 716 return -EOPNOTSUPP; 717 718 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack); 719 if (err) 720 return err; 721 722 if (flags.mask & BR_LEARNING) { 723 bool learning = flags.val & BR_LEARNING; 724 725 if (learning == dp->learning) 726 return 0; 727 728 if ((dp->learning && !learning) && 729 (dp->stp_state == BR_STATE_LEARNING || 730 dp->stp_state == BR_STATE_FORWARDING)) 731 dsa_port_fast_age(dp); 732 733 dp->learning = learning; 734 } 735 736 return 0; 737 } 738 739 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu, 740 bool targeted_match) 741 { 742 struct dsa_notifier_mtu_info info = { 743 .sw_index = dp->ds->index, 744 .targeted_match = targeted_match, 745 .port = dp->index, 746 .mtu = new_mtu, 747 }; 748 749 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info); 750 } 751 752 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr, 753 u16 vid) 754 { 755 struct dsa_notifier_fdb_info info = { 756 .sw_index = dp->ds->index, 757 .port = dp->index, 758 .addr = addr, 759 .vid = vid, 760 }; 761 762 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info); 763 } 764 765 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr, 766 u16 vid) 767 { 768 struct dsa_notifier_fdb_info info = { 769 .sw_index = dp->ds->index, 770 .port = dp->index, 771 .addr = addr, 772 .vid = vid, 773 774 }; 775 776 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info); 777 } 778 779 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr, 780 u16 vid) 781 { 782 struct dsa_notifier_fdb_info info = { 783 .sw_index = dp->ds->index, 784 .port = dp->index, 785 .addr = addr, 786 .vid = vid, 787 }; 788 struct dsa_port *cpu_dp = dp->cpu_dp; 789 int err; 790 791 /* Avoid a call to __dev_set_promiscuity() on the master, which 792 * requires rtnl_lock(), since we can't guarantee that is held here, 793 * and we can't take it either. 794 */ 795 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) { 796 err = dev_uc_add(cpu_dp->master, addr); 797 if (err) 798 return err; 799 } 800 801 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info); 802 } 803 804 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr, 805 u16 vid) 806 { 807 struct dsa_notifier_fdb_info info = { 808 .sw_index = dp->ds->index, 809 .port = dp->index, 810 .addr = addr, 811 .vid = vid, 812 }; 813 struct dsa_port *cpu_dp = dp->cpu_dp; 814 int err; 815 816 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) { 817 err = dev_uc_del(cpu_dp->master, addr); 818 if (err) 819 return err; 820 } 821 822 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info); 823 } 824 825 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data) 826 { 827 struct dsa_switch *ds = dp->ds; 828 int port = dp->index; 829 830 if (!ds->ops->port_fdb_dump) 831 return -EOPNOTSUPP; 832 833 return ds->ops->port_fdb_dump(ds, port, cb, data); 834 } 835 836 int dsa_port_mdb_add(const struct dsa_port *dp, 837 const struct switchdev_obj_port_mdb *mdb) 838 { 839 struct dsa_notifier_mdb_info info = { 840 .sw_index = dp->ds->index, 841 .port = dp->index, 842 .mdb = mdb, 843 }; 844 845 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info); 846 } 847 848 int dsa_port_mdb_del(const struct dsa_port *dp, 849 const struct switchdev_obj_port_mdb *mdb) 850 { 851 struct dsa_notifier_mdb_info info = { 852 .sw_index = dp->ds->index, 853 .port = dp->index, 854 .mdb = mdb, 855 }; 856 857 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info); 858 } 859 860 int dsa_port_host_mdb_add(const struct dsa_port *dp, 861 const struct switchdev_obj_port_mdb *mdb) 862 { 863 struct dsa_notifier_mdb_info info = { 864 .sw_index = dp->ds->index, 865 .port = dp->index, 866 .mdb = mdb, 867 }; 868 struct dsa_port *cpu_dp = dp->cpu_dp; 869 int err; 870 871 err = dev_mc_add(cpu_dp->master, mdb->addr); 872 if (err) 873 return err; 874 875 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info); 876 } 877 878 int dsa_port_host_mdb_del(const struct dsa_port *dp, 879 const struct switchdev_obj_port_mdb *mdb) 880 { 881 struct dsa_notifier_mdb_info info = { 882 .sw_index = dp->ds->index, 883 .port = dp->index, 884 .mdb = mdb, 885 }; 886 struct dsa_port *cpu_dp = dp->cpu_dp; 887 int err; 888 889 err = dev_mc_del(cpu_dp->master, mdb->addr); 890 if (err) 891 return err; 892 893 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info); 894 } 895 896 int dsa_port_vlan_add(struct dsa_port *dp, 897 const struct switchdev_obj_port_vlan *vlan, 898 struct netlink_ext_ack *extack) 899 { 900 struct dsa_notifier_vlan_info info = { 901 .sw_index = dp->ds->index, 902 .port = dp->index, 903 .vlan = vlan, 904 .extack = extack, 905 }; 906 907 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info); 908 } 909 910 int dsa_port_vlan_del(struct dsa_port *dp, 911 const struct switchdev_obj_port_vlan *vlan) 912 { 913 struct dsa_notifier_vlan_info info = { 914 .sw_index = dp->ds->index, 915 .port = dp->index, 916 .vlan = vlan, 917 }; 918 919 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info); 920 } 921 922 int dsa_port_mrp_add(const struct dsa_port *dp, 923 const struct switchdev_obj_mrp *mrp) 924 { 925 struct dsa_switch *ds = dp->ds; 926 927 if (!ds->ops->port_mrp_add) 928 return -EOPNOTSUPP; 929 930 return ds->ops->port_mrp_add(ds, dp->index, mrp); 931 } 932 933 int dsa_port_mrp_del(const struct dsa_port *dp, 934 const struct switchdev_obj_mrp *mrp) 935 { 936 struct dsa_switch *ds = dp->ds; 937 938 if (!ds->ops->port_mrp_del) 939 return -EOPNOTSUPP; 940 941 return ds->ops->port_mrp_del(ds, dp->index, mrp); 942 } 943 944 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp, 945 const struct switchdev_obj_ring_role_mrp *mrp) 946 { 947 struct dsa_switch *ds = dp->ds; 948 949 if (!ds->ops->port_mrp_add_ring_role) 950 return -EOPNOTSUPP; 951 952 return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp); 953 } 954 955 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp, 956 const struct switchdev_obj_ring_role_mrp *mrp) 957 { 958 struct dsa_switch *ds = dp->ds; 959 960 if (!ds->ops->port_mrp_del_ring_role) 961 return -EOPNOTSUPP; 962 963 return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp); 964 } 965 966 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp, 967 const struct dsa_device_ops *tag_ops) 968 { 969 cpu_dp->rcv = tag_ops->rcv; 970 cpu_dp->tag_ops = tag_ops; 971 } 972 973 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp) 974 { 975 struct device_node *phy_dn; 976 struct phy_device *phydev; 977 978 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0); 979 if (!phy_dn) 980 return NULL; 981 982 phydev = of_phy_find_device(phy_dn); 983 if (!phydev) { 984 of_node_put(phy_dn); 985 return ERR_PTR(-EPROBE_DEFER); 986 } 987 988 of_node_put(phy_dn); 989 return phydev; 990 } 991 992 static void dsa_port_phylink_validate(struct phylink_config *config, 993 unsigned long *supported, 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 999 if (!ds->ops->phylink_validate) { 1000 if (config->mac_capabilities) 1001 phylink_generic_validate(config, supported, state); 1002 return; 1003 } 1004 1005 ds->ops->phylink_validate(ds, dp->index, supported, state); 1006 } 1007 1008 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config, 1009 struct phylink_link_state *state) 1010 { 1011 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1012 struct dsa_switch *ds = dp->ds; 1013 int err; 1014 1015 /* Only called for inband modes */ 1016 if (!ds->ops->phylink_mac_link_state) { 1017 state->link = 0; 1018 return; 1019 } 1020 1021 err = ds->ops->phylink_mac_link_state(ds, dp->index, state); 1022 if (err < 0) { 1023 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n", 1024 dp->index, err); 1025 state->link = 0; 1026 } 1027 } 1028 1029 static void dsa_port_phylink_mac_config(struct phylink_config *config, 1030 unsigned int mode, 1031 const struct phylink_link_state *state) 1032 { 1033 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1034 struct dsa_switch *ds = dp->ds; 1035 1036 if (!ds->ops->phylink_mac_config) 1037 return; 1038 1039 ds->ops->phylink_mac_config(ds, dp->index, mode, state); 1040 } 1041 1042 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config) 1043 { 1044 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1045 struct dsa_switch *ds = dp->ds; 1046 1047 if (!ds->ops->phylink_mac_an_restart) 1048 return; 1049 1050 ds->ops->phylink_mac_an_restart(ds, dp->index); 1051 } 1052 1053 static void dsa_port_phylink_mac_link_down(struct phylink_config *config, 1054 unsigned int mode, 1055 phy_interface_t interface) 1056 { 1057 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1058 struct phy_device *phydev = NULL; 1059 struct dsa_switch *ds = dp->ds; 1060 1061 if (dsa_port_is_user(dp)) 1062 phydev = dp->slave->phydev; 1063 1064 if (!ds->ops->phylink_mac_link_down) { 1065 if (ds->ops->adjust_link && phydev) 1066 ds->ops->adjust_link(ds, dp->index, phydev); 1067 return; 1068 } 1069 1070 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface); 1071 } 1072 1073 static void dsa_port_phylink_mac_link_up(struct phylink_config *config, 1074 struct phy_device *phydev, 1075 unsigned int mode, 1076 phy_interface_t interface, 1077 int speed, int duplex, 1078 bool tx_pause, bool rx_pause) 1079 { 1080 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config); 1081 struct dsa_switch *ds = dp->ds; 1082 1083 if (!ds->ops->phylink_mac_link_up) { 1084 if (ds->ops->adjust_link && phydev) 1085 ds->ops->adjust_link(ds, dp->index, phydev); 1086 return; 1087 } 1088 1089 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev, 1090 speed, duplex, tx_pause, rx_pause); 1091 } 1092 1093 static const struct phylink_mac_ops dsa_port_phylink_mac_ops = { 1094 .validate = dsa_port_phylink_validate, 1095 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state, 1096 .mac_config = dsa_port_phylink_mac_config, 1097 .mac_an_restart = dsa_port_phylink_mac_an_restart, 1098 .mac_link_down = dsa_port_phylink_mac_link_down, 1099 .mac_link_up = dsa_port_phylink_mac_link_up, 1100 }; 1101 1102 int dsa_port_phylink_create(struct dsa_port *dp) 1103 { 1104 struct dsa_switch *ds = dp->ds; 1105 phy_interface_t mode; 1106 int err; 1107 1108 err = of_get_phy_mode(dp->dn, &mode); 1109 if (err) 1110 mode = PHY_INTERFACE_MODE_NA; 1111 1112 /* Presence of phylink_mac_link_state or phylink_mac_an_restart is 1113 * an indicator of a legacy phylink driver. 1114 */ 1115 if (ds->ops->phylink_mac_link_state || 1116 ds->ops->phylink_mac_an_restart) 1117 dp->pl_config.legacy_pre_march2020 = true; 1118 1119 if (ds->ops->phylink_get_caps) 1120 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config); 1121 1122 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn), 1123 mode, &dsa_port_phylink_mac_ops); 1124 if (IS_ERR(dp->pl)) { 1125 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl)); 1126 return PTR_ERR(dp->pl); 1127 } 1128 1129 return 0; 1130 } 1131 1132 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable) 1133 { 1134 struct dsa_switch *ds = dp->ds; 1135 struct phy_device *phydev; 1136 int port = dp->index; 1137 int err = 0; 1138 1139 phydev = dsa_port_get_phy_device(dp); 1140 if (!phydev) 1141 return 0; 1142 1143 if (IS_ERR(phydev)) 1144 return PTR_ERR(phydev); 1145 1146 if (enable) { 1147 err = genphy_resume(phydev); 1148 if (err < 0) 1149 goto err_put_dev; 1150 1151 err = genphy_read_status(phydev); 1152 if (err < 0) 1153 goto err_put_dev; 1154 } else { 1155 err = genphy_suspend(phydev); 1156 if (err < 0) 1157 goto err_put_dev; 1158 } 1159 1160 if (ds->ops->adjust_link) 1161 ds->ops->adjust_link(ds, port, phydev); 1162 1163 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev)); 1164 1165 err_put_dev: 1166 put_device(&phydev->mdio.dev); 1167 return err; 1168 } 1169 1170 static int dsa_port_fixed_link_register_of(struct dsa_port *dp) 1171 { 1172 struct device_node *dn = dp->dn; 1173 struct dsa_switch *ds = dp->ds; 1174 struct phy_device *phydev; 1175 int port = dp->index; 1176 phy_interface_t mode; 1177 int err; 1178 1179 err = of_phy_register_fixed_link(dn); 1180 if (err) { 1181 dev_err(ds->dev, 1182 "failed to register the fixed PHY of port %d\n", 1183 port); 1184 return err; 1185 } 1186 1187 phydev = of_phy_find_device(dn); 1188 1189 err = of_get_phy_mode(dn, &mode); 1190 if (err) 1191 mode = PHY_INTERFACE_MODE_NA; 1192 phydev->interface = mode; 1193 1194 genphy_read_status(phydev); 1195 1196 if (ds->ops->adjust_link) 1197 ds->ops->adjust_link(ds, port, phydev); 1198 1199 put_device(&phydev->mdio.dev); 1200 1201 return 0; 1202 } 1203 1204 static int dsa_port_phylink_register(struct dsa_port *dp) 1205 { 1206 struct dsa_switch *ds = dp->ds; 1207 struct device_node *port_dn = dp->dn; 1208 int err; 1209 1210 dp->pl_config.dev = ds->dev; 1211 dp->pl_config.type = PHYLINK_DEV; 1212 dp->pl_config.pcs_poll = ds->pcs_poll; 1213 1214 err = dsa_port_phylink_create(dp); 1215 if (err) 1216 return err; 1217 1218 err = phylink_of_phy_connect(dp->pl, port_dn, 0); 1219 if (err && err != -ENODEV) { 1220 pr_err("could not attach to PHY: %d\n", err); 1221 goto err_phy_connect; 1222 } 1223 1224 return 0; 1225 1226 err_phy_connect: 1227 phylink_destroy(dp->pl); 1228 return err; 1229 } 1230 1231 int dsa_port_link_register_of(struct dsa_port *dp) 1232 { 1233 struct dsa_switch *ds = dp->ds; 1234 struct device_node *phy_np; 1235 int port = dp->index; 1236 1237 if (!ds->ops->adjust_link) { 1238 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0); 1239 if (of_phy_is_fixed_link(dp->dn) || phy_np) { 1240 if (ds->ops->phylink_mac_link_down) 1241 ds->ops->phylink_mac_link_down(ds, port, 1242 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA); 1243 return dsa_port_phylink_register(dp); 1244 } 1245 return 0; 1246 } 1247 1248 dev_warn(ds->dev, 1249 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n"); 1250 1251 if (of_phy_is_fixed_link(dp->dn)) 1252 return dsa_port_fixed_link_register_of(dp); 1253 else 1254 return dsa_port_setup_phy_of(dp, true); 1255 } 1256 1257 void dsa_port_link_unregister_of(struct dsa_port *dp) 1258 { 1259 struct dsa_switch *ds = dp->ds; 1260 1261 if (!ds->ops->adjust_link && dp->pl) { 1262 rtnl_lock(); 1263 phylink_disconnect_phy(dp->pl); 1264 rtnl_unlock(); 1265 phylink_destroy(dp->pl); 1266 dp->pl = NULL; 1267 return; 1268 } 1269 1270 if (of_phy_is_fixed_link(dp->dn)) 1271 of_phy_deregister_fixed_link(dp->dn); 1272 else 1273 dsa_port_setup_phy_of(dp, false); 1274 } 1275 1276 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data) 1277 { 1278 struct phy_device *phydev; 1279 int ret = -EOPNOTSUPP; 1280 1281 if (of_phy_is_fixed_link(dp->dn)) 1282 return ret; 1283 1284 phydev = dsa_port_get_phy_device(dp); 1285 if (IS_ERR_OR_NULL(phydev)) 1286 return ret; 1287 1288 ret = phy_ethtool_get_strings(phydev, data); 1289 put_device(&phydev->mdio.dev); 1290 1291 return ret; 1292 } 1293 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings); 1294 1295 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data) 1296 { 1297 struct phy_device *phydev; 1298 int ret = -EOPNOTSUPP; 1299 1300 if (of_phy_is_fixed_link(dp->dn)) 1301 return ret; 1302 1303 phydev = dsa_port_get_phy_device(dp); 1304 if (IS_ERR_OR_NULL(phydev)) 1305 return ret; 1306 1307 ret = phy_ethtool_get_stats(phydev, NULL, data); 1308 put_device(&phydev->mdio.dev); 1309 1310 return ret; 1311 } 1312 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats); 1313 1314 int dsa_port_get_phy_sset_count(struct dsa_port *dp) 1315 { 1316 struct phy_device *phydev; 1317 int ret = -EOPNOTSUPP; 1318 1319 if (of_phy_is_fixed_link(dp->dn)) 1320 return ret; 1321 1322 phydev = dsa_port_get_phy_device(dp); 1323 if (IS_ERR_OR_NULL(phydev)) 1324 return ret; 1325 1326 ret = phy_ethtool_get_sset_count(phydev); 1327 put_device(&phydev->mdio.dev); 1328 1329 return ret; 1330 } 1331 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count); 1332 1333 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr) 1334 { 1335 struct dsa_switch *ds = dp->ds; 1336 int err; 1337 1338 if (!ds->ops->port_hsr_join) 1339 return -EOPNOTSUPP; 1340 1341 dp->hsr_dev = hsr; 1342 1343 err = ds->ops->port_hsr_join(ds, dp->index, hsr); 1344 if (err) 1345 dp->hsr_dev = NULL; 1346 1347 return err; 1348 } 1349 1350 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr) 1351 { 1352 struct dsa_switch *ds = dp->ds; 1353 int err; 1354 1355 dp->hsr_dev = NULL; 1356 1357 if (ds->ops->port_hsr_leave) { 1358 err = ds->ops->port_hsr_leave(ds, dp->index, hsr); 1359 if (err) 1360 dev_err(dp->ds->dev, 1361 "port %d failed to leave HSR %s: %pe\n", 1362 dp->index, hsr->name, ERR_PTR(err)); 1363 } 1364 } 1365 1366 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast) 1367 { 1368 struct dsa_notifier_tag_8021q_vlan_info info = { 1369 .tree_index = dp->ds->dst->index, 1370 .sw_index = dp->ds->index, 1371 .port = dp->index, 1372 .vid = vid, 1373 }; 1374 1375 if (broadcast) 1376 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info); 1377 1378 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info); 1379 } 1380 1381 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast) 1382 { 1383 struct dsa_notifier_tag_8021q_vlan_info info = { 1384 .tree_index = dp->ds->dst->index, 1385 .sw_index = dp->ds->index, 1386 .port = dp->index, 1387 .vid = vid, 1388 }; 1389 int err; 1390 1391 if (broadcast) 1392 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info); 1393 else 1394 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info); 1395 if (err) 1396 dev_err(dp->ds->dev, 1397 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n", 1398 dp->index, vid, ERR_PTR(err)); 1399 } 1400