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_port_notify(const struct dsa_port
*dp
, unsigned long e
, void *v
)
18 struct raw_notifier_head
*nh
= &dp
->ds
->dst
->nh
;
21 err
= raw_notifier_call_chain(nh
, e
, v
);
23 return notifier_to_errno(err
);
26 int dsa_port_set_state(struct dsa_port
*dp
, u8 state
,
27 struct switchdev_trans
*trans
)
29 struct dsa_switch
*ds
= dp
->ds
;
32 if (switchdev_trans_ph_prepare(trans
))
33 return ds
->ops
->port_stp_state_set
? 0 : -EOPNOTSUPP
;
35 if (ds
->ops
->port_stp_state_set
)
36 ds
->ops
->port_stp_state_set(ds
, port
, state
);
38 if (ds
->ops
->port_fast_age
) {
39 /* Fast age FDB entries or flush appropriate forwarding database
40 * for the given port, if we are moving it from Learning or
41 * Forwarding state, to Disabled or Blocking or Listening state.
44 if ((dp
->stp_state
== BR_STATE_LEARNING
||
45 dp
->stp_state
== BR_STATE_FORWARDING
) &&
46 (state
== BR_STATE_DISABLED
||
47 state
== BR_STATE_BLOCKING
||
48 state
== BR_STATE_LISTENING
))
49 ds
->ops
->port_fast_age(ds
, port
);
52 dp
->stp_state
= state
;
57 static void dsa_port_set_state_now(struct dsa_port
*dp
, u8 state
)
61 err
= dsa_port_set_state(dp
, state
, NULL
);
63 pr_err("DSA: failed to set STP state %u (%d)\n", state
, err
);
66 int dsa_port_enable_rt(struct dsa_port
*dp
, struct phy_device
*phy
)
68 struct dsa_switch
*ds
= dp
->ds
;
72 if (ds
->ops
->port_enable
) {
73 err
= ds
->ops
->port_enable(ds
, port
, phy
);
79 dsa_port_set_state_now(dp
, BR_STATE_FORWARDING
);
82 phylink_start(dp
->pl
);
87 int dsa_port_enable(struct dsa_port
*dp
, struct phy_device
*phy
)
92 err
= dsa_port_enable_rt(dp
, phy
);
98 void dsa_port_disable_rt(struct dsa_port
*dp
)
100 struct dsa_switch
*ds
= dp
->ds
;
101 int port
= dp
->index
;
104 phylink_stop(dp
->pl
);
107 dsa_port_set_state_now(dp
, BR_STATE_DISABLED
);
109 if (ds
->ops
->port_disable
)
110 ds
->ops
->port_disable(ds
, port
);
113 void dsa_port_disable(struct dsa_port
*dp
)
116 dsa_port_disable_rt(dp
);
120 int dsa_port_bridge_join(struct dsa_port
*dp
, struct net_device
*br
)
122 struct dsa_notifier_bridge_info info
= {
123 .sw_index
= dp
->ds
->index
,
129 /* Set the flooding mode before joining the port in the switch */
130 err
= dsa_port_bridge_flags(dp
, BR_FLOOD
| BR_MCAST_FLOOD
, NULL
);
134 /* Here the interface is already bridged. Reflect the current
135 * configuration so that drivers can program their chips accordingly.
139 err
= dsa_port_notify(dp
, DSA_NOTIFIER_BRIDGE_JOIN
, &info
);
141 /* The bridging is rolled back on error */
143 dsa_port_bridge_flags(dp
, 0, NULL
);
144 dp
->bridge_dev
= NULL
;
150 void dsa_port_bridge_leave(struct dsa_port
*dp
, struct net_device
*br
)
152 struct dsa_notifier_bridge_info info
= {
153 .sw_index
= dp
->ds
->index
,
159 /* Here the port is already unbridged. Reflect the current configuration
160 * so that drivers can program their chips accordingly.
162 dp
->bridge_dev
= NULL
;
164 err
= dsa_port_notify(dp
, DSA_NOTIFIER_BRIDGE_LEAVE
, &info
);
166 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
168 /* Port is leaving the bridge, disable flooding */
169 dsa_port_bridge_flags(dp
, 0, NULL
);
171 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
172 * so allow it to be in BR_STATE_FORWARDING to be kept functional
174 dsa_port_set_state_now(dp
, BR_STATE_FORWARDING
);
177 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port
*dp
,
180 struct dsa_switch
*ds
= dp
->ds
;
183 if (!ds
->vlan_filtering_is_global
)
186 /* For cases where enabling/disabling VLAN awareness is global to the
187 * switch, we need to handle the case where multiple bridges span
188 * different ports of the same switch device and one of them has a
189 * different setting than what is being requested.
191 for (i
= 0; i
< ds
->num_ports
; i
++) {
192 struct net_device
*other_bridge
;
194 other_bridge
= dsa_to_port(ds
, i
)->bridge_dev
;
197 /* If it's the same bridge, it also has same
198 * vlan_filtering setting => no need to check
200 if (other_bridge
== dp
->bridge_dev
)
202 if (br_vlan_enabled(other_bridge
) != vlan_filtering
) {
203 dev_err(ds
->dev
, "VLAN filtering is a global setting\n");
210 int dsa_port_vlan_filtering(struct dsa_port
*dp
, bool vlan_filtering
,
211 struct switchdev_trans
*trans
)
213 struct dsa_switch
*ds
= dp
->ds
;
216 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
217 if (switchdev_trans_ph_prepare(trans
))
220 if (!ds
->ops
->port_vlan_filtering
)
223 if (!dsa_port_can_apply_vlan_filtering(dp
, vlan_filtering
))
226 if (dsa_port_is_vlan_filtering(dp
) == vlan_filtering
)
229 err
= ds
->ops
->port_vlan_filtering(ds
, dp
->index
,
234 if (ds
->vlan_filtering_is_global
)
235 ds
->vlan_filtering
= vlan_filtering
;
237 dp
->vlan_filtering
= vlan_filtering
;
241 int dsa_port_ageing_time(struct dsa_port
*dp
, clock_t ageing_clock
,
242 struct switchdev_trans
*trans
)
244 unsigned long ageing_jiffies
= clock_t_to_jiffies(ageing_clock
);
245 unsigned int ageing_time
= jiffies_to_msecs(ageing_jiffies
);
246 struct dsa_notifier_ageing_time_info info
= {
247 .ageing_time
= ageing_time
,
251 if (switchdev_trans_ph_prepare(trans
))
252 return dsa_port_notify(dp
, DSA_NOTIFIER_AGEING_TIME
, &info
);
254 dp
->ageing_time
= ageing_time
;
256 return dsa_port_notify(dp
, DSA_NOTIFIER_AGEING_TIME
, &info
);
259 int dsa_port_pre_bridge_flags(const struct dsa_port
*dp
, unsigned long flags
,
260 struct switchdev_trans
*trans
)
262 struct dsa_switch
*ds
= dp
->ds
;
264 if (!ds
->ops
->port_egress_floods
||
265 (flags
& ~(BR_FLOOD
| BR_MCAST_FLOOD
)))
271 int dsa_port_bridge_flags(const struct dsa_port
*dp
, unsigned long flags
,
272 struct switchdev_trans
*trans
)
274 struct dsa_switch
*ds
= dp
->ds
;
275 int port
= dp
->index
;
278 if (switchdev_trans_ph_prepare(trans
))
281 if (ds
->ops
->port_egress_floods
)
282 err
= ds
->ops
->port_egress_floods(ds
, port
, flags
& BR_FLOOD
,
283 flags
& BR_MCAST_FLOOD
);
288 int dsa_port_mrouter(struct dsa_port
*dp
, bool mrouter
,
289 struct switchdev_trans
*trans
)
291 struct dsa_switch
*ds
= dp
->ds
;
292 int port
= dp
->index
;
294 if (switchdev_trans_ph_prepare(trans
))
295 return ds
->ops
->port_egress_floods
? 0 : -EOPNOTSUPP
;
297 return ds
->ops
->port_egress_floods(ds
, port
, true, mrouter
);
300 int dsa_port_mtu_change(struct dsa_port
*dp
, int new_mtu
,
301 bool propagate_upstream
)
303 struct dsa_notifier_mtu_info info
= {
304 .sw_index
= dp
->ds
->index
,
305 .propagate_upstream
= propagate_upstream
,
310 return dsa_port_notify(dp
, DSA_NOTIFIER_MTU
, &info
);
313 int dsa_port_fdb_add(struct dsa_port
*dp
, const unsigned char *addr
,
316 struct dsa_notifier_fdb_info info
= {
317 .sw_index
= dp
->ds
->index
,
323 return dsa_port_notify(dp
, DSA_NOTIFIER_FDB_ADD
, &info
);
326 int dsa_port_fdb_del(struct dsa_port
*dp
, const unsigned char *addr
,
329 struct dsa_notifier_fdb_info info
= {
330 .sw_index
= dp
->ds
->index
,
337 return dsa_port_notify(dp
, DSA_NOTIFIER_FDB_DEL
, &info
);
340 int dsa_port_fdb_dump(struct dsa_port
*dp
, dsa_fdb_dump_cb_t
*cb
, void *data
)
342 struct dsa_switch
*ds
= dp
->ds
;
343 int port
= dp
->index
;
345 if (!ds
->ops
->port_fdb_dump
)
348 return ds
->ops
->port_fdb_dump(ds
, port
, cb
, data
);
351 int dsa_port_mdb_add(const struct dsa_port
*dp
,
352 const struct switchdev_obj_port_mdb
*mdb
,
353 struct switchdev_trans
*trans
)
355 struct dsa_notifier_mdb_info info
= {
356 .sw_index
= dp
->ds
->index
,
362 return dsa_port_notify(dp
, DSA_NOTIFIER_MDB_ADD
, &info
);
365 int dsa_port_mdb_del(const struct dsa_port
*dp
,
366 const struct switchdev_obj_port_mdb
*mdb
)
368 struct dsa_notifier_mdb_info info
= {
369 .sw_index
= dp
->ds
->index
,
374 return dsa_port_notify(dp
, DSA_NOTIFIER_MDB_DEL
, &info
);
377 int dsa_port_vlan_add(struct dsa_port
*dp
,
378 const struct switchdev_obj_port_vlan
*vlan
,
379 struct switchdev_trans
*trans
)
381 struct dsa_notifier_vlan_info info
= {
382 .sw_index
= dp
->ds
->index
,
388 return dsa_port_notify(dp
, DSA_NOTIFIER_VLAN_ADD
, &info
);
391 int dsa_port_vlan_del(struct dsa_port
*dp
,
392 const struct switchdev_obj_port_vlan
*vlan
)
394 struct dsa_notifier_vlan_info info
= {
395 .sw_index
= dp
->ds
->index
,
400 return dsa_port_notify(dp
, DSA_NOTIFIER_VLAN_DEL
, &info
);
403 int dsa_port_vid_add(struct dsa_port
*dp
, u16 vid
, u16 flags
)
405 struct switchdev_obj_port_vlan vlan
= {
406 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
411 struct switchdev_trans trans
;
414 trans
.ph_prepare
= true;
415 err
= dsa_port_vlan_add(dp
, &vlan
, &trans
);
419 trans
.ph_prepare
= false;
420 return dsa_port_vlan_add(dp
, &vlan
, &trans
);
422 EXPORT_SYMBOL(dsa_port_vid_add
);
424 int dsa_port_vid_del(struct dsa_port
*dp
, u16 vid
)
426 struct switchdev_obj_port_vlan vlan
= {
427 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
432 return dsa_port_vlan_del(dp
, &vlan
);
434 EXPORT_SYMBOL(dsa_port_vid_del
);
436 static struct phy_device
*dsa_port_get_phy_device(struct dsa_port
*dp
)
438 struct device_node
*phy_dn
;
439 struct phy_device
*phydev
;
441 phy_dn
= of_parse_phandle(dp
->dn
, "phy-handle", 0);
445 phydev
= of_phy_find_device(phy_dn
);
448 return ERR_PTR(-EPROBE_DEFER
);
455 static void dsa_port_phylink_validate(struct phylink_config
*config
,
456 unsigned long *supported
,
457 struct phylink_link_state
*state
)
459 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
460 struct dsa_switch
*ds
= dp
->ds
;
462 if (!ds
->ops
->phylink_validate
)
465 ds
->ops
->phylink_validate(ds
, dp
->index
, supported
, state
);
468 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config
*config
,
469 struct phylink_link_state
*state
)
471 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
472 struct dsa_switch
*ds
= dp
->ds
;
475 /* Only called for inband modes */
476 if (!ds
->ops
->phylink_mac_link_state
) {
481 err
= ds
->ops
->phylink_mac_link_state(ds
, dp
->index
, state
);
483 dev_err(ds
->dev
, "p%d: phylink_mac_link_state() failed: %d\n",
489 static void dsa_port_phylink_mac_config(struct phylink_config
*config
,
491 const struct phylink_link_state
*state
)
493 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
494 struct dsa_switch
*ds
= dp
->ds
;
496 if (!ds
->ops
->phylink_mac_config
)
499 ds
->ops
->phylink_mac_config(ds
, dp
->index
, mode
, state
);
502 static void dsa_port_phylink_mac_an_restart(struct phylink_config
*config
)
504 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
505 struct dsa_switch
*ds
= dp
->ds
;
507 if (!ds
->ops
->phylink_mac_an_restart
)
510 ds
->ops
->phylink_mac_an_restart(ds
, dp
->index
);
513 static void dsa_port_phylink_mac_link_down(struct phylink_config
*config
,
515 phy_interface_t interface
)
517 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
518 struct phy_device
*phydev
= NULL
;
519 struct dsa_switch
*ds
= dp
->ds
;
521 if (dsa_is_user_port(ds
, dp
->index
))
522 phydev
= dp
->slave
->phydev
;
524 if (!ds
->ops
->phylink_mac_link_down
) {
525 if (ds
->ops
->adjust_link
&& phydev
)
526 ds
->ops
->adjust_link(ds
, dp
->index
, phydev
);
530 ds
->ops
->phylink_mac_link_down(ds
, dp
->index
, mode
, interface
);
533 static void dsa_port_phylink_mac_link_up(struct phylink_config
*config
,
534 struct phy_device
*phydev
,
536 phy_interface_t interface
,
537 int speed
, int duplex
,
538 bool tx_pause
, bool rx_pause
)
540 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
541 struct dsa_switch
*ds
= dp
->ds
;
543 if (!ds
->ops
->phylink_mac_link_up
) {
544 if (ds
->ops
->adjust_link
&& phydev
)
545 ds
->ops
->adjust_link(ds
, dp
->index
, phydev
);
549 ds
->ops
->phylink_mac_link_up(ds
, dp
->index
, mode
, interface
, phydev
,
550 speed
, duplex
, tx_pause
, rx_pause
);
553 const struct phylink_mac_ops dsa_port_phylink_mac_ops
= {
554 .validate
= dsa_port_phylink_validate
,
555 .mac_pcs_get_state
= dsa_port_phylink_mac_pcs_get_state
,
556 .mac_config
= dsa_port_phylink_mac_config
,
557 .mac_an_restart
= dsa_port_phylink_mac_an_restart
,
558 .mac_link_down
= dsa_port_phylink_mac_link_down
,
559 .mac_link_up
= dsa_port_phylink_mac_link_up
,
562 static int dsa_port_setup_phy_of(struct dsa_port
*dp
, bool enable
)
564 struct dsa_switch
*ds
= dp
->ds
;
565 struct phy_device
*phydev
;
566 int port
= dp
->index
;
569 phydev
= dsa_port_get_phy_device(dp
);
574 return PTR_ERR(phydev
);
577 err
= genphy_resume(phydev
);
581 err
= genphy_read_status(phydev
);
585 err
= genphy_suspend(phydev
);
590 if (ds
->ops
->adjust_link
)
591 ds
->ops
->adjust_link(ds
, port
, phydev
);
593 dev_dbg(ds
->dev
, "enabled port's phy: %s", phydev_name(phydev
));
596 put_device(&phydev
->mdio
.dev
);
600 static int dsa_port_fixed_link_register_of(struct dsa_port
*dp
)
602 struct device_node
*dn
= dp
->dn
;
603 struct dsa_switch
*ds
= dp
->ds
;
604 struct phy_device
*phydev
;
605 int port
= dp
->index
;
606 phy_interface_t mode
;
609 err
= of_phy_register_fixed_link(dn
);
612 "failed to register the fixed PHY of port %d\n",
617 phydev
= of_phy_find_device(dn
);
619 err
= of_get_phy_mode(dn
, &mode
);
621 mode
= PHY_INTERFACE_MODE_NA
;
622 phydev
->interface
= mode
;
624 genphy_read_status(phydev
);
626 if (ds
->ops
->adjust_link
)
627 ds
->ops
->adjust_link(ds
, port
, phydev
);
629 put_device(&phydev
->mdio
.dev
);
634 static int dsa_port_phylink_register(struct dsa_port
*dp
)
636 struct dsa_switch
*ds
= dp
->ds
;
637 struct device_node
*port_dn
= dp
->dn
;
638 phy_interface_t mode
;
641 err
= of_get_phy_mode(port_dn
, &mode
);
643 mode
= PHY_INTERFACE_MODE_NA
;
645 dp
->pl_config
.dev
= ds
->dev
;
646 dp
->pl_config
.type
= PHYLINK_DEV
;
647 dp
->pl_config
.pcs_poll
= ds
->pcs_poll
;
649 dp
->pl
= phylink_create(&dp
->pl_config
, of_fwnode_handle(port_dn
),
650 mode
, &dsa_port_phylink_mac_ops
);
651 if (IS_ERR(dp
->pl
)) {
652 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp
->pl
));
653 return PTR_ERR(dp
->pl
);
656 err
= phylink_of_phy_connect(dp
->pl
, port_dn
, 0);
657 if (err
&& err
!= -ENODEV
) {
658 pr_err("could not attach to PHY: %d\n", err
);
659 goto err_phy_connect
;
665 phylink_destroy(dp
->pl
);
669 int dsa_port_link_register_of(struct dsa_port
*dp
)
671 struct dsa_switch
*ds
= dp
->ds
;
672 struct device_node
*phy_np
;
673 int port
= dp
->index
;
675 if (!ds
->ops
->adjust_link
) {
676 phy_np
= of_parse_phandle(dp
->dn
, "phy-handle", 0);
677 if (of_phy_is_fixed_link(dp
->dn
) || phy_np
) {
678 if (ds
->ops
->phylink_mac_link_down
)
679 ds
->ops
->phylink_mac_link_down(ds
, port
,
680 MLO_AN_FIXED
, PHY_INTERFACE_MODE_NA
);
681 return dsa_port_phylink_register(dp
);
687 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
689 if (of_phy_is_fixed_link(dp
->dn
))
690 return dsa_port_fixed_link_register_of(dp
);
692 return dsa_port_setup_phy_of(dp
, true);
695 void dsa_port_link_unregister_of(struct dsa_port
*dp
)
697 struct dsa_switch
*ds
= dp
->ds
;
699 if (!ds
->ops
->adjust_link
&& dp
->pl
) {
701 phylink_disconnect_phy(dp
->pl
);
703 phylink_destroy(dp
->pl
);
708 if (of_phy_is_fixed_link(dp
->dn
))
709 of_phy_deregister_fixed_link(dp
->dn
);
711 dsa_port_setup_phy_of(dp
, false);
714 int dsa_port_get_phy_strings(struct dsa_port
*dp
, uint8_t *data
)
716 struct phy_device
*phydev
;
717 int ret
= -EOPNOTSUPP
;
719 if (of_phy_is_fixed_link(dp
->dn
))
722 phydev
= dsa_port_get_phy_device(dp
);
723 if (IS_ERR_OR_NULL(phydev
))
726 ret
= phy_ethtool_get_strings(phydev
, data
);
727 put_device(&phydev
->mdio
.dev
);
731 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings
);
733 int dsa_port_get_ethtool_phy_stats(struct dsa_port
*dp
, uint64_t *data
)
735 struct phy_device
*phydev
;
736 int ret
= -EOPNOTSUPP
;
738 if (of_phy_is_fixed_link(dp
->dn
))
741 phydev
= dsa_port_get_phy_device(dp
);
742 if (IS_ERR_OR_NULL(phydev
))
745 ret
= phy_ethtool_get_stats(phydev
, NULL
, data
);
746 put_device(&phydev
->mdio
.dev
);
750 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats
);
752 int dsa_port_get_phy_sset_count(struct dsa_port
*dp
)
754 struct phy_device
*phydev
;
755 int ret
= -EOPNOTSUPP
;
757 if (of_phy_is_fixed_link(dp
->dn
))
760 phydev
= dsa_port_get_phy_device(dp
);
761 if (IS_ERR_OR_NULL(phydev
))
764 ret
= phy_ethtool_get_sset_count(phydev
);
765 put_device(&phydev
->mdio
.dev
);
769 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count
);