1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handling of a single switch port
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
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,
44 struct switchdev_trans *trans)
46 struct dsa_switch *ds = dp->ds;
49 if (switchdev_trans_ph_prepare(trans))
50 return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
52 if (ds->ops->port_stp_state_set)
53 ds->ops->port_stp_state_set(ds, port, state);
55 if (ds->ops->port_fast_age) {
56 /* Fast age FDB entries or flush appropriate forwarding database
57 * for the given port, if we are moving it from Learning or
58 * Forwarding state, to Disabled or Blocking or Listening state.
61 if ((dp->stp_state == BR_STATE_LEARNING ||
62 dp->stp_state == BR_STATE_FORWARDING) &&
63 (state == BR_STATE_DISABLED ||
64 state == BR_STATE_BLOCKING ||
65 state == BR_STATE_LISTENING))
66 ds->ops->port_fast_age(ds, port);
69 dp->stp_state = state;
74 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
78 err = dsa_port_set_state(dp, state, NULL);
80 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
83 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
85 struct dsa_switch *ds = dp->ds;
89 if (ds->ops->port_enable) {
90 err = ds->ops->port_enable(ds, port, phy);
96 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
99 phylink_start(dp->pl);
104 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
109 err = dsa_port_enable_rt(dp, phy);
115 void dsa_port_disable_rt(struct dsa_port *dp)
117 struct dsa_switch *ds = dp->ds;
118 int port = dp->index;
121 phylink_stop(dp->pl);
124 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
126 if (ds->ops->port_disable)
127 ds->ops->port_disable(ds, port);
130 void dsa_port_disable(struct dsa_port *dp)
133 dsa_port_disable_rt(dp);
137 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
139 struct dsa_notifier_bridge_info info = {
140 .tree_index = dp->ds->dst->index,
141 .sw_index = dp->ds->index,
147 /* Set the flooding mode before joining the port in the switch */
148 err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL);
152 /* Here the interface is already bridged. Reflect the current
153 * configuration so that drivers can program their chips accordingly.
157 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
159 /* The bridging is rolled back on error */
161 dsa_port_bridge_flags(dp, 0, NULL);
162 dp->bridge_dev = NULL;
168 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
170 struct dsa_notifier_bridge_info info = {
171 .tree_index = dp->ds->dst->index,
172 .sw_index = dp->ds->index,
178 /* Here the port is already unbridged. Reflect the current configuration
179 * so that drivers can program their chips accordingly.
181 dp->bridge_dev = NULL;
183 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
185 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
187 /* Port is leaving the bridge, disable flooding */
188 dsa_port_bridge_flags(dp, 0, NULL);
190 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
191 * so allow it to be in BR_STATE_FORWARDING to be kept functional
193 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
196 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
199 struct dsa_switch *ds = dp->ds;
202 if (!ds->vlan_filtering_is_global)
205 /* For cases where enabling/disabling VLAN awareness is global to the
206 * switch, we need to handle the case where multiple bridges span
207 * different ports of the same switch device and one of them has a
208 * different setting than what is being requested.
210 for (i = 0; i < ds->num_ports; i++) {
211 struct net_device *other_bridge;
213 other_bridge = dsa_to_port(ds, i)->bridge_dev;
216 /* If it's the same bridge, it also has same
217 * vlan_filtering setting => no need to check
219 if (other_bridge == dp->bridge_dev)
221 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
222 dev_err(ds->dev, "VLAN filtering is a global setting\n");
229 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
230 struct switchdev_trans *trans)
232 struct dsa_switch *ds = dp->ds;
235 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
236 if (switchdev_trans_ph_prepare(trans))
239 if (!ds->ops->port_vlan_filtering)
242 if (!dsa_port_can_apply_vlan_filtering(dp, vlan_filtering))
245 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
248 err = ds->ops->port_vlan_filtering(ds, dp->index,
253 if (ds->vlan_filtering_is_global)
254 ds->vlan_filtering = vlan_filtering;
256 dp->vlan_filtering = vlan_filtering;
260 /* This enforces legacy behavior for switch drivers which assume they can't
261 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
263 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
265 struct dsa_switch *ds = dp->ds;
270 return (!ds->configure_vlan_while_not_filtering &&
271 !br_vlan_enabled(dp->bridge_dev));
274 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
275 struct switchdev_trans *trans)
277 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
278 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
279 struct dsa_notifier_ageing_time_info info = {
280 .ageing_time = ageing_time,
284 if (switchdev_trans_ph_prepare(trans))
285 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
287 dp->ageing_time = ageing_time;
289 return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
292 int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags,
293 struct switchdev_trans *trans)
295 struct dsa_switch *ds = dp->ds;
297 if (!ds->ops->port_egress_floods ||
298 (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
304 int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags,
305 struct switchdev_trans *trans)
307 struct dsa_switch *ds = dp->ds;
308 int port = dp->index;
311 if (switchdev_trans_ph_prepare(trans))
314 if (ds->ops->port_egress_floods)
315 err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
316 flags & BR_MCAST_FLOOD);
321 int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
322 struct switchdev_trans *trans)
324 struct dsa_switch *ds = dp->ds;
325 int port = dp->index;
327 if (switchdev_trans_ph_prepare(trans))
328 return ds->ops->port_egress_floods ? 0 : -EOPNOTSUPP;
330 return ds->ops->port_egress_floods(ds, port, true, mrouter);
333 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
334 bool propagate_upstream)
336 struct dsa_notifier_mtu_info info = {
337 .sw_index = dp->ds->index,
338 .propagate_upstream = propagate_upstream,
343 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
346 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
349 struct dsa_notifier_fdb_info info = {
350 .sw_index = dp->ds->index,
356 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
359 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
362 struct dsa_notifier_fdb_info info = {
363 .sw_index = dp->ds->index,
370 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
373 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
375 struct dsa_switch *ds = dp->ds;
376 int port = dp->index;
378 if (!ds->ops->port_fdb_dump)
381 return ds->ops->port_fdb_dump(ds, port, cb, data);
384 int dsa_port_mdb_add(const struct dsa_port *dp,
385 const struct switchdev_obj_port_mdb *mdb,
386 struct switchdev_trans *trans)
388 struct dsa_notifier_mdb_info info = {
389 .sw_index = dp->ds->index,
395 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
398 int dsa_port_mdb_del(const struct dsa_port *dp,
399 const struct switchdev_obj_port_mdb *mdb)
401 struct dsa_notifier_mdb_info info = {
402 .sw_index = dp->ds->index,
407 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
410 int dsa_port_vlan_add(struct dsa_port *dp,
411 const struct switchdev_obj_port_vlan *vlan,
412 struct switchdev_trans *trans)
414 struct dsa_notifier_vlan_info info = {
415 .sw_index = dp->ds->index,
421 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
424 int dsa_port_vlan_del(struct dsa_port *dp,
425 const struct switchdev_obj_port_vlan *vlan)
427 struct dsa_notifier_vlan_info info = {
428 .sw_index = dp->ds->index,
433 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
436 int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags)
438 struct switchdev_obj_port_vlan vlan = {
439 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
444 struct switchdev_trans trans;
447 trans.ph_prepare = true;
448 err = dsa_port_vlan_add(dp, &vlan, &trans);
452 trans.ph_prepare = false;
453 return dsa_port_vlan_add(dp, &vlan, &trans);
455 EXPORT_SYMBOL(dsa_port_vid_add);
457 int dsa_port_vid_del(struct dsa_port *dp, u16 vid)
459 struct switchdev_obj_port_vlan vlan = {
460 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
465 return dsa_port_vlan_del(dp, &vlan);
467 EXPORT_SYMBOL(dsa_port_vid_del);
469 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
471 struct device_node *phy_dn;
472 struct phy_device *phydev;
474 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
478 phydev = of_phy_find_device(phy_dn);
481 return ERR_PTR(-EPROBE_DEFER);
488 static void dsa_port_phylink_validate(struct phylink_config *config,
489 unsigned long *supported,
490 struct phylink_link_state *state)
492 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
493 struct dsa_switch *ds = dp->ds;
495 if (!ds->ops->phylink_validate)
498 ds->ops->phylink_validate(ds, dp->index, supported, state);
501 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
502 struct phylink_link_state *state)
504 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
505 struct dsa_switch *ds = dp->ds;
508 /* Only called for inband modes */
509 if (!ds->ops->phylink_mac_link_state) {
514 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
516 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
522 static void dsa_port_phylink_mac_config(struct phylink_config *config,
524 const struct phylink_link_state *state)
526 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
527 struct dsa_switch *ds = dp->ds;
529 if (!ds->ops->phylink_mac_config)
532 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
535 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
537 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
538 struct dsa_switch *ds = dp->ds;
540 if (!ds->ops->phylink_mac_an_restart)
543 ds->ops->phylink_mac_an_restart(ds, dp->index);
546 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
548 phy_interface_t interface)
550 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
551 struct phy_device *phydev = NULL;
552 struct dsa_switch *ds = dp->ds;
554 if (dsa_is_user_port(ds, dp->index))
555 phydev = dp->slave->phydev;
557 if (!ds->ops->phylink_mac_link_down) {
558 if (ds->ops->adjust_link && phydev)
559 ds->ops->adjust_link(ds, dp->index, phydev);
563 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
566 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
567 struct phy_device *phydev,
569 phy_interface_t interface,
570 int speed, int duplex,
571 bool tx_pause, bool rx_pause)
573 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
574 struct dsa_switch *ds = dp->ds;
576 if (!ds->ops->phylink_mac_link_up) {
577 if (ds->ops->adjust_link && phydev)
578 ds->ops->adjust_link(ds, dp->index, phydev);
582 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
583 speed, duplex, tx_pause, rx_pause);
586 const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
587 .validate = dsa_port_phylink_validate,
588 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
589 .mac_config = dsa_port_phylink_mac_config,
590 .mac_an_restart = dsa_port_phylink_mac_an_restart,
591 .mac_link_down = dsa_port_phylink_mac_link_down,
592 .mac_link_up = dsa_port_phylink_mac_link_up,
595 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
597 struct dsa_switch *ds = dp->ds;
598 struct phy_device *phydev;
599 int port = dp->index;
602 phydev = dsa_port_get_phy_device(dp);
607 return PTR_ERR(phydev);
610 err = genphy_resume(phydev);
614 err = genphy_read_status(phydev);
618 err = genphy_suspend(phydev);
623 if (ds->ops->adjust_link)
624 ds->ops->adjust_link(ds, port, phydev);
626 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
629 put_device(&phydev->mdio.dev);
633 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
635 struct device_node *dn = dp->dn;
636 struct dsa_switch *ds = dp->ds;
637 struct phy_device *phydev;
638 int port = dp->index;
639 phy_interface_t mode;
642 err = of_phy_register_fixed_link(dn);
645 "failed to register the fixed PHY of port %d\n",
650 phydev = of_phy_find_device(dn);
652 err = of_get_phy_mode(dn, &mode);
654 mode = PHY_INTERFACE_MODE_NA;
655 phydev->interface = mode;
657 genphy_read_status(phydev);
659 if (ds->ops->adjust_link)
660 ds->ops->adjust_link(ds, port, phydev);
662 put_device(&phydev->mdio.dev);
667 static int dsa_port_phylink_register(struct dsa_port *dp)
669 struct dsa_switch *ds = dp->ds;
670 struct device_node *port_dn = dp->dn;
671 phy_interface_t mode;
674 err = of_get_phy_mode(port_dn, &mode);
676 mode = PHY_INTERFACE_MODE_NA;
678 dp->pl_config.dev = ds->dev;
679 dp->pl_config.type = PHYLINK_DEV;
680 dp->pl_config.pcs_poll = ds->pcs_poll;
682 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
683 mode, &dsa_port_phylink_mac_ops);
684 if (IS_ERR(dp->pl)) {
685 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
686 return PTR_ERR(dp->pl);
689 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
690 if (err && err != -ENODEV) {
691 pr_err("could not attach to PHY: %d\n", err);
692 goto err_phy_connect;
698 phylink_destroy(dp->pl);
702 int dsa_port_link_register_of(struct dsa_port *dp)
704 struct dsa_switch *ds = dp->ds;
705 struct device_node *phy_np;
706 int port = dp->index;
708 if (!ds->ops->adjust_link) {
709 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
710 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
711 if (ds->ops->phylink_mac_link_down)
712 ds->ops->phylink_mac_link_down(ds, port,
713 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
714 return dsa_port_phylink_register(dp);
720 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
722 if (of_phy_is_fixed_link(dp->dn))
723 return dsa_port_fixed_link_register_of(dp);
725 return dsa_port_setup_phy_of(dp, true);
728 void dsa_port_link_unregister_of(struct dsa_port *dp)
730 struct dsa_switch *ds = dp->ds;
732 if (!ds->ops->adjust_link && dp->pl) {
734 phylink_disconnect_phy(dp->pl);
736 phylink_destroy(dp->pl);
741 if (of_phy_is_fixed_link(dp->dn))
742 of_phy_deregister_fixed_link(dp->dn);
744 dsa_port_setup_phy_of(dp, false);
747 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
749 struct phy_device *phydev;
750 int ret = -EOPNOTSUPP;
752 if (of_phy_is_fixed_link(dp->dn))
755 phydev = dsa_port_get_phy_device(dp);
756 if (IS_ERR_OR_NULL(phydev))
759 ret = phy_ethtool_get_strings(phydev, data);
760 put_device(&phydev->mdio.dev);
764 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
766 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
768 struct phy_device *phydev;
769 int ret = -EOPNOTSUPP;
771 if (of_phy_is_fixed_link(dp->dn))
774 phydev = dsa_port_get_phy_device(dp);
775 if (IS_ERR_OR_NULL(phydev))
778 ret = phy_ethtool_get_stats(phydev, NULL, data);
779 put_device(&phydev->mdio.dev);
783 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
785 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
787 struct phy_device *phydev;
788 int ret = -EOPNOTSUPP;
790 if (of_phy_is_fixed_link(dp->dn))
793 phydev = dsa_port_get_phy_device(dp);
794 if (IS_ERR_OR_NULL(phydev))
797 ret = phy_ethtool_get_sset_count(phydev);
798 put_device(&phydev->mdio.dev);
802 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);