blob: 4ec5b7f85d51ea2274b69e0ae68fa824fac2edde [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vivien Didelotf515f192017-02-03 13:20:20 -05002/*
3 * Handling of a single switch chip, part of a switch fabric
4 *
Vivien Didelot4333d612017-03-28 15:10:36 -04005 * Copyright (c) 2017 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelotf515f192017-02-03 13:20:20 -05007 */
8
Vladimir Olteand371b7c2019-04-28 21:45:46 +03009#include <linux/if_bridge.h>
Vivien Didelotf515f192017-02-03 13:20:20 -050010#include <linux/netdevice.h>
11#include <linux/notifier.h>
Florian Fainelli061f6a52019-02-20 14:35:39 -080012#include <linux/if_vlan.h>
Vivien Didelot1faabf72017-05-19 17:00:52 -040013#include <net/switchdev.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040014
15#include "dsa_priv.h"
Vivien Didelotf515f192017-02-03 13:20:20 -050016
Vivien Didelot1faabf72017-05-19 17:00:52 -040017static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
18 unsigned int ageing_time)
19{
20 int i;
21
22 for (i = 0; i < ds->num_ports; ++i) {
23 struct dsa_port *dp = &ds->ports[i];
24
25 if (dp->ageing_time && dp->ageing_time < ageing_time)
26 ageing_time = dp->ageing_time;
27 }
28
29 return ageing_time;
30}
31
32static int dsa_switch_ageing_time(struct dsa_switch *ds,
33 struct dsa_notifier_ageing_time_info *info)
34{
35 unsigned int ageing_time = info->ageing_time;
36 struct switchdev_trans *trans = info->trans;
37
Vivien Didelot1faabf72017-05-19 17:00:52 -040038 if (switchdev_trans_ph_prepare(trans)) {
39 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
40 return -ERANGE;
41 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
42 return -ERANGE;
43 return 0;
44 }
45
46 /* Program the fastest ageing time in case of multiple bridges */
47 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
48
49 if (ds->ops->set_ageing_time)
50 return ds->ops->set_ageing_time(ds, ageing_time);
51
52 return 0;
53}
54
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050055static int dsa_switch_bridge_join(struct dsa_switch *ds,
56 struct dsa_notifier_bridge_info *info)
57{
58 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
59 return ds->ops->port_bridge_join(ds, info->port, info->br);
60
Vivien Didelot40ef2c92017-03-30 17:37:14 -040061 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
62 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
63 info->port, info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050064
65 return 0;
66}
67
68static int dsa_switch_bridge_leave(struct dsa_switch *ds,
69 struct dsa_notifier_bridge_info *info)
70{
Vladimir Olteand371b7c2019-04-28 21:45:46 +030071 bool unset_vlan_filtering = br_vlan_enabled(info->br);
72 int err, i;
73
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050074 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
75 ds->ops->port_bridge_leave(ds, info->port, info->br);
76
Vivien Didelot40ef2c92017-03-30 17:37:14 -040077 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
78 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
79 info->br);
Vivien Didelot04d3a4c2017-02-03 13:20:21 -050080
Vladimir Olteand371b7c2019-04-28 21:45:46 +030081 /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
82 * event for changing vlan_filtering setting upon slave ports leaving
83 * it. That is a good thing, because that lets us handle it and also
84 * handle the case where the switch's vlan_filtering setting is global
85 * (not per port). When that happens, the correct moment to trigger the
86 * vlan_filtering callback is only when the last port left this bridge.
87 */
88 if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
89 for (i = 0; i < ds->num_ports; i++) {
90 if (i == info->port)
91 continue;
92 if (dsa_to_port(ds, i)->bridge_dev == info->br) {
93 unset_vlan_filtering = false;
94 break;
95 }
96 }
97 }
98 if (unset_vlan_filtering) {
99 struct switchdev_trans trans = {0};
100
101 err = dsa_port_vlan_filtering(&ds->ports[info->port],
102 false, &trans);
103 if (err && err != EOPNOTSUPP)
104 return err;
105 }
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500106 return 0;
107}
108
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400109static int dsa_switch_fdb_add(struct dsa_switch *ds,
110 struct dsa_notifier_fdb_info *info)
111{
Vivien Didelot31692412017-11-30 12:56:43 -0500112 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400113
Arkadi Sharshevsky1b6dd552017-08-06 16:15:40 +0300114 if (!ds->ops->port_fdb_add)
115 return -EOPNOTSUPP;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400116
Vivien Didelot31692412017-11-30 12:56:43 -0500117 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400118}
119
120static int dsa_switch_fdb_del(struct dsa_switch *ds,
121 struct dsa_notifier_fdb_info *info)
122{
Vivien Didelot31692412017-11-30 12:56:43 -0500123 int port = dsa_towards_port(ds, info->sw_index, info->port);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400124
125 if (!ds->ops->port_fdb_del)
126 return -EOPNOTSUPP;
127
Vivien Didelot31692412017-11-30 12:56:43 -0500128 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400129}
130
Vivien Didelote6db98d2017-11-30 11:24:00 -0500131static int
132dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds,
133 const struct switchdev_obj_port_mdb *mdb,
134 const unsigned long *bitmap)
135{
136 int port, err;
137
138 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
139 return -EOPNOTSUPP;
140
141 for_each_set_bit(port, bitmap, ds->num_ports) {
142 err = ds->ops->port_mdb_prepare(ds, port, mdb);
143 if (err)
144 return err;
145 }
146
147 return 0;
148}
149
150static void dsa_switch_mdb_add_bitmap(struct dsa_switch *ds,
151 const struct switchdev_obj_port_mdb *mdb,
152 const unsigned long *bitmap)
153{
154 int port;
155
156 for_each_set_bit(port, bitmap, ds->num_ports)
157 ds->ops->port_mdb_add(ds, port, mdb);
158}
159
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400160static int dsa_switch_mdb_add(struct dsa_switch *ds,
161 struct dsa_notifier_mdb_info *info)
162{
163 const struct switchdev_obj_port_mdb *mdb = info->mdb;
164 struct switchdev_trans *trans = info->trans;
Vivien Didelote6db98d2017-11-30 11:24:00 -0500165 int port;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400166
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400167 /* Build a mask of Multicast group members */
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700168 bitmap_zero(ds->bitmap, ds->num_ports);
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400169 if (ds->index == info->sw_index)
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700170 set_bit(info->port, ds->bitmap);
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400171 for (port = 0; port < ds->num_ports; port++)
Andrew Lunnae451022017-11-09 23:11:02 +0100172 if (dsa_is_dsa_port(ds, port))
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700173 set_bit(port, ds->bitmap);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400174
Vivien Didelote6db98d2017-11-30 11:24:00 -0500175 if (switchdev_trans_ph_prepare(trans))
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700176 return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400177
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700178 dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400179
180 return 0;
181}
182
183static int dsa_switch_mdb_del(struct dsa_switch *ds,
184 struct dsa_notifier_mdb_info *info)
185{
186 const struct switchdev_obj_port_mdb *mdb = info->mdb;
187
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400188 if (!ds->ops->port_mdb_del)
189 return -EOPNOTSUPP;
190
Vivien Didelota1a6b7e2017-06-15 16:14:48 -0400191 if (ds->index == info->sw_index)
192 return ds->ops->port_mdb_del(ds, info->port, mdb);
193
194 return 0;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400195}
196
Florian Fainelli061f6a52019-02-20 14:35:39 -0800197static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
198 int vlan_dev_vid,
199 void *arg)
200{
201 struct switchdev_obj_port_vlan *vlan = arg;
202 u16 vid;
203
204 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
205 if (vid == vlan_dev_vid)
206 return -EBUSY;
207 }
208
209 return 0;
210}
211
212static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
213 const struct switchdev_obj_port_vlan *vlan)
214{
215 const struct dsa_port *dp = dsa_to_port(ds, port);
216 int err = 0;
217
218 /* Device is not bridged, let it proceed with the VLAN device
219 * creation.
220 */
221 if (!dp->bridge_dev)
222 return err;
223
Vladimir Oltean85478d72019-04-28 21:45:42 +0300224 /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
Florian Fainelli061f6a52019-02-20 14:35:39 -0800225 * already checks whether there is an overlapping bridge VLAN entry
226 * with the same VID, so here we only need to check that if we are
227 * adding a bridge VLAN entry there is not an overlapping VLAN device
228 * claiming that VID.
229 */
230 return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
231 (void *)vlan);
232}
233
Vivien Didelot9c428c52017-11-30 11:23:59 -0500234static int
235dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
236 const struct switchdev_obj_port_vlan *vlan,
237 const unsigned long *bitmap)
238{
239 int port, err;
240
241 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
242 return -EOPNOTSUPP;
243
244 for_each_set_bit(port, bitmap, ds->num_ports) {
Florian Fainelli061f6a52019-02-20 14:35:39 -0800245 err = dsa_port_vlan_check(ds, port, vlan);
246 if (err)
247 return err;
248
Vivien Didelot9c428c52017-11-30 11:23:59 -0500249 err = ds->ops->port_vlan_prepare(ds, port, vlan);
250 if (err)
251 return err;
252 }
253
254 return 0;
255}
256
257static void
258dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
259 const struct switchdev_obj_port_vlan *vlan,
260 const unsigned long *bitmap)
261{
262 int port;
263
264 for_each_set_bit(port, bitmap, ds->num_ports)
265 ds->ops->port_vlan_add(ds, port, vlan);
266}
267
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400268static int dsa_switch_vlan_add(struct dsa_switch *ds,
269 struct dsa_notifier_vlan_info *info)
270{
271 const struct switchdev_obj_port_vlan *vlan = info->vlan;
272 struct switchdev_trans *trans = info->trans;
Vivien Didelot9c428c52017-11-30 11:23:59 -0500273 int port;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400274
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400275 /* Build a mask of VLAN members */
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700276 bitmap_zero(ds->bitmap, ds->num_ports);
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400277 if (ds->index == info->sw_index)
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700278 set_bit(info->port, ds->bitmap);
Vivien Didelotb2f81d32017-06-07 18:12:15 -0400279 for (port = 0; port < ds->num_ports; port++)
280 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700281 set_bit(port, ds->bitmap);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400282
Vivien Didelot9c428c52017-11-30 11:23:59 -0500283 if (switchdev_trans_ph_prepare(trans))
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700284 return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400285
Salvatore Mesoraca0015b802018-07-16 21:10:34 -0700286 dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400287
288 return 0;
289}
290
291static int dsa_switch_vlan_del(struct dsa_switch *ds,
292 struct dsa_notifier_vlan_info *info)
293{
294 const struct switchdev_obj_port_vlan *vlan = info->vlan;
295
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400296 if (!ds->ops->port_vlan_del)
297 return -EOPNOTSUPP;
298
Vivien Didelot1ca4aa92017-06-07 18:12:14 -0400299 if (ds->index == info->sw_index)
300 return ds->ops->port_vlan_del(ds, info->port, vlan);
301
302 return 0;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400303}
304
Vivien Didelotf515f192017-02-03 13:20:20 -0500305static int dsa_switch_event(struct notifier_block *nb,
306 unsigned long event, void *info)
307{
308 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
309 int err;
310
311 switch (event) {
Vivien Didelot1faabf72017-05-19 17:00:52 -0400312 case DSA_NOTIFIER_AGEING_TIME:
313 err = dsa_switch_ageing_time(ds, info);
314 break;
Vivien Didelot04d3a4c2017-02-03 13:20:21 -0500315 case DSA_NOTIFIER_BRIDGE_JOIN:
316 err = dsa_switch_bridge_join(ds, info);
317 break;
318 case DSA_NOTIFIER_BRIDGE_LEAVE:
319 err = dsa_switch_bridge_leave(ds, info);
320 break;
Vivien Didelot685fb6a2017-05-19 17:00:53 -0400321 case DSA_NOTIFIER_FDB_ADD:
322 err = dsa_switch_fdb_add(ds, info);
323 break;
324 case DSA_NOTIFIER_FDB_DEL:
325 err = dsa_switch_fdb_del(ds, info);
326 break;
Vivien Didelot8ae5bcd2017-05-19 17:00:54 -0400327 case DSA_NOTIFIER_MDB_ADD:
328 err = dsa_switch_mdb_add(ds, info);
329 break;
330 case DSA_NOTIFIER_MDB_DEL:
331 err = dsa_switch_mdb_del(ds, info);
332 break;
Vivien Didelotd0c627b2017-05-19 17:00:55 -0400333 case DSA_NOTIFIER_VLAN_ADD:
334 err = dsa_switch_vlan_add(ds, info);
335 break;
336 case DSA_NOTIFIER_VLAN_DEL:
337 err = dsa_switch_vlan_del(ds, info);
338 break;
Vivien Didelotf515f192017-02-03 13:20:20 -0500339 default:
340 err = -EOPNOTSUPP;
341 break;
342 }
343
344 /* Non-switchdev operations cannot be rolled back. If a DSA driver
345 * returns an error during the chained call, switch chips may be in an
346 * inconsistent state.
347 */
348 if (err)
349 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
350 event, err);
351
352 return notifier_from_errno(err);
353}
354
355int dsa_switch_register_notifier(struct dsa_switch *ds)
356{
357 ds->nb.notifier_call = dsa_switch_event;
358
359 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
360}
361
362void dsa_switch_unregister_notifier(struct dsa_switch *ds)
363{
364 int err;
365
366 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
367 if (err)
368 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
369}