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>
16 static int dsa_broadcast(unsigned long e
, void *v
)
18 struct dsa_switch_tree
*dst
;
21 list_for_each_entry(dst
, &dsa_tree_list
, list
) {
22 struct raw_notifier_head
*nh
= &dst
->nh
;
24 err
= raw_notifier_call_chain(nh
, e
, v
);
25 err
= notifier_to_errno(err
);
33 static int dsa_port_notify(const struct dsa_port
*dp
, unsigned long e
, void *v
)
35 struct raw_notifier_head
*nh
= &dp
->ds
->dst
->nh
;
38 err
= raw_notifier_call_chain(nh
, e
, v
);
40 return notifier_to_errno(err
);
43 int dsa_port_set_state(struct dsa_port
*dp
, u8 state
,
44 struct switchdev_trans
*trans
)
46 struct dsa_switch
*ds
= dp
->ds
;
49 if (switchdev_trans_ph_prepare(trans
))
50 return ds
->ops
->port_stp_state_set
? 0 : -EOPNOTSUPP
;
52 if (ds
->ops
->port_stp_state_set
)
53 ds
->ops
->port_stp_state_set(ds
, port
, state
);
55 if (ds
->ops
->port_fast_age
) {
56 /* Fast age FDB entries or flush appropriate forwarding database
57 * for the given port, if we are moving it from Learning or
58 * Forwarding state, to Disabled or Blocking or Listening state.
61 if ((dp
->stp_state
== BR_STATE_LEARNING
||
62 dp
->stp_state
== BR_STATE_FORWARDING
) &&
63 (state
== BR_STATE_DISABLED
||
64 state
== BR_STATE_BLOCKING
||
65 state
== BR_STATE_LISTENING
))
66 ds
->ops
->port_fast_age(ds
, port
);
69 dp
->stp_state
= state
;
74 static void dsa_port_set_state_now(struct dsa_port
*dp
, u8 state
)
78 err
= dsa_port_set_state(dp
, state
, NULL
);
80 pr_err("DSA: failed to set STP state %u (%d)\n", state
, err
);
83 int dsa_port_enable_rt(struct dsa_port
*dp
, struct phy_device
*phy
)
85 struct dsa_switch
*ds
= dp
->ds
;
89 if (ds
->ops
->port_enable
) {
90 err
= ds
->ops
->port_enable(ds
, port
, phy
);
96 dsa_port_set_state_now(dp
, BR_STATE_FORWARDING
);
99 phylink_start(dp
->pl
);
104 int dsa_port_enable(struct dsa_port
*dp
, struct phy_device
*phy
)
109 err
= dsa_port_enable_rt(dp
, phy
);
115 void dsa_port_disable_rt(struct dsa_port
*dp
)
117 struct dsa_switch
*ds
= dp
->ds
;
118 int port
= dp
->index
;
121 phylink_stop(dp
->pl
);
124 dsa_port_set_state_now(dp
, BR_STATE_DISABLED
);
126 if (ds
->ops
->port_disable
)
127 ds
->ops
->port_disable(ds
, port
);
130 void dsa_port_disable(struct dsa_port
*dp
)
133 dsa_port_disable_rt(dp
);
137 int dsa_port_bridge_join(struct dsa_port
*dp
, struct net_device
*br
)
139 struct dsa_notifier_bridge_info info
= {
140 .tree_index
= dp
->ds
->dst
->index
,
141 .sw_index
= dp
->ds
->index
,
147 /* Set the flooding mode before joining the port in the switch */
148 err
= dsa_port_bridge_flags(dp
, BR_FLOOD
| BR_MCAST_FLOOD
, NULL
);
152 /* Here the interface is already bridged. Reflect the current
153 * configuration so that drivers can program their chips accordingly.
157 err
= dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN
, &info
);
159 /* The bridging is rolled back on error */
161 dsa_port_bridge_flags(dp
, 0, NULL
);
162 dp
->bridge_dev
= NULL
;
168 void dsa_port_bridge_leave(struct dsa_port
*dp
, struct net_device
*br
)
170 struct dsa_notifier_bridge_info info
= {
171 .tree_index
= dp
->ds
->dst
->index
,
172 .sw_index
= dp
->ds
->index
,
178 /* Here the port is already unbridged. Reflect the current configuration
179 * so that drivers can program their chips accordingly.
181 dp
->bridge_dev
= NULL
;
183 err
= dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE
, &info
);
185 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
187 /* Port is leaving the bridge, disable flooding */
188 dsa_port_bridge_flags(dp
, 0, NULL
);
190 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
191 * so allow it to be in BR_STATE_FORWARDING to be kept functional
193 dsa_port_set_state_now(dp
, BR_STATE_FORWARDING
);
196 /* Must be called under rcu_read_lock() */
197 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port
*dp
,
200 struct dsa_switch
*ds
= dp
->ds
;
203 /* VLAN awareness was off, so the question is "can we turn it on".
204 * We may have had 8021q uppers, those need to go. Make sure we don't
205 * enter an inconsistent state: deny changing the VLAN awareness state
206 * as long as we have 8021q uppers.
208 if (vlan_filtering
&& dsa_is_user_port(ds
, dp
->index
)) {
209 struct net_device
*upper_dev
, *slave
= dp
->slave
;
210 struct net_device
*br
= dp
->bridge_dev
;
211 struct list_head
*iter
;
213 netdev_for_each_upper_dev_rcu(slave
, upper_dev
, iter
) {
214 struct bridge_vlan_info br_info
;
217 if (!is_vlan_dev(upper_dev
))
220 vid
= vlan_dev_vlan_id(upper_dev
);
222 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
223 * device, respectively the VID is not found, returning
224 * 0 means success, which is a failure for us here.
226 err
= br_vlan_get_info(br
, vid
, &br_info
);
228 dev_err(ds
->dev
, "Must remove upper %s first\n",
235 if (!ds
->vlan_filtering_is_global
)
238 /* For cases where enabling/disabling VLAN awareness is global to the
239 * switch, we need to handle the case where multiple bridges span
240 * different ports of the same switch device and one of them has a
241 * different setting than what is being requested.
243 for (i
= 0; i
< ds
->num_ports
; i
++) {
244 struct net_device
*other_bridge
;
246 other_bridge
= dsa_to_port(ds
, i
)->bridge_dev
;
249 /* If it's the same bridge, it also has same
250 * vlan_filtering setting => no need to check
252 if (other_bridge
== dp
->bridge_dev
)
254 if (br_vlan_enabled(other_bridge
) != vlan_filtering
) {
255 dev_err(ds
->dev
, "VLAN filtering is a global setting\n");
262 int dsa_port_vlan_filtering(struct dsa_port
*dp
, bool vlan_filtering
,
263 struct switchdev_trans
*trans
)
265 struct dsa_switch
*ds
= dp
->ds
;
268 if (switchdev_trans_ph_prepare(trans
)) {
271 if (!ds
->ops
->port_vlan_filtering
)
274 /* We are called from dsa_slave_switchdev_blocking_event(),
275 * which is not under rcu_read_lock(), unlike
276 * dsa_slave_switchdev_event().
279 apply
= dsa_port_can_apply_vlan_filtering(dp
, vlan_filtering
);
285 if (dsa_port_is_vlan_filtering(dp
) == vlan_filtering
)
288 err
= ds
->ops
->port_vlan_filtering(ds
, dp
->index
, vlan_filtering
,
293 if (switchdev_trans_ph_commit(trans
)) {
294 if (ds
->vlan_filtering_is_global
)
295 ds
->vlan_filtering
= vlan_filtering
;
297 dp
->vlan_filtering
= vlan_filtering
;
303 /* This enforces legacy behavior for switch drivers which assume they can't
304 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
306 bool dsa_port_skip_vlan_configuration(struct dsa_port
*dp
)
308 struct dsa_switch
*ds
= dp
->ds
;
313 return (!ds
->configure_vlan_while_not_filtering
&&
314 !br_vlan_enabled(dp
->bridge_dev
));
317 int dsa_port_ageing_time(struct dsa_port
*dp
, clock_t ageing_clock
,
318 struct switchdev_trans
*trans
)
320 unsigned long ageing_jiffies
= clock_t_to_jiffies(ageing_clock
);
321 unsigned int ageing_time
= jiffies_to_msecs(ageing_jiffies
);
322 struct dsa_notifier_ageing_time_info info
= {
323 .ageing_time
= ageing_time
,
327 if (switchdev_trans_ph_prepare(trans
))
328 return dsa_port_notify(dp
, DSA_NOTIFIER_AGEING_TIME
, &info
);
330 dp
->ageing_time
= ageing_time
;
332 return dsa_port_notify(dp
, DSA_NOTIFIER_AGEING_TIME
, &info
);
335 int dsa_port_pre_bridge_flags(const struct dsa_port
*dp
, unsigned long flags
,
336 struct switchdev_trans
*trans
)
338 struct dsa_switch
*ds
= dp
->ds
;
340 if (!ds
->ops
->port_egress_floods
||
341 (flags
& ~(BR_FLOOD
| BR_MCAST_FLOOD
)))
347 int dsa_port_bridge_flags(const struct dsa_port
*dp
, unsigned long flags
,
348 struct switchdev_trans
*trans
)
350 struct dsa_switch
*ds
= dp
->ds
;
351 int port
= dp
->index
;
354 if (switchdev_trans_ph_prepare(trans
))
357 if (ds
->ops
->port_egress_floods
)
358 err
= ds
->ops
->port_egress_floods(ds
, port
, flags
& BR_FLOOD
,
359 flags
& BR_MCAST_FLOOD
);
364 int dsa_port_mrouter(struct dsa_port
*dp
, bool mrouter
,
365 struct switchdev_trans
*trans
)
367 struct dsa_switch
*ds
= dp
->ds
;
368 int port
= dp
->index
;
370 if (switchdev_trans_ph_prepare(trans
))
371 return ds
->ops
->port_egress_floods
? 0 : -EOPNOTSUPP
;
373 return ds
->ops
->port_egress_floods(ds
, port
, true, mrouter
);
376 int dsa_port_mtu_change(struct dsa_port
*dp
, int new_mtu
,
377 bool propagate_upstream
)
379 struct dsa_notifier_mtu_info info
= {
380 .sw_index
= dp
->ds
->index
,
381 .propagate_upstream
= propagate_upstream
,
386 return dsa_port_notify(dp
, DSA_NOTIFIER_MTU
, &info
);
389 int dsa_port_fdb_add(struct dsa_port
*dp
, const unsigned char *addr
,
392 struct dsa_notifier_fdb_info info
= {
393 .sw_index
= dp
->ds
->index
,
399 return dsa_port_notify(dp
, DSA_NOTIFIER_FDB_ADD
, &info
);
402 int dsa_port_fdb_del(struct dsa_port
*dp
, const unsigned char *addr
,
405 struct dsa_notifier_fdb_info info
= {
406 .sw_index
= dp
->ds
->index
,
413 return dsa_port_notify(dp
, DSA_NOTIFIER_FDB_DEL
, &info
);
416 int dsa_port_fdb_dump(struct dsa_port
*dp
, dsa_fdb_dump_cb_t
*cb
, void *data
)
418 struct dsa_switch
*ds
= dp
->ds
;
419 int port
= dp
->index
;
421 if (!ds
->ops
->port_fdb_dump
)
424 return ds
->ops
->port_fdb_dump(ds
, port
, cb
, data
);
427 int dsa_port_mdb_add(const struct dsa_port
*dp
,
428 const struct switchdev_obj_port_mdb
*mdb
,
429 struct switchdev_trans
*trans
)
431 struct dsa_notifier_mdb_info info
= {
432 .sw_index
= dp
->ds
->index
,
438 return dsa_port_notify(dp
, DSA_NOTIFIER_MDB_ADD
, &info
);
441 int dsa_port_mdb_del(const struct dsa_port
*dp
,
442 const struct switchdev_obj_port_mdb
*mdb
)
444 struct dsa_notifier_mdb_info info
= {
445 .sw_index
= dp
->ds
->index
,
450 return dsa_port_notify(dp
, DSA_NOTIFIER_MDB_DEL
, &info
);
453 int dsa_port_vlan_add(struct dsa_port
*dp
,
454 const struct switchdev_obj_port_vlan
*vlan
,
455 struct switchdev_trans
*trans
)
457 struct dsa_notifier_vlan_info info
= {
458 .sw_index
= dp
->ds
->index
,
464 return dsa_port_notify(dp
, DSA_NOTIFIER_VLAN_ADD
, &info
);
467 int dsa_port_vlan_del(struct dsa_port
*dp
,
468 const struct switchdev_obj_port_vlan
*vlan
)
470 struct dsa_notifier_vlan_info info
= {
471 .sw_index
= dp
->ds
->index
,
476 return dsa_port_notify(dp
, DSA_NOTIFIER_VLAN_DEL
, &info
);
479 static struct phy_device
*dsa_port_get_phy_device(struct dsa_port
*dp
)
481 struct device_node
*phy_dn
;
482 struct phy_device
*phydev
;
484 phy_dn
= of_parse_phandle(dp
->dn
, "phy-handle", 0);
488 phydev
= of_phy_find_device(phy_dn
);
491 return ERR_PTR(-EPROBE_DEFER
);
498 static void dsa_port_phylink_validate(struct phylink_config
*config
,
499 unsigned long *supported
,
500 struct phylink_link_state
*state
)
502 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
503 struct dsa_switch
*ds
= dp
->ds
;
505 if (!ds
->ops
->phylink_validate
)
508 ds
->ops
->phylink_validate(ds
, dp
->index
, supported
, state
);
511 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config
*config
,
512 struct phylink_link_state
*state
)
514 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
515 struct dsa_switch
*ds
= dp
->ds
;
518 /* Only called for inband modes */
519 if (!ds
->ops
->phylink_mac_link_state
) {
524 err
= ds
->ops
->phylink_mac_link_state(ds
, dp
->index
, state
);
526 dev_err(ds
->dev
, "p%d: phylink_mac_link_state() failed: %d\n",
532 static void dsa_port_phylink_mac_config(struct phylink_config
*config
,
534 const struct phylink_link_state
*state
)
536 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
537 struct dsa_switch
*ds
= dp
->ds
;
539 if (!ds
->ops
->phylink_mac_config
)
542 ds
->ops
->phylink_mac_config(ds
, dp
->index
, mode
, state
);
545 static void dsa_port_phylink_mac_an_restart(struct phylink_config
*config
)
547 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
548 struct dsa_switch
*ds
= dp
->ds
;
550 if (!ds
->ops
->phylink_mac_an_restart
)
553 ds
->ops
->phylink_mac_an_restart(ds
, dp
->index
);
556 static void dsa_port_phylink_mac_link_down(struct phylink_config
*config
,
558 phy_interface_t interface
)
560 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
561 struct phy_device
*phydev
= NULL
;
562 struct dsa_switch
*ds
= dp
->ds
;
564 if (dsa_is_user_port(ds
, dp
->index
))
565 phydev
= dp
->slave
->phydev
;
567 if (!ds
->ops
->phylink_mac_link_down
) {
568 if (ds
->ops
->adjust_link
&& phydev
)
569 ds
->ops
->adjust_link(ds
, dp
->index
, phydev
);
573 ds
->ops
->phylink_mac_link_down(ds
, dp
->index
, mode
, interface
);
576 static void dsa_port_phylink_mac_link_up(struct phylink_config
*config
,
577 struct phy_device
*phydev
,
579 phy_interface_t interface
,
580 int speed
, int duplex
,
581 bool tx_pause
, bool rx_pause
)
583 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
584 struct dsa_switch
*ds
= dp
->ds
;
586 if (!ds
->ops
->phylink_mac_link_up
) {
587 if (ds
->ops
->adjust_link
&& phydev
)
588 ds
->ops
->adjust_link(ds
, dp
->index
, phydev
);
592 ds
->ops
->phylink_mac_link_up(ds
, dp
->index
, mode
, interface
, phydev
,
593 speed
, duplex
, tx_pause
, rx_pause
);
596 const struct phylink_mac_ops dsa_port_phylink_mac_ops
= {
597 .validate
= dsa_port_phylink_validate
,
598 .mac_pcs_get_state
= dsa_port_phylink_mac_pcs_get_state
,
599 .mac_config
= dsa_port_phylink_mac_config
,
600 .mac_an_restart
= dsa_port_phylink_mac_an_restart
,
601 .mac_link_down
= dsa_port_phylink_mac_link_down
,
602 .mac_link_up
= dsa_port_phylink_mac_link_up
,
605 static int dsa_port_setup_phy_of(struct dsa_port
*dp
, bool enable
)
607 struct dsa_switch
*ds
= dp
->ds
;
608 struct phy_device
*phydev
;
609 int port
= dp
->index
;
612 phydev
= dsa_port_get_phy_device(dp
);
617 return PTR_ERR(phydev
);
620 err
= genphy_resume(phydev
);
624 err
= genphy_read_status(phydev
);
628 err
= genphy_suspend(phydev
);
633 if (ds
->ops
->adjust_link
)
634 ds
->ops
->adjust_link(ds
, port
, phydev
);
636 dev_dbg(ds
->dev
, "enabled port's phy: %s", phydev_name(phydev
));
639 put_device(&phydev
->mdio
.dev
);
643 static int dsa_port_fixed_link_register_of(struct dsa_port
*dp
)
645 struct device_node
*dn
= dp
->dn
;
646 struct dsa_switch
*ds
= dp
->ds
;
647 struct phy_device
*phydev
;
648 int port
= dp
->index
;
649 phy_interface_t mode
;
652 err
= of_phy_register_fixed_link(dn
);
655 "failed to register the fixed PHY of port %d\n",
660 phydev
= of_phy_find_device(dn
);
662 err
= of_get_phy_mode(dn
, &mode
);
664 mode
= PHY_INTERFACE_MODE_NA
;
665 phydev
->interface
= mode
;
667 genphy_read_status(phydev
);
669 if (ds
->ops
->adjust_link
)
670 ds
->ops
->adjust_link(ds
, port
, phydev
);
672 put_device(&phydev
->mdio
.dev
);
677 static int dsa_port_phylink_register(struct dsa_port
*dp
)
679 struct dsa_switch
*ds
= dp
->ds
;
680 struct device_node
*port_dn
= dp
->dn
;
681 phy_interface_t mode
;
684 err
= of_get_phy_mode(port_dn
, &mode
);
686 mode
= PHY_INTERFACE_MODE_NA
;
688 dp
->pl_config
.dev
= ds
->dev
;
689 dp
->pl_config
.type
= PHYLINK_DEV
;
690 dp
->pl_config
.pcs_poll
= ds
->pcs_poll
;
692 dp
->pl
= phylink_create(&dp
->pl_config
, of_fwnode_handle(port_dn
),
693 mode
, &dsa_port_phylink_mac_ops
);
694 if (IS_ERR(dp
->pl
)) {
695 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp
->pl
));
696 return PTR_ERR(dp
->pl
);
699 err
= phylink_of_phy_connect(dp
->pl
, port_dn
, 0);
700 if (err
&& err
!= -ENODEV
) {
701 pr_err("could not attach to PHY: %d\n", err
);
702 goto err_phy_connect
;
708 phylink_destroy(dp
->pl
);
712 int dsa_port_link_register_of(struct dsa_port
*dp
)
714 struct dsa_switch
*ds
= dp
->ds
;
715 struct device_node
*phy_np
;
716 int port
= dp
->index
;
718 if (!ds
->ops
->adjust_link
) {
719 phy_np
= of_parse_phandle(dp
->dn
, "phy-handle", 0);
720 if (of_phy_is_fixed_link(dp
->dn
) || phy_np
) {
721 if (ds
->ops
->phylink_mac_link_down
)
722 ds
->ops
->phylink_mac_link_down(ds
, port
,
723 MLO_AN_FIXED
, PHY_INTERFACE_MODE_NA
);
724 return dsa_port_phylink_register(dp
);
730 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
732 if (of_phy_is_fixed_link(dp
->dn
))
733 return dsa_port_fixed_link_register_of(dp
);
735 return dsa_port_setup_phy_of(dp
, true);
738 void dsa_port_link_unregister_of(struct dsa_port
*dp
)
740 struct dsa_switch
*ds
= dp
->ds
;
742 if (!ds
->ops
->adjust_link
&& dp
->pl
) {
744 phylink_disconnect_phy(dp
->pl
);
746 phylink_destroy(dp
->pl
);
751 if (of_phy_is_fixed_link(dp
->dn
))
752 of_phy_deregister_fixed_link(dp
->dn
);
754 dsa_port_setup_phy_of(dp
, false);
757 int dsa_port_get_phy_strings(struct dsa_port
*dp
, uint8_t *data
)
759 struct phy_device
*phydev
;
760 int ret
= -EOPNOTSUPP
;
762 if (of_phy_is_fixed_link(dp
->dn
))
765 phydev
= dsa_port_get_phy_device(dp
);
766 if (IS_ERR_OR_NULL(phydev
))
769 ret
= phy_ethtool_get_strings(phydev
, data
);
770 put_device(&phydev
->mdio
.dev
);
774 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings
);
776 int dsa_port_get_ethtool_phy_stats(struct dsa_port
*dp
, uint64_t *data
)
778 struct phy_device
*phydev
;
779 int ret
= -EOPNOTSUPP
;
781 if (of_phy_is_fixed_link(dp
->dn
))
784 phydev
= dsa_port_get_phy_device(dp
);
785 if (IS_ERR_OR_NULL(phydev
))
788 ret
= phy_ethtool_get_stats(phydev
, NULL
, data
);
789 put_device(&phydev
->mdio
.dev
);
793 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats
);
795 int dsa_port_get_phy_sset_count(struct dsa_port
*dp
)
797 struct phy_device
*phydev
;
798 int ret
= -EOPNOTSUPP
;
800 if (of_phy_is_fixed_link(dp
->dn
))
803 phydev
= dsa_port_get_phy_device(dp
);
804 if (IS_ERR_OR_NULL(phydev
))
807 ret
= phy_ethtool_get_sset_count(phydev
);
808 put_device(&phydev
->mdio
.dev
);
812 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count
);