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