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 bool dsa_port_supports_hwtstamp(struct dsa_port *dp, struct ifreq *ifr)
80 struct dsa_switch *ds = dp->ds;
83 if (!ds->ops->port_hwtstamp_get || !ds->ops->port_hwtstamp_set)
86 /* "See through" shim implementations of the "get" method.
87 * This will clobber the ifreq structure, but we will either return an
88 * error, or the master will overwrite it with proper values.
90 err = ds->ops->port_hwtstamp_get(ds, dp->index, ifr);
91 return err != -EOPNOTSUPP;
94 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
96 struct dsa_switch *ds = dp->ds;
99 if (!ds->ops->port_stp_state_set)
102 ds->ops->port_stp_state_set(ds, port, state);
104 if (!dsa_port_can_configure_learning(dp) ||
105 (do_fast_age && dp->learning)) {
106 /* Fast age FDB entries or flush appropriate forwarding database
107 * for the given port, if we are moving it from Learning or
108 * Forwarding state, to Disabled or Blocking or Listening state.
109 * Ports that were standalone before the STP state change don't
110 * need to fast age the FDB, since address learning is off in
114 if ((dp->stp_state == BR_STATE_LEARNING ||
115 dp->stp_state == BR_STATE_FORWARDING) &&
116 (state == BR_STATE_DISABLED ||
117 state == BR_STATE_BLOCKING ||
118 state == BR_STATE_LISTENING))
119 dsa_port_fast_age(dp);
122 dp->stp_state = state;
127 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
130 struct dsa_switch *ds = dp->ds;
133 err = dsa_port_set_state(dp, state, do_fast_age);
134 if (err && err != -EOPNOTSUPP) {
135 dev_err(ds->dev, "port %d failed to set STP state %u: %pe\n",
136 dp->index, state, ERR_PTR(err));
140 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
142 struct dsa_switch *ds = dp->ds;
143 int port = dp->index;
146 if (ds->ops->port_enable) {
147 err = ds->ops->port_enable(ds, port, phy);
153 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
156 phylink_start(dp->pl);
161 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
166 err = dsa_port_enable_rt(dp, phy);
172 void dsa_port_disable_rt(struct dsa_port *dp)
174 struct dsa_switch *ds = dp->ds;
175 int port = dp->index;
178 phylink_stop(dp->pl);
181 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
183 if (ds->ops->port_disable)
184 ds->ops->port_disable(ds, port);
187 void dsa_port_disable(struct dsa_port *dp)
190 dsa_port_disable_rt(dp);
194 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
195 struct netlink_ext_ack *extack)
197 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
199 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
202 for_each_set_bit(flag, &mask, 32) {
203 struct switchdev_brport_flags flags = {0};
205 flags.mask = BIT(flag);
207 if (br_port_flag_is_set(brport_dev, BIT(flag)))
208 flags.val = BIT(flag);
210 err = dsa_port_bridge_flags(dp, flags, extack);
211 if (err && err != -EOPNOTSUPP)
218 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
220 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
221 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
225 for_each_set_bit(flag, &mask, 32) {
226 struct switchdev_brport_flags flags = {0};
228 flags.mask = BIT(flag);
229 flags.val = val & BIT(flag);
231 err = dsa_port_bridge_flags(dp, flags, NULL);
232 if (err && err != -EOPNOTSUPP)
234 "failed to clear bridge port flag %lu: %pe\n",
235 flags.val, ERR_PTR(err));
239 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
240 struct netlink_ext_ack *extack)
242 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
243 struct net_device *br = dp->bridge_dev;
246 err = dsa_port_inherit_brport_flags(dp, extack);
250 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
251 if (err && err != -EOPNOTSUPP)
254 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
255 if (err && err != -EOPNOTSUPP)
258 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
259 if (err && err != -EOPNOTSUPP)
265 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp)
267 /* Configure the port for standalone mode (no address learning,
269 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
270 * when the user requests it through netlink or sysfs, but not
271 * automatically at port join or leave, so we need to handle resetting
272 * the brport flags ourselves. But we even prefer it that way, because
273 * otherwise, some setups might never get the notification they need,
274 * for example, when a port leaves a LAG that offloads the bridge,
275 * it becomes standalone, but as far as the bridge is concerned, no
278 dsa_port_clear_brport_flags(dp);
280 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
281 * so allow it to be in BR_STATE_FORWARDING to be kept functional
283 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
285 /* VLAN filtering is handled by dsa_switch_bridge_leave */
287 /* Ageing time may be global to the switch chip, so don't change it
288 * here because we have no good reason (or value) to change it to.
292 static void dsa_port_bridge_tx_fwd_unoffload(struct dsa_port *dp,
293 struct net_device *bridge_dev)
295 int bridge_num = dp->bridge_num;
296 struct dsa_switch *ds = dp->ds;
298 /* No bridge TX forwarding offload => do nothing */
299 if (!ds->ops->port_bridge_tx_fwd_unoffload || dp->bridge_num == -1)
304 dsa_bridge_num_put(bridge_dev, bridge_num);
306 /* Notify the chips only once the offload has been deactivated, so
307 * that they can update their configuration accordingly.
309 ds->ops->port_bridge_tx_fwd_unoffload(ds, dp->index, bridge_dev,
313 static bool dsa_port_bridge_tx_fwd_offload(struct dsa_port *dp,
314 struct net_device *bridge_dev)
316 struct dsa_switch *ds = dp->ds;
319 if (!ds->ops->port_bridge_tx_fwd_offload)
322 bridge_num = dsa_bridge_num_get(bridge_dev,
323 ds->num_fwd_offloading_bridges);
327 dp->bridge_num = bridge_num;
329 /* Notify the driver */
330 err = ds->ops->port_bridge_tx_fwd_offload(ds, dp->index, bridge_dev,
333 dsa_port_bridge_tx_fwd_unoffload(dp, bridge_dev);
340 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
341 struct netlink_ext_ack *extack)
343 struct dsa_notifier_bridge_info info = {
344 .tree_index = dp->ds->dst->index,
345 .sw_index = dp->ds->index,
349 struct net_device *dev = dp->slave;
350 struct net_device *brport_dev;
354 /* Here the interface is already bridged. Reflect the current
355 * configuration so that drivers can program their chips accordingly.
359 brport_dev = dsa_port_to_bridge_port(dp);
361 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
365 tx_fwd_offload = dsa_port_bridge_tx_fwd_offload(dp, br);
367 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
368 &dsa_slave_switchdev_notifier,
369 &dsa_slave_switchdev_blocking_notifier,
370 tx_fwd_offload, extack);
372 goto out_rollback_unbridge;
374 err = dsa_port_switchdev_sync_attrs(dp, extack);
376 goto out_rollback_unoffload;
380 out_rollback_unoffload:
381 switchdev_bridge_port_unoffload(brport_dev, dp,
382 &dsa_slave_switchdev_notifier,
383 &dsa_slave_switchdev_blocking_notifier);
384 out_rollback_unbridge:
385 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
387 dp->bridge_dev = NULL;
391 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
393 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
395 /* Don't try to unoffload something that is not offloaded */
399 switchdev_bridge_port_unoffload(brport_dev, dp,
400 &dsa_slave_switchdev_notifier,
401 &dsa_slave_switchdev_blocking_notifier);
404 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
406 struct dsa_notifier_bridge_info info = {
407 .tree_index = dp->ds->dst->index,
408 .sw_index = dp->ds->index,
414 /* Here the port is already unbridged. Reflect the current configuration
415 * so that drivers can program their chips accordingly.
417 dp->bridge_dev = NULL;
419 dsa_port_bridge_tx_fwd_unoffload(dp, br);
421 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
424 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
425 dp->index, ERR_PTR(err));
427 dsa_port_switchdev_unsync_attrs(dp);
430 int dsa_port_lag_change(struct dsa_port *dp,
431 struct netdev_lag_lower_state_info *linfo)
433 struct dsa_notifier_lag_info info = {
434 .sw_index = dp->ds->index,
442 /* On statically configured aggregates (e.g. loadbalance
443 * without LACP) ports will always be tx_enabled, even if the
444 * link is down. Thus we require both link_up and tx_enabled
445 * in order to include it in the tx set.
447 tx_enabled = linfo->link_up && linfo->tx_enabled;
449 if (tx_enabled == dp->lag_tx_enabled)
452 dp->lag_tx_enabled = tx_enabled;
454 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
457 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
458 struct netdev_lag_upper_info *uinfo,
459 struct netlink_ext_ack *extack)
461 struct dsa_notifier_lag_info info = {
462 .sw_index = dp->ds->index,
467 struct net_device *bridge_dev;
470 dsa_lag_map(dp->ds->dst, lag);
473 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
477 bridge_dev = netdev_master_upper_dev_get(lag);
478 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
481 err = dsa_port_bridge_join(dp, bridge_dev, extack);
483 goto err_bridge_join;
488 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
491 dsa_lag_unmap(dp->ds->dst, lag);
495 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag)
498 dsa_port_pre_bridge_leave(dp, dp->bridge_dev);
501 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
503 struct dsa_notifier_lag_info info = {
504 .sw_index = dp->ds->index,
513 /* Port might have been part of a LAG that in turn was
514 * attached to a bridge.
517 dsa_port_bridge_leave(dp, dp->bridge_dev);
519 dp->lag_tx_enabled = false;
522 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
525 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
526 dp->index, ERR_PTR(err));
528 dsa_lag_unmap(dp->ds->dst, lag);
531 /* Must be called under rcu_read_lock() */
532 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
534 struct netlink_ext_ack *extack)
536 struct dsa_switch *ds = dp->ds;
539 /* VLAN awareness was off, so the question is "can we turn it on".
540 * We may have had 8021q uppers, those need to go. Make sure we don't
541 * enter an inconsistent state: deny changing the VLAN awareness state
542 * as long as we have 8021q uppers.
544 if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
545 struct net_device *upper_dev, *slave = dp->slave;
546 struct net_device *br = dp->bridge_dev;
547 struct list_head *iter;
549 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
550 struct bridge_vlan_info br_info;
553 if (!is_vlan_dev(upper_dev))
556 vid = vlan_dev_vlan_id(upper_dev);
558 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
559 * device, respectively the VID is not found, returning
560 * 0 means success, which is a failure for us here.
562 err = br_vlan_get_info(br, vid, &br_info);
564 NL_SET_ERR_MSG_MOD(extack,
565 "Must first remove VLAN uppers having VIDs also present in bridge");
571 if (!ds->vlan_filtering_is_global)
574 /* For cases where enabling/disabling VLAN awareness is global to the
575 * switch, we need to handle the case where multiple bridges span
576 * different ports of the same switch device and one of them has a
577 * different setting than what is being requested.
579 for (i = 0; i < ds->num_ports; i++) {
580 struct net_device *other_bridge;
582 other_bridge = dsa_to_port(ds, i)->bridge_dev;
585 /* If it's the same bridge, it also has same
586 * vlan_filtering setting => no need to check
588 if (other_bridge == dp->bridge_dev)
590 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
591 NL_SET_ERR_MSG_MOD(extack,
592 "VLAN filtering is a global setting");
599 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
600 struct netlink_ext_ack *extack)
602 bool old_vlan_filtering = dsa_port_is_vlan_filtering(dp);
603 struct dsa_switch *ds = dp->ds;
607 if (!ds->ops->port_vlan_filtering)
610 /* We are called from dsa_slave_switchdev_blocking_event(),
611 * which is not under rcu_read_lock(), unlike
612 * dsa_slave_switchdev_event().
615 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
620 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
623 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
628 if (ds->vlan_filtering_is_global) {
631 ds->vlan_filtering = vlan_filtering;
633 for (port = 0; port < ds->num_ports; port++) {
634 struct net_device *slave;
636 if (!dsa_is_user_port(ds, port))
639 /* We might be called in the unbind path, so not
640 * all slave devices might still be registered.
642 slave = dsa_to_port(ds, port)->slave;
646 err = dsa_slave_manage_vlan_filtering(slave,
652 dp->vlan_filtering = vlan_filtering;
654 err = dsa_slave_manage_vlan_filtering(dp->slave,
663 ds->ops->port_vlan_filtering(ds, dp->index, old_vlan_filtering, NULL);
665 if (ds->vlan_filtering_is_global)
666 ds->vlan_filtering = old_vlan_filtering;
668 dp->vlan_filtering = old_vlan_filtering;
673 /* This enforces legacy behavior for switch drivers which assume they can't
674 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
676 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
678 struct dsa_switch *ds = dp->ds;
683 return (!ds->configure_vlan_while_not_filtering &&
684 !br_vlan_enabled(dp->bridge_dev));
687 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
689 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
690 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
691 struct dsa_notifier_ageing_time_info info;
694 info.ageing_time = ageing_time;
696 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
700 dp->ageing_time = ageing_time;
705 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
706 struct switchdev_brport_flags flags,
707 struct netlink_ext_ack *extack)
709 struct dsa_switch *ds = dp->ds;
711 if (!ds->ops->port_pre_bridge_flags)
714 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
717 int dsa_port_bridge_flags(struct dsa_port *dp,
718 struct switchdev_brport_flags flags,
719 struct netlink_ext_ack *extack)
721 struct dsa_switch *ds = dp->ds;
724 if (!ds->ops->port_bridge_flags)
727 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
731 if (flags.mask & BR_LEARNING) {
732 bool learning = flags.val & BR_LEARNING;
734 if (learning == dp->learning)
737 if ((dp->learning && !learning) &&
738 (dp->stp_state == BR_STATE_LEARNING ||
739 dp->stp_state == BR_STATE_FORWARDING))
740 dsa_port_fast_age(dp);
742 dp->learning = learning;
748 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
751 struct dsa_notifier_mtu_info info = {
752 .sw_index = dp->ds->index,
753 .targeted_match = targeted_match,
758 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
761 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
764 struct dsa_notifier_fdb_info info = {
765 .sw_index = dp->ds->index,
771 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
774 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
777 struct dsa_notifier_fdb_info info = {
778 .sw_index = dp->ds->index,
785 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
788 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
791 struct dsa_notifier_fdb_info info = {
792 .sw_index = dp->ds->index,
797 struct dsa_port *cpu_dp = dp->cpu_dp;
800 err = dev_uc_add(cpu_dp->master, addr);
804 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
807 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
810 struct dsa_notifier_fdb_info info = {
811 .sw_index = dp->ds->index,
816 struct dsa_port *cpu_dp = dp->cpu_dp;
819 err = dev_uc_del(cpu_dp->master, addr);
823 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
826 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
828 struct dsa_switch *ds = dp->ds;
829 int port = dp->index;
831 if (!ds->ops->port_fdb_dump)
834 return ds->ops->port_fdb_dump(ds, port, cb, data);
837 int dsa_port_mdb_add(const struct dsa_port *dp,
838 const struct switchdev_obj_port_mdb *mdb)
840 struct dsa_notifier_mdb_info info = {
841 .sw_index = dp->ds->index,
846 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
849 int dsa_port_mdb_del(const struct dsa_port *dp,
850 const struct switchdev_obj_port_mdb *mdb)
852 struct dsa_notifier_mdb_info info = {
853 .sw_index = dp->ds->index,
858 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
861 int dsa_port_host_mdb_add(const struct dsa_port *dp,
862 const struct switchdev_obj_port_mdb *mdb)
864 struct dsa_notifier_mdb_info info = {
865 .sw_index = dp->ds->index,
869 struct dsa_port *cpu_dp = dp->cpu_dp;
872 err = dev_mc_add(cpu_dp->master, mdb->addr);
876 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
879 int dsa_port_host_mdb_del(const struct dsa_port *dp,
880 const struct switchdev_obj_port_mdb *mdb)
882 struct dsa_notifier_mdb_info info = {
883 .sw_index = dp->ds->index,
887 struct dsa_port *cpu_dp = dp->cpu_dp;
890 err = dev_mc_del(cpu_dp->master, mdb->addr);
894 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
897 int dsa_port_vlan_add(struct dsa_port *dp,
898 const struct switchdev_obj_port_vlan *vlan,
899 struct netlink_ext_ack *extack)
901 struct dsa_notifier_vlan_info info = {
902 .sw_index = dp->ds->index,
908 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
911 int dsa_port_vlan_del(struct dsa_port *dp,
912 const struct switchdev_obj_port_vlan *vlan)
914 struct dsa_notifier_vlan_info info = {
915 .sw_index = dp->ds->index,
920 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
923 int dsa_port_mrp_add(const struct dsa_port *dp,
924 const struct switchdev_obj_mrp *mrp)
926 struct dsa_notifier_mrp_info info = {
927 .sw_index = dp->ds->index,
932 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info);
935 int dsa_port_mrp_del(const struct dsa_port *dp,
936 const struct switchdev_obj_mrp *mrp)
938 struct dsa_notifier_mrp_info info = {
939 .sw_index = dp->ds->index,
944 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info);
947 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
948 const struct switchdev_obj_ring_role_mrp *mrp)
950 struct dsa_notifier_mrp_ring_role_info info = {
951 .sw_index = dp->ds->index,
956 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info);
959 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
960 const struct switchdev_obj_ring_role_mrp *mrp)
962 struct dsa_notifier_mrp_ring_role_info info = {
963 .sw_index = dp->ds->index,
968 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info);
971 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
972 const struct dsa_device_ops *tag_ops)
974 cpu_dp->rcv = tag_ops->rcv;
975 cpu_dp->tag_ops = tag_ops;
978 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
980 struct device_node *phy_dn;
981 struct phy_device *phydev;
983 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
987 phydev = of_phy_find_device(phy_dn);
990 return ERR_PTR(-EPROBE_DEFER);
997 static void dsa_port_phylink_validate(struct phylink_config *config,
998 unsigned long *supported,
999 struct phylink_link_state *state)
1001 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1002 struct dsa_switch *ds = dp->ds;
1004 if (!ds->ops->phylink_validate)
1007 ds->ops->phylink_validate(ds, dp->index, supported, state);
1010 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
1011 struct phylink_link_state *state)
1013 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1014 struct dsa_switch *ds = dp->ds;
1017 /* Only called for inband modes */
1018 if (!ds->ops->phylink_mac_link_state) {
1023 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
1025 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
1031 static void dsa_port_phylink_mac_config(struct phylink_config *config,
1033 const struct phylink_link_state *state)
1035 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1036 struct dsa_switch *ds = dp->ds;
1038 if (!ds->ops->phylink_mac_config)
1041 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
1044 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
1046 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1047 struct dsa_switch *ds = dp->ds;
1049 if (!ds->ops->phylink_mac_an_restart)
1052 ds->ops->phylink_mac_an_restart(ds, dp->index);
1055 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1057 phy_interface_t interface)
1059 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1060 struct phy_device *phydev = NULL;
1061 struct dsa_switch *ds = dp->ds;
1063 if (dsa_is_user_port(ds, dp->index))
1064 phydev = dp->slave->phydev;
1066 if (!ds->ops->phylink_mac_link_down) {
1067 if (ds->ops->adjust_link && phydev)
1068 ds->ops->adjust_link(ds, dp->index, phydev);
1072 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1075 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1076 struct phy_device *phydev,
1078 phy_interface_t interface,
1079 int speed, int duplex,
1080 bool tx_pause, bool rx_pause)
1082 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1083 struct dsa_switch *ds = dp->ds;
1085 if (!ds->ops->phylink_mac_link_up) {
1086 if (ds->ops->adjust_link && phydev)
1087 ds->ops->adjust_link(ds, dp->index, phydev);
1091 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1092 speed, duplex, tx_pause, rx_pause);
1095 const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1096 .validate = dsa_port_phylink_validate,
1097 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1098 .mac_config = dsa_port_phylink_mac_config,
1099 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1100 .mac_link_down = dsa_port_phylink_mac_link_down,
1101 .mac_link_up = dsa_port_phylink_mac_link_up,
1104 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1106 struct dsa_switch *ds = dp->ds;
1107 struct phy_device *phydev;
1108 int port = dp->index;
1111 phydev = dsa_port_get_phy_device(dp);
1116 return PTR_ERR(phydev);
1119 err = genphy_resume(phydev);
1123 err = genphy_read_status(phydev);
1127 err = genphy_suspend(phydev);
1132 if (ds->ops->adjust_link)
1133 ds->ops->adjust_link(ds, port, phydev);
1135 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1138 put_device(&phydev->mdio.dev);
1142 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
1144 struct device_node *dn = dp->dn;
1145 struct dsa_switch *ds = dp->ds;
1146 struct phy_device *phydev;
1147 int port = dp->index;
1148 phy_interface_t mode;
1151 err = of_phy_register_fixed_link(dn);
1154 "failed to register the fixed PHY of port %d\n",
1159 phydev = of_phy_find_device(dn);
1161 err = of_get_phy_mode(dn, &mode);
1163 mode = PHY_INTERFACE_MODE_NA;
1164 phydev->interface = mode;
1166 genphy_read_status(phydev);
1168 if (ds->ops->adjust_link)
1169 ds->ops->adjust_link(ds, port, phydev);
1171 put_device(&phydev->mdio.dev);
1176 static int dsa_port_phylink_register(struct dsa_port *dp)
1178 struct dsa_switch *ds = dp->ds;
1179 struct device_node *port_dn = dp->dn;
1180 phy_interface_t mode;
1183 err = of_get_phy_mode(port_dn, &mode);
1185 mode = PHY_INTERFACE_MODE_NA;
1187 dp->pl_config.dev = ds->dev;
1188 dp->pl_config.type = PHYLINK_DEV;
1189 dp->pl_config.pcs_poll = ds->pcs_poll;
1191 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
1192 mode, &dsa_port_phylink_mac_ops);
1193 if (IS_ERR(dp->pl)) {
1194 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1195 return PTR_ERR(dp->pl);
1198 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1199 if (err && err != -ENODEV) {
1200 pr_err("could not attach to PHY: %d\n", err);
1201 goto err_phy_connect;
1207 phylink_destroy(dp->pl);
1211 int dsa_port_link_register_of(struct dsa_port *dp)
1213 struct dsa_switch *ds = dp->ds;
1214 struct device_node *phy_np;
1215 int port = dp->index;
1217 if (!ds->ops->adjust_link) {
1218 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1219 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1220 if (ds->ops->phylink_mac_link_down)
1221 ds->ops->phylink_mac_link_down(ds, port,
1222 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1223 of_node_put(phy_np);
1224 return dsa_port_phylink_register(dp);
1226 of_node_put(phy_np);
1231 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1233 if (of_phy_is_fixed_link(dp->dn))
1234 return dsa_port_fixed_link_register_of(dp);
1236 return dsa_port_setup_phy_of(dp, true);
1239 void dsa_port_link_unregister_of(struct dsa_port *dp)
1241 struct dsa_switch *ds = dp->ds;
1243 if (!ds->ops->adjust_link && dp->pl) {
1245 phylink_disconnect_phy(dp->pl);
1247 phylink_destroy(dp->pl);
1252 if (of_phy_is_fixed_link(dp->dn))
1253 of_phy_deregister_fixed_link(dp->dn);
1255 dsa_port_setup_phy_of(dp, false);
1258 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
1260 struct phy_device *phydev;
1261 int ret = -EOPNOTSUPP;
1263 if (of_phy_is_fixed_link(dp->dn))
1266 phydev = dsa_port_get_phy_device(dp);
1267 if (IS_ERR_OR_NULL(phydev))
1270 ret = phy_ethtool_get_strings(phydev, data);
1271 put_device(&phydev->mdio.dev);
1275 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
1277 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
1279 struct phy_device *phydev;
1280 int ret = -EOPNOTSUPP;
1282 if (of_phy_is_fixed_link(dp->dn))
1285 phydev = dsa_port_get_phy_device(dp);
1286 if (IS_ERR_OR_NULL(phydev))
1289 ret = phy_ethtool_get_stats(phydev, NULL, data);
1290 put_device(&phydev->mdio.dev);
1294 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
1296 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
1298 struct phy_device *phydev;
1299 int ret = -EOPNOTSUPP;
1301 if (of_phy_is_fixed_link(dp->dn))
1304 phydev = dsa_port_get_phy_device(dp);
1305 if (IS_ERR_OR_NULL(phydev))
1308 ret = phy_ethtool_get_sset_count(phydev);
1309 put_device(&phydev->mdio.dev);
1313 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
1315 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1317 struct dsa_notifier_hsr_info info = {
1318 .sw_index = dp->ds->index,
1326 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
1333 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1335 struct dsa_notifier_hsr_info info = {
1336 .sw_index = dp->ds->index,
1344 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
1346 dev_err(dp->ds->dev,
1347 "port %d failed to notify DSA_NOTIFIER_HSR_LEAVE: %pe\n",
1348 dp->index, ERR_PTR(err));
1351 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid, bool broadcast)
1353 struct dsa_notifier_tag_8021q_vlan_info info = {
1354 .tree_index = dp->ds->dst->index,
1355 .sw_index = dp->ds->index,
1361 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1363 return dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1366 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid, bool broadcast)
1368 struct dsa_notifier_tag_8021q_vlan_info info = {
1369 .tree_index = dp->ds->dst->index,
1370 .sw_index = dp->ds->index,
1377 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1379 err = dsa_port_notify(dp, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1381 dev_err(dp->ds->dev,
1382 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1383 dp->index, vid, ERR_PTR(err));