1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handling of a single switch port
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
9 #include <linux/if_bridge.h>
10 #include <linux/notifier.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_net.h>
16 static int dsa_broadcast(unsigned long e, void *v)
18 struct dsa_switch_tree *dst;
21 list_for_each_entry(dst, &dsa_tree_list, list) {
22 struct raw_notifier_head *nh = &dst->nh;
24 err = raw_notifier_call_chain(nh, e, v);
25 err = notifier_to_errno(err);
33 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
35 struct raw_notifier_head *nh = &dp->ds->dst->nh;
38 err = raw_notifier_call_chain(nh, e, v);
40 return notifier_to_errno(err);
43 int dsa_port_set_state(struct dsa_port *dp, u8 state)
45 struct dsa_switch *ds = dp->ds;
48 if (!ds->ops->port_stp_state_set)
51 ds->ops->port_stp_state_set(ds, port, state);
53 if (ds->ops->port_fast_age) {
54 /* Fast age FDB entries or flush appropriate forwarding database
55 * for the given port, if we are moving it from Learning or
56 * Forwarding state, to Disabled or Blocking or Listening state.
59 if ((dp->stp_state == BR_STATE_LEARNING ||
60 dp->stp_state == BR_STATE_FORWARDING) &&
61 (state == BR_STATE_DISABLED ||
62 state == BR_STATE_BLOCKING ||
63 state == BR_STATE_LISTENING))
64 ds->ops->port_fast_age(ds, port);
67 dp->stp_state = state;
72 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
76 err = dsa_port_set_state(dp, state);
78 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
81 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
83 struct dsa_switch *ds = dp->ds;
87 if (ds->ops->port_enable) {
88 err = ds->ops->port_enable(ds, port, phy);
94 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
97 phylink_start(dp->pl);
102 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
107 err = dsa_port_enable_rt(dp, phy);
113 void dsa_port_disable_rt(struct dsa_port *dp)
115 struct dsa_switch *ds = dp->ds;
116 int port = dp->index;
119 phylink_stop(dp->pl);
122 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
124 if (ds->ops->port_disable)
125 ds->ops->port_disable(ds, port);
128 void dsa_port_disable(struct dsa_port *dp)
131 dsa_port_disable_rt(dp);
135 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
137 struct dsa_notifier_bridge_info info = {
138 .tree_index = dp->ds->dst->index,
139 .sw_index = dp->ds->index,
145 /* Set the flooding mode before joining the port in the switch */
146 err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD);
150 /* Here the interface is already bridged. Reflect the current
151 * configuration so that drivers can program their chips accordingly.
155 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
157 /* The bridging is rolled back on error */
159 dsa_port_bridge_flags(dp, 0);
160 dp->bridge_dev = NULL;
166 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
168 struct dsa_notifier_bridge_info info = {
169 .tree_index = dp->ds->dst->index,
170 .sw_index = dp->ds->index,
176 /* Here the port is already unbridged. Reflect the current configuration
177 * so that drivers can program their chips accordingly.
179 dp->bridge_dev = NULL;
181 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
183 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
185 /* Port is leaving the bridge, disable flooding */
186 dsa_port_bridge_flags(dp, 0);
188 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
189 * so allow it to be in BR_STATE_FORWARDING to be kept functional
191 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
194 int dsa_port_lag_change(struct dsa_port *dp,
195 struct netdev_lag_lower_state_info *linfo)
197 struct dsa_notifier_lag_info info = {
198 .sw_index = dp->ds->index,
206 /* On statically configured aggregates (e.g. loadbalance
207 * without LACP) ports will always be tx_enabled, even if the
208 * link is down. Thus we require both link_up and tx_enabled
209 * in order to include it in the tx set.
211 tx_enabled = linfo->link_up && linfo->tx_enabled;
213 if (tx_enabled == dp->lag_tx_enabled)
216 dp->lag_tx_enabled = tx_enabled;
218 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
221 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
222 struct netdev_lag_upper_info *uinfo)
224 struct dsa_notifier_lag_info info = {
225 .sw_index = dp->ds->index,
232 dsa_lag_map(dp->ds->dst, lag);
235 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
238 dsa_lag_unmap(dp->ds->dst, lag);
244 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
246 struct dsa_notifier_lag_info info = {
247 .sw_index = dp->ds->index,
256 /* Port might have been part of a LAG that in turn was
257 * attached to a bridge.
260 dsa_port_bridge_leave(dp, dp->bridge_dev);
262 dp->lag_tx_enabled = false;
265 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
267 pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n",
270 dsa_lag_unmap(dp->ds->dst, lag);
273 /* Must be called under rcu_read_lock() */
274 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
277 struct dsa_switch *ds = dp->ds;
280 /* VLAN awareness was off, so the question is "can we turn it on".
281 * We may have had 8021q uppers, those need to go. Make sure we don't
282 * enter an inconsistent state: deny changing the VLAN awareness state
283 * as long as we have 8021q uppers.
285 if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
286 struct net_device *upper_dev, *slave = dp->slave;
287 struct net_device *br = dp->bridge_dev;
288 struct list_head *iter;
290 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
291 struct bridge_vlan_info br_info;
294 if (!is_vlan_dev(upper_dev))
297 vid = vlan_dev_vlan_id(upper_dev);
299 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
300 * device, respectively the VID is not found, returning
301 * 0 means success, which is a failure for us here.
303 err = br_vlan_get_info(br, vid, &br_info);
305 dev_err(ds->dev, "Must remove upper %s first\n",
312 if (!ds->vlan_filtering_is_global)
315 /* For cases where enabling/disabling VLAN awareness is global to the
316 * switch, we need to handle the case where multiple bridges span
317 * different ports of the same switch device and one of them has a
318 * different setting than what is being requested.
320 for (i = 0; i < ds->num_ports; i++) {
321 struct net_device *other_bridge;
323 other_bridge = dsa_to_port(ds, i)->bridge_dev;
326 /* If it's the same bridge, it also has same
327 * vlan_filtering setting => no need to check
329 if (other_bridge == dp->bridge_dev)
331 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
332 dev_err(ds->dev, "VLAN filtering is a global setting\n");
339 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering)
341 struct dsa_switch *ds = dp->ds;
345 if (!ds->ops->port_vlan_filtering)
348 /* We are called from dsa_slave_switchdev_blocking_event(),
349 * which is not under rcu_read_lock(), unlike
350 * dsa_slave_switchdev_event().
353 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering);
358 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
361 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering);
365 if (ds->vlan_filtering_is_global)
366 ds->vlan_filtering = vlan_filtering;
368 dp->vlan_filtering = vlan_filtering;
373 /* This enforces legacy behavior for switch drivers which assume they can't
374 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
376 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
378 struct dsa_switch *ds = dp->ds;
383 return (!ds->configure_vlan_while_not_filtering &&
384 !br_vlan_enabled(dp->bridge_dev));
387 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
389 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
390 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
391 struct dsa_notifier_ageing_time_info info;
394 info.ageing_time = ageing_time;
396 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
400 dp->ageing_time = ageing_time;
405 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags)
407 struct dsa_switch *ds = dp->ds;
409 if (!ds->ops->port_egress_floods ||
410 (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
416 int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags)
418 struct dsa_switch *ds = dp->ds;
419 int port = dp->index;
422 if (ds->ops->port_egress_floods)
423 err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
424 flags & BR_MCAST_FLOOD);
429 int dsa_port_mrouter(struct dsa_port *dp, bool mrouter)
431 struct dsa_switch *ds = dp->ds;
432 int port = dp->index;
434 if (!ds->ops->port_egress_floods)
437 return ds->ops->port_egress_floods(ds, port, true, mrouter);
440 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
441 bool propagate_upstream)
443 struct dsa_notifier_mtu_info info = {
444 .sw_index = dp->ds->index,
445 .propagate_upstream = propagate_upstream,
450 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
453 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
456 struct dsa_notifier_fdb_info info = {
457 .sw_index = dp->ds->index,
463 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
466 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
469 struct dsa_notifier_fdb_info info = {
470 .sw_index = dp->ds->index,
477 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
480 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
482 struct dsa_switch *ds = dp->ds;
483 int port = dp->index;
485 if (!ds->ops->port_fdb_dump)
488 return ds->ops->port_fdb_dump(ds, port, cb, data);
491 int dsa_port_mdb_add(const struct dsa_port *dp,
492 const struct switchdev_obj_port_mdb *mdb)
494 struct dsa_notifier_mdb_info info = {
495 .sw_index = dp->ds->index,
500 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
503 int dsa_port_mdb_del(const struct dsa_port *dp,
504 const struct switchdev_obj_port_mdb *mdb)
506 struct dsa_notifier_mdb_info info = {
507 .sw_index = dp->ds->index,
512 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
515 int dsa_port_vlan_add(struct dsa_port *dp,
516 const struct switchdev_obj_port_vlan *vlan)
518 struct dsa_notifier_vlan_info info = {
519 .sw_index = dp->ds->index,
524 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
527 int dsa_port_vlan_del(struct dsa_port *dp,
528 const struct switchdev_obj_port_vlan *vlan)
530 struct dsa_notifier_vlan_info info = {
531 .sw_index = dp->ds->index,
536 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
539 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
541 struct device_node *phy_dn;
542 struct phy_device *phydev;
544 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
548 phydev = of_phy_find_device(phy_dn);
551 return ERR_PTR(-EPROBE_DEFER);
558 static void dsa_port_phylink_validate(struct phylink_config *config,
559 unsigned long *supported,
560 struct phylink_link_state *state)
562 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
563 struct dsa_switch *ds = dp->ds;
565 if (!ds->ops->phylink_validate)
568 ds->ops->phylink_validate(ds, dp->index, supported, state);
571 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
572 struct phylink_link_state *state)
574 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
575 struct dsa_switch *ds = dp->ds;
578 /* Only called for inband modes */
579 if (!ds->ops->phylink_mac_link_state) {
584 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
586 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
592 static void dsa_port_phylink_mac_config(struct phylink_config *config,
594 const struct phylink_link_state *state)
596 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
597 struct dsa_switch *ds = dp->ds;
599 if (!ds->ops->phylink_mac_config)
602 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
605 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
607 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
608 struct dsa_switch *ds = dp->ds;
610 if (!ds->ops->phylink_mac_an_restart)
613 ds->ops->phylink_mac_an_restart(ds, dp->index);
616 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
618 phy_interface_t interface)
620 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
621 struct phy_device *phydev = NULL;
622 struct dsa_switch *ds = dp->ds;
624 if (dsa_is_user_port(ds, dp->index))
625 phydev = dp->slave->phydev;
627 if (!ds->ops->phylink_mac_link_down) {
628 if (ds->ops->adjust_link && phydev)
629 ds->ops->adjust_link(ds, dp->index, phydev);
633 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
636 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
637 struct phy_device *phydev,
639 phy_interface_t interface,
640 int speed, int duplex,
641 bool tx_pause, bool rx_pause)
643 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
644 struct dsa_switch *ds = dp->ds;
646 if (!ds->ops->phylink_mac_link_up) {
647 if (ds->ops->adjust_link && phydev)
648 ds->ops->adjust_link(ds, dp->index, phydev);
652 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
653 speed, duplex, tx_pause, rx_pause);
656 const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
657 .validate = dsa_port_phylink_validate,
658 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
659 .mac_config = dsa_port_phylink_mac_config,
660 .mac_an_restart = dsa_port_phylink_mac_an_restart,
661 .mac_link_down = dsa_port_phylink_mac_link_down,
662 .mac_link_up = dsa_port_phylink_mac_link_up,
665 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
667 struct dsa_switch *ds = dp->ds;
668 struct phy_device *phydev;
669 int port = dp->index;
672 phydev = dsa_port_get_phy_device(dp);
677 return PTR_ERR(phydev);
680 err = genphy_resume(phydev);
684 err = genphy_read_status(phydev);
688 err = genphy_suspend(phydev);
693 if (ds->ops->adjust_link)
694 ds->ops->adjust_link(ds, port, phydev);
696 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
699 put_device(&phydev->mdio.dev);
703 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
705 struct device_node *dn = dp->dn;
706 struct dsa_switch *ds = dp->ds;
707 struct phy_device *phydev;
708 int port = dp->index;
709 phy_interface_t mode;
712 err = of_phy_register_fixed_link(dn);
715 "failed to register the fixed PHY of port %d\n",
720 phydev = of_phy_find_device(dn);
722 err = of_get_phy_mode(dn, &mode);
724 mode = PHY_INTERFACE_MODE_NA;
725 phydev->interface = mode;
727 genphy_read_status(phydev);
729 if (ds->ops->adjust_link)
730 ds->ops->adjust_link(ds, port, phydev);
732 put_device(&phydev->mdio.dev);
737 static int dsa_port_phylink_register(struct dsa_port *dp)
739 struct dsa_switch *ds = dp->ds;
740 struct device_node *port_dn = dp->dn;
741 phy_interface_t mode;
744 err = of_get_phy_mode(port_dn, &mode);
746 mode = PHY_INTERFACE_MODE_NA;
748 dp->pl_config.dev = ds->dev;
749 dp->pl_config.type = PHYLINK_DEV;
750 dp->pl_config.pcs_poll = ds->pcs_poll;
752 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
753 mode, &dsa_port_phylink_mac_ops);
754 if (IS_ERR(dp->pl)) {
755 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
756 return PTR_ERR(dp->pl);
759 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
760 if (err && err != -ENODEV) {
761 pr_err("could not attach to PHY: %d\n", err);
762 goto err_phy_connect;
768 phylink_destroy(dp->pl);
772 int dsa_port_link_register_of(struct dsa_port *dp)
774 struct dsa_switch *ds = dp->ds;
775 struct device_node *phy_np;
776 int port = dp->index;
778 if (!ds->ops->adjust_link) {
779 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
780 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
781 if (ds->ops->phylink_mac_link_down)
782 ds->ops->phylink_mac_link_down(ds, port,
783 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
784 return dsa_port_phylink_register(dp);
790 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
792 if (of_phy_is_fixed_link(dp->dn))
793 return dsa_port_fixed_link_register_of(dp);
795 return dsa_port_setup_phy_of(dp, true);
798 void dsa_port_link_unregister_of(struct dsa_port *dp)
800 struct dsa_switch *ds = dp->ds;
802 if (!ds->ops->adjust_link && dp->pl) {
804 phylink_disconnect_phy(dp->pl);
806 phylink_destroy(dp->pl);
811 if (of_phy_is_fixed_link(dp->dn))
812 of_phy_deregister_fixed_link(dp->dn);
814 dsa_port_setup_phy_of(dp, false);
817 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
819 struct phy_device *phydev;
820 int ret = -EOPNOTSUPP;
822 if (of_phy_is_fixed_link(dp->dn))
825 phydev = dsa_port_get_phy_device(dp);
826 if (IS_ERR_OR_NULL(phydev))
829 ret = phy_ethtool_get_strings(phydev, data);
830 put_device(&phydev->mdio.dev);
834 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
836 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
838 struct phy_device *phydev;
839 int ret = -EOPNOTSUPP;
841 if (of_phy_is_fixed_link(dp->dn))
844 phydev = dsa_port_get_phy_device(dp);
845 if (IS_ERR_OR_NULL(phydev))
848 ret = phy_ethtool_get_stats(phydev, NULL, data);
849 put_device(&phydev->mdio.dev);
853 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
855 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
857 struct phy_device *phydev;
858 int ret = -EOPNOTSUPP;
860 if (of_phy_is_fixed_link(dp->dn))
863 phydev = dsa_port_get_phy_device(dp);
864 if (IS_ERR_OR_NULL(phydev))
867 ret = phy_ethtool_get_sset_count(phydev);
868 put_device(&phydev->mdio.dev);
872 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);