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>
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.
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.
28 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
30 return dsa_tree_notify(dp->ds->dst, e, v);
33 int dsa_port_set_state(struct dsa_port *dp, u8 state)
35 struct dsa_switch *ds = dp->ds;
38 if (!ds->ops->port_stp_state_set)
41 ds->ops->port_stp_state_set(ds, port, state);
43 if (ds->ops->port_fast_age) {
44 /* Fast age FDB entries or flush appropriate forwarding database
45 * for the given port, if we are moving it from Learning or
46 * Forwarding state, to Disabled or Blocking or Listening state.
49 if ((dp->stp_state == BR_STATE_LEARNING ||
50 dp->stp_state == BR_STATE_FORWARDING) &&
51 (state == BR_STATE_DISABLED ||
52 state == BR_STATE_BLOCKING ||
53 state == BR_STATE_LISTENING))
54 ds->ops->port_fast_age(ds, port);
57 dp->stp_state = state;
62 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
66 err = dsa_port_set_state(dp, state);
68 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
71 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
73 struct dsa_switch *ds = dp->ds;
77 if (ds->ops->port_enable) {
78 err = ds->ops->port_enable(ds, port, phy);
84 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
87 phylink_start(dp->pl);
92 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
97 err = dsa_port_enable_rt(dp, phy);
103 void dsa_port_disable_rt(struct dsa_port *dp)
105 struct dsa_switch *ds = dp->ds;
106 int port = dp->index;
109 phylink_stop(dp->pl);
112 dsa_port_set_state_now(dp, BR_STATE_DISABLED);
114 if (ds->ops->port_disable)
115 ds->ops->port_disable(ds, port);
118 void dsa_port_disable(struct dsa_port *dp)
121 dsa_port_disable_rt(dp);
125 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
126 struct netlink_ext_ack *extack)
128 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
130 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
133 for_each_set_bit(flag, &mask, 32) {
134 struct switchdev_brport_flags flags = {0};
136 flags.mask = BIT(flag);
138 if (br_port_flag_is_set(brport_dev, BIT(flag)))
139 flags.val = BIT(flag);
141 err = dsa_port_bridge_flags(dp, flags, extack);
142 if (err && err != -EOPNOTSUPP)
149 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
151 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
152 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
156 for_each_set_bit(flag, &mask, 32) {
157 struct switchdev_brport_flags flags = {0};
159 flags.mask = BIT(flag);
160 flags.val = val & BIT(flag);
162 err = dsa_port_bridge_flags(dp, flags, NULL);
163 if (err && err != -EOPNOTSUPP)
165 "failed to clear bridge port flag %lu: %pe\n",
166 flags.val, ERR_PTR(err));
170 static int dsa_port_switchdev_sync(struct dsa_port *dp,
171 struct netlink_ext_ack *extack)
173 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
174 struct net_device *br = dp->bridge_dev;
177 err = dsa_port_inherit_brport_flags(dp, extack);
181 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev));
182 if (err && err != -EOPNOTSUPP)
185 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
186 if (err && err != -EOPNOTSUPP)
189 err = dsa_port_mrouter(dp->cpu_dp, br_multicast_router(br), extack);
190 if (err && err != -EOPNOTSUPP)
193 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
194 if (err && err != -EOPNOTSUPP)
197 err = br_mdb_replay(br, brport_dev,
198 &dsa_slave_switchdev_blocking_notifier,
200 if (err && err != -EOPNOTSUPP)
203 err = br_fdb_replay(br, brport_dev, &dsa_slave_switchdev_notifier);
204 if (err && err != -EOPNOTSUPP)
207 err = br_vlan_replay(br, brport_dev,
208 &dsa_slave_switchdev_blocking_notifier,
210 if (err && err != -EOPNOTSUPP)
216 static void dsa_port_switchdev_unsync(struct dsa_port *dp)
218 /* Configure the port for standalone mode (no address learning,
220 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
221 * when the user requests it through netlink or sysfs, but not
222 * automatically at port join or leave, so we need to handle resetting
223 * the brport flags ourselves. But we even prefer it that way, because
224 * otherwise, some setups might never get the notification they need,
225 * for example, when a port leaves a LAG that offloads the bridge,
226 * it becomes standalone, but as far as the bridge is concerned, no
229 dsa_port_clear_brport_flags(dp);
231 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
232 * so allow it to be in BR_STATE_FORWARDING to be kept functional
234 dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
236 /* VLAN filtering is handled by dsa_switch_bridge_leave */
238 /* Some drivers treat the notification for having a local multicast
239 * router by allowing multicast to be flooded to the CPU, so we should
240 * allow this in standalone mode too.
242 dsa_port_mrouter(dp->cpu_dp, true, NULL);
244 /* Ageing time may be global to the switch chip, so don't change it
245 * here because we have no good reason (or value) to change it to.
249 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
250 struct netlink_ext_ack *extack)
252 struct dsa_notifier_bridge_info info = {
253 .tree_index = dp->ds->dst->index,
254 .sw_index = dp->ds->index,
260 /* Here the interface is already bridged. Reflect the current
261 * configuration so that drivers can program their chips accordingly.
265 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
269 err = dsa_port_switchdev_sync(dp, extack);
271 goto out_rollback_unbridge;
275 out_rollback_unbridge:
276 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
278 dp->bridge_dev = NULL;
282 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
284 struct dsa_notifier_bridge_info info = {
285 .tree_index = dp->ds->dst->index,
286 .sw_index = dp->ds->index,
292 /* Here the port is already unbridged. Reflect the current configuration
293 * so that drivers can program their chips accordingly.
295 dp->bridge_dev = NULL;
297 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
299 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
301 dsa_port_switchdev_unsync(dp);
304 int dsa_port_lag_change(struct dsa_port *dp,
305 struct netdev_lag_lower_state_info *linfo)
307 struct dsa_notifier_lag_info info = {
308 .sw_index = dp->ds->index,
316 /* On statically configured aggregates (e.g. loadbalance
317 * without LACP) ports will always be tx_enabled, even if the
318 * link is down. Thus we require both link_up and tx_enabled
319 * in order to include it in the tx set.
321 tx_enabled = linfo->link_up && linfo->tx_enabled;
323 if (tx_enabled == dp->lag_tx_enabled)
326 dp->lag_tx_enabled = tx_enabled;
328 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
331 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
332 struct netdev_lag_upper_info *uinfo,
333 struct netlink_ext_ack *extack)
335 struct dsa_notifier_lag_info info = {
336 .sw_index = dp->ds->index,
341 struct net_device *bridge_dev;
344 dsa_lag_map(dp->ds->dst, lag);
347 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
351 bridge_dev = netdev_master_upper_dev_get(lag);
352 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
355 err = dsa_port_bridge_join(dp, bridge_dev, extack);
357 goto err_bridge_join;
362 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
365 dsa_lag_unmap(dp->ds->dst, lag);
369 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
371 struct dsa_notifier_lag_info info = {
372 .sw_index = dp->ds->index,
381 /* Port might have been part of a LAG that in turn was
382 * attached to a bridge.
385 dsa_port_bridge_leave(dp, dp->bridge_dev);
387 dp->lag_tx_enabled = false;
390 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
392 pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n",
395 dsa_lag_unmap(dp->ds->dst, lag);
398 /* Must be called under rcu_read_lock() */
399 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
401 struct netlink_ext_ack *extack)
403 struct dsa_switch *ds = dp->ds;
406 /* VLAN awareness was off, so the question is "can we turn it on".
407 * We may have had 8021q uppers, those need to go. Make sure we don't
408 * enter an inconsistent state: deny changing the VLAN awareness state
409 * as long as we have 8021q uppers.
411 if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
412 struct net_device *upper_dev, *slave = dp->slave;
413 struct net_device *br = dp->bridge_dev;
414 struct list_head *iter;
416 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
417 struct bridge_vlan_info br_info;
420 if (!is_vlan_dev(upper_dev))
423 vid = vlan_dev_vlan_id(upper_dev);
425 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
426 * device, respectively the VID is not found, returning
427 * 0 means success, which is a failure for us here.
429 err = br_vlan_get_info(br, vid, &br_info);
431 NL_SET_ERR_MSG_MOD(extack,
432 "Must first remove VLAN uppers having VIDs also present in bridge");
438 if (!ds->vlan_filtering_is_global)
441 /* For cases where enabling/disabling VLAN awareness is global to the
442 * switch, we need to handle the case where multiple bridges span
443 * different ports of the same switch device and one of them has a
444 * different setting than what is being requested.
446 for (i = 0; i < ds->num_ports; i++) {
447 struct net_device *other_bridge;
449 other_bridge = dsa_to_port(ds, i)->bridge_dev;
452 /* If it's the same bridge, it also has same
453 * vlan_filtering setting => no need to check
455 if (other_bridge == dp->bridge_dev)
457 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
458 NL_SET_ERR_MSG_MOD(extack,
459 "VLAN filtering is a global setting");
466 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
467 struct netlink_ext_ack *extack)
469 struct dsa_switch *ds = dp->ds;
473 if (!ds->ops->port_vlan_filtering)
476 /* We are called from dsa_slave_switchdev_blocking_event(),
477 * which is not under rcu_read_lock(), unlike
478 * dsa_slave_switchdev_event().
481 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
486 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
489 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
494 if (ds->vlan_filtering_is_global)
495 ds->vlan_filtering = vlan_filtering;
497 dp->vlan_filtering = vlan_filtering;
502 /* This enforces legacy behavior for switch drivers which assume they can't
503 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
505 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
507 struct dsa_switch *ds = dp->ds;
512 return (!ds->configure_vlan_while_not_filtering &&
513 !br_vlan_enabled(dp->bridge_dev));
516 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
518 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
519 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
520 struct dsa_notifier_ageing_time_info info;
523 info.ageing_time = ageing_time;
525 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
529 dp->ageing_time = ageing_time;
534 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
535 struct switchdev_brport_flags flags,
536 struct netlink_ext_ack *extack)
538 struct dsa_switch *ds = dp->ds;
540 if (!ds->ops->port_pre_bridge_flags)
543 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
546 int dsa_port_bridge_flags(const struct dsa_port *dp,
547 struct switchdev_brport_flags flags,
548 struct netlink_ext_ack *extack)
550 struct dsa_switch *ds = dp->ds;
552 if (!ds->ops->port_bridge_flags)
555 return ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
558 int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
559 struct netlink_ext_ack *extack)
561 struct dsa_switch *ds = dp->ds;
563 if (!ds->ops->port_set_mrouter)
566 return ds->ops->port_set_mrouter(ds, dp->index, mrouter, extack);
569 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
570 bool propagate_upstream)
572 struct dsa_notifier_mtu_info info = {
573 .sw_index = dp->ds->index,
574 .propagate_upstream = propagate_upstream,
579 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
582 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
585 struct dsa_notifier_fdb_info info = {
586 .sw_index = dp->ds->index,
592 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
595 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
598 struct dsa_notifier_fdb_info info = {
599 .sw_index = dp->ds->index,
606 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
609 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
611 struct dsa_switch *ds = dp->ds;
612 int port = dp->index;
614 if (!ds->ops->port_fdb_dump)
617 return ds->ops->port_fdb_dump(ds, port, cb, data);
620 int dsa_port_mdb_add(const struct dsa_port *dp,
621 const struct switchdev_obj_port_mdb *mdb)
623 struct dsa_notifier_mdb_info info = {
624 .sw_index = dp->ds->index,
629 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
632 int dsa_port_mdb_del(const struct dsa_port *dp,
633 const struct switchdev_obj_port_mdb *mdb)
635 struct dsa_notifier_mdb_info info = {
636 .sw_index = dp->ds->index,
641 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
644 int dsa_port_vlan_add(struct dsa_port *dp,
645 const struct switchdev_obj_port_vlan *vlan,
646 struct netlink_ext_ack *extack)
648 struct dsa_notifier_vlan_info info = {
649 .sw_index = dp->ds->index,
655 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
658 int dsa_port_vlan_del(struct dsa_port *dp,
659 const struct switchdev_obj_port_vlan *vlan)
661 struct dsa_notifier_vlan_info info = {
662 .sw_index = dp->ds->index,
667 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
670 int dsa_port_mrp_add(const struct dsa_port *dp,
671 const struct switchdev_obj_mrp *mrp)
673 struct dsa_notifier_mrp_info info = {
674 .sw_index = dp->ds->index,
679 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info);
682 int dsa_port_mrp_del(const struct dsa_port *dp,
683 const struct switchdev_obj_mrp *mrp)
685 struct dsa_notifier_mrp_info info = {
686 .sw_index = dp->ds->index,
691 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info);
694 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
695 const struct switchdev_obj_ring_role_mrp *mrp)
697 struct dsa_notifier_mrp_ring_role_info info = {
698 .sw_index = dp->ds->index,
703 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info);
706 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
707 const struct switchdev_obj_ring_role_mrp *mrp)
709 struct dsa_notifier_mrp_ring_role_info info = {
710 .sw_index = dp->ds->index,
715 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info);
718 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
719 const struct dsa_device_ops *tag_ops)
721 cpu_dp->filter = tag_ops->filter;
722 cpu_dp->rcv = tag_ops->rcv;
723 cpu_dp->tag_ops = tag_ops;
726 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
728 struct device_node *phy_dn;
729 struct phy_device *phydev;
731 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
735 phydev = of_phy_find_device(phy_dn);
738 return ERR_PTR(-EPROBE_DEFER);
745 static void dsa_port_phylink_validate(struct phylink_config *config,
746 unsigned long *supported,
747 struct phylink_link_state *state)
749 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
750 struct dsa_switch *ds = dp->ds;
752 if (!ds->ops->phylink_validate)
755 ds->ops->phylink_validate(ds, dp->index, supported, state);
758 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
759 struct phylink_link_state *state)
761 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
762 struct dsa_switch *ds = dp->ds;
765 /* Only called for inband modes */
766 if (!ds->ops->phylink_mac_link_state) {
771 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
773 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
779 static void dsa_port_phylink_mac_config(struct phylink_config *config,
781 const struct phylink_link_state *state)
783 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
784 struct dsa_switch *ds = dp->ds;
786 if (!ds->ops->phylink_mac_config)
789 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
792 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
794 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
795 struct dsa_switch *ds = dp->ds;
797 if (!ds->ops->phylink_mac_an_restart)
800 ds->ops->phylink_mac_an_restart(ds, dp->index);
803 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
805 phy_interface_t interface)
807 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
808 struct phy_device *phydev = NULL;
809 struct dsa_switch *ds = dp->ds;
811 if (dsa_is_user_port(ds, dp->index))
812 phydev = dp->slave->phydev;
814 if (!ds->ops->phylink_mac_link_down) {
815 if (ds->ops->adjust_link && phydev)
816 ds->ops->adjust_link(ds, dp->index, phydev);
820 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
823 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
824 struct phy_device *phydev,
826 phy_interface_t interface,
827 int speed, int duplex,
828 bool tx_pause, bool rx_pause)
830 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
831 struct dsa_switch *ds = dp->ds;
833 if (!ds->ops->phylink_mac_link_up) {
834 if (ds->ops->adjust_link && phydev)
835 ds->ops->adjust_link(ds, dp->index, phydev);
839 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
840 speed, duplex, tx_pause, rx_pause);
843 const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
844 .validate = dsa_port_phylink_validate,
845 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
846 .mac_config = dsa_port_phylink_mac_config,
847 .mac_an_restart = dsa_port_phylink_mac_an_restart,
848 .mac_link_down = dsa_port_phylink_mac_link_down,
849 .mac_link_up = dsa_port_phylink_mac_link_up,
852 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
854 struct dsa_switch *ds = dp->ds;
855 struct phy_device *phydev;
856 int port = dp->index;
859 phydev = dsa_port_get_phy_device(dp);
864 return PTR_ERR(phydev);
867 err = genphy_resume(phydev);
871 err = genphy_read_status(phydev);
875 err = genphy_suspend(phydev);
880 if (ds->ops->adjust_link)
881 ds->ops->adjust_link(ds, port, phydev);
883 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
886 put_device(&phydev->mdio.dev);
890 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
892 struct device_node *dn = dp->dn;
893 struct dsa_switch *ds = dp->ds;
894 struct phy_device *phydev;
895 int port = dp->index;
896 phy_interface_t mode;
899 err = of_phy_register_fixed_link(dn);
902 "failed to register the fixed PHY of port %d\n",
907 phydev = of_phy_find_device(dn);
909 err = of_get_phy_mode(dn, &mode);
911 mode = PHY_INTERFACE_MODE_NA;
912 phydev->interface = mode;
914 genphy_read_status(phydev);
916 if (ds->ops->adjust_link)
917 ds->ops->adjust_link(ds, port, phydev);
919 put_device(&phydev->mdio.dev);
924 static int dsa_port_phylink_register(struct dsa_port *dp)
926 struct dsa_switch *ds = dp->ds;
927 struct device_node *port_dn = dp->dn;
928 phy_interface_t mode;
931 err = of_get_phy_mode(port_dn, &mode);
933 mode = PHY_INTERFACE_MODE_NA;
935 dp->pl_config.dev = ds->dev;
936 dp->pl_config.type = PHYLINK_DEV;
937 dp->pl_config.pcs_poll = ds->pcs_poll;
939 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
940 mode, &dsa_port_phylink_mac_ops);
941 if (IS_ERR(dp->pl)) {
942 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
943 return PTR_ERR(dp->pl);
946 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
947 if (err && err != -ENODEV) {
948 pr_err("could not attach to PHY: %d\n", err);
949 goto err_phy_connect;
955 phylink_destroy(dp->pl);
959 int dsa_port_link_register_of(struct dsa_port *dp)
961 struct dsa_switch *ds = dp->ds;
962 struct device_node *phy_np;
963 int port = dp->index;
965 if (!ds->ops->adjust_link) {
966 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
967 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
968 if (ds->ops->phylink_mac_link_down)
969 ds->ops->phylink_mac_link_down(ds, port,
970 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
971 return dsa_port_phylink_register(dp);
977 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
979 if (of_phy_is_fixed_link(dp->dn))
980 return dsa_port_fixed_link_register_of(dp);
982 return dsa_port_setup_phy_of(dp, true);
985 void dsa_port_link_unregister_of(struct dsa_port *dp)
987 struct dsa_switch *ds = dp->ds;
989 if (!ds->ops->adjust_link && dp->pl) {
991 phylink_disconnect_phy(dp->pl);
993 phylink_destroy(dp->pl);
998 if (of_phy_is_fixed_link(dp->dn))
999 of_phy_deregister_fixed_link(dp->dn);
1001 dsa_port_setup_phy_of(dp, false);
1004 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
1006 struct phy_device *phydev;
1007 int ret = -EOPNOTSUPP;
1009 if (of_phy_is_fixed_link(dp->dn))
1012 phydev = dsa_port_get_phy_device(dp);
1013 if (IS_ERR_OR_NULL(phydev))
1016 ret = phy_ethtool_get_strings(phydev, data);
1017 put_device(&phydev->mdio.dev);
1021 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
1023 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
1025 struct phy_device *phydev;
1026 int ret = -EOPNOTSUPP;
1028 if (of_phy_is_fixed_link(dp->dn))
1031 phydev = dsa_port_get_phy_device(dp);
1032 if (IS_ERR_OR_NULL(phydev))
1035 ret = phy_ethtool_get_stats(phydev, NULL, data);
1036 put_device(&phydev->mdio.dev);
1040 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
1042 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
1044 struct phy_device *phydev;
1045 int ret = -EOPNOTSUPP;
1047 if (of_phy_is_fixed_link(dp->dn))
1050 phydev = dsa_port_get_phy_device(dp);
1051 if (IS_ERR_OR_NULL(phydev))
1054 ret = phy_ethtool_get_sset_count(phydev);
1055 put_device(&phydev->mdio.dev);
1059 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
1061 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1063 struct dsa_notifier_hsr_info info = {
1064 .sw_index = dp->ds->index,
1072 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
1079 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1081 struct dsa_notifier_hsr_info info = {
1082 .sw_index = dp->ds->index,
1090 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
1092 pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n");