1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handling of a single switch port
5 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
9 #include <linux/if_bridge.h>
10 #include <linux/notifier.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_net.h>
17 * dsa_port_notify - Notify the switching fabric of changes to a port
18 * @dp: port on which change occurred
19 * @e: event, must be of type DSA_NOTIFIER_*
20 * @v: event-specific value.
22 * Notify all switches in the DSA tree that this port's switch belongs to,
23 * including this switch itself, of an event. Allows the other switches to
24 * reconfigure themselves for cross-chip operations. Can also be used to
25 * reconfigure ports without net_devices (CPU ports, DSA links) whenever
26 * a user port's state changes.
28 static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
30 return dsa_tree_notify(dp->ds->dst, e, v);
33 static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port *dp)
35 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
36 struct switchdev_notifier_fdb_info info = {
41 /* When the port becomes standalone it has already left the bridge.
42 * Don't notify the bridge in that case.
47 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
48 brport_dev, &info.info, NULL);
51 static void dsa_port_fast_age(const struct dsa_port *dp)
53 struct dsa_switch *ds = dp->ds;
55 if (!ds->ops->port_fast_age)
58 ds->ops->port_fast_age(ds, dp->index);
60 dsa_port_notify_bridge_fdb_flush(dp);
63 static bool dsa_port_can_configure_learning(struct dsa_port *dp)
65 struct switchdev_brport_flags flags = {
68 struct dsa_switch *ds = dp->ds;
71 if (!ds->ops->port_bridge_flags || !ds->ops->port_pre_bridge_flags)
74 err = ds->ops->port_pre_bridge_flags(ds, dp->index, flags, NULL);
78 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
80 struct dsa_switch *ds = dp->ds;
83 if (!ds->ops->port_stp_state_set)
86 ds->ops->port_stp_state_set(ds, port, state);
88 if (!dsa_port_can_configure_learning(dp) ||
89 (do_fast_age && dp->learning)) {
90 /* Fast age FDB entries or flush appropriate forwarding database
91 * for the given port, if we are moving it from Learning or
92 * Forwarding state, to Disabled or Blocking or Listening state.
93 * Ports that were standalone before the STP state change don't
94 * need to fast age the FDB, since address learning is off in
98 if ((dp->stp_state == BR_STATE_LEARNING ||
99 dp->stp_state == BR_STATE_FORWARDING) &&
100 (state == BR_STATE_DISABLED ||
101 state == BR_STATE_BLOCKING ||
102 state == BR_STATE_LISTENING))
103 dsa_port_fast_age(dp);
106 dp->stp_state = state;
111 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
116 err = dsa_port_set_state(dp, state, do_fast_age);
118 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
121 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
123 struct dsa_switch *ds = dp->ds;
124 int port = dp->index;
127 if (ds->ops->port_enable) {
128 err = ds->ops->port_enable(ds, port, phy);
134 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
137 phylink_start(dp->pl);
142 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
147 err = dsa_port_enable_rt(dp, phy);
153 void dsa_port_disable_rt(struct dsa_port *dp)
155 struct dsa_switch *ds = dp->ds;
156 int port = dp->index;
159 phylink_stop(dp->pl);
162 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
164 if (ds->ops->port_disable)
165 ds->ops->port_disable(ds, port);
168 void dsa_port_disable(struct dsa_port *dp)
171 dsa_port_disable_rt(dp);
175 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
176 struct netlink_ext_ack *extack)
178 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
179 BR_BCAST_FLOOD | BR_PORT_LOCKED;
180 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
183 for_each_set_bit(flag, &mask, 32) {
184 struct switchdev_brport_flags flags = {0};
186 flags.mask = BIT(flag);
188 if (br_port_flag_is_set(brport_dev, BIT(flag)))
189 flags.val = BIT(flag);
191 err = dsa_port_bridge_flags(dp, flags, extack);
192 if (err && err != -EOPNOTSUPP)
199 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
201 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
202 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
203 BR_BCAST_FLOOD | BR_PORT_LOCKED;
206 for_each_set_bit(flag, &mask, 32) {
207 struct switchdev_brport_flags flags = {0};
209 flags.mask = BIT(flag);
210 flags.val = val & BIT(flag);
212 err = dsa_port_bridge_flags(dp, flags, NULL);
213 if (err && err != -EOPNOTSUPP)
215 "failed to clear bridge port flag %lu: %pe\n",
216 flags.val, ERR_PTR(err));
220 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
221 struct netlink_ext_ack *extack)
223 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
224 struct net_device *br = dsa_port_bridge_dev_get(dp);
227 err = dsa_port_inherit_brport_flags(dp, extack);
231 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
232 if (err && err != -EOPNOTSUPP)
235 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
236 if (err && err != -EOPNOTSUPP)
239 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
240 if (err && err != -EOPNOTSUPP)
246 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp)
248 /* Configure the port for standalone mode (no address learning,
250 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
251 * when the user requests it through netlink or sysfs, but not
252 * automatically at port join or leave, so we need to handle resetting
253 * the brport flags ourselves. But we even prefer it that way, because
254 * otherwise, some setups might never get the notification they need,
255 * for example, when a port leaves a LAG that offloads the bridge,
256 * it becomes standalone, but as far as the bridge is concerned, no
259 dsa_port_clear_brport_flags(dp);
261 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
262 * so allow it to be in BR_STATE_FORWARDING to be kept functional
264 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
266 /* VLAN filtering is handled by dsa_switch_bridge_leave */
268 /* Ageing time may be global to the switch chip, so don't change it
269 * here because we have no good reason (or value) to change it to.
273 static int dsa_port_bridge_create(struct dsa_port *dp,
274 struct net_device *br,
275 struct netlink_ext_ack *extack)
277 struct dsa_switch *ds = dp->ds;
278 struct dsa_bridge *bridge;
280 bridge = dsa_tree_bridge_find(ds->dst, br);
282 refcount_inc(&bridge->refcount);
287 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
291 refcount_set(&bridge->refcount, 1);
295 bridge->num = dsa_bridge_num_get(br, ds->max_num_bridges);
296 if (ds->max_num_bridges && !bridge->num) {
297 NL_SET_ERR_MSG_MOD(extack,
298 "Range of offloadable bridges exceeded");
308 static void dsa_port_bridge_destroy(struct dsa_port *dp,
309 const struct net_device *br)
311 struct dsa_bridge *bridge = dp->bridge;
315 if (!refcount_dec_and_test(&bridge->refcount))
319 dsa_bridge_num_put(br, bridge->num);
324 static bool dsa_port_supports_mst(struct dsa_port *dp)
326 return dsa_port_can_configure_learning(dp);
329 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
330 struct netlink_ext_ack *extack)
332 struct dsa_notifier_bridge_info info = {
333 .tree_index = dp->ds->dst->index,
334 .sw_index = dp->ds->index,
338 struct net_device *dev = dp->slave;
339 struct net_device *brport_dev;
342 if (br_mst_enabled(br) && !dsa_port_supports_mst(dp))
345 /* Here the interface is already bridged. Reflect the current
346 * configuration so that drivers can program their chips accordingly.
348 err = dsa_port_bridge_create(dp, br, extack);
352 brport_dev = dsa_port_to_bridge_port(dp);
354 info.bridge = *dp->bridge;
355 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
359 /* Drivers which support bridge TX forwarding should set this */
360 dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
362 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
363 &dsa_slave_switchdev_notifier,
364 &dsa_slave_switchdev_blocking_notifier,
365 dp->bridge->tx_fwd_offload, extack);
367 goto out_rollback_unbridge;
369 err = dsa_port_switchdev_sync_attrs(dp, extack);
371 goto out_rollback_unoffload;
375 out_rollback_unoffload:
376 switchdev_bridge_port_unoffload(brport_dev, dp,
377 &dsa_slave_switchdev_notifier,
378 &dsa_slave_switchdev_blocking_notifier);
379 out_rollback_unbridge:
380 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
382 dsa_port_bridge_destroy(dp, br);
386 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
388 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
390 /* Don't try to unoffload something that is not offloaded */
394 switchdev_bridge_port_unoffload(brport_dev, dp,
395 &dsa_slave_switchdev_notifier,
396 &dsa_slave_switchdev_blocking_notifier);
398 dsa_flush_workqueue();
401 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
403 struct dsa_notifier_bridge_info info = {
404 .tree_index = dp->ds->dst->index,
405 .sw_index = dp->ds->index,
410 /* If the port could not be offloaded to begin with, then
411 * there is nothing to do.
416 info.bridge = *dp->bridge;
418 /* Here the port is already unbridged. Reflect the current configuration
419 * so that drivers can program their chips accordingly.
421 dsa_port_bridge_destroy(dp, br);
423 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
426 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
427 dp->index, ERR_PTR(err));
429 dsa_port_switchdev_unsync_attrs(dp);
432 int dsa_port_lag_change(struct dsa_port *dp,
433 struct netdev_lag_lower_state_info *linfo)
435 struct dsa_notifier_lag_info info = {
436 .sw_index = dp->ds->index,
444 /* On statically configured aggregates (e.g. loadbalance
445 * without LACP) ports will always be tx_enabled, even if the
446 * link is down. Thus we require both link_up and tx_enabled
447 * in order to include it in the tx set.
449 tx_enabled = linfo->link_up && linfo->tx_enabled;
451 if (tx_enabled == dp->lag_tx_enabled)
454 dp->lag_tx_enabled = tx_enabled;
456 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
459 static int dsa_port_lag_create(struct dsa_port *dp,
460 struct net_device *lag_dev)
462 struct dsa_switch *ds = dp->ds;
465 lag = dsa_tree_lag_find(ds->dst, lag_dev);
467 refcount_inc(&lag->refcount);
472 lag = kzalloc(sizeof(*lag), GFP_KERNEL);
476 refcount_set(&lag->refcount, 1);
477 mutex_init(&lag->fdb_lock);
478 INIT_LIST_HEAD(&lag->fdbs);
480 dsa_lag_map(ds->dst, lag);
486 static void dsa_port_lag_destroy(struct dsa_port *dp)
488 struct dsa_lag *lag = dp->lag;
491 dp->lag_tx_enabled = false;
493 if (!refcount_dec_and_test(&lag->refcount))
496 WARN_ON(!list_empty(&lag->fdbs));
497 dsa_lag_unmap(dp->ds->dst, lag);
501 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
502 struct netdev_lag_upper_info *uinfo,
503 struct netlink_ext_ack *extack)
505 struct dsa_notifier_lag_info info = {
506 .sw_index = dp->ds->index,
510 struct net_device *bridge_dev;
513 err = dsa_port_lag_create(dp, lag_dev);
518 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
522 bridge_dev = netdev_master_upper_dev_get(lag_dev);
523 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
526 err = dsa_port_bridge_join(dp, bridge_dev, extack);
528 goto err_bridge_join;
533 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
535 dsa_port_lag_destroy(dp);
540 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
542 struct net_device *br = dsa_port_bridge_dev_get(dp);
545 dsa_port_pre_bridge_leave(dp, br);
548 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
550 struct net_device *br = dsa_port_bridge_dev_get(dp);
551 struct dsa_notifier_lag_info info = {
552 .sw_index = dp->ds->index,
560 /* Port might have been part of a LAG that in turn was
561 * attached to a bridge.
564 dsa_port_bridge_leave(dp, br);
568 dsa_port_lag_destroy(dp);
570 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
573 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
574 dp->index, ERR_PTR(err));
577 /* Must be called under rcu_read_lock() */
578 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
580 struct netlink_ext_ack *extack)
582 struct dsa_switch *ds = dp->ds;
583 struct dsa_port *other_dp;
586 /* VLAN awareness was off, so the question is "can we turn it on".
587 * We may have had 8021q uppers, those need to go. Make sure we don't
588 * enter an inconsistent state: deny changing the VLAN awareness state
589 * as long as we have 8021q uppers.
591 if (vlan_filtering && dsa_port_is_user(dp)) {
592 struct net_device *br = dsa_port_bridge_dev_get(dp);
593 struct net_device *upper_dev, *slave = dp->slave;
594 struct list_head *iter;
596 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
597 struct bridge_vlan_info br_info;
600 if (!is_vlan_dev(upper_dev))
603 vid = vlan_dev_vlan_id(upper_dev);
605 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
606 * device, respectively the VID is not found, returning
607 * 0 means success, which is a failure for us here.
609 err = br_vlan_get_info(br, vid, &br_info);
611 NL_SET_ERR_MSG_MOD(extack,
612 "Must first remove VLAN uppers having VIDs also present in bridge");
618 if (!ds->vlan_filtering_is_global)
621 /* For cases where enabling/disabling VLAN awareness is global to the
622 * switch, we need to handle the case where multiple bridges span
623 * different ports of the same switch device and one of them has a
624 * different setting than what is being requested.
626 dsa_switch_for_each_port(other_dp, ds) {
627 struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
629 /* If it's the same bridge, it also has same
630 * vlan_filtering setting => no need to check
632 if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
635 if (br_vlan_enabled(other_br) != vlan_filtering) {
636 NL_SET_ERR_MSG_MOD(extack,
637 "VLAN filtering is a global setting");
644 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
645 struct netlink_ext_ack *extack)
647 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
648 struct dsa_switch *ds = dp->ds;
652 if (!ds->ops->port_vlan_filtering)
655 /* We are called from dsa_slave_switchdev_blocking_event(),
656 * which is not under rcu_read_lock(), unlike
657 * dsa_slave_switchdev_event().
660 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
665 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
668 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
673 if (ds->vlan_filtering_is_global) {
674 struct dsa_port *other_dp;
676 ds->vlan_filtering = vlan_filtering;
678 dsa_switch_for_each_user_port(other_dp, ds) {
679 struct net_device *slave = dp->slave;
681 /* We might be called in the unbind path, so not
682 * all slave devices might still be registered.
687 err = dsa_slave_manage_vlan_filtering(slave,
693 dp->vlan_filtering = vlan_filtering;
695 err = dsa_slave_manage_vlan_filtering(dp->slave,
704 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
706 if (ds->vlan_filtering_is_global)
707 ds->vlan_filtering = old_vlan_filtering;
709 dp->vlan_filtering = old_vlan_filtering;
714 /* This enforces legacy behavior for switch drivers which assume they can't
715 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
717 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
719 struct net_device *br = dsa_port_bridge_dev_get(dp);
720 struct dsa_switch *ds = dp->ds;
725 return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
728 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
730 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
731 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
732 struct dsa_notifier_ageing_time_info info;
735 info.ageing_time = ageing_time;
737 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
741 dp->ageing_time = ageing_time;
746 int dsa_port_mst_enable(struct dsa_port *dp, bool on,
747 struct netlink_ext_ack *extack)
752 if (!dsa_port_supports_mst(dp)) {
753 NL_SET_ERR_MSG_MOD(extack, "Hardware does not support MST");
760 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
761 struct switchdev_brport_flags flags,
762 struct netlink_ext_ack *extack)
764 struct dsa_switch *ds = dp->ds;
766 if (!ds->ops->port_pre_bridge_flags)
769 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
772 int dsa_port_bridge_flags(struct dsa_port *dp,
773 struct switchdev_brport_flags flags,
774 struct netlink_ext_ack *extack)
776 struct dsa_switch *ds = dp->ds;
779 if (!ds->ops->port_bridge_flags)
782 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
786 if (flags.mask & BR_LEARNING) {
787 bool learning = flags.val & BR_LEARNING;
789 if (learning == dp->learning)
792 if ((dp->learning && !learning) &&
793 (dp->stp_state == BR_STATE_LEARNING ||
794 dp->stp_state == BR_STATE_FORWARDING))
795 dsa_port_fast_age(dp);
797 dp->learning = learning;
803 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
806 struct dsa_notifier_mtu_info info = {
807 .sw_index = dp->ds->index,
808 .targeted_match = targeted_match,
813 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
816 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
819 struct dsa_notifier_fdb_info info = {
820 .sw_index = dp->ds->index,
825 .type = DSA_DB_BRIDGE,
826 .bridge = *dp->bridge,
830 /* Refcounting takes bridge.num as a key, and should be global for all
831 * bridges in the absence of FDB isolation, and per bridge otherwise.
832 * Force the bridge.num to zero here in the absence of FDB isolation.
834 if (!dp->ds->fdb_isolation)
835 info.db.bridge.num = 0;
837 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
840 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
843 struct dsa_notifier_fdb_info info = {
844 .sw_index = dp->ds->index,
849 .type = DSA_DB_BRIDGE,
850 .bridge = *dp->bridge,
854 if (!dp->ds->fdb_isolation)
855 info.db.bridge.num = 0;
857 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
860 static int dsa_port_host_fdb_add(struct dsa_port *dp,
861 const unsigned char *addr, u16 vid,
864 struct dsa_notifier_fdb_info info = {
865 .sw_index = dp->ds->index,
872 if (!dp->ds->fdb_isolation)
873 info.db.bridge.num = 0;
875 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
878 int dsa_port_standalone_host_fdb_add(struct dsa_port *dp,
879 const unsigned char *addr, u16 vid)
886 return dsa_port_host_fdb_add(dp, addr, vid, db);
889 int dsa_port_bridge_host_fdb_add(struct dsa_port *dp,
890 const unsigned char *addr, u16 vid)
892 struct dsa_port *cpu_dp = dp->cpu_dp;
894 .type = DSA_DB_BRIDGE,
895 .bridge = *dp->bridge,
899 /* Avoid a call to __dev_set_promiscuity() on the master, which
900 * requires rtnl_lock(), since we can't guarantee that is held here,
901 * and we can't take it either.
903 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
904 err = dev_uc_add(cpu_dp->master, addr);
909 return dsa_port_host_fdb_add(dp, addr, vid, db);
912 static int dsa_port_host_fdb_del(struct dsa_port *dp,
913 const unsigned char *addr, u16 vid,
916 struct dsa_notifier_fdb_info info = {
917 .sw_index = dp->ds->index,
924 if (!dp->ds->fdb_isolation)
925 info.db.bridge.num = 0;
927 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
930 int dsa_port_standalone_host_fdb_del(struct dsa_port *dp,
931 const unsigned char *addr, u16 vid)
938 return dsa_port_host_fdb_del(dp, addr, vid, db);
941 int dsa_port_bridge_host_fdb_del(struct dsa_port *dp,
942 const unsigned char *addr, u16 vid)
944 struct dsa_port *cpu_dp = dp->cpu_dp;
946 .type = DSA_DB_BRIDGE,
947 .bridge = *dp->bridge,
951 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
952 err = dev_uc_del(cpu_dp->master, addr);
957 return dsa_port_host_fdb_del(dp, addr, vid, db);
960 int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
963 struct dsa_notifier_lag_fdb_info info = {
968 .type = DSA_DB_BRIDGE,
969 .bridge = *dp->bridge,
973 if (!dp->ds->fdb_isolation)
974 info.db.bridge.num = 0;
976 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
979 int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
982 struct dsa_notifier_lag_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_LAG_FDB_DEL, &info);
998 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
1000 struct dsa_switch *ds = dp->ds;
1001 int port = dp->index;
1003 if (!ds->ops->port_fdb_dump)
1006 return ds->ops->port_fdb_dump(ds, port, cb, data);
1009 int dsa_port_mdb_add(const struct dsa_port *dp,
1010 const struct switchdev_obj_port_mdb *mdb)
1012 struct dsa_notifier_mdb_info info = {
1013 .sw_index = dp->ds->index,
1017 .type = DSA_DB_BRIDGE,
1018 .bridge = *dp->bridge,
1022 if (!dp->ds->fdb_isolation)
1023 info.db.bridge.num = 0;
1025 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
1028 int dsa_port_mdb_del(const struct dsa_port *dp,
1029 const struct switchdev_obj_port_mdb *mdb)
1031 struct dsa_notifier_mdb_info info = {
1032 .sw_index = dp->ds->index,
1036 .type = DSA_DB_BRIDGE,
1037 .bridge = *dp->bridge,
1041 if (!dp->ds->fdb_isolation)
1042 info.db.bridge.num = 0;
1044 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
1047 static int dsa_port_host_mdb_add(const struct dsa_port *dp,
1048 const struct switchdev_obj_port_mdb *mdb,
1051 struct dsa_notifier_mdb_info info = {
1052 .sw_index = dp->ds->index,
1058 if (!dp->ds->fdb_isolation)
1059 info.db.bridge.num = 0;
1061 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
1064 int dsa_port_standalone_host_mdb_add(const struct dsa_port *dp,
1065 const struct switchdev_obj_port_mdb *mdb)
1067 struct dsa_db db = {
1068 .type = DSA_DB_PORT,
1072 return dsa_port_host_mdb_add(dp, mdb, db);
1075 int dsa_port_bridge_host_mdb_add(const struct dsa_port *dp,
1076 const struct switchdev_obj_port_mdb *mdb)
1078 struct dsa_port *cpu_dp = dp->cpu_dp;
1079 struct dsa_db db = {
1080 .type = DSA_DB_BRIDGE,
1081 .bridge = *dp->bridge,
1085 err = dev_mc_add(cpu_dp->master, mdb->addr);
1089 return dsa_port_host_mdb_add(dp, mdb, db);
1092 static int dsa_port_host_mdb_del(const struct dsa_port *dp,
1093 const struct switchdev_obj_port_mdb *mdb,
1096 struct dsa_notifier_mdb_info info = {
1097 .sw_index = dp->ds->index,
1103 if (!dp->ds->fdb_isolation)
1104 info.db.bridge.num = 0;
1106 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
1109 int dsa_port_standalone_host_mdb_del(const struct dsa_port *dp,
1110 const struct switchdev_obj_port_mdb *mdb)
1112 struct dsa_db db = {
1113 .type = DSA_DB_PORT,
1117 return dsa_port_host_mdb_del(dp, mdb, db);
1120 int dsa_port_bridge_host_mdb_del(const struct dsa_port *dp,
1121 const struct switchdev_obj_port_mdb *mdb)
1123 struct dsa_port *cpu_dp = dp->cpu_dp;
1124 struct dsa_db db = {
1125 .type = DSA_DB_BRIDGE,
1126 .bridge = *dp->bridge,
1130 err = dev_mc_del(cpu_dp->master, mdb->addr);
1134 return dsa_port_host_mdb_del(dp, mdb, db);
1137 int dsa_port_vlan_add(struct dsa_port *dp,
1138 const struct switchdev_obj_port_vlan *vlan,
1139 struct netlink_ext_ack *extack)
1141 struct dsa_notifier_vlan_info info = {
1142 .sw_index = dp->ds->index,
1148 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
1151 int dsa_port_vlan_del(struct dsa_port *dp,
1152 const struct switchdev_obj_port_vlan *vlan)
1154 struct dsa_notifier_vlan_info info = {
1155 .sw_index = dp->ds->index,
1160 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
1163 int dsa_port_host_vlan_add(struct dsa_port *dp,
1164 const struct switchdev_obj_port_vlan *vlan,
1165 struct netlink_ext_ack *extack)
1167 struct dsa_notifier_vlan_info info = {
1168 .sw_index = dp->ds->index,
1173 struct dsa_port *cpu_dp = dp->cpu_dp;
1176 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
1177 if (err && err != -EOPNOTSUPP)
1180 vlan_vid_add(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1185 int dsa_port_host_vlan_del(struct dsa_port *dp,
1186 const struct switchdev_obj_port_vlan *vlan)
1188 struct dsa_notifier_vlan_info info = {
1189 .sw_index = dp->ds->index,
1193 struct dsa_port *cpu_dp = dp->cpu_dp;
1196 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
1197 if (err && err != -EOPNOTSUPP)
1200 vlan_vid_del(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1205 int dsa_port_mrp_add(const struct dsa_port *dp,
1206 const struct switchdev_obj_mrp *mrp)
1208 struct dsa_switch *ds = dp->ds;
1210 if (!ds->ops->port_mrp_add)
1213 return ds->ops->port_mrp_add(ds, dp->index, mrp);
1216 int dsa_port_mrp_del(const struct dsa_port *dp,
1217 const struct switchdev_obj_mrp *mrp)
1219 struct dsa_switch *ds = dp->ds;
1221 if (!ds->ops->port_mrp_del)
1224 return ds->ops->port_mrp_del(ds, dp->index, mrp);
1227 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
1228 const struct switchdev_obj_ring_role_mrp *mrp)
1230 struct dsa_switch *ds = dp->ds;
1232 if (!ds->ops->port_mrp_add_ring_role)
1235 return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
1238 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
1239 const struct switchdev_obj_ring_role_mrp *mrp)
1241 struct dsa_switch *ds = dp->ds;
1243 if (!ds->ops->port_mrp_del_ring_role)
1246 return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
1249 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
1250 const struct dsa_device_ops *tag_ops)
1252 cpu_dp->rcv = tag_ops->rcv;
1253 cpu_dp->tag_ops = tag_ops;
1256 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
1258 struct device_node *phy_dn;
1259 struct phy_device *phydev;
1261 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
1265 phydev = of_phy_find_device(phy_dn);
1267 of_node_put(phy_dn);
1268 return ERR_PTR(-EPROBE_DEFER);
1271 of_node_put(phy_dn);
1275 static void dsa_port_phylink_validate(struct phylink_config *config,
1276 unsigned long *supported,
1277 struct phylink_link_state *state)
1279 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1280 struct dsa_switch *ds = dp->ds;
1282 if (!ds->ops->phylink_validate) {
1283 if (config->mac_capabilities)
1284 phylink_generic_validate(config, supported, state);
1288 ds->ops->phylink_validate(ds, dp->index, supported, state);
1291 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
1292 struct phylink_link_state *state)
1294 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1295 struct dsa_switch *ds = dp->ds;
1298 /* Only called for inband modes */
1299 if (!ds->ops->phylink_mac_link_state) {
1304 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
1306 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
1312 static struct phylink_pcs *
1313 dsa_port_phylink_mac_select_pcs(struct phylink_config *config,
1314 phy_interface_t interface)
1316 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1317 struct phylink_pcs *pcs = ERR_PTR(-EOPNOTSUPP);
1318 struct dsa_switch *ds = dp->ds;
1320 if (ds->ops->phylink_mac_select_pcs)
1321 pcs = ds->ops->phylink_mac_select_pcs(ds, dp->index, interface);
1326 static void dsa_port_phylink_mac_config(struct phylink_config *config,
1328 const struct phylink_link_state *state)
1330 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1331 struct dsa_switch *ds = dp->ds;
1333 if (!ds->ops->phylink_mac_config)
1336 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1339 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
1341 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1342 struct dsa_switch *ds = dp->ds;
1344 if (!ds->ops->phylink_mac_an_restart)
1347 ds->ops->phylink_mac_an_restart(ds, dp->index);
1350 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1352 phy_interface_t interface)
1354 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1355 struct phy_device *phydev = NULL;
1356 struct dsa_switch *ds = dp->ds;
1358 if (dsa_port_is_user(dp))
1359 phydev = dp->slave->phydev;
1361 if (!ds->ops->phylink_mac_link_down) {
1362 if (ds->ops->adjust_link && phydev)
1363 ds->ops->adjust_link(ds, dp->index, phydev);
1367 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1370 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1371 struct phy_device *phydev,
1373 phy_interface_t interface,
1374 int speed, int duplex,
1375 bool tx_pause, bool rx_pause)
1377 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1378 struct dsa_switch *ds = dp->ds;
1380 if (!ds->ops->phylink_mac_link_up) {
1381 if (ds->ops->adjust_link && phydev)
1382 ds->ops->adjust_link(ds, dp->index, phydev);
1386 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1387 speed, duplex, tx_pause, rx_pause);
1390 static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1391 .validate = dsa_port_phylink_validate,
1392 .mac_select_pcs = dsa_port_phylink_mac_select_pcs,
1393 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1394 .mac_config = dsa_port_phylink_mac_config,
1395 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1396 .mac_link_down = dsa_port_phylink_mac_link_down,
1397 .mac_link_up = dsa_port_phylink_mac_link_up,
1400 int dsa_port_phylink_create(struct dsa_port *dp)
1402 struct dsa_switch *ds = dp->ds;
1403 phy_interface_t mode;
1406 err = of_get_phy_mode(dp->dn, &mode);
1408 mode = PHY_INTERFACE_MODE_NA;
1410 /* Presence of phylink_mac_link_state or phylink_mac_an_restart is
1411 * an indicator of a legacy phylink driver.
1413 if (ds->ops->phylink_mac_link_state ||
1414 ds->ops->phylink_mac_an_restart)
1415 dp->pl_config.legacy_pre_march2020 = true;
1417 if (ds->ops->phylink_get_caps)
1418 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
1420 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn),
1421 mode, &dsa_port_phylink_mac_ops);
1422 if (IS_ERR(dp->pl)) {
1423 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1424 return PTR_ERR(dp->pl);
1430 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1432 struct dsa_switch *ds = dp->ds;
1433 struct phy_device *phydev;
1434 int port = dp->index;
1437 phydev = dsa_port_get_phy_device(dp);
1442 return PTR_ERR(phydev);
1445 err = genphy_resume(phydev);
1449 err = genphy_read_status(phydev);
1453 err = genphy_suspend(phydev);
1458 if (ds->ops->adjust_link)
1459 ds->ops->adjust_link(ds, port, phydev);
1461 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1464 put_device(&phydev->mdio.dev);
1468 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
1470 struct device_node *dn = dp->dn;
1471 struct dsa_switch *ds = dp->ds;
1472 struct phy_device *phydev;
1473 int port = dp->index;
1474 phy_interface_t mode;
1477 err = of_phy_register_fixed_link(dn);
1480 "failed to register the fixed PHY of port %d\n",
1485 phydev = of_phy_find_device(dn);
1487 err = of_get_phy_mode(dn, &mode);
1489 mode = PHY_INTERFACE_MODE_NA;
1490 phydev->interface = mode;
1492 genphy_read_status(phydev);
1494 if (ds->ops->adjust_link)
1495 ds->ops->adjust_link(ds, port, phydev);
1497 put_device(&phydev->mdio.dev);
1502 static int dsa_port_phylink_register(struct dsa_port *dp)
1504 struct dsa_switch *ds = dp->ds;
1505 struct device_node *port_dn = dp->dn;
1508 dp->pl_config.dev = ds->dev;
1509 dp->pl_config.type = PHYLINK_DEV;
1511 err = dsa_port_phylink_create(dp);
1515 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1516 if (err && err != -ENODEV) {
1517 pr_err("could not attach to PHY: %d\n", err);
1518 goto err_phy_connect;
1524 phylink_destroy(dp->pl);
1528 int dsa_port_link_register_of(struct dsa_port *dp)
1530 struct dsa_switch *ds = dp->ds;
1531 struct device_node *phy_np;
1532 int port = dp->index;
1534 if (!ds->ops->adjust_link) {
1535 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1536 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1537 if (ds->ops->phylink_mac_link_down)
1538 ds->ops->phylink_mac_link_down(ds, port,
1539 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1540 return dsa_port_phylink_register(dp);
1546 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1548 if (of_phy_is_fixed_link(dp->dn))
1549 return dsa_port_fixed_link_register_of(dp);
1551 return dsa_port_setup_phy_of(dp, true);
1554 void dsa_port_link_unregister_of(struct dsa_port *dp)
1556 struct dsa_switch *ds = dp->ds;
1558 if (!ds->ops->adjust_link && dp->pl) {
1560 phylink_disconnect_phy(dp->pl);
1562 phylink_destroy(dp->pl);
1567 if (of_phy_is_fixed_link(dp->dn))
1568 of_phy_deregister_fixed_link(dp->dn);
1570 dsa_port_setup_phy_of(dp, false);
1573 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1575 struct dsa_switch *ds = dp->ds;
1578 if (!ds->ops->port_hsr_join)
1583 err = ds->ops->port_hsr_join(ds, dp->index, hsr);
1590 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1592 struct dsa_switch *ds = dp->ds;
1597 if (ds->ops->port_hsr_leave) {
1598 err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
1600 dev_err(dp->ds->dev,
1601 "port %d failed to leave HSR %s: %pe\n",
1602 dp->index, hsr->name, ERR_PTR(err));
1606 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
1608 struct dsa_notifier_tag_8021q_vlan_info info = {
1609 .tree_index = dp->ds->dst->index,
1610 .sw_index = dp->ds->index,
1616 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1618 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1621 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
1623 struct dsa_notifier_tag_8021q_vlan_info info = {
1624 .tree_index = dp->ds->dst->index,
1625 .sw_index = dp->ds->index,
1632 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1634 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1636 dev_err(dp->ds->dev,
1637 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1638 dp->index, vid, ERR_PTR(err));