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/netdevice.h>
11 #include <linux/notifier.h>
12 #include <linux/of_mdio.h>
13 #include <linux/of_net.h>
18 #include "tag_8021q.h"
22 * dsa_port_notify - Notify the switching fabric of changes to a port
23 * @dp: port on which change occurred
24 * @e: event, must be of type DSA_NOTIFIER_*
25 * @v: event-specific value.
27 * Notify all switches in the DSA tree that this port's switch belongs to,
28 * including this switch itself, of an event. Allows the other switches to
29 * reconfigure themselves for cross-chip operations. Can also be used to
30 * reconfigure ports without net_devices (CPU ports, DSA links) whenever
31 * a user port's state changes.
33 static int dsa_port_notify(const struct dsa_port
*dp
, unsigned long e
, void *v
)
35 return dsa_tree_notify(dp
->ds
->dst
, e
, v
);
38 static void dsa_port_notify_bridge_fdb_flush(const struct dsa_port
*dp
, u16 vid
)
40 struct net_device
*brport_dev
= dsa_port_to_bridge_port(dp
);
41 struct switchdev_notifier_fdb_info info
= {
45 /* When the port becomes standalone it has already left the bridge.
46 * Don't notify the bridge in that case.
51 call_switchdev_notifiers(SWITCHDEV_FDB_FLUSH_TO_BRIDGE
,
52 brport_dev
, &info
.info
, NULL
);
55 static void dsa_port_fast_age(const struct dsa_port
*dp
)
57 struct dsa_switch
*ds
= dp
->ds
;
59 if (!ds
->ops
->port_fast_age
)
62 ds
->ops
->port_fast_age(ds
, dp
->index
);
65 dsa_port_notify_bridge_fdb_flush(dp
, 0);
68 static int dsa_port_vlan_fast_age(const struct dsa_port
*dp
, u16 vid
)
70 struct dsa_switch
*ds
= dp
->ds
;
73 if (!ds
->ops
->port_vlan_fast_age
)
76 err
= ds
->ops
->port_vlan_fast_age(ds
, dp
->index
, vid
);
79 dsa_port_notify_bridge_fdb_flush(dp
, vid
);
84 static int dsa_port_msti_fast_age(const struct dsa_port
*dp
, u16 msti
)
86 DECLARE_BITMAP(vids
, VLAN_N_VID
) = { 0 };
89 err
= br_mst_get_info(dsa_port_bridge_dev_get(dp
), msti
, vids
);
93 for_each_set_bit(vid
, vids
, VLAN_N_VID
) {
94 err
= dsa_port_vlan_fast_age(dp
, vid
);
102 static bool dsa_port_can_configure_learning(struct dsa_port
*dp
)
104 struct switchdev_brport_flags flags
= {
107 struct dsa_switch
*ds
= dp
->ds
;
110 if (!ds
->ops
->port_bridge_flags
|| !ds
->ops
->port_pre_bridge_flags
)
113 err
= ds
->ops
->port_pre_bridge_flags(ds
, dp
->index
, flags
, NULL
);
117 bool dsa_port_supports_hwtstamp(struct dsa_port
*dp
)
119 struct dsa_switch
*ds
= dp
->ds
;
120 struct ifreq ifr
= {};
123 if (!ds
->ops
->port_hwtstamp_get
|| !ds
->ops
->port_hwtstamp_set
)
126 /* "See through" shim implementations of the "get" method.
127 * Since we can't cook up a complete ioctl request structure, this will
128 * fail in copy_to_user() with -EFAULT, which hopefully is enough to
129 * detect a valid implementation.
131 err
= ds
->ops
->port_hwtstamp_get(ds
, dp
->index
, &ifr
);
132 return err
!= -EOPNOTSUPP
;
135 int dsa_port_set_state(struct dsa_port
*dp
, u8 state
, bool do_fast_age
)
137 struct dsa_switch
*ds
= dp
->ds
;
138 int port
= dp
->index
;
140 if (!ds
->ops
->port_stp_state_set
)
143 ds
->ops
->port_stp_state_set(ds
, port
, state
);
145 if (!dsa_port_can_configure_learning(dp
) ||
146 (do_fast_age
&& dp
->learning
)) {
147 /* Fast age FDB entries or flush appropriate forwarding database
148 * for the given port, if we are moving it from Learning or
149 * Forwarding state, to Disabled or Blocking or Listening state.
150 * Ports that were standalone before the STP state change don't
151 * need to fast age the FDB, since address learning is off in
155 if ((dp
->stp_state
== BR_STATE_LEARNING
||
156 dp
->stp_state
== BR_STATE_FORWARDING
) &&
157 (state
== BR_STATE_DISABLED
||
158 state
== BR_STATE_BLOCKING
||
159 state
== BR_STATE_LISTENING
))
160 dsa_port_fast_age(dp
);
163 dp
->stp_state
= state
;
168 static void dsa_port_set_state_now(struct dsa_port
*dp
, u8 state
,
171 struct dsa_switch
*ds
= dp
->ds
;
174 err
= dsa_port_set_state(dp
, state
, do_fast_age
);
175 if (err
&& err
!= -EOPNOTSUPP
) {
176 dev_err(ds
->dev
, "port %d failed to set STP state %u: %pe\n",
177 dp
->index
, state
, ERR_PTR(err
));
181 int dsa_port_set_mst_state(struct dsa_port
*dp
,
182 const struct switchdev_mst_state
*state
,
183 struct netlink_ext_ack
*extack
)
185 struct dsa_switch
*ds
= dp
->ds
;
189 if (!ds
->ops
->port_mst_state_set
)
192 err
= br_mst_get_state(dsa_port_to_bridge_port(dp
), state
->msti
,
197 err
= ds
->ops
->port_mst_state_set(ds
, dp
->index
, state
);
201 if (!(dp
->learning
&&
202 (prev_state
== BR_STATE_LEARNING
||
203 prev_state
== BR_STATE_FORWARDING
) &&
204 (state
->state
== BR_STATE_DISABLED
||
205 state
->state
== BR_STATE_BLOCKING
||
206 state
->state
== BR_STATE_LISTENING
)))
209 err
= dsa_port_msti_fast_age(dp
, state
->msti
);
211 NL_SET_ERR_MSG_MOD(extack
,
212 "Unable to flush associated VLANs");
217 int dsa_port_enable_rt(struct dsa_port
*dp
, struct phy_device
*phy
)
219 struct dsa_switch
*ds
= dp
->ds
;
220 int port
= dp
->index
;
223 if (ds
->ops
->port_enable
) {
224 err
= ds
->ops
->port_enable(ds
, port
, phy
);
230 dsa_port_set_state_now(dp
, BR_STATE_FORWARDING
, false);
233 phylink_start(dp
->pl
);
238 int dsa_port_enable(struct dsa_port
*dp
, struct phy_device
*phy
)
243 err
= dsa_port_enable_rt(dp
, phy
);
249 void dsa_port_disable_rt(struct dsa_port
*dp
)
251 struct dsa_switch
*ds
= dp
->ds
;
252 int port
= dp
->index
;
255 phylink_stop(dp
->pl
);
258 dsa_port_set_state_now(dp
, BR_STATE_DISABLED
, false);
260 if (ds
->ops
->port_disable
)
261 ds
->ops
->port_disable(ds
, port
);
264 void dsa_port_disable(struct dsa_port
*dp
)
267 dsa_port_disable_rt(dp
);
271 static void dsa_port_reset_vlan_filtering(struct dsa_port
*dp
,
272 struct dsa_bridge bridge
)
274 struct netlink_ext_ack extack
= {0};
275 bool change_vlan_filtering
= false;
276 struct dsa_switch
*ds
= dp
->ds
;
277 struct dsa_port
*other_dp
;
281 if (ds
->needs_standalone_vlan_filtering
&&
282 !br_vlan_enabled(bridge
.dev
)) {
283 change_vlan_filtering
= true;
284 vlan_filtering
= true;
285 } else if (!ds
->needs_standalone_vlan_filtering
&&
286 br_vlan_enabled(bridge
.dev
)) {
287 change_vlan_filtering
= true;
288 vlan_filtering
= false;
291 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
292 * event for changing vlan_filtering setting upon user ports leaving
293 * it. That is a good thing, because that lets us handle it and also
294 * handle the case where the switch's vlan_filtering setting is global
295 * (not per port). When that happens, the correct moment to trigger the
296 * vlan_filtering callback is only when the last port leaves the last
299 if (change_vlan_filtering
&& ds
->vlan_filtering_is_global
) {
300 dsa_switch_for_each_port(other_dp
, ds
) {
301 struct net_device
*br
= dsa_port_bridge_dev_get(other_dp
);
303 if (br
&& br_vlan_enabled(br
)) {
304 change_vlan_filtering
= false;
310 if (!change_vlan_filtering
)
313 err
= dsa_port_vlan_filtering(dp
, vlan_filtering
, &extack
);
315 dev_err(ds
->dev
, "port %d: %s\n", dp
->index
,
318 if (err
&& err
!= -EOPNOTSUPP
) {
320 "port %d failed to reset VLAN filtering to %d: %pe\n",
321 dp
->index
, vlan_filtering
, ERR_PTR(err
));
325 static int dsa_port_inherit_brport_flags(struct dsa_port
*dp
,
326 struct netlink_ext_ack
*extack
)
328 const unsigned long mask
= BR_LEARNING
| BR_FLOOD
| BR_MCAST_FLOOD
|
329 BR_BCAST_FLOOD
| BR_PORT_LOCKED
;
330 struct net_device
*brport_dev
= dsa_port_to_bridge_port(dp
);
333 for_each_set_bit(flag
, &mask
, 32) {
334 struct switchdev_brport_flags flags
= {0};
336 flags
.mask
= BIT(flag
);
338 if (br_port_flag_is_set(brport_dev
, BIT(flag
)))
339 flags
.val
= BIT(flag
);
341 err
= dsa_port_bridge_flags(dp
, flags
, extack
);
342 if (err
&& err
!= -EOPNOTSUPP
)
349 static void dsa_port_clear_brport_flags(struct dsa_port
*dp
)
351 const unsigned long val
= BR_FLOOD
| BR_MCAST_FLOOD
| BR_BCAST_FLOOD
;
352 const unsigned long mask
= BR_LEARNING
| BR_FLOOD
| BR_MCAST_FLOOD
|
353 BR_BCAST_FLOOD
| BR_PORT_LOCKED
;
356 for_each_set_bit(flag
, &mask
, 32) {
357 struct switchdev_brport_flags flags
= {0};
359 flags
.mask
= BIT(flag
);
360 flags
.val
= val
& BIT(flag
);
362 err
= dsa_port_bridge_flags(dp
, flags
, NULL
);
363 if (err
&& err
!= -EOPNOTSUPP
)
365 "failed to clear bridge port flag %lu: %pe\n",
366 flags
.val
, ERR_PTR(err
));
370 static int dsa_port_switchdev_sync_attrs(struct dsa_port
*dp
,
371 struct netlink_ext_ack
*extack
)
373 struct net_device
*brport_dev
= dsa_port_to_bridge_port(dp
);
374 struct net_device
*br
= dsa_port_bridge_dev_get(dp
);
377 err
= dsa_port_inherit_brport_flags(dp
, extack
);
381 err
= dsa_port_set_state(dp
, br_port_get_stp_state(brport_dev
), false);
382 if (err
&& err
!= -EOPNOTSUPP
)
385 err
= dsa_port_vlan_filtering(dp
, br_vlan_enabled(br
), extack
);
386 if (err
&& err
!= -EOPNOTSUPP
)
389 err
= dsa_port_ageing_time(dp
, br_get_ageing_time(br
));
390 if (err
&& err
!= -EOPNOTSUPP
)
396 static void dsa_port_switchdev_unsync_attrs(struct dsa_port
*dp
,
397 struct dsa_bridge bridge
)
399 /* Configure the port for standalone mode (no address learning,
401 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
402 * when the user requests it through netlink or sysfs, but not
403 * automatically at port join or leave, so we need to handle resetting
404 * the brport flags ourselves. But we even prefer it that way, because
405 * otherwise, some setups might never get the notification they need,
406 * for example, when a port leaves a LAG that offloads the bridge,
407 * it becomes standalone, but as far as the bridge is concerned, no
410 dsa_port_clear_brport_flags(dp
);
412 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
413 * so allow it to be in BR_STATE_FORWARDING to be kept functional
415 dsa_port_set_state_now(dp
, BR_STATE_FORWARDING
, true);
417 dsa_port_reset_vlan_filtering(dp
, bridge
);
419 /* Ageing time may be global to the switch chip, so don't change it
420 * here because we have no good reason (or value) to change it to.
424 static int dsa_port_bridge_create(struct dsa_port
*dp
,
425 struct net_device
*br
,
426 struct netlink_ext_ack
*extack
)
428 struct dsa_switch
*ds
= dp
->ds
;
429 struct dsa_bridge
*bridge
;
431 bridge
= dsa_tree_bridge_find(ds
->dst
, br
);
433 refcount_inc(&bridge
->refcount
);
438 bridge
= kzalloc(sizeof(*bridge
), GFP_KERNEL
);
442 refcount_set(&bridge
->refcount
, 1);
446 bridge
->num
= dsa_bridge_num_get(br
, ds
->max_num_bridges
);
447 if (ds
->max_num_bridges
&& !bridge
->num
) {
448 NL_SET_ERR_MSG_MOD(extack
,
449 "Range of offloadable bridges exceeded");
459 static void dsa_port_bridge_destroy(struct dsa_port
*dp
,
460 const struct net_device
*br
)
462 struct dsa_bridge
*bridge
= dp
->bridge
;
466 if (!refcount_dec_and_test(&bridge
->refcount
))
470 dsa_bridge_num_put(br
, bridge
->num
);
475 static bool dsa_port_supports_mst(struct dsa_port
*dp
)
477 struct dsa_switch
*ds
= dp
->ds
;
479 return ds
->ops
->vlan_msti_set
&&
480 ds
->ops
->port_mst_state_set
&&
481 ds
->ops
->port_vlan_fast_age
&&
482 dsa_port_can_configure_learning(dp
);
485 int dsa_port_bridge_join(struct dsa_port
*dp
, struct net_device
*br
,
486 struct netlink_ext_ack
*extack
)
488 struct dsa_notifier_bridge_info info
= {
492 struct net_device
*dev
= dp
->user
;
493 struct net_device
*brport_dev
;
496 if (br_mst_enabled(br
) && !dsa_port_supports_mst(dp
))
499 /* Here the interface is already bridged. Reflect the current
500 * configuration so that drivers can program their chips accordingly.
502 err
= dsa_port_bridge_create(dp
, br
, extack
);
506 brport_dev
= dsa_port_to_bridge_port(dp
);
508 info
.bridge
= *dp
->bridge
;
509 err
= dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN
, &info
);
513 /* Drivers which support bridge TX forwarding should set this */
514 dp
->bridge
->tx_fwd_offload
= info
.tx_fwd_offload
;
516 err
= switchdev_bridge_port_offload(brport_dev
, dev
, dp
,
517 &dsa_user_switchdev_notifier
,
518 &dsa_user_switchdev_blocking_notifier
,
519 dp
->bridge
->tx_fwd_offload
, extack
);
521 goto out_rollback_unbridge
;
523 err
= dsa_port_switchdev_sync_attrs(dp
, extack
);
525 goto out_rollback_unoffload
;
529 out_rollback_unoffload
:
530 switchdev_bridge_port_unoffload(brport_dev
, dp
,
531 &dsa_user_switchdev_notifier
,
532 &dsa_user_switchdev_blocking_notifier
);
533 dsa_flush_workqueue();
534 out_rollback_unbridge
:
535 dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE
, &info
);
537 dsa_port_bridge_destroy(dp
, br
);
541 void dsa_port_pre_bridge_leave(struct dsa_port
*dp
, struct net_device
*br
)
543 struct net_device
*brport_dev
= dsa_port_to_bridge_port(dp
);
545 /* Don't try to unoffload something that is not offloaded */
549 switchdev_bridge_port_unoffload(brport_dev
, dp
,
550 &dsa_user_switchdev_notifier
,
551 &dsa_user_switchdev_blocking_notifier
);
553 dsa_flush_workqueue();
556 void dsa_port_bridge_leave(struct dsa_port
*dp
, struct net_device
*br
)
558 struct dsa_notifier_bridge_info info
= {
563 /* If the port could not be offloaded to begin with, then
564 * there is nothing to do.
569 info
.bridge
= *dp
->bridge
;
571 /* Here the port is already unbridged. Reflect the current configuration
572 * so that drivers can program their chips accordingly.
574 dsa_port_bridge_destroy(dp
, br
);
576 err
= dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE
, &info
);
579 "port %d failed to notify DSA_NOTIFIER_BRIDGE_LEAVE: %pe\n",
580 dp
->index
, ERR_PTR(err
));
582 dsa_port_switchdev_unsync_attrs(dp
, info
.bridge
);
585 int dsa_port_lag_change(struct dsa_port
*dp
,
586 struct netdev_lag_lower_state_info
*linfo
)
588 struct dsa_notifier_lag_info info
= {
596 /* On statically configured aggregates (e.g. loadbalance
597 * without LACP) ports will always be tx_enabled, even if the
598 * link is down. Thus we require both link_up and tx_enabled
599 * in order to include it in the tx set.
601 tx_enabled
= linfo
->link_up
&& linfo
->tx_enabled
;
603 if (tx_enabled
== dp
->lag_tx_enabled
)
606 dp
->lag_tx_enabled
= tx_enabled
;
608 return dsa_port_notify(dp
, DSA_NOTIFIER_LAG_CHANGE
, &info
);
611 static int dsa_port_lag_create(struct dsa_port
*dp
,
612 struct net_device
*lag_dev
)
614 struct dsa_switch
*ds
= dp
->ds
;
617 lag
= dsa_tree_lag_find(ds
->dst
, lag_dev
);
619 refcount_inc(&lag
->refcount
);
624 lag
= kzalloc(sizeof(*lag
), GFP_KERNEL
);
628 refcount_set(&lag
->refcount
, 1);
629 mutex_init(&lag
->fdb_lock
);
630 INIT_LIST_HEAD(&lag
->fdbs
);
632 dsa_lag_map(ds
->dst
, lag
);
638 static void dsa_port_lag_destroy(struct dsa_port
*dp
)
640 struct dsa_lag
*lag
= dp
->lag
;
643 dp
->lag_tx_enabled
= false;
645 if (!refcount_dec_and_test(&lag
->refcount
))
648 WARN_ON(!list_empty(&lag
->fdbs
));
649 dsa_lag_unmap(dp
->ds
->dst
, lag
);
653 int dsa_port_lag_join(struct dsa_port
*dp
, struct net_device
*lag_dev
,
654 struct netdev_lag_upper_info
*uinfo
,
655 struct netlink_ext_ack
*extack
)
657 struct dsa_notifier_lag_info info
= {
662 struct net_device
*bridge_dev
;
665 err
= dsa_port_lag_create(dp
, lag_dev
);
670 err
= dsa_port_notify(dp
, DSA_NOTIFIER_LAG_JOIN
, &info
);
674 bridge_dev
= netdev_master_upper_dev_get(lag_dev
);
675 if (!bridge_dev
|| !netif_is_bridge_master(bridge_dev
))
678 err
= dsa_port_bridge_join(dp
, bridge_dev
, extack
);
680 goto err_bridge_join
;
685 dsa_port_notify(dp
, DSA_NOTIFIER_LAG_LEAVE
, &info
);
687 dsa_port_lag_destroy(dp
);
692 void dsa_port_pre_lag_leave(struct dsa_port
*dp
, struct net_device
*lag_dev
)
694 struct net_device
*br
= dsa_port_bridge_dev_get(dp
);
697 dsa_port_pre_bridge_leave(dp
, br
);
700 void dsa_port_lag_leave(struct dsa_port
*dp
, struct net_device
*lag_dev
)
702 struct net_device
*br
= dsa_port_bridge_dev_get(dp
);
703 struct dsa_notifier_lag_info info
= {
711 /* Port might have been part of a LAG that in turn was
712 * attached to a bridge.
715 dsa_port_bridge_leave(dp
, br
);
719 dsa_port_lag_destroy(dp
);
721 err
= dsa_port_notify(dp
, DSA_NOTIFIER_LAG_LEAVE
, &info
);
724 "port %d failed to notify DSA_NOTIFIER_LAG_LEAVE: %pe\n",
725 dp
->index
, ERR_PTR(err
));
728 /* Must be called under rcu_read_lock() */
729 static bool dsa_port_can_apply_vlan_filtering(struct dsa_port
*dp
,
731 struct netlink_ext_ack
*extack
)
733 struct dsa_switch
*ds
= dp
->ds
;
734 struct dsa_port
*other_dp
;
737 /* VLAN awareness was off, so the question is "can we turn it on".
738 * We may have had 8021q uppers, those need to go. Make sure we don't
739 * enter an inconsistent state: deny changing the VLAN awareness state
740 * as long as we have 8021q uppers.
742 if (vlan_filtering
&& dsa_port_is_user(dp
)) {
743 struct net_device
*br
= dsa_port_bridge_dev_get(dp
);
744 struct net_device
*upper_dev
, *user
= dp
->user
;
745 struct list_head
*iter
;
747 netdev_for_each_upper_dev_rcu(user
, upper_dev
, iter
) {
748 struct bridge_vlan_info br_info
;
751 if (!is_vlan_dev(upper_dev
))
754 vid
= vlan_dev_vlan_id(upper_dev
);
756 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
757 * device, respectively the VID is not found, returning
758 * 0 means success, which is a failure for us here.
760 err
= br_vlan_get_info(br
, vid
, &br_info
);
762 NL_SET_ERR_MSG_MOD(extack
,
763 "Must first remove VLAN uppers having VIDs also present in bridge");
769 if (!ds
->vlan_filtering_is_global
)
772 /* For cases where enabling/disabling VLAN awareness is global to the
773 * switch, we need to handle the case where multiple bridges span
774 * different ports of the same switch device and one of them has a
775 * different setting than what is being requested.
777 dsa_switch_for_each_port(other_dp
, ds
) {
778 struct net_device
*other_br
= dsa_port_bridge_dev_get(other_dp
);
780 /* If it's the same bridge, it also has same
781 * vlan_filtering setting => no need to check
783 if (!other_br
|| other_br
== dsa_port_bridge_dev_get(dp
))
786 if (br_vlan_enabled(other_br
) != vlan_filtering
) {
787 NL_SET_ERR_MSG_MOD(extack
,
788 "VLAN filtering is a global setting");
795 int dsa_port_vlan_filtering(struct dsa_port
*dp
, bool vlan_filtering
,
796 struct netlink_ext_ack
*extack
)
798 bool old_vlan_filtering
= dsa_port_is_vlan_filtering(dp
);
799 struct dsa_switch
*ds
= dp
->ds
;
803 if (!ds
->ops
->port_vlan_filtering
)
806 /* We are called from dsa_user_switchdev_blocking_event(),
807 * which is not under rcu_read_lock(), unlike
808 * dsa_user_switchdev_event().
811 apply
= dsa_port_can_apply_vlan_filtering(dp
, vlan_filtering
, extack
);
816 if (dsa_port_is_vlan_filtering(dp
) == vlan_filtering
)
819 err
= ds
->ops
->port_vlan_filtering(ds
, dp
->index
, vlan_filtering
,
824 if (ds
->vlan_filtering_is_global
) {
825 struct dsa_port
*other_dp
;
827 ds
->vlan_filtering
= vlan_filtering
;
829 dsa_switch_for_each_user_port(other_dp
, ds
) {
830 struct net_device
*user
= other_dp
->user
;
832 /* We might be called in the unbind path, so not
833 * all user devices might still be registered.
838 err
= dsa_user_manage_vlan_filtering(user
,
844 dp
->vlan_filtering
= vlan_filtering
;
846 err
= dsa_user_manage_vlan_filtering(dp
->user
,
855 ds
->ops
->port_vlan_filtering(ds
, dp
->index
, old_vlan_filtering
, NULL
);
857 if (ds
->vlan_filtering_is_global
)
858 ds
->vlan_filtering
= old_vlan_filtering
;
860 dp
->vlan_filtering
= old_vlan_filtering
;
865 /* This enforces legacy behavior for switch drivers which assume they can't
866 * receive VLAN configuration when joining a bridge with vlan_filtering=0
868 bool dsa_port_skip_vlan_configuration(struct dsa_port
*dp
)
870 struct net_device
*br
= dsa_port_bridge_dev_get(dp
);
871 struct dsa_switch
*ds
= dp
->ds
;
876 return !ds
->configure_vlan_while_not_filtering
&& !br_vlan_enabled(br
);
879 int dsa_port_ageing_time(struct dsa_port
*dp
, clock_t ageing_clock
)
881 unsigned long ageing_jiffies
= clock_t_to_jiffies(ageing_clock
);
882 unsigned int ageing_time
= jiffies_to_msecs(ageing_jiffies
);
883 struct dsa_notifier_ageing_time_info info
;
886 info
.ageing_time
= ageing_time
;
888 err
= dsa_port_notify(dp
, DSA_NOTIFIER_AGEING_TIME
, &info
);
892 dp
->ageing_time
= ageing_time
;
897 int dsa_port_mst_enable(struct dsa_port
*dp
, bool on
,
898 struct netlink_ext_ack
*extack
)
900 if (on
&& !dsa_port_supports_mst(dp
)) {
901 NL_SET_ERR_MSG_MOD(extack
, "Hardware does not support MST");
908 int dsa_port_pre_bridge_flags(const struct dsa_port
*dp
,
909 struct switchdev_brport_flags flags
,
910 struct netlink_ext_ack
*extack
)
912 struct dsa_switch
*ds
= dp
->ds
;
914 if (!ds
->ops
->port_pre_bridge_flags
)
917 return ds
->ops
->port_pre_bridge_flags(ds
, dp
->index
, flags
, extack
);
920 int dsa_port_bridge_flags(struct dsa_port
*dp
,
921 struct switchdev_brport_flags flags
,
922 struct netlink_ext_ack
*extack
)
924 struct dsa_switch
*ds
= dp
->ds
;
927 if (!ds
->ops
->port_bridge_flags
)
930 err
= ds
->ops
->port_bridge_flags(ds
, dp
->index
, flags
, extack
);
934 if (flags
.mask
& BR_LEARNING
) {
935 bool learning
= flags
.val
& BR_LEARNING
;
937 if (learning
== dp
->learning
)
940 if ((dp
->learning
&& !learning
) &&
941 (dp
->stp_state
== BR_STATE_LEARNING
||
942 dp
->stp_state
== BR_STATE_FORWARDING
))
943 dsa_port_fast_age(dp
);
945 dp
->learning
= learning
;
951 void dsa_port_set_host_flood(struct dsa_port
*dp
, bool uc
, bool mc
)
953 struct dsa_switch
*ds
= dp
->ds
;
955 if (ds
->ops
->port_set_host_flood
)
956 ds
->ops
->port_set_host_flood(ds
, dp
->index
, uc
, mc
);
959 int dsa_port_vlan_msti(struct dsa_port
*dp
,
960 const struct switchdev_vlan_msti
*msti
)
962 struct dsa_switch
*ds
= dp
->ds
;
964 if (!ds
->ops
->vlan_msti_set
)
967 return ds
->ops
->vlan_msti_set(ds
, *dp
->bridge
, msti
);
970 int dsa_port_mtu_change(struct dsa_port
*dp
, int new_mtu
)
972 struct dsa_notifier_mtu_info info
= {
977 return dsa_port_notify(dp
, DSA_NOTIFIER_MTU
, &info
);
980 int dsa_port_fdb_add(struct dsa_port
*dp
, const unsigned char *addr
,
983 struct dsa_notifier_fdb_info info
= {
988 .type
= DSA_DB_BRIDGE
,
989 .bridge
= *dp
->bridge
,
993 /* Refcounting takes bridge.num as a key, and should be global for all
994 * bridges in the absence of FDB isolation, and per bridge otherwise.
995 * Force the bridge.num to zero here in the absence of FDB isolation.
997 if (!dp
->ds
->fdb_isolation
)
998 info
.db
.bridge
.num
= 0;
1000 return dsa_port_notify(dp
, DSA_NOTIFIER_FDB_ADD
, &info
);
1003 int dsa_port_fdb_del(struct dsa_port
*dp
, const unsigned char *addr
,
1006 struct dsa_notifier_fdb_info info
= {
1011 .type
= DSA_DB_BRIDGE
,
1012 .bridge
= *dp
->bridge
,
1016 if (!dp
->ds
->fdb_isolation
)
1017 info
.db
.bridge
.num
= 0;
1019 return dsa_port_notify(dp
, DSA_NOTIFIER_FDB_DEL
, &info
);
1022 static int dsa_port_host_fdb_add(struct dsa_port
*dp
,
1023 const unsigned char *addr
, u16 vid
,
1026 struct dsa_notifier_fdb_info info
= {
1033 return dsa_port_notify(dp
, DSA_NOTIFIER_HOST_FDB_ADD
, &info
);
1036 int dsa_port_standalone_host_fdb_add(struct dsa_port
*dp
,
1037 const unsigned char *addr
, u16 vid
)
1039 struct dsa_db db
= {
1040 .type
= DSA_DB_PORT
,
1044 return dsa_port_host_fdb_add(dp
, addr
, vid
, db
);
1047 int dsa_port_bridge_host_fdb_add(struct dsa_port
*dp
,
1048 const unsigned char *addr
, u16 vid
)
1050 struct net_device
*conduit
= dsa_port_to_conduit(dp
);
1051 struct dsa_db db
= {
1052 .type
= DSA_DB_BRIDGE
,
1053 .bridge
= *dp
->bridge
,
1057 if (!dp
->ds
->fdb_isolation
)
1060 /* Avoid a call to __dev_set_promiscuity() on the conduit, which
1061 * requires rtnl_lock(), since we can't guarantee that is held here,
1062 * and we can't take it either.
1064 if (conduit
->priv_flags
& IFF_UNICAST_FLT
) {
1065 err
= dev_uc_add(conduit
, addr
);
1070 return dsa_port_host_fdb_add(dp
, addr
, vid
, db
);
1073 static int dsa_port_host_fdb_del(struct dsa_port
*dp
,
1074 const unsigned char *addr
, u16 vid
,
1077 struct dsa_notifier_fdb_info info
= {
1084 return dsa_port_notify(dp
, DSA_NOTIFIER_HOST_FDB_DEL
, &info
);
1087 int dsa_port_standalone_host_fdb_del(struct dsa_port
*dp
,
1088 const unsigned char *addr
, u16 vid
)
1090 struct dsa_db db
= {
1091 .type
= DSA_DB_PORT
,
1095 return dsa_port_host_fdb_del(dp
, addr
, vid
, db
);
1098 int dsa_port_bridge_host_fdb_del(struct dsa_port
*dp
,
1099 const unsigned char *addr
, u16 vid
)
1101 struct net_device
*conduit
= dsa_port_to_conduit(dp
);
1102 struct dsa_db db
= {
1103 .type
= DSA_DB_BRIDGE
,
1104 .bridge
= *dp
->bridge
,
1108 if (!dp
->ds
->fdb_isolation
)
1111 if (conduit
->priv_flags
& IFF_UNICAST_FLT
) {
1112 err
= dev_uc_del(conduit
, addr
);
1117 return dsa_port_host_fdb_del(dp
, addr
, vid
, db
);
1120 int dsa_port_lag_fdb_add(struct dsa_port
*dp
, const unsigned char *addr
,
1123 struct dsa_notifier_lag_fdb_info info
= {
1128 .type
= DSA_DB_BRIDGE
,
1129 .bridge
= *dp
->bridge
,
1133 if (!dp
->ds
->fdb_isolation
)
1134 info
.db
.bridge
.num
= 0;
1136 return dsa_port_notify(dp
, DSA_NOTIFIER_LAG_FDB_ADD
, &info
);
1139 int dsa_port_lag_fdb_del(struct dsa_port
*dp
, const unsigned char *addr
,
1142 struct dsa_notifier_lag_fdb_info info
= {
1147 .type
= DSA_DB_BRIDGE
,
1148 .bridge
= *dp
->bridge
,
1152 if (!dp
->ds
->fdb_isolation
)
1153 info
.db
.bridge
.num
= 0;
1155 return dsa_port_notify(dp
, DSA_NOTIFIER_LAG_FDB_DEL
, &info
);
1158 int dsa_port_fdb_dump(struct dsa_port
*dp
, dsa_fdb_dump_cb_t
*cb
, void *data
)
1160 struct dsa_switch
*ds
= dp
->ds
;
1161 int port
= dp
->index
;
1163 if (!ds
->ops
->port_fdb_dump
)
1166 return ds
->ops
->port_fdb_dump(ds
, port
, cb
, data
);
1169 int dsa_port_mdb_add(const struct dsa_port
*dp
,
1170 const struct switchdev_obj_port_mdb
*mdb
)
1172 struct dsa_notifier_mdb_info info
= {
1176 .type
= DSA_DB_BRIDGE
,
1177 .bridge
= *dp
->bridge
,
1181 if (!dp
->ds
->fdb_isolation
)
1182 info
.db
.bridge
.num
= 0;
1184 return dsa_port_notify(dp
, DSA_NOTIFIER_MDB_ADD
, &info
);
1187 int dsa_port_mdb_del(const struct dsa_port
*dp
,
1188 const struct switchdev_obj_port_mdb
*mdb
)
1190 struct dsa_notifier_mdb_info info
= {
1194 .type
= DSA_DB_BRIDGE
,
1195 .bridge
= *dp
->bridge
,
1199 if (!dp
->ds
->fdb_isolation
)
1200 info
.db
.bridge
.num
= 0;
1202 return dsa_port_notify(dp
, DSA_NOTIFIER_MDB_DEL
, &info
);
1205 static int dsa_port_host_mdb_add(const struct dsa_port
*dp
,
1206 const struct switchdev_obj_port_mdb
*mdb
,
1209 struct dsa_notifier_mdb_info info
= {
1215 return dsa_port_notify(dp
, DSA_NOTIFIER_HOST_MDB_ADD
, &info
);
1218 int dsa_port_standalone_host_mdb_add(const struct dsa_port
*dp
,
1219 const struct switchdev_obj_port_mdb
*mdb
)
1221 struct dsa_db db
= {
1222 .type
= DSA_DB_PORT
,
1226 return dsa_port_host_mdb_add(dp
, mdb
, db
);
1229 int dsa_port_bridge_host_mdb_add(const struct dsa_port
*dp
,
1230 const struct switchdev_obj_port_mdb
*mdb
)
1232 struct net_device
*conduit
= dsa_port_to_conduit(dp
);
1233 struct dsa_db db
= {
1234 .type
= DSA_DB_BRIDGE
,
1235 .bridge
= *dp
->bridge
,
1239 if (!dp
->ds
->fdb_isolation
)
1242 err
= dev_mc_add(conduit
, mdb
->addr
);
1246 return dsa_port_host_mdb_add(dp
, mdb
, db
);
1249 static int dsa_port_host_mdb_del(const struct dsa_port
*dp
,
1250 const struct switchdev_obj_port_mdb
*mdb
,
1253 struct dsa_notifier_mdb_info info
= {
1259 return dsa_port_notify(dp
, DSA_NOTIFIER_HOST_MDB_DEL
, &info
);
1262 int dsa_port_standalone_host_mdb_del(const struct dsa_port
*dp
,
1263 const struct switchdev_obj_port_mdb
*mdb
)
1265 struct dsa_db db
= {
1266 .type
= DSA_DB_PORT
,
1270 return dsa_port_host_mdb_del(dp
, mdb
, db
);
1273 int dsa_port_bridge_host_mdb_del(const struct dsa_port
*dp
,
1274 const struct switchdev_obj_port_mdb
*mdb
)
1276 struct net_device
*conduit
= dsa_port_to_conduit(dp
);
1277 struct dsa_db db
= {
1278 .type
= DSA_DB_BRIDGE
,
1279 .bridge
= *dp
->bridge
,
1283 if (!dp
->ds
->fdb_isolation
)
1286 err
= dev_mc_del(conduit
, mdb
->addr
);
1290 return dsa_port_host_mdb_del(dp
, mdb
, db
);
1293 int dsa_port_vlan_add(struct dsa_port
*dp
,
1294 const struct switchdev_obj_port_vlan
*vlan
,
1295 struct netlink_ext_ack
*extack
)
1297 struct dsa_notifier_vlan_info info
= {
1303 return dsa_port_notify(dp
, DSA_NOTIFIER_VLAN_ADD
, &info
);
1306 int dsa_port_vlan_del(struct dsa_port
*dp
,
1307 const struct switchdev_obj_port_vlan
*vlan
)
1309 struct dsa_notifier_vlan_info info
= {
1314 return dsa_port_notify(dp
, DSA_NOTIFIER_VLAN_DEL
, &info
);
1317 int dsa_port_host_vlan_add(struct dsa_port
*dp
,
1318 const struct switchdev_obj_port_vlan
*vlan
,
1319 struct netlink_ext_ack
*extack
)
1321 struct net_device
*conduit
= dsa_port_to_conduit(dp
);
1322 struct dsa_notifier_vlan_info info
= {
1329 err
= dsa_port_notify(dp
, DSA_NOTIFIER_HOST_VLAN_ADD
, &info
);
1330 if (err
&& err
!= -EOPNOTSUPP
)
1333 vlan_vid_add(conduit
, htons(ETH_P_8021Q
), vlan
->vid
);
1338 int dsa_port_host_vlan_del(struct dsa_port
*dp
,
1339 const struct switchdev_obj_port_vlan
*vlan
)
1341 struct net_device
*conduit
= dsa_port_to_conduit(dp
);
1342 struct dsa_notifier_vlan_info info
= {
1348 err
= dsa_port_notify(dp
, DSA_NOTIFIER_HOST_VLAN_DEL
, &info
);
1349 if (err
&& err
!= -EOPNOTSUPP
)
1352 vlan_vid_del(conduit
, htons(ETH_P_8021Q
), vlan
->vid
);
1357 int dsa_port_mrp_add(const struct dsa_port
*dp
,
1358 const struct switchdev_obj_mrp
*mrp
)
1360 struct dsa_switch
*ds
= dp
->ds
;
1362 if (!ds
->ops
->port_mrp_add
)
1365 return ds
->ops
->port_mrp_add(ds
, dp
->index
, mrp
);
1368 int dsa_port_mrp_del(const struct dsa_port
*dp
,
1369 const struct switchdev_obj_mrp
*mrp
)
1371 struct dsa_switch
*ds
= dp
->ds
;
1373 if (!ds
->ops
->port_mrp_del
)
1376 return ds
->ops
->port_mrp_del(ds
, dp
->index
, mrp
);
1379 int dsa_port_mrp_add_ring_role(const struct dsa_port
*dp
,
1380 const struct switchdev_obj_ring_role_mrp
*mrp
)
1382 struct dsa_switch
*ds
= dp
->ds
;
1384 if (!ds
->ops
->port_mrp_add_ring_role
)
1387 return ds
->ops
->port_mrp_add_ring_role(ds
, dp
->index
, mrp
);
1390 int dsa_port_mrp_del_ring_role(const struct dsa_port
*dp
,
1391 const struct switchdev_obj_ring_role_mrp
*mrp
)
1393 struct dsa_switch
*ds
= dp
->ds
;
1395 if (!ds
->ops
->port_mrp_del_ring_role
)
1398 return ds
->ops
->port_mrp_del_ring_role(ds
, dp
->index
, mrp
);
1401 static int dsa_port_assign_conduit(struct dsa_port
*dp
,
1402 struct net_device
*conduit
,
1403 struct netlink_ext_ack
*extack
,
1406 struct dsa_switch
*ds
= dp
->ds
;
1407 int port
= dp
->index
, err
;
1409 err
= ds
->ops
->port_change_conduit(ds
, port
, conduit
, extack
);
1410 if (err
&& !fail_on_err
)
1411 dev_err(ds
->dev
, "port %d failed to assign conduit %s: %pe\n",
1412 port
, conduit
->name
, ERR_PTR(err
));
1414 if (err
&& fail_on_err
)
1417 dp
->cpu_dp
= conduit
->dsa_ptr
;
1418 dp
->cpu_port_in_lag
= netif_is_lag_master(conduit
);
1423 /* Change the dp->cpu_dp affinity for a user port. Note that both cross-chip
1424 * notifiers and drivers have implicit assumptions about user-to-CPU-port
1425 * mappings, so we unfortunately cannot delay the deletion of the objects
1426 * (switchdev, standalone addresses, standalone VLANs) on the old CPU port
1427 * until the new CPU port has been set up. So we need to completely tear down
1428 * the old CPU port before changing it, and restore it on errors during the
1429 * bringup of the new one.
1431 int dsa_port_change_conduit(struct dsa_port
*dp
, struct net_device
*conduit
,
1432 struct netlink_ext_ack
*extack
)
1434 struct net_device
*bridge_dev
= dsa_port_bridge_dev_get(dp
);
1435 struct net_device
*old_conduit
= dsa_port_to_conduit(dp
);
1436 struct net_device
*dev
= dp
->user
;
1437 struct dsa_switch
*ds
= dp
->ds
;
1438 bool vlan_filtering
;
1441 /* Bridges may hold host FDB, MDB and VLAN objects. These need to be
1442 * migrated, so dynamically unoffload and later reoffload the bridge
1446 dsa_port_pre_bridge_leave(dp
, bridge_dev
);
1447 dsa_port_bridge_leave(dp
, bridge_dev
);
1450 /* The port might still be VLAN filtering even if it's no longer
1451 * under a bridge, either due to ds->vlan_filtering_is_global or
1452 * ds->needs_standalone_vlan_filtering. In turn this means VLANs
1455 vlan_filtering
= dsa_port_is_vlan_filtering(dp
);
1456 if (vlan_filtering
) {
1457 err
= dsa_user_manage_vlan_filtering(dev
, false);
1459 NL_SET_ERR_MSG_MOD(extack
,
1460 "Failed to remove standalone VLANs");
1461 goto rewind_old_bridge
;
1465 /* Standalone addresses, and addresses of upper interfaces like
1466 * VLAN, LAG, HSR need to be migrated.
1468 dsa_user_unsync_ha(dev
);
1470 /* If live-changing, we also need to uninstall the user device address
1471 * from the port FDB and the conduit interface.
1473 if (dev
->flags
& IFF_UP
)
1474 dsa_user_host_uc_uninstall(dev
);
1476 err
= dsa_port_assign_conduit(dp
, conduit
, extack
, true);
1478 goto rewind_old_addrs
;
1480 /* If the port doesn't have its own MAC address and relies on the DSA
1481 * conduit's one, inherit it again from the new DSA conduit.
1483 if (is_zero_ether_addr(dp
->mac
))
1484 eth_hw_addr_inherit(dev
, conduit
);
1486 /* If live-changing, we need to install the user device address to the
1487 * port FDB and the conduit interface.
1489 if (dev
->flags
& IFF_UP
) {
1490 err
= dsa_user_host_uc_install(dev
, dev
->dev_addr
);
1492 NL_SET_ERR_MSG_MOD(extack
,
1493 "Failed to install host UC address");
1494 goto rewind_addr_inherit
;
1498 dsa_user_sync_ha(dev
);
1500 if (vlan_filtering
) {
1501 err
= dsa_user_manage_vlan_filtering(dev
, true);
1503 NL_SET_ERR_MSG_MOD(extack
,
1504 "Failed to restore standalone VLANs");
1505 goto rewind_new_addrs
;
1510 err
= dsa_port_bridge_join(dp
, bridge_dev
, extack
);
1511 if (err
&& err
== -EOPNOTSUPP
) {
1512 NL_SET_ERR_MSG_MOD(extack
,
1513 "Failed to reoffload bridge");
1514 goto rewind_new_vlan
;
1522 dsa_user_manage_vlan_filtering(dev
, false);
1525 dsa_user_unsync_ha(dev
);
1527 if (dev
->flags
& IFF_UP
)
1528 dsa_user_host_uc_uninstall(dev
);
1530 rewind_addr_inherit
:
1531 if (is_zero_ether_addr(dp
->mac
))
1532 eth_hw_addr_inherit(dev
, old_conduit
);
1534 dsa_port_assign_conduit(dp
, old_conduit
, NULL
, false);
1536 /* Restore the objects on the old CPU port */
1538 if (dev
->flags
& IFF_UP
) {
1539 tmp
= dsa_user_host_uc_install(dev
, dev
->dev_addr
);
1542 "port %d failed to restore host UC address: %pe\n",
1543 dp
->index
, ERR_PTR(tmp
));
1547 dsa_user_sync_ha(dev
);
1549 if (vlan_filtering
) {
1550 tmp
= dsa_user_manage_vlan_filtering(dev
, true);
1553 "port %d failed to restore standalone VLANs: %pe\n",
1554 dp
->index
, ERR_PTR(tmp
));
1560 tmp
= dsa_port_bridge_join(dp
, bridge_dev
, extack
);
1563 "port %d failed to rejoin bridge %s: %pe\n",
1564 dp
->index
, bridge_dev
->name
, ERR_PTR(tmp
));
1571 void dsa_port_set_tag_protocol(struct dsa_port
*cpu_dp
,
1572 const struct dsa_device_ops
*tag_ops
)
1574 cpu_dp
->rcv
= tag_ops
->rcv
;
1575 cpu_dp
->tag_ops
= tag_ops
;
1578 static struct phylink_pcs
*
1579 dsa_port_phylink_mac_select_pcs(struct phylink_config
*config
,
1580 phy_interface_t interface
)
1582 struct dsa_port
*dp
= dsa_phylink_to_port(config
);
1583 struct phylink_pcs
*pcs
= ERR_PTR(-EOPNOTSUPP
);
1584 struct dsa_switch
*ds
= dp
->ds
;
1586 if (ds
->ops
->phylink_mac_select_pcs
)
1587 pcs
= ds
->ops
->phylink_mac_select_pcs(ds
, dp
->index
, interface
);
1592 static void dsa_port_phylink_mac_config(struct phylink_config
*config
,
1594 const struct phylink_link_state
*state
)
1596 struct dsa_port
*dp
= dsa_phylink_to_port(config
);
1597 struct dsa_switch
*ds
= dp
->ds
;
1599 if (!ds
->ops
->phylink_mac_config
)
1602 ds
->ops
->phylink_mac_config(ds
, dp
->index
, mode
, state
);
1605 static void dsa_port_phylink_mac_link_down(struct phylink_config
*config
,
1607 phy_interface_t interface
)
1609 struct dsa_port
*dp
= dsa_phylink_to_port(config
);
1610 struct dsa_switch
*ds
= dp
->ds
;
1612 if (!ds
->ops
->phylink_mac_link_down
)
1615 ds
->ops
->phylink_mac_link_down(ds
, dp
->index
, mode
, interface
);
1618 static void dsa_port_phylink_mac_link_up(struct phylink_config
*config
,
1619 struct phy_device
*phydev
,
1621 phy_interface_t interface
,
1622 int speed
, int duplex
,
1623 bool tx_pause
, bool rx_pause
)
1625 struct dsa_port
*dp
= dsa_phylink_to_port(config
);
1626 struct dsa_switch
*ds
= dp
->ds
;
1628 if (!ds
->ops
->phylink_mac_link_up
)
1631 ds
->ops
->phylink_mac_link_up(ds
, dp
->index
, mode
, interface
, phydev
,
1632 speed
, duplex
, tx_pause
, rx_pause
);
1635 static const struct phylink_mac_ops dsa_port_phylink_mac_ops
= {
1636 .mac_select_pcs
= dsa_port_phylink_mac_select_pcs
,
1637 .mac_config
= dsa_port_phylink_mac_config
,
1638 .mac_link_down
= dsa_port_phylink_mac_link_down
,
1639 .mac_link_up
= dsa_port_phylink_mac_link_up
,
1642 int dsa_port_phylink_create(struct dsa_port
*dp
)
1644 const struct phylink_mac_ops
*mac_ops
;
1645 struct dsa_switch
*ds
= dp
->ds
;
1646 phy_interface_t mode
;
1650 err
= of_get_phy_mode(dp
->dn
, &mode
);
1652 mode
= PHY_INTERFACE_MODE_NA
;
1654 if (ds
->ops
->phylink_get_caps
) {
1655 ds
->ops
->phylink_get_caps(ds
, dp
->index
, &dp
->pl_config
);
1657 /* For legacy drivers */
1658 if (mode
!= PHY_INTERFACE_MODE_NA
) {
1659 __set_bit(mode
, dp
->pl_config
.supported_interfaces
);
1661 __set_bit(PHY_INTERFACE_MODE_INTERNAL
,
1662 dp
->pl_config
.supported_interfaces
);
1663 __set_bit(PHY_INTERFACE_MODE_GMII
,
1664 dp
->pl_config
.supported_interfaces
);
1668 mac_ops
= &dsa_port_phylink_mac_ops
;
1669 if (ds
->phylink_mac_ops
)
1670 mac_ops
= ds
->phylink_mac_ops
;
1672 pl
= phylink_create(&dp
->pl_config
, of_fwnode_handle(dp
->dn
), mode
,
1675 pr_err("error creating PHYLINK: %ld\n", PTR_ERR(pl
));
1684 void dsa_port_phylink_destroy(struct dsa_port
*dp
)
1686 phylink_destroy(dp
->pl
);
1690 static int dsa_shared_port_phylink_register(struct dsa_port
*dp
)
1692 struct dsa_switch
*ds
= dp
->ds
;
1693 struct device_node
*port_dn
= dp
->dn
;
1696 dp
->pl_config
.dev
= ds
->dev
;
1697 dp
->pl_config
.type
= PHYLINK_DEV
;
1699 err
= dsa_port_phylink_create(dp
);
1703 err
= phylink_of_phy_connect(dp
->pl
, port_dn
, 0);
1704 if (err
&& err
!= -ENODEV
) {
1705 pr_err("could not attach to PHY: %d\n", err
);
1706 goto err_phy_connect
;
1712 dsa_port_phylink_destroy(dp
);
1716 /* During the initial DSA driver migration to OF, port nodes were sometimes
1717 * added to device trees with no indication of how they should operate from a
1718 * link management perspective (phy-handle, fixed-link, etc). Additionally, the
1719 * phy-mode may be absent. The interpretation of these port OF nodes depends on
1722 * User ports with no phy-handle or fixed-link are expected to connect to an
1723 * internal PHY located on the ds->user_mii_bus at an MDIO address equal to
1724 * the port number. This description is still actively supported.
1726 * Shared (CPU and DSA) ports with no phy-handle or fixed-link are expected to
1727 * operate at the maximum speed that their phy-mode is capable of. If the
1728 * phy-mode is absent, they are expected to operate using the phy-mode
1729 * supported by the port that gives the highest link speed. It is unspecified
1730 * if the port should use flow control or not, half duplex or full duplex, or
1731 * if the phy-mode is a SERDES link, whether in-band autoneg is expected to be
1734 * In the latter case of shared ports, omitting the link management description
1735 * from the firmware node is deprecated and strongly discouraged. DSA uses
1736 * phylink, which rejects the firmware nodes of these ports for lacking
1737 * required properties.
1739 * For switches in this table, DSA will skip enforcing validation and will
1740 * later omit registering a phylink instance for the shared ports, if they lack
1741 * a fixed-link, a phy-handle, or a managed = "in-band-status" property.
1742 * It becomes the responsibility of the driver to ensure that these ports
1743 * operate at the maximum speed (whatever this means) and will interoperate
1744 * with the DSA conduit or other cascade port, since phylink methods will not be
1747 * If you are considering expanding this table for newly introduced switches,
1748 * think again. It is OK to remove switches from this table if there aren't DT
1749 * blobs in circulation which rely on defaulting the shared ports.
1751 static const char * const dsa_switches_apply_workarounds
[] = {
1752 #if IS_ENABLED(CONFIG_NET_DSA_XRS700X)
1758 #if IS_ENABLED(CONFIG_B53)
1768 "brcm,bcm53010-srab",
1769 "brcm,bcm53011-srab",
1770 "brcm,bcm53012-srab",
1771 "brcm,bcm53018-srab",
1772 "brcm,bcm53019-srab",
1773 "brcm,bcm5301x-srab",
1774 "brcm,bcm11360-srab",
1775 "brcm,bcm58522-srab",
1776 "brcm,bcm58525-srab",
1777 "brcm,bcm58535-srab",
1778 "brcm,bcm58622-srab",
1779 "brcm,bcm58623-srab",
1780 "brcm,bcm58625-srab",
1781 "brcm,bcm88312-srab",
1785 "brcm,bcm3384-switch",
1786 "brcm,bcm6328-switch",
1787 "brcm,bcm6368-switch",
1788 "brcm,bcm63xx-switch",
1790 #if IS_ENABLED(CONFIG_NET_DSA_BCM_SF2)
1791 "brcm,bcm7445-switch-v4.0",
1792 "brcm,bcm7278-switch-v4.0",
1793 "brcm,bcm7278-switch-v4.8",
1795 #if IS_ENABLED(CONFIG_NET_DSA_LANTIQ_GSWIP)
1796 "lantiq,xrx200-gswip",
1797 "lantiq,xrx300-gswip",
1798 "lantiq,xrx330-gswip",
1800 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6060)
1801 "marvell,mv88e6060",
1803 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6XXX)
1804 "marvell,mv88e6085",
1805 "marvell,mv88e6190",
1806 "marvell,mv88e6250",
1808 #if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON)
1809 "microchip,ksz8765",
1810 "microchip,ksz8794",
1811 "microchip,ksz8795",
1812 "microchip,ksz8863",
1813 "microchip,ksz8873",
1814 "microchip,ksz9477",
1815 "microchip,ksz9897",
1816 "microchip,ksz9893",
1817 "microchip,ksz9563",
1818 "microchip,ksz8563",
1819 "microchip,ksz9567",
1821 #if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_MDIO)
1822 "smsc,lan9303-mdio",
1824 #if IS_ENABLED(CONFIG_NET_DSA_SMSC_LAN9303_I2C)
1830 static void dsa_shared_port_validate_of(struct dsa_port
*dp
,
1831 bool *missing_phy_mode
,
1832 bool *missing_link_description
)
1834 struct device_node
*dn
= dp
->dn
, *phy_np
;
1835 struct dsa_switch
*ds
= dp
->ds
;
1836 phy_interface_t mode
;
1838 *missing_phy_mode
= false;
1839 *missing_link_description
= false;
1841 if (of_get_phy_mode(dn
, &mode
)) {
1842 *missing_phy_mode
= true;
1844 "OF node %pOF of %s port %d lacks the required \"phy-mode\" property\n",
1845 dn
, dsa_port_is_cpu(dp
) ? "CPU" : "DSA", dp
->index
);
1848 /* Note: of_phy_is_fixed_link() also returns true for
1849 * managed = "in-band-status"
1851 if (of_phy_is_fixed_link(dn
))
1854 phy_np
= of_parse_phandle(dn
, "phy-handle", 0);
1856 of_node_put(phy_np
);
1860 *missing_link_description
= true;
1863 "OF node %pOF of %s port %d lacks the required \"phy-handle\", \"fixed-link\" or \"managed\" properties\n",
1864 dn
, dsa_port_is_cpu(dp
) ? "CPU" : "DSA", dp
->index
);
1867 static void dsa_shared_port_link_down(struct dsa_port
*dp
)
1869 struct dsa_switch
*ds
= dp
->ds
;
1871 if (ds
->phylink_mac_ops
&& ds
->phylink_mac_ops
->mac_link_down
)
1872 ds
->phylink_mac_ops
->mac_link_down(&dp
->pl_config
, MLO_AN_FIXED
,
1873 PHY_INTERFACE_MODE_NA
);
1874 else if (ds
->ops
->phylink_mac_link_down
)
1875 ds
->ops
->phylink_mac_link_down(ds
, dp
->index
, MLO_AN_FIXED
,
1876 PHY_INTERFACE_MODE_NA
);
1879 int dsa_shared_port_link_register_of(struct dsa_port
*dp
)
1881 struct dsa_switch
*ds
= dp
->ds
;
1882 bool missing_link_description
;
1883 bool missing_phy_mode
;
1885 dsa_shared_port_validate_of(dp
, &missing_phy_mode
,
1886 &missing_link_description
);
1888 if ((missing_phy_mode
|| missing_link_description
) &&
1889 !of_device_compatible_match(ds
->dev
->of_node
,
1890 dsa_switches_apply_workarounds
))
1893 if (missing_link_description
) {
1895 "Skipping phylink registration for %s port %d\n",
1896 dsa_port_is_cpu(dp
) ? "CPU" : "DSA", dp
->index
);
1898 dsa_shared_port_link_down(dp
);
1900 return dsa_shared_port_phylink_register(dp
);
1906 void dsa_shared_port_link_unregister_of(struct dsa_port
*dp
)
1910 phylink_disconnect_phy(dp
->pl
);
1912 dsa_port_phylink_destroy(dp
);
1917 int dsa_port_hsr_join(struct dsa_port
*dp
, struct net_device
*hsr
,
1918 struct netlink_ext_ack
*extack
)
1920 struct dsa_switch
*ds
= dp
->ds
;
1923 if (!ds
->ops
->port_hsr_join
)
1928 err
= ds
->ops
->port_hsr_join(ds
, dp
->index
, hsr
, extack
);
1935 void dsa_port_hsr_leave(struct dsa_port
*dp
, struct net_device
*hsr
)
1937 struct dsa_switch
*ds
= dp
->ds
;
1942 if (ds
->ops
->port_hsr_leave
) {
1943 err
= ds
->ops
->port_hsr_leave(ds
, dp
->index
, hsr
);
1945 dev_err(dp
->ds
->dev
,
1946 "port %d failed to leave HSR %s: %pe\n",
1947 dp
->index
, hsr
->name
, ERR_PTR(err
));
1951 int dsa_port_tag_8021q_vlan_add(struct dsa_port
*dp
, u16 vid
, bool broadcast
)
1953 struct dsa_notifier_tag_8021q_vlan_info info
= {
1959 return dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_ADD
, &info
);
1961 return dsa_port_notify(dp
, DSA_NOTIFIER_TAG_8021Q_VLAN_ADD
, &info
);
1964 void dsa_port_tag_8021q_vlan_del(struct dsa_port
*dp
, u16 vid
, bool broadcast
)
1966 struct dsa_notifier_tag_8021q_vlan_info info
= {
1973 err
= dsa_broadcast(DSA_NOTIFIER_TAG_8021Q_VLAN_DEL
, &info
);
1975 err
= dsa_port_notify(dp
, DSA_NOTIFIER_TAG_8021Q_VLAN_DEL
, &info
);
1977 dev_err(dp
->ds
->dev
,
1978 "port %d failed to notify tag_8021q VLAN %d deletion: %pe\n",
1979 dp
->index
, vid
, ERR_PTR(err
));