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 int dsa_port_set_state(struct dsa_port *dp, u8 state, bool do_fast_age)
65 struct dsa_switch *ds = dp->ds;
68 if (!ds->ops->port_stp_state_set)
71 ds->ops->port_stp_state_set(ds, port, state);
73 if (do_fast_age && dp->learning) {
74 /* Fast age FDB entries or flush appropriate forwarding database
75 * for the given port, if we are moving it from Learning or
76 * Forwarding state, to Disabled or Blocking or Listening state.
77 * Ports that were standalone before the STP state change don't
78 * need to fast age the FDB, since address learning is off in
82 if ((dp->stp_state == BR_STATE_LEARNING ||
83 dp->stp_state == BR_STATE_FORWARDING) &&
84 (state == BR_STATE_DISABLED ||
85 state == BR_STATE_BLOCKING ||
86 state == BR_STATE_LISTENING))
87 dsa_port_fast_age(dp);
90 dp->stp_state = state;
95 static void dsa_port_set_state_now(struct dsa_port *dp, u8 state,
100 err = dsa_port_set_state(dp, state, do_fast_age);
102 pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
105 int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
107 struct dsa_switch *ds = dp->ds;
108 int port = dp->index;
111 if (ds->ops->port_enable) {
112 err = ds->ops->port_enable(ds, port, phy);
118 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, false);
121 phylink_start(dp->pl);
126 int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
131 err = dsa_port_enable_rt(dp, phy);
137 void dsa_port_disable_rt(struct dsa_port *dp)
139 struct dsa_switch *ds = dp->ds;
140 int port = dp->index;
143 phylink_stop(dp->pl);
146 dsa_port_set_state_now(dp, BR_STATE_DISABLED, false);
148 if (ds->ops->port_disable)
149 ds->ops->port_disable(ds, port);
152 void dsa_port_disable(struct dsa_port *dp)
155 dsa_port_disable_rt(dp);
159 static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
160 struct netlink_ext_ack *extack)
162 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
164 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
167 for_each_set_bit(flag, &mask, 32) {
168 struct switchdev_brport_flags flags = {0};
170 flags.mask = BIT(flag);
172 if (br_port_flag_is_set(brport_dev, BIT(flag)))
173 flags.val = BIT(flag);
175 err = dsa_port_bridge_flags(dp, flags, extack);
176 if (err && err != -EOPNOTSUPP)
183 static void dsa_port_clear_brport_flags(struct dsa_port *dp)
185 const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
186 const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
190 for_each_set_bit(flag, &mask, 32) {
191 struct switchdev_brport_flags flags = {0};
193 flags.mask = BIT(flag);
194 flags.val = val & BIT(flag);
196 err = dsa_port_bridge_flags(dp, flags, NULL);
197 if (err && err != -EOPNOTSUPP)
199 "failed to clear bridge port flag %lu: %pe\n",
200 flags.val, ERR_PTR(err));
204 static int dsa_port_switchdev_sync_attrs(struct dsa_port *dp,
205 struct netlink_ext_ack *extack)
207 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
208 struct net_device *br = dp->bridge_dev;
211 err = dsa_port_inherit_brport_flags(dp, extack);
215 err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev), false);
216 if (err && err != -EOPNOTSUPP)
219 err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
220 if (err && err != -EOPNOTSUPP)
223 err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
224 if (err && err != -EOPNOTSUPP)
230 static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp)
232 /* Configure the port for standalone mode (no address learning,
234 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
235 * when the user requests it through netlink or sysfs, but not
236 * automatically at port join or leave, so we need to handle resetting
237 * the brport flags ourselves. But we even prefer it that way, because
238 * otherwise, some setups might never get the notification they need,
239 * for example, when a port leaves a LAG that offloads the bridge,
240 * it becomes standalone, but as far as the bridge is concerned, no
243 dsa_port_clear_brport_flags(dp);
245 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
246 * so allow it to be in BR_STATE_FORWARDING to be kept functional
248 dsa_port_set_state_now(dp, BR_STATE_FORWARDING, true);
250 /* VLAN filtering is handled by dsa_switch_bridge_leave */
252 /* Ageing time may be global to the switch chip, so don't change it
253 * here because we have no good reason (or value) to change it to.
257 static int dsa_tree_find_bridge_num(struct dsa_switch_tree *dst,
258 struct net_device *bridge_dev)
262 /* When preparing the offload for a port, it will have a valid
263 * dp->bridge_dev pointer but a not yet valid dp->bridge_num.
264 * However there might be other ports having the same dp->bridge_dev
265 * and a valid dp->bridge_num, so just ignore this port.
267 list_for_each_entry(dp, &dst->ports, list)
268 if (dp->bridge_dev == bridge_dev && dp->bridge_num != -1)
269 return dp->bridge_num;
274 static void dsa_port_bridge_tx_fwd_unoffload(struct dsa_port *dp,
275 struct net_device *bridge_dev)
277 struct dsa_switch_tree *dst = dp->ds->dst;
278 int bridge_num = dp->bridge_num;
279 struct dsa_switch *ds = dp->ds;
281 /* No bridge TX forwarding offload => do nothing */
282 if (!ds->ops->port_bridge_tx_fwd_unoffload || dp->bridge_num == -1)
287 /* Check if the bridge is still in use, otherwise it is time
288 * to clean it up so we can reuse this bridge_num later.
290 if (!dsa_tree_find_bridge_num(dst, bridge_dev))
291 clear_bit(bridge_num, &dst->fwd_offloading_bridges);
293 /* Notify the chips only once the offload has been deactivated, so
294 * that they can update their configuration accordingly.
296 ds->ops->port_bridge_tx_fwd_unoffload(ds, dp->index, bridge_dev,
300 static bool dsa_port_bridge_tx_fwd_offload(struct dsa_port *dp,
301 struct net_device *bridge_dev)
303 struct dsa_switch_tree *dst = dp->ds->dst;
304 struct dsa_switch *ds = dp->ds;
307 if (!ds->ops->port_bridge_tx_fwd_offload)
310 bridge_num = dsa_tree_find_bridge_num(dst, bridge_dev);
311 if (bridge_num < 0) {
312 /* First port that offloads TX forwarding for this bridge */
313 bridge_num = find_first_zero_bit(&dst->fwd_offloading_bridges,
314 DSA_MAX_NUM_OFFLOADING_BRIDGES);
315 if (bridge_num >= ds->num_fwd_offloading_bridges)
318 set_bit(bridge_num, &dst->fwd_offloading_bridges);
321 dp->bridge_num = bridge_num;
323 /* Notify the driver */
324 err = ds->ops->port_bridge_tx_fwd_offload(ds, dp->index, bridge_dev,
327 dsa_port_bridge_tx_fwd_unoffload(dp, bridge_dev);
334 int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
335 struct netlink_ext_ack *extack)
337 struct dsa_notifier_bridge_info info = {
338 .tree_index = dp->ds->dst->index,
339 .sw_index = dp->ds->index,
343 struct net_device *dev = dp->slave;
344 struct net_device *brport_dev;
348 /* Here the interface is already bridged. Reflect the current
349 * configuration so that drivers can program their chips accordingly.
353 brport_dev = dsa_port_to_bridge_port(dp);
355 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
359 tx_fwd_offload = dsa_port_bridge_tx_fwd_offload(dp, br);
361 err = switchdev_bridge_port_offload(brport_dev, dev, dp,
362 &dsa_slave_switchdev_notifier,
363 &dsa_slave_switchdev_blocking_notifier,
364 tx_fwd_offload, extack);
366 goto out_rollback_unbridge;
368 err = dsa_port_switchdev_sync_attrs(dp, extack);
370 goto out_rollback_unoffload;
374 out_rollback_unoffload:
375 switchdev_bridge_port_unoffload(brport_dev, dp,
376 &dsa_slave_switchdev_notifier,
377 &dsa_slave_switchdev_blocking_notifier);
378 out_rollback_unbridge:
379 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
381 dp->bridge_dev = NULL;
385 void dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br)
387 struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
389 switchdev_bridge_port_unoffload(brport_dev, dp,
390 &dsa_slave_switchdev_notifier,
391 &dsa_slave_switchdev_blocking_notifier);
394 void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
396 struct dsa_notifier_bridge_info info = {
397 .tree_index = dp->ds->dst->index,
398 .sw_index = dp->ds->index,
404 /* Here the port is already unbridged. Reflect the current configuration
405 * so that drivers can program their chips accordingly.
407 dp->bridge_dev = NULL;
409 dsa_port_bridge_tx_fwd_unoffload(dp, br);
411 err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
413 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
415 dsa_port_switchdev_unsync_attrs(dp);
418 int dsa_port_lag_change(struct dsa_port *dp,
419 struct netdev_lag_lower_state_info *linfo)
421 struct dsa_notifier_lag_info info = {
422 .sw_index = dp->ds->index,
430 /* On statically configured aggregates (e.g. loadbalance
431 * without LACP) ports will always be tx_enabled, even if the
432 * link is down. Thus we require both link_up and tx_enabled
433 * in order to include it in the tx set.
435 tx_enabled = linfo->link_up && linfo->tx_enabled;
437 if (tx_enabled == dp->lag_tx_enabled)
440 dp->lag_tx_enabled = tx_enabled;
442 return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
445 int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
446 struct netdev_lag_upper_info *uinfo,
447 struct netlink_ext_ack *extack)
449 struct dsa_notifier_lag_info info = {
450 .sw_index = dp->ds->index,
455 struct net_device *bridge_dev;
458 dsa_lag_map(dp->ds->dst, lag);
461 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
465 bridge_dev = netdev_master_upper_dev_get(lag);
466 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
469 err = dsa_port_bridge_join(dp, bridge_dev, extack);
471 goto err_bridge_join;
476 dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
479 dsa_lag_unmap(dp->ds->dst, lag);
483 void dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag)
486 dsa_port_pre_bridge_leave(dp, dp->bridge_dev);
489 void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
491 struct dsa_notifier_lag_info info = {
492 .sw_index = dp->ds->index,
501 /* Port might have been part of a LAG that in turn was
502 * attached to a bridge.
505 dsa_port_bridge_leave(dp, dp->bridge_dev);
507 dp->lag_tx_enabled = false;
510 err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
512 pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n",
515 dsa_lag_unmap(dp->ds->dst, lag);
518 /* Must be called under rcu_read_lock() */
519 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
521 struct netlink_ext_ack *extack)
523 struct dsa_switch *ds = dp->ds;
526 /* VLAN awareness was off, so the question is "can we turn it on".
527 * We may have had 8021q uppers, those need to go. Make sure we don't
528 * enter an inconsistent state: deny changing the VLAN awareness state
529 * as long as we have 8021q uppers.
531 if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
532 struct net_device *upper_dev, *slave = dp->slave;
533 struct net_device *br = dp->bridge_dev;
534 struct list_head *iter;
536 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
537 struct bridge_vlan_info br_info;
540 if (!is_vlan_dev(upper_dev))
543 vid = vlan_dev_vlan_id(upper_dev);
545 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
546 * device, respectively the VID is not found, returning
547 * 0 means success, which is a failure for us here.
549 err = br_vlan_get_info(br, vid, &br_info);
551 NL_SET_ERR_MSG_MOD(extack,
552 "Must first remove VLAN uppers having VIDs also present in bridge");
558 if (!ds->vlan_filtering_is_global)
561 /* For cases where enabling/disabling VLAN awareness is global to the
562 * switch, we need to handle the case where multiple bridges span
563 * different ports of the same switch device and one of them has a
564 * different setting than what is being requested.
566 for (i = 0; i < ds->num_ports; i++) {
567 struct net_device *other_bridge;
569 other_bridge = dsa_to_port(ds, i)->bridge_dev;
572 /* If it's the same bridge, it also has same
573 * vlan_filtering setting => no need to check
575 if (other_bridge == dp->bridge_dev)
577 if (br_vlan_enabled(other_bridge) != vlan_filtering) {
578 NL_SET_ERR_MSG_MOD(extack,
579 "VLAN filtering is a global setting");
586 int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
587 struct netlink_ext_ack *extack)
589 struct dsa_switch *ds = dp->ds;
593 if (!ds->ops->port_vlan_filtering)
596 /* We are called from dsa_slave_switchdev_blocking_event(),
597 * which is not under rcu_read_lock(), unlike
598 * dsa_slave_switchdev_event().
601 apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
606 if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
609 err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
614 if (ds->vlan_filtering_is_global)
615 ds->vlan_filtering = vlan_filtering;
617 dp->vlan_filtering = vlan_filtering;
622 /* This enforces legacy behavior for switch drivers which assume they can't
623 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
625 bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
627 struct dsa_switch *ds = dp->ds;
632 return (!ds->configure_vlan_while_not_filtering &&
633 !br_vlan_enabled(dp->bridge_dev));
636 int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
638 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
639 unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
640 struct dsa_notifier_ageing_time_info info;
643 info.ageing_time = ageing_time;
645 err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
649 dp->ageing_time = ageing_time;
654 int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
655 struct switchdev_brport_flags flags,
656 struct netlink_ext_ack *extack)
658 struct dsa_switch *ds = dp->ds;
660 if (!ds->ops->port_pre_bridge_flags)
663 return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
666 int dsa_port_bridge_flags(struct dsa_port *dp,
667 struct switchdev_brport_flags flags,
668 struct netlink_ext_ack *extack)
670 struct dsa_switch *ds = dp->ds;
673 if (!ds->ops->port_bridge_flags)
676 err = ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
680 if (flags.mask & BR_LEARNING) {
681 bool learning = flags.val & BR_LEARNING;
683 if (learning == dp->learning)
686 if (dp->learning && !learning)
687 dsa_port_fast_age(dp);
689 dp->learning = learning;
695 int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
698 struct dsa_notifier_mtu_info info = {
699 .sw_index = dp->ds->index,
700 .targeted_match = targeted_match,
705 return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
708 int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
711 struct dsa_notifier_fdb_info info = {
712 .sw_index = dp->ds->index,
718 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
721 int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
724 struct dsa_notifier_fdb_info info = {
725 .sw_index = dp->ds->index,
732 return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
735 int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
738 struct dsa_notifier_fdb_info info = {
739 .sw_index = dp->ds->index,
744 struct dsa_port *cpu_dp = dp->cpu_dp;
747 err = dev_uc_add(cpu_dp->master, addr);
751 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
754 int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
757 struct dsa_notifier_fdb_info info = {
758 .sw_index = dp->ds->index,
763 struct dsa_port *cpu_dp = dp->cpu_dp;
766 err = dev_uc_del(cpu_dp->master, addr);
770 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
773 int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
775 struct dsa_switch *ds = dp->ds;
776 int port = dp->index;
778 if (!ds->ops->port_fdb_dump)
781 return ds->ops->port_fdb_dump(ds, port, cb, data);
784 int dsa_port_mdb_add(const struct dsa_port *dp,
785 const struct switchdev_obj_port_mdb *mdb)
787 struct dsa_notifier_mdb_info info = {
788 .sw_index = dp->ds->index,
793 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
796 int dsa_port_mdb_del(const struct dsa_port *dp,
797 const struct switchdev_obj_port_mdb *mdb)
799 struct dsa_notifier_mdb_info info = {
800 .sw_index = dp->ds->index,
805 return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
808 int dsa_port_host_mdb_add(const struct dsa_port *dp,
809 const struct switchdev_obj_port_mdb *mdb)
811 struct dsa_notifier_mdb_info info = {
812 .sw_index = dp->ds->index,
816 struct dsa_port *cpu_dp = dp->cpu_dp;
819 err = dev_mc_add(cpu_dp->master, mdb->addr);
823 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
826 int dsa_port_host_mdb_del(const struct dsa_port *dp,
827 const struct switchdev_obj_port_mdb *mdb)
829 struct dsa_notifier_mdb_info info = {
830 .sw_index = dp->ds->index,
834 struct dsa_port *cpu_dp = dp->cpu_dp;
837 err = dev_mc_del(cpu_dp->master, mdb->addr);
841 return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
844 int dsa_port_vlan_add(struct dsa_port *dp,
845 const struct switchdev_obj_port_vlan *vlan,
846 struct netlink_ext_ack *extack)
848 struct dsa_notifier_vlan_info info = {
849 .sw_index = dp->ds->index,
855 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
858 int dsa_port_vlan_del(struct dsa_port *dp,
859 const struct switchdev_obj_port_vlan *vlan)
861 struct dsa_notifier_vlan_info info = {
862 .sw_index = dp->ds->index,
867 return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
870 int dsa_port_mrp_add(const struct dsa_port *dp,
871 const struct switchdev_obj_mrp *mrp)
873 struct dsa_notifier_mrp_info info = {
874 .sw_index = dp->ds->index,
879 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info);
882 int dsa_port_mrp_del(const struct dsa_port *dp,
883 const struct switchdev_obj_mrp *mrp)
885 struct dsa_notifier_mrp_info info = {
886 .sw_index = dp->ds->index,
891 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info);
894 int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
895 const struct switchdev_obj_ring_role_mrp *mrp)
897 struct dsa_notifier_mrp_ring_role_info info = {
898 .sw_index = dp->ds->index,
903 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info);
906 int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
907 const struct switchdev_obj_ring_role_mrp *mrp)
909 struct dsa_notifier_mrp_ring_role_info info = {
910 .sw_index = dp->ds->index,
915 return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info);
918 void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
919 const struct dsa_device_ops *tag_ops)
921 cpu_dp->rcv = tag_ops->rcv;
922 cpu_dp->tag_ops = tag_ops;
925 static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
927 struct device_node *phy_dn;
928 struct phy_device *phydev;
930 phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
934 phydev = of_phy_find_device(phy_dn);
937 return ERR_PTR(-EPROBE_DEFER);
944 static void dsa_port_phylink_validate(struct phylink_config *config,
945 unsigned long *supported,
946 struct phylink_link_state *state)
948 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
949 struct dsa_switch *ds = dp->ds;
951 if (!ds->ops->phylink_validate)
954 ds->ops->phylink_validate(ds, dp->index, supported, state);
957 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
958 struct phylink_link_state *state)
960 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
961 struct dsa_switch *ds = dp->ds;
964 /* Only called for inband modes */
965 if (!ds->ops->phylink_mac_link_state) {
970 err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
972 dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
978 static void dsa_port_phylink_mac_config(struct phylink_config *config,
980 const struct phylink_link_state *state)
982 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
983 struct dsa_switch *ds = dp->ds;
985 if (!ds->ops->phylink_mac_config)
988 ds->ops->phylink_mac_config(ds, dp->index, mode, state);
991 static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
993 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
994 struct dsa_switch *ds = dp->ds;
996 if (!ds->ops->phylink_mac_an_restart)
999 ds->ops->phylink_mac_an_restart(ds, dp->index);
1002 static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
1004 phy_interface_t interface)
1006 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1007 struct phy_device *phydev = NULL;
1008 struct dsa_switch *ds = dp->ds;
1010 if (dsa_is_user_port(ds, dp->index))
1011 phydev = dp->slave->phydev;
1013 if (!ds->ops->phylink_mac_link_down) {
1014 if (ds->ops->adjust_link && phydev)
1015 ds->ops->adjust_link(ds, dp->index, phydev);
1019 ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
1022 static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
1023 struct phy_device *phydev,
1025 phy_interface_t interface,
1026 int speed, int duplex,
1027 bool tx_pause, bool rx_pause)
1029 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
1030 struct dsa_switch *ds = dp->ds;
1032 if (!ds->ops->phylink_mac_link_up) {
1033 if (ds->ops->adjust_link && phydev)
1034 ds->ops->adjust_link(ds, dp->index, phydev);
1038 ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
1039 speed, duplex, tx_pause, rx_pause);
1042 const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
1043 .validate = dsa_port_phylink_validate,
1044 .mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
1045 .mac_config = dsa_port_phylink_mac_config,
1046 .mac_an_restart = dsa_port_phylink_mac_an_restart,
1047 .mac_link_down = dsa_port_phylink_mac_link_down,
1048 .mac_link_up = dsa_port_phylink_mac_link_up,
1051 static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
1053 struct dsa_switch *ds = dp->ds;
1054 struct phy_device *phydev;
1055 int port = dp->index;
1058 phydev = dsa_port_get_phy_device(dp);
1063 return PTR_ERR(phydev);
1066 err = genphy_resume(phydev);
1070 err = genphy_read_status(phydev);
1074 err = genphy_suspend(phydev);
1079 if (ds->ops->adjust_link)
1080 ds->ops->adjust_link(ds, port, phydev);
1082 dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
1085 put_device(&phydev->mdio.dev);
1089 static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
1091 struct device_node *dn = dp->dn;
1092 struct dsa_switch *ds = dp->ds;
1093 struct phy_device *phydev;
1094 int port = dp->index;
1095 phy_interface_t mode;
1098 err = of_phy_register_fixed_link(dn);
1101 "failed to register the fixed PHY of port %d\n",
1106 phydev = of_phy_find_device(dn);
1108 err = of_get_phy_mode(dn, &mode);
1110 mode = PHY_INTERFACE_MODE_NA;
1111 phydev->interface = mode;
1113 genphy_read_status(phydev);
1115 if (ds->ops->adjust_link)
1116 ds->ops->adjust_link(ds, port, phydev);
1118 put_device(&phydev->mdio.dev);
1123 static int dsa_port_phylink_register(struct dsa_port *dp)
1125 struct dsa_switch *ds = dp->ds;
1126 struct device_node *port_dn = dp->dn;
1127 phy_interface_t mode;
1130 err = of_get_phy_mode(port_dn, &mode);
1132 mode = PHY_INTERFACE_MODE_NA;
1134 dp->pl_config.dev = ds->dev;
1135 dp->pl_config.type = PHYLINK_DEV;
1136 dp->pl_config.pcs_poll = ds->pcs_poll;
1138 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
1139 mode, &dsa_port_phylink_mac_ops);
1140 if (IS_ERR(dp->pl)) {
1141 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1142 return PTR_ERR(dp->pl);
1145 err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1146 if (err && err != -ENODEV) {
1147 pr_err("could not attach to PHY: %d\n", err);
1148 goto err_phy_connect;
1154 phylink_destroy(dp->pl);
1158 int dsa_port_link_register_of(struct dsa_port *dp)
1160 struct dsa_switch *ds = dp->ds;
1161 struct device_node *phy_np;
1162 int port = dp->index;
1164 if (!ds->ops->adjust_link) {
1165 phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1166 if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1167 if (ds->ops->phylink_mac_link_down)
1168 ds->ops->phylink_mac_link_down(ds, port,
1169 MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1170 return dsa_port_phylink_register(dp);
1176 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1178 if (of_phy_is_fixed_link(dp->dn))
1179 return dsa_port_fixed_link_register_of(dp);
1181 return dsa_port_setup_phy_of(dp, true);
1184 void dsa_port_link_unregister_of(struct dsa_port *dp)
1186 struct dsa_switch *ds = dp->ds;
1188 if (!ds->ops->adjust_link && dp->pl) {
1190 phylink_disconnect_phy(dp->pl);
1192 phylink_destroy(dp->pl);
1197 if (of_phy_is_fixed_link(dp->dn))
1198 of_phy_deregister_fixed_link(dp->dn);
1200 dsa_port_setup_phy_of(dp, false);
1203 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
1205 struct phy_device *phydev;
1206 int ret = -EOPNOTSUPP;
1208 if (of_phy_is_fixed_link(dp->dn))
1211 phydev = dsa_port_get_phy_device(dp);
1212 if (IS_ERR_OR_NULL(phydev))
1215 ret = phy_ethtool_get_strings(phydev, data);
1216 put_device(&phydev->mdio.dev);
1220 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
1222 int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
1224 struct phy_device *phydev;
1225 int ret = -EOPNOTSUPP;
1227 if (of_phy_is_fixed_link(dp->dn))
1230 phydev = dsa_port_get_phy_device(dp);
1231 if (IS_ERR_OR_NULL(phydev))
1234 ret = phy_ethtool_get_stats(phydev, NULL, data);
1235 put_device(&phydev->mdio.dev);
1239 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
1241 int dsa_port_get_phy_sset_count(struct dsa_port *dp)
1243 struct phy_device *phydev;
1244 int ret = -EOPNOTSUPP;
1246 if (of_phy_is_fixed_link(dp->dn))
1249 phydev = dsa_port_get_phy_device(dp);
1250 if (IS_ERR_OR_NULL(phydev))
1253 ret = phy_ethtool_get_sset_count(phydev);
1254 put_device(&phydev->mdio.dev);
1258 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
1260 int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1262 struct dsa_notifier_hsr_info info = {
1263 .sw_index = dp->ds->index,
1271 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
1278 void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1280 struct dsa_notifier_hsr_info info = {
1281 .sw_index = dp->ds->index,
1289 err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
1291 pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n");
1294 int dsa_port_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid)
1296 struct dsa_notifier_tag_8021q_vlan_info info = {
1297 .tree_index = dp->ds->dst->index,
1298 .sw_index = dp->ds->index,
1303 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD, &info);
1306 void dsa_port_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid)
1308 struct dsa_notifier_tag_8021q_vlan_info info = {
1309 .tree_index = dp->ds->dst->index,
1310 .sw_index = dp->ds->index,
1316 err = dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL, &info);
1318 pr_err("DSA: failed to notify tag_8021q VLAN deletion: %pe\n",