2 * Handling of a single switch chip, part of a switch fabric
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/if_bridge.h>
14 #include <linux/netdevice.h>
15 #include <linux/notifier.h>
16 #include <linux/if_vlan.h>
17 #include <net/switchdev.h>
21 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
22 unsigned int ageing_time)
26 for (i = 0; i < ds->num_ports; ++i) {
27 struct dsa_port *dp = &ds->ports[i];
29 if (dp->ageing_time && dp->ageing_time < ageing_time)
30 ageing_time = dp->ageing_time;
36 static int dsa_switch_ageing_time(struct dsa_switch *ds,
37 struct dsa_notifier_ageing_time_info *info)
39 unsigned int ageing_time = info->ageing_time;
40 struct switchdev_trans *trans = info->trans;
42 if (switchdev_trans_ph_prepare(trans)) {
43 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
45 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
50 /* Program the fastest ageing time in case of multiple bridges */
51 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
53 if (ds->ops->set_ageing_time)
54 return ds->ops->set_ageing_time(ds, ageing_time);
59 static int dsa_switch_bridge_join(struct dsa_switch *ds,
60 struct dsa_notifier_bridge_info *info)
62 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
63 return ds->ops->port_bridge_join(ds, info->port, info->br);
65 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
66 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
67 info->port, info->br);
72 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
73 struct dsa_notifier_bridge_info *info)
75 bool unset_vlan_filtering = br_vlan_enabled(info->br);
78 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
79 ds->ops->port_bridge_leave(ds, info->port, info->br);
81 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
82 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
85 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
86 * event for changing vlan_filtering setting upon slave ports leaving
87 * it. That is a good thing, because that lets us handle it and also
88 * handle the case where the switch's vlan_filtering setting is global
89 * (not per port). When that happens, the correct moment to trigger the
90 * vlan_filtering callback is only when the last port left this bridge.
92 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
93 for (i = 0; i < ds->num_ports; i++) {
96 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
97 unset_vlan_filtering = false;
102 if (unset_vlan_filtering) {
103 struct switchdev_trans trans = {0};
105 err = dsa_port_vlan_filtering(&ds->ports[info->port],
107 if (err && err != EOPNOTSUPP)
113 static int dsa_switch_fdb_add(struct dsa_switch *ds,
114 struct dsa_notifier_fdb_info *info)
116 int port = dsa_towards_port(ds, info->sw_index, info->port);
118 if (!ds->ops->port_fdb_add)
121 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
124 static int dsa_switch_fdb_del(struct dsa_switch *ds,
125 struct dsa_notifier_fdb_info *info)
127 int port = dsa_towards_port(ds, info->sw_index, info->port);
129 if (!ds->ops->port_fdb_del)
132 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
136 dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds,
137 const struct switchdev_obj_port_mdb *mdb,
138 const unsigned long *bitmap)
142 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
145 for_each_set_bit(port, bitmap, ds->num_ports) {
146 err = ds->ops->port_mdb_prepare(ds, port, mdb);
154 static void dsa_switch_mdb_add_bitmap(struct dsa_switch *ds,
155 const struct switchdev_obj_port_mdb *mdb,
156 const unsigned long *bitmap)
160 for_each_set_bit(port, bitmap, ds->num_ports)
161 ds->ops->port_mdb_add(ds, port, mdb);
164 static int dsa_switch_mdb_add(struct dsa_switch *ds,
165 struct dsa_notifier_mdb_info *info)
167 const struct switchdev_obj_port_mdb *mdb = info->mdb;
168 struct switchdev_trans *trans = info->trans;
171 /* Build a mask of Multicast group members */
172 bitmap_zero(ds->bitmap, ds->num_ports);
173 if (ds->index == info->sw_index)
174 set_bit(info->port, ds->bitmap);
175 for (port = 0; port < ds->num_ports; port++)
176 if (dsa_is_dsa_port(ds, port))
177 set_bit(port, ds->bitmap);
179 if (switchdev_trans_ph_prepare(trans))
180 return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
182 dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
187 static int dsa_switch_mdb_del(struct dsa_switch *ds,
188 struct dsa_notifier_mdb_info *info)
190 const struct switchdev_obj_port_mdb *mdb = info->mdb;
192 if (!ds->ops->port_mdb_del)
195 if (ds->index == info->sw_index)
196 return ds->ops->port_mdb_del(ds, info->port, mdb);
201 static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
205 struct switchdev_obj_port_vlan *vlan = arg;
208 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
209 if (vid == vlan_dev_vid)
216 static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
217 const struct switchdev_obj_port_vlan *vlan)
219 const struct dsa_port *dp = dsa_to_port(ds, port);
222 /* Device is not bridged, let it proceed with the VLAN device
228 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
229 * already checks whether there is an overlapping bridge VLAN entry
230 * with the same VID, so here we only need to check that if we are
231 * adding a bridge VLAN entry there is not an overlapping VLAN device
234 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
239 dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
240 const struct switchdev_obj_port_vlan *vlan,
241 const unsigned long *bitmap)
245 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
248 for_each_set_bit(port, bitmap, ds->num_ports) {
249 err = dsa_port_vlan_check(ds, port, vlan);
253 err = ds->ops->port_vlan_prepare(ds, port, vlan);
262 dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
263 const struct switchdev_obj_port_vlan *vlan,
264 const unsigned long *bitmap)
268 for_each_set_bit(port, bitmap, ds->num_ports)
269 ds->ops->port_vlan_add(ds, port, vlan);
272 static int dsa_switch_vlan_add(struct dsa_switch *ds,
273 struct dsa_notifier_vlan_info *info)
275 const struct switchdev_obj_port_vlan *vlan = info->vlan;
276 struct switchdev_trans *trans = info->trans;
279 /* Build a mask of VLAN members */
280 bitmap_zero(ds->bitmap, ds->num_ports);
281 if (ds->index == info->sw_index)
282 set_bit(info->port, ds->bitmap);
283 for (port = 0; port < ds->num_ports; port++)
284 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
285 set_bit(port, ds->bitmap);
287 if (switchdev_trans_ph_prepare(trans))
288 return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
290 dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
295 static int dsa_switch_vlan_del(struct dsa_switch *ds,
296 struct dsa_notifier_vlan_info *info)
298 const struct switchdev_obj_port_vlan *vlan = info->vlan;
300 if (!ds->ops->port_vlan_del)
303 if (ds->index == info->sw_index)
304 return ds->ops->port_vlan_del(ds, info->port, vlan);
309 static int dsa_switch_event(struct notifier_block *nb,
310 unsigned long event, void *info)
312 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
316 case DSA_NOTIFIER_AGEING_TIME:
317 err = dsa_switch_ageing_time(ds, info);
319 case DSA_NOTIFIER_BRIDGE_JOIN:
320 err = dsa_switch_bridge_join(ds, info);
322 case DSA_NOTIFIER_BRIDGE_LEAVE:
323 err = dsa_switch_bridge_leave(ds, info);
325 case DSA_NOTIFIER_FDB_ADD:
326 err = dsa_switch_fdb_add(ds, info);
328 case DSA_NOTIFIER_FDB_DEL:
329 err = dsa_switch_fdb_del(ds, info);
331 case DSA_NOTIFIER_MDB_ADD:
332 err = dsa_switch_mdb_add(ds, info);
334 case DSA_NOTIFIER_MDB_DEL:
335 err = dsa_switch_mdb_del(ds, info);
337 case DSA_NOTIFIER_VLAN_ADD:
338 err = dsa_switch_vlan_add(ds, info);
340 case DSA_NOTIFIER_VLAN_DEL:
341 err = dsa_switch_vlan_del(ds, info);
348 /* Non-switchdev operations cannot be rolled back. If a DSA driver
349 * returns an error during the chained call, switch chips may be in an
350 * inconsistent state.
353 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
356 return notifier_from_errno(err);
359 int dsa_switch_register_notifier(struct dsa_switch *ds)
361 ds->nb.notifier_call = dsa_switch_event;
363 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
366 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
370 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
372 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);