1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handling of a single switch chip, part of a switch fabric
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/if_vlan.h>
13 #include <net/switchdev.h>
17 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch
*ds
,
18 unsigned int ageing_time
)
22 for (i
= 0; i
< ds
->num_ports
; ++i
) {
23 struct dsa_port
*dp
= dsa_to_port(ds
, i
);
25 if (dp
->ageing_time
&& dp
->ageing_time
< ageing_time
)
26 ageing_time
= dp
->ageing_time
;
32 static int dsa_switch_ageing_time(struct dsa_switch
*ds
,
33 struct dsa_notifier_ageing_time_info
*info
)
35 unsigned int ageing_time
= info
->ageing_time
;
37 if (ds
->ageing_time_min
&& ageing_time
< ds
->ageing_time_min
)
40 if (ds
->ageing_time_max
&& ageing_time
> ds
->ageing_time_max
)
43 /* Program the fastest ageing time in case of multiple bridges */
44 ageing_time
= dsa_switch_fastest_ageing_time(ds
, ageing_time
);
46 if (ds
->ops
->set_ageing_time
)
47 return ds
->ops
->set_ageing_time(ds
, ageing_time
);
52 static bool dsa_switch_mtu_match(struct dsa_switch
*ds
, int port
,
53 struct dsa_notifier_mtu_info
*info
)
55 if (ds
->index
== info
->sw_index
&& port
== info
->port
)
58 /* Do not propagate to other switches in the tree if the notifier was
59 * targeted for a single switch.
61 if (info
->targeted_match
)
64 if (dsa_is_dsa_port(ds
, port
) || dsa_is_cpu_port(ds
, port
))
70 static int dsa_switch_mtu(struct dsa_switch
*ds
,
71 struct dsa_notifier_mtu_info
*info
)
75 if (!ds
->ops
->port_change_mtu
)
78 for (port
= 0; port
< ds
->num_ports
; port
++) {
79 if (dsa_switch_mtu_match(ds
, port
, info
)) {
80 ret
= ds
->ops
->port_change_mtu(ds
, port
, info
->mtu
);
89 static int dsa_switch_bridge_join(struct dsa_switch
*ds
,
90 struct dsa_notifier_bridge_info
*info
)
92 struct dsa_switch_tree
*dst
= ds
->dst
;
94 if (dst
->index
== info
->tree_index
&& ds
->index
== info
->sw_index
&&
95 ds
->ops
->port_bridge_join
)
96 return ds
->ops
->port_bridge_join(ds
, info
->port
, info
->br
);
98 if ((dst
->index
!= info
->tree_index
|| ds
->index
!= info
->sw_index
) &&
99 ds
->ops
->crosschip_bridge_join
)
100 return ds
->ops
->crosschip_bridge_join(ds
, info
->tree_index
,
102 info
->port
, info
->br
);
107 static int dsa_switch_bridge_leave(struct dsa_switch
*ds
,
108 struct dsa_notifier_bridge_info
*info
)
110 bool unset_vlan_filtering
= br_vlan_enabled(info
->br
);
111 struct dsa_switch_tree
*dst
= ds
->dst
;
112 struct netlink_ext_ack extack
= {0};
115 if (dst
->index
== info
->tree_index
&& ds
->index
== info
->sw_index
&&
116 ds
->ops
->port_bridge_leave
)
117 ds
->ops
->port_bridge_leave(ds
, info
->port
, info
->br
);
119 if ((dst
->index
!= info
->tree_index
|| ds
->index
!= info
->sw_index
) &&
120 ds
->ops
->crosschip_bridge_leave
)
121 ds
->ops
->crosschip_bridge_leave(ds
, info
->tree_index
,
122 info
->sw_index
, info
->port
,
125 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
126 * event for changing vlan_filtering setting upon slave ports leaving
127 * it. That is a good thing, because that lets us handle it and also
128 * handle the case where the switch's vlan_filtering setting is global
129 * (not per port). When that happens, the correct moment to trigger the
130 * vlan_filtering callback is only when the last port leaves the last
133 if (unset_vlan_filtering
&& ds
->vlan_filtering_is_global
) {
134 for (port
= 0; port
< ds
->num_ports
; port
++) {
135 struct net_device
*bridge_dev
;
137 bridge_dev
= dsa_to_port(ds
, port
)->bridge_dev
;
139 if (bridge_dev
&& br_vlan_enabled(bridge_dev
)) {
140 unset_vlan_filtering
= false;
145 if (unset_vlan_filtering
) {
146 err
= dsa_port_vlan_filtering(dsa_to_port(ds
, info
->port
),
149 dev_err(ds
->dev
, "port %d: %s\n", info
->port
,
151 if (err
&& err
!= EOPNOTSUPP
)
157 /* Matches for all upstream-facing ports (the CPU port and all upstream-facing
158 * DSA links) that sit between the targeted port on which the notifier was
159 * emitted and its dedicated CPU port.
161 static bool dsa_switch_host_address_match(struct dsa_switch
*ds
, int port
,
162 int info_sw_index
, int info_port
)
164 struct dsa_port
*targeted_dp
, *cpu_dp
;
165 struct dsa_switch
*targeted_ds
;
167 targeted_ds
= dsa_switch_find(ds
->dst
->index
, info_sw_index
);
168 targeted_dp
= dsa_to_port(targeted_ds
, info_port
);
169 cpu_dp
= targeted_dp
->cpu_dp
;
171 if (dsa_switch_is_upstream_of(ds
, targeted_ds
))
172 return port
== dsa_towards_port(ds
, cpu_dp
->ds
->index
,
178 static struct dsa_mac_addr
*dsa_mac_addr_find(struct list_head
*addr_list
,
179 const unsigned char *addr
,
182 struct dsa_mac_addr
*a
;
184 list_for_each_entry(a
, addr_list
, list
)
185 if (ether_addr_equal(a
->addr
, addr
) && a
->vid
== vid
)
191 static int dsa_switch_do_mdb_add(struct dsa_switch
*ds
, int port
,
192 const struct switchdev_obj_port_mdb
*mdb
)
194 struct dsa_port
*dp
= dsa_to_port(ds
, port
);
195 struct dsa_mac_addr
*a
;
198 /* No need to bother with refcounting for user ports */
199 if (!(dsa_port_is_cpu(dp
) || dsa_port_is_dsa(dp
)))
200 return ds
->ops
->port_mdb_add(ds
, port
, mdb
);
202 a
= dsa_mac_addr_find(&dp
->mdbs
, mdb
->addr
, mdb
->vid
);
204 refcount_inc(&a
->refcount
);
208 a
= kzalloc(sizeof(*a
), GFP_KERNEL
);
212 err
= ds
->ops
->port_mdb_add(ds
, port
, mdb
);
218 ether_addr_copy(a
->addr
, mdb
->addr
);
220 refcount_set(&a
->refcount
, 1);
221 list_add_tail(&a
->list
, &dp
->mdbs
);
226 static int dsa_switch_do_mdb_del(struct dsa_switch
*ds
, int port
,
227 const struct switchdev_obj_port_mdb
*mdb
)
229 struct dsa_port
*dp
= dsa_to_port(ds
, port
);
230 struct dsa_mac_addr
*a
;
233 /* No need to bother with refcounting for user ports */
234 if (!(dsa_port_is_cpu(dp
) || dsa_port_is_dsa(dp
)))
235 return ds
->ops
->port_mdb_del(ds
, port
, mdb
);
237 a
= dsa_mac_addr_find(&dp
->mdbs
, mdb
->addr
, mdb
->vid
);
241 if (!refcount_dec_and_test(&a
->refcount
))
244 err
= ds
->ops
->port_mdb_del(ds
, port
, mdb
);
246 refcount_inc(&a
->refcount
);
256 static int dsa_switch_do_fdb_add(struct dsa_switch
*ds
, int port
,
257 const unsigned char *addr
, u16 vid
)
259 struct dsa_port
*dp
= dsa_to_port(ds
, port
);
260 struct dsa_mac_addr
*a
;
263 /* No need to bother with refcounting for user ports */
264 if (!(dsa_port_is_cpu(dp
) || dsa_port_is_dsa(dp
)))
265 return ds
->ops
->port_fdb_add(ds
, port
, addr
, vid
);
267 a
= dsa_mac_addr_find(&dp
->fdbs
, addr
, vid
);
269 refcount_inc(&a
->refcount
);
273 a
= kzalloc(sizeof(*a
), GFP_KERNEL
);
277 err
= ds
->ops
->port_fdb_add(ds
, port
, addr
, vid
);
283 ether_addr_copy(a
->addr
, addr
);
285 refcount_set(&a
->refcount
, 1);
286 list_add_tail(&a
->list
, &dp
->fdbs
);
291 static int dsa_switch_do_fdb_del(struct dsa_switch
*ds
, int port
,
292 const unsigned char *addr
, u16 vid
)
294 struct dsa_port
*dp
= dsa_to_port(ds
, port
);
295 struct dsa_mac_addr
*a
;
298 /* No need to bother with refcounting for user ports */
299 if (!(dsa_port_is_cpu(dp
) || dsa_port_is_dsa(dp
)))
300 return ds
->ops
->port_fdb_del(ds
, port
, addr
, vid
);
302 a
= dsa_mac_addr_find(&dp
->fdbs
, addr
, vid
);
306 if (!refcount_dec_and_test(&a
->refcount
))
309 err
= ds
->ops
->port_fdb_del(ds
, port
, addr
, vid
);
311 refcount_inc(&a
->refcount
);
321 static int dsa_switch_host_fdb_add(struct dsa_switch
*ds
,
322 struct dsa_notifier_fdb_info
*info
)
327 if (!ds
->ops
->port_fdb_add
)
330 for (port
= 0; port
< ds
->num_ports
; port
++) {
331 if (dsa_switch_host_address_match(ds
, port
, info
->sw_index
,
333 err
= dsa_switch_do_fdb_add(ds
, port
, info
->addr
,
343 static int dsa_switch_host_fdb_del(struct dsa_switch
*ds
,
344 struct dsa_notifier_fdb_info
*info
)
349 if (!ds
->ops
->port_fdb_del
)
352 for (port
= 0; port
< ds
->num_ports
; port
++) {
353 if (dsa_switch_host_address_match(ds
, port
, info
->sw_index
,
355 err
= dsa_switch_do_fdb_del(ds
, port
, info
->addr
,
365 static int dsa_switch_fdb_add(struct dsa_switch
*ds
,
366 struct dsa_notifier_fdb_info
*info
)
368 int port
= dsa_towards_port(ds
, info
->sw_index
, info
->port
);
370 if (!ds
->ops
->port_fdb_add
)
373 return dsa_switch_do_fdb_add(ds
, port
, info
->addr
, info
->vid
);
376 static int dsa_switch_fdb_del(struct dsa_switch
*ds
,
377 struct dsa_notifier_fdb_info
*info
)
379 int port
= dsa_towards_port(ds
, info
->sw_index
, info
->port
);
381 if (!ds
->ops
->port_fdb_del
)
384 return dsa_switch_do_fdb_del(ds
, port
, info
->addr
, info
->vid
);
387 static int dsa_switch_hsr_join(struct dsa_switch
*ds
,
388 struct dsa_notifier_hsr_info
*info
)
390 if (ds
->index
== info
->sw_index
&& ds
->ops
->port_hsr_join
)
391 return ds
->ops
->port_hsr_join(ds
, info
->port
, info
->hsr
);
396 static int dsa_switch_hsr_leave(struct dsa_switch
*ds
,
397 struct dsa_notifier_hsr_info
*info
)
399 if (ds
->index
== info
->sw_index
&& ds
->ops
->port_hsr_leave
)
400 return ds
->ops
->port_hsr_leave(ds
, info
->port
, info
->hsr
);
405 static int dsa_switch_lag_change(struct dsa_switch
*ds
,
406 struct dsa_notifier_lag_info
*info
)
408 if (ds
->index
== info
->sw_index
&& ds
->ops
->port_lag_change
)
409 return ds
->ops
->port_lag_change(ds
, info
->port
);
411 if (ds
->index
!= info
->sw_index
&& ds
->ops
->crosschip_lag_change
)
412 return ds
->ops
->crosschip_lag_change(ds
, info
->sw_index
,
418 static int dsa_switch_lag_join(struct dsa_switch
*ds
,
419 struct dsa_notifier_lag_info
*info
)
421 if (ds
->index
== info
->sw_index
&& ds
->ops
->port_lag_join
)
422 return ds
->ops
->port_lag_join(ds
, info
->port
, info
->lag
,
425 if (ds
->index
!= info
->sw_index
&& ds
->ops
->crosschip_lag_join
)
426 return ds
->ops
->crosschip_lag_join(ds
, info
->sw_index
,
427 info
->port
, info
->lag
,
433 static int dsa_switch_lag_leave(struct dsa_switch
*ds
,
434 struct dsa_notifier_lag_info
*info
)
436 if (ds
->index
== info
->sw_index
&& ds
->ops
->port_lag_leave
)
437 return ds
->ops
->port_lag_leave(ds
, info
->port
, info
->lag
);
439 if (ds
->index
!= info
->sw_index
&& ds
->ops
->crosschip_lag_leave
)
440 return ds
->ops
->crosschip_lag_leave(ds
, info
->sw_index
,
441 info
->port
, info
->lag
);
446 static int dsa_switch_mdb_add(struct dsa_switch
*ds
,
447 struct dsa_notifier_mdb_info
*info
)
449 int port
= dsa_towards_port(ds
, info
->sw_index
, info
->port
);
451 if (!ds
->ops
->port_mdb_add
)
454 return dsa_switch_do_mdb_add(ds
, port
, info
->mdb
);
457 static int dsa_switch_mdb_del(struct dsa_switch
*ds
,
458 struct dsa_notifier_mdb_info
*info
)
460 int port
= dsa_towards_port(ds
, info
->sw_index
, info
->port
);
462 if (!ds
->ops
->port_mdb_del
)
465 return dsa_switch_do_mdb_del(ds
, port
, info
->mdb
);
468 static int dsa_switch_host_mdb_add(struct dsa_switch
*ds
,
469 struct dsa_notifier_mdb_info
*info
)
474 if (!ds
->ops
->port_mdb_add
)
477 for (port
= 0; port
< ds
->num_ports
; port
++) {
478 if (dsa_switch_host_address_match(ds
, port
, info
->sw_index
,
480 err
= dsa_switch_do_mdb_add(ds
, port
, info
->mdb
);
489 static int dsa_switch_host_mdb_del(struct dsa_switch
*ds
,
490 struct dsa_notifier_mdb_info
*info
)
495 if (!ds
->ops
->port_mdb_del
)
498 for (port
= 0; port
< ds
->num_ports
; port
++) {
499 if (dsa_switch_host_address_match(ds
, port
, info
->sw_index
,
501 err
= dsa_switch_do_mdb_del(ds
, port
, info
->mdb
);
510 static bool dsa_switch_vlan_match(struct dsa_switch
*ds
, int port
,
511 struct dsa_notifier_vlan_info
*info
)
513 if (ds
->index
== info
->sw_index
&& port
== info
->port
)
516 if (dsa_is_dsa_port(ds
, port
))
522 static int dsa_switch_vlan_add(struct dsa_switch
*ds
,
523 struct dsa_notifier_vlan_info
*info
)
527 if (!ds
->ops
->port_vlan_add
)
530 for (port
= 0; port
< ds
->num_ports
; port
++) {
531 if (dsa_switch_vlan_match(ds
, port
, info
)) {
532 err
= ds
->ops
->port_vlan_add(ds
, port
, info
->vlan
,
542 static int dsa_switch_vlan_del(struct dsa_switch
*ds
,
543 struct dsa_notifier_vlan_info
*info
)
545 if (!ds
->ops
->port_vlan_del
)
548 if (ds
->index
== info
->sw_index
)
549 return ds
->ops
->port_vlan_del(ds
, info
->port
, info
->vlan
);
551 /* Do not deprogram the DSA links as they may be used as conduit
552 * for other VLAN members in the fabric.
557 static int dsa_switch_change_tag_proto(struct dsa_switch
*ds
,
558 struct dsa_notifier_tag_proto_info
*info
)
560 const struct dsa_device_ops
*tag_ops
= info
->tag_ops
;
563 if (!ds
->ops
->change_tag_protocol
)
568 for (port
= 0; port
< ds
->num_ports
; port
++) {
569 if (!dsa_is_cpu_port(ds
, port
))
572 err
= ds
->ops
->change_tag_protocol(ds
, port
, tag_ops
->proto
);
576 dsa_port_set_tag_protocol(dsa_to_port(ds
, port
), tag_ops
);
579 /* Now that changing the tag protocol can no longer fail, let's update
580 * the remaining bits which are "duplicated for faster access", and the
581 * bits that depend on the tagger, such as the MTU.
583 for (port
= 0; port
< ds
->num_ports
; port
++) {
584 if (dsa_is_user_port(ds
, port
)) {
585 struct net_device
*slave
;
587 slave
= dsa_to_port(ds
, port
)->slave
;
588 dsa_slave_setup_tagger(slave
);
590 /* rtnl_mutex is held in dsa_tree_change_tag_proto */
591 dsa_slave_change_mtu(slave
, slave
->mtu
);
598 static int dsa_switch_mrp_add(struct dsa_switch
*ds
,
599 struct dsa_notifier_mrp_info
*info
)
601 if (!ds
->ops
->port_mrp_add
)
604 if (ds
->index
== info
->sw_index
)
605 return ds
->ops
->port_mrp_add(ds
, info
->port
, info
->mrp
);
610 static int dsa_switch_mrp_del(struct dsa_switch
*ds
,
611 struct dsa_notifier_mrp_info
*info
)
613 if (!ds
->ops
->port_mrp_del
)
616 if (ds
->index
== info
->sw_index
)
617 return ds
->ops
->port_mrp_del(ds
, info
->port
, info
->mrp
);
623 dsa_switch_mrp_add_ring_role(struct dsa_switch
*ds
,
624 struct dsa_notifier_mrp_ring_role_info
*info
)
626 if (!ds
->ops
->port_mrp_add
)
629 if (ds
->index
== info
->sw_index
)
630 return ds
->ops
->port_mrp_add_ring_role(ds
, info
->port
,
637 dsa_switch_mrp_del_ring_role(struct dsa_switch
*ds
,
638 struct dsa_notifier_mrp_ring_role_info
*info
)
640 if (!ds
->ops
->port_mrp_del
)
643 if (ds
->index
== info
->sw_index
)
644 return ds
->ops
->port_mrp_del_ring_role(ds
, info
->port
,
650 static int dsa_switch_event(struct notifier_block
*nb
,
651 unsigned long event
, void *info
)
653 struct dsa_switch
*ds
= container_of(nb
, struct dsa_switch
, nb
);
657 case DSA_NOTIFIER_AGEING_TIME
:
658 err
= dsa_switch_ageing_time(ds
, info
);
660 case DSA_NOTIFIER_BRIDGE_JOIN
:
661 err
= dsa_switch_bridge_join(ds
, info
);
663 case DSA_NOTIFIER_BRIDGE_LEAVE
:
664 err
= dsa_switch_bridge_leave(ds
, info
);
666 case DSA_NOTIFIER_FDB_ADD
:
667 err
= dsa_switch_fdb_add(ds
, info
);
669 case DSA_NOTIFIER_FDB_DEL
:
670 err
= dsa_switch_fdb_del(ds
, info
);
672 case DSA_NOTIFIER_HOST_FDB_ADD
:
673 err
= dsa_switch_host_fdb_add(ds
, info
);
675 case DSA_NOTIFIER_HOST_FDB_DEL
:
676 err
= dsa_switch_host_fdb_del(ds
, info
);
678 case DSA_NOTIFIER_HSR_JOIN
:
679 err
= dsa_switch_hsr_join(ds
, info
);
681 case DSA_NOTIFIER_HSR_LEAVE
:
682 err
= dsa_switch_hsr_leave(ds
, info
);
684 case DSA_NOTIFIER_LAG_CHANGE
:
685 err
= dsa_switch_lag_change(ds
, info
);
687 case DSA_NOTIFIER_LAG_JOIN
:
688 err
= dsa_switch_lag_join(ds
, info
);
690 case DSA_NOTIFIER_LAG_LEAVE
:
691 err
= dsa_switch_lag_leave(ds
, info
);
693 case DSA_NOTIFIER_MDB_ADD
:
694 err
= dsa_switch_mdb_add(ds
, info
);
696 case DSA_NOTIFIER_MDB_DEL
:
697 err
= dsa_switch_mdb_del(ds
, info
);
699 case DSA_NOTIFIER_HOST_MDB_ADD
:
700 err
= dsa_switch_host_mdb_add(ds
, info
);
702 case DSA_NOTIFIER_HOST_MDB_DEL
:
703 err
= dsa_switch_host_mdb_del(ds
, info
);
705 case DSA_NOTIFIER_VLAN_ADD
:
706 err
= dsa_switch_vlan_add(ds
, info
);
708 case DSA_NOTIFIER_VLAN_DEL
:
709 err
= dsa_switch_vlan_del(ds
, info
);
711 case DSA_NOTIFIER_MTU
:
712 err
= dsa_switch_mtu(ds
, info
);
714 case DSA_NOTIFIER_TAG_PROTO
:
715 err
= dsa_switch_change_tag_proto(ds
, info
);
717 case DSA_NOTIFIER_MRP_ADD
:
718 err
= dsa_switch_mrp_add(ds
, info
);
720 case DSA_NOTIFIER_MRP_DEL
:
721 err
= dsa_switch_mrp_del(ds
, info
);
723 case DSA_NOTIFIER_MRP_ADD_RING_ROLE
:
724 err
= dsa_switch_mrp_add_ring_role(ds
, info
);
726 case DSA_NOTIFIER_MRP_DEL_RING_ROLE
:
727 err
= dsa_switch_mrp_del_ring_role(ds
, info
);
735 dev_dbg(ds
->dev
, "breaking chain for DSA event %lu (%d)\n",
738 return notifier_from_errno(err
);
741 int dsa_switch_register_notifier(struct dsa_switch
*ds
)
743 ds
->nb
.notifier_call
= dsa_switch_event
;
745 return raw_notifier_chain_register(&ds
->dst
->nh
, &ds
->nb
);
748 void dsa_switch_unregister_notifier(struct dsa_switch
*ds
)
752 err
= raw_notifier_chain_unregister(&ds
->dst
->nh
, &ds
->nb
);
754 dev_err(ds
->dev
, "failed to unregister notifier (%d)\n", err
);