OSDN Git Service

net: dsa: request drivers to perform FDB isolation
[uclinux-h8/linux.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         struct dsa_port *dp;
21
22         dsa_switch_for_each_port(dp, ds)
23                 if (dp->ageing_time && dp->ageing_time < ageing_time)
24                         ageing_time = dp->ageing_time;
25
26         return ageing_time;
27 }
28
29 static int dsa_switch_ageing_time(struct dsa_switch *ds,
30                                   struct dsa_notifier_ageing_time_info *info)
31 {
32         unsigned int ageing_time = info->ageing_time;
33
34         if (ds->ageing_time_min && ageing_time < ds->ageing_time_min)
35                 return -ERANGE;
36
37         if (ds->ageing_time_max && ageing_time > ds->ageing_time_max)
38                 return -ERANGE;
39
40         /* Program the fastest ageing time in case of multiple bridges */
41         ageing_time = dsa_switch_fastest_ageing_time(ds, ageing_time);
42
43         if (ds->ops->set_ageing_time)
44                 return ds->ops->set_ageing_time(ds, ageing_time);
45
46         return 0;
47 }
48
49 static bool dsa_port_mtu_match(struct dsa_port *dp,
50                                struct dsa_notifier_mtu_info *info)
51 {
52         if (dp->ds->index == info->sw_index && dp->index == info->port)
53                 return true;
54
55         /* Do not propagate to other switches in the tree if the notifier was
56          * targeted for a single switch.
57          */
58         if (info->targeted_match)
59                 return false;
60
61         if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
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         struct dsa_port *dp;
71         int ret;
72
73         if (!ds->ops->port_change_mtu)
74                 return -EOPNOTSUPP;
75
76         dsa_switch_for_each_port(dp, ds) {
77                 if (dsa_port_mtu_match(dp, info)) {
78                         ret = ds->ops->port_change_mtu(ds, dp->index,
79                                                        info->mtu);
80                         if (ret)
81                                 return ret;
82                 }
83         }
84
85         return 0;
86 }
87
88 static int dsa_switch_bridge_join(struct dsa_switch *ds,
89                                   struct dsa_notifier_bridge_info *info)
90 {
91         struct dsa_switch_tree *dst = ds->dst;
92         int err;
93
94         if (dst->index == info->tree_index && ds->index == info->sw_index) {
95                 if (!ds->ops->port_bridge_join)
96                         return -EOPNOTSUPP;
97
98                 err = ds->ops->port_bridge_join(ds, info->port, info->bridge,
99                                                 &info->tx_fwd_offload);
100                 if (err)
101                         return err;
102         }
103
104         if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
105             ds->ops->crosschip_bridge_join) {
106                 err = ds->ops->crosschip_bridge_join(ds, info->tree_index,
107                                                      info->sw_index,
108                                                      info->port, info->bridge);
109                 if (err)
110                         return err;
111         }
112
113         return 0;
114 }
115
116 static int dsa_switch_sync_vlan_filtering(struct dsa_switch *ds,
117                                           struct dsa_notifier_bridge_info *info)
118 {
119         struct netlink_ext_ack extack = {0};
120         bool change_vlan_filtering = false;
121         bool vlan_filtering;
122         struct dsa_port *dp;
123         int err;
124
125         if (ds->needs_standalone_vlan_filtering &&
126             !br_vlan_enabled(info->bridge.dev)) {
127                 change_vlan_filtering = true;
128                 vlan_filtering = true;
129         } else if (!ds->needs_standalone_vlan_filtering &&
130                    br_vlan_enabled(info->bridge.dev)) {
131                 change_vlan_filtering = true;
132                 vlan_filtering = false;
133         }
134
135         /* If the bridge was vlan_filtering, the bridge core doesn't trigger an
136          * event for changing vlan_filtering setting upon slave ports leaving
137          * it. That is a good thing, because that lets us handle it and also
138          * handle the case where the switch's vlan_filtering setting is global
139          * (not per port). When that happens, the correct moment to trigger the
140          * vlan_filtering callback is only when the last port leaves the last
141          * VLAN-aware bridge.
142          */
143         if (change_vlan_filtering && ds->vlan_filtering_is_global) {
144                 dsa_switch_for_each_port(dp, ds) {
145                         struct net_device *br = dsa_port_bridge_dev_get(dp);
146
147                         if (br && br_vlan_enabled(br)) {
148                                 change_vlan_filtering = false;
149                                 break;
150                         }
151                 }
152         }
153
154         if (change_vlan_filtering) {
155                 err = dsa_port_vlan_filtering(dsa_to_port(ds, info->port),
156                                               vlan_filtering, &extack);
157                 if (extack._msg)
158                         dev_err(ds->dev, "port %d: %s\n", info->port,
159                                 extack._msg);
160                 if (err && err != -EOPNOTSUPP)
161                         return err;
162         }
163
164         return 0;
165 }
166
167 static int dsa_switch_bridge_leave(struct dsa_switch *ds,
168                                    struct dsa_notifier_bridge_info *info)
169 {
170         struct dsa_switch_tree *dst = ds->dst;
171         int err;
172
173         if (dst->index == info->tree_index && ds->index == info->sw_index &&
174             ds->ops->port_bridge_leave)
175                 ds->ops->port_bridge_leave(ds, info->port, info->bridge);
176
177         if ((dst->index != info->tree_index || ds->index != info->sw_index) &&
178             ds->ops->crosschip_bridge_leave)
179                 ds->ops->crosschip_bridge_leave(ds, info->tree_index,
180                                                 info->sw_index, info->port,
181                                                 info->bridge);
182
183         if (ds->dst->index == info->tree_index && ds->index == info->sw_index) {
184                 err = dsa_switch_sync_vlan_filtering(ds, info);
185                 if (err)
186                         return err;
187         }
188
189         return 0;
190 }
191
192 /* Matches for all upstream-facing ports (the CPU port and all upstream-facing
193  * DSA links) that sit between the targeted port on which the notifier was
194  * emitted and its dedicated CPU port.
195  */
196 static bool dsa_port_host_address_match(struct dsa_port *dp,
197                                         int info_sw_index, int info_port)
198 {
199         struct dsa_port *targeted_dp, *cpu_dp;
200         struct dsa_switch *targeted_ds;
201
202         targeted_ds = dsa_switch_find(dp->ds->dst->index, info_sw_index);
203         targeted_dp = dsa_to_port(targeted_ds, info_port);
204         cpu_dp = targeted_dp->cpu_dp;
205
206         if (dsa_switch_is_upstream_of(dp->ds, targeted_ds))
207                 return dp->index == dsa_towards_port(dp->ds, cpu_dp->ds->index,
208                                                      cpu_dp->index);
209
210         return false;
211 }
212
213 static bool dsa_db_equal(const struct dsa_db *a, const struct dsa_db *b)
214 {
215         if (a->type != b->type)
216                 return false;
217
218         switch (a->type) {
219         case DSA_DB_PORT:
220                 return a->dp == b->dp;
221         case DSA_DB_LAG:
222                 return a->lag.dev == b->lag.dev;
223         case DSA_DB_BRIDGE:
224                 return a->bridge.num == b->bridge.num;
225         default:
226                 WARN_ON(1);
227                 return false;
228         }
229 }
230
231 static struct dsa_mac_addr *dsa_mac_addr_find(struct list_head *addr_list,
232                                               const unsigned char *addr, u16 vid,
233                                               struct dsa_db db)
234 {
235         struct dsa_mac_addr *a;
236
237         list_for_each_entry(a, addr_list, list)
238                 if (ether_addr_equal(a->addr, addr) && a->vid == vid &&
239                     dsa_db_equal(&a->db, &db))
240                         return a;
241
242         return NULL;
243 }
244
245 static int dsa_port_do_mdb_add(struct dsa_port *dp,
246                                const struct switchdev_obj_port_mdb *mdb,
247                                struct dsa_db db)
248 {
249         struct dsa_switch *ds = dp->ds;
250         struct dsa_mac_addr *a;
251         int port = dp->index;
252         int err = 0;
253
254         /* No need to bother with refcounting for user ports */
255         if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
256                 return ds->ops->port_mdb_add(ds, port, mdb, db);
257
258         mutex_lock(&dp->addr_lists_lock);
259
260         a = dsa_mac_addr_find(&dp->mdbs, mdb->addr, mdb->vid, db);
261         if (a) {
262                 refcount_inc(&a->refcount);
263                 goto out;
264         }
265
266         a = kzalloc(sizeof(*a), GFP_KERNEL);
267         if (!a) {
268                 err = -ENOMEM;
269                 goto out;
270         }
271
272         err = ds->ops->port_mdb_add(ds, port, mdb, db);
273         if (err) {
274                 kfree(a);
275                 goto out;
276         }
277
278         ether_addr_copy(a->addr, mdb->addr);
279         a->vid = mdb->vid;
280         a->db = db;
281         refcount_set(&a->refcount, 1);
282         list_add_tail(&a->list, &dp->mdbs);
283
284 out:
285         mutex_unlock(&dp->addr_lists_lock);
286
287         return err;
288 }
289
290 static int dsa_port_do_mdb_del(struct dsa_port *dp,
291                                const struct switchdev_obj_port_mdb *mdb,
292                                struct dsa_db db)
293 {
294         struct dsa_switch *ds = dp->ds;
295         struct dsa_mac_addr *a;
296         int port = dp->index;
297         int err = 0;
298
299         /* No need to bother with refcounting for user ports */
300         if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
301                 return ds->ops->port_mdb_del(ds, port, mdb, db);
302
303         mutex_lock(&dp->addr_lists_lock);
304
305         a = dsa_mac_addr_find(&dp->mdbs, mdb->addr, mdb->vid, db);
306         if (!a) {
307                 err = -ENOENT;
308                 goto out;
309         }
310
311         if (!refcount_dec_and_test(&a->refcount))
312                 goto out;
313
314         err = ds->ops->port_mdb_del(ds, port, mdb, db);
315         if (err) {
316                 refcount_set(&a->refcount, 1);
317                 goto out;
318         }
319
320         list_del(&a->list);
321         kfree(a);
322
323 out:
324         mutex_unlock(&dp->addr_lists_lock);
325
326         return err;
327 }
328
329 static int dsa_port_do_fdb_add(struct dsa_port *dp, const unsigned char *addr,
330                                u16 vid, struct dsa_db db)
331 {
332         struct dsa_switch *ds = dp->ds;
333         struct dsa_mac_addr *a;
334         int port = dp->index;
335         int err = 0;
336
337         /* No need to bother with refcounting for user ports */
338         if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
339                 return ds->ops->port_fdb_add(ds, port, addr, vid, db);
340
341         mutex_lock(&dp->addr_lists_lock);
342
343         a = dsa_mac_addr_find(&dp->fdbs, addr, vid, db);
344         if (a) {
345                 refcount_inc(&a->refcount);
346                 goto out;
347         }
348
349         a = kzalloc(sizeof(*a), GFP_KERNEL);
350         if (!a) {
351                 err = -ENOMEM;
352                 goto out;
353         }
354
355         err = ds->ops->port_fdb_add(ds, port, addr, vid, db);
356         if (err) {
357                 kfree(a);
358                 goto out;
359         }
360
361         ether_addr_copy(a->addr, addr);
362         a->vid = vid;
363         a->db = db;
364         refcount_set(&a->refcount, 1);
365         list_add_tail(&a->list, &dp->fdbs);
366
367 out:
368         mutex_unlock(&dp->addr_lists_lock);
369
370         return err;
371 }
372
373 static int dsa_port_do_fdb_del(struct dsa_port *dp, const unsigned char *addr,
374                                u16 vid, struct dsa_db db)
375 {
376         struct dsa_switch *ds = dp->ds;
377         struct dsa_mac_addr *a;
378         int port = dp->index;
379         int err = 0;
380
381         /* No need to bother with refcounting for user ports */
382         if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
383                 return ds->ops->port_fdb_del(ds, port, addr, vid, db);
384
385         mutex_lock(&dp->addr_lists_lock);
386
387         a = dsa_mac_addr_find(&dp->fdbs, addr, vid, db);
388         if (!a) {
389                 err = -ENOENT;
390                 goto out;
391         }
392
393         if (!refcount_dec_and_test(&a->refcount))
394                 goto out;
395
396         err = ds->ops->port_fdb_del(ds, port, addr, vid, db);
397         if (err) {
398                 refcount_set(&a->refcount, 1);
399                 goto out;
400         }
401
402         list_del(&a->list);
403         kfree(a);
404
405 out:
406         mutex_unlock(&dp->addr_lists_lock);
407
408         return err;
409 }
410
411 static int dsa_switch_do_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag *lag,
412                                      const unsigned char *addr, u16 vid,
413                                      struct dsa_db db)
414 {
415         struct dsa_mac_addr *a;
416         int err = 0;
417
418         mutex_lock(&lag->fdb_lock);
419
420         a = dsa_mac_addr_find(&lag->fdbs, addr, vid, db);
421         if (a) {
422                 refcount_inc(&a->refcount);
423                 goto out;
424         }
425
426         a = kzalloc(sizeof(*a), GFP_KERNEL);
427         if (!a) {
428                 err = -ENOMEM;
429                 goto out;
430         }
431
432         err = ds->ops->lag_fdb_add(ds, *lag, addr, vid, db);
433         if (err) {
434                 kfree(a);
435                 goto out;
436         }
437
438         ether_addr_copy(a->addr, addr);
439         a->vid = vid;
440         refcount_set(&a->refcount, 1);
441         list_add_tail(&a->list, &lag->fdbs);
442
443 out:
444         mutex_unlock(&lag->fdb_lock);
445
446         return err;
447 }
448
449 static int dsa_switch_do_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag *lag,
450                                      const unsigned char *addr, u16 vid,
451                                      struct dsa_db db)
452 {
453         struct dsa_mac_addr *a;
454         int err = 0;
455
456         mutex_lock(&lag->fdb_lock);
457
458         a = dsa_mac_addr_find(&lag->fdbs, addr, vid, db);
459         if (!a) {
460                 err = -ENOENT;
461                 goto out;
462         }
463
464         if (!refcount_dec_and_test(&a->refcount))
465                 goto out;
466
467         err = ds->ops->lag_fdb_del(ds, *lag, addr, vid, db);
468         if (err) {
469                 refcount_set(&a->refcount, 1);
470                 goto out;
471         }
472
473         list_del(&a->list);
474         kfree(a);
475
476 out:
477         mutex_unlock(&lag->fdb_lock);
478
479         return err;
480 }
481
482 static int dsa_switch_host_fdb_add(struct dsa_switch *ds,
483                                    struct dsa_notifier_fdb_info *info)
484 {
485         struct dsa_port *dp;
486         int err = 0;
487
488         if (!ds->ops->port_fdb_add)
489                 return -EOPNOTSUPP;
490
491         dsa_switch_for_each_port(dp, ds) {
492                 if (dsa_port_host_address_match(dp, info->sw_index,
493                                                 info->port)) {
494                         err = dsa_port_do_fdb_add(dp, info->addr, info->vid,
495                                                   info->db);
496                         if (err)
497                                 break;
498                 }
499         }
500
501         return err;
502 }
503
504 static int dsa_switch_host_fdb_del(struct dsa_switch *ds,
505                                    struct dsa_notifier_fdb_info *info)
506 {
507         struct dsa_port *dp;
508         int err = 0;
509
510         if (!ds->ops->port_fdb_del)
511                 return -EOPNOTSUPP;
512
513         dsa_switch_for_each_port(dp, ds) {
514                 if (dsa_port_host_address_match(dp, info->sw_index,
515                                                 info->port)) {
516                         err = dsa_port_do_fdb_del(dp, info->addr, info->vid,
517                                                   info->db);
518                         if (err)
519                                 break;
520                 }
521         }
522
523         return err;
524 }
525
526 static int dsa_switch_fdb_add(struct dsa_switch *ds,
527                               struct dsa_notifier_fdb_info *info)
528 {
529         int port = dsa_towards_port(ds, info->sw_index, info->port);
530         struct dsa_port *dp = dsa_to_port(ds, port);
531
532         if (!ds->ops->port_fdb_add)
533                 return -EOPNOTSUPP;
534
535         return dsa_port_do_fdb_add(dp, info->addr, info->vid, info->db);
536 }
537
538 static int dsa_switch_fdb_del(struct dsa_switch *ds,
539                               struct dsa_notifier_fdb_info *info)
540 {
541         int port = dsa_towards_port(ds, info->sw_index, info->port);
542         struct dsa_port *dp = dsa_to_port(ds, port);
543
544         if (!ds->ops->port_fdb_del)
545                 return -EOPNOTSUPP;
546
547         return dsa_port_do_fdb_del(dp, info->addr, info->vid, info->db);
548 }
549
550 static int dsa_switch_lag_fdb_add(struct dsa_switch *ds,
551                                   struct dsa_notifier_lag_fdb_info *info)
552 {
553         struct dsa_port *dp;
554
555         if (!ds->ops->lag_fdb_add)
556                 return -EOPNOTSUPP;
557
558         /* Notify switch only if it has a port in this LAG */
559         dsa_switch_for_each_port(dp, ds)
560                 if (dsa_port_offloads_lag(dp, info->lag))
561                         return dsa_switch_do_lag_fdb_add(ds, info->lag,
562                                                          info->addr, info->vid,
563                                                          info->db);
564
565         return 0;
566 }
567
568 static int dsa_switch_lag_fdb_del(struct dsa_switch *ds,
569                                   struct dsa_notifier_lag_fdb_info *info)
570 {
571         struct dsa_port *dp;
572
573         if (!ds->ops->lag_fdb_del)
574                 return -EOPNOTSUPP;
575
576         /* Notify switch only if it has a port in this LAG */
577         dsa_switch_for_each_port(dp, ds)
578                 if (dsa_port_offloads_lag(dp, info->lag))
579                         return dsa_switch_do_lag_fdb_del(ds, info->lag,
580                                                          info->addr, info->vid,
581                                                          info->db);
582
583         return 0;
584 }
585
586 static int dsa_switch_lag_change(struct dsa_switch *ds,
587                                  struct dsa_notifier_lag_info *info)
588 {
589         if (ds->index == info->sw_index && ds->ops->port_lag_change)
590                 return ds->ops->port_lag_change(ds, info->port);
591
592         if (ds->index != info->sw_index && ds->ops->crosschip_lag_change)
593                 return ds->ops->crosschip_lag_change(ds, info->sw_index,
594                                                      info->port);
595
596         return 0;
597 }
598
599 static int dsa_switch_lag_join(struct dsa_switch *ds,
600                                struct dsa_notifier_lag_info *info)
601 {
602         if (ds->index == info->sw_index && ds->ops->port_lag_join)
603                 return ds->ops->port_lag_join(ds, info->port, info->lag,
604                                               info->info);
605
606         if (ds->index != info->sw_index && ds->ops->crosschip_lag_join)
607                 return ds->ops->crosschip_lag_join(ds, info->sw_index,
608                                                    info->port, info->lag,
609                                                    info->info);
610
611         return -EOPNOTSUPP;
612 }
613
614 static int dsa_switch_lag_leave(struct dsa_switch *ds,
615                                 struct dsa_notifier_lag_info *info)
616 {
617         if (ds->index == info->sw_index && ds->ops->port_lag_leave)
618                 return ds->ops->port_lag_leave(ds, info->port, info->lag);
619
620         if (ds->index != info->sw_index && ds->ops->crosschip_lag_leave)
621                 return ds->ops->crosschip_lag_leave(ds, info->sw_index,
622                                                     info->port, info->lag);
623
624         return -EOPNOTSUPP;
625 }
626
627 static int dsa_switch_mdb_add(struct dsa_switch *ds,
628                               struct dsa_notifier_mdb_info *info)
629 {
630         int port = dsa_towards_port(ds, info->sw_index, info->port);
631         struct dsa_port *dp = dsa_to_port(ds, port);
632
633         if (!ds->ops->port_mdb_add)
634                 return -EOPNOTSUPP;
635
636         return dsa_port_do_mdb_add(dp, info->mdb, info->db);
637 }
638
639 static int dsa_switch_mdb_del(struct dsa_switch *ds,
640                               struct dsa_notifier_mdb_info *info)
641 {
642         int port = dsa_towards_port(ds, info->sw_index, info->port);
643         struct dsa_port *dp = dsa_to_port(ds, port);
644
645         if (!ds->ops->port_mdb_del)
646                 return -EOPNOTSUPP;
647
648         return dsa_port_do_mdb_del(dp, info->mdb, info->db);
649 }
650
651 static int dsa_switch_host_mdb_add(struct dsa_switch *ds,
652                                    struct dsa_notifier_mdb_info *info)
653 {
654         struct dsa_port *dp;
655         int err = 0;
656
657         if (!ds->ops->port_mdb_add)
658                 return -EOPNOTSUPP;
659
660         dsa_switch_for_each_port(dp, ds) {
661                 if (dsa_port_host_address_match(dp, info->sw_index,
662                                                 info->port)) {
663                         err = dsa_port_do_mdb_add(dp, info->mdb, info->db);
664                         if (err)
665                                 break;
666                 }
667         }
668
669         return err;
670 }
671
672 static int dsa_switch_host_mdb_del(struct dsa_switch *ds,
673                                    struct dsa_notifier_mdb_info *info)
674 {
675         struct dsa_port *dp;
676         int err = 0;
677
678         if (!ds->ops->port_mdb_del)
679                 return -EOPNOTSUPP;
680
681         dsa_switch_for_each_port(dp, ds) {
682                 if (dsa_port_host_address_match(dp, info->sw_index,
683                                                 info->port)) {
684                         err = dsa_port_do_mdb_del(dp, info->mdb, info->db);
685                         if (err)
686                                 break;
687                 }
688         }
689
690         return err;
691 }
692
693 /* Port VLANs match on the targeted port and on all DSA ports */
694 static bool dsa_port_vlan_match(struct dsa_port *dp,
695                                 struct dsa_notifier_vlan_info *info)
696 {
697         if (dp->ds->index == info->sw_index && dp->index == info->port)
698                 return true;
699
700         if (dsa_port_is_dsa(dp))
701                 return true;
702
703         return false;
704 }
705
706 /* Host VLANs match on the targeted port's CPU port, and on all DSA ports
707  * (upstream and downstream) of that switch and its upstream switches.
708  */
709 static bool dsa_port_host_vlan_match(struct dsa_port *dp,
710                                      struct dsa_notifier_vlan_info *info)
711 {
712         struct dsa_port *targeted_dp, *cpu_dp;
713         struct dsa_switch *targeted_ds;
714
715         targeted_ds = dsa_switch_find(dp->ds->dst->index, info->sw_index);
716         targeted_dp = dsa_to_port(targeted_ds, info->port);
717         cpu_dp = targeted_dp->cpu_dp;
718
719         if (dsa_switch_is_upstream_of(dp->ds, targeted_ds))
720                 return dsa_port_is_dsa(dp) || dp == cpu_dp;
721
722         return false;
723 }
724
725 static struct dsa_vlan *dsa_vlan_find(struct list_head *vlan_list,
726                                       const struct switchdev_obj_port_vlan *vlan)
727 {
728         struct dsa_vlan *v;
729
730         list_for_each_entry(v, vlan_list, list)
731                 if (v->vid == vlan->vid)
732                         return v;
733
734         return NULL;
735 }
736
737 static int dsa_port_do_vlan_add(struct dsa_port *dp,
738                                 const struct switchdev_obj_port_vlan *vlan,
739                                 struct netlink_ext_ack *extack)
740 {
741         struct dsa_switch *ds = dp->ds;
742         int port = dp->index;
743         struct dsa_vlan *v;
744         int err = 0;
745
746         /* No need to bother with refcounting for user ports. */
747         if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
748                 return ds->ops->port_vlan_add(ds, port, vlan, extack);
749
750         /* No need to propagate on shared ports the existing VLANs that were
751          * re-notified after just the flags have changed. This would cause a
752          * refcount bump which we need to avoid, since it unbalances the
753          * additions with the deletions.
754          */
755         if (vlan->changed)
756                 return 0;
757
758         mutex_lock(&dp->vlans_lock);
759
760         v = dsa_vlan_find(&dp->vlans, vlan);
761         if (v) {
762                 refcount_inc(&v->refcount);
763                 goto out;
764         }
765
766         v = kzalloc(sizeof(*v), GFP_KERNEL);
767         if (!v) {
768                 err = -ENOMEM;
769                 goto out;
770         }
771
772         err = ds->ops->port_vlan_add(ds, port, vlan, extack);
773         if (err) {
774                 kfree(v);
775                 goto out;
776         }
777
778         v->vid = vlan->vid;
779         refcount_set(&v->refcount, 1);
780         list_add_tail(&v->list, &dp->vlans);
781
782 out:
783         mutex_unlock(&dp->vlans_lock);
784
785         return err;
786 }
787
788 static int dsa_port_do_vlan_del(struct dsa_port *dp,
789                                 const struct switchdev_obj_port_vlan *vlan)
790 {
791         struct dsa_switch *ds = dp->ds;
792         int port = dp->index;
793         struct dsa_vlan *v;
794         int err = 0;
795
796         /* No need to bother with refcounting for user ports */
797         if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
798                 return ds->ops->port_vlan_del(ds, port, vlan);
799
800         mutex_lock(&dp->vlans_lock);
801
802         v = dsa_vlan_find(&dp->vlans, vlan);
803         if (!v) {
804                 err = -ENOENT;
805                 goto out;
806         }
807
808         if (!refcount_dec_and_test(&v->refcount))
809                 goto out;
810
811         err = ds->ops->port_vlan_del(ds, port, vlan);
812         if (err) {
813                 refcount_set(&v->refcount, 1);
814                 goto out;
815         }
816
817         list_del(&v->list);
818         kfree(v);
819
820 out:
821         mutex_unlock(&dp->vlans_lock);
822
823         return err;
824 }
825
826 static int dsa_switch_vlan_add(struct dsa_switch *ds,
827                                struct dsa_notifier_vlan_info *info)
828 {
829         struct dsa_port *dp;
830         int err;
831
832         if (!ds->ops->port_vlan_add)
833                 return -EOPNOTSUPP;
834
835         dsa_switch_for_each_port(dp, ds) {
836                 if (dsa_port_vlan_match(dp, info)) {
837                         err = dsa_port_do_vlan_add(dp, info->vlan,
838                                                    info->extack);
839                         if (err)
840                                 return err;
841                 }
842         }
843
844         return 0;
845 }
846
847 static int dsa_switch_vlan_del(struct dsa_switch *ds,
848                                struct dsa_notifier_vlan_info *info)
849 {
850         struct dsa_port *dp;
851         int err;
852
853         if (!ds->ops->port_vlan_del)
854                 return -EOPNOTSUPP;
855
856         dsa_switch_for_each_port(dp, ds) {
857                 if (dsa_port_vlan_match(dp, info)) {
858                         err = dsa_port_do_vlan_del(dp, info->vlan);
859                         if (err)
860                                 return err;
861                 }
862         }
863
864         return 0;
865 }
866
867 static int dsa_switch_host_vlan_add(struct dsa_switch *ds,
868                                     struct dsa_notifier_vlan_info *info)
869 {
870         struct dsa_port *dp;
871         int err;
872
873         if (!ds->ops->port_vlan_add)
874                 return -EOPNOTSUPP;
875
876         dsa_switch_for_each_port(dp, ds) {
877                 if (dsa_port_host_vlan_match(dp, info)) {
878                         err = dsa_port_do_vlan_add(dp, info->vlan,
879                                                    info->extack);
880                         if (err)
881                                 return err;
882                 }
883         }
884
885         return 0;
886 }
887
888 static int dsa_switch_host_vlan_del(struct dsa_switch *ds,
889                                     struct dsa_notifier_vlan_info *info)
890 {
891         struct dsa_port *dp;
892         int err;
893
894         if (!ds->ops->port_vlan_del)
895                 return -EOPNOTSUPP;
896
897         dsa_switch_for_each_port(dp, ds) {
898                 if (dsa_port_host_vlan_match(dp, info)) {
899                         err = dsa_port_do_vlan_del(dp, info->vlan);
900                         if (err)
901                                 return err;
902                 }
903         }
904
905         return 0;
906 }
907
908 static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
909                                        struct dsa_notifier_tag_proto_info *info)
910 {
911         const struct dsa_device_ops *tag_ops = info->tag_ops;
912         struct dsa_port *dp, *cpu_dp;
913         int err;
914
915         if (!ds->ops->change_tag_protocol)
916                 return -EOPNOTSUPP;
917
918         ASSERT_RTNL();
919
920         dsa_switch_for_each_cpu_port(cpu_dp, ds) {
921                 err = ds->ops->change_tag_protocol(ds, cpu_dp->index,
922                                                    tag_ops->proto);
923                 if (err)
924                         return err;
925
926                 dsa_port_set_tag_protocol(cpu_dp, tag_ops);
927         }
928
929         /* Now that changing the tag protocol can no longer fail, let's update
930          * the remaining bits which are "duplicated for faster access", and the
931          * bits that depend on the tagger, such as the MTU.
932          */
933         dsa_switch_for_each_user_port(dp, ds) {
934                 struct net_device *slave = dp->slave;
935
936                 dsa_slave_setup_tagger(slave);
937
938                 /* rtnl_mutex is held in dsa_tree_change_tag_proto */
939                 dsa_slave_change_mtu(slave, slave->mtu);
940         }
941
942         return 0;
943 }
944
945 /* We use the same cross-chip notifiers to inform both the tagger side, as well
946  * as the switch side, of connection and disconnection events.
947  * Since ds->tagger_data is owned by the tagger, it isn't a hard error if the
948  * switch side doesn't support connecting to this tagger, and therefore, the
949  * fact that we don't disconnect the tagger side doesn't constitute a memory
950  * leak: the tagger will still operate with persistent per-switch memory, just
951  * with the switch side unconnected to it. What does constitute a hard error is
952  * when the switch side supports connecting but fails.
953  */
954 static int
955 dsa_switch_connect_tag_proto(struct dsa_switch *ds,
956                              struct dsa_notifier_tag_proto_info *info)
957 {
958         const struct dsa_device_ops *tag_ops = info->tag_ops;
959         int err;
960
961         /* Notify the new tagger about the connection to this switch */
962         if (tag_ops->connect) {
963                 err = tag_ops->connect(ds);
964                 if (err)
965                         return err;
966         }
967
968         if (!ds->ops->connect_tag_protocol)
969                 return -EOPNOTSUPP;
970
971         /* Notify the switch about the connection to the new tagger */
972         err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
973         if (err) {
974                 /* Revert the new tagger's connection to this tree */
975                 if (tag_ops->disconnect)
976                         tag_ops->disconnect(ds);
977                 return err;
978         }
979
980         return 0;
981 }
982
983 static int
984 dsa_switch_disconnect_tag_proto(struct dsa_switch *ds,
985                                 struct dsa_notifier_tag_proto_info *info)
986 {
987         const struct dsa_device_ops *tag_ops = info->tag_ops;
988
989         /* Notify the tagger about the disconnection from this switch */
990         if (tag_ops->disconnect && ds->tagger_data)
991                 tag_ops->disconnect(ds);
992
993         /* No need to notify the switch, since it shouldn't have any
994          * resources to tear down
995          */
996         return 0;
997 }
998
999 static int
1000 dsa_switch_master_state_change(struct dsa_switch *ds,
1001                                struct dsa_notifier_master_state_info *info)
1002 {
1003         if (!ds->ops->master_state_change)
1004                 return 0;
1005
1006         ds->ops->master_state_change(ds, info->master, info->operational);
1007
1008         return 0;
1009 }
1010
1011 static int dsa_switch_event(struct notifier_block *nb,
1012                             unsigned long event, void *info)
1013 {
1014         struct dsa_switch *ds = container_of(nb, struct dsa_switch, nb);
1015         int err;
1016
1017         switch (event) {
1018         case DSA_NOTIFIER_AGEING_TIME:
1019                 err = dsa_switch_ageing_time(ds, info);
1020                 break;
1021         case DSA_NOTIFIER_BRIDGE_JOIN:
1022                 err = dsa_switch_bridge_join(ds, info);
1023                 break;
1024         case DSA_NOTIFIER_BRIDGE_LEAVE:
1025                 err = dsa_switch_bridge_leave(ds, info);
1026                 break;
1027         case DSA_NOTIFIER_FDB_ADD:
1028                 err = dsa_switch_fdb_add(ds, info);
1029                 break;
1030         case DSA_NOTIFIER_FDB_DEL:
1031                 err = dsa_switch_fdb_del(ds, info);
1032                 break;
1033         case DSA_NOTIFIER_HOST_FDB_ADD:
1034                 err = dsa_switch_host_fdb_add(ds, info);
1035                 break;
1036         case DSA_NOTIFIER_HOST_FDB_DEL:
1037                 err = dsa_switch_host_fdb_del(ds, info);
1038                 break;
1039         case DSA_NOTIFIER_LAG_FDB_ADD:
1040                 err = dsa_switch_lag_fdb_add(ds, info);
1041                 break;
1042         case DSA_NOTIFIER_LAG_FDB_DEL:
1043                 err = dsa_switch_lag_fdb_del(ds, info);
1044                 break;
1045         case DSA_NOTIFIER_LAG_CHANGE:
1046                 err = dsa_switch_lag_change(ds, info);
1047                 break;
1048         case DSA_NOTIFIER_LAG_JOIN:
1049                 err = dsa_switch_lag_join(ds, info);
1050                 break;
1051         case DSA_NOTIFIER_LAG_LEAVE:
1052                 err = dsa_switch_lag_leave(ds, info);
1053                 break;
1054         case DSA_NOTIFIER_MDB_ADD:
1055                 err = dsa_switch_mdb_add(ds, info);
1056                 break;
1057         case DSA_NOTIFIER_MDB_DEL:
1058                 err = dsa_switch_mdb_del(ds, info);
1059                 break;
1060         case DSA_NOTIFIER_HOST_MDB_ADD:
1061                 err = dsa_switch_host_mdb_add(ds, info);
1062                 break;
1063         case DSA_NOTIFIER_HOST_MDB_DEL:
1064                 err = dsa_switch_host_mdb_del(ds, info);
1065                 break;
1066         case DSA_NOTIFIER_VLAN_ADD:
1067                 err = dsa_switch_vlan_add(ds, info);
1068                 break;
1069         case DSA_NOTIFIER_VLAN_DEL:
1070                 err = dsa_switch_vlan_del(ds, info);
1071                 break;
1072         case DSA_NOTIFIER_HOST_VLAN_ADD:
1073                 err = dsa_switch_host_vlan_add(ds, info);
1074                 break;
1075         case DSA_NOTIFIER_HOST_VLAN_DEL:
1076                 err = dsa_switch_host_vlan_del(ds, info);
1077                 break;
1078         case DSA_NOTIFIER_MTU:
1079                 err = dsa_switch_mtu(ds, info);
1080                 break;
1081         case DSA_NOTIFIER_TAG_PROTO:
1082                 err = dsa_switch_change_tag_proto(ds, info);
1083                 break;
1084         case DSA_NOTIFIER_TAG_PROTO_CONNECT:
1085                 err = dsa_switch_connect_tag_proto(ds, info);
1086                 break;
1087         case DSA_NOTIFIER_TAG_PROTO_DISCONNECT:
1088                 err = dsa_switch_disconnect_tag_proto(ds, info);
1089                 break;
1090         case DSA_NOTIFIER_TAG_8021Q_VLAN_ADD:
1091                 err = dsa_switch_tag_8021q_vlan_add(ds, info);
1092                 break;
1093         case DSA_NOTIFIER_TAG_8021Q_VLAN_DEL:
1094                 err = dsa_switch_tag_8021q_vlan_del(ds, info);
1095                 break;
1096         case DSA_NOTIFIER_MASTER_STATE_CHANGE:
1097                 err = dsa_switch_master_state_change(ds, info);
1098                 break;
1099         default:
1100                 err = -EOPNOTSUPP;
1101                 break;
1102         }
1103
1104         if (err)
1105                 dev_dbg(ds->dev, "breaking chain for DSA event %lu (%d)\n",
1106                         event, err);
1107
1108         return notifier_from_errno(err);
1109 }
1110
1111 int dsa_switch_register_notifier(struct dsa_switch *ds)
1112 {
1113         ds->nb.notifier_call = dsa_switch_event;
1114
1115         return raw_notifier_chain_register(&ds->dst->nh, &ds->nb);
1116 }
1117
1118 void dsa_switch_unregister_notifier(struct dsa_switch *ds)
1119 {
1120         int err;
1121
1122         err = raw_notifier_chain_unregister(&ds->dst->nh, &ds->nb);
1123         if (err)
1124                 dev_err(ds->dev, "failed to unregister notifier (%d)\n", err);
1125 }