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, u16 vid)
35 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
36 struct switchdev_notifier_fdb_info info = {
40 /* When the port becomes standalone it has already left the bridge.
41 * Don't notify the bridge in that case.
46 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
47 brport_dev, &info.info, NULL);
50 static void dsa_port_fast_age(const struct dsa_port *dp)
52 struct dsa_switch *ds = dp->ds;
54 if (!ds->ops->port_fast_age)
57 ds->ops->port_fast_age(ds, dp->index);
60 dsa_port_notify_bridge_fdb_flush(dp, 0);
63 static int dsa_port_vlan_fast_age(const struct dsa_port *dp, u16 vid)
65 struct dsa_switch *ds = dp->ds;
68 if (!ds->ops->port_vlan_fast_age)
71 err = ds->ops->port_vlan_fast_age(ds, dp->index, vid);
74 dsa_port_notify_bridge_fdb_flush(dp, vid);
79 static int dsa_port_msti_fast_age(const struct dsa_port *dp, u16 msti)
81 DECLARE_BITMAP(vids, VLAN_N_VID) = { 0 };
84 err = br_mst_get_info(dsa_port_bridge_dev_get(dp), msti, vids);
88 for_each_set_bit(vid, vids, VLAN_N_VID) {
89 err = dsa_port_vlan_fast_age(dp, vid);
97 static bool dsa_port_can_configure_learning(struct dsa_port *dp)
99 struct switchdev_brport_flags flags = {
102 struct dsa_switch *ds = dp->ds;
105 if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags)
108 err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL);
112 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
114 struct dsa_switch *ds = dp->ds;
115 int port = dp->index;
117 if (!ds->ops->port_stp_state_set)
120 ds->ops->port_stp_state_set(ds, port, state);
122 if (!dsa_port_can_configure_learning(dp) ||
123 (do_fast_age && dp->learning)) {
124 /* Fast age FDB entries or flush appropriate forwarding database
125 * for the given port, if we are moving it from Learning or
126 * Forwarding state, to Disabled or Blocking or Listening state.
127 * Ports that were standalone before the STP state change don't
128 * need to fast age the FDB, since address learning is off in
132 if ((dp->stp_state == BR_STATE_LEARNING ||
133 dp->stp_state == BR_STATE_FORWARDING) &&
134 (state == BR_STATE_DISABLED ||
135 state == BR_STATE_BLOCKING ||
136 state == BR_STATE_LISTENING))
137 dsa_port_fast_age(dp);
140 dp->stp_state = state;
145 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
148 struct dsa_switch *ds = dp->ds;
151 err = dsa_port_set_state(dp, state, do_fast_age);
152 if (err && err != -EOPNOTSUPP) {
153 dev_err(ds->dev, "port %d failed to set STP state %u: %pe\n",
154 dp->index, state, ERR_PTR(err));
158 int dsa_port_set_mst_state(struct dsa_port *dp,
159 const struct switchdev_mst_state *state,
160 struct netlink_ext_ack *extack)
162 struct dsa_switch *ds = dp->ds;
166 if (!ds->ops->port_mst_state_set)
169 err = br_mst_get_state(dsa_port_to_bridge_port(dp), state->msti,
174 err = ds->ops->port_mst_state_set(ds, dp->index, state);
178 if (!(dp->learning &&
179 (prev_state == BR_STATE_LEARNING ||
180 prev_state == BR_STATE_FORWARDING) &&
181 (state->state == BR_STATE_DISABLED ||
182 state->state == BR_STATE_BLOCKING ||
183 state->state == BR_STATE_LISTENING)))
186 err = dsa_port_msti_fast_age(dp, state->msti);
188 NL_SET_ERR_MSG_MOD(extack,
189 "Unable to flush associated VLANs");
194 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
196 struct dsa_switch *ds = dp->ds;
197 int port = dp->index;
200 if (ds->ops->port_enable) {
201 err = ds->ops->port_enable(ds, port, phy);
207 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
210 phylink_start(dp->pl);
215 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
220 err = dsa_port_enable_rt(dp, phy);
226 void dsa_port_disable_rt(struct dsa_port *dp)
228 struct dsa_switch *ds = dp->ds;
229 int port = dp->index;
232 phylink_stop(dp->pl);
235 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
237 if (ds->ops->port_disable)
238 ds->ops->port_disable(ds, port);
241 void dsa_port_disable(struct dsa_port *dp)
244 dsa_port_disable_rt(dp);
248 static void dsa_port_reset_vlan_filtering(struct dsa_port *dp,
249 struct dsa_bridge bridge)
251 struct netlink_ext_ack extack = {0};
252 bool change_vlan_filtering = false;
253 struct dsa_switch *ds = dp->ds;
254 struct dsa_port *other_dp;
258 if (ds->needs_standalone_vlan_filtering &&
259 !br_vlan_enabled(bridge.dev)) {
260 change_vlan_filtering = true;
261 vlan_filtering = true;
262 } else if (!ds->needs_standalone_vlan_filtering &&
263 br_vlan_enabled(bridge.dev)) {
264 change_vlan_filtering = true;
265 vlan_filtering = false;
268 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
269 * event for changing vlan_filtering setting upon slave ports leaving
270 * it. That is a good thing, because that lets us handle it and also
271 * handle the case where the switch's vlan_filtering setting is global
272 * (not per port). When that happens, the correct moment to trigger the
273 * vlan_filtering callback is only when the last port leaves the last
276 if (change_vlan_filtering && ds->vlan_filtering_is_global) {
277 dsa_switch_for_each_port(other_dp, ds) {
278 struct net_device *br = dsa_port_bridge_dev_get(other_dp);
280 if (br && br_vlan_enabled(br)) {
281 change_vlan_filtering = false;
287 if (!change_vlan_filtering)
290 err = dsa_port_vlan_filtering(dp, vlan_filtering, &extack);
292 dev_err(ds->dev, "port %d: %s\n", dp->index,
295 if (err && err != -EOPNOTSUPP) {
297 "port %d failed to reset VLAN filtering to %d: %pe\n",
298 dp->index, vlan_filtering, ERR_PTR(err));
302 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
303 struct netlink_ext_ack *extack)
305 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
306 BR_BCAST_FLOOD | BR_PORT_LOCKED;
307 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
310 for_each_set_bit(flag, &mask, 32) {
311 struct switchdev_brport_flags flags = {0};
313 flags.mask = BIT(flag);
315 if (br_port_flag_is_set(brport_dev, BIT(flag)))
316 flags.val = BIT(flag);
318 err = dsa_port_bridge_flags(dp, flags, extack);
319 if (err && err != -EOPNOTSUPP)
326 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
328 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
329 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
330 BR_BCAST_FLOOD | BR_PORT_LOCKED;
333 for_each_set_bit(flag, &mask, 32) {
334 struct switchdev_brport_flags flags = {0};
336 flags.mask = BIT(flag);
337 flags.val = val & BIT(flag);
339 err = dsa_port_bridge_flags(dp, flags, NULL);
340 if (err && err != -EOPNOTSUPP)
342 "failed to clear bridge port flag %lu: %pe\n",
343 flags.val, ERR_PTR(err));
347 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
348 struct netlink_ext_ack *extack)
350 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
351 struct net_device *br = dsa_port_bridge_dev_get(dp);
354 err = dsa_port_inherit_brport_flags(dp, extack);
358 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
359 if (err && err != -EOPNOTSUPP)
362 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
363 if (err && err != -EOPNOTSUPP)
366 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
367 if (err && err != -EOPNOTSUPP)
373 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp,
374 struct dsa_bridge bridge)
376 /* Configure the port for standalone mode (no address learning,
378 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
379 * when the user requests it through netlink or sysfs, but not
380 * automatically at port join or leave, so we need to handle resetting
381 * the brport flags ourselves. But we even prefer it that way, because
382 * otherwise, some setups might never get the notification they need,
383 * for example, when a port leaves a LAG that offloads the bridge,
384 * it becomes standalone, but as far as the bridge is concerned, no
387 dsa_port_clear_brport_flags(dp);
389 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
390 * so allow it to be in BR_STATE_FORWARDING to be kept functional
392 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
394 dsa_port_reset_vlan_filtering(dp, bridge);
396 /* Ageing time may be global to the switch chip, so don't change it
397 * here because we have no good reason (or value) to change it to.
401 static int dsa_port_bridge_create(struct dsa_port *dp,
402 struct net_device *br,
403 struct netlink_ext_ack *extack)
405 struct dsa_switch *ds = dp->ds;
406 struct dsa_bridge *bridge;
408 bridge = dsa_tree_bridge_find(ds->dst, br);
410 refcount_inc(&bridge->refcount);
415 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
419 refcount_set(&bridge->refcount, 1);
423 bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges);
424 if (ds->max_num_bridges && !bridge->num) {
425 NL_SET_ERR_MSG_MOD(extack,
426 "Range of offloadable bridges exceeded");
436 static void dsa_port_bridge_destroy(struct dsa_port *dp,
437 const struct net_device *br)
439 struct dsa_bridge *bridge = dp->bridge;
443 if (!refcount_dec_and_test(&bridge->refcount))
447 dsa_bridge_num_put(br, bridge->num);
452 static bool dsa_port_supports_mst(struct dsa_port *dp)
454 struct dsa_switch *ds = dp->ds;
456 return ds->ops->vlan_msti_set &&
457 ds->ops->port_mst_state_set &&
458 ds->ops->port_vlan_fast_age &&
459 dsa_port_can_configure_learning(dp);
462 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
463 struct netlink_ext_ack *extack)
465 struct dsa_notifier_bridge_info info = {
469 struct net_device *dev = dp->slave;
470 struct net_device *brport_dev;
473 if (br_mst_enabled(br) && !dsa_port_supports_mst(dp))
476 /* Here the interface is already bridged. Reflect the current
477 * configuration so that drivers can program their chips accordingly.
479 err = dsa_port_bridge_create(dp, br, extack);
483 brport_dev = dsa_port_to_bridge_port(dp);
485 info.bridge = *dp->bridge;
486 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
490 /* Drivers which support bridge TX forwarding should set this */
491 dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
493 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
494 &dsa_slave_switchdev_notifier,
495 &dsa_slave_switchdev_blocking_notifier,
496 dp->bridge->tx_fwd_offload, extack);
498 goto out_rollback_unbridge;
500 err = dsa_port_switchdev_sync_attrs(dp, extack);
502 goto out_rollback_unoffload;
506 out_rollback_unoffload:
507 switchdev_bridge_port_unoffload(brport_dev, dp,
508 &dsa_slave_switchdev_notifier,
509 &dsa_slave_switchdev_blocking_notifier);
510 dsa_flush_workqueue();
511 out_rollback_unbridge:
512 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
514 dsa_port_bridge_destroy(dp, br);
518 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
520 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
522 /* Don't try to unoffload something that is not offloaded */
526 switchdev_bridge_port_unoffload(brport_dev, dp,
527 &dsa_slave_switchdev_notifier,
528 &dsa_slave_switchdev_blocking_notifier);
530 dsa_flush_workqueue();
533 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
535 struct dsa_notifier_bridge_info info = {
540 /* If the port could not be offloaded to begin with, then
541 * there is nothing to do.
546 info.bridge = *dp->bridge;
548 /* Here the port is already unbridged. Reflect the current configuration
549 * so that drivers can program their chips accordingly.
551 dsa_port_bridge_destroy(dp, br);
553 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
556 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
557 dp->index, ERR_PTR(err));
559 dsa_port_switchdev_unsync_attrs(dp, info.bridge);
562 int dsa_port_lag_change(struct dsa_port *dp,
563 struct netdev_lag_lower_state_info *linfo)
565 struct dsa_notifier_lag_info info = {
573 /* On statically configured aggregates (e.g. loadbalance
574 * without LACP) ports will always be tx_enabled, even if the
575 * link is down. Thus we require both link_up and tx_enabled
576 * in order to include it in the tx set.
578 tx_enabled = linfo->link_up && linfo->tx_enabled;
580 if (tx_enabled == dp->lag_tx_enabled)
583 dp->lag_tx_enabled = tx_enabled;
585 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
588 static int dsa_port_lag_create(struct dsa_port *dp,
589 struct net_device *lag_dev)
591 struct dsa_switch *ds = dp->ds;
594 lag = dsa_tree_lag_find(ds->dst, lag_dev);
596 refcount_inc(&lag->refcount);
601 lag = kzalloc(sizeof(*lag), GFP_KERNEL);
605 refcount_set(&lag->refcount, 1);
606 mutex_init(&lag->fdb_lock);
607 INIT_LIST_HEAD(&lag->fdbs);
609 dsa_lag_map(ds->dst, lag);
615 static void dsa_port_lag_destroy(struct dsa_port *dp)
617 struct dsa_lag *lag = dp->lag;
620 dp->lag_tx_enabled = false;
622 if (!refcount_dec_and_test(&lag->refcount))
625 WARN_ON(!list_empty(&lag->fdbs));
626 dsa_lag_unmap(dp->ds->dst, lag);
630 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
631 struct netdev_lag_upper_info *uinfo,
632 struct netlink_ext_ack *extack)
634 struct dsa_notifier_lag_info info = {
638 struct net_device *bridge_dev;
641 err = dsa_port_lag_create(dp, lag_dev);
646 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
650 bridge_dev = netdev_master_upper_dev_get(lag_dev);
651 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
654 err = dsa_port_bridge_join(dp, bridge_dev, extack);
656 goto err_bridge_join;
661 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
663 dsa_port_lag_destroy(dp);
668 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
670 struct net_device *br = dsa_port_bridge_dev_get(dp);
673 dsa_port_pre_bridge_leave(dp, br);
676 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
678 struct net_device *br = dsa_port_bridge_dev_get(dp);
679 struct dsa_notifier_lag_info info = {
687 /* Port might have been part of a LAG that in turn was
688 * attached to a bridge.
691 dsa_port_bridge_leave(dp, br);
695 dsa_port_lag_destroy(dp);
697 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
700 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
701 dp->index, ERR_PTR(err));
704 /* Must be called under rcu_read_lock() */
705 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
707 struct netlink_ext_ack *extack)
709 struct dsa_switch *ds = dp->ds;
710 struct dsa_port *other_dp;
713 /* VLAN awareness was off, so the question is "can we turn it on".
714 * We may have had 8021q uppers, those need to go. Make sure we don't
715 * enter an inconsistent state: deny changing the VLAN awareness state
716 * as long as we have 8021q uppers.
718 if (vlan_filtering && dsa_port_is_user(dp)) {
719 struct net_device *br = dsa_port_bridge_dev_get(dp);
720 struct net_device *upper_dev, *slave = dp->slave;
721 struct list_head *iter;
723 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
724 struct bridge_vlan_info br_info;
727 if (!is_vlan_dev(upper_dev))
730 vid = vlan_dev_vlan_id(upper_dev);
732 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
733 * device, respectively the VID is not found, returning
734 * 0 means success, which is a failure for us here.
736 err = br_vlan_get_info(br, vid, &br_info);
738 NL_SET_ERR_MSG_MOD(extack,
739 "Must first remove VLAN uppers having VIDs also present in bridge");
745 if (!ds->vlan_filtering_is_global)
748 /* For cases where enabling/disabling VLAN awareness is global to the
749 * switch, we need to handle the case where multiple bridges span
750 * different ports of the same switch device and one of them has a
751 * different setting than what is being requested.
753 dsa_switch_for_each_port(other_dp, ds) {
754 struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
756 /* If it's the same bridge, it also has same
757 * vlan_filtering setting => no need to check
759 if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
762 if (br_vlan_enabled(other_br) != vlan_filtering) {
763 NL_SET_ERR_MSG_MOD(extack,
764 "VLAN filtering is a global setting");
771 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
772 struct netlink_ext_ack *extack)
774 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
775 struct dsa_switch *ds = dp->ds;
779 if (!ds->ops->port_vlan_filtering)
782 /* We are called from dsa_slave_switchdev_blocking_event(),
783 * which is not under rcu_read_lock(), unlike
784 * dsa_slave_switchdev_event().
787 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
792 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
795 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
800 if (ds->vlan_filtering_is_global) {
801 struct dsa_port *other_dp;
803 ds->vlan_filtering = vlan_filtering;
805 dsa_switch_for_each_user_port(other_dp, ds) {
806 struct net_device *slave = other_dp->slave;
808 /* We might be called in the unbind path, so not
809 * all slave devices might still be registered.
814 err = dsa_slave_manage_vlan_filtering(slave,
820 dp->vlan_filtering = vlan_filtering;
822 err = dsa_slave_manage_vlan_filtering(dp->slave,
831 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
833 if (ds->vlan_filtering_is_global)
834 ds->vlan_filtering = old_vlan_filtering;
836 dp->vlan_filtering = old_vlan_filtering;
841 /* This enforces legacy behavior for switch drivers which assume they can't
842 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
844 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
846 struct net_device *br = dsa_port_bridge_dev_get(dp);
847 struct dsa_switch *ds = dp->ds;
852 return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
855 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
857 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
858 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
859 struct dsa_notifier_ageing_time_info info;
862 info.ageing_time = ageing_time;
864 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
868 dp->ageing_time = ageing_time;
873 int dsa_port_mst_enable(struct dsa_port *dp, bool on,
874 struct netlink_ext_ack *extack)
876 if (on && !dsa_port_supports_mst(dp)) {
877 NL_SET_ERR_MSG_MOD(extack, "Hardware does not support MST");
884 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
885 struct switchdev_brport_flags flags,
886 struct netlink_ext_ack *extack)
888 struct dsa_switch *ds = dp->ds;
890 if (!ds->ops->port_pre_bridge_flags)
893 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
896 int dsa_port_bridge_flags(struct dsa_port *dp,
897 struct switchdev_brport_flags flags,
898 struct netlink_ext_ack *extack)
900 struct dsa_switch *ds = dp->ds;
903 if (!ds->ops->port_bridge_flags)
906 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
910 if (flags.mask & BR_LEARNING) {
911 bool learning = flags.val & BR_LEARNING;
913 if (learning == dp->learning)
916 if ((dp->learning && !learning) &&
917 (dp->stp_state == BR_STATE_LEARNING ||
918 dp->stp_state == BR_STATE_FORWARDING))
919 dsa_port_fast_age(dp);
921 dp->learning = learning;
927 void dsa_port_set_host_flood(struct dsa_port *dp, bool uc, bool mc)
929 struct dsa_switch *ds = dp->ds;
931 if (ds->ops->port_set_host_flood)
932 ds->ops->port_set_host_flood(ds, dp->index, uc, mc);
935 int dsa_port_vlan_msti(struct dsa_port *dp,
936 const struct switchdev_vlan_msti *msti)
938 struct dsa_switch *ds = dp->ds;
940 if (!ds->ops->vlan_msti_set)
943 return ds->ops->vlan_msti_set(ds, *dp->bridge, msti);
946 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu)
948 struct dsa_notifier_mtu_info info = {
953 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
956 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
959 struct dsa_notifier_fdb_info info = {
964 .type = DSA_DB_BRIDGE,
965 .bridge = *dp->bridge,
969 /* Refcounting takes bridge.num as a key, and should be global for all
970 * bridges in the absence of FDB isolation, and per bridge otherwise.
971 * Force the bridge.num to zero here in the absence of FDB isolation.
973 if (!dp->ds->fdb_isolation)
974 info.db.bridge.num = 0;
976 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
979 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
982 struct dsa_notifier_fdb_info info = {
987 .type = DSA_DB_BRIDGE,
988 .bridge = *dp->bridge,
992 if (!dp->ds->fdb_isolation)
993 info.db.bridge.num = 0;
995 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
998 static int dsa_port_host_fdb_add(struct dsa_port *dp,
999 const unsigned char *addr, u16 vid,
1002 struct dsa_notifier_fdb_info info = {
1009 if (!dp->ds->fdb_isolation)
1010 info.db.bridge.num = 0;
1012 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
1015 int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
1016 const unsigned char *addr, u16 vid)
1018 struct dsa_db db = {
1019 .type = DSA_DB_PORT,
1023 return dsa_port_host_fdb_add(dp, addr, vid, db);
1026 int dsa_port_bridge_host_fdb_add(struct dsa_port *dp,
1027 const unsigned char *addr, u16 vid)
1029 struct dsa_port *cpu_dp = dp->cpu_dp;
1030 struct dsa_db db = {
1031 .type = DSA_DB_BRIDGE,
1032 .bridge = *dp->bridge,
1036 /* Avoid a call to __dev_set_promiscuity() on the master, which
1037 * requires rtnl_lock(), since we can't guarantee that is held here,
1038 * and we can't take it either.
1040 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
1041 err = dev_uc_add(cpu_dp->master, addr);
1046 return dsa_port_host_fdb_add(dp, addr, vid, db);
1049 static int dsa_port_host_fdb_del(struct dsa_port *dp,
1050 const unsigned char *addr, u16 vid,
1053 struct dsa_notifier_fdb_info info = {
1060 if (!dp->ds->fdb_isolation)
1061 info.db.bridge.num = 0;
1063 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
1066 int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
1067 const unsigned char *addr, u16 vid)
1069 struct dsa_db db = {
1070 .type = DSA_DB_PORT,
1074 return dsa_port_host_fdb_del(dp, addr, vid, db);
1077 int dsa_port_bridge_host_fdb_del(struct dsa_port *dp,
1078 const unsigned char *addr, u16 vid)
1080 struct dsa_port *cpu_dp = dp->cpu_dp;
1081 struct dsa_db db = {
1082 .type = DSA_DB_BRIDGE,
1083 .bridge = *dp->bridge,
1087 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
1088 err = dev_uc_del(cpu_dp->master, addr);
1093 return dsa_port_host_fdb_del(dp, addr, vid, db);
1096 int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
1099 struct dsa_notifier_lag_fdb_info info = {
1104 .type = DSA_DB_BRIDGE,
1105 .bridge = *dp->bridge,
1109 if (!dp->ds->fdb_isolation)
1110 info.db.bridge.num = 0;
1112 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
1115 int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
1118 struct dsa_notifier_lag_fdb_info info = {
1123 .type = DSA_DB_BRIDGE,
1124 .bridge = *dp->bridge,
1128 if (!dp->ds->fdb_isolation)
1129 info.db.bridge.num = 0;
1131 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_DEL, &info);
1134 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
1136 struct dsa_switch *ds = dp->ds;
1137 int port = dp->index;
1139 if (!ds->ops->port_fdb_dump)
1142 return ds->ops->port_fdb_dump(ds, port, cb, data);
1145 int dsa_port_mdb_add(const struct dsa_port *dp,
1146 const struct switchdev_obj_port_mdb *mdb)
1148 struct dsa_notifier_mdb_info info = {
1152 .type = DSA_DB_BRIDGE,
1153 .bridge = *dp->bridge,
1157 if (!dp->ds->fdb_isolation)
1158 info.db.bridge.num = 0;
1160 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
1163 int dsa_port_mdb_del(const struct dsa_port *dp,
1164 const struct switchdev_obj_port_mdb *mdb)
1166 struct dsa_notifier_mdb_info info = {
1170 .type = DSA_DB_BRIDGE,
1171 .bridge = *dp->bridge,
1175 if (!dp->ds->fdb_isolation)
1176 info.db.bridge.num = 0;
1178 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
1181 static int dsa_port_host_mdb_add(const struct dsa_port *dp,
1182 const struct switchdev_obj_port_mdb *mdb,
1185 struct dsa_notifier_mdb_info info = {
1191 if (!dp->ds->fdb_isolation)
1192 info.db.bridge.num = 0;
1194 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
1197 int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
1198 const struct switchdev_obj_port_mdb *mdb)
1200 struct dsa_db db = {
1201 .type = DSA_DB_PORT,
1205 return dsa_port_host_mdb_add(dp, mdb, db);
1208 int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
1209 const struct switchdev_obj_port_mdb *mdb)
1211 struct dsa_port *cpu_dp = dp->cpu_dp;
1212 struct dsa_db db = {
1213 .type = DSA_DB_BRIDGE,
1214 .bridge = *dp->bridge,
1218 err = dev_mc_add(cpu_dp->master, mdb->addr);
1222 return dsa_port_host_mdb_add(dp, mdb, db);
1225 static int dsa_port_host_mdb_del(const struct dsa_port *dp,
1226 const struct switchdev_obj_port_mdb *mdb,
1229 struct dsa_notifier_mdb_info info = {
1235 if (!dp->ds->fdb_isolation)
1236 info.db.bridge.num = 0;
1238 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
1241 int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
1242 const struct switchdev_obj_port_mdb *mdb)
1244 struct dsa_db db = {
1245 .type = DSA_DB_PORT,
1249 return dsa_port_host_mdb_del(dp, mdb, db);
1252 int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
1253 const struct switchdev_obj_port_mdb *mdb)
1255 struct dsa_port *cpu_dp = dp->cpu_dp;
1256 struct dsa_db db = {
1257 .type = DSA_DB_BRIDGE,
1258 .bridge = *dp->bridge,
1262 err = dev_mc_del(cpu_dp->master, mdb->addr);
1266 return dsa_port_host_mdb_del(dp, mdb, db);
1269 int dsa_port_vlan_add(struct dsa_port *dp,
1270 const struct switchdev_obj_port_vlan *vlan,
1271 struct netlink_ext_ack *extack)
1273 struct dsa_notifier_vlan_info info = {
1279 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
1282 int dsa_port_vlan_del(struct dsa_port *dp,
1283 const struct switchdev_obj_port_vlan *vlan)
1285 struct dsa_notifier_vlan_info info = {
1290 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
1293 int dsa_port_host_vlan_add(struct dsa_port *dp,
1294 const struct switchdev_obj_port_vlan *vlan,
1295 struct netlink_ext_ack *extack)
1297 struct dsa_notifier_vlan_info info = {
1302 struct dsa_port *cpu_dp = dp->cpu_dp;
1305 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
1306 if (err && err != -EOPNOTSUPP)
1309 vlan_vid_add(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1314 int dsa_port_host_vlan_del(struct dsa_port *dp,
1315 const struct switchdev_obj_port_vlan *vlan)
1317 struct dsa_notifier_vlan_info info = {
1321 struct dsa_port *cpu_dp = dp->cpu_dp;
1324 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
1325 if (err && err != -EOPNOTSUPP)
1328 vlan_vid_del(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1333 int dsa_port_mrp_add(const struct dsa_port *dp,
1334 const struct switchdev_obj_mrp *mrp)
1336 struct dsa_switch *ds = dp->ds;
1338 if (!ds->ops->port_mrp_add)
1341 return ds->ops->port_mrp_add(ds, dp->index, mrp);
1344 int dsa_port_mrp_del(const struct dsa_port *dp,
1345 const struct switchdev_obj_mrp *mrp)
1347 struct dsa_switch *ds = dp->ds;
1349 if (!ds->ops->port_mrp_del)
1352 return ds->ops->port_mrp_del(ds, dp->index, mrp);
1355 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
1356 const struct switchdev_obj_ring_role_mrp *mrp)
1358 struct dsa_switch *ds = dp->ds;
1360 if (!ds->ops->port_mrp_add_ring_role)
1363 return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
1366 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
1367 const struct switchdev_obj_ring_role_mrp *mrp)
1369 struct dsa_switch *ds = dp->ds;
1371 if (!ds->ops->port_mrp_del_ring_role)
1374 return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
1377 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
1378 const struct dsa_device_ops *tag_ops)
1380 cpu_dp->rcv = tag_ops->rcv;
1381 cpu_dp->tag_ops = tag_ops;
1384 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
1386 struct device_node *phy_dn;
1387 struct phy_device *phydev;
1389 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
1393 phydev = of_phy_find_device(phy_dn);
1395 of_node_put(phy_dn);
1396 return ERR_PTR(-EPROBE_DEFER);
1399 of_node_put(phy_dn);
1403 static void dsa_port_phylink_validate(struct phylink_config *config,
1404 unsigned long *supported,
1405 struct phylink_link_state *state)
1407 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1408 struct dsa_switch *ds = dp->ds;
1410 if (!ds->ops->phylink_validate) {
1411 if (config->mac_capabilities)
1412 phylink_generic_validate(config, supported, state);
1416 ds->ops->phylink_validate(ds, dp->index, supported, state);
1419 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
1420 struct phylink_link_state *state)
1422 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1423 struct dsa_switch *ds = dp->ds;
1426 /* Only called for inband modes */
1427 if (!ds->ops->phylink_mac_link_state) {
1432 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
1434 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
1440 static struct phylink_pcs *
1441 dsa_port_phylink_mac_select_pcs(struct phylink_config *config,
1442 phy_interface_t interface)
1444 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1445 struct phylink_pcs *pcs = ERR_PTR(-EOPNOTSUPP);
1446 struct dsa_switch *ds = dp->ds;
1448 if (ds->ops->phylink_mac_select_pcs)
1449 pcs = ds->ops->phylink_mac_select_pcs(ds, dp->index, interface);
1454 static void dsa_port_phylink_mac_config(struct phylink_config *config,
1456 const struct phylink_link_state *state)
1458 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1459 struct dsa_switch *ds = dp->ds;
1461 if (!ds->ops->phylink_mac_config)
1464 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1467 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
1469 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1470 struct dsa_switch *ds = dp->ds;
1472 if (!ds->ops->phylink_mac_an_restart)
1475 ds->ops->phylink_mac_an_restart(ds, dp->index);
1478 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1480 phy_interface_t interface)
1482 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1483 struct phy_device *phydev = NULL;
1484 struct dsa_switch *ds = dp->ds;
1486 if (dsa_port_is_user(dp))
1487 phydev = dp->slave->phydev;
1489 if (!ds->ops->phylink_mac_link_down) {
1490 if (ds->ops->adjust_link && phydev)
1491 ds->ops->adjust_link(ds, dp->index, phydev);
1495 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1498 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1499 struct phy_device *phydev,
1501 phy_interface_t interface,
1502 int speed, int duplex,
1503 bool tx_pause, bool rx_pause)
1505 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1506 struct dsa_switch *ds = dp->ds;
1508 if (!ds->ops->phylink_mac_link_up) {
1509 if (ds->ops->adjust_link && phydev)
1510 ds->ops->adjust_link(ds, dp->index, phydev);
1514 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1515 speed, duplex, tx_pause, rx_pause);
1518 static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1519 .validate = dsa_port_phylink_validate,
1520 .mac_select_pcs = dsa_port_phylink_mac_select_pcs,
1521 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1522 .mac_config = dsa_port_phylink_mac_config,
1523 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1524 .mac_link_down = dsa_port_phylink_mac_link_down,
1525 .mac_link_up = dsa_port_phylink_mac_link_up,
1528 int dsa_port_phylink_create(struct dsa_port *dp)
1530 struct dsa_switch *ds = dp->ds;
1531 phy_interface_t mode;
1534 err = of_get_phy_mode(dp->dn, &mode);
1536 mode = PHY_INTERFACE_MODE_NA;
1538 /* Presence of phylink_mac_link_state or phylink_mac_an_restart is
1539 * an indicator of a legacy phylink driver.
1541 if (ds->ops->phylink_mac_link_state ||
1542 ds->ops->phylink_mac_an_restart)
1543 dp->pl_config.legacy_pre_march2020 = true;
1545 if (ds->ops->phylink_get_caps)
1546 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
1548 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn),
1549 mode, &dsa_port_phylink_mac_ops);
1550 if (IS_ERR(dp->pl)) {
1551 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1552 return PTR_ERR(dp->pl);
1558 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1560 struct dsa_switch *ds = dp->ds;
1561 struct phy_device *phydev;
1562 int port = dp->index;
1565 phydev = dsa_port_get_phy_device(dp);
1570 return PTR_ERR(phydev);
1573 err = genphy_resume(phydev);
1577 err = genphy_read_status(phydev);
1581 err = genphy_suspend(phydev);
1586 if (ds->ops->adjust_link)
1587 ds->ops->adjust_link(ds, port, phydev);
1589 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1592 put_device(&phydev->mdio.dev);
1596 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
1598 struct device_node *dn = dp->dn;
1599 struct dsa_switch *ds = dp->ds;
1600 struct phy_device *phydev;
1601 int port = dp->index;
1602 phy_interface_t mode;
1605 err = of_phy_register_fixed_link(dn);
1608 "failed to register the fixed PHY of port %d\n",
1613 phydev = of_phy_find_device(dn);
1615 err = of_get_phy_mode(dn, &mode);
1617 mode = PHY_INTERFACE_MODE_NA;
1618 phydev->interface = mode;
1620 genphy_read_status(phydev);
1622 if (ds->ops->adjust_link)
1623 ds->ops->adjust_link(ds, port, phydev);
1625 put_device(&phydev->mdio.dev);
1630 static int dsa_port_phylink_register(struct dsa_port *dp)
1632 struct dsa_switch *ds = dp->ds;
1633 struct device_node *port_dn = dp->dn;
1636 dp->pl_config.dev = ds->dev;
1637 dp->pl_config.type = PHYLINK_DEV;
1639 err = dsa_port_phylink_create(dp);
1643 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1644 if (err && err != -ENODEV) {
1645 pr_err("could not attach to PHY: %d\n", err);
1646 goto err_phy_connect;
1652 phylink_destroy(dp->pl);
1656 int dsa_port_link_register_of(struct dsa_port *dp)
1658 struct dsa_switch *ds = dp->ds;
1659 struct device_node *phy_np;
1660 int port = dp->index;
1662 if (!ds->ops->adjust_link) {
1663 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1664 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1665 if (ds->ops->phylink_mac_link_down)
1666 ds->ops->phylink_mac_link_down(ds, port,
1667 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1668 of_node_put(phy_np);
1669 return dsa_port_phylink_register(dp);
1671 of_node_put(phy_np);
1676 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1678 if (of_phy_is_fixed_link(dp->dn))
1679 return dsa_port_fixed_link_register_of(dp);
1681 return dsa_port_setup_phy_of(dp, true);
1684 void dsa_port_link_unregister_of(struct dsa_port *dp)
1686 struct dsa_switch *ds = dp->ds;
1688 if (!ds->ops->adjust_link && dp->pl) {
1690 phylink_disconnect_phy(dp->pl);
1692 phylink_destroy(dp->pl);
1697 if (of_phy_is_fixed_link(dp->dn))
1698 of_phy_deregister_fixed_link(dp->dn);
1700 dsa_port_setup_phy_of(dp, false);
1703 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1705 struct dsa_switch *ds = dp->ds;
1708 if (!ds->ops->port_hsr_join)
1713 err = ds->ops->port_hsr_join(ds, dp->index, hsr);
1720 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1722 struct dsa_switch *ds = dp->ds;
1727 if (ds->ops->port_hsr_leave) {
1728 err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
1730 dev_err(dp->ds->dev,
1731 "port %d failed to leave HSR %s: %pe\n",
1732 dp->index, hsr->name, ERR_PTR(err));
1736 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
1738 struct dsa_notifier_tag_8021q_vlan_info info = {
1744 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1746 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1749 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
1751 struct dsa_notifier_tag_8021q_vlan_info info = {
1758 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1760 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1762 dev_err(dp->ds->dev,
1763 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1764 dp->index, vid, ERR_PTR(err));