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.
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_join)
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_join)
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 left this bridge.
129 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
130 for (i = 0; i < ds->num_ports; i++) {
133 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
134 unset_vlan_filtering = false;
139 if (unset_vlan_filtering) {
140 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
143 dev_err(ds->dev, "port %d: %s\n", info->port,
145 if (err && err != EOPNOTSUPP)
151 static int dsa_switch_fdb_add(struct dsa_switch *ds,
152 struct dsa_notifier_fdb_info *info)
154 int port = dsa_towards_port(ds, info->sw_index, info->port);
156 if (!ds->ops->port_fdb_add)
159 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
162 static int dsa_switch_fdb_del(struct dsa_switch *ds,
163 struct dsa_notifier_fdb_info *info)
165 int port = dsa_towards_port(ds, info->sw_index, info->port);
167 if (!ds->ops->port_fdb_del)
170 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
173 static int dsa_switch_hsr_join(struct dsa_switch *ds,
174 struct dsa_notifier_hsr_info *info)
176 if (ds->index == info->sw_index && ds->ops->port_hsr_join)
177 return ds->ops->port_hsr_join(ds, info->port, info->hsr);
182 static int dsa_switch_hsr_leave(struct dsa_switch *ds,
183 struct dsa_notifier_hsr_info *info)
185 if (ds->index == info->sw_index && ds->ops->port_hsr_leave)
186 return ds->ops->port_hsr_leave(ds, info->port, info->hsr);
191 static int dsa_switch_lag_change(struct dsa_switch *ds,
192 struct dsa_notifier_lag_info *info)
194 if (ds->index == info->sw_index && ds->ops->port_lag_change)
195 return ds->ops->port_lag_change(ds, info->port);
197 if (ds->index != info->sw_index && ds->ops->crosschip_lag_change)
198 return ds->ops->crosschip_lag_change(ds, info->sw_index,
204 static int dsa_switch_lag_join(struct dsa_switch *ds,
205 struct dsa_notifier_lag_info *info)
207 if (ds->index == info->sw_index && ds->ops->port_lag_join)
208 return ds->ops->port_lag_join(ds, info->port, info->lag,
211 if (ds->index != info->sw_index && ds->ops->crosschip_lag_join)
212 return ds->ops->crosschip_lag_join(ds, info->sw_index,
213 info->port, info->lag,
219 static int dsa_switch_lag_leave(struct dsa_switch *ds,
220 struct dsa_notifier_lag_info *info)
222 if (ds->index == info->sw_index && ds->ops->port_lag_leave)
223 return ds->ops->port_lag_leave(ds, info->port, info->lag);
225 if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave)
226 return ds->ops->crosschip_lag_leave(ds, info->sw_index,
227 info->port, info->lag);
232 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
233 struct dsa_notifier_mdb_info *info)
235 if (ds->index == info->sw_index && port == info->port)
238 if (dsa_is_dsa_port(ds, port))
244 static int dsa_switch_mdb_add(struct dsa_switch *ds,
245 struct dsa_notifier_mdb_info *info)
250 if (!ds->ops->port_mdb_add)
253 for (port = 0; port < ds->num_ports; port++) {
254 if (dsa_switch_mdb_match(ds, port, info)) {
255 err = ds->ops->port_mdb_add(ds, port, info->mdb);
264 static int dsa_switch_mdb_del(struct dsa_switch *ds,
265 struct dsa_notifier_mdb_info *info)
267 if (!ds->ops->port_mdb_del)
270 if (ds->index == info->sw_index)
271 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
276 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
277 struct dsa_notifier_vlan_info *info)
279 if (ds->index == info->sw_index && port == info->port)
282 if (dsa_is_dsa_port(ds, port))
288 static int dsa_switch_vlan_add(struct dsa_switch *ds,
289 struct dsa_notifier_vlan_info *info)
293 if (!ds->ops->port_vlan_add)
296 for (port = 0; port < ds->num_ports; port++) {
297 if (dsa_switch_vlan_match(ds, port, info)) {
298 err = ds->ops->port_vlan_add(ds, port, info->vlan,
308 static int dsa_switch_vlan_del(struct dsa_switch *ds,
309 struct dsa_notifier_vlan_info *info)
311 if (!ds->ops->port_vlan_del)
314 if (ds->index == info->sw_index)
315 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
317 /* Do not deprogram the DSA links as they may be used as conduit
318 * for other VLAN members in the fabric.
323 static bool dsa_switch_tag_proto_match(struct dsa_switch *ds, int port,
324 struct dsa_notifier_tag_proto_info *info)
326 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
332 static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
333 struct dsa_notifier_tag_proto_info *info)
335 const struct dsa_device_ops *tag_ops = info->tag_ops;
338 if (!ds->ops->change_tag_protocol)
343 for (port = 0; port < ds->num_ports; port++) {
344 if (dsa_switch_tag_proto_match(ds, port, info)) {
345 err = ds->ops->change_tag_protocol(ds, port,
350 if (dsa_is_cpu_port(ds, port))
351 dsa_port_set_tag_protocol(dsa_to_port(ds, port),
356 /* Now that changing the tag protocol can no longer fail, let's update
357 * the remaining bits which are "duplicated for faster access", and the
358 * bits that depend on the tagger, such as the MTU.
360 for (port = 0; port < ds->num_ports; port++) {
361 if (dsa_is_user_port(ds, port)) {
362 struct net_device *slave;
364 slave = dsa_to_port(ds, port)->slave;
365 dsa_slave_setup_tagger(slave);
367 /* rtnl_mutex is held in dsa_tree_change_tag_proto */
368 dsa_slave_change_mtu(slave, slave->mtu);
375 static bool dsa_switch_mrp_match(struct dsa_switch *ds, int port,
376 struct dsa_notifier_mrp_info *info)
378 if (ds->index == info->sw_index && port == info->port)
381 if (dsa_is_dsa_port(ds, port))
387 static int dsa_switch_mrp_add(struct dsa_switch *ds,
388 struct dsa_notifier_mrp_info *info)
393 if (!ds->ops->port_mrp_add)
396 for (port = 0; port < ds->num_ports; port++) {
397 if (dsa_switch_mrp_match(ds, port, info)) {
398 err = ds->ops->port_mrp_add(ds, port, info->mrp);
407 static int dsa_switch_mrp_del(struct dsa_switch *ds,
408 struct dsa_notifier_mrp_info *info)
410 if (!ds->ops->port_mrp_del)
413 if (ds->index == info->sw_index)
414 return ds->ops->port_mrp_del(ds, info->port, info->mrp);
420 dsa_switch_mrp_ring_role_match(struct dsa_switch *ds, int port,
421 struct dsa_notifier_mrp_ring_role_info *info)
423 if (ds->index == info->sw_index && port == info->port)
426 if (dsa_is_dsa_port(ds, port))
433 dsa_switch_mrp_add_ring_role(struct dsa_switch *ds,
434 struct dsa_notifier_mrp_ring_role_info *info)
439 if (!ds->ops->port_mrp_add)
442 for (port = 0; port < ds->num_ports; port++) {
443 if (dsa_switch_mrp_ring_role_match(ds, port, info)) {
444 err = ds->ops->port_mrp_add_ring_role(ds, port,
455 dsa_switch_mrp_del_ring_role(struct dsa_switch *ds,
456 struct dsa_notifier_mrp_ring_role_info *info)
458 if (!ds->ops->port_mrp_del)
461 if (ds->index == info->sw_index)
462 return ds->ops->port_mrp_del_ring_role(ds, info->port,
468 static int dsa_switch_event(struct notifier_block *nb,
469 unsigned long event, void *info)
471 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
475 case DSA_NOTIFIER_AGEING_TIME:
476 err = dsa_switch_ageing_time(ds, info);
478 case DSA_NOTIFIER_BRIDGE_JOIN:
479 err = dsa_switch_bridge_join(ds, info);
481 case DSA_NOTIFIER_BRIDGE_LEAVE:
482 err = dsa_switch_bridge_leave(ds, info);
484 case DSA_NOTIFIER_FDB_ADD:
485 err = dsa_switch_fdb_add(ds, info);
487 case DSA_NOTIFIER_FDB_DEL:
488 err = dsa_switch_fdb_del(ds, info);
490 case DSA_NOTIFIER_HSR_JOIN:
491 err = dsa_switch_hsr_join(ds, info);
493 case DSA_NOTIFIER_HSR_LEAVE:
494 err = dsa_switch_hsr_leave(ds, info);
496 case DSA_NOTIFIER_LAG_CHANGE:
497 err = dsa_switch_lag_change(ds, info);
499 case DSA_NOTIFIER_LAG_JOIN:
500 err = dsa_switch_lag_join(ds, info);
502 case DSA_NOTIFIER_LAG_LEAVE:
503 err = dsa_switch_lag_leave(ds, info);
505 case DSA_NOTIFIER_MDB_ADD:
506 err = dsa_switch_mdb_add(ds, info);
508 case DSA_NOTIFIER_MDB_DEL:
509 err = dsa_switch_mdb_del(ds, info);
511 case DSA_NOTIFIER_VLAN_ADD:
512 err = dsa_switch_vlan_add(ds, info);
514 case DSA_NOTIFIER_VLAN_DEL:
515 err = dsa_switch_vlan_del(ds, info);
517 case DSA_NOTIFIER_MTU:
518 err = dsa_switch_mtu(ds, info);
520 case DSA_NOTIFIER_TAG_PROTO:
521 err = dsa_switch_change_tag_proto(ds, info);
523 case DSA_NOTIFIER_MRP_ADD:
524 err = dsa_switch_mrp_add(ds, info);
526 case DSA_NOTIFIER_MRP_DEL:
527 err = dsa_switch_mrp_del(ds, info);
529 case DSA_NOTIFIER_MRP_ADD_RING_ROLE:
530 err = dsa_switch_mrp_add_ring_role(ds, info);
532 case DSA_NOTIFIER_MRP_DEL_RING_ROLE:
533 err = dsa_switch_mrp_del_ring_role(ds, info);
541 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
544 return notifier_from_errno(err);
547 int dsa_switch_register_notifier(struct dsa_switch *ds)
549 ds->nb.notifier_call = dsa_switch_event;
551 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
554 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
558 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
560 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);