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(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
);
84 void dsa_port_disable(struct dsa_port
*dp
)
86 struct dsa_switch
*ds
= dp
->ds
;
90 dsa_port_set_state_now(dp
, BR_STATE_DISABLED
);
92 if (ds
->ops
->port_disable
)
93 ds
->ops
->port_disable(ds
, port
);
96 int dsa_port_bridge_join(struct dsa_port
*dp
, struct net_device
*br
)
98 struct dsa_notifier_bridge_info info
= {
99 .sw_index
= dp
->ds
->index
,
105 /* Set the flooding mode before joining the port in the switch */
106 err
= dsa_port_bridge_flags(dp
, BR_FLOOD
| BR_MCAST_FLOOD
, NULL
);
110 /* Here the interface is already bridged. Reflect the current
111 * configuration so that drivers can program their chips accordingly.
115 err
= dsa_port_notify(dp
, DSA_NOTIFIER_BRIDGE_JOIN
, &info
);
117 /* The bridging is rolled back on error */
119 dsa_port_bridge_flags(dp
, 0, NULL
);
120 dp
->bridge_dev
= NULL
;
126 void dsa_port_bridge_leave(struct dsa_port
*dp
, struct net_device
*br
)
128 struct dsa_notifier_bridge_info info
= {
129 .sw_index
= dp
->ds
->index
,
135 /* Here the port is already unbridged. Reflect the current configuration
136 * so that drivers can program their chips accordingly.
138 dp
->bridge_dev
= NULL
;
140 err
= dsa_port_notify(dp
, DSA_NOTIFIER_BRIDGE_LEAVE
, &info
);
142 pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
144 /* Port is leaving the bridge, disable flooding */
145 dsa_port_bridge_flags(dp
, 0, NULL
);
147 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
148 * so allow it to be in BR_STATE_FORWARDING to be kept functional
150 dsa_port_set_state_now(dp
, BR_STATE_FORWARDING
);
153 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port
*dp
,
156 struct dsa_switch
*ds
= dp
->ds
;
159 if (!ds
->vlan_filtering_is_global
)
162 /* For cases where enabling/disabling VLAN awareness is global to the
163 * switch, we need to handle the case where multiple bridges span
164 * different ports of the same switch device and one of them has a
165 * different setting than what is being requested.
167 for (i
= 0; i
< ds
->num_ports
; i
++) {
168 struct net_device
*other_bridge
;
170 other_bridge
= dsa_to_port(ds
, i
)->bridge_dev
;
173 /* If it's the same bridge, it also has same
174 * vlan_filtering setting => no need to check
176 if (other_bridge
== dp
->bridge_dev
)
178 if (br_vlan_enabled(other_bridge
) != vlan_filtering
) {
179 dev_err(ds
->dev
, "VLAN filtering is a global setting\n");
186 int dsa_port_vlan_filtering(struct dsa_port
*dp
, bool vlan_filtering
,
187 struct switchdev_trans
*trans
)
189 struct dsa_switch
*ds
= dp
->ds
;
192 /* bridge skips -EOPNOTSUPP, so skip the prepare phase */
193 if (switchdev_trans_ph_prepare(trans
))
196 if (!ds
->ops
->port_vlan_filtering
)
199 if (!dsa_port_can_apply_vlan_filtering(dp
, vlan_filtering
))
202 if (dsa_port_is_vlan_filtering(dp
) == vlan_filtering
)
205 err
= ds
->ops
->port_vlan_filtering(ds
, dp
->index
,
210 if (ds
->vlan_filtering_is_global
)
211 ds
->vlan_filtering
= vlan_filtering
;
213 dp
->vlan_filtering
= vlan_filtering
;
217 int dsa_port_ageing_time(struct dsa_port
*dp
, clock_t ageing_clock
,
218 struct switchdev_trans
*trans
)
220 unsigned long ageing_jiffies
= clock_t_to_jiffies(ageing_clock
);
221 unsigned int ageing_time
= jiffies_to_msecs(ageing_jiffies
);
222 struct dsa_notifier_ageing_time_info info
= {
223 .ageing_time
= ageing_time
,
227 if (switchdev_trans_ph_prepare(trans
))
228 return dsa_port_notify(dp
, DSA_NOTIFIER_AGEING_TIME
, &info
);
230 dp
->ageing_time
= ageing_time
;
232 return dsa_port_notify(dp
, DSA_NOTIFIER_AGEING_TIME
, &info
);
235 int dsa_port_pre_bridge_flags(const struct dsa_port
*dp
, unsigned long flags
,
236 struct switchdev_trans
*trans
)
238 struct dsa_switch
*ds
= dp
->ds
;
240 if (!ds
->ops
->port_egress_floods
||
241 (flags
& ~(BR_FLOOD
| BR_MCAST_FLOOD
)))
247 int dsa_port_bridge_flags(const struct dsa_port
*dp
, unsigned long flags
,
248 struct switchdev_trans
*trans
)
250 struct dsa_switch
*ds
= dp
->ds
;
251 int port
= dp
->index
;
254 if (switchdev_trans_ph_prepare(trans
))
257 if (ds
->ops
->port_egress_floods
)
258 err
= ds
->ops
->port_egress_floods(ds
, port
, flags
& BR_FLOOD
,
259 flags
& BR_MCAST_FLOOD
);
264 int dsa_port_mrouter(struct dsa_port
*dp
, bool mrouter
,
265 struct switchdev_trans
*trans
)
267 struct dsa_switch
*ds
= dp
->ds
;
268 int port
= dp
->index
;
270 if (switchdev_trans_ph_prepare(trans
))
271 return ds
->ops
->port_egress_floods
? 0 : -EOPNOTSUPP
;
273 return ds
->ops
->port_egress_floods(ds
, port
, true, mrouter
);
276 int dsa_port_fdb_add(struct dsa_port
*dp
, const unsigned char *addr
,
279 struct dsa_notifier_fdb_info info
= {
280 .sw_index
= dp
->ds
->index
,
286 return dsa_port_notify(dp
, DSA_NOTIFIER_FDB_ADD
, &info
);
289 int dsa_port_fdb_del(struct dsa_port
*dp
, const unsigned char *addr
,
292 struct dsa_notifier_fdb_info info
= {
293 .sw_index
= dp
->ds
->index
,
300 return dsa_port_notify(dp
, DSA_NOTIFIER_FDB_DEL
, &info
);
303 int dsa_port_fdb_dump(struct dsa_port
*dp
, dsa_fdb_dump_cb_t
*cb
, void *data
)
305 struct dsa_switch
*ds
= dp
->ds
;
306 int port
= dp
->index
;
308 if (!ds
->ops
->port_fdb_dump
)
311 return ds
->ops
->port_fdb_dump(ds
, port
, cb
, data
);
314 int dsa_port_mdb_add(const struct dsa_port
*dp
,
315 const struct switchdev_obj_port_mdb
*mdb
,
316 struct switchdev_trans
*trans
)
318 struct dsa_notifier_mdb_info info
= {
319 .sw_index
= dp
->ds
->index
,
325 return dsa_port_notify(dp
, DSA_NOTIFIER_MDB_ADD
, &info
);
328 int dsa_port_mdb_del(const struct dsa_port
*dp
,
329 const struct switchdev_obj_port_mdb
*mdb
)
331 struct dsa_notifier_mdb_info info
= {
332 .sw_index
= dp
->ds
->index
,
337 return dsa_port_notify(dp
, DSA_NOTIFIER_MDB_DEL
, &info
);
340 int dsa_port_vlan_add(struct dsa_port
*dp
,
341 const struct switchdev_obj_port_vlan
*vlan
,
342 struct switchdev_trans
*trans
)
344 struct dsa_notifier_vlan_info info
= {
345 .sw_index
= dp
->ds
->index
,
351 return dsa_port_notify(dp
, DSA_NOTIFIER_VLAN_ADD
, &info
);
354 int dsa_port_vlan_del(struct dsa_port
*dp
,
355 const struct switchdev_obj_port_vlan
*vlan
)
357 struct dsa_notifier_vlan_info info
= {
358 .sw_index
= dp
->ds
->index
,
363 return dsa_port_notify(dp
, DSA_NOTIFIER_VLAN_DEL
, &info
);
366 int dsa_port_vid_add(struct dsa_port
*dp
, u16 vid
, u16 flags
)
368 struct switchdev_obj_port_vlan vlan
= {
369 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
374 struct switchdev_trans trans
;
377 trans
.ph_prepare
= true;
378 err
= dsa_port_vlan_add(dp
, &vlan
, &trans
);
382 trans
.ph_prepare
= false;
383 return dsa_port_vlan_add(dp
, &vlan
, &trans
);
385 EXPORT_SYMBOL(dsa_port_vid_add
);
387 int dsa_port_vid_del(struct dsa_port
*dp
, u16 vid
)
389 struct switchdev_obj_port_vlan vlan
= {
390 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
395 return dsa_port_vlan_del(dp
, &vlan
);
397 EXPORT_SYMBOL(dsa_port_vid_del
);
399 static struct phy_device
*dsa_port_get_phy_device(struct dsa_port
*dp
)
401 struct device_node
*phy_dn
;
402 struct phy_device
*phydev
;
404 phy_dn
= of_parse_phandle(dp
->dn
, "phy-handle", 0);
408 phydev
= of_phy_find_device(phy_dn
);
411 return ERR_PTR(-EPROBE_DEFER
);
418 static void dsa_port_phylink_validate(struct phylink_config
*config
,
419 unsigned long *supported
,
420 struct phylink_link_state
*state
)
422 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
423 struct dsa_switch
*ds
= dp
->ds
;
425 if (!ds
->ops
->phylink_validate
)
428 ds
->ops
->phylink_validate(ds
, dp
->index
, supported
, state
);
431 static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config
*config
,
432 struct phylink_link_state
*state
)
434 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
435 struct dsa_switch
*ds
= dp
->ds
;
437 /* Only called for inband modes */
438 if (!ds
->ops
->phylink_mac_link_state
) {
443 if (ds
->ops
->phylink_mac_link_state(ds
, dp
->index
, state
) < 0)
447 static void dsa_port_phylink_mac_config(struct phylink_config
*config
,
449 const struct phylink_link_state
*state
)
451 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
452 struct dsa_switch
*ds
= dp
->ds
;
454 if (!ds
->ops
->phylink_mac_config
)
457 ds
->ops
->phylink_mac_config(ds
, dp
->index
, mode
, state
);
460 static void dsa_port_phylink_mac_an_restart(struct phylink_config
*config
)
462 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
463 struct dsa_switch
*ds
= dp
->ds
;
465 if (!ds
->ops
->phylink_mac_an_restart
)
468 ds
->ops
->phylink_mac_an_restart(ds
, dp
->index
);
471 static void dsa_port_phylink_mac_link_down(struct phylink_config
*config
,
473 phy_interface_t interface
)
475 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
476 struct phy_device
*phydev
= NULL
;
477 struct dsa_switch
*ds
= dp
->ds
;
479 if (dsa_is_user_port(ds
, dp
->index
))
480 phydev
= dp
->slave
->phydev
;
482 if (!ds
->ops
->phylink_mac_link_down
) {
483 if (ds
->ops
->adjust_link
&& phydev
)
484 ds
->ops
->adjust_link(ds
, dp
->index
, phydev
);
488 ds
->ops
->phylink_mac_link_down(ds
, dp
->index
, mode
, interface
);
491 static void dsa_port_phylink_mac_link_up(struct phylink_config
*config
,
493 phy_interface_t interface
,
494 struct phy_device
*phydev
)
496 struct dsa_port
*dp
= container_of(config
, struct dsa_port
, pl_config
);
497 struct dsa_switch
*ds
= dp
->ds
;
499 if (!ds
->ops
->phylink_mac_link_up
) {
500 if (ds
->ops
->adjust_link
&& phydev
)
501 ds
->ops
->adjust_link(ds
, dp
->index
, phydev
);
505 ds
->ops
->phylink_mac_link_up(ds
, dp
->index
, mode
, interface
, phydev
);
508 const struct phylink_mac_ops dsa_port_phylink_mac_ops
= {
509 .validate
= dsa_port_phylink_validate
,
510 .mac_pcs_get_state
= dsa_port_phylink_mac_pcs_get_state
,
511 .mac_config
= dsa_port_phylink_mac_config
,
512 .mac_an_restart
= dsa_port_phylink_mac_an_restart
,
513 .mac_link_down
= dsa_port_phylink_mac_link_down
,
514 .mac_link_up
= dsa_port_phylink_mac_link_up
,
517 static int dsa_port_setup_phy_of(struct dsa_port
*dp
, bool enable
)
519 struct dsa_switch
*ds
= dp
->ds
;
520 struct phy_device
*phydev
;
521 int port
= dp
->index
;
524 phydev
= dsa_port_get_phy_device(dp
);
529 return PTR_ERR(phydev
);
532 err
= genphy_resume(phydev
);
536 err
= genphy_read_status(phydev
);
540 err
= genphy_suspend(phydev
);
545 if (ds
->ops
->adjust_link
)
546 ds
->ops
->adjust_link(ds
, port
, phydev
);
548 dev_dbg(ds
->dev
, "enabled port's phy: %s", phydev_name(phydev
));
551 put_device(&phydev
->mdio
.dev
);
555 static int dsa_port_fixed_link_register_of(struct dsa_port
*dp
)
557 struct device_node
*dn
= dp
->dn
;
558 struct dsa_switch
*ds
= dp
->ds
;
559 struct phy_device
*phydev
;
560 int port
= dp
->index
;
561 phy_interface_t mode
;
564 err
= of_phy_register_fixed_link(dn
);
567 "failed to register the fixed PHY of port %d\n",
572 phydev
= of_phy_find_device(dn
);
574 err
= of_get_phy_mode(dn
, &mode
);
576 mode
= PHY_INTERFACE_MODE_NA
;
577 phydev
->interface
= mode
;
579 genphy_read_status(phydev
);
581 if (ds
->ops
->adjust_link
)
582 ds
->ops
->adjust_link(ds
, port
, phydev
);
584 put_device(&phydev
->mdio
.dev
);
589 static int dsa_port_phylink_register(struct dsa_port
*dp
)
591 struct dsa_switch
*ds
= dp
->ds
;
592 struct device_node
*port_dn
= dp
->dn
;
593 phy_interface_t mode
;
596 err
= of_get_phy_mode(port_dn
, &mode
);
598 mode
= PHY_INTERFACE_MODE_NA
;
600 dp
->pl_config
.dev
= ds
->dev
;
601 dp
->pl_config
.type
= PHYLINK_DEV
;
602 dp
->pl_config
.pcs_poll
= ds
->pcs_poll
;
604 dp
->pl
= phylink_create(&dp
->pl_config
, of_fwnode_handle(port_dn
),
605 mode
, &dsa_port_phylink_mac_ops
);
606 if (IS_ERR(dp
->pl
)) {
607 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp
->pl
));
608 return PTR_ERR(dp
->pl
);
611 err
= phylink_of_phy_connect(dp
->pl
, port_dn
, 0);
612 if (err
&& err
!= -ENODEV
) {
613 pr_err("could not attach to PHY: %d\n", err
);
614 goto err_phy_connect
;
618 phylink_start(dp
->pl
);
624 phylink_destroy(dp
->pl
);
628 int dsa_port_link_register_of(struct dsa_port
*dp
)
630 struct dsa_switch
*ds
= dp
->ds
;
632 if (!ds
->ops
->adjust_link
)
633 return dsa_port_phylink_register(dp
);
636 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
638 if (of_phy_is_fixed_link(dp
->dn
))
639 return dsa_port_fixed_link_register_of(dp
);
641 return dsa_port_setup_phy_of(dp
, true);
644 void dsa_port_link_unregister_of(struct dsa_port
*dp
)
646 struct dsa_switch
*ds
= dp
->ds
;
648 if (!ds
->ops
->adjust_link
) {
650 phylink_disconnect_phy(dp
->pl
);
652 phylink_destroy(dp
->pl
);
656 if (of_phy_is_fixed_link(dp
->dn
))
657 of_phy_deregister_fixed_link(dp
->dn
);
659 dsa_port_setup_phy_of(dp
, false);
662 int dsa_port_get_phy_strings(struct dsa_port
*dp
, uint8_t *data
)
664 struct phy_device
*phydev
;
665 int ret
= -EOPNOTSUPP
;
667 if (of_phy_is_fixed_link(dp
->dn
))
670 phydev
= dsa_port_get_phy_device(dp
);
671 if (IS_ERR_OR_NULL(phydev
))
674 ret
= phy_ethtool_get_strings(phydev
, data
);
675 put_device(&phydev
->mdio
.dev
);
679 EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings
);
681 int dsa_port_get_ethtool_phy_stats(struct dsa_port
*dp
, uint64_t *data
)
683 struct phy_device
*phydev
;
684 int ret
= -EOPNOTSUPP
;
686 if (of_phy_is_fixed_link(dp
->dn
))
689 phydev
= dsa_port_get_phy_device(dp
);
690 if (IS_ERR_OR_NULL(phydev
))
693 ret
= phy_ethtool_get_stats(phydev
, NULL
, data
);
694 put_device(&phydev
->mdio
.dev
);
698 EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats
);
700 int dsa_port_get_phy_sset_count(struct dsa_port
*dp
)
702 struct phy_device
*phydev
;
703 int ret
= -EOPNOTSUPP
;
705 if (of_phy_is_fixed_link(dp
->dn
))
708 phydev
= dsa_port_get_phy_device(dp
);
709 if (IS_ERR_OR_NULL(phydev
))
712 ret
= phy_ethtool_get_sset_count(phydev
);
713 put_device(&phydev
->mdio
.dev
);
717 EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count
);