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_fast_age(const struct dsa_port *dp)
35 struct dsa_switch *ds = dp->ds;
37 if (!ds->ops->port_fast_age)
40 ds->ops->port_fast_age(ds, dp->index);
43 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
45 struct dsa_switch *ds = dp->ds;
48 if (!ds->ops->port_stp_state_set)
51 ds->ops->port_stp_state_set(ds, port, state);
53 if (do_fast_age && dp->learning) {
54 /* Fast age FDB entries or flush appropriate forwarding database
55 * for the given port, if we are moving it from Learning or
56 * Forwarding state, to Disabled or Blocking or Listening state.
57 * Ports that were standalone before the STP state change don't
58 * need to fast age the FDB, since address learning is off in
62 if ((dp->stp_state == BR_STATE_LEARNING ||
63 dp->stp_state == BR_STATE_FORWARDING) &&
64 (state == BR_STATE_DISABLED ||
65 state == BR_STATE_BLOCKING ||
66 state == BR_STATE_LISTENING))
67 dsa_port_fast_age(dp);
70 dp->stp_state = state;
75 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
80 err = dsa_port_set_state(dp, state, do_fast_age);
82 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
85 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
87 struct dsa_switch *ds = dp->ds;
91 if (ds->ops->port_enable) {
92 err = ds->ops->port_enable(ds, port, phy);
98 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
101 phylink_start(dp->pl);
106 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
111 err = dsa_port_enable_rt(dp, phy);
117 void dsa_port_disable_rt(struct dsa_port *dp)
119 struct dsa_switch *ds = dp->ds;
120 int port = dp->index;
123 phylink_stop(dp->pl);
126 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
128 if (ds->ops->port_disable)
129 ds->ops->port_disable(ds, port);
132 void dsa_port_disable(struct dsa_port *dp)
135 dsa_port_disable_rt(dp);
139 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
140 struct netlink_ext_ack *extack)
142 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
144 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
147 for_each_set_bit(flag, &mask, 32) {
148 struct switchdev_brport_flags flags = {0};
150 flags.mask = BIT(flag);
152 if (br_port_flag_is_set(brport_dev, BIT(flag)))
153 flags.val = BIT(flag);
155 err = dsa_port_bridge_flags(dp, flags, extack);
156 if (err && err != -EOPNOTSUPP)
163 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
165 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
166 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
170 for_each_set_bit(flag, &mask, 32) {
171 struct switchdev_brport_flags flags = {0};
173 flags.mask = BIT(flag);
174 flags.val = val & BIT(flag);
176 err = dsa_port_bridge_flags(dp, flags, NULL);
177 if (err && err != -EOPNOTSUPP)
179 "failed to clear bridge port flag %lu: %pe\n",
180 flags.val, ERR_PTR(err));
184 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
185 struct netlink_ext_ack *extack)
187 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
188 struct net_device *br = dp->bridge_dev;
191 err = dsa_port_inherit_brport_flags(dp, extack);
195 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
196 if (err && err != -EOPNOTSUPP)
199 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
200 if (err && err != -EOPNOTSUPP)
203 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
204 if (err && err != -EOPNOTSUPP)
210 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp)
212 /* Configure the port for standalone mode (no address learning,
214 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
215 * when the user requests it through netlink or sysfs, but not
216 * automatically at port join or leave, so we need to handle resetting
217 * the brport flags ourselves. But we even prefer it that way, because
218 * otherwise, some setups might never get the notification they need,
219 * for example, when a port leaves a LAG that offloads the bridge,
220 * it becomes standalone, but as far as the bridge is concerned, no
223 dsa_port_clear_brport_flags(dp);
225 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
226 * so allow it to be in BR_STATE_FORWARDING to be kept functional
228 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
230 /* VLAN filtering is handled by dsa_switch_bridge_leave */
232 /* Ageing time may be global to the switch chip, so don't change it
233 * here because we have no good reason (or value) to change it to.
237 static int dsa_tree_find_bridge_num(struct dsa_switch_tree *dst,
238 struct net_device *bridge_dev)
242 /* When preparing the offload for a port, it will have a valid
243 * dp->bridge_dev pointer but a not yet valid dp->bridge_num.
244 * However there might be other ports having the same dp->bridge_dev
245 * and a valid dp->bridge_num, so just ignore this port.
247 list_for_each_entry(dp, &dst->ports, list)
248 if (dp->bridge_dev == bridge_dev && dp->bridge_num != -1)
249 return dp->bridge_num;
254 static void dsa_port_bridge_tx_fwd_unoffload(struct dsa_port *dp,
255 struct net_device *bridge_dev)
257 struct dsa_switch_tree *dst = dp->ds->dst;
258 int bridge_num = dp->bridge_num;
259 struct dsa_switch *ds = dp->ds;
261 /* No bridge TX forwarding offload => do nothing */
262 if (!ds->ops->port_bridge_tx_fwd_unoffload || dp->bridge_num == -1)
267 /* Check if the bridge is still in use, otherwise it is time
268 * to clean it up so we can reuse this bridge_num later.
270 if (!dsa_tree_find_bridge_num(dst, bridge_dev))
271 clear_bit(bridge_num, &dst->fwd_offloading_bridges);
273 /* Notify the chips only once the offload has been deactivated, so
274 * that they can update their configuration accordingly.
276 ds->ops->port_bridge_tx_fwd_unoffload(ds, dp->index, bridge_dev,
280 static bool dsa_port_bridge_tx_fwd_offload(struct dsa_port *dp,
281 struct net_device *bridge_dev)
283 struct dsa_switch_tree *dst = dp->ds->dst;
284 struct dsa_switch *ds = dp->ds;
287 if (!ds->ops->port_bridge_tx_fwd_offload)
290 bridge_num = dsa_tree_find_bridge_num(dst, bridge_dev);
291 if (bridge_num < 0) {
292 /* First port that offloads TX forwarding for this bridge */
293 bridge_num = find_first_zero_bit(&dst->fwd_offloading_bridges,
294 DSA_MAX_NUM_OFFLOADING_BRIDGES);
295 if (bridge_num >= ds->num_fwd_offloading_bridges)
298 set_bit(bridge_num, &dst->fwd_offloading_bridges);
301 dp->bridge_num = bridge_num;
303 /* Notify the driver */
304 err = ds->ops->port_bridge_tx_fwd_offload(ds, dp->index, bridge_dev,
307 dsa_port_bridge_tx_fwd_unoffload(dp, bridge_dev);
314 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
315 struct netlink_ext_ack *extack)
317 struct dsa_notifier_bridge_info info = {
318 .tree_index = dp->ds->dst->index,
319 .sw_index = dp->ds->index,
323 struct net_device *dev = dp->slave;
324 struct net_device *brport_dev;
328 /* Here the interface is already bridged. Reflect the current
329 * configuration so that drivers can program their chips accordingly.
333 brport_dev = dsa_port_to_bridge_port(dp);
335 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
339 tx_fwd_offload = dsa_port_bridge_tx_fwd_offload(dp, br);
341 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
342 &dsa_slave_switchdev_notifier,
343 &dsa_slave_switchdev_blocking_notifier,
344 tx_fwd_offload, extack);
346 goto out_rollback_unbridge;
348 err = dsa_port_switchdev_sync_attrs(dp, extack);
350 goto out_rollback_unoffload;
354 out_rollback_unoffload:
355 switchdev_bridge_port_unoffload(brport_dev, dp,
356 &dsa_slave_switchdev_notifier,
357 &dsa_slave_switchdev_blocking_notifier);
358 out_rollback_unbridge:
359 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
361 dp->bridge_dev = NULL;
365 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
367 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
369 switchdev_bridge_port_unoffload(brport_dev, dp,
370 &dsa_slave_switchdev_notifier,
371 &dsa_slave_switchdev_blocking_notifier);
374 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
376 struct dsa_notifier_bridge_info info = {
377 .tree_index = dp->ds->dst->index,
378 .sw_index = dp->ds->index,
384 /* Here the port is already unbridged. Reflect the current configuration
385 * so that drivers can program their chips accordingly.
387 dp->bridge_dev = NULL;
389 dsa_port_bridge_tx_fwd_unoffload(dp, br);
391 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
393 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
395 dsa_port_switchdev_unsync_attrs(dp);
398 int dsa_port_lag_change(struct dsa_port *dp,
399 struct netdev_lag_lower_state_info *linfo)
401 struct dsa_notifier_lag_info info = {
402 .sw_index = dp->ds->index,
410 /* On statically configured aggregates (e.g. loadbalance
411 * without LACP) ports will always be tx_enabled, even if the
412 * link is down. Thus we require both link_up and tx_enabled
413 * in order to include it in the tx set.
415 tx_enabled = linfo->link_up && linfo->tx_enabled;
417 if (tx_enabled == dp->lag_tx_enabled)
420 dp->lag_tx_enabled = tx_enabled;
422 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
425 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
426 struct netdev_lag_upper_info *uinfo,
427 struct netlink_ext_ack *extack)
429 struct dsa_notifier_lag_info info = {
430 .sw_index = dp->ds->index,
435 struct net_device *bridge_dev;
438 dsa_lag_map(dp->ds->dst, lag);
441 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
445 bridge_dev = netdev_master_upper_dev_get(lag);
446 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
449 err = dsa_port_bridge_join(dp, bridge_dev, extack);
451 goto err_bridge_join;
456 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
459 dsa_lag_unmap(dp->ds->dst, lag);
463 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag)
466 dsa_port_pre_bridge_leave(dp, dp->bridge_dev);
469 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
471 struct dsa_notifier_lag_info info = {
472 .sw_index = dp->ds->index,
481 /* Port might have been part of a LAG that in turn was
482 * attached to a bridge.
485 dsa_port_bridge_leave(dp, dp->bridge_dev);
487 dp->lag_tx_enabled = false;
490 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
492 pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n",
495 dsa_lag_unmap(dp->ds->dst, lag);
498 /* Must be called under rcu_read_lock() */
499 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
501 struct netlink_ext_ack *extack)
503 struct dsa_switch *ds = dp->ds;
506 /* VLAN awareness was off, so the question is "can we turn it on".
507 * We may have had 8021q uppers, those need to go. Make sure we don't
508 * enter an inconsistent state: deny changing the VLAN awareness state
509 * as long as we have 8021q uppers.
511 if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
512 struct net_device *upper_dev, *slave = dp->slave;
513 struct net_device *br = dp->bridge_dev;
514 struct list_head *iter;
516 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
517 struct bridge_vlan_info br_info;
520 if (!is_vlan_dev(upper_dev))
523 vid = vlan_dev_vlan_id(upper_dev);
525 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
526 * device, respectively the VID is not found, returning
527 * 0 means success, which is a failure for us here.
529 err = br_vlan_get_info(br, vid, &br_info);
531 NL_SET_ERR_MSG_MOD(extack,
532 "Must first remove VLAN uppers having VIDs also present in bridge");
538 if (!ds->vlan_filtering_is_global)
541 /* For cases where enabling/disabling VLAN awareness is global to the
542 * switch, we need to handle the case where multiple bridges span
543 * different ports of the same switch device and one of them has a
544 * different setting than what is being requested.
546 for (i = 0; i < ds->num_ports; i++) {
547 struct net_device *other_bridge;
549 other_bridge = dsa_to_port(ds, i)->bridge_dev;
552 /* If it's the same bridge, it also has same
553 * vlan_filtering setting => no need to check
555 if (other_bridge == dp->bridge_dev)
557 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
558 NL_SET_ERR_MSG_MOD(extack,
559 "VLAN filtering is a global setting");
566 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
567 struct netlink_ext_ack *extack)
569 struct dsa_switch *ds = dp->ds;
573 if (!ds->ops->port_vlan_filtering)
576 /* We are called from dsa_slave_switchdev_blocking_event(),
577 * which is not under rcu_read_lock(), unlike
578 * dsa_slave_switchdev_event().
581 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
586 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
589 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
594 if (ds->vlan_filtering_is_global)
595 ds->vlan_filtering = vlan_filtering;
597 dp->vlan_filtering = vlan_filtering;
602 /* This enforces legacy behavior for switch drivers which assume they can't
603 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
605 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
607 struct dsa_switch *ds = dp->ds;
612 return (!ds->configure_vlan_while_not_filtering &&
613 !br_vlan_enabled(dp->bridge_dev));
616 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
618 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
619 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
620 struct dsa_notifier_ageing_time_info info;
623 info.ageing_time = ageing_time;
625 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
629 dp->ageing_time = ageing_time;
634 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
635 struct switchdev_brport_flags flags,
636 struct netlink_ext_ack *extack)
638 struct dsa_switch *ds = dp->ds;
640 if (!ds->ops->port_pre_bridge_flags)
643 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
646 int dsa_port_bridge_flags(struct dsa_port *dp,
647 struct switchdev_brport_flags flags,
648 struct netlink_ext_ack *extack)
650 struct dsa_switch *ds = dp->ds;
653 if (!ds->ops->port_bridge_flags)
656 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
660 if (flags.mask & BR_LEARNING) {
661 bool learning = flags.val & BR_LEARNING;
663 if (learning == dp->learning)
666 if (dp->learning && !learning)
667 dsa_port_fast_age(dp);
669 dp->learning = learning;
675 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
678 struct dsa_notifier_mtu_info info = {
679 .sw_index = dp->ds->index,
680 .targeted_match = targeted_match,
685 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
688 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
691 struct dsa_notifier_fdb_info info = {
692 .sw_index = dp->ds->index,
698 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
701 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
704 struct dsa_notifier_fdb_info info = {
705 .sw_index = dp->ds->index,
712 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
715 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
718 struct dsa_notifier_fdb_info info = {
719 .sw_index = dp->ds->index,
724 struct dsa_port *cpu_dp = dp->cpu_dp;
727 err = dev_uc_add(cpu_dp->master, addr);
731 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
734 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
737 struct dsa_notifier_fdb_info info = {
738 .sw_index = dp->ds->index,
743 struct dsa_port *cpu_dp = dp->cpu_dp;
746 err = dev_uc_del(cpu_dp->master, addr);
750 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
753 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
755 struct dsa_switch *ds = dp->ds;
756 int port = dp->index;
758 if (!ds->ops->port_fdb_dump)
761 return ds->ops->port_fdb_dump(ds, port, cb, data);
764 int dsa_port_mdb_add(const struct dsa_port *dp,
765 const struct switchdev_obj_port_mdb *mdb)
767 struct dsa_notifier_mdb_info info = {
768 .sw_index = dp->ds->index,
773 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
776 int dsa_port_mdb_del(const struct dsa_port *dp,
777 const struct switchdev_obj_port_mdb *mdb)
779 struct dsa_notifier_mdb_info info = {
780 .sw_index = dp->ds->index,
785 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
788 int dsa_port_host_mdb_add(const struct dsa_port *dp,
789 const struct switchdev_obj_port_mdb *mdb)
791 struct dsa_notifier_mdb_info info = {
792 .sw_index = dp->ds->index,
796 struct dsa_port *cpu_dp = dp->cpu_dp;
799 err = dev_mc_add(cpu_dp->master, mdb->addr);
803 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
806 int dsa_port_host_mdb_del(const struct dsa_port *dp,
807 const struct switchdev_obj_port_mdb *mdb)
809 struct dsa_notifier_mdb_info info = {
810 .sw_index = dp->ds->index,
814 struct dsa_port *cpu_dp = dp->cpu_dp;
817 err = dev_mc_del(cpu_dp->master, mdb->addr);
821 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
824 int dsa_port_vlan_add(struct dsa_port *dp,
825 const struct switchdev_obj_port_vlan *vlan,
826 struct netlink_ext_ack *extack)
828 struct dsa_notifier_vlan_info info = {
829 .sw_index = dp->ds->index,
835 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
838 int dsa_port_vlan_del(struct dsa_port *dp,
839 const struct switchdev_obj_port_vlan *vlan)
841 struct dsa_notifier_vlan_info info = {
842 .sw_index = dp->ds->index,
847 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
850 int dsa_port_mrp_add(const struct dsa_port *dp,
851 const struct switchdev_obj_mrp *mrp)
853 struct dsa_notifier_mrp_info info = {
854 .sw_index = dp->ds->index,
859 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info);
862 int dsa_port_mrp_del(const struct dsa_port *dp,
863 const struct switchdev_obj_mrp *mrp)
865 struct dsa_notifier_mrp_info info = {
866 .sw_index = dp->ds->index,
871 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info);
874 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
875 const struct switchdev_obj_ring_role_mrp *mrp)
877 struct dsa_notifier_mrp_ring_role_info info = {
878 .sw_index = dp->ds->index,
883 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info);
886 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
887 const struct switchdev_obj_ring_role_mrp *mrp)
889 struct dsa_notifier_mrp_ring_role_info info = {
890 .sw_index = dp->ds->index,
895 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info);
898 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
899 const struct dsa_device_ops *tag_ops)
901 cpu_dp->rcv = tag_ops->rcv;
902 cpu_dp->tag_ops = tag_ops;
905 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
907 struct device_node *phy_dn;
908 struct phy_device *phydev;
910 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
914 phydev = of_phy_find_device(phy_dn);
917 return ERR_PTR(-EPROBE_DEFER);
924 static void dsa_port_phylink_validate(struct phylink_config *config,
925 unsigned long *supported,
926 struct phylink_link_state *state)
928 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
929 struct dsa_switch *ds = dp->ds;
931 if (!ds->ops->phylink_validate)
934 ds->ops->phylink_validate(ds, dp->index, supported, state);
937 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
938 struct phylink_link_state *state)
940 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
941 struct dsa_switch *ds = dp->ds;
944 /* Only called for inband modes */
945 if (!ds->ops->phylink_mac_link_state) {
950 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
952 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
958 static void dsa_port_phylink_mac_config(struct phylink_config *config,
960 const struct phylink_link_state *state)
962 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
963 struct dsa_switch *ds = dp->ds;
965 if (!ds->ops->phylink_mac_config)
968 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
971 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
973 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
974 struct dsa_switch *ds = dp->ds;
976 if (!ds->ops->phylink_mac_an_restart)
979 ds->ops->phylink_mac_an_restart(ds, dp->index);
982 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
984 phy_interface_t interface)
986 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
987 struct phy_device *phydev = NULL;
988 struct dsa_switch *ds = dp->ds;
990 if (dsa_is_user_port(ds, dp->index))
991 phydev = dp->slave->phydev;
993 if (!ds->ops->phylink_mac_link_down) {
994 if (ds->ops->adjust_link && phydev)
995 ds->ops->adjust_link(ds, dp->index, phydev);
999 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1002 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1003 struct phy_device *phydev,
1005 phy_interface_t interface,
1006 int speed, int duplex,
1007 bool tx_pause, bool rx_pause)
1009 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1010 struct dsa_switch *ds = dp->ds;
1012 if (!ds->ops->phylink_mac_link_up) {
1013 if (ds->ops->adjust_link && phydev)
1014 ds->ops->adjust_link(ds, dp->index, phydev);
1018 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1019 speed, duplex, tx_pause, rx_pause);
1022 const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1023 .validate = dsa_port_phylink_validate,
1024 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1025 .mac_config = dsa_port_phylink_mac_config,
1026 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1027 .mac_link_down = dsa_port_phylink_mac_link_down,
1028 .mac_link_up = dsa_port_phylink_mac_link_up,
1031 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1033 struct dsa_switch *ds = dp->ds;
1034 struct phy_device *phydev;
1035 int port = dp->index;
1038 phydev = dsa_port_get_phy_device(dp);
1043 return PTR_ERR(phydev);
1046 err = genphy_resume(phydev);
1050 err = genphy_read_status(phydev);
1054 err = genphy_suspend(phydev);
1059 if (ds->ops->adjust_link)
1060 ds->ops->adjust_link(ds, port, phydev);
1062 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1065 put_device(&phydev->mdio.dev);
1069 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
1071 struct device_node *dn = dp->dn;
1072 struct dsa_switch *ds = dp->ds;
1073 struct phy_device *phydev;
1074 int port = dp->index;
1075 phy_interface_t mode;
1078 err = of_phy_register_fixed_link(dn);
1081 "failed to register the fixed PHY of port %d\n",
1086 phydev = of_phy_find_device(dn);
1088 err = of_get_phy_mode(dn, &mode);
1090 mode = PHY_INTERFACE_MODE_NA;
1091 phydev->interface = mode;
1093 genphy_read_status(phydev);
1095 if (ds->ops->adjust_link)
1096 ds->ops->adjust_link(ds, port, phydev);
1098 put_device(&phydev->mdio.dev);
1103 static int dsa_port_phylink_register(struct dsa_port *dp)
1105 struct dsa_switch *ds = dp->ds;
1106 struct device_node *port_dn = dp->dn;
1107 phy_interface_t mode;
1110 err = of_get_phy_mode(port_dn, &mode);
1112 mode = PHY_INTERFACE_MODE_NA;
1114 dp->pl_config.dev = ds->dev;
1115 dp->pl_config.type = PHYLINK_DEV;
1116 dp->pl_config.pcs_poll = ds->pcs_poll;
1118 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
1119 mode, &dsa_port_phylink_mac_ops);
1120 if (IS_ERR(dp->pl)) {
1121 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1122 return PTR_ERR(dp->pl);
1125 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1126 if (err && err != -ENODEV) {
1127 pr_err("could not attach to PHY: %d\n", err);
1128 goto err_phy_connect;
1134 phylink_destroy(dp->pl);
1138 int dsa_port_link_register_of(struct dsa_port *dp)
1140 struct dsa_switch *ds = dp->ds;
1141 struct device_node *phy_np;
1142 int port = dp->index;
1144 if (!ds->ops->adjust_link) {
1145 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1146 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1147 if (ds->ops->phylink_mac_link_down)
1148 ds->ops->phylink_mac_link_down(ds, port,
1149 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1150 return dsa_port_phylink_register(dp);
1156 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1158 if (of_phy_is_fixed_link(dp->dn))
1159 return dsa_port_fixed_link_register_of(dp);
1161 return dsa_port_setup_phy_of(dp, true);
1164 void dsa_port_link_unregister_of(struct dsa_port *dp)
1166 struct dsa_switch *ds = dp->ds;
1168 if (!ds->ops->adjust_link && dp->pl) {
1170 phylink_disconnect_phy(dp->pl);
1172 phylink_destroy(dp->pl);
1177 if (of_phy_is_fixed_link(dp->dn))
1178 of_phy_deregister_fixed_link(dp->dn);
1180 dsa_port_setup_phy_of(dp, false);
1183 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
1185 struct phy_device *phydev;
1186 int ret = -EOPNOTSUPP;
1188 if (of_phy_is_fixed_link(dp->dn))
1191 phydev = dsa_port_get_phy_device(dp);
1192 if (IS_ERR_OR_NULL(phydev))
1195 ret = phy_ethtool_get_strings(phydev, data);
1196 put_device(&phydev->mdio.dev);
1200 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
1202 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
1204 struct phy_device *phydev;
1205 int ret = -EOPNOTSUPP;
1207 if (of_phy_is_fixed_link(dp->dn))
1210 phydev = dsa_port_get_phy_device(dp);
1211 if (IS_ERR_OR_NULL(phydev))
1214 ret = phy_ethtool_get_stats(phydev, NULL, data);
1215 put_device(&phydev->mdio.dev);
1219 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
1221 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
1223 struct phy_device *phydev;
1224 int ret = -EOPNOTSUPP;
1226 if (of_phy_is_fixed_link(dp->dn))
1229 phydev = dsa_port_get_phy_device(dp);
1230 if (IS_ERR_OR_NULL(phydev))
1233 ret = phy_ethtool_get_sset_count(phydev);
1234 put_device(&phydev->mdio.dev);
1238 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
1240 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1242 struct dsa_notifier_hsr_info info = {
1243 .sw_index = dp->ds->index,
1251 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
1258 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1260 struct dsa_notifier_hsr_info info = {
1261 .sw_index = dp->ds->index,
1269 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
1271 pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n");
1274 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid)
1276 struct dsa_notifier_tag_8021q_vlan_info info = {
1277 .tree_index = dp->ds->dst->index,
1278 .sw_index = dp->ds->index,
1283 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1286 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid)
1288 struct dsa_notifier_tag_8021q_vlan_info info = {
1289 .tree_index = dp->ds->dst->index,
1290 .sw_index = dp->ds->index,
1296 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1298 pr_err("DSA: failed to notify tag_8021q VLAN deletion: %pe\n",