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/netdevice.h>
11 #include <linux/notifier.h>
12 #include <linux/of_mdio.h>
13 #include <linux/of_net.h>
18 #include "tag_8021q.h"
22 * dsa_port_notify - Notify the switching fabric of changes to a port
23 * @dp: port on which change occurred
24 * @e: event, must be of type DSA_NOTIFIER_*
25 * @v: event-specific value.
27 * Notify all switches in the DSA tree that this port's switch belongs to,
28 * including this switch itself, of an event. Allows the other switches to
29 * reconfigure themselves for cross-chip operations. Can also be used to
30 * reconfigure ports without net_devices (CPU ports, DSA links) whenever
31 * a user port's state changes.
33 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
35 return dsa_tree_notify(dp->ds->dst, e, v);
38 static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp, u16 vid)
40 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
41 struct switchdev_notifier_fdb_info info = {
45 /* When the port becomes standalone it has already left the bridge.
46 * Don't notify the bridge in that case.
51 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
52 brport_dev, &info.info, NULL);
55 static void dsa_port_fast_age(const struct dsa_port *dp)
57 struct dsa_switch *ds = dp->ds;
59 if (!ds->ops->port_fast_age)
62 ds->ops->port_fast_age(ds, dp->index);
65 dsa_port_notify_bridge_fdb_flush(dp, 0);
68 static int dsa_port_vlan_fast_age(const struct dsa_port *dp, u16 vid)
70 struct dsa_switch *ds = dp->ds;
73 if (!ds->ops->port_vlan_fast_age)
76 err = ds->ops->port_vlan_fast_age(ds, dp->index, vid);
79 dsa_port_notify_bridge_fdb_flush(dp, vid);
84 static int dsa_port_msti_fast_age(const struct dsa_port *dp, u16 msti)
86 DECLARE_BITMAP(vids, VLAN_N_VID) = { 0 };
89 err = br_mst_get_info(dsa_port_bridge_dev_get(dp), msti, vids);
93 for_each_set_bit(vid, vids, VLAN_N_VID) {
94 err = dsa_port_vlan_fast_age(dp, vid);
102 static bool dsa_port_can_configure_learning(struct dsa_port *dp)
104 struct switchdev_brport_flags flags = {
107 struct dsa_switch *ds = dp->ds;
110 if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags)
113 err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL);
117 bool dsa_port_supports_hwtstamp(struct dsa_port *dp)
119 struct kernel_hwtstamp_config config = {};
120 struct dsa_switch *ds = dp->ds;
123 if (!ds->ops->port_hwtstamp_get || !ds->ops->port_hwtstamp_set)
126 /* "See through" shim implementations of the "get" method. */
127 err = ds->ops->port_hwtstamp_get(ds, dp->index, &config);
128 return err != -EOPNOTSUPP;
131 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
133 struct dsa_switch *ds = dp->ds;
134 int port = dp->index;
136 if (!ds->ops->port_stp_state_set)
139 ds->ops->port_stp_state_set(ds, port, state);
141 if (!dsa_port_can_configure_learning(dp) ||
142 (do_fast_age && dp->learning)) {
143 /* Fast age FDB entries or flush appropriate forwarding database
144 * for the given port, if we are moving it from Learning or
145 * Forwarding state, to Disabled or Blocking or Listening state.
146 * Ports that were standalone before the STP state change don't
147 * need to fast age the FDB, since address learning is off in
151 if ((dp->stp_state == BR_STATE_LEARNING ||
152 dp->stp_state == BR_STATE_FORWARDING) &&
153 (state == BR_STATE_DISABLED ||
154 state == BR_STATE_BLOCKING ||
155 state == BR_STATE_LISTENING))
156 dsa_port_fast_age(dp);
159 dp->stp_state = state;
164 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
167 struct dsa_switch *ds = dp->ds;
170 err = dsa_port_set_state(dp, state, do_fast_age);
171 if (err && err != -EOPNOTSUPP) {
172 dev_err(ds->dev, "port %d failed to set STP state %u: %pe\n",
173 dp->index, state, ERR_PTR(err));
177 int dsa_port_set_mst_state(struct dsa_port *dp,
178 const struct switchdev_mst_state *state,
179 struct netlink_ext_ack *extack)
181 struct dsa_switch *ds = dp->ds;
185 if (!ds->ops->port_mst_state_set)
188 err = br_mst_get_state(dsa_port_to_bridge_port(dp), state->msti,
193 err = ds->ops->port_mst_state_set(ds, dp->index, state);
197 if (!(dp->learning &&
198 (prev_state == BR_STATE_LEARNING ||
199 prev_state == BR_STATE_FORWARDING) &&
200 (state->state == BR_STATE_DISABLED ||
201 state->state == BR_STATE_BLOCKING ||
202 state->state == BR_STATE_LISTENING)))
205 err = dsa_port_msti_fast_age(dp, state->msti);
207 NL_SET_ERR_MSG_MOD(extack,
208 "Unable to flush associated VLANs");
213 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
215 struct dsa_switch *ds = dp->ds;
216 int port = dp->index;
219 if (ds->ops->port_enable) {
220 err = ds->ops->port_enable(ds, port, phy);
226 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
229 phylink_start(dp->pl);
234 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
239 err = dsa_port_enable_rt(dp, phy);
245 void dsa_port_disable_rt(struct dsa_port *dp)
247 struct dsa_switch *ds = dp->ds;
248 int port = dp->index;
251 phylink_stop(dp->pl);
254 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
256 if (ds->ops->port_disable)
257 ds->ops->port_disable(ds, port);
260 void dsa_port_disable(struct dsa_port *dp)
263 dsa_port_disable_rt(dp);
267 static void dsa_port_reset_vlan_filtering(struct dsa_port *dp,
268 struct dsa_bridge bridge)
270 struct netlink_ext_ack extack = {0};
271 bool change_vlan_filtering = false;
272 struct dsa_switch *ds = dp->ds;
273 struct dsa_port *other_dp;
277 if (ds->needs_standalone_vlan_filtering &&
278 !br_vlan_enabled(bridge.dev)) {
279 change_vlan_filtering = true;
280 vlan_filtering = true;
281 } else if (!ds->needs_standalone_vlan_filtering &&
282 br_vlan_enabled(bridge.dev)) {
283 change_vlan_filtering = true;
284 vlan_filtering = false;
287 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
288 * event for changing vlan_filtering setting upon user ports leaving
289 * it. That is a good thing, because that lets us handle it and also
290 * handle the case where the switch's vlan_filtering setting is global
291 * (not per port). When that happens, the correct moment to trigger the
292 * vlan_filtering callback is only when the last port leaves the last
295 if (change_vlan_filtering && ds->vlan_filtering_is_global) {
296 dsa_switch_for_each_port(other_dp, ds) {
297 struct net_device *br = dsa_port_bridge_dev_get(other_dp);
299 if (br && br_vlan_enabled(br)) {
300 change_vlan_filtering = false;
306 if (!change_vlan_filtering)
309 err = dsa_port_vlan_filtering(dp, vlan_filtering, &extack);
311 dev_err(ds->dev, "port %d: %s\n", dp->index,
314 if (err && err != -EOPNOTSUPP) {
316 "port %d failed to reset VLAN filtering to %d: %pe\n",
317 dp->index, vlan_filtering, ERR_PTR(err));
321 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
322 struct netlink_ext_ack *extack)
324 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
325 BR_BCAST_FLOOD | BR_PORT_LOCKED;
326 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
329 for_each_set_bit(flag, &mask, 32) {
330 struct switchdev_brport_flags flags = {0};
332 flags.mask = BIT(flag);
334 if (br_port_flag_is_set(brport_dev, BIT(flag)))
335 flags.val = BIT(flag);
337 err = dsa_port_bridge_flags(dp, flags, extack);
338 if (err && err != -EOPNOTSUPP)
345 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
347 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
348 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
349 BR_BCAST_FLOOD | BR_PORT_LOCKED;
352 for_each_set_bit(flag, &mask, 32) {
353 struct switchdev_brport_flags flags = {0};
355 flags.mask = BIT(flag);
356 flags.val = val & BIT(flag);
358 err = dsa_port_bridge_flags(dp, flags, NULL);
359 if (err && err != -EOPNOTSUPP)
361 "failed to clear bridge port flag %lu: %pe\n",
362 flags.val, ERR_PTR(err));
366 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
367 struct netlink_ext_ack *extack)
369 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
370 struct net_device *br = dsa_port_bridge_dev_get(dp);
373 err = dsa_port_inherit_brport_flags(dp, extack);
377 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
378 if (err && err != -EOPNOTSUPP)
381 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
382 if (err && err != -EOPNOTSUPP)
385 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
386 if (err && err != -EOPNOTSUPP)
392 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp,
393 struct dsa_bridge bridge)
395 /* Configure the port for standalone mode (no address learning,
397 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
398 * when the user requests it through netlink or sysfs, but not
399 * automatically at port join or leave, so we need to handle resetting
400 * the brport flags ourselves. But we even prefer it that way, because
401 * otherwise, some setups might never get the notification they need,
402 * for example, when a port leaves a LAG that offloads the bridge,
403 * it becomes standalone, but as far as the bridge is concerned, no
406 dsa_port_clear_brport_flags(dp);
408 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
409 * so allow it to be in BR_STATE_FORWARDING to be kept functional
411 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
413 dsa_port_reset_vlan_filtering(dp, bridge);
415 /* Ageing time may be global to the switch chip, so don't change it
416 * here because we have no good reason (or value) to change it to.
420 static int dsa_port_bridge_create(struct dsa_port *dp,
421 struct net_device *br,
422 struct netlink_ext_ack *extack)
424 struct dsa_switch *ds = dp->ds;
425 struct dsa_bridge *bridge;
427 bridge = dsa_tree_bridge_find(ds->dst, br);
429 refcount_inc(&bridge->refcount);
434 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
438 refcount_set(&bridge->refcount, 1);
442 bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges);
443 if (ds->max_num_bridges && !bridge->num) {
444 NL_SET_ERR_MSG_MOD(extack,
445 "Range of offloadable bridges exceeded");
455 static void dsa_port_bridge_destroy(struct dsa_port *dp,
456 const struct net_device *br)
458 struct dsa_bridge *bridge = dp->bridge;
462 if (!refcount_dec_and_test(&bridge->refcount))
466 dsa_bridge_num_put(br, bridge->num);
471 static bool dsa_port_supports_mst(struct dsa_port *dp)
473 struct dsa_switch *ds = dp->ds;
475 return ds->ops->vlan_msti_set &&
476 ds->ops->port_mst_state_set &&
477 ds->ops->port_vlan_fast_age &&
478 dsa_port_can_configure_learning(dp);
481 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
482 struct netlink_ext_ack *extack)
484 struct dsa_notifier_bridge_info info = {
488 struct net_device *dev = dp->user;
489 struct net_device *brport_dev;
492 if (br_mst_enabled(br) && !dsa_port_supports_mst(dp))
495 /* Here the interface is already bridged. Reflect the current
496 * configuration so that drivers can program their chips accordingly.
498 err = dsa_port_bridge_create(dp, br, extack);
502 brport_dev = dsa_port_to_bridge_port(dp);
504 info.bridge = *dp->bridge;
505 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
509 /* Drivers which support bridge TX forwarding should set this */
510 dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
512 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
513 &dsa_user_switchdev_notifier,
514 &dsa_user_switchdev_blocking_notifier,
515 dp->bridge->tx_fwd_offload, extack);
517 goto out_rollback_unbridge;
519 err = dsa_port_switchdev_sync_attrs(dp, extack);
521 goto out_rollback_unoffload;
525 out_rollback_unoffload:
526 switchdev_bridge_port_unoffload(brport_dev, dp,
527 &dsa_user_switchdev_notifier,
528 &dsa_user_switchdev_blocking_notifier);
529 dsa_flush_workqueue();
530 out_rollback_unbridge:
531 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
533 dsa_port_bridge_destroy(dp, br);
537 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
539 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
541 /* Don't try to unoffload something that is not offloaded */
545 switchdev_bridge_port_unoffload(brport_dev, dp,
546 &dsa_user_switchdev_notifier,
547 &dsa_user_switchdev_blocking_notifier);
549 dsa_flush_workqueue();
552 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
554 struct dsa_notifier_bridge_info info = {
559 /* If the port could not be offloaded to begin with, then
560 * there is nothing to do.
565 info.bridge = *dp->bridge;
567 /* Here the port is already unbridged. Reflect the current configuration
568 * so that drivers can program their chips accordingly.
570 dsa_port_bridge_destroy(dp, br);
572 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
575 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
576 dp->index, ERR_PTR(err));
578 dsa_port_switchdev_unsync_attrs(dp, info.bridge);
581 int dsa_port_lag_change(struct dsa_port *dp,
582 struct netdev_lag_lower_state_info *linfo)
584 struct dsa_notifier_lag_info info = {
592 /* On statically configured aggregates (e.g. loadbalance
593 * without LACP) ports will always be tx_enabled, even if the
594 * link is down. Thus we require both link_up and tx_enabled
595 * in order to include it in the tx set.
597 tx_enabled = linfo->link_up && linfo->tx_enabled;
599 if (tx_enabled == dp->lag_tx_enabled)
602 dp->lag_tx_enabled = tx_enabled;
604 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
607 static int dsa_port_lag_create(struct dsa_port *dp,
608 struct net_device *lag_dev)
610 struct dsa_switch *ds = dp->ds;
613 lag = dsa_tree_lag_find(ds->dst, lag_dev);
615 refcount_inc(&lag->refcount);
620 lag = kzalloc(sizeof(*lag), GFP_KERNEL);
624 refcount_set(&lag->refcount, 1);
625 mutex_init(&lag->fdb_lock);
626 INIT_LIST_HEAD(&lag->fdbs);
628 dsa_lag_map(ds->dst, lag);
634 static void dsa_port_lag_destroy(struct dsa_port *dp)
636 struct dsa_lag *lag = dp->lag;
639 dp->lag_tx_enabled = false;
641 if (!refcount_dec_and_test(&lag->refcount))
644 WARN_ON(!list_empty(&lag->fdbs));
645 dsa_lag_unmap(dp->ds->dst, lag);
649 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
650 struct netdev_lag_upper_info *uinfo,
651 struct netlink_ext_ack *extack)
653 struct dsa_notifier_lag_info info = {
658 struct net_device *bridge_dev;
661 err = dsa_port_lag_create(dp, lag_dev);
666 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
670 bridge_dev = netdev_master_upper_dev_get(lag_dev);
671 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
674 err = dsa_port_bridge_join(dp, bridge_dev, extack);
676 goto err_bridge_join;
681 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
683 dsa_port_lag_destroy(dp);
688 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
690 struct net_device *br = dsa_port_bridge_dev_get(dp);
693 dsa_port_pre_bridge_leave(dp, br);
696 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
698 struct net_device *br = dsa_port_bridge_dev_get(dp);
699 struct dsa_notifier_lag_info info = {
707 /* Port might have been part of a LAG that in turn was
708 * attached to a bridge.
711 dsa_port_bridge_leave(dp, br);
715 dsa_port_lag_destroy(dp);
717 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
720 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
721 dp->index, ERR_PTR(err));
724 /* Must be called under rcu_read_lock() */
725 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
727 struct netlink_ext_ack *extack)
729 struct dsa_switch *ds = dp->ds;
730 struct dsa_port *other_dp;
733 /* VLAN awareness was off, so the question is "can we turn it on".
734 * We may have had 8021q uppers, those need to go. Make sure we don't
735 * enter an inconsistent state: deny changing the VLAN awareness state
736 * as long as we have 8021q uppers.
738 if (vlan_filtering && dsa_port_is_user(dp)) {
739 struct net_device *br = dsa_port_bridge_dev_get(dp);
740 struct net_device *upper_dev, *user = dp->user;
741 struct list_head *iter;
743 netdev_for_each_upper_dev_rcu(user, upper_dev, iter) {
744 struct bridge_vlan_info br_info;
747 if (!is_vlan_dev(upper_dev))
750 vid = vlan_dev_vlan_id(upper_dev);
752 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
753 * device, respectively the VID is not found, returning
754 * 0 means success, which is a failure for us here.
756 err = br_vlan_get_info(br, vid, &br_info);
758 NL_SET_ERR_MSG_MOD(extack,
759 "Must first remove VLAN uppers having VIDs also present in bridge");
765 if (!ds->vlan_filtering_is_global)
768 /* For cases where enabling/disabling VLAN awareness is global to the
769 * switch, we need to handle the case where multiple bridges span
770 * different ports of the same switch device and one of them has a
771 * different setting than what is being requested.
773 dsa_switch_for_each_port(other_dp, ds) {
774 struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
776 /* If it's the same bridge, it also has same
777 * vlan_filtering setting => no need to check
779 if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
782 if (br_vlan_enabled(other_br) != vlan_filtering) {
783 NL_SET_ERR_MSG_MOD(extack,
784 "VLAN filtering is a global setting");
791 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
792 struct netlink_ext_ack *extack)
794 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
795 struct dsa_switch *ds = dp->ds;
799 if (!ds->ops->port_vlan_filtering)
802 /* We are called from dsa_user_switchdev_blocking_event(),
803 * which is not under rcu_read_lock(), unlike
804 * dsa_user_switchdev_event().
807 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
812 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
815 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
820 if (ds->vlan_filtering_is_global) {
821 struct dsa_port *other_dp;
823 ds->vlan_filtering = vlan_filtering;
825 dsa_switch_for_each_user_port(other_dp, ds) {
826 struct net_device *user = other_dp->user;
828 /* We might be called in the unbind path, so not
829 * all user devices might still be registered.
834 err = dsa_user_manage_vlan_filtering(user,
840 dp->vlan_filtering = vlan_filtering;
842 err = dsa_user_manage_vlan_filtering(dp->user,
851 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
853 if (ds->vlan_filtering_is_global)
854 ds->vlan_filtering = old_vlan_filtering;
856 dp->vlan_filtering = old_vlan_filtering;
861 /* This enforces legacy behavior for switch drivers which assume they can't
862 * receive VLAN configuration when joining a bridge with vlan_filtering=0
864 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
866 struct net_device *br = dsa_port_bridge_dev_get(dp);
867 struct dsa_switch *ds = dp->ds;
872 return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
875 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
877 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
878 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
879 struct dsa_notifier_ageing_time_info info;
882 info.ageing_time = ageing_time;
884 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
888 dp->ageing_time = ageing_time;
893 int dsa_port_mst_enable(struct dsa_port *dp, bool on,
894 struct netlink_ext_ack *extack)
896 if (on && !dsa_port_supports_mst(dp)) {
897 NL_SET_ERR_MSG_MOD(extack, "Hardware does not support MST");
904 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
905 struct switchdev_brport_flags flags,
906 struct netlink_ext_ack *extack)
908 struct dsa_switch *ds = dp->ds;
910 if (!ds->ops->port_pre_bridge_flags)
913 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
916 int dsa_port_bridge_flags(struct dsa_port *dp,
917 struct switchdev_brport_flags flags,
918 struct netlink_ext_ack *extack)
920 struct dsa_switch *ds = dp->ds;
923 if (!ds->ops->port_bridge_flags)
926 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
930 if (flags.mask & BR_LEARNING) {
931 bool learning = flags.val & BR_LEARNING;
933 if (learning == dp->learning)
936 if ((dp->learning && !learning) &&
937 (dp->stp_state == BR_STATE_LEARNING ||
938 dp->stp_state == BR_STATE_FORWARDING))
939 dsa_port_fast_age(dp);
941 dp->learning = learning;
947 void dsa_port_set_host_flood(struct dsa_port *dp, bool uc, bool mc)
949 struct dsa_switch *ds = dp->ds;
951 if (ds->ops->port_set_host_flood)
952 ds->ops->port_set_host_flood(ds, dp->index, uc, mc);
955 int dsa_port_vlan_msti(struct dsa_port *dp,
956 const struct switchdev_vlan_msti *msti)
958 struct dsa_switch *ds = dp->ds;
960 if (!ds->ops->vlan_msti_set)
963 return ds->ops->vlan_msti_set(ds, *dp->bridge, msti);
966 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu)
968 struct dsa_notifier_mtu_info info = {
973 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
976 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
979 struct dsa_notifier_fdb_info info = {
984 .type = DSA_DB_BRIDGE,
985 .bridge = *dp->bridge,
989 /* Refcounting takes bridge.num as a key, and should be global for all
990 * bridges in the absence of FDB isolation, and per bridge otherwise.
991 * Force the bridge.num to zero here in the absence of FDB isolation.
993 if (!dp->ds->fdb_isolation)
994 info.db.bridge.num = 0;
996 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
999 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
1002 struct dsa_notifier_fdb_info info = {
1007 .type = DSA_DB_BRIDGE,
1008 .bridge = *dp->bridge,
1012 if (!dp->ds->fdb_isolation)
1013 info.db.bridge.num = 0;
1015 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
1018 static int dsa_port_host_fdb_add(struct dsa_port *dp,
1019 const unsigned char *addr, u16 vid,
1022 struct dsa_notifier_fdb_info info = {
1029 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
1032 int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
1033 const unsigned char *addr, u16 vid)
1035 struct dsa_db db = {
1036 .type = DSA_DB_PORT,
1040 return dsa_port_host_fdb_add(dp, addr, vid, db);
1043 int dsa_port_bridge_host_fdb_add(struct dsa_port *dp,
1044 const unsigned char *addr, u16 vid)
1046 struct net_device *conduit = dsa_port_to_conduit(dp);
1047 struct dsa_db db = {
1048 .type = DSA_DB_BRIDGE,
1049 .bridge = *dp->bridge,
1053 if (!dp->ds->fdb_isolation)
1056 /* Avoid a call to __dev_set_promiscuity() on the conduit, which
1057 * requires rtnl_lock(), since we can't guarantee that is held here,
1058 * and we can't take it either.
1060 if (conduit->priv_flags & IFF_UNICAST_FLT) {
1061 err = dev_uc_add(conduit, addr);
1066 return dsa_port_host_fdb_add(dp, addr, vid, db);
1069 static int dsa_port_host_fdb_del(struct dsa_port *dp,
1070 const unsigned char *addr, u16 vid,
1073 struct dsa_notifier_fdb_info info = {
1080 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
1083 int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
1084 const unsigned char *addr, u16 vid)
1086 struct dsa_db db = {
1087 .type = DSA_DB_PORT,
1091 return dsa_port_host_fdb_del(dp, addr, vid, db);
1094 int dsa_port_bridge_host_fdb_del(struct dsa_port *dp,
1095 const unsigned char *addr, u16 vid)
1097 struct net_device *conduit = dsa_port_to_conduit(dp);
1098 struct dsa_db db = {
1099 .type = DSA_DB_BRIDGE,
1100 .bridge = *dp->bridge,
1104 if (!dp->ds->fdb_isolation)
1107 if (conduit->priv_flags & IFF_UNICAST_FLT) {
1108 err = dev_uc_del(conduit, addr);
1113 return dsa_port_host_fdb_del(dp, addr, vid, db);
1116 int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
1119 struct dsa_notifier_lag_fdb_info info = {
1124 .type = DSA_DB_BRIDGE,
1125 .bridge = *dp->bridge,
1129 if (!dp->ds->fdb_isolation)
1130 info.db.bridge.num = 0;
1132 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
1135 int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
1138 struct dsa_notifier_lag_fdb_info info = {
1143 .type = DSA_DB_BRIDGE,
1144 .bridge = *dp->bridge,
1148 if (!dp->ds->fdb_isolation)
1149 info.db.bridge.num = 0;
1151 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_DEL, &info);
1154 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
1156 struct dsa_switch *ds = dp->ds;
1157 int port = dp->index;
1159 if (!ds->ops->port_fdb_dump)
1162 return ds->ops->port_fdb_dump(ds, port, cb, data);
1165 int dsa_port_mdb_add(const struct dsa_port *dp,
1166 const struct switchdev_obj_port_mdb *mdb)
1168 struct dsa_notifier_mdb_info info = {
1172 .type = DSA_DB_BRIDGE,
1173 .bridge = *dp->bridge,
1177 if (!dp->ds->fdb_isolation)
1178 info.db.bridge.num = 0;
1180 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
1183 int dsa_port_mdb_del(const struct dsa_port *dp,
1184 const struct switchdev_obj_port_mdb *mdb)
1186 struct dsa_notifier_mdb_info info = {
1190 .type = DSA_DB_BRIDGE,
1191 .bridge = *dp->bridge,
1195 if (!dp->ds->fdb_isolation)
1196 info.db.bridge.num = 0;
1198 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
1201 static int dsa_port_host_mdb_add(const struct dsa_port *dp,
1202 const struct switchdev_obj_port_mdb *mdb,
1205 struct dsa_notifier_mdb_info info = {
1211 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
1214 int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
1215 const struct switchdev_obj_port_mdb *mdb)
1217 struct dsa_db db = {
1218 .type = DSA_DB_PORT,
1222 return dsa_port_host_mdb_add(dp, mdb, db);
1225 int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
1226 const struct switchdev_obj_port_mdb *mdb)
1228 struct net_device *conduit = dsa_port_to_conduit(dp);
1229 struct dsa_db db = {
1230 .type = DSA_DB_BRIDGE,
1231 .bridge = *dp->bridge,
1235 if (!dp->ds->fdb_isolation)
1238 err = dev_mc_add(conduit, mdb->addr);
1242 return dsa_port_host_mdb_add(dp, mdb, db);
1245 static int dsa_port_host_mdb_del(const struct dsa_port *dp,
1246 const struct switchdev_obj_port_mdb *mdb,
1249 struct dsa_notifier_mdb_info info = {
1255 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
1258 int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
1259 const struct switchdev_obj_port_mdb *mdb)
1261 struct dsa_db db = {
1262 .type = DSA_DB_PORT,
1266 return dsa_port_host_mdb_del(dp, mdb, db);
1269 int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
1270 const struct switchdev_obj_port_mdb *mdb)
1272 struct net_device *conduit = dsa_port_to_conduit(dp);
1273 struct dsa_db db = {
1274 .type = DSA_DB_BRIDGE,
1275 .bridge = *dp->bridge,
1279 if (!dp->ds->fdb_isolation)
1282 err = dev_mc_del(conduit, mdb->addr);
1286 return dsa_port_host_mdb_del(dp, mdb, db);
1289 int dsa_port_vlan_add(struct dsa_port *dp,
1290 const struct switchdev_obj_port_vlan *vlan,
1291 struct netlink_ext_ack *extack)
1293 struct dsa_notifier_vlan_info info = {
1299 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
1302 int dsa_port_vlan_del(struct dsa_port *dp,
1303 const struct switchdev_obj_port_vlan *vlan)
1305 struct dsa_notifier_vlan_info info = {
1310 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
1313 int dsa_port_host_vlan_add(struct dsa_port *dp,
1314 const struct switchdev_obj_port_vlan *vlan,
1315 struct netlink_ext_ack *extack)
1317 struct net_device *conduit = dsa_port_to_conduit(dp);
1318 struct dsa_notifier_vlan_info info = {
1325 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
1326 if (err && err != -EOPNOTSUPP)
1329 vlan_vid_add(conduit, htons(ETH_P_8021Q), vlan->vid);
1334 int dsa_port_host_vlan_del(struct dsa_port *dp,
1335 const struct switchdev_obj_port_vlan *vlan)
1337 struct net_device *conduit = dsa_port_to_conduit(dp);
1338 struct dsa_notifier_vlan_info info = {
1344 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
1345 if (err && err != -EOPNOTSUPP)
1348 vlan_vid_del(conduit, htons(ETH_P_8021Q), vlan->vid);
1353 int dsa_port_mrp_add(const struct dsa_port *dp,
1354 const struct switchdev_obj_mrp *mrp)
1356 struct dsa_switch *ds = dp->ds;
1358 if (!ds->ops->port_mrp_add)
1361 return ds->ops->port_mrp_add(ds, dp->index, mrp);
1364 int dsa_port_mrp_del(const struct dsa_port *dp,
1365 const struct switchdev_obj_mrp *mrp)
1367 struct dsa_switch *ds = dp->ds;
1369 if (!ds->ops->port_mrp_del)
1372 return ds->ops->port_mrp_del(ds, dp->index, mrp);
1375 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
1376 const struct switchdev_obj_ring_role_mrp *mrp)
1378 struct dsa_switch *ds = dp->ds;
1380 if (!ds->ops->port_mrp_add_ring_role)
1383 return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
1386 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
1387 const struct switchdev_obj_ring_role_mrp *mrp)
1389 struct dsa_switch *ds = dp->ds;
1391 if (!ds->ops->port_mrp_del_ring_role)
1394 return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
1397 static int dsa_port_assign_conduit(struct dsa_port *dp,
1398 struct net_device *conduit,
1399 struct netlink_ext_ack *extack,
1402 struct dsa_switch *ds = dp->ds;
1403 int port = dp->index, err;
1405 err = ds->ops->port_change_conduit(ds, port, conduit, extack);
1406 if (err && !fail_on_err)
1407 dev_err(ds->dev, "port %d failed to assign conduit %s: %pe\n",
1408 port, conduit->name, ERR_PTR(err));
1410 if (err && fail_on_err)
1413 dp->cpu_dp = conduit->dsa_ptr;
1414 dp->cpu_port_in_lag = netif_is_lag_master(conduit);
1419 /* Change the dp->cpu_dp affinity for a user port. Note that both cross-chip
1420 * notifiers and drivers have implicit assumptions about user-to-CPU-port
1421 * mappings, so we unfortunately cannot delay the deletion of the objects
1422 * (switchdev, standalone addresses, standalone VLANs) on the old CPU port
1423 * until the new CPU port has been set up. So we need to completely tear down
1424 * the old CPU port before changing it, and restore it on errors during the
1425 * bringup of the new one.
1427 int dsa_port_change_conduit(struct dsa_port *dp, struct net_device *conduit,
1428 struct netlink_ext_ack *extack)
1430 struct net_device *bridge_dev = dsa_port_bridge_dev_get(dp);
1431 struct net_device *old_conduit = dsa_port_to_conduit(dp);
1432 struct net_device *dev = dp->user;
1433 struct dsa_switch *ds = dp->ds;
1434 bool vlan_filtering;
1437 /* Bridges may hold host FDB, MDB and VLAN objects. These need to be
1438 * migrated, so dynamically unoffload and later reoffload the bridge
1442 dsa_port_pre_bridge_leave(dp, bridge_dev);
1443 dsa_port_bridge_leave(dp, bridge_dev);
1446 /* The port might still be VLAN filtering even if it's no longer
1447 * under a bridge, either due to ds->vlan_filtering_is_global or
1448 * ds->needs_standalone_vlan_filtering. In turn this means VLANs
1451 vlan_filtering = dsa_port_is_vlan_filtering(dp);
1452 if (vlan_filtering) {
1453 err = dsa_user_manage_vlan_filtering(dev, false);
1455 NL_SET_ERR_MSG_MOD(extack,
1456 "Failed to remove standalone VLANs");
1457 goto rewind_old_bridge;
1461 /* Standalone addresses, and addresses of upper interfaces like
1462 * VLAN, LAG, HSR need to be migrated.
1464 dsa_user_unsync_ha(dev);
1466 /* If live-changing, we also need to uninstall the user device address
1467 * from the port FDB and the conduit interface.
1469 if (dev->flags & IFF_UP)
1470 dsa_user_host_uc_uninstall(dev);
1472 err = dsa_port_assign_conduit(dp, conduit, extack, true);
1474 goto rewind_old_addrs;
1476 /* If the port doesn't have its own MAC address and relies on the DSA
1477 * conduit's one, inherit it again from the new DSA conduit.
1479 if (is_zero_ether_addr(dp->mac))
1480 eth_hw_addr_inherit(dev, conduit);
1482 /* If live-changing, we need to install the user device address to the
1483 * port FDB and the conduit interface.
1485 if (dev->flags & IFF_UP) {
1486 err = dsa_user_host_uc_install(dev, dev->dev_addr);
1488 NL_SET_ERR_MSG_MOD(extack,
1489 "Failed to install host UC address");
1490 goto rewind_addr_inherit;
1494 dsa_user_sync_ha(dev);
1496 if (vlan_filtering) {
1497 err = dsa_user_manage_vlan_filtering(dev, true);
1499 NL_SET_ERR_MSG_MOD(extack,
1500 "Failed to restore standalone VLANs");
1501 goto rewind_new_addrs;
1506 err = dsa_port_bridge_join(dp, bridge_dev, extack);
1507 if (err && err == -EOPNOTSUPP) {
1508 NL_SET_ERR_MSG_MOD(extack,
1509 "Failed to reoffload bridge");
1510 goto rewind_new_vlan;
1518 dsa_user_manage_vlan_filtering(dev, false);
1521 dsa_user_unsync_ha(dev);
1523 if (dev->flags & IFF_UP)
1524 dsa_user_host_uc_uninstall(dev);
1526 rewind_addr_inherit:
1527 if (is_zero_ether_addr(dp->mac))
1528 eth_hw_addr_inherit(dev, old_conduit);
1530 dsa_port_assign_conduit(dp, old_conduit, NULL, false);
1532 /* Restore the objects on the old CPU port */
1534 if (dev->flags & IFF_UP) {
1535 tmp = dsa_user_host_uc_install(dev, dev->dev_addr);
1538 "port %d failed to restore host UC address: %pe\n",
1539 dp->index, ERR_PTR(tmp));
1543 dsa_user_sync_ha(dev);
1545 if (vlan_filtering) {
1546 tmp = dsa_user_manage_vlan_filtering(dev, true);
1549 "port %d failed to restore standalone VLANs: %pe\n",
1550 dp->index, ERR_PTR(tmp));
1556 tmp = dsa_port_bridge_join(dp, bridge_dev, extack);
1559 "port %d failed to rejoin bridge %s: %pe\n",
1560 dp->index, bridge_dev->name, ERR_PTR(tmp));
1567 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
1568 const struct dsa_device_ops *tag_ops)
1570 cpu_dp->rcv = tag_ops->rcv;
1571 cpu_dp->tag_ops = tag_ops;
1574 /* dsa_supports_eee - indicate that EEE is supported
1575 * @ds: pointer to &struct dsa_switch
1578 * A default implementation for the .support_eee() DSA operations member,
1579 * which drivers can use to indicate that they support EEE on all of their
1584 bool dsa_supports_eee(struct dsa_switch *ds, int port)
1588 EXPORT_SYMBOL_GPL(dsa_supports_eee);
1590 static void dsa_port_phylink_mac_config(struct phylink_config *config,
1592 const struct phylink_link_state *state)
1596 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1598 phy_interface_t interface)
1602 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1603 struct phy_device *phydev,
1605 phy_interface_t interface,
1606 int speed, int duplex,
1607 bool tx_pause, bool rx_pause)
1611 static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1612 .mac_config = dsa_port_phylink_mac_config,
1613 .mac_link_down = dsa_port_phylink_mac_link_down,
1614 .mac_link_up = dsa_port_phylink_mac_link_up,
1617 int dsa_port_phylink_create(struct dsa_port *dp)
1619 const struct phylink_mac_ops *mac_ops;
1620 struct dsa_switch *ds = dp->ds;
1621 phy_interface_t mode;
1625 err = of_get_phy_mode(dp->dn, &mode);
1627 mode = PHY_INTERFACE_MODE_NA;
1629 if (ds->ops->phylink_get_caps) {
1630 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
1632 /* For legacy drivers */
1633 if (mode != PHY_INTERFACE_MODE_NA) {
1634 __set_bit(mode, dp->pl_config.supported_interfaces);
1636 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1637 dp->pl_config.supported_interfaces);
1638 __set_bit(PHY_INTERFACE_MODE_GMII,
1639 dp->pl_config.supported_interfaces);
1643 mac_ops = &dsa_port_phylink_mac_ops;
1644 if (ds->phylink_mac_ops)
1645 mac_ops = ds->phylink_mac_ops;
1647 pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn), mode,
1650 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(pl));
1659 void dsa_port_phylink_destroy(struct dsa_port *dp)
1661 phylink_destroy(dp->pl);
1665 static int dsa_shared_port_phylink_register(struct dsa_port *dp)
1667 struct dsa_switch *ds = dp->ds;
1668 struct device_node *port_dn = dp->dn;
1671 dp->pl_config.dev = ds->dev;
1672 dp->pl_config.type = PHYLINK_DEV;
1674 err = dsa_port_phylink_create(dp);
1678 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1679 if (err && err != -ENODEV) {
1680 pr_err("could not attach to PHY: %d\n", err);
1681 goto err_phy_connect;
1687 dsa_port_phylink_destroy(dp);
1691 /* During the initial DSA driver migration to OF, port nodes were sometimes
1692 * added to device trees with no indication of how they should operate from a
1693 * link management perspective (phy-handle, fixed-link, etc). Additionally, the
1694 * phy-mode may be absent. The interpretation of these port OF nodes depends on
1697 * User ports with no phy-handle or fixed-link are expected to connect to an
1698 * internal PHY located on the ds->user_mii_bus at an MDIO address equal to
1699 * the port number. This description is still actively supported.
1701 * Shared (CPU and DSA) ports with no phy-handle or fixed-link are expected to
1702 * operate at the maximum speed that their phy-mode is capable of. If the
1703 * phy-mode is absent, they are expected to operate using the phy-mode
1704 * supported by the port that gives the highest link speed. It is unspecified
1705 * if the port should use flow control or not, half duplex or full duplex, or
1706 * if the phy-mode is a SERDES link, whether in-band autoneg is expected to be
1709 * In the latter case of shared ports, omitting the link management description
1710 * from the firmware node is deprecated and strongly discouraged. DSA uses
1711 * phylink, which rejects the firmware nodes of these ports for lacking
1712 * required properties.
1714 * For switches in this table, DSA will skip enforcing validation and will
1715 * later omit registering a phylink instance for the shared ports, if they lack
1716 * a fixed-link, a phy-handle, or a managed = "in-band-status" property.
1717 * It becomes the responsibility of the driver to ensure that these ports
1718 * operate at the maximum speed (whatever this means) and will interoperate
1719 * with the DSA conduit or other cascade port, since phylink methods will not be
1722 * If you are considering expanding this table for newly introduced switches,
1723 * think again. It is OK to remove switches from this table if there aren't DT
1724 * blobs in circulation which rely on defaulting the shared ports.
1726 static const char * const dsa_switches_apply_workarounds[] = {
1727 #if IS_ENABLED(CONFIG_NET_DSA_XRS700X)
1733 #if IS_ENABLED(CONFIG_B53)
1743 "brcm,bcm53010-srab",
1744 "brcm,bcm53011-srab",
1745 "brcm,bcm53012-srab",
1746 "brcm,bcm53018-srab",
1747 "brcm,bcm53019-srab",
1748 "brcm,bcm5301x-srab",
1749 "brcm,bcm11360-srab",
1750 "brcm,bcm58522-srab",
1751 "brcm,bcm58525-srab",
1752 "brcm,bcm58535-srab",
1753 "brcm,bcm58622-srab",
1754 "brcm,bcm58623-srab",
1755 "brcm,bcm58625-srab",
1756 "brcm,bcm88312-srab",
1760 "brcm,bcm3384-switch",
1761 "brcm,bcm6328-switch",
1762 "brcm,bcm6368-switch",
1763 "brcm,bcm63xx-switch",
1765 #if IS_ENABLED(CONFIG_NET_DSA_BCM_SF2)
1766 "brcm,bcm7445-switch-v4.0",
1767 "brcm,bcm7278-switch-v4.0",
1768 "brcm,bcm7278-switch-v4.8",
1770 #if IS_ENABLED(CONFIG_NET_DSA_LANTIQ_GSWIP)
1771 "lantiq,xrx200-gswip",
1772 "lantiq,xrx300-gswip",
1773 "lantiq,xrx330-gswip",
1775 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6060)
1776 "marvell,mv88e6060",
1778 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6XXX)
1779 "marvell,mv88e6085",
1780 "marvell,mv88e6190",
1781 "marvell,mv88e6250",
1783 #if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON)
1784 "microchip,ksz8765",
1785 "microchip,ksz8794",
1786 "microchip,ksz8795",
1787 "microchip,ksz8863",
1788 "microchip,ksz8873",
1789 "microchip,ksz9477",
1790 "microchip,ksz9897",
1791 "microchip,ksz9893",
1792 "microchip,ksz9563",
1793 "microchip,ksz8563",
1794 "microchip,ksz9567",
1796 #if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_MDIO)
1797 "smsc,lan9303-mdio",
1799 #if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_I2C)
1805 static void dsa_shared_port_validate_of(struct dsa_port *dp,
1806 bool *missing_phy_mode,
1807 bool *missing_link_description)
1809 struct device_node *dn = dp->dn, *phy_np;
1810 struct dsa_switch *ds = dp->ds;
1811 phy_interface_t mode;
1813 *missing_phy_mode = false;
1814 *missing_link_description = false;
1816 if (of_get_phy_mode(dn, &mode)) {
1817 *missing_phy_mode = true;
1819 "OF node %pOF of %s port %d lacks the required \"phy-mode\" property\n",
1820 dn, dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1823 /* Note: of_phy_is_fixed_link() also returns true for
1824 * managed = "in-band-status"
1826 if (of_phy_is_fixed_link(dn))
1829 phy_np = of_parse_phandle(dn, "phy-handle", 0);
1831 of_node_put(phy_np);
1835 *missing_link_description = true;
1838 "OF node %pOF of %s port %d lacks the required \"phy-handle\", \"fixed-link\" or \"managed\" properties\n",
1839 dn, dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1842 static void dsa_shared_port_link_down(struct dsa_port *dp)
1844 struct dsa_switch *ds = dp->ds;
1846 if (ds->phylink_mac_ops && ds->phylink_mac_ops->mac_link_down)
1847 ds->phylink_mac_ops->mac_link_down(&dp->pl_config, MLO_AN_FIXED,
1848 PHY_INTERFACE_MODE_NA);
1851 int dsa_shared_port_link_register_of(struct dsa_port *dp)
1853 struct dsa_switch *ds = dp->ds;
1854 bool missing_link_description;
1855 bool missing_phy_mode;
1857 dsa_shared_port_validate_of(dp, &missing_phy_mode,
1858 &missing_link_description);
1860 if ((missing_phy_mode || missing_link_description) &&
1861 !of_device_compatible_match(ds->dev->of_node,
1862 dsa_switches_apply_workarounds))
1865 if (missing_link_description) {
1867 "Skipping phylink registration for %s port %d\n",
1868 dsa_port_is_cpu(dp) ? "CPU" : "DSA", dp->index);
1870 dsa_shared_port_link_down(dp);
1872 return dsa_shared_port_phylink_register(dp);
1878 void dsa_shared_port_link_unregister_of(struct dsa_port *dp)
1882 phylink_disconnect_phy(dp->pl);
1884 dsa_port_phylink_destroy(dp);
1889 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr,
1890 struct netlink_ext_ack *extack)
1892 struct dsa_switch *ds = dp->ds;
1895 if (!ds->ops->port_hsr_join)
1900 err = ds->ops->port_hsr_join(ds, dp->index, hsr, extack);
1907 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1909 struct dsa_switch *ds = dp->ds;
1914 if (ds->ops->port_hsr_leave) {
1915 err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
1917 dev_err(dp->ds->dev,
1918 "port %d failed to leave HSR %s: %pe\n",
1919 dp->index, hsr->name, ERR_PTR(err));
1923 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
1925 struct dsa_notifier_tag_8021q_vlan_info info = {
1931 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1933 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1936 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
1938 struct dsa_notifier_tag_8021q_vlan_info info = {
1945 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1947 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1949 dev_err(dp->ds->dev,
1950 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1951 dp->index, vid, ERR_PTR(err));