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>
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 static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp)
35 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
36 struct switchdev_notifier_fdb_info info = {
41 /* When the port becomes standalone it has already left the bridge.
42 * Don't notify the bridge in that case.
47 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
48 brport_dev, &info.info, NULL);
51 static void dsa_port_fast_age(const struct dsa_port *dp)
53 struct dsa_switch *ds = dp->ds;
55 if (!ds->ops->port_fast_age)
58 ds->ops->port_fast_age(ds, dp->index);
60 dsa_port_notify_bridge_fdb_flush(dp);
63 static bool dsa_port_can_configure_learning(struct dsa_port *dp)
65 struct switchdev_brport_flags flags = {
68 struct dsa_switch *ds = dp->ds;
71 if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags)
74 err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL);
78 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
80 struct dsa_switch *ds = dp->ds;
83 if (!ds->ops->port_stp_state_set)
86 ds->ops->port_stp_state_set(ds, port, state);
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
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);
106 dp->stp_state = state;
111 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
114 struct dsa_switch *ds = dp->ds;
117 err = dsa_port_set_state(dp, state, do_fast_age);
118 if (err && err != -EOPNOTSUPP) {
119 dev_err(ds->dev, "port %d failed to set STP state %u: %pe\n",
120 dp->index, state, ERR_PTR(err));
124 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
126 struct dsa_switch *ds = dp->ds;
127 int port = dp->index;
130 if (ds->ops->port_enable) {
131 err = ds->ops->port_enable(ds, port, phy);
137 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
140 phylink_start(dp->pl);
145 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
150 err = dsa_port_enable_rt(dp, phy);
156 void dsa_port_disable_rt(struct dsa_port *dp)
158 struct dsa_switch *ds = dp->ds;
159 int port = dp->index;
162 phylink_stop(dp->pl);
165 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
167 if (ds->ops->port_disable)
168 ds->ops->port_disable(ds, port);
171 void dsa_port_disable(struct dsa_port *dp)
174 dsa_port_disable_rt(dp);
178 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
179 struct netlink_ext_ack *extack)
181 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
183 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
186 for_each_set_bit(flag, &mask, 32) {
187 struct switchdev_brport_flags flags = {0};
189 flags.mask = BIT(flag);
191 if (br_port_flag_is_set(brport_dev, BIT(flag)))
192 flags.val = BIT(flag);
194 err = dsa_port_bridge_flags(dp, flags, extack);
195 if (err && err != -EOPNOTSUPP)
202 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
204 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
205 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
209 for_each_set_bit(flag, &mask, 32) {
210 struct switchdev_brport_flags flags = {0};
212 flags.mask = BIT(flag);
213 flags.val = val & BIT(flag);
215 err = dsa_port_bridge_flags(dp, flags, NULL);
216 if (err && err != -EOPNOTSUPP)
218 "failed to clear bridge port flag %lu: %pe\n",
219 flags.val, ERR_PTR(err));
223 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
224 struct netlink_ext_ack *extack)
226 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
227 struct net_device *br = dp->bridge_dev;
230 err = dsa_port_inherit_brport_flags(dp, extack);
234 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
235 if (err && err != -EOPNOTSUPP)
238 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
239 if (err && err != -EOPNOTSUPP)
242 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
243 if (err && err != -EOPNOTSUPP)
249 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp)
251 /* Configure the port for standalone mode (no address learning,
253 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
254 * when the user requests it through netlink or sysfs, but not
255 * automatically at port join or leave, so we need to handle resetting
256 * the brport flags ourselves. But we even prefer it that way, because
257 * otherwise, some setups might never get the notification they need,
258 * for example, when a port leaves a LAG that offloads the bridge,
259 * it becomes standalone, but as far as the bridge is concerned, no
262 dsa_port_clear_brport_flags(dp);
264 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
265 * so allow it to be in BR_STATE_FORWARDING to be kept functional
267 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
269 /* VLAN filtering is handled by dsa_switch_bridge_leave */
271 /* Ageing time may be global to the switch chip, so don't change it
272 * here because we have no good reason (or value) to change it to.
276 static void dsa_port_bridge_tx_fwd_unoffload(struct dsa_port *dp,
277 struct net_device *bridge_dev)
279 int bridge_num = dp->bridge_num;
280 struct dsa_switch *ds = dp->ds;
282 /* No bridge TX forwarding offload => do nothing */
283 if (!ds->ops->port_bridge_tx_fwd_unoffload || dp->bridge_num == -1)
288 dsa_bridge_num_put(bridge_dev, bridge_num);
290 /* Notify the chips only once the offload has been deactivated, so
291 * that they can update their configuration accordingly.
293 ds->ops->port_bridge_tx_fwd_unoffload(ds, dp->index, bridge_dev,
297 static bool dsa_port_bridge_tx_fwd_offload(struct dsa_port *dp,
298 struct net_device *bridge_dev)
300 struct dsa_switch *ds = dp->ds;
303 if (!ds->ops->port_bridge_tx_fwd_offload)
306 bridge_num = dsa_bridge_num_get(bridge_dev,
307 ds->num_fwd_offloading_bridges);
311 dp->bridge_num = bridge_num;
313 /* Notify the driver */
314 err = ds->ops->port_bridge_tx_fwd_offload(ds, dp->index, bridge_dev,
317 dsa_port_bridge_tx_fwd_unoffload(dp, bridge_dev);
324 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
325 struct netlink_ext_ack *extack)
327 struct dsa_notifier_bridge_info info = {
328 .tree_index = dp->ds->dst->index,
329 .sw_index = dp->ds->index,
333 struct net_device *dev = dp->slave;
334 struct net_device *brport_dev;
338 /* Here the interface is already bridged. Reflect the current
339 * configuration so that drivers can program their chips accordingly.
343 brport_dev = dsa_port_to_bridge_port(dp);
345 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
349 tx_fwd_offload = dsa_port_bridge_tx_fwd_offload(dp, br);
351 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
352 &dsa_slave_switchdev_notifier,
353 &dsa_slave_switchdev_blocking_notifier,
354 tx_fwd_offload, extack);
356 goto out_rollback_unbridge;
358 err = dsa_port_switchdev_sync_attrs(dp, extack);
360 goto out_rollback_unoffload;
364 out_rollback_unoffload:
365 switchdev_bridge_port_unoffload(brport_dev, dp,
366 &dsa_slave_switchdev_notifier,
367 &dsa_slave_switchdev_blocking_notifier);
368 out_rollback_unbridge:
369 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
371 dp->bridge_dev = NULL;
375 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
377 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
379 /* Don't try to unoffload something that is not offloaded */
383 switchdev_bridge_port_unoffload(brport_dev, dp,
384 &dsa_slave_switchdev_notifier,
385 &dsa_slave_switchdev_blocking_notifier);
388 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
390 struct dsa_notifier_bridge_info info = {
391 .tree_index = dp->ds->dst->index,
392 .sw_index = dp->ds->index,
398 /* Here the port is already unbridged. Reflect the current configuration
399 * so that drivers can program their chips accordingly.
401 dp->bridge_dev = NULL;
403 dsa_port_bridge_tx_fwd_unoffload(dp, br);
405 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
408 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
409 dp->index, ERR_PTR(err));
411 dsa_port_switchdev_unsync_attrs(dp);
414 int dsa_port_lag_change(struct dsa_port *dp,
415 struct netdev_lag_lower_state_info *linfo)
417 struct dsa_notifier_lag_info info = {
418 .sw_index = dp->ds->index,
426 /* On statically configured aggregates (e.g. loadbalance
427 * without LACP) ports will always be tx_enabled, even if the
428 * link is down. Thus we require both link_up and tx_enabled
429 * in order to include it in the tx set.
431 tx_enabled = linfo->link_up && linfo->tx_enabled;
433 if (tx_enabled == dp->lag_tx_enabled)
436 dp->lag_tx_enabled = tx_enabled;
438 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
441 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
442 struct netdev_lag_upper_info *uinfo,
443 struct netlink_ext_ack *extack)
445 struct dsa_notifier_lag_info info = {
446 .sw_index = dp->ds->index,
451 struct net_device *bridge_dev;
454 dsa_lag_map(dp->ds->dst, lag);
457 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
461 bridge_dev = netdev_master_upper_dev_get(lag);
462 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
465 err = dsa_port_bridge_join(dp, bridge_dev, extack);
467 goto err_bridge_join;
472 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
475 dsa_lag_unmap(dp->ds->dst, lag);
479 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag)
482 dsa_port_pre_bridge_leave(dp, dp->bridge_dev);
485 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
487 struct dsa_notifier_lag_info info = {
488 .sw_index = dp->ds->index,
497 /* Port might have been part of a LAG that in turn was
498 * attached to a bridge.
501 dsa_port_bridge_leave(dp, dp->bridge_dev);
503 dp->lag_tx_enabled = false;
506 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
509 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
510 dp->index, ERR_PTR(err));
512 dsa_lag_unmap(dp->ds->dst, lag);
515 /* Must be called under rcu_read_lock() */
516 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
518 struct netlink_ext_ack *extack)
520 struct dsa_switch *ds = dp->ds;
523 /* VLAN awareness was off, so the question is "can we turn it on".
524 * We may have had 8021q uppers, those need to go. Make sure we don't
525 * enter an inconsistent state: deny changing the VLAN awareness state
526 * as long as we have 8021q uppers.
528 if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
529 struct net_device *upper_dev, *slave = dp->slave;
530 struct net_device *br = dp->bridge_dev;
531 struct list_head *iter;
533 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
534 struct bridge_vlan_info br_info;
537 if (!is_vlan_dev(upper_dev))
540 vid = vlan_dev_vlan_id(upper_dev);
542 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
543 * device, respectively the VID is not found, returning
544 * 0 means success, which is a failure for us here.
546 err = br_vlan_get_info(br, vid, &br_info);
548 NL_SET_ERR_MSG_MOD(extack,
549 "Must first remove VLAN uppers having VIDs also present in bridge");
555 if (!ds->vlan_filtering_is_global)
558 /* For cases where enabling/disabling VLAN awareness is global to the
559 * switch, we need to handle the case where multiple bridges span
560 * different ports of the same switch device and one of them has a
561 * different setting than what is being requested.
563 for (i = 0; i < ds->num_ports; i++) {
564 struct net_device *other_bridge;
566 other_bridge = dsa_to_port(ds, i)->bridge_dev;
569 /* If it's the same bridge, it also has same
570 * vlan_filtering setting => no need to check
572 if (other_bridge == dp->bridge_dev)
574 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
575 NL_SET_ERR_MSG_MOD(extack,
576 "VLAN filtering is a global setting");
583 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
584 struct netlink_ext_ack *extack)
586 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
587 struct dsa_switch *ds = dp->ds;
591 if (!ds->ops->port_vlan_filtering)
594 /* We are called from dsa_slave_switchdev_blocking_event(),
595 * which is not under rcu_read_lock(), unlike
596 * dsa_slave_switchdev_event().
599 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
604 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
607 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
612 if (ds->vlan_filtering_is_global) {
615 ds->vlan_filtering = vlan_filtering;
617 for (port = 0; port < ds->num_ports; port++) {
618 struct net_device *slave;
620 if (!dsa_is_user_port(ds, port))
623 /* We might be called in the unbind path, so not
624 * all slave devices might still be registered.
626 slave = dsa_to_port(ds, port)->slave;
630 err = dsa_slave_manage_vlan_filtering(slave,
636 dp->vlan_filtering = vlan_filtering;
638 err = dsa_slave_manage_vlan_filtering(dp->slave,
647 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
649 if (ds->vlan_filtering_is_global)
650 ds->vlan_filtering = old_vlan_filtering;
652 dp->vlan_filtering = old_vlan_filtering;
657 /* This enforces legacy behavior for switch drivers which assume they can't
658 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
660 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
662 struct dsa_switch *ds = dp->ds;
667 return (!ds->configure_vlan_while_not_filtering &&
668 !br_vlan_enabled(dp->bridge_dev));
671 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
673 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
674 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
675 struct dsa_notifier_ageing_time_info info;
678 info.ageing_time = ageing_time;
680 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
684 dp->ageing_time = ageing_time;
689 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
690 struct switchdev_brport_flags flags,
691 struct netlink_ext_ack *extack)
693 struct dsa_switch *ds = dp->ds;
695 if (!ds->ops->port_pre_bridge_flags)
698 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
701 int dsa_port_bridge_flags(struct dsa_port *dp,
702 struct switchdev_brport_flags flags,
703 struct netlink_ext_ack *extack)
705 struct dsa_switch *ds = dp->ds;
708 if (!ds->ops->port_bridge_flags)
711 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
715 if (flags.mask & BR_LEARNING) {
716 bool learning = flags.val & BR_LEARNING;
718 if (learning == dp->learning)
721 if ((dp->learning && !learning) &&
722 (dp->stp_state == BR_STATE_LEARNING ||
723 dp->stp_state == BR_STATE_FORWARDING))
724 dsa_port_fast_age(dp);
726 dp->learning = learning;
732 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
735 struct dsa_notifier_mtu_info info = {
736 .sw_index = dp->ds->index,
737 .targeted_match = targeted_match,
742 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
745 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
748 struct dsa_notifier_fdb_info info = {
749 .sw_index = dp->ds->index,
755 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
758 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
761 struct dsa_notifier_fdb_info info = {
762 .sw_index = dp->ds->index,
769 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
772 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
775 struct dsa_notifier_fdb_info info = {
776 .sw_index = dp->ds->index,
781 struct dsa_port *cpu_dp = dp->cpu_dp;
784 err = dev_uc_add(cpu_dp->master, addr);
788 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
791 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
794 struct dsa_notifier_fdb_info info = {
795 .sw_index = dp->ds->index,
800 struct dsa_port *cpu_dp = dp->cpu_dp;
803 err = dev_uc_del(cpu_dp->master, addr);
807 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
810 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
812 struct dsa_switch *ds = dp->ds;
813 int port = dp->index;
815 if (!ds->ops->port_fdb_dump)
818 return ds->ops->port_fdb_dump(ds, port, cb, data);
821 int dsa_port_mdb_add(const struct dsa_port *dp,
822 const struct switchdev_obj_port_mdb *mdb)
824 struct dsa_notifier_mdb_info info = {
825 .sw_index = dp->ds->index,
830 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
833 int dsa_port_mdb_del(const struct dsa_port *dp,
834 const struct switchdev_obj_port_mdb *mdb)
836 struct dsa_notifier_mdb_info info = {
837 .sw_index = dp->ds->index,
842 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
845 int dsa_port_host_mdb_add(const struct dsa_port *dp,
846 const struct switchdev_obj_port_mdb *mdb)
848 struct dsa_notifier_mdb_info info = {
849 .sw_index = dp->ds->index,
853 struct dsa_port *cpu_dp = dp->cpu_dp;
856 err = dev_mc_add(cpu_dp->master, mdb->addr);
860 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
863 int dsa_port_host_mdb_del(const struct dsa_port *dp,
864 const struct switchdev_obj_port_mdb *mdb)
866 struct dsa_notifier_mdb_info info = {
867 .sw_index = dp->ds->index,
871 struct dsa_port *cpu_dp = dp->cpu_dp;
874 err = dev_mc_del(cpu_dp->master, mdb->addr);
878 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
881 int dsa_port_vlan_add(struct dsa_port *dp,
882 const struct switchdev_obj_port_vlan *vlan,
883 struct netlink_ext_ack *extack)
885 struct dsa_notifier_vlan_info info = {
886 .sw_index = dp->ds->index,
892 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
895 int dsa_port_vlan_del(struct dsa_port *dp,
896 const struct switchdev_obj_port_vlan *vlan)
898 struct dsa_notifier_vlan_info info = {
899 .sw_index = dp->ds->index,
904 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
907 int dsa_port_mrp_add(const struct dsa_port *dp,
908 const struct switchdev_obj_mrp *mrp)
910 struct dsa_notifier_mrp_info info = {
911 .sw_index = dp->ds->index,
916 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info);
919 int dsa_port_mrp_del(const struct dsa_port *dp,
920 const struct switchdev_obj_mrp *mrp)
922 struct dsa_notifier_mrp_info info = {
923 .sw_index = dp->ds->index,
928 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info);
931 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
932 const struct switchdev_obj_ring_role_mrp *mrp)
934 struct dsa_notifier_mrp_ring_role_info info = {
935 .sw_index = dp->ds->index,
940 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info);
943 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
944 const struct switchdev_obj_ring_role_mrp *mrp)
946 struct dsa_notifier_mrp_ring_role_info info = {
947 .sw_index = dp->ds->index,
952 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info);
955 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
956 const struct dsa_device_ops *tag_ops)
958 cpu_dp->rcv = tag_ops->rcv;
959 cpu_dp->tag_ops = tag_ops;
962 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
964 struct device_node *phy_dn;
965 struct phy_device *phydev;
967 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
971 phydev = of_phy_find_device(phy_dn);
974 return ERR_PTR(-EPROBE_DEFER);
981 static void dsa_port_phylink_validate(struct phylink_config *config,
982 unsigned long *supported,
983 struct phylink_link_state *state)
985 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
986 struct dsa_switch *ds = dp->ds;
988 if (!ds->ops->phylink_validate)
991 ds->ops->phylink_validate(ds, dp->index, supported, state);
994 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
995 struct phylink_link_state *state)
997 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
998 struct dsa_switch *ds = dp->ds;
1001 /* Only called for inband modes */
1002 if (!ds->ops->phylink_mac_link_state) {
1007 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
1009 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
1015 static void dsa_port_phylink_mac_config(struct phylink_config *config,
1017 const struct phylink_link_state *state)
1019 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1020 struct dsa_switch *ds = dp->ds;
1022 if (!ds->ops->phylink_mac_config)
1025 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1028 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
1030 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1031 struct dsa_switch *ds = dp->ds;
1033 if (!ds->ops->phylink_mac_an_restart)
1036 ds->ops->phylink_mac_an_restart(ds, dp->index);
1039 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1041 phy_interface_t interface)
1043 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1044 struct phy_device *phydev = NULL;
1045 struct dsa_switch *ds = dp->ds;
1047 if (dsa_is_user_port(ds, dp->index))
1048 phydev = dp->slave->phydev;
1050 if (!ds->ops->phylink_mac_link_down) {
1051 if (ds->ops->adjust_link && phydev)
1052 ds->ops->adjust_link(ds, dp->index, phydev);
1056 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1059 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1060 struct phy_device *phydev,
1062 phy_interface_t interface,
1063 int speed, int duplex,
1064 bool tx_pause, bool rx_pause)
1066 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1067 struct dsa_switch *ds = dp->ds;
1069 if (!ds->ops->phylink_mac_link_up) {
1070 if (ds->ops->adjust_link && phydev)
1071 ds->ops->adjust_link(ds, dp->index, phydev);
1075 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1076 speed, duplex, tx_pause, rx_pause);
1079 const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1080 .validate = dsa_port_phylink_validate,
1081 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1082 .mac_config = dsa_port_phylink_mac_config,
1083 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1084 .mac_link_down = dsa_port_phylink_mac_link_down,
1085 .mac_link_up = dsa_port_phylink_mac_link_up,
1088 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1090 struct dsa_switch *ds = dp->ds;
1091 struct phy_device *phydev;
1092 int port = dp->index;
1095 phydev = dsa_port_get_phy_device(dp);
1100 return PTR_ERR(phydev);
1103 err = genphy_resume(phydev);
1107 err = genphy_read_status(phydev);
1111 err = genphy_suspend(phydev);
1116 if (ds->ops->adjust_link)
1117 ds->ops->adjust_link(ds, port, phydev);
1119 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1122 put_device(&phydev->mdio.dev);
1126 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
1128 struct device_node *dn = dp->dn;
1129 struct dsa_switch *ds = dp->ds;
1130 struct phy_device *phydev;
1131 int port = dp->index;
1132 phy_interface_t mode;
1135 err = of_phy_register_fixed_link(dn);
1138 "failed to register the fixed PHY of port %d\n",
1143 phydev = of_phy_find_device(dn);
1145 err = of_get_phy_mode(dn, &mode);
1147 mode = PHY_INTERFACE_MODE_NA;
1148 phydev->interface = mode;
1150 genphy_read_status(phydev);
1152 if (ds->ops->adjust_link)
1153 ds->ops->adjust_link(ds, port, phydev);
1155 put_device(&phydev->mdio.dev);
1160 static int dsa_port_phylink_register(struct dsa_port *dp)
1162 struct dsa_switch *ds = dp->ds;
1163 struct device_node *port_dn = dp->dn;
1164 phy_interface_t mode;
1167 err = of_get_phy_mode(port_dn, &mode);
1169 mode = PHY_INTERFACE_MODE_NA;
1171 dp->pl_config.dev = ds->dev;
1172 dp->pl_config.type = PHYLINK_DEV;
1173 dp->pl_config.pcs_poll = ds->pcs_poll;
1175 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
1176 mode, &dsa_port_phylink_mac_ops);
1177 if (IS_ERR(dp->pl)) {
1178 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1179 return PTR_ERR(dp->pl);
1182 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1183 if (err && err != -ENODEV) {
1184 pr_err("could not attach to PHY: %d\n", err);
1185 goto err_phy_connect;
1191 phylink_destroy(dp->pl);
1195 int dsa_port_link_register_of(struct dsa_port *dp)
1197 struct dsa_switch *ds = dp->ds;
1198 struct device_node *phy_np;
1199 int port = dp->index;
1201 if (!ds->ops->adjust_link) {
1202 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1203 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1204 if (ds->ops->phylink_mac_link_down)
1205 ds->ops->phylink_mac_link_down(ds, port,
1206 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1207 of_node_put(phy_np);
1208 return dsa_port_phylink_register(dp);
1210 of_node_put(phy_np);
1215 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1217 if (of_phy_is_fixed_link(dp->dn))
1218 return dsa_port_fixed_link_register_of(dp);
1220 return dsa_port_setup_phy_of(dp, true);
1223 void dsa_port_link_unregister_of(struct dsa_port *dp)
1225 struct dsa_switch *ds = dp->ds;
1227 if (!ds->ops->adjust_link && dp->pl) {
1229 phylink_disconnect_phy(dp->pl);
1231 phylink_destroy(dp->pl);
1236 if (of_phy_is_fixed_link(dp->dn))
1237 of_phy_deregister_fixed_link(dp->dn);
1239 dsa_port_setup_phy_of(dp, false);
1242 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
1244 struct phy_device *phydev;
1245 int ret = -EOPNOTSUPP;
1247 if (of_phy_is_fixed_link(dp->dn))
1250 phydev = dsa_port_get_phy_device(dp);
1251 if (IS_ERR_OR_NULL(phydev))
1254 ret = phy_ethtool_get_strings(phydev, data);
1255 put_device(&phydev->mdio.dev);
1259 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
1261 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
1263 struct phy_device *phydev;
1264 int ret = -EOPNOTSUPP;
1266 if (of_phy_is_fixed_link(dp->dn))
1269 phydev = dsa_port_get_phy_device(dp);
1270 if (IS_ERR_OR_NULL(phydev))
1273 ret = phy_ethtool_get_stats(phydev, NULL, data);
1274 put_device(&phydev->mdio.dev);
1278 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
1280 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
1282 struct phy_device *phydev;
1283 int ret = -EOPNOTSUPP;
1285 if (of_phy_is_fixed_link(dp->dn))
1288 phydev = dsa_port_get_phy_device(dp);
1289 if (IS_ERR_OR_NULL(phydev))
1292 ret = phy_ethtool_get_sset_count(phydev);
1293 put_device(&phydev->mdio.dev);
1297 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
1299 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1301 struct dsa_notifier_hsr_info info = {
1302 .sw_index = dp->ds->index,
1310 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
1317 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1319 struct dsa_notifier_hsr_info info = {
1320 .sw_index = dp->ds->index,
1328 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
1330 dev_err(dp->ds->dev,
1331 "port %d failed to notify DSA_NOTIFIER_HSR_LEAVE: %pe\n",
1332 dp->index, ERR_PTR(err));
1335 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
1337 struct dsa_notifier_tag_8021q_vlan_info info = {
1338 .tree_index = dp->ds->dst->index,
1339 .sw_index = dp->ds->index,
1345 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1347 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1350 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
1352 struct dsa_notifier_tag_8021q_vlan_info info = {
1353 .tree_index = dp->ds->dst->index,
1354 .sw_index = dp->ds->index,
1361 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1363 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1365 dev_err(dp->ds->dev,
1366 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1367 dp->index, vid, ERR_PTR(err));