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)
56 return (port == info->port) || dsa_is_dsa_port(ds, port);
58 if (!info->propagate_upstream)
61 if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
67 static int dsa_switch_mtu(struct dsa_switch *ds,
68 struct dsa_notifier_mtu_info *info)
72 if (!ds->ops->port_change_mtu)
75 for (port = 0; port < ds->num_ports; port++) {
76 if (dsa_switch_mtu_match(ds, port, info)) {
77 ret = ds->ops->port_change_mtu(ds, port, info->mtu);
86 static int dsa_switch_bridge_join(struct dsa_switch *ds,
87 struct dsa_notifier_bridge_info *info)
89 struct dsa_switch_tree *dst = ds->dst;
91 if (dst->index == info->tree_index && ds->index == info->sw_index &&
92 ds->ops->port_bridge_join)
93 return ds->ops->port_bridge_join(ds, info->port, info->br);
95 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
96 ds->ops->crosschip_bridge_join)
97 return ds->ops->crosschip_bridge_join(ds, info->tree_index,
99 info->port, info->br);
104 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
105 struct dsa_notifier_bridge_info *info)
107 bool unset_vlan_filtering = br_vlan_enabled(info->br);
108 struct dsa_switch_tree *dst = ds->dst;
109 struct netlink_ext_ack extack = {0};
112 if (dst->index == info->tree_index && ds->index == info->sw_index &&
113 ds->ops->port_bridge_leave)
114 ds->ops->port_bridge_leave(ds, info->port, info->br);
116 if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
117 ds->ops->crosschip_bridge_leave)
118 ds->ops->crosschip_bridge_leave(ds, info->tree_index,
119 info->sw_index, info->port,
122 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
123 * event for changing vlan_filtering setting upon slave ports leaving
124 * it. That is a good thing, because that lets us handle it and also
125 * handle the case where the switch's vlan_filtering setting is global
126 * (not per port). When that happens, the correct moment to trigger the
127 * vlan_filtering callback is only when the last port leaves the last
130 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
131 for (port = 0; port < ds->num_ports; port++) {
132 struct net_device *bridge_dev;
134 bridge_dev = dsa_to_port(ds, port)->bridge_dev;
136 if (bridge_dev && br_vlan_enabled(bridge_dev)) {
137 unset_vlan_filtering = false;
142 if (unset_vlan_filtering) {
143 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
146 dev_err(ds->dev, "port %d: %s\n", info->port,
148 if (err && err != EOPNOTSUPP)
154 static int dsa_switch_fdb_add(struct dsa_switch *ds,
155 struct dsa_notifier_fdb_info *info)
157 int port = dsa_towards_port(ds, info->sw_index, info->port);
159 if (!ds->ops->port_fdb_add)
162 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
165 static int dsa_switch_fdb_del(struct dsa_switch *ds,
166 struct dsa_notifier_fdb_info *info)
168 int port = dsa_towards_port(ds, info->sw_index, info->port);
170 if (!ds->ops->port_fdb_del)
173 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
176 static int dsa_switch_hsr_join(struct dsa_switch *ds,
177 struct dsa_notifier_hsr_info *info)
179 if (ds->index == info->sw_index && ds->ops->port_hsr_join)
180 return ds->ops->port_hsr_join(ds, info->port, info->hsr);
185 static int dsa_switch_hsr_leave(struct dsa_switch *ds,
186 struct dsa_notifier_hsr_info *info)
188 if (ds->index == info->sw_index && ds->ops->port_hsr_leave)
189 return ds->ops->port_hsr_leave(ds, info->port, info->hsr);
194 static int dsa_switch_lag_change(struct dsa_switch *ds,
195 struct dsa_notifier_lag_info *info)
197 if (ds->index == info->sw_index && ds->ops->port_lag_change)
198 return ds->ops->port_lag_change(ds, info->port);
200 if (ds->index != info->sw_index && ds->ops->crosschip_lag_change)
201 return ds->ops->crosschip_lag_change(ds, info->sw_index,
207 static int dsa_switch_lag_join(struct dsa_switch *ds,
208 struct dsa_notifier_lag_info *info)
210 if (ds->index == info->sw_index && ds->ops->port_lag_join)
211 return ds->ops->port_lag_join(ds, info->port, info->lag,
214 if (ds->index != info->sw_index && ds->ops->crosschip_lag_join)
215 return ds->ops->crosschip_lag_join(ds, info->sw_index,
216 info->port, info->lag,
222 static int dsa_switch_lag_leave(struct dsa_switch *ds,
223 struct dsa_notifier_lag_info *info)
225 if (ds->index == info->sw_index && ds->ops->port_lag_leave)
226 return ds->ops->port_lag_leave(ds, info->port, info->lag);
228 if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave)
229 return ds->ops->crosschip_lag_leave(ds, info->sw_index,
230 info->port, info->lag);
235 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
236 struct dsa_notifier_mdb_info *info)
238 if (ds->index == info->sw_index && port == info->port)
241 if (dsa_is_dsa_port(ds, port))
247 static int dsa_switch_mdb_add(struct dsa_switch *ds,
248 struct dsa_notifier_mdb_info *info)
253 if (!ds->ops->port_mdb_add)
256 for (port = 0; port < ds->num_ports; port++) {
257 if (dsa_switch_mdb_match(ds, port, info)) {
258 err = ds->ops->port_mdb_add(ds, port, info->mdb);
267 static int dsa_switch_mdb_del(struct dsa_switch *ds,
268 struct dsa_notifier_mdb_info *info)
270 if (!ds->ops->port_mdb_del)
273 if (ds->index == info->sw_index)
274 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
279 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
280 struct dsa_notifier_vlan_info *info)
282 if (ds->index == info->sw_index && port == info->port)
285 if (dsa_is_dsa_port(ds, port))
291 static int dsa_switch_vlan_add(struct dsa_switch *ds,
292 struct dsa_notifier_vlan_info *info)
296 if (!ds->ops->port_vlan_add)
299 for (port = 0; port < ds->num_ports; port++) {
300 if (dsa_switch_vlan_match(ds, port, info)) {
301 err = ds->ops->port_vlan_add(ds, port, info->vlan,
311 static int dsa_switch_vlan_del(struct dsa_switch *ds,
312 struct dsa_notifier_vlan_info *info)
314 if (!ds->ops->port_vlan_del)
317 if (ds->index == info->sw_index)
318 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
320 /* Do not deprogram the DSA links as they may be used as conduit
321 * for other VLAN members in the fabric.
326 static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
327 struct dsa_notifier_tag_proto_info *info)
329 const struct dsa_device_ops *tag_ops = info->tag_ops;
332 if (!ds->ops->change_tag_protocol)
337 for (port = 0; port < ds->num_ports; port++) {
338 if (!dsa_is_cpu_port(ds, port))
341 err = ds->ops->change_tag_protocol(ds, port, tag_ops->proto);
345 dsa_port_set_tag_protocol(dsa_to_port(ds, port), tag_ops);
348 /* Now that changing the tag protocol can no longer fail, let's update
349 * the remaining bits which are "duplicated for faster access", and the
350 * bits that depend on the tagger, such as the MTU.
352 for (port = 0; port < ds->num_ports; port++) {
353 if (dsa_is_user_port(ds, port)) {
354 struct net_device *slave;
356 slave = dsa_to_port(ds, port)->slave;
357 dsa_slave_setup_tagger(slave);
359 /* rtnl_mutex is held in dsa_tree_change_tag_proto */
360 dsa_slave_change_mtu(slave, slave->mtu);
367 static bool dsa_switch_mrp_match(struct dsa_switch *ds, int port,
368 struct dsa_notifier_mrp_info *info)
370 if (ds->index == info->sw_index && port == info->port)
373 if (dsa_is_dsa_port(ds, port))
379 static int dsa_switch_mrp_add(struct dsa_switch *ds,
380 struct dsa_notifier_mrp_info *info)
385 if (!ds->ops->port_mrp_add)
388 for (port = 0; port < ds->num_ports; port++) {
389 if (dsa_switch_mrp_match(ds, port, info)) {
390 err = ds->ops->port_mrp_add(ds, port, info->mrp);
399 static int dsa_switch_mrp_del(struct dsa_switch *ds,
400 struct dsa_notifier_mrp_info *info)
402 if (!ds->ops->port_mrp_del)
405 if (ds->index == info->sw_index)
406 return ds->ops->port_mrp_del(ds, info->port, info->mrp);
412 dsa_switch_mrp_ring_role_match(struct dsa_switch *ds, int port,
413 struct dsa_notifier_mrp_ring_role_info *info)
415 if (ds->index == info->sw_index && port == info->port)
418 if (dsa_is_dsa_port(ds, port))
425 dsa_switch_mrp_add_ring_role(struct dsa_switch *ds,
426 struct dsa_notifier_mrp_ring_role_info *info)
431 if (!ds->ops->port_mrp_add)
434 for (port = 0; port < ds->num_ports; port++) {
435 if (dsa_switch_mrp_ring_role_match(ds, port, info)) {
436 err = ds->ops->port_mrp_add_ring_role(ds, port,
447 dsa_switch_mrp_del_ring_role(struct dsa_switch *ds,
448 struct dsa_notifier_mrp_ring_role_info *info)
450 if (!ds->ops->port_mrp_del)
453 if (ds->index == info->sw_index)
454 return ds->ops->port_mrp_del_ring_role(ds, info->port,
460 static int dsa_switch_event(struct notifier_block *nb,
461 unsigned long event, void *info)
463 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
467 case DSA_NOTIFIER_AGEING_TIME:
468 err = dsa_switch_ageing_time(ds, info);
470 case DSA_NOTIFIER_BRIDGE_JOIN:
471 err = dsa_switch_bridge_join(ds, info);
473 case DSA_NOTIFIER_BRIDGE_LEAVE:
474 err = dsa_switch_bridge_leave(ds, info);
476 case DSA_NOTIFIER_FDB_ADD:
477 err = dsa_switch_fdb_add(ds, info);
479 case DSA_NOTIFIER_FDB_DEL:
480 err = dsa_switch_fdb_del(ds, info);
482 case DSA_NOTIFIER_HSR_JOIN:
483 err = dsa_switch_hsr_join(ds, info);
485 case DSA_NOTIFIER_HSR_LEAVE:
486 err = dsa_switch_hsr_leave(ds, info);
488 case DSA_NOTIFIER_LAG_CHANGE:
489 err = dsa_switch_lag_change(ds, info);
491 case DSA_NOTIFIER_LAG_JOIN:
492 err = dsa_switch_lag_join(ds, info);
494 case DSA_NOTIFIER_LAG_LEAVE:
495 err = dsa_switch_lag_leave(ds, info);
497 case DSA_NOTIFIER_MDB_ADD:
498 err = dsa_switch_mdb_add(ds, info);
500 case DSA_NOTIFIER_MDB_DEL:
501 err = dsa_switch_mdb_del(ds, info);
503 case DSA_NOTIFIER_VLAN_ADD:
504 err = dsa_switch_vlan_add(ds, info);
506 case DSA_NOTIFIER_VLAN_DEL:
507 err = dsa_switch_vlan_del(ds, info);
509 case DSA_NOTIFIER_MTU:
510 err = dsa_switch_mtu(ds, info);
512 case DSA_NOTIFIER_TAG_PROTO:
513 err = dsa_switch_change_tag_proto(ds, info);
515 case DSA_NOTIFIER_MRP_ADD:
516 err = dsa_switch_mrp_add(ds, info);
518 case DSA_NOTIFIER_MRP_DEL:
519 err = dsa_switch_mrp_del(ds, info);
521 case DSA_NOTIFIER_MRP_ADD_RING_ROLE:
522 err = dsa_switch_mrp_add_ring_role(ds, info);
524 case DSA_NOTIFIER_MRP_DEL_RING_ROLE:
525 err = dsa_switch_mrp_del_ring_role(ds, info);
533 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
536 return notifier_from_errno(err);
539 int dsa_switch_register_notifier(struct dsa_switch *ds)
541 ds->nb.notifier_call = dsa_switch_event;
543 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
546 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
550 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
552 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);