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;
95 if (dst->index == info->tree_index && ds->index == info->sw_index) {
96 if (!ds->ops->port_bridge_join)
99 err = ds->ops->port_bridge_join(ds, info->port, info->br);
104 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
105 ds->ops->crosschip_bridge_join) {
106 err = ds->ops->crosschip_bridge_join(ds, info->tree_index,
108 info->port, info->br);
113 return dsa_tag_8021q_bridge_join(ds, info);
116 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
117 struct dsa_notifier_bridge_info *info)
119 struct dsa_switch_tree *dst = ds->dst;
120 struct netlink_ext_ack extack = {0};
121 bool change_vlan_filtering = false;
125 if (dst->index == info->tree_index && ds->index == info->sw_index &&
126 ds->ops->port_bridge_leave)
127 ds->ops->port_bridge_leave(ds, info->port, info->br);
129 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
130 ds->ops->crosschip_bridge_leave)
131 ds->ops->crosschip_bridge_leave(ds, info->tree_index,
132 info->sw_index, info->port,
135 if (ds->needs_standalone_vlan_filtering && !br_vlan_enabled(info->br)) {
136 change_vlan_filtering = true;
137 vlan_filtering = true;
138 } else if (!ds->needs_standalone_vlan_filtering &&
139 br_vlan_enabled(info->br)) {
140 change_vlan_filtering = true;
141 vlan_filtering = false;
144 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
145 * event for changing vlan_filtering setting upon slave ports leaving
146 * it. That is a good thing, because that lets us handle it and also
147 * handle the case where the switch's vlan_filtering setting is global
148 * (not per port). When that happens, the correct moment to trigger the
149 * vlan_filtering callback is only when the last port leaves the last
152 if (change_vlan_filtering && ds->vlan_filtering_is_global) {
153 for (port = 0; port < ds->num_ports; port++) {
154 struct net_device *bridge_dev;
156 bridge_dev = dsa_to_port(ds, port)->bridge_dev;
158 if (bridge_dev && br_vlan_enabled(bridge_dev)) {
159 change_vlan_filtering = false;
165 if (change_vlan_filtering) {
166 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
167 vlan_filtering, &extack);
169 dev_err(ds->dev, "port %d: %s\n", info->port,
171 if (err && err != -EOPNOTSUPP)
175 return dsa_tag_8021q_bridge_leave(ds, info);
178 /* Matches for all upstream-facing ports (the CPU port and all upstream-facing
179 * DSA links) that sit between the targeted port on which the notifier was
180 * emitted and its dedicated CPU port.
182 static bool dsa_switch_host_address_match(struct dsa_switch *ds, int port,
183 int info_sw_index, int info_port)
185 struct dsa_port *targeted_dp, *cpu_dp;
186 struct dsa_switch *targeted_ds;
188 targeted_ds = dsa_switch_find(ds->dst->index, info_sw_index);
189 targeted_dp = dsa_to_port(targeted_ds, info_port);
190 cpu_dp = targeted_dp->cpu_dp;
192 if (dsa_switch_is_upstream_of(ds, targeted_ds))
193 return port == dsa_towards_port(ds, cpu_dp->ds->index,
199 static struct dsa_mac_addr *dsa_mac_addr_find(struct list_head *addr_list,
200 const unsigned char *addr,
203 struct dsa_mac_addr *a;
205 list_for_each_entry(a, addr_list, list)
206 if (ether_addr_equal(a->addr, addr) && a->vid == vid)
212 static int dsa_switch_do_mdb_add(struct dsa_switch *ds, int port,
213 const struct switchdev_obj_port_mdb *mdb)
215 struct dsa_port *dp = dsa_to_port(ds, port);
216 struct dsa_mac_addr *a;
219 /* No need to bother with refcounting for user ports */
220 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
221 return ds->ops->port_mdb_add(ds, port, mdb);
223 a = dsa_mac_addr_find(&dp->mdbs, mdb->addr, mdb->vid);
225 refcount_inc(&a->refcount);
229 a = kzalloc(sizeof(*a), GFP_KERNEL);
233 err = ds->ops->port_mdb_add(ds, port, mdb);
239 ether_addr_copy(a->addr, mdb->addr);
241 refcount_set(&a->refcount, 1);
242 list_add_tail(&a->list, &dp->mdbs);
247 static int dsa_switch_do_mdb_del(struct dsa_switch *ds, int port,
248 const struct switchdev_obj_port_mdb *mdb)
250 struct dsa_port *dp = dsa_to_port(ds, port);
251 struct dsa_mac_addr *a;
254 /* No need to bother with refcounting for user ports */
255 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
256 return ds->ops->port_mdb_del(ds, port, mdb);
258 a = dsa_mac_addr_find(&dp->mdbs, mdb->addr, mdb->vid);
262 if (!refcount_dec_and_test(&a->refcount))
265 err = ds->ops->port_mdb_del(ds, port, mdb);
267 refcount_inc(&a->refcount);
277 static int dsa_switch_do_fdb_add(struct dsa_switch *ds, int port,
278 const unsigned char *addr, u16 vid)
280 struct dsa_port *dp = dsa_to_port(ds, port);
281 struct dsa_mac_addr *a;
284 /* No need to bother with refcounting for user ports */
285 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
286 return ds->ops->port_fdb_add(ds, port, addr, vid);
288 a = dsa_mac_addr_find(&dp->fdbs, addr, vid);
290 refcount_inc(&a->refcount);
294 a = kzalloc(sizeof(*a), GFP_KERNEL);
298 err = ds->ops->port_fdb_add(ds, port, addr, vid);
304 ether_addr_copy(a->addr, addr);
306 refcount_set(&a->refcount, 1);
307 list_add_tail(&a->list, &dp->fdbs);
312 static int dsa_switch_do_fdb_del(struct dsa_switch *ds, int port,
313 const unsigned char *addr, u16 vid)
315 struct dsa_port *dp = dsa_to_port(ds, port);
316 struct dsa_mac_addr *a;
319 /* No need to bother with refcounting for user ports */
320 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
321 return ds->ops->port_fdb_del(ds, port, addr, vid);
323 a = dsa_mac_addr_find(&dp->fdbs, addr, vid);
327 if (!refcount_dec_and_test(&a->refcount))
330 err = ds->ops->port_fdb_del(ds, port, addr, vid);
332 refcount_inc(&a->refcount);
342 static int dsa_switch_host_fdb_add(struct dsa_switch *ds,
343 struct dsa_notifier_fdb_info *info)
348 if (!ds->ops->port_fdb_add)
351 for (port = 0; port < ds->num_ports; port++) {
352 if (dsa_switch_host_address_match(ds, port, info->sw_index,
354 err = dsa_switch_do_fdb_add(ds, port, info->addr,
364 static int dsa_switch_host_fdb_del(struct dsa_switch *ds,
365 struct dsa_notifier_fdb_info *info)
370 if (!ds->ops->port_fdb_del)
373 for (port = 0; port < ds->num_ports; port++) {
374 if (dsa_switch_host_address_match(ds, port, info->sw_index,
376 err = dsa_switch_do_fdb_del(ds, port, info->addr,
386 static int dsa_switch_fdb_add(struct dsa_switch *ds,
387 struct dsa_notifier_fdb_info *info)
389 int port = dsa_towards_port(ds, info->sw_index, info->port);
391 if (!ds->ops->port_fdb_add)
394 return dsa_switch_do_fdb_add(ds, port, info->addr, info->vid);
397 static int dsa_switch_fdb_del(struct dsa_switch *ds,
398 struct dsa_notifier_fdb_info *info)
400 int port = dsa_towards_port(ds, info->sw_index, info->port);
402 if (!ds->ops->port_fdb_del)
405 return dsa_switch_do_fdb_del(ds, port, info->addr, info->vid);
408 static int dsa_switch_hsr_join(struct dsa_switch *ds,
409 struct dsa_notifier_hsr_info *info)
411 if (ds->index == info->sw_index && ds->ops->port_hsr_join)
412 return ds->ops->port_hsr_join(ds, info->port, info->hsr);
417 static int dsa_switch_hsr_leave(struct dsa_switch *ds,
418 struct dsa_notifier_hsr_info *info)
420 if (ds->index == info->sw_index && ds->ops->port_hsr_leave)
421 return ds->ops->port_hsr_leave(ds, info->port, info->hsr);
426 static int dsa_switch_lag_change(struct dsa_switch *ds,
427 struct dsa_notifier_lag_info *info)
429 if (ds->index == info->sw_index && ds->ops->port_lag_change)
430 return ds->ops->port_lag_change(ds, info->port);
432 if (ds->index != info->sw_index && ds->ops->crosschip_lag_change)
433 return ds->ops->crosschip_lag_change(ds, info->sw_index,
439 static int dsa_switch_lag_join(struct dsa_switch *ds,
440 struct dsa_notifier_lag_info *info)
442 if (ds->index == info->sw_index && ds->ops->port_lag_join)
443 return ds->ops->port_lag_join(ds, info->port, info->lag,
446 if (ds->index != info->sw_index && ds->ops->crosschip_lag_join)
447 return ds->ops->crosschip_lag_join(ds, info->sw_index,
448 info->port, info->lag,
454 static int dsa_switch_lag_leave(struct dsa_switch *ds,
455 struct dsa_notifier_lag_info *info)
457 if (ds->index == info->sw_index && ds->ops->port_lag_leave)
458 return ds->ops->port_lag_leave(ds, info->port, info->lag);
460 if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave)
461 return ds->ops->crosschip_lag_leave(ds, info->sw_index,
462 info->port, info->lag);
467 static int dsa_switch_mdb_add(struct dsa_switch *ds,
468 struct dsa_notifier_mdb_info *info)
470 int port = dsa_towards_port(ds, info->sw_index, info->port);
472 if (!ds->ops->port_mdb_add)
475 return dsa_switch_do_mdb_add(ds, port, info->mdb);
478 static int dsa_switch_mdb_del(struct dsa_switch *ds,
479 struct dsa_notifier_mdb_info *info)
481 int port = dsa_towards_port(ds, info->sw_index, info->port);
483 if (!ds->ops->port_mdb_del)
486 return dsa_switch_do_mdb_del(ds, port, info->mdb);
489 static int dsa_switch_host_mdb_add(struct dsa_switch *ds,
490 struct dsa_notifier_mdb_info *info)
495 if (!ds->ops->port_mdb_add)
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_add(ds, port, info->mdb);
510 static int dsa_switch_host_mdb_del(struct dsa_switch *ds,
511 struct dsa_notifier_mdb_info *info)
516 if (!ds->ops->port_mdb_del)
519 for (port = 0; port < ds->num_ports; port++) {
520 if (dsa_switch_host_address_match(ds, port, info->sw_index,
522 err = dsa_switch_do_mdb_del(ds, port, info->mdb);
531 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
532 struct dsa_notifier_vlan_info *info)
534 if (ds->index == info->sw_index && port == info->port)
537 if (dsa_is_dsa_port(ds, port))
543 static int dsa_switch_vlan_add(struct dsa_switch *ds,
544 struct dsa_notifier_vlan_info *info)
548 if (!ds->ops->port_vlan_add)
551 for (port = 0; port < ds->num_ports; port++) {
552 if (dsa_switch_vlan_match(ds, port, info)) {
553 err = ds->ops->port_vlan_add(ds, port, info->vlan,
563 static int dsa_switch_vlan_del(struct dsa_switch *ds,
564 struct dsa_notifier_vlan_info *info)
566 if (!ds->ops->port_vlan_del)
569 if (ds->index == info->sw_index)
570 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
572 /* Do not deprogram the DSA links as they may be used as conduit
573 * for other VLAN members in the fabric.
578 static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
579 struct dsa_notifier_tag_proto_info *info)
581 const struct dsa_device_ops *tag_ops = info->tag_ops;
584 if (!ds->ops->change_tag_protocol)
589 for (port = 0; port < ds->num_ports; port++) {
590 if (!dsa_is_cpu_port(ds, port))
593 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
597 dsa_port_set_tag_protocol(dsa_to_port(ds, port), tag_ops);
600 /* Now that changing the tag protocol can no longer fail, let's update
601 * the remaining bits which are "duplicated for faster access", and the
602 * bits that depend on the tagger, such as the MTU.
604 for (port = 0; port < ds->num_ports; port++) {
605 if (dsa_is_user_port(ds, port)) {
606 struct net_device *slave;
608 slave = dsa_to_port(ds, port)->slave;
609 dsa_slave_setup_tagger(slave);
611 /* rtnl_mutex is held in dsa_tree_change_tag_proto */
612 dsa_slave_change_mtu(slave, slave->mtu);
619 static int dsa_switch_mrp_add(struct dsa_switch *ds,
620 struct dsa_notifier_mrp_info *info)
622 if (!ds->ops->port_mrp_add)
625 if (ds->index == info->sw_index)
626 return ds->ops->port_mrp_add(ds, info->port, info->mrp);
631 static int dsa_switch_mrp_del(struct dsa_switch *ds,
632 struct dsa_notifier_mrp_info *info)
634 if (!ds->ops->port_mrp_del)
637 if (ds->index == info->sw_index)
638 return ds->ops->port_mrp_del(ds, info->port, info->mrp);
644 dsa_switch_mrp_add_ring_role(struct dsa_switch *ds,
645 struct dsa_notifier_mrp_ring_role_info *info)
647 if (!ds->ops->port_mrp_add)
650 if (ds->index == info->sw_index)
651 return ds->ops->port_mrp_add_ring_role(ds, info->port,
658 dsa_switch_mrp_del_ring_role(struct dsa_switch *ds,
659 struct dsa_notifier_mrp_ring_role_info *info)
661 if (!ds->ops->port_mrp_del)
664 if (ds->index == info->sw_index)
665 return ds->ops->port_mrp_del_ring_role(ds, info->port,
671 static int dsa_switch_event(struct notifier_block *nb,
672 unsigned long event, void *info)
674 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
678 case DSA_NOTIFIER_AGEING_TIME:
679 err = dsa_switch_ageing_time(ds, info);
681 case DSA_NOTIFIER_BRIDGE_JOIN:
682 err = dsa_switch_bridge_join(ds, info);
684 case DSA_NOTIFIER_BRIDGE_LEAVE:
685 err = dsa_switch_bridge_leave(ds, info);
687 case DSA_NOTIFIER_FDB_ADD:
688 err = dsa_switch_fdb_add(ds, info);
690 case DSA_NOTIFIER_FDB_DEL:
691 err = dsa_switch_fdb_del(ds, info);
693 case DSA_NOTIFIER_HOST_FDB_ADD:
694 err = dsa_switch_host_fdb_add(ds, info);
696 case DSA_NOTIFIER_HOST_FDB_DEL:
697 err = dsa_switch_host_fdb_del(ds, info);
699 case DSA_NOTIFIER_HSR_JOIN:
700 err = dsa_switch_hsr_join(ds, info);
702 case DSA_NOTIFIER_HSR_LEAVE:
703 err = dsa_switch_hsr_leave(ds, info);
705 case DSA_NOTIFIER_LAG_CHANGE:
706 err = dsa_switch_lag_change(ds, info);
708 case DSA_NOTIFIER_LAG_JOIN:
709 err = dsa_switch_lag_join(ds, info);
711 case DSA_NOTIFIER_LAG_LEAVE:
712 err = dsa_switch_lag_leave(ds, info);
714 case DSA_NOTIFIER_MDB_ADD:
715 err = dsa_switch_mdb_add(ds, info);
717 case DSA_NOTIFIER_MDB_DEL:
718 err = dsa_switch_mdb_del(ds, info);
720 case DSA_NOTIFIER_HOST_MDB_ADD:
721 err = dsa_switch_host_mdb_add(ds, info);
723 case DSA_NOTIFIER_HOST_MDB_DEL:
724 err = dsa_switch_host_mdb_del(ds, info);
726 case DSA_NOTIFIER_VLAN_ADD:
727 err = dsa_switch_vlan_add(ds, info);
729 case DSA_NOTIFIER_VLAN_DEL:
730 err = dsa_switch_vlan_del(ds, info);
732 case DSA_NOTIFIER_MTU:
733 err = dsa_switch_mtu(ds, info);
735 case DSA_NOTIFIER_TAG_PROTO:
736 err = dsa_switch_change_tag_proto(ds, info);
738 case DSA_NOTIFIER_MRP_ADD:
739 err = dsa_switch_mrp_add(ds, info);
741 case DSA_NOTIFIER_MRP_DEL:
742 err = dsa_switch_mrp_del(ds, info);
744 case DSA_NOTIFIER_MRP_ADD_RING_ROLE:
745 err = dsa_switch_mrp_add_ring_role(ds, info);
747 case DSA_NOTIFIER_MRP_DEL_RING_ROLE:
748 err = dsa_switch_mrp_del_ring_role(ds, info);
750 case DSA_NOTIFIER_TAG_8021Q_VLAN_ADD:
751 err = dsa_switch_tag_8021q_vlan_add(ds, info);
753 case DSA_NOTIFIER_TAG_8021Q_VLAN_DEL:
754 err = dsa_switch_tag_8021q_vlan_del(ds, info);
762 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
765 return notifier_from_errno(err);
768 int dsa_switch_register_notifier(struct dsa_switch *ds)
770 ds->nb.notifier_call = dsa_switch_event;
772 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
775 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
779 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
781 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);