1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handling of a single switch port
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
9 #include <linux/if_bridge.h>
10 #include <linux/notifier.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_net.h>
17 * dsa_port_notify - Notify the switching fabric of changes to a port
18 * @dp: port on which change occurred
19 * @e: event, must be of type DSA_NOTIFIER_*
20 * @v: event-specific value.
22 * Notify all switches in the DSA tree that this port's switch belongs to,
23 * including this switch itself, of an event. Allows the other switches to
24 * reconfigure themselves for cross-chip operations. Can also be used to
25 * reconfigure ports without net_devices (CPU ports, DSA links) whenever
26 * a user port's state changes.
28 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
30 return dsa_tree_notify(dp->ds->dst, e, v);
33 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,
150 err = dsa_port_set_state(dp, state, do_fast_age);
152 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
155 int dsa_port_set_mst_state(struct dsa_port *dp,
156 const struct switchdev_mst_state *state,
157 struct netlink_ext_ack *extack)
159 struct dsa_switch *ds = dp->ds;
163 if (!ds->ops->port_mst_state_set)
166 err = br_mst_get_state(dsa_port_to_bridge_port(dp), state->msti,
171 err = ds->ops->port_mst_state_set(ds, dp->index, state);
175 if (!(dp->learning &&
176 (prev_state == BR_STATE_LEARNING ||
177 prev_state == BR_STATE_FORWARDING) &&
178 (state->state == BR_STATE_DISABLED ||
179 state->state == BR_STATE_BLOCKING ||
180 state->state == BR_STATE_LISTENING)))
183 err = dsa_port_msti_fast_age(dp, state->msti);
185 NL_SET_ERR_MSG_MOD(extack,
186 "Unable to flush associated VLANs");
191 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
193 struct dsa_switch *ds = dp->ds;
194 int port = dp->index;
197 if (ds->ops->port_enable) {
198 err = ds->ops->port_enable(ds, port, phy);
204 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
207 phylink_start(dp->pl);
212 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
217 err = dsa_port_enable_rt(dp, phy);
223 void dsa_port_disable_rt(struct dsa_port *dp)
225 struct dsa_switch *ds = dp->ds;
226 int port = dp->index;
229 phylink_stop(dp->pl);
232 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
234 if (ds->ops->port_disable)
235 ds->ops->port_disable(ds, port);
238 void dsa_port_disable(struct dsa_port *dp)
241 dsa_port_disable_rt(dp);
245 static void dsa_port_reset_vlan_filtering(struct dsa_port *dp,
246 struct dsa_bridge bridge)
248 struct netlink_ext_ack extack = {0};
249 bool change_vlan_filtering = false;
250 struct dsa_switch *ds = dp->ds;
254 if (ds->needs_standalone_vlan_filtering &&
255 !br_vlan_enabled(bridge.dev)) {
256 change_vlan_filtering = true;
257 vlan_filtering = true;
258 } else if (!ds->needs_standalone_vlan_filtering &&
259 br_vlan_enabled(bridge.dev)) {
260 change_vlan_filtering = true;
261 vlan_filtering = false;
264 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
265 * event for changing vlan_filtering setting upon slave ports leaving
266 * it. That is a good thing, because that lets us handle it and also
267 * handle the case where the switch's vlan_filtering setting is global
268 * (not per port). When that happens, the correct moment to trigger the
269 * vlan_filtering callback is only when the last port leaves the last
272 if (change_vlan_filtering && ds->vlan_filtering_is_global) {
273 dsa_switch_for_each_port(dp, ds) {
274 struct net_device *br = dsa_port_bridge_dev_get(dp);
276 if (br && br_vlan_enabled(br)) {
277 change_vlan_filtering = false;
283 if (!change_vlan_filtering)
286 err = dsa_port_vlan_filtering(dp, vlan_filtering, &extack);
288 dev_err(ds->dev, "port %d: %s\n", dp->index,
291 if (err && err != -EOPNOTSUPP) {
293 "port %d failed to reset VLAN filtering to %d: %pe\n",
294 dp->index, vlan_filtering, ERR_PTR(err));
298 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
299 struct netlink_ext_ack *extack)
301 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
302 BR_BCAST_FLOOD | BR_PORT_LOCKED;
303 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
306 for_each_set_bit(flag, &mask, 32) {
307 struct switchdev_brport_flags flags = {0};
309 flags.mask = BIT(flag);
311 if (br_port_flag_is_set(brport_dev, BIT(flag)))
312 flags.val = BIT(flag);
314 err = dsa_port_bridge_flags(dp, flags, extack);
315 if (err && err != -EOPNOTSUPP)
322 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
324 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
325 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
326 BR_BCAST_FLOOD | BR_PORT_LOCKED;
329 for_each_set_bit(flag, &mask, 32) {
330 struct switchdev_brport_flags flags = {0};
332 flags.mask = BIT(flag);
333 flags.val = val & BIT(flag);
335 err = dsa_port_bridge_flags(dp, flags, NULL);
336 if (err && err != -EOPNOTSUPP)
338 "failed to clear bridge port flag %lu: %pe\n",
339 flags.val, ERR_PTR(err));
343 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
344 struct netlink_ext_ack *extack)
346 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
347 struct net_device *br = dsa_port_bridge_dev_get(dp);
350 err = dsa_port_inherit_brport_flags(dp, extack);
354 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
355 if (err && err != -EOPNOTSUPP)
358 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
359 if (err && err != -EOPNOTSUPP)
362 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
363 if (err && err != -EOPNOTSUPP)
369 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp,
370 struct dsa_bridge bridge)
372 /* Configure the port for standalone mode (no address learning,
374 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
375 * when the user requests it through netlink or sysfs, but not
376 * automatically at port join or leave, so we need to handle resetting
377 * the brport flags ourselves. But we even prefer it that way, because
378 * otherwise, some setups might never get the notification they need,
379 * for example, when a port leaves a LAG that offloads the bridge,
380 * it becomes standalone, but as far as the bridge is concerned, no
383 dsa_port_clear_brport_flags(dp);
385 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
386 * so allow it to be in BR_STATE_FORWARDING to be kept functional
388 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
390 dsa_port_reset_vlan_filtering(dp, bridge);
392 /* Ageing time may be global to the switch chip, so don't change it
393 * here because we have no good reason (or value) to change it to.
397 static int dsa_port_bridge_create(struct dsa_port *dp,
398 struct net_device *br,
399 struct netlink_ext_ack *extack)
401 struct dsa_switch *ds = dp->ds;
402 struct dsa_bridge *bridge;
404 bridge = dsa_tree_bridge_find(ds->dst, br);
406 refcount_inc(&bridge->refcount);
411 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
415 refcount_set(&bridge->refcount, 1);
419 bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges);
420 if (ds->max_num_bridges && !bridge->num) {
421 NL_SET_ERR_MSG_MOD(extack,
422 "Range of offloadable bridges exceeded");
432 static void dsa_port_bridge_destroy(struct dsa_port *dp,
433 const struct net_device *br)
435 struct dsa_bridge *bridge = dp->bridge;
439 if (!refcount_dec_and_test(&bridge->refcount))
443 dsa_bridge_num_put(br, bridge->num);
448 static bool dsa_port_supports_mst(struct dsa_port *dp)
450 struct dsa_switch *ds = dp->ds;
452 return ds->ops->vlan_msti_set &&
453 ds->ops->port_mst_state_set &&
454 ds->ops->port_vlan_fast_age &&
455 dsa_port_can_configure_learning(dp);
458 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
459 struct netlink_ext_ack *extack)
461 struct dsa_notifier_bridge_info info = {
465 struct net_device *dev = dp->slave;
466 struct net_device *brport_dev;
469 if (br_mst_enabled(br) && !dsa_port_supports_mst(dp))
472 /* Here the interface is already bridged. Reflect the current
473 * configuration so that drivers can program their chips accordingly.
475 err = dsa_port_bridge_create(dp, br, extack);
479 brport_dev = dsa_port_to_bridge_port(dp);
481 info.bridge = *dp->bridge;
482 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
486 /* Drivers which support bridge TX forwarding should set this */
487 dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
489 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
490 &dsa_slave_switchdev_notifier,
491 &dsa_slave_switchdev_blocking_notifier,
492 dp->bridge->tx_fwd_offload, extack);
494 goto out_rollback_unbridge;
496 err = dsa_port_switchdev_sync_attrs(dp, extack);
498 goto out_rollback_unoffload;
502 out_rollback_unoffload:
503 switchdev_bridge_port_unoffload(brport_dev, dp,
504 &dsa_slave_switchdev_notifier,
505 &dsa_slave_switchdev_blocking_notifier);
506 dsa_flush_workqueue();
507 out_rollback_unbridge:
508 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
510 dsa_port_bridge_destroy(dp, br);
514 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
516 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
518 /* Don't try to unoffload something that is not offloaded */
522 switchdev_bridge_port_unoffload(brport_dev, dp,
523 &dsa_slave_switchdev_notifier,
524 &dsa_slave_switchdev_blocking_notifier);
526 dsa_flush_workqueue();
529 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
531 struct dsa_notifier_bridge_info info = {
536 /* If the port could not be offloaded to begin with, then
537 * there is nothing to do.
542 info.bridge = *dp->bridge;
544 /* Here the port is already unbridged. Reflect the current configuration
545 * so that drivers can program their chips accordingly.
547 dsa_port_bridge_destroy(dp, br);
549 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
552 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
553 dp->index, ERR_PTR(err));
555 dsa_port_switchdev_unsync_attrs(dp, info.bridge);
558 int dsa_port_lag_change(struct dsa_port *dp,
559 struct netdev_lag_lower_state_info *linfo)
561 struct dsa_notifier_lag_info info = {
569 /* On statically configured aggregates (e.g. loadbalance
570 * without LACP) ports will always be tx_enabled, even if the
571 * link is down. Thus we require both link_up and tx_enabled
572 * in order to include it in the tx set.
574 tx_enabled = linfo->link_up && linfo->tx_enabled;
576 if (tx_enabled == dp->lag_tx_enabled)
579 dp->lag_tx_enabled = tx_enabled;
581 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
584 static int dsa_port_lag_create(struct dsa_port *dp,
585 struct net_device *lag_dev)
587 struct dsa_switch *ds = dp->ds;
590 lag = dsa_tree_lag_find(ds->dst, lag_dev);
592 refcount_inc(&lag->refcount);
597 lag = kzalloc(sizeof(*lag), GFP_KERNEL);
601 refcount_set(&lag->refcount, 1);
602 mutex_init(&lag->fdb_lock);
603 INIT_LIST_HEAD(&lag->fdbs);
605 dsa_lag_map(ds->dst, lag);
611 static void dsa_port_lag_destroy(struct dsa_port *dp)
613 struct dsa_lag *lag = dp->lag;
616 dp->lag_tx_enabled = false;
618 if (!refcount_dec_and_test(&lag->refcount))
621 WARN_ON(!list_empty(&lag->fdbs));
622 dsa_lag_unmap(dp->ds->dst, lag);
626 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
627 struct netdev_lag_upper_info *uinfo,
628 struct netlink_ext_ack *extack)
630 struct dsa_notifier_lag_info info = {
634 struct net_device *bridge_dev;
637 err = dsa_port_lag_create(dp, lag_dev);
642 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
646 bridge_dev = netdev_master_upper_dev_get(lag_dev);
647 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
650 err = dsa_port_bridge_join(dp, bridge_dev, extack);
652 goto err_bridge_join;
657 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
659 dsa_port_lag_destroy(dp);
664 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
666 struct net_device *br = dsa_port_bridge_dev_get(dp);
669 dsa_port_pre_bridge_leave(dp, br);
672 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
674 struct net_device *br = dsa_port_bridge_dev_get(dp);
675 struct dsa_notifier_lag_info info = {
683 /* Port might have been part of a LAG that in turn was
684 * attached to a bridge.
687 dsa_port_bridge_leave(dp, br);
691 dsa_port_lag_destroy(dp);
693 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
696 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
697 dp->index, ERR_PTR(err));
700 /* Must be called under rcu_read_lock() */
701 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
703 struct netlink_ext_ack *extack)
705 struct dsa_switch *ds = dp->ds;
706 struct dsa_port *other_dp;
709 /* VLAN awareness was off, so the question is "can we turn it on".
710 * We may have had 8021q uppers, those need to go. Make sure we don't
711 * enter an inconsistent state: deny changing the VLAN awareness state
712 * as long as we have 8021q uppers.
714 if (vlan_filtering && dsa_port_is_user(dp)) {
715 struct net_device *br = dsa_port_bridge_dev_get(dp);
716 struct net_device *upper_dev, *slave = dp->slave;
717 struct list_head *iter;
719 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
720 struct bridge_vlan_info br_info;
723 if (!is_vlan_dev(upper_dev))
726 vid = vlan_dev_vlan_id(upper_dev);
728 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
729 * device, respectively the VID is not found, returning
730 * 0 means success, which is a failure for us here.
732 err = br_vlan_get_info(br, vid, &br_info);
734 NL_SET_ERR_MSG_MOD(extack,
735 "Must first remove VLAN uppers having VIDs also present in bridge");
741 if (!ds->vlan_filtering_is_global)
744 /* For cases where enabling/disabling VLAN awareness is global to the
745 * switch, we need to handle the case where multiple bridges span
746 * different ports of the same switch device and one of them has a
747 * different setting than what is being requested.
749 dsa_switch_for_each_port(other_dp, ds) {
750 struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
752 /* If it's the same bridge, it also has same
753 * vlan_filtering setting => no need to check
755 if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
758 if (br_vlan_enabled(other_br) != vlan_filtering) {
759 NL_SET_ERR_MSG_MOD(extack,
760 "VLAN filtering is a global setting");
767 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
768 struct netlink_ext_ack *extack)
770 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
771 struct dsa_switch *ds = dp->ds;
775 if (!ds->ops->port_vlan_filtering)
778 /* We are called from dsa_slave_switchdev_blocking_event(),
779 * which is not under rcu_read_lock(), unlike
780 * dsa_slave_switchdev_event().
783 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
788 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
791 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
796 if (ds->vlan_filtering_is_global) {
797 struct dsa_port *other_dp;
799 ds->vlan_filtering = vlan_filtering;
801 dsa_switch_for_each_user_port(other_dp, ds) {
802 struct net_device *slave = dp->slave;
804 /* We might be called in the unbind path, so not
805 * all slave devices might still be registered.
810 err = dsa_slave_manage_vlan_filtering(slave,
816 dp->vlan_filtering = vlan_filtering;
818 err = dsa_slave_manage_vlan_filtering(dp->slave,
827 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
829 if (ds->vlan_filtering_is_global)
830 ds->vlan_filtering = old_vlan_filtering;
832 dp->vlan_filtering = old_vlan_filtering;
837 /* This enforces legacy behavior for switch drivers which assume they can't
838 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
840 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
842 struct net_device *br = dsa_port_bridge_dev_get(dp);
843 struct dsa_switch *ds = dp->ds;
848 return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
851 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
853 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
854 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
855 struct dsa_notifier_ageing_time_info info;
858 info.ageing_time = ageing_time;
860 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
864 dp->ageing_time = ageing_time;
869 int dsa_port_mst_enable(struct dsa_port *dp, bool on,
870 struct netlink_ext_ack *extack)
872 if (on && !dsa_port_supports_mst(dp)) {
873 NL_SET_ERR_MSG_MOD(extack, "Hardware does not support MST");
880 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
881 struct switchdev_brport_flags flags,
882 struct netlink_ext_ack *extack)
884 struct dsa_switch *ds = dp->ds;
886 if (!ds->ops->port_pre_bridge_flags)
889 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
892 int dsa_port_bridge_flags(struct dsa_port *dp,
893 struct switchdev_brport_flags flags,
894 struct netlink_ext_ack *extack)
896 struct dsa_switch *ds = dp->ds;
899 if (!ds->ops->port_bridge_flags)
902 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
906 if (flags.mask & BR_LEARNING) {
907 bool learning = flags.val & BR_LEARNING;
909 if (learning == dp->learning)
912 if ((dp->learning && !learning) &&
913 (dp->stp_state == BR_STATE_LEARNING ||
914 dp->stp_state == BR_STATE_FORWARDING))
915 dsa_port_fast_age(dp);
917 dp->learning = learning;
923 void dsa_port_set_host_flood(struct dsa_port *dp, bool uc, bool mc)
925 struct dsa_switch *ds = dp->ds;
927 if (ds->ops->port_set_host_flood)
928 ds->ops->port_set_host_flood(ds, dp->index, uc, mc);
931 int dsa_port_vlan_msti(struct dsa_port *dp,
932 const struct switchdev_vlan_msti *msti)
934 struct dsa_switch *ds = dp->ds;
936 if (!ds->ops->vlan_msti_set)
939 return ds->ops->vlan_msti_set(ds, *dp->bridge, msti);
942 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu)
944 struct dsa_notifier_mtu_info info = {
949 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
952 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
955 struct dsa_notifier_fdb_info info = {
960 .type = DSA_DB_BRIDGE,
961 .bridge = *dp->bridge,
965 /* Refcounting takes bridge.num as a key, and should be global for all
966 * bridges in the absence of FDB isolation, and per bridge otherwise.
967 * Force the bridge.num to zero here in the absence of FDB isolation.
969 if (!dp->ds->fdb_isolation)
970 info.db.bridge.num = 0;
972 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
975 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
978 struct dsa_notifier_fdb_info info = {
983 .type = DSA_DB_BRIDGE,
984 .bridge = *dp->bridge,
988 if (!dp->ds->fdb_isolation)
989 info.db.bridge.num = 0;
991 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
994 static int dsa_port_host_fdb_add(struct dsa_port *dp,
995 const unsigned char *addr, u16 vid,
998 struct dsa_notifier_fdb_info info = {
1005 if (!dp->ds->fdb_isolation)
1006 info.db.bridge.num = 0;
1008 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
1011 int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
1012 const unsigned char *addr, u16 vid)
1014 struct dsa_db db = {
1015 .type = DSA_DB_PORT,
1019 return dsa_port_host_fdb_add(dp, addr, vid, db);
1022 int dsa_port_bridge_host_fdb_add(struct dsa_port *dp,
1023 const unsigned char *addr, u16 vid)
1025 struct dsa_port *cpu_dp = dp->cpu_dp;
1026 struct dsa_db db = {
1027 .type = DSA_DB_BRIDGE,
1028 .bridge = *dp->bridge,
1032 /* Avoid a call to __dev_set_promiscuity() on the master, which
1033 * requires rtnl_lock(), since we can't guarantee that is held here,
1034 * and we can't take it either.
1036 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
1037 err = dev_uc_add(cpu_dp->master, addr);
1042 return dsa_port_host_fdb_add(dp, addr, vid, db);
1045 static int dsa_port_host_fdb_del(struct dsa_port *dp,
1046 const unsigned char *addr, u16 vid,
1049 struct dsa_notifier_fdb_info info = {
1056 if (!dp->ds->fdb_isolation)
1057 info.db.bridge.num = 0;
1059 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
1062 int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
1063 const unsigned char *addr, u16 vid)
1065 struct dsa_db db = {
1066 .type = DSA_DB_PORT,
1070 return dsa_port_host_fdb_del(dp, addr, vid, db);
1073 int dsa_port_bridge_host_fdb_del(struct dsa_port *dp,
1074 const unsigned char *addr, u16 vid)
1076 struct dsa_port *cpu_dp = dp->cpu_dp;
1077 struct dsa_db db = {
1078 .type = DSA_DB_BRIDGE,
1079 .bridge = *dp->bridge,
1083 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
1084 err = dev_uc_del(cpu_dp->master, addr);
1089 return dsa_port_host_fdb_del(dp, addr, vid, db);
1092 int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
1095 struct dsa_notifier_lag_fdb_info info = {
1100 .type = DSA_DB_BRIDGE,
1101 .bridge = *dp->bridge,
1105 if (!dp->ds->fdb_isolation)
1106 info.db.bridge.num = 0;
1108 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
1111 int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
1114 struct dsa_notifier_lag_fdb_info info = {
1119 .type = DSA_DB_BRIDGE,
1120 .bridge = *dp->bridge,
1124 if (!dp->ds->fdb_isolation)
1125 info.db.bridge.num = 0;
1127 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_DEL, &info);
1130 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
1132 struct dsa_switch *ds = dp->ds;
1133 int port = dp->index;
1135 if (!ds->ops->port_fdb_dump)
1138 return ds->ops->port_fdb_dump(ds, port, cb, data);
1141 int dsa_port_mdb_add(const struct dsa_port *dp,
1142 const struct switchdev_obj_port_mdb *mdb)
1144 struct dsa_notifier_mdb_info info = {
1148 .type = DSA_DB_BRIDGE,
1149 .bridge = *dp->bridge,
1153 if (!dp->ds->fdb_isolation)
1154 info.db.bridge.num = 0;
1156 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
1159 int dsa_port_mdb_del(const struct dsa_port *dp,
1160 const struct switchdev_obj_port_mdb *mdb)
1162 struct dsa_notifier_mdb_info info = {
1166 .type = DSA_DB_BRIDGE,
1167 .bridge = *dp->bridge,
1171 if (!dp->ds->fdb_isolation)
1172 info.db.bridge.num = 0;
1174 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
1177 static int dsa_port_host_mdb_add(const struct dsa_port *dp,
1178 const struct switchdev_obj_port_mdb *mdb,
1181 struct dsa_notifier_mdb_info info = {
1187 if (!dp->ds->fdb_isolation)
1188 info.db.bridge.num = 0;
1190 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
1193 int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
1194 const struct switchdev_obj_port_mdb *mdb)
1196 struct dsa_db db = {
1197 .type = DSA_DB_PORT,
1201 return dsa_port_host_mdb_add(dp, mdb, db);
1204 int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
1205 const struct switchdev_obj_port_mdb *mdb)
1207 struct dsa_port *cpu_dp = dp->cpu_dp;
1208 struct dsa_db db = {
1209 .type = DSA_DB_BRIDGE,
1210 .bridge = *dp->bridge,
1214 err = dev_mc_add(cpu_dp->master, mdb->addr);
1218 return dsa_port_host_mdb_add(dp, mdb, db);
1221 static int dsa_port_host_mdb_del(const struct dsa_port *dp,
1222 const struct switchdev_obj_port_mdb *mdb,
1225 struct dsa_notifier_mdb_info info = {
1231 if (!dp->ds->fdb_isolation)
1232 info.db.bridge.num = 0;
1234 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
1237 int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
1238 const struct switchdev_obj_port_mdb *mdb)
1240 struct dsa_db db = {
1241 .type = DSA_DB_PORT,
1245 return dsa_port_host_mdb_del(dp, mdb, db);
1248 int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
1249 const struct switchdev_obj_port_mdb *mdb)
1251 struct dsa_port *cpu_dp = dp->cpu_dp;
1252 struct dsa_db db = {
1253 .type = DSA_DB_BRIDGE,
1254 .bridge = *dp->bridge,
1258 err = dev_mc_del(cpu_dp->master, mdb->addr);
1262 return dsa_port_host_mdb_del(dp, mdb, db);
1265 int dsa_port_vlan_add(struct dsa_port *dp,
1266 const struct switchdev_obj_port_vlan *vlan,
1267 struct netlink_ext_ack *extack)
1269 struct dsa_notifier_vlan_info info = {
1275 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
1278 int dsa_port_vlan_del(struct dsa_port *dp,
1279 const struct switchdev_obj_port_vlan *vlan)
1281 struct dsa_notifier_vlan_info info = {
1286 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
1289 int dsa_port_host_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 = {
1298 struct dsa_port *cpu_dp = dp->cpu_dp;
1301 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
1302 if (err && err != -EOPNOTSUPP)
1305 vlan_vid_add(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1310 int dsa_port_host_vlan_del(struct dsa_port *dp,
1311 const struct switchdev_obj_port_vlan *vlan)
1313 struct dsa_notifier_vlan_info info = {
1317 struct dsa_port *cpu_dp = dp->cpu_dp;
1320 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
1321 if (err && err != -EOPNOTSUPP)
1324 vlan_vid_del(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1329 int dsa_port_mrp_add(const struct dsa_port *dp,
1330 const struct switchdev_obj_mrp *mrp)
1332 struct dsa_switch *ds = dp->ds;
1334 if (!ds->ops->port_mrp_add)
1337 return ds->ops->port_mrp_add(ds, dp->index, mrp);
1340 int dsa_port_mrp_del(const struct dsa_port *dp,
1341 const struct switchdev_obj_mrp *mrp)
1343 struct dsa_switch *ds = dp->ds;
1345 if (!ds->ops->port_mrp_del)
1348 return ds->ops->port_mrp_del(ds, dp->index, mrp);
1351 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
1352 const struct switchdev_obj_ring_role_mrp *mrp)
1354 struct dsa_switch *ds = dp->ds;
1356 if (!ds->ops->port_mrp_add_ring_role)
1359 return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
1362 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
1363 const struct switchdev_obj_ring_role_mrp *mrp)
1365 struct dsa_switch *ds = dp->ds;
1367 if (!ds->ops->port_mrp_del_ring_role)
1370 return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
1373 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
1374 const struct dsa_device_ops *tag_ops)
1376 cpu_dp->rcv = tag_ops->rcv;
1377 cpu_dp->tag_ops = tag_ops;
1380 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
1382 struct device_node *phy_dn;
1383 struct phy_device *phydev;
1385 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
1389 phydev = of_phy_find_device(phy_dn);
1391 of_node_put(phy_dn);
1392 return ERR_PTR(-EPROBE_DEFER);
1395 of_node_put(phy_dn);
1399 static void dsa_port_phylink_validate(struct phylink_config *config,
1400 unsigned long *supported,
1401 struct phylink_link_state *state)
1403 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1404 struct dsa_switch *ds = dp->ds;
1406 if (!ds->ops->phylink_validate) {
1407 if (config->mac_capabilities)
1408 phylink_generic_validate(config, supported, state);
1412 ds->ops->phylink_validate(ds, dp->index, supported, state);
1415 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
1416 struct phylink_link_state *state)
1418 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1419 struct dsa_switch *ds = dp->ds;
1422 /* Only called for inband modes */
1423 if (!ds->ops->phylink_mac_link_state) {
1428 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
1430 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
1436 static struct phylink_pcs *
1437 dsa_port_phylink_mac_select_pcs(struct phylink_config *config,
1438 phy_interface_t interface)
1440 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1441 struct phylink_pcs *pcs = ERR_PTR(-EOPNOTSUPP);
1442 struct dsa_switch *ds = dp->ds;
1444 if (ds->ops->phylink_mac_select_pcs)
1445 pcs = ds->ops->phylink_mac_select_pcs(ds, dp->index, interface);
1450 static void dsa_port_phylink_mac_config(struct phylink_config *config,
1452 const struct phylink_link_state *state)
1454 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1455 struct dsa_switch *ds = dp->ds;
1457 if (!ds->ops->phylink_mac_config)
1460 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1463 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
1465 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1466 struct dsa_switch *ds = dp->ds;
1468 if (!ds->ops->phylink_mac_an_restart)
1471 ds->ops->phylink_mac_an_restart(ds, dp->index);
1474 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1476 phy_interface_t interface)
1478 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1479 struct phy_device *phydev = NULL;
1480 struct dsa_switch *ds = dp->ds;
1482 if (dsa_port_is_user(dp))
1483 phydev = dp->slave->phydev;
1485 if (!ds->ops->phylink_mac_link_down) {
1486 if (ds->ops->adjust_link && phydev)
1487 ds->ops->adjust_link(ds, dp->index, phydev);
1491 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1494 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1495 struct phy_device *phydev,
1497 phy_interface_t interface,
1498 int speed, int duplex,
1499 bool tx_pause, bool rx_pause)
1501 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1502 struct dsa_switch *ds = dp->ds;
1504 if (!ds->ops->phylink_mac_link_up) {
1505 if (ds->ops->adjust_link && phydev)
1506 ds->ops->adjust_link(ds, dp->index, phydev);
1510 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1511 speed, duplex, tx_pause, rx_pause);
1514 static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1515 .validate = dsa_port_phylink_validate,
1516 .mac_select_pcs = dsa_port_phylink_mac_select_pcs,
1517 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1518 .mac_config = dsa_port_phylink_mac_config,
1519 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1520 .mac_link_down = dsa_port_phylink_mac_link_down,
1521 .mac_link_up = dsa_port_phylink_mac_link_up,
1524 int dsa_port_phylink_create(struct dsa_port *dp)
1526 struct dsa_switch *ds = dp->ds;
1527 phy_interface_t mode;
1530 err = of_get_phy_mode(dp->dn, &mode);
1532 mode = PHY_INTERFACE_MODE_NA;
1534 /* Presence of phylink_mac_link_state or phylink_mac_an_restart is
1535 * an indicator of a legacy phylink driver.
1537 if (ds->ops->phylink_mac_link_state ||
1538 ds->ops->phylink_mac_an_restart)
1539 dp->pl_config.legacy_pre_march2020 = true;
1541 if (ds->ops->phylink_get_caps)
1542 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
1544 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn),
1545 mode, &dsa_port_phylink_mac_ops);
1546 if (IS_ERR(dp->pl)) {
1547 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1548 return PTR_ERR(dp->pl);
1554 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1556 struct dsa_switch *ds = dp->ds;
1557 struct phy_device *phydev;
1558 int port = dp->index;
1561 phydev = dsa_port_get_phy_device(dp);
1566 return PTR_ERR(phydev);
1569 err = genphy_resume(phydev);
1573 err = genphy_read_status(phydev);
1577 err = genphy_suspend(phydev);
1582 if (ds->ops->adjust_link)
1583 ds->ops->adjust_link(ds, port, phydev);
1585 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1588 put_device(&phydev->mdio.dev);
1592 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
1594 struct device_node *dn = dp->dn;
1595 struct dsa_switch *ds = dp->ds;
1596 struct phy_device *phydev;
1597 int port = dp->index;
1598 phy_interface_t mode;
1601 err = of_phy_register_fixed_link(dn);
1604 "failed to register the fixed PHY of port %d\n",
1609 phydev = of_phy_find_device(dn);
1611 err = of_get_phy_mode(dn, &mode);
1613 mode = PHY_INTERFACE_MODE_NA;
1614 phydev->interface = mode;
1616 genphy_read_status(phydev);
1618 if (ds->ops->adjust_link)
1619 ds->ops->adjust_link(ds, port, phydev);
1621 put_device(&phydev->mdio.dev);
1626 static int dsa_port_phylink_register(struct dsa_port *dp)
1628 struct dsa_switch *ds = dp->ds;
1629 struct device_node *port_dn = dp->dn;
1632 dp->pl_config.dev = ds->dev;
1633 dp->pl_config.type = PHYLINK_DEV;
1635 err = dsa_port_phylink_create(dp);
1639 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1640 if (err && err != -ENODEV) {
1641 pr_err("could not attach to PHY: %d\n", err);
1642 goto err_phy_connect;
1648 phylink_destroy(dp->pl);
1652 int dsa_port_link_register_of(struct dsa_port *dp)
1654 struct dsa_switch *ds = dp->ds;
1655 struct device_node *phy_np;
1656 int port = dp->index;
1658 if (!ds->ops->adjust_link) {
1659 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1660 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1661 if (ds->ops->phylink_mac_link_down)
1662 ds->ops->phylink_mac_link_down(ds, port,
1663 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1664 of_node_put(phy_np);
1665 return dsa_port_phylink_register(dp);
1667 of_node_put(phy_np);
1672 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1674 if (of_phy_is_fixed_link(dp->dn))
1675 return dsa_port_fixed_link_register_of(dp);
1677 return dsa_port_setup_phy_of(dp, true);
1680 void dsa_port_link_unregister_of(struct dsa_port *dp)
1682 struct dsa_switch *ds = dp->ds;
1684 if (!ds->ops->adjust_link && dp->pl) {
1686 phylink_disconnect_phy(dp->pl);
1688 phylink_destroy(dp->pl);
1693 if (of_phy_is_fixed_link(dp->dn))
1694 of_phy_deregister_fixed_link(dp->dn);
1696 dsa_port_setup_phy_of(dp, false);
1699 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1701 struct dsa_switch *ds = dp->ds;
1704 if (!ds->ops->port_hsr_join)
1709 err = ds->ops->port_hsr_join(ds, dp->index, hsr);
1716 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1718 struct dsa_switch *ds = dp->ds;
1723 if (ds->ops->port_hsr_leave) {
1724 err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
1726 dev_err(dp->ds->dev,
1727 "port %d failed to leave HSR %s: %pe\n",
1728 dp->index, hsr->name, ERR_PTR(err));
1732 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
1734 struct dsa_notifier_tag_8021q_vlan_info info = {
1740 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1742 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1745 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
1747 struct dsa_notifier_tag_8021q_vlan_info info = {
1754 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1756 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1758 dev_err(dp->ds->dev,
1759 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1760 dp->index, vid, ERR_PTR(err));