net: dsa: Deny enslaving VLAN devices into VLAN aware bridge
[linux-2.6-block.git] / net / dsa / switch.c
CommitLineData
f515f192
VD
1/*
2 * Handling of a single switch chip, part of a switch fabric
3 *
4333d619
VD
4 * Copyright (c) 2017 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
f515f192
VD
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/netdevice.h>
14#include <linux/notifier.h>
1faabf74 15#include <net/switchdev.h>
ea5dd34b
VD
16
17#include "dsa_priv.h"
f515f192 18
1faabf74
VD
19static unsigned int dsa_switch_fastest_ageing_time(struct dsa_switch *ds,
20 unsigned int ageing_time)
21{
22 int i;
23
24 for (i = 0; i < ds->num_ports; ++i) {
25 struct dsa_port *dp = &ds->ports[i];
26
27 if (dp->ageing_time && dp->ageing_time < ageing_time)
28 ageing_time = dp->ageing_time;
29 }
30
31 return ageing_time;
32}
33
34static int dsa_switch_ageing_time(struct dsa_switch *ds,
35 struct dsa_notifier_ageing_time_info *info)
36{
37 unsigned int ageing_time = info->ageing_time;
38 struct switchdev_trans *trans = info->trans;
39
1faabf74
VD
40 if (switchdev_trans_ph_prepare(trans)) {
41 if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
42 return -ERANGE;
43 if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
44 return -ERANGE;
45 return 0;
46 }
47
48 /* Program the fastest ageing time in case of multiple bridges */
49 ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
50
51 if (ds->ops->set_ageing_time)
52 return ds->ops->set_ageing_time(ds, ageing_time);
53
54 return 0;
55}
56
04d3a4c6
VD
57static int dsa_switch_bridge_join(struct dsa_switch *ds,
58 struct dsa_notifier_bridge_info *info)
59{
60 if (ds->index == info->sw_index && ds->ops->port_bridge_join)
61 return ds->ops->port_bridge_join(ds, info->port, info->br);
62
40ef2c93
VD
63 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_join)
64 return ds->ops->crosschip_bridge_join(ds, info->sw_index,
65 info->port, info->br);
04d3a4c6
VD
66
67 return 0;
68}
69
70static int dsa_switch_bridge_leave(struct dsa_switch *ds,
71 struct dsa_notifier_bridge_info *info)
72{
73 if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
74 ds->ops->port_bridge_leave(ds, info->port, info->br);
75
40ef2c93
VD
76 if (ds->index != info->sw_index && ds->ops->crosschip_bridge_leave)
77 ds->ops->crosschip_bridge_leave(ds, info->sw_index, info->port,
78 info->br);
04d3a4c6
VD
79
80 return 0;
81}
82
685fb6a4
VD
83static int dsa_switch_fdb_add(struct dsa_switch *ds,
84 struct dsa_notifier_fdb_info *info)
85{
3169241f 86 int port = dsa_towards_port(ds, info->sw_index, info->port);
685fb6a4 87
1b6dd556
AS
88 if (!ds->ops->port_fdb_add)
89 return -EOPNOTSUPP;
685fb6a4 90
3169241f 91 return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
685fb6a4
VD
92}
93
94static int dsa_switch_fdb_del(struct dsa_switch *ds,
95 struct dsa_notifier_fdb_info *info)
96{
3169241f 97 int port = dsa_towards_port(ds, info->sw_index, info->port);
685fb6a4
VD
98
99 if (!ds->ops->port_fdb_del)
100 return -EOPNOTSUPP;
101
3169241f 102 return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
685fb6a4
VD
103}
104
e6db98db
VD
105static int
106dsa_switch_mdb_prepare_bitmap(struct dsa_switch *ds,
107 const struct switchdev_obj_port_mdb *mdb,
108 const unsigned long *bitmap)
109{
110 int port, err;
111
112 if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
113 return -EOPNOTSUPP;
114
115 for_each_set_bit(port, bitmap, ds->num_ports) {
116 err = ds->ops->port_mdb_prepare(ds, port, mdb);
117 if (err)
118 return err;
119 }
120
121 return 0;
122}
123
124static void dsa_switch_mdb_add_bitmap(struct dsa_switch *ds,
125 const struct switchdev_obj_port_mdb *mdb,
126 const unsigned long *bitmap)
127{
128 int port;
129
130 for_each_set_bit(port, bitmap, ds->num_ports)
131 ds->ops->port_mdb_add(ds, port, mdb);
132}
133
8ae5bcdc
VD
134static int dsa_switch_mdb_add(struct dsa_switch *ds,
135 struct dsa_notifier_mdb_info *info)
136{
137 const struct switchdev_obj_port_mdb *mdb = info->mdb;
138 struct switchdev_trans *trans = info->trans;
e6db98db 139 int port;
8ae5bcdc 140
a1a6b7ea 141 /* Build a mask of Multicast group members */
0015b80a 142 bitmap_zero(ds->bitmap, ds->num_ports);
a1a6b7ea 143 if (ds->index == info->sw_index)
0015b80a 144 set_bit(info->port, ds->bitmap);
a1a6b7ea 145 for (port = 0; port < ds->num_ports; port++)
ae45102c 146 if (dsa_is_dsa_port(ds, port))
0015b80a 147 set_bit(port, ds->bitmap);
8ae5bcdc 148
e6db98db 149 if (switchdev_trans_ph_prepare(trans))
0015b80a 150 return dsa_switch_mdb_prepare_bitmap(ds, mdb, ds->bitmap);
8ae5bcdc 151
0015b80a 152 dsa_switch_mdb_add_bitmap(ds, mdb, ds->bitmap);
8ae5bcdc
VD
153
154 return 0;
155}
156
157static int dsa_switch_mdb_del(struct dsa_switch *ds,
158 struct dsa_notifier_mdb_info *info)
159{
160 const struct switchdev_obj_port_mdb *mdb = info->mdb;
161
8ae5bcdc
VD
162 if (!ds->ops->port_mdb_del)
163 return -EOPNOTSUPP;
164
a1a6b7ea
VD
165 if (ds->index == info->sw_index)
166 return ds->ops->port_mdb_del(ds, info->port, mdb);
167
168 return 0;
8ae5bcdc
VD
169}
170
9c428c59
VD
171static int
172dsa_switch_vlan_prepare_bitmap(struct dsa_switch *ds,
173 const struct switchdev_obj_port_vlan *vlan,
174 const unsigned long *bitmap)
175{
176 int port, err;
177
178 if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
179 return -EOPNOTSUPP;
180
181 for_each_set_bit(port, bitmap, ds->num_ports) {
182 err = ds->ops->port_vlan_prepare(ds, port, vlan);
183 if (err)
184 return err;
185 }
186
187 return 0;
188}
189
190static void
191dsa_switch_vlan_add_bitmap(struct dsa_switch *ds,
192 const struct switchdev_obj_port_vlan *vlan,
193 const unsigned long *bitmap)
194{
195 int port;
196
197 for_each_set_bit(port, bitmap, ds->num_ports)
198 ds->ops->port_vlan_add(ds, port, vlan);
199}
200
d0c627b8
VD
201static int dsa_switch_vlan_add(struct dsa_switch *ds,
202 struct dsa_notifier_vlan_info *info)
203{
204 const struct switchdev_obj_port_vlan *vlan = info->vlan;
205 struct switchdev_trans *trans = info->trans;
9c428c59 206 int port;
d0c627b8 207
1ca4aa9c 208 /* Build a mask of VLAN members */
0015b80a 209 bitmap_zero(ds->bitmap, ds->num_ports);
1ca4aa9c 210 if (ds->index == info->sw_index)
0015b80a 211 set_bit(info->port, ds->bitmap);
b2f81d30
VD
212 for (port = 0; port < ds->num_ports; port++)
213 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
0015b80a 214 set_bit(port, ds->bitmap);
d0c627b8 215
9c428c59 216 if (switchdev_trans_ph_prepare(trans))
0015b80a 217 return dsa_switch_vlan_prepare_bitmap(ds, vlan, ds->bitmap);
d0c627b8 218
0015b80a 219 dsa_switch_vlan_add_bitmap(ds, vlan, ds->bitmap);
d0c627b8
VD
220
221 return 0;
222}
223
224static int dsa_switch_vlan_del(struct dsa_switch *ds,
225 struct dsa_notifier_vlan_info *info)
226{
227 const struct switchdev_obj_port_vlan *vlan = info->vlan;
228
d0c627b8
VD
229 if (!ds->ops->port_vlan_del)
230 return -EOPNOTSUPP;
231
1ca4aa9c
VD
232 if (ds->index == info->sw_index)
233 return ds->ops->port_vlan_del(ds, info->port, vlan);
234
235 return 0;
d0c627b8
VD
236}
237
f515f192
VD
238static int dsa_switch_event(struct notifier_block *nb,
239 unsigned long event, void *info)
240{
241 struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
242 int err;
243
244 switch (event) {
1faabf74
VD
245 case DSA_NOTIFIER_AGEING_TIME:
246 err = dsa_switch_ageing_time(ds, info);
247 break;
04d3a4c6
VD
248 case DSA_NOTIFIER_BRIDGE_JOIN:
249 err = dsa_switch_bridge_join(ds, info);
250 break;
251 case DSA_NOTIFIER_BRIDGE_LEAVE:
252 err = dsa_switch_bridge_leave(ds, info);
253 break;
685fb6a4
VD
254 case DSA_NOTIFIER_FDB_ADD:
255 err = dsa_switch_fdb_add(ds, info);
256 break;
257 case DSA_NOTIFIER_FDB_DEL:
258 err = dsa_switch_fdb_del(ds, info);
259 break;
8ae5bcdc
VD
260 case DSA_NOTIFIER_MDB_ADD:
261 err = dsa_switch_mdb_add(ds, info);
262 break;
263 case DSA_NOTIFIER_MDB_DEL:
264 err = dsa_switch_mdb_del(ds, info);
265 break;
d0c627b8
VD
266 case DSA_NOTIFIER_VLAN_ADD:
267 err = dsa_switch_vlan_add(ds, info);
268 break;
269 case DSA_NOTIFIER_VLAN_DEL:
270 err = dsa_switch_vlan_del(ds, info);
271 break;
f515f192
VD
272 default:
273 err = -EOPNOTSUPP;
274 break;
275 }
276
277 /* Non-switchdev operations cannot be rolled back. If a DSA driver
278 * returns an error during the chained call, switch chips may be in an
279 * inconsistent state.
280 */
281 if (err)
282 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
283 event, err);
284
285 return notifier_from_errno(err);
286}
287
288int dsa_switch_register_notifier(struct dsa_switch *ds)
289{
290 ds->nb.notifier_call = dsa_switch_event;
291
292 return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
293}
294
295void dsa_switch_unregister_notifier(struct dsa_switch *ds)
296{
297 int err;
298
299 err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
300 if (err)
301 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
302}