]> asedeno.scripts.mit.edu Git - linux.git/blob - net/dsa/switch.c
Merge branch 'etnaviv/fixes' of https://git.pengutronix.de/git/lst/linux into drm-fixes
[linux.git] / net / dsa / switch.c
1 /*
2  * Handling of a single switch chip, part of a switch fabric
3  *
4  * Copyright (c) 2017 Savoir-faire Linux Inc.
5  *      Vivien Didelot <vivien.didelot@savoirfairelinux.com>
6  *
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.
11  */
12
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>
18
19 #include "dsa_priv.h"
20
21 static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
22                                                    unsigned int ageing_time)
23 {
24         int i;
25
26         for (i = 0; i < ds->num_ports; ++i) {
27                 struct dsa_port *dp = &ds->ports[i];
28
29                 if (dp->ageing_time && dp->ageing_time < ageing_time)
30                         ageing_time = dp->ageing_time;
31         }
32
33         return ageing_time;
34 }
35
36 static int dsa_switch_ageing_time(struct dsa_switch *ds,
37                                   struct dsa_notifier_ageing_time_info *info)
38 {
39         unsigned int ageing_time = info->ageing_time;
40         struct switchdev_trans *trans = info->trans;
41
42         if (switchdev_trans_ph_prepare(trans)) {
43                 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
44                         return -ERANGE;
45                 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
46                         return -ERANGE;
47                 return 0;
48         }
49
50         /* Program the fastest ageing time in case of multiple bridges */
51         ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
52
53         if (ds->ops->set_ageing_time)
54                 return ds->ops->set_ageing_time(ds, ageing_time);
55
56         return 0;
57 }
58
59 static int dsa_switch_bridge_join(struct dsa_switch *ds,
60                                   struct dsa_notifier_bridge_info *info)
61 {
62         if (ds->index == info->sw_index && ds->ops->port_bridge_join)
63                 return ds->ops->port_bridge_join(ds, info->port, info->br);
64
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);
68
69         return 0;
70 }
71
72 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
73                                    struct dsa_notifier_bridge_info *info)
74 {
75         bool unset_vlan_filtering = br_vlan_enabled(info->br);
76         int err, i;
77
78         if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
79                 ds->ops->port_bridge_leave(ds, info->port, info->br);
80
81         if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
82                 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
83                                                 info->br);
84
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.
91          */
92         if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
93                 for (i = 0; i < ds->num_ports; i++) {
94                         if (i == info->port)
95                                 continue;
96                         if (dsa_to_port(ds, i)->bridge_dev == info->br) {
97                                 unset_vlan_filtering = false;
98                                 break;
99                         }
100                 }
101         }
102         if (unset_vlan_filtering) {
103                 struct switchdev_trans trans = {0};
104
105                 err = dsa_port_vlan_filtering(&ds->ports[info->port],
106                                               false, &trans);
107                 if (err && err != EOPNOTSUPP)
108                         return err;
109         }
110         return 0;
111 }
112
113 static int dsa_switch_fdb_add(struct dsa_switch *ds,
114                               struct dsa_notifier_fdb_info *info)
115 {
116         int port = dsa_towards_port(ds, info->sw_index, info->port);
117
118         if (!ds->ops->port_fdb_add)
119                 return -EOPNOTSUPP;
120
121         return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
122 }
123
124 static int dsa_switch_fdb_del(struct dsa_switch *ds,
125                               struct dsa_notifier_fdb_info *info)
126 {
127         int port = dsa_towards_port(ds, info->sw_index, info->port);
128
129         if (!ds->ops->port_fdb_del)
130                 return -EOPNOTSUPP;
131
132         return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
133 }
134
135 static int
136 dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds,
137                               const struct switchdev_obj_port_mdb *mdb,
138                               const unsigned long *bitmap)
139 {
140         int port, err;
141
142         if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
143                 return -EOPNOTSUPP;
144
145         for_each_set_bit(port, bitmap, ds->num_ports) {
146                 err = ds->ops->port_mdb_prepare(ds, port, mdb);
147                 if (err)
148                         return err;
149         }
150
151         return 0;
152 }
153
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)
157 {
158         int port;
159
160         for_each_set_bit(port, bitmap, ds->num_ports)
161                 ds->ops->port_mdb_add(ds, port, mdb);
162 }
163
164 static int dsa_switch_mdb_add(struct dsa_switch *ds,
165                               struct dsa_notifier_mdb_info *info)
166 {
167         const struct switchdev_obj_port_mdb *mdb = info->mdb;
168         struct switchdev_trans *trans = info->trans;
169         int port;
170
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);
178
179         if (switchdev_trans_ph_prepare(trans))
180                 return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
181
182         dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
183
184         return 0;
185 }
186
187 static int dsa_switch_mdb_del(struct dsa_switch *ds,
188                               struct dsa_notifier_mdb_info *info)
189 {
190         const struct switchdev_obj_port_mdb *mdb = info->mdb;
191
192         if (!ds->ops->port_mdb_del)
193                 return -EOPNOTSUPP;
194
195         if (ds->index == info->sw_index)
196                 return ds->ops->port_mdb_del(ds, info->port, mdb);
197
198         return 0;
199 }
200
201 static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
202                                       int vlan_dev_vid,
203                                       void *arg)
204 {
205         struct switchdev_obj_port_vlan *vlan = arg;
206         u16 vid;
207
208         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
209                 if (vid == vlan_dev_vid)
210                         return -EBUSY;
211         }
212
213         return 0;
214 }
215
216 static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
217                                const struct switchdev_obj_port_vlan *vlan)
218 {
219         const struct dsa_port *dp = dsa_to_port(ds, port);
220         int err = 0;
221
222         /* Device is not bridged, let it proceed with the VLAN device
223          * creation.
224          */
225         if (!dp->bridge_dev)
226                 return err;
227
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
232          * claiming that VID.
233          */
234         return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
235                              (void *)vlan);
236 }
237
238 static int
239 dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
240                                const struct switchdev_obj_port_vlan *vlan,
241                                const unsigned long *bitmap)
242 {
243         int port, err;
244
245         if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
246                 return -EOPNOTSUPP;
247
248         for_each_set_bit(port, bitmap, ds->num_ports) {
249                 err = dsa_port_vlan_check(ds, port, vlan);
250                 if (err)
251                         return err;
252
253                 err = ds->ops->port_vlan_prepare(ds, port, vlan);
254                 if (err)
255                         return err;
256         }
257
258         return 0;
259 }
260
261 static void
262 dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
263                            const struct switchdev_obj_port_vlan *vlan,
264                            const unsigned long *bitmap)
265 {
266         int port;
267
268         for_each_set_bit(port, bitmap, ds->num_ports)
269                 ds->ops->port_vlan_add(ds, port, vlan);
270 }
271
272 static int dsa_switch_vlan_add(struct dsa_switch *ds,
273                                struct dsa_notifier_vlan_info *info)
274 {
275         const struct switchdev_obj_port_vlan *vlan = info->vlan;
276         struct switchdev_trans *trans = info->trans;
277         int port;
278
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);
286
287         if (switchdev_trans_ph_prepare(trans))
288                 return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
289
290         dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
291
292         return 0;
293 }
294
295 static int dsa_switch_vlan_del(struct dsa_switch *ds,
296                                struct dsa_notifier_vlan_info *info)
297 {
298         const struct switchdev_obj_port_vlan *vlan = info->vlan;
299
300         if (!ds->ops->port_vlan_del)
301                 return -EOPNOTSUPP;
302
303         if (ds->index == info->sw_index)
304                 return ds->ops->port_vlan_del(ds, info->port, vlan);
305
306         return 0;
307 }
308
309 static int dsa_switch_event(struct notifier_block *nb,
310                             unsigned long event, void *info)
311 {
312         struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
313         int err;
314
315         switch (event) {
316         case DSA_NOTIFIER_AGEING_TIME:
317                 err = dsa_switch_ageing_time(ds, info);
318                 break;
319         case DSA_NOTIFIER_BRIDGE_JOIN:
320                 err = dsa_switch_bridge_join(ds, info);
321                 break;
322         case DSA_NOTIFIER_BRIDGE_LEAVE:
323                 err = dsa_switch_bridge_leave(ds, info);
324                 break;
325         case DSA_NOTIFIER_FDB_ADD:
326                 err = dsa_switch_fdb_add(ds, info);
327                 break;
328         case DSA_NOTIFIER_FDB_DEL:
329                 err = dsa_switch_fdb_del(ds, info);
330                 break;
331         case DSA_NOTIFIER_MDB_ADD:
332                 err = dsa_switch_mdb_add(ds, info);
333                 break;
334         case DSA_NOTIFIER_MDB_DEL:
335                 err = dsa_switch_mdb_del(ds, info);
336                 break;
337         case DSA_NOTIFIER_VLAN_ADD:
338                 err = dsa_switch_vlan_add(ds, info);
339                 break;
340         case DSA_NOTIFIER_VLAN_DEL:
341                 err = dsa_switch_vlan_del(ds, info);
342                 break;
343         default:
344                 err = -EOPNOTSUPP;
345                 break;
346         }
347
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.
351          */
352         if (err)
353                 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
354                         event, err);
355
356         return notifier_from_errno(err);
357 }
358
359 int dsa_switch_register_notifier(struct dsa_switch *ds)
360 {
361         ds->nb.notifier_call = dsa_switch_event;
362
363         return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
364 }
365
366 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
367 {
368         int err;
369
370         err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
371         if (err)
372                 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
373 }