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)
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 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
325 struct netlink_ext_ack *extack)
327 struct dsa_notifier_bridge_info info = {
328 .tree_index = dp->ds->dst->index,
329 .sw_index = dp->ds->index,
333 struct net_device *dev = dp->slave;
334 struct net_device *brport_dev;
337 /* Here the interface is already bridged. Reflect the current
338 * configuration so that drivers can program their chips accordingly.
340 err = dsa_port_bridge_create(dp, br, extack);
344 brport_dev = dsa_port_to_bridge_port(dp);
346 info.bridge = *dp->bridge;
347 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
351 /* Drivers which support bridge TX forwarding should set this */
352 dp->bridge->tx_fwd_offload = info.tx_fwd_offload;
354 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
355 &dsa_slave_switchdev_notifier,
356 &dsa_slave_switchdev_blocking_notifier,
357 dp->bridge->tx_fwd_offload, extack);
359 goto out_rollback_unbridge;
361 err = dsa_port_switchdev_sync_attrs(dp, extack);
363 goto out_rollback_unoffload;
367 out_rollback_unoffload:
368 switchdev_bridge_port_unoffload(brport_dev, dp,
369 &dsa_slave_switchdev_notifier,
370 &dsa_slave_switchdev_blocking_notifier);
371 out_rollback_unbridge:
372 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
374 dsa_port_bridge_destroy(dp, br);
378 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
380 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
382 /* Don't try to unoffload something that is not offloaded */
386 switchdev_bridge_port_unoffload(brport_dev, dp,
387 &dsa_slave_switchdev_notifier,
388 &dsa_slave_switchdev_blocking_notifier);
390 dsa_flush_workqueue();
393 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
395 struct dsa_notifier_bridge_info info = {
396 .tree_index = dp->ds->dst->index,
397 .sw_index = dp->ds->index,
402 /* If the port could not be offloaded to begin with, then
403 * there is nothing to do.
408 info.bridge = *dp->bridge;
410 /* Here the port is already unbridged. Reflect the current configuration
411 * so that drivers can program their chips accordingly.
413 dsa_port_bridge_destroy(dp, br);
415 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
418 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
419 dp->index, ERR_PTR(err));
421 dsa_port_switchdev_unsync_attrs(dp);
424 int dsa_port_lag_change(struct dsa_port *dp,
425 struct netdev_lag_lower_state_info *linfo)
427 struct dsa_notifier_lag_info info = {
428 .sw_index = dp->ds->index,
436 /* On statically configured aggregates (e.g. loadbalance
437 * without LACP) ports will always be tx_enabled, even if the
438 * link is down. Thus we require both link_up and tx_enabled
439 * in order to include it in the tx set.
441 tx_enabled = linfo->link_up && linfo->tx_enabled;
443 if (tx_enabled == dp->lag_tx_enabled)
446 dp->lag_tx_enabled = tx_enabled;
448 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
451 static int dsa_port_lag_create(struct dsa_port *dp,
452 struct net_device *lag_dev)
454 struct dsa_switch *ds = dp->ds;
457 lag = dsa_tree_lag_find(ds->dst, lag_dev);
459 refcount_inc(&lag->refcount);
464 lag = kzalloc(sizeof(*lag), GFP_KERNEL);
468 refcount_set(&lag->refcount, 1);
469 mutex_init(&lag->fdb_lock);
470 INIT_LIST_HEAD(&lag->fdbs);
472 dsa_lag_map(ds->dst, lag);
478 static void dsa_port_lag_destroy(struct dsa_port *dp)
480 struct dsa_lag *lag = dp->lag;
483 dp->lag_tx_enabled = false;
485 if (!refcount_dec_and_test(&lag->refcount))
488 WARN_ON(!list_empty(&lag->fdbs));
489 dsa_lag_unmap(dp->ds->dst, lag);
493 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag_dev,
494 struct netdev_lag_upper_info *uinfo,
495 struct netlink_ext_ack *extack)
497 struct dsa_notifier_lag_info info = {
498 .sw_index = dp->ds->index,
502 struct net_device *bridge_dev;
505 err = dsa_port_lag_create(dp, lag_dev);
510 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
514 bridge_dev = netdev_master_upper_dev_get(lag_dev);
515 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
518 err = dsa_port_bridge_join(dp, bridge_dev, extack);
520 goto err_bridge_join;
525 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
527 dsa_port_lag_destroy(dp);
532 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
534 struct net_device *br = dsa_port_bridge_dev_get(dp);
537 dsa_port_pre_bridge_leave(dp, br);
540 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag_dev)
542 struct net_device *br = dsa_port_bridge_dev_get(dp);
543 struct dsa_notifier_lag_info info = {
544 .sw_index = dp->ds->index,
552 /* Port might have been part of a LAG that in turn was
553 * attached to a bridge.
556 dsa_port_bridge_leave(dp, br);
560 dsa_port_lag_destroy(dp);
562 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
565 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
566 dp->index, ERR_PTR(err));
569 /* Must be called under rcu_read_lock() */
570 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
572 struct netlink_ext_ack *extack)
574 struct dsa_switch *ds = dp->ds;
575 struct dsa_port *other_dp;
578 /* VLAN awareness was off, so the question is "can we turn it on".
579 * We may have had 8021q uppers, those need to go. Make sure we don't
580 * enter an inconsistent state: deny changing the VLAN awareness state
581 * as long as we have 8021q uppers.
583 if (vlan_filtering && dsa_port_is_user(dp)) {
584 struct net_device *br = dsa_port_bridge_dev_get(dp);
585 struct net_device *upper_dev, *slave = dp->slave;
586 struct list_head *iter;
588 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
589 struct bridge_vlan_info br_info;
592 if (!is_vlan_dev(upper_dev))
595 vid = vlan_dev_vlan_id(upper_dev);
597 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
598 * device, respectively the VID is not found, returning
599 * 0 means success, which is a failure for us here.
601 err = br_vlan_get_info(br, vid, &br_info);
603 NL_SET_ERR_MSG_MOD(extack,
604 "Must first remove VLAN uppers having VIDs also present in bridge");
610 if (!ds->vlan_filtering_is_global)
613 /* For cases where enabling/disabling VLAN awareness is global to the
614 * switch, we need to handle the case where multiple bridges span
615 * different ports of the same switch device and one of them has a
616 * different setting than what is being requested.
618 dsa_switch_for_each_port(other_dp, ds) {
619 struct net_device *other_br = dsa_port_bridge_dev_get(other_dp);
621 /* If it's the same bridge, it also has same
622 * vlan_filtering setting => no need to check
624 if (!other_br || other_br == dsa_port_bridge_dev_get(dp))
627 if (br_vlan_enabled(other_br) != vlan_filtering) {
628 NL_SET_ERR_MSG_MOD(extack,
629 "VLAN filtering is a global setting");
636 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
637 struct netlink_ext_ack *extack)
639 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
640 struct dsa_switch *ds = dp->ds;
644 if (!ds->ops->port_vlan_filtering)
647 /* We are called from dsa_slave_switchdev_blocking_event(),
648 * which is not under rcu_read_lock(), unlike
649 * dsa_slave_switchdev_event().
652 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
657 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
660 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
665 if (ds->vlan_filtering_is_global) {
666 struct dsa_port *other_dp;
668 ds->vlan_filtering = vlan_filtering;
670 dsa_switch_for_each_user_port(other_dp, ds) {
671 struct net_device *slave = dp->slave;
673 /* We might be called in the unbind path, so not
674 * all slave devices might still be registered.
679 err = dsa_slave_manage_vlan_filtering(slave,
685 dp->vlan_filtering = vlan_filtering;
687 err = dsa_slave_manage_vlan_filtering(dp->slave,
696 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
698 if (ds->vlan_filtering_is_global)
699 ds->vlan_filtering = old_vlan_filtering;
701 dp->vlan_filtering = old_vlan_filtering;
706 /* This enforces legacy behavior for switch drivers which assume they can't
707 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
709 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
711 struct net_device *br = dsa_port_bridge_dev_get(dp);
712 struct dsa_switch *ds = dp->ds;
717 return !ds->configure_vlan_while_not_filtering && !br_vlan_enabled(br);
720 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
722 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
723 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
724 struct dsa_notifier_ageing_time_info info;
727 info.ageing_time = ageing_time;
729 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
733 dp->ageing_time = ageing_time;
738 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
739 struct switchdev_brport_flags flags,
740 struct netlink_ext_ack *extack)
742 struct dsa_switch *ds = dp->ds;
744 if (!ds->ops->port_pre_bridge_flags)
747 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
750 int dsa_port_bridge_flags(struct dsa_port *dp,
751 struct switchdev_brport_flags flags,
752 struct netlink_ext_ack *extack)
754 struct dsa_switch *ds = dp->ds;
757 if (!ds->ops->port_bridge_flags)
760 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
764 if (flags.mask & BR_LEARNING) {
765 bool learning = flags.val & BR_LEARNING;
767 if (learning == dp->learning)
770 if ((dp->learning && !learning) &&
771 (dp->stp_state == BR_STATE_LEARNING ||
772 dp->stp_state == BR_STATE_FORWARDING))
773 dsa_port_fast_age(dp);
775 dp->learning = learning;
781 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
784 struct dsa_notifier_mtu_info info = {
785 .sw_index = dp->ds->index,
786 .targeted_match = targeted_match,
791 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
794 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
797 struct dsa_notifier_fdb_info info = {
798 .sw_index = dp->ds->index,
803 .type = DSA_DB_BRIDGE,
804 .bridge = *dp->bridge,
808 /* Refcounting takes bridge.num as a key, and should be global for all
809 * bridges in the absence of FDB isolation, and per bridge otherwise.
810 * Force the bridge.num to zero here in the absence of FDB isolation.
812 if (!dp->ds->fdb_isolation)
813 info.db.bridge.num = 0;
815 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
818 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
821 struct dsa_notifier_fdb_info info = {
822 .sw_index = dp->ds->index,
827 .type = DSA_DB_BRIDGE,
828 .bridge = *dp->bridge,
832 if (!dp->ds->fdb_isolation)
833 info.db.bridge.num = 0;
835 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
838 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
841 struct dsa_notifier_fdb_info info = {
842 .sw_index = dp->ds->index,
847 .type = DSA_DB_BRIDGE,
848 .bridge = *dp->bridge,
851 struct dsa_port *cpu_dp = dp->cpu_dp;
854 /* Avoid a call to __dev_set_promiscuity() on the master, which
855 * requires rtnl_lock(), since we can't guarantee that is held here,
856 * and we can't take it either.
858 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
859 err = dev_uc_add(cpu_dp->master, addr);
864 if (!dp->ds->fdb_isolation)
865 info.db.bridge.num = 0;
867 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
870 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
873 struct dsa_notifier_fdb_info info = {
874 .sw_index = dp->ds->index,
879 .type = DSA_DB_BRIDGE,
880 .bridge = *dp->bridge,
883 struct dsa_port *cpu_dp = dp->cpu_dp;
886 if (cpu_dp->master->priv_flags & IFF_UNICAST_FLT) {
887 err = dev_uc_del(cpu_dp->master, addr);
892 if (!dp->ds->fdb_isolation)
893 info.db.bridge.num = 0;
895 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
898 int dsa_port_lag_fdb_add(struct dsa_port *dp, const unsigned char *addr,
901 struct dsa_notifier_lag_fdb_info info = {
906 .type = DSA_DB_BRIDGE,
907 .bridge = *dp->bridge,
911 if (!dp->ds->fdb_isolation)
912 info.db.bridge.num = 0;
914 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_ADD, &info);
917 int dsa_port_lag_fdb_del(struct dsa_port *dp, const unsigned char *addr,
920 struct dsa_notifier_lag_fdb_info info = {
925 .type = DSA_DB_BRIDGE,
926 .bridge = *dp->bridge,
930 if (!dp->ds->fdb_isolation)
931 info.db.bridge.num = 0;
933 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_FDB_DEL, &info);
936 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
938 struct dsa_switch *ds = dp->ds;
939 int port = dp->index;
941 if (!ds->ops->port_fdb_dump)
944 return ds->ops->port_fdb_dump(ds, port, cb, data);
947 int dsa_port_mdb_add(const struct dsa_port *dp,
948 const struct switchdev_obj_port_mdb *mdb)
950 struct dsa_notifier_mdb_info info = {
951 .sw_index = dp->ds->index,
955 .type = DSA_DB_BRIDGE,
956 .bridge = *dp->bridge,
960 if (!dp->ds->fdb_isolation)
961 info.db.bridge.num = 0;
963 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
966 int dsa_port_mdb_del(const struct dsa_port *dp,
967 const struct switchdev_obj_port_mdb *mdb)
969 struct dsa_notifier_mdb_info info = {
970 .sw_index = dp->ds->index,
974 .type = DSA_DB_BRIDGE,
975 .bridge = *dp->bridge,
979 if (!dp->ds->fdb_isolation)
980 info.db.bridge.num = 0;
982 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
985 int dsa_port_host_mdb_add(const struct dsa_port *dp,
986 const struct switchdev_obj_port_mdb *mdb)
988 struct dsa_notifier_mdb_info info = {
989 .sw_index = dp->ds->index,
993 .type = DSA_DB_BRIDGE,
994 .bridge = *dp->bridge,
997 struct dsa_port *cpu_dp = dp->cpu_dp;
1000 err = dev_mc_add(cpu_dp->master, mdb->addr);
1004 if (!dp->ds->fdb_isolation)
1005 info.db.bridge.num = 0;
1007 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
1010 int dsa_port_host_mdb_del(const struct dsa_port *dp,
1011 const struct switchdev_obj_port_mdb *mdb)
1013 struct dsa_notifier_mdb_info info = {
1014 .sw_index = dp->ds->index,
1018 .type = DSA_DB_BRIDGE,
1019 .bridge = *dp->bridge,
1022 struct dsa_port *cpu_dp = dp->cpu_dp;
1025 err = dev_mc_del(cpu_dp->master, mdb->addr);
1029 if (!dp->ds->fdb_isolation)
1030 info.db.bridge.num = 0;
1032 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
1035 int dsa_port_vlan_add(struct dsa_port *dp,
1036 const struct switchdev_obj_port_vlan *vlan,
1037 struct netlink_ext_ack *extack)
1039 struct dsa_notifier_vlan_info info = {
1040 .sw_index = dp->ds->index,
1046 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
1049 int dsa_port_vlan_del(struct dsa_port *dp,
1050 const struct switchdev_obj_port_vlan *vlan)
1052 struct dsa_notifier_vlan_info info = {
1053 .sw_index = dp->ds->index,
1058 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
1061 int dsa_port_host_vlan_add(struct dsa_port *dp,
1062 const struct switchdev_obj_port_vlan *vlan,
1063 struct netlink_ext_ack *extack)
1065 struct dsa_notifier_vlan_info info = {
1066 .sw_index = dp->ds->index,
1071 struct dsa_port *cpu_dp = dp->cpu_dp;
1074 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_ADD, &info);
1075 if (err && err != -EOPNOTSUPP)
1078 vlan_vid_add(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1083 int dsa_port_host_vlan_del(struct dsa_port *dp,
1084 const struct switchdev_obj_port_vlan *vlan)
1086 struct dsa_notifier_vlan_info info = {
1087 .sw_index = dp->ds->index,
1091 struct dsa_port *cpu_dp = dp->cpu_dp;
1094 err = dsa_port_notify(dp, DSA_NOTIFIER_HOST_VLAN_DEL, &info);
1095 if (err && err != -EOPNOTSUPP)
1098 vlan_vid_del(cpu_dp->master, htons(ETH_P_8021Q), vlan->vid);
1103 int dsa_port_mrp_add(const struct dsa_port *dp,
1104 const struct switchdev_obj_mrp *mrp)
1106 struct dsa_switch *ds = dp->ds;
1108 if (!ds->ops->port_mrp_add)
1111 return ds->ops->port_mrp_add(ds, dp->index, mrp);
1114 int dsa_port_mrp_del(const struct dsa_port *dp,
1115 const struct switchdev_obj_mrp *mrp)
1117 struct dsa_switch *ds = dp->ds;
1119 if (!ds->ops->port_mrp_del)
1122 return ds->ops->port_mrp_del(ds, dp->index, mrp);
1125 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
1126 const struct switchdev_obj_ring_role_mrp *mrp)
1128 struct dsa_switch *ds = dp->ds;
1130 if (!ds->ops->port_mrp_add_ring_role)
1133 return ds->ops->port_mrp_add_ring_role(ds, dp->index, mrp);
1136 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
1137 const struct switchdev_obj_ring_role_mrp *mrp)
1139 struct dsa_switch *ds = dp->ds;
1141 if (!ds->ops->port_mrp_del_ring_role)
1144 return ds->ops->port_mrp_del_ring_role(ds, dp->index, mrp);
1147 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
1148 const struct dsa_device_ops *tag_ops)
1150 cpu_dp->rcv = tag_ops->rcv;
1151 cpu_dp->tag_ops = tag_ops;
1154 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
1156 struct device_node *phy_dn;
1157 struct phy_device *phydev;
1159 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
1163 phydev = of_phy_find_device(phy_dn);
1165 of_node_put(phy_dn);
1166 return ERR_PTR(-EPROBE_DEFER);
1169 of_node_put(phy_dn);
1173 static void dsa_port_phylink_validate(struct phylink_config *config,
1174 unsigned long *supported,
1175 struct phylink_link_state *state)
1177 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1178 struct dsa_switch *ds = dp->ds;
1180 if (!ds->ops->phylink_validate) {
1181 if (config->mac_capabilities)
1182 phylink_generic_validate(config, supported, state);
1186 ds->ops->phylink_validate(ds, dp->index, supported, state);
1189 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
1190 struct phylink_link_state *state)
1192 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1193 struct dsa_switch *ds = dp->ds;
1196 /* Only called for inband modes */
1197 if (!ds->ops->phylink_mac_link_state) {
1202 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
1204 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
1210 static struct phylink_pcs *
1211 dsa_port_phylink_mac_select_pcs(struct phylink_config *config,
1212 phy_interface_t interface)
1214 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1215 struct phylink_pcs *pcs = ERR_PTR(-EOPNOTSUPP);
1216 struct dsa_switch *ds = dp->ds;
1218 if (ds->ops->phylink_mac_select_pcs)
1219 pcs = ds->ops->phylink_mac_select_pcs(ds, dp->index, interface);
1224 static void dsa_port_phylink_mac_config(struct phylink_config *config,
1226 const struct phylink_link_state *state)
1228 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1229 struct dsa_switch *ds = dp->ds;
1231 if (!ds->ops->phylink_mac_config)
1234 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1237 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
1239 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1240 struct dsa_switch *ds = dp->ds;
1242 if (!ds->ops->phylink_mac_an_restart)
1245 ds->ops->phylink_mac_an_restart(ds, dp->index);
1248 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1250 phy_interface_t interface)
1252 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1253 struct phy_device *phydev = NULL;
1254 struct dsa_switch *ds = dp->ds;
1256 if (dsa_port_is_user(dp))
1257 phydev = dp->slave->phydev;
1259 if (!ds->ops->phylink_mac_link_down) {
1260 if (ds->ops->adjust_link && phydev)
1261 ds->ops->adjust_link(ds, dp->index, phydev);
1265 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1268 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1269 struct phy_device *phydev,
1271 phy_interface_t interface,
1272 int speed, int duplex,
1273 bool tx_pause, bool rx_pause)
1275 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1276 struct dsa_switch *ds = dp->ds;
1278 if (!ds->ops->phylink_mac_link_up) {
1279 if (ds->ops->adjust_link && phydev)
1280 ds->ops->adjust_link(ds, dp->index, phydev);
1284 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1285 speed, duplex, tx_pause, rx_pause);
1288 static const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1289 .validate = dsa_port_phylink_validate,
1290 .mac_select_pcs = dsa_port_phylink_mac_select_pcs,
1291 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1292 .mac_config = dsa_port_phylink_mac_config,
1293 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1294 .mac_link_down = dsa_port_phylink_mac_link_down,
1295 .mac_link_up = dsa_port_phylink_mac_link_up,
1298 int dsa_port_phylink_create(struct dsa_port *dp)
1300 struct dsa_switch *ds = dp->ds;
1301 phy_interface_t mode;
1304 err = of_get_phy_mode(dp->dn, &mode);
1306 mode = PHY_INTERFACE_MODE_NA;
1308 /* Presence of phylink_mac_link_state or phylink_mac_an_restart is
1309 * an indicator of a legacy phylink driver.
1311 if (ds->ops->phylink_mac_link_state ||
1312 ds->ops->phylink_mac_an_restart)
1313 dp->pl_config.legacy_pre_march2020 = true;
1315 if (ds->ops->phylink_get_caps)
1316 ds->ops->phylink_get_caps(ds, dp->index, &dp->pl_config);
1318 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(dp->dn),
1319 mode, &dsa_port_phylink_mac_ops);
1320 if (IS_ERR(dp->pl)) {
1321 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1322 return PTR_ERR(dp->pl);
1328 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1330 struct dsa_switch *ds = dp->ds;
1331 struct phy_device *phydev;
1332 int port = dp->index;
1335 phydev = dsa_port_get_phy_device(dp);
1340 return PTR_ERR(phydev);
1343 err = genphy_resume(phydev);
1347 err = genphy_read_status(phydev);
1351 err = genphy_suspend(phydev);
1356 if (ds->ops->adjust_link)
1357 ds->ops->adjust_link(ds, port, phydev);
1359 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1362 put_device(&phydev->mdio.dev);
1366 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
1368 struct device_node *dn = dp->dn;
1369 struct dsa_switch *ds = dp->ds;
1370 struct phy_device *phydev;
1371 int port = dp->index;
1372 phy_interface_t mode;
1375 err = of_phy_register_fixed_link(dn);
1378 "failed to register the fixed PHY of port %d\n",
1383 phydev = of_phy_find_device(dn);
1385 err = of_get_phy_mode(dn, &mode);
1387 mode = PHY_INTERFACE_MODE_NA;
1388 phydev->interface = mode;
1390 genphy_read_status(phydev);
1392 if (ds->ops->adjust_link)
1393 ds->ops->adjust_link(ds, port, phydev);
1395 put_device(&phydev->mdio.dev);
1400 static int dsa_port_phylink_register(struct dsa_port *dp)
1402 struct dsa_switch *ds = dp->ds;
1403 struct device_node *port_dn = dp->dn;
1406 dp->pl_config.dev = ds->dev;
1407 dp->pl_config.type = PHYLINK_DEV;
1409 err = dsa_port_phylink_create(dp);
1413 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1414 if (err && err != -ENODEV) {
1415 pr_err("could not attach to PHY: %d\n", err);
1416 goto err_phy_connect;
1422 phylink_destroy(dp->pl);
1426 int dsa_port_link_register_of(struct dsa_port *dp)
1428 struct dsa_switch *ds = dp->ds;
1429 struct device_node *phy_np;
1430 int port = dp->index;
1432 if (!ds->ops->adjust_link) {
1433 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1434 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1435 if (ds->ops->phylink_mac_link_down)
1436 ds->ops->phylink_mac_link_down(ds, port,
1437 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1438 return dsa_port_phylink_register(dp);
1444 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1446 if (of_phy_is_fixed_link(dp->dn))
1447 return dsa_port_fixed_link_register_of(dp);
1449 return dsa_port_setup_phy_of(dp, true);
1452 void dsa_port_link_unregister_of(struct dsa_port *dp)
1454 struct dsa_switch *ds = dp->ds;
1456 if (!ds->ops->adjust_link && dp->pl) {
1458 phylink_disconnect_phy(dp->pl);
1460 phylink_destroy(dp->pl);
1465 if (of_phy_is_fixed_link(dp->dn))
1466 of_phy_deregister_fixed_link(dp->dn);
1468 dsa_port_setup_phy_of(dp, false);
1471 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1473 struct dsa_switch *ds = dp->ds;
1476 if (!ds->ops->port_hsr_join)
1481 err = ds->ops->port_hsr_join(ds, dp->index, hsr);
1488 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1490 struct dsa_switch *ds = dp->ds;
1495 if (ds->ops->port_hsr_leave) {
1496 err = ds->ops->port_hsr_leave(ds, dp->index, hsr);
1498 dev_err(dp->ds->dev,
1499 "port %d failed to leave HSR %s: %pe\n",
1500 dp->index, hsr->name, ERR_PTR(err));
1504 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
1506 struct dsa_notifier_tag_8021q_vlan_info info = {
1507 .tree_index = dp->ds->dst->index,
1508 .sw_index = dp->ds->index,
1514 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1516 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1519 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
1521 struct dsa_notifier_tag_8021q_vlan_info info = {
1522 .tree_index = dp->ds->dst->index,
1523 .sw_index = dp->ds->index,
1530 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1532 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1534 dev_err(dp->ds->dev,
1535 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1536 dp->index, vid, ERR_PTR(err));