net: dsa: propagate extack to .port_vlan_add
[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
37         if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
38                 return -ERANGE;
39
40         if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
41                 return -ERANGE;
42
43         /* Program the fastest ageing time in case of multiple bridges */
44         ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
45
46         if (ds->ops->set_ageing_time)
47                 return ds->ops->set_ageing_time(ds, ageing_time);
48
49         return 0;
50 }
51
52 static bool dsa_switch_mtu_match(struct dsa_switch *ds, int port,
53                                  struct dsa_notifier_mtu_info *info)
54 {
55         if (ds->index == info->sw_index)
56                 return (port == info->port) || dsa_is_dsa_port(ds, port);
57
58         if (!info->propagate_upstream)
59                 return false;
60
61         if (dsa_is_dsa_port(ds, port) || dsa_is_cpu_port(ds, port))
62                 return true;
63
64         return false;
65 }
66
67 static int dsa_switch_mtu(struct dsa_switch *ds,
68                           struct dsa_notifier_mtu_info *info)
69 {
70         int port, ret;
71
72         if (!ds->ops->port_change_mtu)
73                 return -EOPNOTSUPP;
74
75         for (port = 0; port < ds->num_ports; port++) {
76                 if (dsa_switch_mtu_match(ds, port, info)) {
77                         ret = ds->ops->port_change_mtu(ds, port, info->mtu);
78                         if (ret)
79                                 return ret;
80                 }
81         }
82
83         return 0;
84 }
85
86 static int dsa_switch_bridge_join(struct dsa_switch *ds,
87                                   struct dsa_notifier_bridge_info *info)
88 {
89         struct dsa_switch_tree *dst = ds->dst;
90
91         if (dst->index == info->tree_index && ds->index == info->sw_index &&
92             ds->ops->port_bridge_join)
93                 return ds->ops->port_bridge_join(ds, info->port, info->br);
94
95         if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
96             ds->ops->crosschip_bridge_join)
97                 return ds->ops->crosschip_bridge_join(ds, info->tree_index,
98                                                       info->sw_index,
99                                                       info->port, info->br);
100
101         return 0;
102 }
103
104 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
105                                    struct dsa_notifier_bridge_info *info)
106 {
107         bool unset_vlan_filtering = br_vlan_enabled(info->br);
108         struct dsa_switch_tree *dst = ds->dst;
109         int err, i;
110
111         if (dst->index == info->tree_index && ds->index == info->sw_index &&
112             ds->ops->port_bridge_join)
113                 ds->ops->port_bridge_leave(ds, info->port, info->br);
114
115         if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
116             ds->ops->crosschip_bridge_join)
117                 ds->ops->crosschip_bridge_leave(ds, info->tree_index,
118                                                 info->sw_index, info->port,
119                                                 info->br);
120
121         /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
122          * event for changing vlan_filtering setting upon slave ports leaving
123          * it. That is a good thing, because that lets us handle it and also
124          * handle the case where the switch's vlan_filtering setting is global
125          * (not per port). When that happens, the correct moment to trigger the
126          * vlan_filtering callback is only when the last port left this bridge.
127          */
128         if (unset_vlan_filtering && ds->vlan_filtering_is_global) {
129                 for (i = 0; i < ds->num_ports; i++) {
130                         if (i == info->port)
131                                 continue;
132                         if (dsa_to_port(ds, i)->bridge_dev == info->br) {
133                                 unset_vlan_filtering = false;
134                                 break;
135                         }
136                 }
137         }
138         if (unset_vlan_filtering) {
139                 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
140                                               false);
141                 if (err && err != EOPNOTSUPP)
142                         return err;
143         }
144         return 0;
145 }
146
147 static int dsa_switch_fdb_add(struct dsa_switch *ds,
148                               struct dsa_notifier_fdb_info *info)
149 {
150         int port = dsa_towards_port(ds, info->sw_index, info->port);
151
152         if (!ds->ops->port_fdb_add)
153                 return -EOPNOTSUPP;
154
155         return ds->ops->port_fdb_add(ds, port, info->addr, info->vid);
156 }
157
158 static int dsa_switch_fdb_del(struct dsa_switch *ds,
159                               struct dsa_notifier_fdb_info *info)
160 {
161         int port = dsa_towards_port(ds, info->sw_index, info->port);
162
163         if (!ds->ops->port_fdb_del)
164                 return -EOPNOTSUPP;
165
166         return ds->ops->port_fdb_del(ds, port, info->addr, info->vid);
167 }
168
169 static int dsa_switch_hsr_join(struct dsa_switch *ds,
170                                struct dsa_notifier_hsr_info *info)
171 {
172         if (ds->index == info->sw_index && ds->ops->port_hsr_join)
173                 return ds->ops->port_hsr_join(ds, info->port, info->hsr);
174
175         return -EOPNOTSUPP;
176 }
177
178 static int dsa_switch_hsr_leave(struct dsa_switch *ds,
179                                 struct dsa_notifier_hsr_info *info)
180 {
181         if (ds->index == info->sw_index && ds->ops->port_hsr_leave)
182                 return ds->ops->port_hsr_leave(ds, info->port, info->hsr);
183
184         return -EOPNOTSUPP;
185 }
186
187 static int dsa_switch_lag_change(struct dsa_switch *ds,
188                                  struct dsa_notifier_lag_info *info)
189 {
190         if (ds->index == info->sw_index && ds->ops->port_lag_change)
191                 return ds->ops->port_lag_change(ds, info->port);
192
193         if (ds->index != info->sw_index && ds->ops->crosschip_lag_change)
194                 return ds->ops->crosschip_lag_change(ds, info->sw_index,
195                                                      info->port);
196
197         return 0;
198 }
199
200 static int dsa_switch_lag_join(struct dsa_switch *ds,
201                                struct dsa_notifier_lag_info *info)
202 {
203         if (ds->index == info->sw_index && ds->ops->port_lag_join)
204                 return ds->ops->port_lag_join(ds, info->port, info->lag,
205                                               info->info);
206
207         if (ds->index != info->sw_index && ds->ops->crosschip_lag_join)
208                 return ds->ops->crosschip_lag_join(ds, info->sw_index,
209                                                    info->port, info->lag,
210                                                    info->info);
211
212         return 0;
213 }
214
215 static int dsa_switch_lag_leave(struct dsa_switch *ds,
216                                 struct dsa_notifier_lag_info *info)
217 {
218         if (ds->index == info->sw_index && ds->ops->port_lag_leave)
219                 return ds->ops->port_lag_leave(ds, info->port, info->lag);
220
221         if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave)
222                 return ds->ops->crosschip_lag_leave(ds, info->sw_index,
223                                                     info->port, info->lag);
224
225         return 0;
226 }
227
228 static bool dsa_switch_mdb_match(struct dsa_switch *ds, int port,
229                                  struct dsa_notifier_mdb_info *info)
230 {
231         if (ds->index == info->sw_index && port == info->port)
232                 return true;
233
234         if (dsa_is_dsa_port(ds, port))
235                 return true;
236
237         return false;
238 }
239
240 static int dsa_switch_mdb_add(struct dsa_switch *ds,
241                               struct dsa_notifier_mdb_info *info)
242 {
243         int err = 0;
244         int port;
245
246         if (!ds->ops->port_mdb_add)
247                 return -EOPNOTSUPP;
248
249         for (port = 0; port < ds->num_ports; port++) {
250                 if (dsa_switch_mdb_match(ds, port, info)) {
251                         err = ds->ops->port_mdb_add(ds, port, info->mdb);
252                         if (err)
253                                 break;
254                 }
255         }
256
257         return err;
258 }
259
260 static int dsa_switch_mdb_del(struct dsa_switch *ds,
261                               struct dsa_notifier_mdb_info *info)
262 {
263         if (!ds->ops->port_mdb_del)
264                 return -EOPNOTSUPP;
265
266         if (ds->index == info->sw_index)
267                 return ds->ops->port_mdb_del(ds, info->port, info->mdb);
268
269         return 0;
270 }
271
272 static bool dsa_switch_vlan_match(struct dsa_switch *ds, int port,
273                                   struct dsa_notifier_vlan_info *info)
274 {
275         if (ds->index == info->sw_index && port == info->port)
276                 return true;
277
278         if (dsa_is_dsa_port(ds, port))
279                 return true;
280
281         return false;
282 }
283
284 static int dsa_switch_vlan_add(struct dsa_switch *ds,
285                                struct dsa_notifier_vlan_info *info)
286 {
287         int port, err;
288
289         if (!ds->ops->port_vlan_add)
290                 return -EOPNOTSUPP;
291
292         for (port = 0; port < ds->num_ports; port++) {
293                 if (dsa_switch_vlan_match(ds, port, info)) {
294                         err = ds->ops->port_vlan_add(ds, port, info->vlan,
295                                                      info->extack);
296                         if (err)
297                                 return err;
298                 }
299         }
300
301         return 0;
302 }
303
304 static int dsa_switch_vlan_del(struct dsa_switch *ds,
305                                struct dsa_notifier_vlan_info *info)
306 {
307         if (!ds->ops->port_vlan_del)
308                 return -EOPNOTSUPP;
309
310         if (ds->index == info->sw_index)
311                 return ds->ops->port_vlan_del(ds, info->port, info->vlan);
312
313         /* Do not deprogram the DSA links as they may be used as conduit
314          * for other VLAN members in the fabric.
315          */
316         return 0;
317 }
318
319 static bool dsa_switch_tag_proto_match(struct dsa_switch *ds, int port,
320                                        struct dsa_notifier_tag_proto_info *info)
321 {
322         if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))
323                 return true;
324
325         return false;
326 }
327
328 static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
329                                        struct dsa_notifier_tag_proto_info *info)
330 {
331         const struct dsa_device_ops *tag_ops = info->tag_ops;
332         int port, err;
333
334         if (!ds->ops->change_tag_protocol)
335                 return -EOPNOTSUPP;
336
337         ASSERT_RTNL();
338
339         for (port = 0; port < ds->num_ports; port++) {
340                 if (dsa_switch_tag_proto_match(ds, port, info)) {
341                         err = ds->ops->change_tag_protocol(ds, port,
342                                                            tag_ops->proto);
343                         if (err)
344                                 return err;
345
346                         if (dsa_is_cpu_port(ds, port))
347                                 dsa_port_set_tag_protocol(dsa_to_port(ds, port),
348                                                           tag_ops);
349                 }
350         }
351
352         /* Now that changing the tag protocol can no longer fail, let's update
353          * the remaining bits which are "duplicated for faster access", and the
354          * bits that depend on the tagger, such as the MTU.
355          */
356         for (port = 0; port < ds->num_ports; port++) {
357                 if (dsa_is_user_port(ds, port)) {
358                         struct net_device *slave;
359
360                         slave = dsa_to_port(ds, port)->slave;
361                         dsa_slave_setup_tagger(slave);
362
363                         /* rtnl_mutex is held in dsa_tree_change_tag_proto */
364                         dsa_slave_change_mtu(slave, slave->mtu);
365                 }
366         }
367
368         return 0;
369 }
370
371 static int dsa_switch_event(struct notifier_block *nb,
372                             unsigned long event, void *info)
373 {
374         struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
375         int err;
376
377         switch (event) {
378         case DSA_NOTIFIER_AGEING_TIME:
379                 err = dsa_switch_ageing_time(ds, info);
380                 break;
381         case DSA_NOTIFIER_BRIDGE_JOIN:
382                 err = dsa_switch_bridge_join(ds, info);
383                 break;
384         case DSA_NOTIFIER_BRIDGE_LEAVE:
385                 err = dsa_switch_bridge_leave(ds, info);
386                 break;
387         case DSA_NOTIFIER_FDB_ADD:
388                 err = dsa_switch_fdb_add(ds, info);
389                 break;
390         case DSA_NOTIFIER_FDB_DEL:
391                 err = dsa_switch_fdb_del(ds, info);
392                 break;
393         case DSA_NOTIFIER_HSR_JOIN:
394                 err = dsa_switch_hsr_join(ds, info);
395                 break;
396         case DSA_NOTIFIER_HSR_LEAVE:
397                 err = dsa_switch_hsr_leave(ds, info);
398                 break;
399         case DSA_NOTIFIER_LAG_CHANGE:
400                 err = dsa_switch_lag_change(ds, info);
401                 break;
402         case DSA_NOTIFIER_LAG_JOIN:
403                 err = dsa_switch_lag_join(ds, info);
404                 break;
405         case DSA_NOTIFIER_LAG_LEAVE:
406                 err = dsa_switch_lag_leave(ds, info);
407                 break;
408         case DSA_NOTIFIER_MDB_ADD:
409                 err = dsa_switch_mdb_add(ds, info);
410                 break;
411         case DSA_NOTIFIER_MDB_DEL:
412                 err = dsa_switch_mdb_del(ds, info);
413                 break;
414         case DSA_NOTIFIER_VLAN_ADD:
415                 err = dsa_switch_vlan_add(ds, info);
416                 break;
417         case DSA_NOTIFIER_VLAN_DEL:
418                 err = dsa_switch_vlan_del(ds, info);
419                 break;
420         case DSA_NOTIFIER_MTU:
421                 err = dsa_switch_mtu(ds, info);
422                 break;
423         case DSA_NOTIFIER_TAG_PROTO:
424                 err = dsa_switch_change_tag_proto(ds, info);
425                 break;
426         default:
427                 err = -EOPNOTSUPP;
428                 break;
429         }
430
431         if (err)
432                 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
433                         event, err);
434
435         return notifier_from_errno(err);
436 }
437
438 int dsa_switch_register_notifier(struct dsa_switch *ds)
439 {
440         ds->nb.notifier_call = dsa_switch_event;
441
442         return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
443 }
444
445 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
446 {
447         int err;
448
449         err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
450         if (err)
451                 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
452 }