2 * Handling of a single switch chip, part of a switch fabric
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
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/netdevice.h>
14 #include <linux/notifier.h>
15 #include <linux/if_vlan.h>
16 #include <net/switchdev.h>
20 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
21 unsigned int ageing_time)
25 for (i = 0; i < ds->num_ports; ++i) {
26 struct dsa_port *dp = &ds->ports[i];
28 if (dp->ageing_time && dp->ageing_time < ageing_time)
29 ageing_time = dp->ageing_time;
35 static int dsa_switch_ageing_time(struct dsa_switch *ds,
36 struct dsa_notifier_ageing_time_info *info)
38 unsigned int ageing_time = info->ageing_time;
39 struct switchdev_trans *trans = info->trans;
41 if (switchdev_trans_ph_prepare(trans)) {
42 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
44 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
49 /* Program the fastest ageing time in case of multiple bridges */
50 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
52 if (ds->ops->set_ageing_time)
53 return ds->ops->set_ageing_time(ds, ageing_time);
58 static int dsa_switch_bridge_join(struct dsa_switch *ds,
59 struct dsa_notifier_bridge_info *info)
61 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
62 return ds->ops->port_bridge_join(ds, info->port, info->br);
64 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
65 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
66 info->port, info->br);
71 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
72 struct dsa_notifier_bridge_info *info)
74 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
75 ds->ops->port_bridge_leave(ds, info->port, info->br);
77 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
78 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
84 static int dsa_switch_fdb_add(struct dsa_switch *ds,
85 struct dsa_notifier_fdb_info *info)
87 int port = dsa_towards_port(ds, info->sw_index, info->port);
89 if (!ds->ops->port_fdb_add)
92 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
95 static int dsa_switch_fdb_del(struct dsa_switch *ds,
96 struct dsa_notifier_fdb_info *info)
98 int port = dsa_towards_port(ds, info->sw_index, info->port);
100 if (!ds->ops->port_fdb_del)
103 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
107 dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds,
108 const struct switchdev_obj_port_mdb *mdb,
109 const unsigned long *bitmap)
113 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
116 for_each_set_bit(port, bitmap, ds->num_ports) {
117 err = ds->ops->port_mdb_prepare(ds, port, mdb);
125 static void dsa_switch_mdb_add_bitmap(struct dsa_switch *ds,
126 const struct switchdev_obj_port_mdb *mdb,
127 const unsigned long *bitmap)
131 for_each_set_bit(port, bitmap, ds->num_ports)
132 ds->ops->port_mdb_add(ds, port, mdb);
135 static int dsa_switch_mdb_add(struct dsa_switch *ds,
136 struct dsa_notifier_mdb_info *info)
138 const struct switchdev_obj_port_mdb *mdb = info->mdb;
139 struct switchdev_trans *trans = info->trans;
142 /* Build a mask of Multicast group members */
143 bitmap_zero(ds->bitmap, ds->num_ports);
144 if (ds->index == info->sw_index)
145 set_bit(info->port, ds->bitmap);
146 for (port = 0; port < ds->num_ports; port++)
147 if (dsa_is_dsa_port(ds, port))
148 set_bit(port, ds->bitmap);
150 if (switchdev_trans_ph_prepare(trans))
151 return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
153 dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
158 static int dsa_switch_mdb_del(struct dsa_switch *ds,
159 struct dsa_notifier_mdb_info *info)
161 const struct switchdev_obj_port_mdb *mdb = info->mdb;
163 if (!ds->ops->port_mdb_del)
166 if (ds->index == info->sw_index)
167 return ds->ops->port_mdb_del(ds, info->port, mdb);
172 static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
176 struct switchdev_obj_port_vlan *vlan = arg;
179 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
180 if (vid == vlan_dev_vid)
187 static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
188 const struct switchdev_obj_port_vlan *vlan)
190 const struct dsa_port *dp = dsa_to_port(ds, port);
193 /* Device is not bridged, let it proceed with the VLAN device
199 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare pharse and
200 * already checks whether there is an overlapping bridge VLAN entry
201 * with the same VID, so here we only need to check that if we are
202 * adding a bridge VLAN entry there is not an overlapping VLAN device
205 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
210 dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
211 const struct switchdev_obj_port_vlan *vlan,
212 const unsigned long *bitmap)
216 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
219 for_each_set_bit(port, bitmap, ds->num_ports) {
220 err = dsa_port_vlan_check(ds, port, vlan);
224 err = ds->ops->port_vlan_prepare(ds, port, vlan);
233 dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
234 const struct switchdev_obj_port_vlan *vlan,
235 const unsigned long *bitmap)
239 for_each_set_bit(port, bitmap, ds->num_ports)
240 ds->ops->port_vlan_add(ds, port, vlan);
243 static int dsa_switch_vlan_add(struct dsa_switch *ds,
244 struct dsa_notifier_vlan_info *info)
246 const struct switchdev_obj_port_vlan *vlan = info->vlan;
247 struct switchdev_trans *trans = info->trans;
250 /* Build a mask of VLAN members */
251 bitmap_zero(ds->bitmap, ds->num_ports);
252 if (ds->index == info->sw_index)
253 set_bit(info->port, ds->bitmap);
254 for (port = 0; port < ds->num_ports; port++)
255 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
256 set_bit(port, ds->bitmap);
258 if (switchdev_trans_ph_prepare(trans))
259 return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
261 dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
266 static int dsa_switch_vlan_del(struct dsa_switch *ds,
267 struct dsa_notifier_vlan_info *info)
269 const struct switchdev_obj_port_vlan *vlan = info->vlan;
271 if (!ds->ops->port_vlan_del)
274 if (ds->index == info->sw_index)
275 return ds->ops->port_vlan_del(ds, info->port, vlan);
280 static int dsa_switch_event(struct notifier_block *nb,
281 unsigned long event, void *info)
283 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
287 case DSA_NOTIFIER_AGEING_TIME:
288 err = dsa_switch_ageing_time(ds, info);
290 case DSA_NOTIFIER_BRIDGE_JOIN:
291 err = dsa_switch_bridge_join(ds, info);
293 case DSA_NOTIFIER_BRIDGE_LEAVE:
294 err = dsa_switch_bridge_leave(ds, info);
296 case DSA_NOTIFIER_FDB_ADD:
297 err = dsa_switch_fdb_add(ds, info);
299 case DSA_NOTIFIER_FDB_DEL:
300 err = dsa_switch_fdb_del(ds, info);
302 case DSA_NOTIFIER_MDB_ADD:
303 err = dsa_switch_mdb_add(ds, info);
305 case DSA_NOTIFIER_MDB_DEL:
306 err = dsa_switch_mdb_del(ds, info);
308 case DSA_NOTIFIER_VLAN_ADD:
309 err = dsa_switch_vlan_add(ds, info);
311 case DSA_NOTIFIER_VLAN_DEL:
312 err = dsa_switch_vlan_del(ds, info);
319 /* Non-switchdev operations cannot be rolled back. If a DSA driver
320 * returns an error during the chained call, switch chips may be in an
321 * inconsistent state.
324 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
327 return notifier_from_errno(err);
330 int dsa_switch_register_notifier(struct dsa_switch *ds)
332 ds->nb.notifier_call = dsa_switch_event;
334 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
337 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
341 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
343 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);