]> Git Repo - linux.git/blob - net/dsa/dsa2.c
net/smc: add sysctl interface for SMC
[linux.git] / net / dsa / dsa2.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  * Copyright (c) 2013 Florian Fainelli <[email protected]>
6  * Copyright (c) 2016 Andrew Lunn <[email protected]>
7  */
8
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/of.h>
16 #include <linux/of_net.h>
17 #include <net/devlink.h>
18 #include <net/sch_generic.h>
19
20 #include "dsa_priv.h"
21
22 static DEFINE_MUTEX(dsa2_mutex);
23 LIST_HEAD(dsa_tree_list);
24
25 /* Track the bridges with forwarding offload enabled */
26 static unsigned long dsa_fwd_offloading_bridges;
27
28 /**
29  * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
30  * @dst: collection of struct dsa_switch devices to notify.
31  * @e: event, must be of type DSA_NOTIFIER_*
32  * @v: event-specific value.
33  *
34  * Given a struct dsa_switch_tree, this can be used to run a function once for
35  * each member DSA switch. The other alternative of traversing the tree is only
36  * through its ports list, which does not uniquely list the switches.
37  */
38 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
39 {
40         struct raw_notifier_head *nh = &dst->nh;
41         int err;
42
43         err = raw_notifier_call_chain(nh, e, v);
44
45         return notifier_to_errno(err);
46 }
47
48 /**
49  * dsa_broadcast - Notify all DSA trees in the system.
50  * @e: event, must be of type DSA_NOTIFIER_*
51  * @v: event-specific value.
52  *
53  * Can be used to notify the switching fabric of events such as cross-chip
54  * bridging between disjoint trees (such as islands of tagger-compatible
55  * switches bridged by an incompatible middle switch).
56  *
57  * WARNING: this function is not reliable during probe time, because probing
58  * between trees is asynchronous and not all DSA trees might have probed.
59  */
60 int dsa_broadcast(unsigned long e, void *v)
61 {
62         struct dsa_switch_tree *dst;
63         int err = 0;
64
65         list_for_each_entry(dst, &dsa_tree_list, list) {
66                 err = dsa_tree_notify(dst, e, v);
67                 if (err)
68                         break;
69         }
70
71         return err;
72 }
73
74 /**
75  * dsa_lag_map() - Map LAG structure to a linear LAG array
76  * @dst: Tree in which to record the mapping.
77  * @lag: LAG structure that is to be mapped to the tree's array.
78  *
79  * dsa_lag_id/dsa_lag_by_id can then be used to translate between the
80  * two spaces. The size of the mapping space is determined by the
81  * driver by setting ds->num_lag_ids. It is perfectly legal to leave
82  * it unset if it is not needed, in which case these functions become
83  * no-ops.
84  */
85 void dsa_lag_map(struct dsa_switch_tree *dst, struct dsa_lag *lag)
86 {
87         unsigned int id;
88
89         for (id = 1; id <= dst->lags_len; id++) {
90                 if (!dsa_lag_by_id(dst, id)) {
91                         dst->lags[id - 1] = lag;
92                         lag->id = id;
93                         return;
94                 }
95         }
96
97         /* No IDs left, which is OK. Some drivers do not need it. The
98          * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
99          * returns an error for this device when joining the LAG. The
100          * driver can then return -EOPNOTSUPP back to DSA, which will
101          * fall back to a software LAG.
102          */
103 }
104
105 /**
106  * dsa_lag_unmap() - Remove a LAG ID mapping
107  * @dst: Tree in which the mapping is recorded.
108  * @lag: LAG structure that was mapped.
109  *
110  * As there may be multiple users of the mapping, it is only removed
111  * if there are no other references to it.
112  */
113 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct dsa_lag *lag)
114 {
115         unsigned int id;
116
117         dsa_lags_foreach_id(id, dst) {
118                 if (dsa_lag_by_id(dst, id) == lag) {
119                         dst->lags[id - 1] = NULL;
120                         lag->id = 0;
121                         break;
122                 }
123         }
124 }
125
126 struct dsa_lag *dsa_tree_lag_find(struct dsa_switch_tree *dst,
127                                   const struct net_device *lag_dev)
128 {
129         struct dsa_port *dp;
130
131         list_for_each_entry(dp, &dst->ports, list)
132                 if (dsa_port_lag_dev_get(dp) == lag_dev)
133                         return dp->lag;
134
135         return NULL;
136 }
137
138 struct dsa_bridge *dsa_tree_bridge_find(struct dsa_switch_tree *dst,
139                                         const struct net_device *br)
140 {
141         struct dsa_port *dp;
142
143         list_for_each_entry(dp, &dst->ports, list)
144                 if (dsa_port_bridge_dev_get(dp) == br)
145                         return dp->bridge;
146
147         return NULL;
148 }
149
150 static int dsa_bridge_num_find(const struct net_device *bridge_dev)
151 {
152         struct dsa_switch_tree *dst;
153
154         list_for_each_entry(dst, &dsa_tree_list, list) {
155                 struct dsa_bridge *bridge;
156
157                 bridge = dsa_tree_bridge_find(dst, bridge_dev);
158                 if (bridge)
159                         return bridge->num;
160         }
161
162         return 0;
163 }
164
165 unsigned int dsa_bridge_num_get(const struct net_device *bridge_dev, int max)
166 {
167         unsigned int bridge_num = dsa_bridge_num_find(bridge_dev);
168
169         /* Switches without FDB isolation support don't get unique
170          * bridge numbering
171          */
172         if (!max)
173                 return 0;
174
175         if (!bridge_num) {
176                 /* First port that requests FDB isolation or TX forwarding
177                  * offload for this bridge
178                  */
179                 bridge_num = find_next_zero_bit(&dsa_fwd_offloading_bridges,
180                                                 DSA_MAX_NUM_OFFLOADING_BRIDGES,
181                                                 1);
182                 if (bridge_num >= max)
183                         return 0;
184
185                 set_bit(bridge_num, &dsa_fwd_offloading_bridges);
186         }
187
188         return bridge_num;
189 }
190
191 void dsa_bridge_num_put(const struct net_device *bridge_dev,
192                         unsigned int bridge_num)
193 {
194         /* Since we refcount bridges, we know that when we call this function
195          * it is no longer in use, so we can just go ahead and remove it from
196          * the bit mask.
197          */
198         clear_bit(bridge_num, &dsa_fwd_offloading_bridges);
199 }
200
201 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
202 {
203         struct dsa_switch_tree *dst;
204         struct dsa_port *dp;
205
206         list_for_each_entry(dst, &dsa_tree_list, list) {
207                 if (dst->index != tree_index)
208                         continue;
209
210                 list_for_each_entry(dp, &dst->ports, list) {
211                         if (dp->ds->index != sw_index)
212                                 continue;
213
214                         return dp->ds;
215                 }
216         }
217
218         return NULL;
219 }
220 EXPORT_SYMBOL_GPL(dsa_switch_find);
221
222 static struct dsa_switch_tree *dsa_tree_find(int index)
223 {
224         struct dsa_switch_tree *dst;
225
226         list_for_each_entry(dst, &dsa_tree_list, list)
227                 if (dst->index == index)
228                         return dst;
229
230         return NULL;
231 }
232
233 static struct dsa_switch_tree *dsa_tree_alloc(int index)
234 {
235         struct dsa_switch_tree *dst;
236
237         dst = kzalloc(sizeof(*dst), GFP_KERNEL);
238         if (!dst)
239                 return NULL;
240
241         dst->index = index;
242
243         INIT_LIST_HEAD(&dst->rtable);
244
245         INIT_LIST_HEAD(&dst->ports);
246
247         INIT_LIST_HEAD(&dst->list);
248         list_add_tail(&dst->list, &dsa_tree_list);
249
250         kref_init(&dst->refcount);
251
252         return dst;
253 }
254
255 static void dsa_tree_free(struct dsa_switch_tree *dst)
256 {
257         if (dst->tag_ops)
258                 dsa_tag_driver_put(dst->tag_ops);
259         list_del(&dst->list);
260         kfree(dst);
261 }
262
263 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
264 {
265         if (dst)
266                 kref_get(&dst->refcount);
267
268         return dst;
269 }
270
271 static struct dsa_switch_tree *dsa_tree_touch(int index)
272 {
273         struct dsa_switch_tree *dst;
274
275         dst = dsa_tree_find(index);
276         if (dst)
277                 return dsa_tree_get(dst);
278         else
279                 return dsa_tree_alloc(index);
280 }
281
282 static void dsa_tree_release(struct kref *ref)
283 {
284         struct dsa_switch_tree *dst;
285
286         dst = container_of(ref, struct dsa_switch_tree, refcount);
287
288         dsa_tree_free(dst);
289 }
290
291 static void dsa_tree_put(struct dsa_switch_tree *dst)
292 {
293         if (dst)
294                 kref_put(&dst->refcount, dsa_tree_release);
295 }
296
297 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
298                                                    struct device_node *dn)
299 {
300         struct dsa_port *dp;
301
302         list_for_each_entry(dp, &dst->ports, list)
303                 if (dp->dn == dn)
304                         return dp;
305
306         return NULL;
307 }
308
309 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
310                                        struct dsa_port *link_dp)
311 {
312         struct dsa_switch *ds = dp->ds;
313         struct dsa_switch_tree *dst;
314         struct dsa_link *dl;
315
316         dst = ds->dst;
317
318         list_for_each_entry(dl, &dst->rtable, list)
319                 if (dl->dp == dp && dl->link_dp == link_dp)
320                         return dl;
321
322         dl = kzalloc(sizeof(*dl), GFP_KERNEL);
323         if (!dl)
324                 return NULL;
325
326         dl->dp = dp;
327         dl->link_dp = link_dp;
328
329         INIT_LIST_HEAD(&dl->list);
330         list_add_tail(&dl->list, &dst->rtable);
331
332         return dl;
333 }
334
335 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
336 {
337         struct dsa_switch *ds = dp->ds;
338         struct dsa_switch_tree *dst = ds->dst;
339         struct device_node *dn = dp->dn;
340         struct of_phandle_iterator it;
341         struct dsa_port *link_dp;
342         struct dsa_link *dl;
343         int err;
344
345         of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
346                 link_dp = dsa_tree_find_port_by_node(dst, it.node);
347                 if (!link_dp) {
348                         of_node_put(it.node);
349                         return false;
350                 }
351
352                 dl = dsa_link_touch(dp, link_dp);
353                 if (!dl) {
354                         of_node_put(it.node);
355                         return false;
356                 }
357         }
358
359         return true;
360 }
361
362 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
363 {
364         bool complete = true;
365         struct dsa_port *dp;
366
367         list_for_each_entry(dp, &dst->ports, list) {
368                 if (dsa_port_is_dsa(dp)) {
369                         complete = dsa_port_setup_routing_table(dp);
370                         if (!complete)
371                                 break;
372                 }
373         }
374
375         return complete;
376 }
377
378 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
379 {
380         struct dsa_port *dp;
381
382         list_for_each_entry(dp, &dst->ports, list)
383                 if (dsa_port_is_cpu(dp))
384                         return dp;
385
386         return NULL;
387 }
388
389 /* Assign the default CPU port (the first one in the tree) to all ports of the
390  * fabric which don't already have one as part of their own switch.
391  */
392 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
393 {
394         struct dsa_port *cpu_dp, *dp;
395
396         cpu_dp = dsa_tree_find_first_cpu(dst);
397         if (!cpu_dp) {
398                 pr_err("DSA: tree %d has no CPU port\n", dst->index);
399                 return -EINVAL;
400         }
401
402         list_for_each_entry(dp, &dst->ports, list) {
403                 if (dp->cpu_dp)
404                         continue;
405
406                 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
407                         dp->cpu_dp = cpu_dp;
408         }
409
410         return 0;
411 }
412
413 /* Perform initial assignment of CPU ports to user ports and DSA links in the
414  * fabric, giving preference to CPU ports local to each switch. Default to
415  * using the first CPU port in the switch tree if the port does not have a CPU
416  * port local to this switch.
417  */
418 static int dsa_tree_setup_cpu_ports(struct dsa_switch_tree *dst)
419 {
420         struct dsa_port *cpu_dp, *dp;
421
422         list_for_each_entry(cpu_dp, &dst->ports, list) {
423                 if (!dsa_port_is_cpu(cpu_dp))
424                         continue;
425
426                 /* Prefer a local CPU port */
427                 dsa_switch_for_each_port(dp, cpu_dp->ds) {
428                         /* Prefer the first local CPU port found */
429                         if (dp->cpu_dp)
430                                 continue;
431
432                         if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
433                                 dp->cpu_dp = cpu_dp;
434                 }
435         }
436
437         return dsa_tree_setup_default_cpu(dst);
438 }
439
440 static void dsa_tree_teardown_cpu_ports(struct dsa_switch_tree *dst)
441 {
442         struct dsa_port *dp;
443
444         list_for_each_entry(dp, &dst->ports, list)
445                 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
446                         dp->cpu_dp = NULL;
447 }
448
449 static int dsa_port_setup(struct dsa_port *dp)
450 {
451         struct devlink_port *dlp = &dp->devlink_port;
452         bool dsa_port_link_registered = false;
453         struct dsa_switch *ds = dp->ds;
454         bool dsa_port_enabled = false;
455         int err = 0;
456
457         if (dp->setup)
458                 return 0;
459
460         mutex_init(&dp->addr_lists_lock);
461         mutex_init(&dp->vlans_lock);
462         INIT_LIST_HEAD(&dp->fdbs);
463         INIT_LIST_HEAD(&dp->mdbs);
464         INIT_LIST_HEAD(&dp->vlans);
465
466         if (ds->ops->port_setup) {
467                 err = ds->ops->port_setup(ds, dp->index);
468                 if (err)
469                         return err;
470         }
471
472         switch (dp->type) {
473         case DSA_PORT_TYPE_UNUSED:
474                 dsa_port_disable(dp);
475                 break;
476         case DSA_PORT_TYPE_CPU:
477                 err = dsa_port_link_register_of(dp);
478                 if (err)
479                         break;
480                 dsa_port_link_registered = true;
481
482                 err = dsa_port_enable(dp, NULL);
483                 if (err)
484                         break;
485                 dsa_port_enabled = true;
486
487                 break;
488         case DSA_PORT_TYPE_DSA:
489                 err = dsa_port_link_register_of(dp);
490                 if (err)
491                         break;
492                 dsa_port_link_registered = true;
493
494                 err = dsa_port_enable(dp, NULL);
495                 if (err)
496                         break;
497                 dsa_port_enabled = true;
498
499                 break;
500         case DSA_PORT_TYPE_USER:
501                 of_get_mac_address(dp->dn, dp->mac);
502                 err = dsa_slave_create(dp);
503                 if (err)
504                         break;
505
506                 devlink_port_type_eth_set(dlp, dp->slave);
507                 break;
508         }
509
510         if (err && dsa_port_enabled)
511                 dsa_port_disable(dp);
512         if (err && dsa_port_link_registered)
513                 dsa_port_link_unregister_of(dp);
514         if (err) {
515                 if (ds->ops->port_teardown)
516                         ds->ops->port_teardown(ds, dp->index);
517                 return err;
518         }
519
520         dp->setup = true;
521
522         return 0;
523 }
524
525 static int dsa_port_devlink_setup(struct dsa_port *dp)
526 {
527         struct devlink_port *dlp = &dp->devlink_port;
528         struct dsa_switch_tree *dst = dp->ds->dst;
529         struct devlink_port_attrs attrs = {};
530         struct devlink *dl = dp->ds->devlink;
531         const unsigned char *id;
532         unsigned char len;
533         int err;
534
535         id = (const unsigned char *)&dst->index;
536         len = sizeof(dst->index);
537
538         attrs.phys.port_number = dp->index;
539         memcpy(attrs.switch_id.id, id, len);
540         attrs.switch_id.id_len = len;
541         memset(dlp, 0, sizeof(*dlp));
542
543         switch (dp->type) {
544         case DSA_PORT_TYPE_UNUSED:
545                 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
546                 break;
547         case DSA_PORT_TYPE_CPU:
548                 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
549                 break;
550         case DSA_PORT_TYPE_DSA:
551                 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
552                 break;
553         case DSA_PORT_TYPE_USER:
554                 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
555                 break;
556         }
557
558         devlink_port_attrs_set(dlp, &attrs);
559         err = devlink_port_register(dl, dlp, dp->index);
560
561         if (!err)
562                 dp->devlink_port_setup = true;
563
564         return err;
565 }
566
567 static void dsa_port_teardown(struct dsa_port *dp)
568 {
569         struct devlink_port *dlp = &dp->devlink_port;
570         struct dsa_switch *ds = dp->ds;
571         struct dsa_mac_addr *a, *tmp;
572         struct net_device *slave;
573         struct dsa_vlan *v, *n;
574
575         if (!dp->setup)
576                 return;
577
578         if (ds->ops->port_teardown)
579                 ds->ops->port_teardown(ds, dp->index);
580
581         devlink_port_type_clear(dlp);
582
583         switch (dp->type) {
584         case DSA_PORT_TYPE_UNUSED:
585                 break;
586         case DSA_PORT_TYPE_CPU:
587                 dsa_port_disable(dp);
588                 dsa_port_link_unregister_of(dp);
589                 break;
590         case DSA_PORT_TYPE_DSA:
591                 dsa_port_disable(dp);
592                 dsa_port_link_unregister_of(dp);
593                 break;
594         case DSA_PORT_TYPE_USER:
595                 slave = dp->slave;
596
597                 if (slave) {
598                         dp->slave = NULL;
599                         dsa_slave_destroy(slave);
600                 }
601                 break;
602         }
603
604         list_for_each_entry_safe(a, tmp, &dp->fdbs, list) {
605                 list_del(&a->list);
606                 kfree(a);
607         }
608
609         list_for_each_entry_safe(a, tmp, &dp->mdbs, list) {
610                 list_del(&a->list);
611                 kfree(a);
612         }
613
614         list_for_each_entry_safe(v, n, &dp->vlans, list) {
615                 list_del(&v->list);
616                 kfree(v);
617         }
618
619         dp->setup = false;
620 }
621
622 static void dsa_port_devlink_teardown(struct dsa_port *dp)
623 {
624         struct devlink_port *dlp = &dp->devlink_port;
625
626         if (dp->devlink_port_setup)
627                 devlink_port_unregister(dlp);
628         dp->devlink_port_setup = false;
629 }
630
631 /* Destroy the current devlink port, and create a new one which has the UNUSED
632  * flavour. At this point, any call to ds->ops->port_setup has been already
633  * balanced out by a call to ds->ops->port_teardown, so we know that any
634  * devlink port regions the driver had are now unregistered. We then call its
635  * ds->ops->port_setup again, in order for the driver to re-create them on the
636  * new devlink port.
637  */
638 static int dsa_port_reinit_as_unused(struct dsa_port *dp)
639 {
640         struct dsa_switch *ds = dp->ds;
641         int err;
642
643         dsa_port_devlink_teardown(dp);
644         dp->type = DSA_PORT_TYPE_UNUSED;
645         err = dsa_port_devlink_setup(dp);
646         if (err)
647                 return err;
648
649         if (ds->ops->port_setup) {
650                 /* On error, leave the devlink port registered,
651                  * dsa_switch_teardown will clean it up later.
652                  */
653                 err = ds->ops->port_setup(ds, dp->index);
654                 if (err)
655                         return err;
656         }
657
658         return 0;
659 }
660
661 static int dsa_devlink_info_get(struct devlink *dl,
662                                 struct devlink_info_req *req,
663                                 struct netlink_ext_ack *extack)
664 {
665         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
666
667         if (ds->ops->devlink_info_get)
668                 return ds->ops->devlink_info_get(ds, req, extack);
669
670         return -EOPNOTSUPP;
671 }
672
673 static int dsa_devlink_sb_pool_get(struct devlink *dl,
674                                    unsigned int sb_index, u16 pool_index,
675                                    struct devlink_sb_pool_info *pool_info)
676 {
677         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
678
679         if (!ds->ops->devlink_sb_pool_get)
680                 return -EOPNOTSUPP;
681
682         return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
683                                             pool_info);
684 }
685
686 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
687                                    u16 pool_index, u32 size,
688                                    enum devlink_sb_threshold_type threshold_type,
689                                    struct netlink_ext_ack *extack)
690 {
691         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
692
693         if (!ds->ops->devlink_sb_pool_set)
694                 return -EOPNOTSUPP;
695
696         return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
697                                             threshold_type, extack);
698 }
699
700 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
701                                         unsigned int sb_index, u16 pool_index,
702                                         u32 *p_threshold)
703 {
704         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
705         int port = dsa_devlink_port_to_port(dlp);
706
707         if (!ds->ops->devlink_sb_port_pool_get)
708                 return -EOPNOTSUPP;
709
710         return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
711                                                  pool_index, p_threshold);
712 }
713
714 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
715                                         unsigned int sb_index, u16 pool_index,
716                                         u32 threshold,
717                                         struct netlink_ext_ack *extack)
718 {
719         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
720         int port = dsa_devlink_port_to_port(dlp);
721
722         if (!ds->ops->devlink_sb_port_pool_set)
723                 return -EOPNOTSUPP;
724
725         return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
726                                                  pool_index, threshold, extack);
727 }
728
729 static int
730 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
731                                 unsigned int sb_index, u16 tc_index,
732                                 enum devlink_sb_pool_type pool_type,
733                                 u16 *p_pool_index, u32 *p_threshold)
734 {
735         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
736         int port = dsa_devlink_port_to_port(dlp);
737
738         if (!ds->ops->devlink_sb_tc_pool_bind_get)
739                 return -EOPNOTSUPP;
740
741         return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
742                                                     tc_index, pool_type,
743                                                     p_pool_index, p_threshold);
744 }
745
746 static int
747 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
748                                 unsigned int sb_index, u16 tc_index,
749                                 enum devlink_sb_pool_type pool_type,
750                                 u16 pool_index, u32 threshold,
751                                 struct netlink_ext_ack *extack)
752 {
753         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
754         int port = dsa_devlink_port_to_port(dlp);
755
756         if (!ds->ops->devlink_sb_tc_pool_bind_set)
757                 return -EOPNOTSUPP;
758
759         return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
760                                                     tc_index, pool_type,
761                                                     pool_index, threshold,
762                                                     extack);
763 }
764
765 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
766                                        unsigned int sb_index)
767 {
768         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
769
770         if (!ds->ops->devlink_sb_occ_snapshot)
771                 return -EOPNOTSUPP;
772
773         return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
774 }
775
776 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
777                                         unsigned int sb_index)
778 {
779         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
780
781         if (!ds->ops->devlink_sb_occ_max_clear)
782                 return -EOPNOTSUPP;
783
784         return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
785 }
786
787 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
788                                             unsigned int sb_index,
789                                             u16 pool_index, u32 *p_cur,
790                                             u32 *p_max)
791 {
792         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
793         int port = dsa_devlink_port_to_port(dlp);
794
795         if (!ds->ops->devlink_sb_occ_port_pool_get)
796                 return -EOPNOTSUPP;
797
798         return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
799                                                      pool_index, p_cur, p_max);
800 }
801
802 static int
803 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
804                                     unsigned int sb_index, u16 tc_index,
805                                     enum devlink_sb_pool_type pool_type,
806                                     u32 *p_cur, u32 *p_max)
807 {
808         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
809         int port = dsa_devlink_port_to_port(dlp);
810
811         if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
812                 return -EOPNOTSUPP;
813
814         return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
815                                                         sb_index, tc_index,
816                                                         pool_type, p_cur,
817                                                         p_max);
818 }
819
820 static const struct devlink_ops dsa_devlink_ops = {
821         .info_get                       = dsa_devlink_info_get,
822         .sb_pool_get                    = dsa_devlink_sb_pool_get,
823         .sb_pool_set                    = dsa_devlink_sb_pool_set,
824         .sb_port_pool_get               = dsa_devlink_sb_port_pool_get,
825         .sb_port_pool_set               = dsa_devlink_sb_port_pool_set,
826         .sb_tc_pool_bind_get            = dsa_devlink_sb_tc_pool_bind_get,
827         .sb_tc_pool_bind_set            = dsa_devlink_sb_tc_pool_bind_set,
828         .sb_occ_snapshot                = dsa_devlink_sb_occ_snapshot,
829         .sb_occ_max_clear               = dsa_devlink_sb_occ_max_clear,
830         .sb_occ_port_pool_get           = dsa_devlink_sb_occ_port_pool_get,
831         .sb_occ_tc_port_bind_get        = dsa_devlink_sb_occ_tc_port_bind_get,
832 };
833
834 static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
835 {
836         const struct dsa_device_ops *tag_ops = ds->dst->tag_ops;
837         struct dsa_switch_tree *dst = ds->dst;
838         struct dsa_port *cpu_dp;
839         int err;
840
841         if (tag_ops->proto == dst->default_proto)
842                 goto connect;
843
844         dsa_switch_for_each_cpu_port(cpu_dp, ds) {
845                 rtnl_lock();
846                 err = ds->ops->change_tag_protocol(ds, cpu_dp->index,
847                                                    tag_ops->proto);
848                 rtnl_unlock();
849                 if (err) {
850                         dev_err(ds->dev, "Unable to use tag protocol \"%s\": %pe\n",
851                                 tag_ops->name, ERR_PTR(err));
852                         return err;
853                 }
854         }
855
856 connect:
857         if (tag_ops->connect) {
858                 err = tag_ops->connect(ds);
859                 if (err)
860                         return err;
861         }
862
863         if (ds->ops->connect_tag_protocol) {
864                 err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
865                 if (err) {
866                         dev_err(ds->dev,
867                                 "Unable to connect to tag protocol \"%s\": %pe\n",
868                                 tag_ops->name, ERR_PTR(err));
869                         goto disconnect;
870                 }
871         }
872
873         return 0;
874
875 disconnect:
876         if (tag_ops->disconnect)
877                 tag_ops->disconnect(ds);
878
879         return err;
880 }
881
882 static int dsa_switch_setup(struct dsa_switch *ds)
883 {
884         struct dsa_devlink_priv *dl_priv;
885         struct dsa_port *dp;
886         int err;
887
888         if (ds->setup)
889                 return 0;
890
891         /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
892          * driver and before ops->setup() has run, since the switch drivers and
893          * the slave MDIO bus driver rely on these values for probing PHY
894          * devices or not
895          */
896         ds->phys_mii_mask |= dsa_user_ports(ds);
897
898         /* Add the switch to devlink before calling setup, so that setup can
899          * add dpipe tables
900          */
901         ds->devlink =
902                 devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
903         if (!ds->devlink)
904                 return -ENOMEM;
905         dl_priv = devlink_priv(ds->devlink);
906         dl_priv->ds = ds;
907
908         /* Setup devlink port instances now, so that the switch
909          * setup() can register regions etc, against the ports
910          */
911         dsa_switch_for_each_port(dp, ds) {
912                 err = dsa_port_devlink_setup(dp);
913                 if (err)
914                         goto unregister_devlink_ports;
915         }
916
917         err = dsa_switch_register_notifier(ds);
918         if (err)
919                 goto unregister_devlink_ports;
920
921         ds->configure_vlan_while_not_filtering = true;
922
923         err = ds->ops->setup(ds);
924         if (err < 0)
925                 goto unregister_notifier;
926
927         err = dsa_switch_setup_tag_protocol(ds);
928         if (err)
929                 goto teardown;
930
931         if (!ds->slave_mii_bus && ds->ops->phy_read) {
932                 ds->slave_mii_bus = mdiobus_alloc();
933                 if (!ds->slave_mii_bus) {
934                         err = -ENOMEM;
935                         goto teardown;
936                 }
937
938                 dsa_slave_mii_bus_init(ds);
939
940                 err = mdiobus_register(ds->slave_mii_bus);
941                 if (err < 0)
942                         goto free_slave_mii_bus;
943         }
944
945         ds->setup = true;
946         devlink_register(ds->devlink);
947         return 0;
948
949 free_slave_mii_bus:
950         if (ds->slave_mii_bus && ds->ops->phy_read)
951                 mdiobus_free(ds->slave_mii_bus);
952 teardown:
953         if (ds->ops->teardown)
954                 ds->ops->teardown(ds);
955 unregister_notifier:
956         dsa_switch_unregister_notifier(ds);
957 unregister_devlink_ports:
958         dsa_switch_for_each_port(dp, ds)
959                 dsa_port_devlink_teardown(dp);
960         devlink_free(ds->devlink);
961         ds->devlink = NULL;
962         return err;
963 }
964
965 static void dsa_switch_teardown(struct dsa_switch *ds)
966 {
967         struct dsa_port *dp;
968
969         if (!ds->setup)
970                 return;
971
972         if (ds->devlink)
973                 devlink_unregister(ds->devlink);
974
975         if (ds->slave_mii_bus && ds->ops->phy_read) {
976                 mdiobus_unregister(ds->slave_mii_bus);
977                 mdiobus_free(ds->slave_mii_bus);
978                 ds->slave_mii_bus = NULL;
979         }
980
981         if (ds->ops->teardown)
982                 ds->ops->teardown(ds);
983
984         dsa_switch_unregister_notifier(ds);
985
986         if (ds->devlink) {
987                 dsa_switch_for_each_port(dp, ds)
988                         dsa_port_devlink_teardown(dp);
989                 devlink_free(ds->devlink);
990                 ds->devlink = NULL;
991         }
992
993         ds->setup = false;
994 }
995
996 /* First tear down the non-shared, then the shared ports. This ensures that
997  * all work items scheduled by our switchdev handlers for user ports have
998  * completed before we destroy the refcounting kept on the shared ports.
999  */
1000 static void dsa_tree_teardown_ports(struct dsa_switch_tree *dst)
1001 {
1002         struct dsa_port *dp;
1003
1004         list_for_each_entry(dp, &dst->ports, list)
1005                 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp))
1006                         dsa_port_teardown(dp);
1007
1008         dsa_flush_workqueue();
1009
1010         list_for_each_entry(dp, &dst->ports, list)
1011                 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp))
1012                         dsa_port_teardown(dp);
1013 }
1014
1015 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
1016 {
1017         struct dsa_port *dp;
1018
1019         list_for_each_entry(dp, &dst->ports, list)
1020                 dsa_switch_teardown(dp->ds);
1021 }
1022
1023 /* Bring shared ports up first, then non-shared ports */
1024 static int dsa_tree_setup_ports(struct dsa_switch_tree *dst)
1025 {
1026         struct dsa_port *dp;
1027         int err = 0;
1028
1029         list_for_each_entry(dp, &dst->ports, list) {
1030                 if (dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp)) {
1031                         err = dsa_port_setup(dp);
1032                         if (err)
1033                                 goto teardown;
1034                 }
1035         }
1036
1037         list_for_each_entry(dp, &dst->ports, list) {
1038                 if (dsa_port_is_user(dp) || dsa_port_is_unused(dp)) {
1039                         err = dsa_port_setup(dp);
1040                         if (err) {
1041                                 err = dsa_port_reinit_as_unused(dp);
1042                                 if (err)
1043                                         goto teardown;
1044                         }
1045                 }
1046         }
1047
1048         return 0;
1049
1050 teardown:
1051         dsa_tree_teardown_ports(dst);
1052
1053         return err;
1054 }
1055
1056 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
1057 {
1058         struct dsa_port *dp;
1059         int err = 0;
1060
1061         list_for_each_entry(dp, &dst->ports, list) {
1062                 err = dsa_switch_setup(dp->ds);
1063                 if (err) {
1064                         dsa_tree_teardown_switches(dst);
1065                         break;
1066                 }
1067         }
1068
1069         return err;
1070 }
1071
1072 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
1073 {
1074         struct dsa_port *dp;
1075         int err;
1076
1077         rtnl_lock();
1078
1079         list_for_each_entry(dp, &dst->ports, list) {
1080                 if (dsa_port_is_cpu(dp)) {
1081                         struct net_device *master = dp->master;
1082                         bool admin_up = (master->flags & IFF_UP) &&
1083                                         !qdisc_tx_is_noop(master);
1084
1085                         err = dsa_master_setup(master, dp);
1086                         if (err)
1087                                 return err;
1088
1089                         /* Replay master state event */
1090                         dsa_tree_master_admin_state_change(dst, master, admin_up);
1091                         dsa_tree_master_oper_state_change(dst, master,
1092                                                           netif_oper_up(master));
1093                 }
1094         }
1095
1096         rtnl_unlock();
1097
1098         return 0;
1099 }
1100
1101 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
1102 {
1103         struct dsa_port *dp;
1104
1105         rtnl_lock();
1106
1107         list_for_each_entry(dp, &dst->ports, list) {
1108                 if (dsa_port_is_cpu(dp)) {
1109                         struct net_device *master = dp->master;
1110
1111                         /* Synthesizing an "admin down" state is sufficient for
1112                          * the switches to get a notification if the master is
1113                          * currently up and running.
1114                          */
1115                         dsa_tree_master_admin_state_change(dst, master, false);
1116
1117                         dsa_master_teardown(master);
1118                 }
1119         }
1120
1121         rtnl_unlock();
1122 }
1123
1124 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
1125 {
1126         unsigned int len = 0;
1127         struct dsa_port *dp;
1128
1129         list_for_each_entry(dp, &dst->ports, list) {
1130                 if (dp->ds->num_lag_ids > len)
1131                         len = dp->ds->num_lag_ids;
1132         }
1133
1134         if (!len)
1135                 return 0;
1136
1137         dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
1138         if (!dst->lags)
1139                 return -ENOMEM;
1140
1141         dst->lags_len = len;
1142         return 0;
1143 }
1144
1145 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
1146 {
1147         kfree(dst->lags);
1148 }
1149
1150 static int dsa_tree_setup(struct dsa_switch_tree *dst)
1151 {
1152         bool complete;
1153         int err;
1154
1155         if (dst->setup) {
1156                 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
1157                        dst->index);
1158                 return -EEXIST;
1159         }
1160
1161         complete = dsa_tree_setup_routing_table(dst);
1162         if (!complete)
1163                 return 0;
1164
1165         err = dsa_tree_setup_cpu_ports(dst);
1166         if (err)
1167                 return err;
1168
1169         err = dsa_tree_setup_switches(dst);
1170         if (err)
1171                 goto teardown_cpu_ports;
1172
1173         err = dsa_tree_setup_master(dst);
1174         if (err)
1175                 goto teardown_switches;
1176
1177         err = dsa_tree_setup_ports(dst);
1178         if (err)
1179                 goto teardown_master;
1180
1181         err = dsa_tree_setup_lags(dst);
1182         if (err)
1183                 goto teardown_ports;
1184
1185         dst->setup = true;
1186
1187         pr_info("DSA: tree %d setup\n", dst->index);
1188
1189         return 0;
1190
1191 teardown_ports:
1192         dsa_tree_teardown_ports(dst);
1193 teardown_master:
1194         dsa_tree_teardown_master(dst);
1195 teardown_switches:
1196         dsa_tree_teardown_switches(dst);
1197 teardown_cpu_ports:
1198         dsa_tree_teardown_cpu_ports(dst);
1199
1200         return err;
1201 }
1202
1203 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
1204 {
1205         struct dsa_link *dl, *next;
1206
1207         if (!dst->setup)
1208                 return;
1209
1210         dsa_tree_teardown_lags(dst);
1211
1212         dsa_tree_teardown_ports(dst);
1213
1214         dsa_tree_teardown_master(dst);
1215
1216         dsa_tree_teardown_switches(dst);
1217
1218         dsa_tree_teardown_cpu_ports(dst);
1219
1220         list_for_each_entry_safe(dl, next, &dst->rtable, list) {
1221                 list_del(&dl->list);
1222                 kfree(dl);
1223         }
1224
1225         pr_info("DSA: tree %d torn down\n", dst->index);
1226
1227         dst->setup = false;
1228 }
1229
1230 static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
1231                                    const struct dsa_device_ops *tag_ops)
1232 {
1233         const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
1234         struct dsa_notifier_tag_proto_info info;
1235         int err;
1236
1237         dst->tag_ops = tag_ops;
1238
1239         /* Notify the switches from this tree about the connection
1240          * to the new tagger
1241          */
1242         info.tag_ops = tag_ops;
1243         err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
1244         if (err && err != -EOPNOTSUPP)
1245                 goto out_disconnect;
1246
1247         /* Notify the old tagger about the disconnection from this tree */
1248         info.tag_ops = old_tag_ops;
1249         dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1250
1251         return 0;
1252
1253 out_disconnect:
1254         info.tag_ops = tag_ops;
1255         dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
1256         dst->tag_ops = old_tag_ops;
1257
1258         return err;
1259 }
1260
1261 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
1262  * is that all DSA switches within a tree share the same tagger, otherwise
1263  * they would have formed disjoint trees (different "dsa,member" values).
1264  */
1265 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
1266                               struct net_device *master,
1267                               const struct dsa_device_ops *tag_ops,
1268                               const struct dsa_device_ops *old_tag_ops)
1269 {
1270         struct dsa_notifier_tag_proto_info info;
1271         struct dsa_port *dp;
1272         int err = -EBUSY;
1273
1274         if (!rtnl_trylock())
1275                 return restart_syscall();
1276
1277         /* At the moment we don't allow changing the tag protocol under
1278          * traffic. The rtnl_mutex also happens to serialize concurrent
1279          * attempts to change the tagging protocol. If we ever lift the IFF_UP
1280          * restriction, there needs to be another mutex which serializes this.
1281          */
1282         if (master->flags & IFF_UP)
1283                 goto out_unlock;
1284
1285         list_for_each_entry(dp, &dst->ports, list) {
1286                 if (!dsa_port_is_user(dp))
1287                         continue;
1288
1289                 if (dp->slave->flags & IFF_UP)
1290                         goto out_unlock;
1291         }
1292
1293         /* Notify the tag protocol change */
1294         info.tag_ops = tag_ops;
1295         err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1296         if (err)
1297                 return err;
1298
1299         err = dsa_tree_bind_tag_proto(dst, tag_ops);
1300         if (err)
1301                 goto out_unwind_tagger;
1302
1303         rtnl_unlock();
1304
1305         return 0;
1306
1307 out_unwind_tagger:
1308         info.tag_ops = old_tag_ops;
1309         dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1310 out_unlock:
1311         rtnl_unlock();
1312         return err;
1313 }
1314
1315 static void dsa_tree_master_state_change(struct dsa_switch_tree *dst,
1316                                          struct net_device *master)
1317 {
1318         struct dsa_notifier_master_state_info info;
1319         struct dsa_port *cpu_dp = master->dsa_ptr;
1320
1321         info.master = master;
1322         info.operational = dsa_port_master_is_operational(cpu_dp);
1323
1324         dsa_tree_notify(dst, DSA_NOTIFIER_MASTER_STATE_CHANGE, &info);
1325 }
1326
1327 void dsa_tree_master_admin_state_change(struct dsa_switch_tree *dst,
1328                                         struct net_device *master,
1329                                         bool up)
1330 {
1331         struct dsa_port *cpu_dp = master->dsa_ptr;
1332         bool notify = false;
1333
1334         if ((dsa_port_master_is_operational(cpu_dp)) !=
1335             (up && cpu_dp->master_oper_up))
1336                 notify = true;
1337
1338         cpu_dp->master_admin_up = up;
1339
1340         if (notify)
1341                 dsa_tree_master_state_change(dst, master);
1342 }
1343
1344 void dsa_tree_master_oper_state_change(struct dsa_switch_tree *dst,
1345                                        struct net_device *master,
1346                                        bool up)
1347 {
1348         struct dsa_port *cpu_dp = master->dsa_ptr;
1349         bool notify = false;
1350
1351         if ((dsa_port_master_is_operational(cpu_dp)) !=
1352             (cpu_dp->master_admin_up && up))
1353                 notify = true;
1354
1355         cpu_dp->master_oper_up = up;
1356
1357         if (notify)
1358                 dsa_tree_master_state_change(dst, master);
1359 }
1360
1361 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1362 {
1363         struct dsa_switch_tree *dst = ds->dst;
1364         struct dsa_port *dp;
1365
1366         dsa_switch_for_each_port(dp, ds)
1367                 if (dp->index == index)
1368                         return dp;
1369
1370         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1371         if (!dp)
1372                 return NULL;
1373
1374         dp->ds = ds;
1375         dp->index = index;
1376
1377         INIT_LIST_HEAD(&dp->list);
1378         list_add_tail(&dp->list, &dst->ports);
1379
1380         return dp;
1381 }
1382
1383 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1384 {
1385         if (!name)
1386                 name = "eth%d";
1387
1388         dp->type = DSA_PORT_TYPE_USER;
1389         dp->name = name;
1390
1391         return 0;
1392 }
1393
1394 static int dsa_port_parse_dsa(struct dsa_port *dp)
1395 {
1396         dp->type = DSA_PORT_TYPE_DSA;
1397
1398         return 0;
1399 }
1400
1401 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1402                                                   struct net_device *master)
1403 {
1404         enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1405         struct dsa_switch *mds, *ds = dp->ds;
1406         unsigned int mdp_upstream;
1407         struct dsa_port *mdp;
1408
1409         /* It is possible to stack DSA switches onto one another when that
1410          * happens the switch driver may want to know if its tagging protocol
1411          * is going to work in such a configuration.
1412          */
1413         if (dsa_slave_dev_check(master)) {
1414                 mdp = dsa_slave_to_port(master);
1415                 mds = mdp->ds;
1416                 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1417                 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1418                                                           DSA_TAG_PROTO_NONE);
1419         }
1420
1421         /* If the master device is not itself a DSA slave in a disjoint DSA
1422          * tree, then return immediately.
1423          */
1424         return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1425 }
1426
1427 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
1428                               const char *user_protocol)
1429 {
1430         struct dsa_switch *ds = dp->ds;
1431         struct dsa_switch_tree *dst = ds->dst;
1432         const struct dsa_device_ops *tag_ops;
1433         enum dsa_tag_protocol default_proto;
1434
1435         /* Find out which protocol the switch would prefer. */
1436         default_proto = dsa_get_tag_protocol(dp, master);
1437         if (dst->default_proto) {
1438                 if (dst->default_proto != default_proto) {
1439                         dev_err(ds->dev,
1440                                 "A DSA switch tree can have only one tagging protocol\n");
1441                         return -EINVAL;
1442                 }
1443         } else {
1444                 dst->default_proto = default_proto;
1445         }
1446
1447         /* See if the user wants to override that preference. */
1448         if (user_protocol) {
1449                 if (!ds->ops->change_tag_protocol) {
1450                         dev_err(ds->dev, "Tag protocol cannot be modified\n");
1451                         return -EINVAL;
1452                 }
1453
1454                 tag_ops = dsa_find_tagger_by_name(user_protocol);
1455         } else {
1456                 tag_ops = dsa_tag_driver_get(default_proto);
1457         }
1458
1459         if (IS_ERR(tag_ops)) {
1460                 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1461                         return -EPROBE_DEFER;
1462
1463                 dev_warn(ds->dev, "No tagger for this switch\n");
1464                 return PTR_ERR(tag_ops);
1465         }
1466
1467         if (dst->tag_ops) {
1468                 if (dst->tag_ops != tag_ops) {
1469                         dev_err(ds->dev,
1470                                 "A DSA switch tree can have only one tagging protocol\n");
1471
1472                         dsa_tag_driver_put(tag_ops);
1473                         return -EINVAL;
1474                 }
1475
1476                 /* In the case of multiple CPU ports per switch, the tagging
1477                  * protocol is still reference-counted only per switch tree.
1478                  */
1479                 dsa_tag_driver_put(tag_ops);
1480         } else {
1481                 dst->tag_ops = tag_ops;
1482         }
1483
1484         dp->master = master;
1485         dp->type = DSA_PORT_TYPE_CPU;
1486         dsa_port_set_tag_protocol(dp, dst->tag_ops);
1487         dp->dst = dst;
1488
1489         /* At this point, the tree may be configured to use a different
1490          * tagger than the one chosen by the switch driver during
1491          * .setup, in the case when a user selects a custom protocol
1492          * through the DT.
1493          *
1494          * This is resolved by syncing the driver with the tree in
1495          * dsa_switch_setup_tag_protocol once .setup has run and the
1496          * driver is ready to accept calls to .change_tag_protocol. If
1497          * the driver does not support the custom protocol at that
1498          * point, the tree is wholly rejected, thereby ensuring that the
1499          * tree and driver are always in agreement on the protocol to
1500          * use.
1501          */
1502         return 0;
1503 }
1504
1505 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1506 {
1507         struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1508         const char *name = of_get_property(dn, "label", NULL);
1509         bool link = of_property_read_bool(dn, "link");
1510
1511         dp->dn = dn;
1512
1513         if (ethernet) {
1514                 struct net_device *master;
1515                 const char *user_protocol;
1516
1517                 master = of_find_net_device_by_node(ethernet);
1518                 if (!master)
1519                         return -EPROBE_DEFER;
1520
1521                 user_protocol = of_get_property(dn, "dsa-tag-protocol", NULL);
1522                 return dsa_port_parse_cpu(dp, master, user_protocol);
1523         }
1524
1525         if (link)
1526                 return dsa_port_parse_dsa(dp);
1527
1528         return dsa_port_parse_user(dp, name);
1529 }
1530
1531 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1532                                      struct device_node *dn)
1533 {
1534         struct device_node *ports, *port;
1535         struct dsa_port *dp;
1536         int err = 0;
1537         u32 reg;
1538
1539         ports = of_get_child_by_name(dn, "ports");
1540         if (!ports) {
1541                 /* The second possibility is "ethernet-ports" */
1542                 ports = of_get_child_by_name(dn, "ethernet-ports");
1543                 if (!ports) {
1544                         dev_err(ds->dev, "no ports child node found\n");
1545                         return -EINVAL;
1546                 }
1547         }
1548
1549         for_each_available_child_of_node(ports, port) {
1550                 err = of_property_read_u32(port, "reg", &reg);
1551                 if (err) {
1552                         of_node_put(port);
1553                         goto out_put_node;
1554                 }
1555
1556                 if (reg >= ds->num_ports) {
1557                         dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%u)\n",
1558                                 port, reg, ds->num_ports);
1559                         of_node_put(port);
1560                         err = -EINVAL;
1561                         goto out_put_node;
1562                 }
1563
1564                 dp = dsa_to_port(ds, reg);
1565
1566                 err = dsa_port_parse_of(dp, port);
1567                 if (err) {
1568                         of_node_put(port);
1569                         goto out_put_node;
1570                 }
1571         }
1572
1573 out_put_node:
1574         of_node_put(ports);
1575         return err;
1576 }
1577
1578 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1579                                       struct device_node *dn)
1580 {
1581         u32 m[2] = { 0, 0 };
1582         int sz;
1583
1584         /* Don't error out if this optional property isn't found */
1585         sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1586         if (sz < 0 && sz != -EINVAL)
1587                 return sz;
1588
1589         ds->index = m[1];
1590
1591         ds->dst = dsa_tree_touch(m[0]);
1592         if (!ds->dst)
1593                 return -ENOMEM;
1594
1595         if (dsa_switch_find(ds->dst->index, ds->index)) {
1596                 dev_err(ds->dev,
1597                         "A DSA switch with index %d already exists in tree %d\n",
1598                         ds->index, ds->dst->index);
1599                 return -EEXIST;
1600         }
1601
1602         if (ds->dst->last_switch < ds->index)
1603                 ds->dst->last_switch = ds->index;
1604
1605         return 0;
1606 }
1607
1608 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1609 {
1610         struct dsa_port *dp;
1611         int port;
1612
1613         for (port = 0; port < ds->num_ports; port++) {
1614                 dp = dsa_port_touch(ds, port);
1615                 if (!dp)
1616                         return -ENOMEM;
1617         }
1618
1619         return 0;
1620 }
1621
1622 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1623 {
1624         int err;
1625
1626         err = dsa_switch_parse_member_of(ds, dn);
1627         if (err)
1628                 return err;
1629
1630         err = dsa_switch_touch_ports(ds);
1631         if (err)
1632                 return err;
1633
1634         return dsa_switch_parse_ports_of(ds, dn);
1635 }
1636
1637 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1638                           struct device *dev)
1639 {
1640         if (!strcmp(name, "cpu")) {
1641                 struct net_device *master;
1642
1643                 master = dsa_dev_to_net_device(dev);
1644                 if (!master)
1645                         return -EPROBE_DEFER;
1646
1647                 dev_put(master);
1648
1649                 return dsa_port_parse_cpu(dp, master, NULL);
1650         }
1651
1652         if (!strcmp(name, "dsa"))
1653                 return dsa_port_parse_dsa(dp);
1654
1655         return dsa_port_parse_user(dp, name);
1656 }
1657
1658 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1659                                   struct dsa_chip_data *cd)
1660 {
1661         bool valid_name_found = false;
1662         struct dsa_port *dp;
1663         struct device *dev;
1664         const char *name;
1665         unsigned int i;
1666         int err;
1667
1668         for (i = 0; i < DSA_MAX_PORTS; i++) {
1669                 name = cd->port_names[i];
1670                 dev = cd->netdev[i];
1671                 dp = dsa_to_port(ds, i);
1672
1673                 if (!name)
1674                         continue;
1675
1676                 err = dsa_port_parse(dp, name, dev);
1677                 if (err)
1678                         return err;
1679
1680                 valid_name_found = true;
1681         }
1682
1683         if (!valid_name_found && i == DSA_MAX_PORTS)
1684                 return -EINVAL;
1685
1686         return 0;
1687 }
1688
1689 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1690 {
1691         int err;
1692
1693         ds->cd = cd;
1694
1695         /* We don't support interconnected switches nor multiple trees via
1696          * platform data, so this is the unique switch of the tree.
1697          */
1698         ds->index = 0;
1699         ds->dst = dsa_tree_touch(0);
1700         if (!ds->dst)
1701                 return -ENOMEM;
1702
1703         err = dsa_switch_touch_ports(ds);
1704         if (err)
1705                 return err;
1706
1707         return dsa_switch_parse_ports(ds, cd);
1708 }
1709
1710 static void dsa_switch_release_ports(struct dsa_switch *ds)
1711 {
1712         struct dsa_port *dp, *next;
1713
1714         dsa_switch_for_each_port_safe(dp, next, ds) {
1715                 list_del(&dp->list);
1716                 kfree(dp);
1717         }
1718 }
1719
1720 static int dsa_switch_probe(struct dsa_switch *ds)
1721 {
1722         struct dsa_switch_tree *dst;
1723         struct dsa_chip_data *pdata;
1724         struct device_node *np;
1725         int err;
1726
1727         if (!ds->dev)
1728                 return -ENODEV;
1729
1730         pdata = ds->dev->platform_data;
1731         np = ds->dev->of_node;
1732
1733         if (!ds->num_ports)
1734                 return -EINVAL;
1735
1736         if (np) {
1737                 err = dsa_switch_parse_of(ds, np);
1738                 if (err)
1739                         dsa_switch_release_ports(ds);
1740         } else if (pdata) {
1741                 err = dsa_switch_parse(ds, pdata);
1742                 if (err)
1743                         dsa_switch_release_ports(ds);
1744         } else {
1745                 err = -ENODEV;
1746         }
1747
1748         if (err)
1749                 return err;
1750
1751         dst = ds->dst;
1752         dsa_tree_get(dst);
1753         err = dsa_tree_setup(dst);
1754         if (err) {
1755                 dsa_switch_release_ports(ds);
1756                 dsa_tree_put(dst);
1757         }
1758
1759         return err;
1760 }
1761
1762 int dsa_register_switch(struct dsa_switch *ds)
1763 {
1764         int err;
1765
1766         mutex_lock(&dsa2_mutex);
1767         err = dsa_switch_probe(ds);
1768         dsa_tree_put(ds->dst);
1769         mutex_unlock(&dsa2_mutex);
1770
1771         return err;
1772 }
1773 EXPORT_SYMBOL_GPL(dsa_register_switch);
1774
1775 static void dsa_switch_remove(struct dsa_switch *ds)
1776 {
1777         struct dsa_switch_tree *dst = ds->dst;
1778
1779         dsa_tree_teardown(dst);
1780         dsa_switch_release_ports(ds);
1781         dsa_tree_put(dst);
1782 }
1783
1784 void dsa_unregister_switch(struct dsa_switch *ds)
1785 {
1786         mutex_lock(&dsa2_mutex);
1787         dsa_switch_remove(ds);
1788         mutex_unlock(&dsa2_mutex);
1789 }
1790 EXPORT_SYMBOL_GPL(dsa_unregister_switch);
1791
1792 /* If the DSA master chooses to unregister its net_device on .shutdown, DSA is
1793  * blocking that operation from completion, due to the dev_hold taken inside
1794  * netdev_upper_dev_link. Unlink the DSA slave interfaces from being uppers of
1795  * the DSA master, so that the system can reboot successfully.
1796  */
1797 void dsa_switch_shutdown(struct dsa_switch *ds)
1798 {
1799         struct net_device *master, *slave_dev;
1800         struct dsa_port *dp;
1801
1802         mutex_lock(&dsa2_mutex);
1803         rtnl_lock();
1804
1805         dsa_switch_for_each_user_port(dp, ds) {
1806                 master = dp->cpu_dp->master;
1807                 slave_dev = dp->slave;
1808
1809                 netdev_upper_dev_unlink(master, slave_dev);
1810         }
1811
1812         /* Disconnect from further netdevice notifiers on the master,
1813          * since netdev_uses_dsa() will now return false.
1814          */
1815         dsa_switch_for_each_cpu_port(dp, ds)
1816                 dp->master->dsa_ptr = NULL;
1817
1818         rtnl_unlock();
1819         mutex_unlock(&dsa2_mutex);
1820 }
1821 EXPORT_SYMBOL_GPL(dsa_switch_shutdown);
This page took 0.139194 seconds and 4 git commands to generate.