Merge tag 'tegra-for-5.5-soc-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / net / dsa / switch.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Handling of a single switch chip, part of a switch fabric
4  *
5  * Copyright (c) 2017 Savoir-faire Linux Inc.
6  *      Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7  */
8
9 #include <linux/if_bridge.h>
10 #include <linux/netdevice.h>
11 #include <linux/notifier.h>
12 #include <linux/if_vlan.h>
13 #include <net/switchdev.h>
14
15 #include "dsa_priv.h"
16
17 static 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 = dsa_to_port(ds, 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
32 static 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
38         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
55 static 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
61         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);
64
65         return 0;
66 }
67
68 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
69                                    struct dsa_notifier_bridge_info *info)
70 {
71         bool unset_vlan_filtering = br_vlan_enabled(info->br);
72         int err, i;
73
74         if (ds->index == info->sw_index && ds->ops->port_bridge_leave)
75                 ds->ops->port_bridge_leave(ds, info->port, info->br);
76
77         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);
80
81         /* 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(dsa_to_port(ds, info->port),
102                                               false, &trans);
103                 if (err && err != EOPNOTSUPP)
104                         return err;
105         }
106         return 0;
107 }
108
109 static int dsa_switch_fdb_add(struct dsa_switch *ds,
110                               struct dsa_notifier_fdb_info *info)
111 {
112         int port = dsa_towards_port(ds, info->sw_index, info->port);
113
114         if (!ds->ops->port_fdb_add)
115                 return -EOPNOTSUPP;
116
117         return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
118 }
119
120 static int dsa_switch_fdb_del(struct dsa_switch *ds,
121                               struct dsa_notifier_fdb_info *info)
122 {
123         int port = dsa_towards_port(ds, info->sw_index, info->port);
124
125         if (!ds->ops->port_fdb_del)
126                 return -EOPNOTSUPP;
127
128         return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
129 }
130
131 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
132                                  struct dsa_notifier_mdb_info *info)
133 {
134         if (ds->index == info->sw_index && port == info->port)
135                 return true;
136
137         if (dsa_is_dsa_port(ds, port))
138                 return true;
139
140         return false;
141 }
142
143 static int dsa_switch_mdb_prepare(struct dsa_switch *ds,
144                                   struct dsa_notifier_mdb_info *info)
145 {
146         int port, err;
147
148         if (!ds->ops->port_mdb_prepare || !ds->ops->port_mdb_add)
149                 return -EOPNOTSUPP;
150
151         for (port = 0; port < ds->num_ports; port++) {
152                 if (dsa_switch_mdb_match(ds, port, info)) {
153                         err = ds->ops->port_mdb_prepare(ds, port, info->mdb);
154                         if (err)
155                                 return err;
156                 }
157         }
158
159         return 0;
160 }
161
162 static int dsa_switch_mdb_add(struct dsa_switch *ds,
163                               struct dsa_notifier_mdb_info *info)
164 {
165         int port;
166
167         if (switchdev_trans_ph_prepare(info->trans))
168                 return dsa_switch_mdb_prepare(ds, info);
169
170         if (!ds->ops->port_mdb_add)
171                 return 0;
172
173         for (port = 0; port < ds->num_ports; port++)
174                 if (dsa_switch_mdb_match(ds, port, info))
175                         ds->ops->port_mdb_add(ds, port, info->mdb);
176
177         return 0;
178 }
179
180 static int dsa_switch_mdb_del(struct dsa_switch *ds,
181                               struct dsa_notifier_mdb_info *info)
182 {
183         if (!ds->ops->port_mdb_del)
184                 return -EOPNOTSUPP;
185
186         if (ds->index == info->sw_index)
187                 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
188
189         return 0;
190 }
191
192 static int dsa_port_vlan_device_check(struct net_device *vlan_dev,
193                                       int vlan_dev_vid,
194                                       void *arg)
195 {
196         struct switchdev_obj_port_vlan *vlan = arg;
197         u16 vid;
198
199         for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
200                 if (vid == vlan_dev_vid)
201                         return -EBUSY;
202         }
203
204         return 0;
205 }
206
207 static int dsa_port_vlan_check(struct dsa_switch *ds, int port,
208                                const struct switchdev_obj_port_vlan *vlan)
209 {
210         const struct dsa_port *dp = dsa_to_port(ds, port);
211         int err = 0;
212
213         /* Device is not bridged, let it proceed with the VLAN device
214          * creation.
215          */
216         if (!dp->bridge_dev)
217                 return err;
218
219         /* dsa_slave_vlan_rx_{add,kill}_vid() cannot use the prepare phase and
220          * already checks whether there is an overlapping bridge VLAN entry
221          * with the same VID, so here we only need to check that if we are
222          * adding a bridge VLAN entry there is not an overlapping VLAN device
223          * claiming that VID.
224          */
225         return vlan_for_each(dp->slave, dsa_port_vlan_device_check,
226                              (void *)vlan);
227 }
228
229 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
230                                   struct dsa_notifier_vlan_info *info)
231 {
232         if (ds->index == info->sw_index && port == info->port)
233                 return true;
234
235         if (dsa_is_dsa_port(ds, port))
236                 return true;
237
238         return false;
239 }
240
241 static int dsa_switch_vlan_prepare(struct dsa_switch *ds,
242                                    struct dsa_notifier_vlan_info *info)
243 {
244         int port, err;
245
246         if (!ds->ops->port_vlan_prepare || !ds->ops->port_vlan_add)
247                 return -EOPNOTSUPP;
248
249         for (port = 0; port < ds->num_ports; port++) {
250                 if (dsa_switch_vlan_match(ds, port, info)) {
251                         err = dsa_port_vlan_check(ds, port, info->vlan);
252                         if (err)
253                                 return err;
254
255                         err = ds->ops->port_vlan_prepare(ds, port, info->vlan);
256                         if (err)
257                                 return err;
258                 }
259         }
260
261         return 0;
262 }
263
264 static int dsa_switch_vlan_add(struct dsa_switch *ds,
265                                struct dsa_notifier_vlan_info *info)
266 {
267         int port;
268
269         if (switchdev_trans_ph_prepare(info->trans))
270                 return dsa_switch_vlan_prepare(ds, info);
271
272         if (!ds->ops->port_vlan_add)
273                 return 0;
274
275         for (port = 0; port < ds->num_ports; port++)
276                 if (dsa_switch_vlan_match(ds, port, info))
277                         ds->ops->port_vlan_add(ds, port, info->vlan);
278
279         return 0;
280 }
281
282 static int dsa_switch_vlan_del(struct dsa_switch *ds,
283                                struct dsa_notifier_vlan_info *info)
284 {
285         if (!ds->ops->port_vlan_del)
286                 return -EOPNOTSUPP;
287
288         if (ds->index == info->sw_index)
289                 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
290
291         /* Do not deprogram the DSA links as they may be used as conduit
292          * for other VLAN members in the fabric.
293          */
294         return 0;
295 }
296
297 static int dsa_switch_event(struct notifier_block *nb,
298                             unsigned long event, void *info)
299 {
300         struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
301         int err;
302
303         switch (event) {
304         case DSA_NOTIFIER_AGEING_TIME:
305                 err = dsa_switch_ageing_time(ds, info);
306                 break;
307         case DSA_NOTIFIER_BRIDGE_JOIN:
308                 err = dsa_switch_bridge_join(ds, info);
309                 break;
310         case DSA_NOTIFIER_BRIDGE_LEAVE:
311                 err = dsa_switch_bridge_leave(ds, info);
312                 break;
313         case DSA_NOTIFIER_FDB_ADD:
314                 err = dsa_switch_fdb_add(ds, info);
315                 break;
316         case DSA_NOTIFIER_FDB_DEL:
317                 err = dsa_switch_fdb_del(ds, info);
318                 break;
319         case DSA_NOTIFIER_MDB_ADD:
320                 err = dsa_switch_mdb_add(ds, info);
321                 break;
322         case DSA_NOTIFIER_MDB_DEL:
323                 err = dsa_switch_mdb_del(ds, info);
324                 break;
325         case DSA_NOTIFIER_VLAN_ADD:
326                 err = dsa_switch_vlan_add(ds, info);
327                 break;
328         case DSA_NOTIFIER_VLAN_DEL:
329                 err = dsa_switch_vlan_del(ds, info);
330                 break;
331         default:
332                 err = -EOPNOTSUPP;
333                 break;
334         }
335
336         /* Non-switchdev operations cannot be rolled back. If a DSA driver
337          * returns an error during the chained call, switch chips may be in an
338          * inconsistent state.
339          */
340         if (err)
341                 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
342                         event, err);
343
344         return notifier_from_errno(err);
345 }
346
347 int dsa_switch_register_notifier(struct dsa_switch *ds)
348 {
349         ds->nb.notifier_call = dsa_switch_event;
350
351         return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
352 }
353
354 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
355 {
356         int err;
357
358         err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
359         if (err)
360                 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
361 }