net: dsa: Fix type was not set for devlink port
[linux-2.6-microblaze.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 /**
25  * dsa_tree_notify - Execute code for all switches in a DSA switch tree.
26  * @dst: collection of struct dsa_switch devices to notify.
27  * @e: event, must be of type DSA_NOTIFIER_*
28  * @v: event-specific value.
29  *
30  * Given a struct dsa_switch_tree, this can be used to run a function once for
31  * each member DSA switch. The other alternative of traversing the tree is only
32  * through its ports list, which does not uniquely list the switches.
33  */
34 int dsa_tree_notify(struct dsa_switch_tree *dst, unsigned long e, void *v)
35 {
36         struct raw_notifier_head *nh = &dst->nh;
37         int err;
38
39         err = raw_notifier_call_chain(nh, e, v);
40
41         return notifier_to_errno(err);
42 }
43
44 /**
45  * dsa_broadcast - Notify all DSA trees in the system.
46  * @e: event, must be of type DSA_NOTIFIER_*
47  * @v: event-specific value.
48  *
49  * Can be used to notify the switching fabric of events such as cross-chip
50  * bridging between disjoint trees (such as islands of tagger-compatible
51  * switches bridged by an incompatible middle switch).
52  */
53 int dsa_broadcast(unsigned long e, void *v)
54 {
55         struct dsa_switch_tree *dst;
56         int err = 0;
57
58         list_for_each_entry(dst, &dsa_tree_list, list) {
59                 err = dsa_tree_notify(dst, e, v);
60                 if (err)
61                         break;
62         }
63
64         return err;
65 }
66
67 /**
68  * dsa_lag_map() - Map LAG netdev to a linear LAG ID
69  * @dst: Tree in which to record the mapping.
70  * @lag: Netdev that is to be mapped to an ID.
71  *
72  * dsa_lag_id/dsa_lag_dev can then be used to translate between the
73  * two spaces. The size of the mapping space is determined by the
74  * driver by setting ds->num_lag_ids. It is perfectly legal to leave
75  * it unset if it is not needed, in which case these functions become
76  * no-ops.
77  */
78 void dsa_lag_map(struct dsa_switch_tree *dst, struct net_device *lag)
79 {
80         unsigned int id;
81
82         if (dsa_lag_id(dst, lag) >= 0)
83                 /* Already mapped */
84                 return;
85
86         for (id = 0; id < dst->lags_len; id++) {
87                 if (!dsa_lag_dev(dst, id)) {
88                         dst->lags[id] = lag;
89                         return;
90                 }
91         }
92
93         /* No IDs left, which is OK. Some drivers do not need it. The
94          * ones that do, e.g. mv88e6xxx, will discover that dsa_lag_id
95          * returns an error for this device when joining the LAG. The
96          * driver can then return -EOPNOTSUPP back to DSA, which will
97          * fall back to a software LAG.
98          */
99 }
100
101 /**
102  * dsa_lag_unmap() - Remove a LAG ID mapping
103  * @dst: Tree in which the mapping is recorded.
104  * @lag: Netdev that was mapped.
105  *
106  * As there may be multiple users of the mapping, it is only removed
107  * if there are no other references to it.
108  */
109 void dsa_lag_unmap(struct dsa_switch_tree *dst, struct net_device *lag)
110 {
111         struct dsa_port *dp;
112         unsigned int id;
113
114         dsa_lag_foreach_port(dp, dst, lag)
115                 /* There are remaining users of this mapping */
116                 return;
117
118         dsa_lags_foreach_id(id, dst) {
119                 if (dsa_lag_dev(dst, id) == lag) {
120                         dst->lags[id] = NULL;
121                         break;
122                 }
123         }
124 }
125
126 struct dsa_switch *dsa_switch_find(int tree_index, int sw_index)
127 {
128         struct dsa_switch_tree *dst;
129         struct dsa_port *dp;
130
131         list_for_each_entry(dst, &dsa_tree_list, list) {
132                 if (dst->index != tree_index)
133                         continue;
134
135                 list_for_each_entry(dp, &dst->ports, list) {
136                         if (dp->ds->index != sw_index)
137                                 continue;
138
139                         return dp->ds;
140                 }
141         }
142
143         return NULL;
144 }
145 EXPORT_SYMBOL_GPL(dsa_switch_find);
146
147 static struct dsa_switch_tree *dsa_tree_find(int index)
148 {
149         struct dsa_switch_tree *dst;
150
151         list_for_each_entry(dst, &dsa_tree_list, list)
152                 if (dst->index == index)
153                         return dst;
154
155         return NULL;
156 }
157
158 static struct dsa_switch_tree *dsa_tree_alloc(int index)
159 {
160         struct dsa_switch_tree *dst;
161
162         dst = kzalloc(sizeof(*dst), GFP_KERNEL);
163         if (!dst)
164                 return NULL;
165
166         dst->index = index;
167
168         INIT_LIST_HEAD(&dst->rtable);
169
170         INIT_LIST_HEAD(&dst->ports);
171
172         INIT_LIST_HEAD(&dst->list);
173         list_add_tail(&dst->list, &dsa_tree_list);
174
175         kref_init(&dst->refcount);
176
177         return dst;
178 }
179
180 static void dsa_tree_free(struct dsa_switch_tree *dst)
181 {
182         if (dst->tag_ops)
183                 dsa_tag_driver_put(dst->tag_ops);
184         list_del(&dst->list);
185         kfree(dst);
186 }
187
188 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
189 {
190         if (dst)
191                 kref_get(&dst->refcount);
192
193         return dst;
194 }
195
196 static struct dsa_switch_tree *dsa_tree_touch(int index)
197 {
198         struct dsa_switch_tree *dst;
199
200         dst = dsa_tree_find(index);
201         if (dst)
202                 return dsa_tree_get(dst);
203         else
204                 return dsa_tree_alloc(index);
205 }
206
207 static void dsa_tree_release(struct kref *ref)
208 {
209         struct dsa_switch_tree *dst;
210
211         dst = container_of(ref, struct dsa_switch_tree, refcount);
212
213         dsa_tree_free(dst);
214 }
215
216 static void dsa_tree_put(struct dsa_switch_tree *dst)
217 {
218         if (dst)
219                 kref_put(&dst->refcount, dsa_tree_release);
220 }
221
222 static bool dsa_port_is_dsa(struct dsa_port *port)
223 {
224         return port->type == DSA_PORT_TYPE_DSA;
225 }
226
227 static bool dsa_port_is_cpu(struct dsa_port *port)
228 {
229         return port->type == DSA_PORT_TYPE_CPU;
230 }
231
232 static bool dsa_port_is_user(struct dsa_port *dp)
233 {
234         return dp->type == DSA_PORT_TYPE_USER;
235 }
236
237 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
238                                                    struct device_node *dn)
239 {
240         struct dsa_port *dp;
241
242         list_for_each_entry(dp, &dst->ports, list)
243                 if (dp->dn == dn)
244                         return dp;
245
246         return NULL;
247 }
248
249 static struct dsa_link *dsa_link_touch(struct dsa_port *dp,
250                                        struct dsa_port *link_dp)
251 {
252         struct dsa_switch *ds = dp->ds;
253         struct dsa_switch_tree *dst;
254         struct dsa_link *dl;
255
256         dst = ds->dst;
257
258         list_for_each_entry(dl, &dst->rtable, list)
259                 if (dl->dp == dp && dl->link_dp == link_dp)
260                         return dl;
261
262         dl = kzalloc(sizeof(*dl), GFP_KERNEL);
263         if (!dl)
264                 return NULL;
265
266         dl->dp = dp;
267         dl->link_dp = link_dp;
268
269         INIT_LIST_HEAD(&dl->list);
270         list_add_tail(&dl->list, &dst->rtable);
271
272         return dl;
273 }
274
275 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
276 {
277         struct dsa_switch *ds = dp->ds;
278         struct dsa_switch_tree *dst = ds->dst;
279         struct device_node *dn = dp->dn;
280         struct of_phandle_iterator it;
281         struct dsa_port *link_dp;
282         struct dsa_link *dl;
283         int err;
284
285         of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
286                 link_dp = dsa_tree_find_port_by_node(dst, it.node);
287                 if (!link_dp) {
288                         of_node_put(it.node);
289                         return false;
290                 }
291
292                 dl = dsa_link_touch(dp, link_dp);
293                 if (!dl) {
294                         of_node_put(it.node);
295                         return false;
296                 }
297         }
298
299         return true;
300 }
301
302 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
303 {
304         bool complete = true;
305         struct dsa_port *dp;
306
307         list_for_each_entry(dp, &dst->ports, list) {
308                 if (dsa_port_is_dsa(dp)) {
309                         complete = dsa_port_setup_routing_table(dp);
310                         if (!complete)
311                                 break;
312                 }
313         }
314
315         return complete;
316 }
317
318 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
319 {
320         struct dsa_port *dp;
321
322         list_for_each_entry(dp, &dst->ports, list)
323                 if (dsa_port_is_cpu(dp))
324                         return dp;
325
326         return NULL;
327 }
328
329 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
330 {
331         struct dsa_port *cpu_dp, *dp;
332
333         cpu_dp = dsa_tree_find_first_cpu(dst);
334         if (!cpu_dp) {
335                 pr_err("DSA: tree %d has no CPU port\n", dst->index);
336                 return -EINVAL;
337         }
338
339         /* Assign the default CPU port to all ports of the fabric */
340         list_for_each_entry(dp, &dst->ports, list)
341                 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
342                         dp->cpu_dp = cpu_dp;
343
344         return 0;
345 }
346
347 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
348 {
349         struct dsa_port *dp;
350
351         list_for_each_entry(dp, &dst->ports, list)
352                 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
353                         dp->cpu_dp = NULL;
354 }
355
356 static int dsa_port_setup(struct dsa_port *dp)
357 {
358         struct devlink_port *dlp = &dp->devlink_port;
359         bool dsa_port_link_registered = false;
360         bool dsa_port_enabled = false;
361         int err = 0;
362
363         if (dp->setup)
364                 return 0;
365
366         switch (dp->type) {
367         case DSA_PORT_TYPE_UNUSED:
368                 dsa_port_disable(dp);
369                 break;
370         case DSA_PORT_TYPE_CPU:
371                 err = dsa_port_link_register_of(dp);
372                 if (err)
373                         break;
374                 dsa_port_link_registered = true;
375
376                 err = dsa_port_enable(dp, NULL);
377                 if (err)
378                         break;
379                 dsa_port_enabled = true;
380
381                 break;
382         case DSA_PORT_TYPE_DSA:
383                 err = dsa_port_link_register_of(dp);
384                 if (err)
385                         break;
386                 dsa_port_link_registered = true;
387
388                 err = dsa_port_enable(dp, NULL);
389                 if (err)
390                         break;
391                 dsa_port_enabled = true;
392
393                 break;
394         case DSA_PORT_TYPE_USER:
395                 dp->mac = of_get_mac_address(dp->dn);
396                 err = dsa_slave_create(dp);
397                 if (err)
398                         break;
399
400                 devlink_port_type_eth_set(dlp, dp->slave);
401                 break;
402         }
403
404         if (err && dsa_port_enabled)
405                 dsa_port_disable(dp);
406         if (err && dsa_port_link_registered)
407                 dsa_port_link_unregister_of(dp);
408         if (err)
409                 return err;
410
411         dp->setup = true;
412
413         return 0;
414 }
415
416 static int dsa_port_devlink_setup(struct dsa_port *dp)
417 {
418         struct devlink_port *dlp = &dp->devlink_port;
419         struct dsa_switch_tree *dst = dp->ds->dst;
420         struct devlink_port_attrs attrs = {};
421         struct devlink *dl = dp->ds->devlink;
422         const unsigned char *id;
423         unsigned char len;
424         int err;
425
426         id = (const unsigned char *)&dst->index;
427         len = sizeof(dst->index);
428
429         attrs.phys.port_number = dp->index;
430         memcpy(attrs.switch_id.id, id, len);
431         attrs.switch_id.id_len = len;
432         memset(dlp, 0, sizeof(*dlp));
433
434         switch (dp->type) {
435         case DSA_PORT_TYPE_UNUSED:
436                 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
437                 break;
438         case DSA_PORT_TYPE_CPU:
439                 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
440                 break;
441         case DSA_PORT_TYPE_DSA:
442                 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
443                 break;
444         case DSA_PORT_TYPE_USER:
445                 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
446                 break;
447         }
448
449         devlink_port_attrs_set(dlp, &attrs);
450         err = devlink_port_register(dl, dlp, dp->index);
451
452         if (!err)
453                 dp->devlink_port_setup = true;
454
455         return err;
456 }
457
458 static void dsa_port_teardown(struct dsa_port *dp)
459 {
460         struct devlink_port *dlp = &dp->devlink_port;
461
462         if (!dp->setup)
463                 return;
464
465         devlink_port_type_clear(dlp);
466
467         switch (dp->type) {
468         case DSA_PORT_TYPE_UNUSED:
469                 break;
470         case DSA_PORT_TYPE_CPU:
471                 dsa_port_disable(dp);
472                 dsa_port_link_unregister_of(dp);
473                 break;
474         case DSA_PORT_TYPE_DSA:
475                 dsa_port_disable(dp);
476                 dsa_port_link_unregister_of(dp);
477                 break;
478         case DSA_PORT_TYPE_USER:
479                 if (dp->slave) {
480                         dsa_slave_destroy(dp->slave);
481                         dp->slave = NULL;
482                 }
483                 break;
484         }
485
486         dp->setup = false;
487 }
488
489 static void dsa_port_devlink_teardown(struct dsa_port *dp)
490 {
491         struct devlink_port *dlp = &dp->devlink_port;
492
493         if (dp->devlink_port_setup)
494                 devlink_port_unregister(dlp);
495         dp->devlink_port_setup = false;
496 }
497
498 static int dsa_devlink_info_get(struct devlink *dl,
499                                 struct devlink_info_req *req,
500                                 struct netlink_ext_ack *extack)
501 {
502         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
503
504         if (ds->ops->devlink_info_get)
505                 return ds->ops->devlink_info_get(ds, req, extack);
506
507         return -EOPNOTSUPP;
508 }
509
510 static int dsa_devlink_sb_pool_get(struct devlink *dl,
511                                    unsigned int sb_index, u16 pool_index,
512                                    struct devlink_sb_pool_info *pool_info)
513 {
514         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
515
516         if (!ds->ops->devlink_sb_pool_get)
517                 return -EOPNOTSUPP;
518
519         return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
520                                             pool_info);
521 }
522
523 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
524                                    u16 pool_index, u32 size,
525                                    enum devlink_sb_threshold_type threshold_type,
526                                    struct netlink_ext_ack *extack)
527 {
528         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
529
530         if (!ds->ops->devlink_sb_pool_set)
531                 return -EOPNOTSUPP;
532
533         return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
534                                             threshold_type, extack);
535 }
536
537 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
538                                         unsigned int sb_index, u16 pool_index,
539                                         u32 *p_threshold)
540 {
541         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
542         int port = dsa_devlink_port_to_port(dlp);
543
544         if (!ds->ops->devlink_sb_port_pool_get)
545                 return -EOPNOTSUPP;
546
547         return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
548                                                  pool_index, p_threshold);
549 }
550
551 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
552                                         unsigned int sb_index, u16 pool_index,
553                                         u32 threshold,
554                                         struct netlink_ext_ack *extack)
555 {
556         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
557         int port = dsa_devlink_port_to_port(dlp);
558
559         if (!ds->ops->devlink_sb_port_pool_set)
560                 return -EOPNOTSUPP;
561
562         return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
563                                                  pool_index, threshold, extack);
564 }
565
566 static int
567 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
568                                 unsigned int sb_index, u16 tc_index,
569                                 enum devlink_sb_pool_type pool_type,
570                                 u16 *p_pool_index, u32 *p_threshold)
571 {
572         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
573         int port = dsa_devlink_port_to_port(dlp);
574
575         if (!ds->ops->devlink_sb_tc_pool_bind_get)
576                 return -EOPNOTSUPP;
577
578         return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
579                                                     tc_index, pool_type,
580                                                     p_pool_index, p_threshold);
581 }
582
583 static int
584 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
585                                 unsigned int sb_index, u16 tc_index,
586                                 enum devlink_sb_pool_type pool_type,
587                                 u16 pool_index, u32 threshold,
588                                 struct netlink_ext_ack *extack)
589 {
590         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
591         int port = dsa_devlink_port_to_port(dlp);
592
593         if (!ds->ops->devlink_sb_tc_pool_bind_set)
594                 return -EOPNOTSUPP;
595
596         return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
597                                                     tc_index, pool_type,
598                                                     pool_index, threshold,
599                                                     extack);
600 }
601
602 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
603                                        unsigned int sb_index)
604 {
605         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
606
607         if (!ds->ops->devlink_sb_occ_snapshot)
608                 return -EOPNOTSUPP;
609
610         return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
611 }
612
613 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
614                                         unsigned int sb_index)
615 {
616         struct dsa_switch *ds = dsa_devlink_to_ds(dl);
617
618         if (!ds->ops->devlink_sb_occ_max_clear)
619                 return -EOPNOTSUPP;
620
621         return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
622 }
623
624 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
625                                             unsigned int sb_index,
626                                             u16 pool_index, u32 *p_cur,
627                                             u32 *p_max)
628 {
629         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
630         int port = dsa_devlink_port_to_port(dlp);
631
632         if (!ds->ops->devlink_sb_occ_port_pool_get)
633                 return -EOPNOTSUPP;
634
635         return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
636                                                      pool_index, p_cur, p_max);
637 }
638
639 static int
640 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
641                                     unsigned int sb_index, u16 tc_index,
642                                     enum devlink_sb_pool_type pool_type,
643                                     u32 *p_cur, u32 *p_max)
644 {
645         struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
646         int port = dsa_devlink_port_to_port(dlp);
647
648         if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
649                 return -EOPNOTSUPP;
650
651         return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
652                                                         sb_index, tc_index,
653                                                         pool_type, p_cur,
654                                                         p_max);
655 }
656
657 static const struct devlink_ops dsa_devlink_ops = {
658         .info_get                       = dsa_devlink_info_get,
659         .sb_pool_get                    = dsa_devlink_sb_pool_get,
660         .sb_pool_set                    = dsa_devlink_sb_pool_set,
661         .sb_port_pool_get               = dsa_devlink_sb_port_pool_get,
662         .sb_port_pool_set               = dsa_devlink_sb_port_pool_set,
663         .sb_tc_pool_bind_get            = dsa_devlink_sb_tc_pool_bind_get,
664         .sb_tc_pool_bind_set            = dsa_devlink_sb_tc_pool_bind_set,
665         .sb_occ_snapshot                = dsa_devlink_sb_occ_snapshot,
666         .sb_occ_max_clear               = dsa_devlink_sb_occ_max_clear,
667         .sb_occ_port_pool_get           = dsa_devlink_sb_occ_port_pool_get,
668         .sb_occ_tc_port_bind_get        = dsa_devlink_sb_occ_tc_port_bind_get,
669 };
670
671 static int dsa_switch_setup(struct dsa_switch *ds)
672 {
673         struct dsa_devlink_priv *dl_priv;
674         struct dsa_port *dp;
675         int err;
676
677         if (ds->setup)
678                 return 0;
679
680         /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
681          * driver and before ops->setup() has run, since the switch drivers and
682          * the slave MDIO bus driver rely on these values for probing PHY
683          * devices or not
684          */
685         ds->phys_mii_mask |= dsa_user_ports(ds);
686
687         /* Add the switch to devlink before calling setup, so that setup can
688          * add dpipe tables
689          */
690         ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
691         if (!ds->devlink)
692                 return -ENOMEM;
693         dl_priv = devlink_priv(ds->devlink);
694         dl_priv->ds = ds;
695
696         err = devlink_register(ds->devlink, ds->dev);
697         if (err)
698                 goto free_devlink;
699
700         /* Setup devlink port instances now, so that the switch
701          * setup() can register regions etc, against the ports
702          */
703         list_for_each_entry(dp, &ds->dst->ports, list) {
704                 if (dp->ds == ds) {
705                         err = dsa_port_devlink_setup(dp);
706                         if (err)
707                                 goto unregister_devlink_ports;
708                 }
709         }
710
711         err = dsa_switch_register_notifier(ds);
712         if (err)
713                 goto unregister_devlink_ports;
714
715         ds->configure_vlan_while_not_filtering = true;
716
717         err = ds->ops->setup(ds);
718         if (err < 0)
719                 goto unregister_notifier;
720
721         devlink_params_publish(ds->devlink);
722
723         if (!ds->slave_mii_bus && ds->ops->phy_read) {
724                 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
725                 if (!ds->slave_mii_bus) {
726                         err = -ENOMEM;
727                         goto teardown;
728                 }
729
730                 dsa_slave_mii_bus_init(ds);
731
732                 err = mdiobus_register(ds->slave_mii_bus);
733                 if (err < 0)
734                         goto teardown;
735         }
736
737         ds->setup = true;
738
739         return 0;
740
741 teardown:
742         if (ds->ops->teardown)
743                 ds->ops->teardown(ds);
744 unregister_notifier:
745         dsa_switch_unregister_notifier(ds);
746 unregister_devlink_ports:
747         list_for_each_entry(dp, &ds->dst->ports, list)
748                 if (dp->ds == ds)
749                         dsa_port_devlink_teardown(dp);
750         devlink_unregister(ds->devlink);
751 free_devlink:
752         devlink_free(ds->devlink);
753         ds->devlink = NULL;
754
755         return err;
756 }
757
758 static void dsa_switch_teardown(struct dsa_switch *ds)
759 {
760         struct dsa_port *dp;
761
762         if (!ds->setup)
763                 return;
764
765         if (ds->slave_mii_bus && ds->ops->phy_read)
766                 mdiobus_unregister(ds->slave_mii_bus);
767
768         dsa_switch_unregister_notifier(ds);
769
770         if (ds->ops->teardown)
771                 ds->ops->teardown(ds);
772
773         if (ds->devlink) {
774                 list_for_each_entry(dp, &ds->dst->ports, list)
775                         if (dp->ds == ds)
776                                 dsa_port_devlink_teardown(dp);
777                 devlink_unregister(ds->devlink);
778                 devlink_free(ds->devlink);
779                 ds->devlink = NULL;
780         }
781
782         ds->setup = false;
783 }
784
785 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
786 {
787         struct dsa_port *dp;
788         int err;
789
790         list_for_each_entry(dp, &dst->ports, list) {
791                 err = dsa_switch_setup(dp->ds);
792                 if (err)
793                         goto teardown;
794         }
795
796         list_for_each_entry(dp, &dst->ports, list) {
797                 err = dsa_port_setup(dp);
798                 if (err) {
799                         dsa_port_devlink_teardown(dp);
800                         dp->type = DSA_PORT_TYPE_UNUSED;
801                         err = dsa_port_devlink_setup(dp);
802                         if (err)
803                                 goto teardown;
804                         continue;
805                 }
806         }
807
808         return 0;
809
810 teardown:
811         list_for_each_entry(dp, &dst->ports, list)
812                 dsa_port_teardown(dp);
813
814         list_for_each_entry(dp, &dst->ports, list)
815                 dsa_switch_teardown(dp->ds);
816
817         return err;
818 }
819
820 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
821 {
822         struct dsa_port *dp;
823
824         list_for_each_entry(dp, &dst->ports, list)
825                 dsa_port_teardown(dp);
826
827         list_for_each_entry(dp, &dst->ports, list)
828                 dsa_switch_teardown(dp->ds);
829 }
830
831 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
832 {
833         struct dsa_port *dp;
834         int err;
835
836         list_for_each_entry(dp, &dst->ports, list) {
837                 if (dsa_port_is_cpu(dp)) {
838                         err = dsa_master_setup(dp->master, dp);
839                         if (err)
840                                 return err;
841                 }
842         }
843
844         return 0;
845 }
846
847 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
848 {
849         struct dsa_port *dp;
850
851         list_for_each_entry(dp, &dst->ports, list)
852                 if (dsa_port_is_cpu(dp))
853                         dsa_master_teardown(dp->master);
854 }
855
856 static int dsa_tree_setup_lags(struct dsa_switch_tree *dst)
857 {
858         unsigned int len = 0;
859         struct dsa_port *dp;
860
861         list_for_each_entry(dp, &dst->ports, list) {
862                 if (dp->ds->num_lag_ids > len)
863                         len = dp->ds->num_lag_ids;
864         }
865
866         if (!len)
867                 return 0;
868
869         dst->lags = kcalloc(len, sizeof(*dst->lags), GFP_KERNEL);
870         if (!dst->lags)
871                 return -ENOMEM;
872
873         dst->lags_len = len;
874         return 0;
875 }
876
877 static void dsa_tree_teardown_lags(struct dsa_switch_tree *dst)
878 {
879         kfree(dst->lags);
880 }
881
882 static int dsa_tree_setup(struct dsa_switch_tree *dst)
883 {
884         bool complete;
885         int err;
886
887         if (dst->setup) {
888                 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
889                        dst->index);
890                 return -EEXIST;
891         }
892
893         complete = dsa_tree_setup_routing_table(dst);
894         if (!complete)
895                 return 0;
896
897         err = dsa_tree_setup_default_cpu(dst);
898         if (err)
899                 return err;
900
901         err = dsa_tree_setup_switches(dst);
902         if (err)
903                 goto teardown_default_cpu;
904
905         err = dsa_tree_setup_master(dst);
906         if (err)
907                 goto teardown_switches;
908
909         err = dsa_tree_setup_lags(dst);
910         if (err)
911                 goto teardown_master;
912
913         dst->setup = true;
914
915         pr_info("DSA: tree %d setup\n", dst->index);
916
917         return 0;
918
919 teardown_master:
920         dsa_tree_teardown_master(dst);
921 teardown_switches:
922         dsa_tree_teardown_switches(dst);
923 teardown_default_cpu:
924         dsa_tree_teardown_default_cpu(dst);
925
926         return err;
927 }
928
929 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
930 {
931         struct dsa_link *dl, *next;
932
933         if (!dst->setup)
934                 return;
935
936         dsa_tree_teardown_lags(dst);
937
938         dsa_tree_teardown_master(dst);
939
940         dsa_tree_teardown_switches(dst);
941
942         dsa_tree_teardown_default_cpu(dst);
943
944         list_for_each_entry_safe(dl, next, &dst->rtable, list) {
945                 list_del(&dl->list);
946                 kfree(dl);
947         }
948
949         pr_info("DSA: tree %d torn down\n", dst->index);
950
951         dst->setup = false;
952 }
953
954 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
955  * is that all DSA switches within a tree share the same tagger, otherwise
956  * they would have formed disjoint trees (different "dsa,member" values).
957  */
958 int dsa_tree_change_tag_proto(struct dsa_switch_tree *dst,
959                               struct net_device *master,
960                               const struct dsa_device_ops *tag_ops,
961                               const struct dsa_device_ops *old_tag_ops)
962 {
963         struct dsa_notifier_tag_proto_info info;
964         struct dsa_port *dp;
965         int err = -EBUSY;
966
967         if (!rtnl_trylock())
968                 return restart_syscall();
969
970         /* At the moment we don't allow changing the tag protocol under
971          * traffic. The rtnl_mutex also happens to serialize concurrent
972          * attempts to change the tagging protocol. If we ever lift the IFF_UP
973          * restriction, there needs to be another mutex which serializes this.
974          */
975         if (master->flags & IFF_UP)
976                 goto out_unlock;
977
978         list_for_each_entry(dp, &dst->ports, list) {
979                 if (!dsa_is_user_port(dp->ds, dp->index))
980                         continue;
981
982                 if (dp->slave->flags & IFF_UP)
983                         goto out_unlock;
984         }
985
986         info.tag_ops = tag_ops;
987         err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
988         if (err)
989                 goto out_unwind_tagger;
990
991         dst->tag_ops = tag_ops;
992
993         rtnl_unlock();
994
995         return 0;
996
997 out_unwind_tagger:
998         info.tag_ops = old_tag_ops;
999         dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
1000 out_unlock:
1001         rtnl_unlock();
1002         return err;
1003 }
1004
1005 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
1006 {
1007         struct dsa_switch_tree *dst = ds->dst;
1008         struct dsa_port *dp;
1009
1010         list_for_each_entry(dp, &dst->ports, list)
1011                 if (dp->ds == ds && dp->index == index)
1012                         return dp;
1013
1014         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1015         if (!dp)
1016                 return NULL;
1017
1018         dp->ds = ds;
1019         dp->index = index;
1020
1021         INIT_LIST_HEAD(&dp->list);
1022         list_add_tail(&dp->list, &dst->ports);
1023
1024         return dp;
1025 }
1026
1027 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
1028 {
1029         if (!name)
1030                 name = "eth%d";
1031
1032         dp->type = DSA_PORT_TYPE_USER;
1033         dp->name = name;
1034
1035         return 0;
1036 }
1037
1038 static int dsa_port_parse_dsa(struct dsa_port *dp)
1039 {
1040         dp->type = DSA_PORT_TYPE_DSA;
1041
1042         return 0;
1043 }
1044
1045 static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp,
1046                                                   struct net_device *master)
1047 {
1048         enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE;
1049         struct dsa_switch *mds, *ds = dp->ds;
1050         unsigned int mdp_upstream;
1051         struct dsa_port *mdp;
1052
1053         /* It is possible to stack DSA switches onto one another when that
1054          * happens the switch driver may want to know if its tagging protocol
1055          * is going to work in such a configuration.
1056          */
1057         if (dsa_slave_dev_check(master)) {
1058                 mdp = dsa_slave_to_port(master);
1059                 mds = mdp->ds;
1060                 mdp_upstream = dsa_upstream_port(mds, mdp->index);
1061                 tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream,
1062                                                           DSA_TAG_PROTO_NONE);
1063         }
1064
1065         /* If the master device is not itself a DSA slave in a disjoint DSA
1066          * tree, then return immediately.
1067          */
1068         return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol);
1069 }
1070
1071 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
1072 {
1073         struct dsa_switch *ds = dp->ds;
1074         struct dsa_switch_tree *dst = ds->dst;
1075         const struct dsa_device_ops *tag_ops;
1076         enum dsa_tag_protocol tag_protocol;
1077
1078         tag_protocol = dsa_get_tag_protocol(dp, master);
1079         if (dst->tag_ops) {
1080                 if (dst->tag_ops->proto != tag_protocol) {
1081                         dev_err(ds->dev,
1082                                 "A DSA switch tree can have only one tagging protocol\n");
1083                         return -EINVAL;
1084                 }
1085                 /* In the case of multiple CPU ports per switch, the tagging
1086                  * protocol is still reference-counted only per switch tree, so
1087                  * nothing to do here.
1088                  */
1089         } else {
1090                 tag_ops = dsa_tag_driver_get(tag_protocol);
1091                 if (IS_ERR(tag_ops)) {
1092                         if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
1093                                 return -EPROBE_DEFER;
1094                         dev_warn(ds->dev, "No tagger for this switch\n");
1095                         dp->master = NULL;
1096                         return PTR_ERR(tag_ops);
1097                 }
1098
1099                 dst->tag_ops = tag_ops;
1100         }
1101
1102         dp->master = master;
1103         dp->type = DSA_PORT_TYPE_CPU;
1104         dsa_port_set_tag_protocol(dp, dst->tag_ops);
1105         dp->dst = dst;
1106
1107         return 0;
1108 }
1109
1110 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
1111 {
1112         struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
1113         const char *name = of_get_property(dn, "label", NULL);
1114         bool link = of_property_read_bool(dn, "link");
1115
1116         dp->dn = dn;
1117
1118         if (ethernet) {
1119                 struct net_device *master;
1120
1121                 master = of_find_net_device_by_node(ethernet);
1122                 if (!master)
1123                         return -EPROBE_DEFER;
1124
1125                 return dsa_port_parse_cpu(dp, master);
1126         }
1127
1128         if (link)
1129                 return dsa_port_parse_dsa(dp);
1130
1131         return dsa_port_parse_user(dp, name);
1132 }
1133
1134 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
1135                                      struct device_node *dn)
1136 {
1137         struct device_node *ports, *port;
1138         struct dsa_port *dp;
1139         int err = 0;
1140         u32 reg;
1141
1142         ports = of_get_child_by_name(dn, "ports");
1143         if (!ports) {
1144                 /* The second possibility is "ethernet-ports" */
1145                 ports = of_get_child_by_name(dn, "ethernet-ports");
1146                 if (!ports) {
1147                         dev_err(ds->dev, "no ports child node found\n");
1148                         return -EINVAL;
1149                 }
1150         }
1151
1152         for_each_available_child_of_node(ports, port) {
1153                 err = of_property_read_u32(port, "reg", &reg);
1154                 if (err)
1155                         goto out_put_node;
1156
1157                 if (reg >= ds->num_ports) {
1158                         dev_err(ds->dev, "port %pOF index %u exceeds num_ports (%zu)\n",
1159                                 port, reg, ds->num_ports);
1160                         err = -EINVAL;
1161                         goto out_put_node;
1162                 }
1163
1164                 dp = dsa_to_port(ds, reg);
1165
1166                 err = dsa_port_parse_of(dp, port);
1167                 if (err)
1168                         goto out_put_node;
1169         }
1170
1171 out_put_node:
1172         of_node_put(ports);
1173         return err;
1174 }
1175
1176 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
1177                                       struct device_node *dn)
1178 {
1179         u32 m[2] = { 0, 0 };
1180         int sz;
1181
1182         /* Don't error out if this optional property isn't found */
1183         sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
1184         if (sz < 0 && sz != -EINVAL)
1185                 return sz;
1186
1187         ds->index = m[1];
1188
1189         ds->dst = dsa_tree_touch(m[0]);
1190         if (!ds->dst)
1191                 return -ENOMEM;
1192
1193         return 0;
1194 }
1195
1196 static int dsa_switch_touch_ports(struct dsa_switch *ds)
1197 {
1198         struct dsa_port *dp;
1199         int port;
1200
1201         for (port = 0; port < ds->num_ports; port++) {
1202                 dp = dsa_port_touch(ds, port);
1203                 if (!dp)
1204                         return -ENOMEM;
1205         }
1206
1207         return 0;
1208 }
1209
1210 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
1211 {
1212         int err;
1213
1214         err = dsa_switch_parse_member_of(ds, dn);
1215         if (err)
1216                 return err;
1217
1218         err = dsa_switch_touch_ports(ds);
1219         if (err)
1220                 return err;
1221
1222         return dsa_switch_parse_ports_of(ds, dn);
1223 }
1224
1225 static int dsa_port_parse(struct dsa_port *dp, const char *name,
1226                           struct device *dev)
1227 {
1228         if (!strcmp(name, "cpu")) {
1229                 struct net_device *master;
1230
1231                 master = dsa_dev_to_net_device(dev);
1232                 if (!master)
1233                         return -EPROBE_DEFER;
1234
1235                 dev_put(master);
1236
1237                 return dsa_port_parse_cpu(dp, master);
1238         }
1239
1240         if (!strcmp(name, "dsa"))
1241                 return dsa_port_parse_dsa(dp);
1242
1243         return dsa_port_parse_user(dp, name);
1244 }
1245
1246 static int dsa_switch_parse_ports(struct dsa_switch *ds,
1247                                   struct dsa_chip_data *cd)
1248 {
1249         bool valid_name_found = false;
1250         struct dsa_port *dp;
1251         struct device *dev;
1252         const char *name;
1253         unsigned int i;
1254         int err;
1255
1256         for (i = 0; i < DSA_MAX_PORTS; i++) {
1257                 name = cd->port_names[i];
1258                 dev = cd->netdev[i];
1259                 dp = dsa_to_port(ds, i);
1260
1261                 if (!name)
1262                         continue;
1263
1264                 err = dsa_port_parse(dp, name, dev);
1265                 if (err)
1266                         return err;
1267
1268                 valid_name_found = true;
1269         }
1270
1271         if (!valid_name_found && i == DSA_MAX_PORTS)
1272                 return -EINVAL;
1273
1274         return 0;
1275 }
1276
1277 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
1278 {
1279         int err;
1280
1281         ds->cd = cd;
1282
1283         /* We don't support interconnected switches nor multiple trees via
1284          * platform data, so this is the unique switch of the tree.
1285          */
1286         ds->index = 0;
1287         ds->dst = dsa_tree_touch(0);
1288         if (!ds->dst)
1289                 return -ENOMEM;
1290
1291         err = dsa_switch_touch_ports(ds);
1292         if (err)
1293                 return err;
1294
1295         return dsa_switch_parse_ports(ds, cd);
1296 }
1297
1298 static void dsa_switch_release_ports(struct dsa_switch *ds)
1299 {
1300         struct dsa_switch_tree *dst = ds->dst;
1301         struct dsa_port *dp, *next;
1302
1303         list_for_each_entry_safe(dp, next, &dst->ports, list) {
1304                 if (dp->ds != ds)
1305                         continue;
1306                 list_del(&dp->list);
1307                 kfree(dp);
1308         }
1309 }
1310
1311 static int dsa_switch_probe(struct dsa_switch *ds)
1312 {
1313         struct dsa_switch_tree *dst;
1314         struct dsa_chip_data *pdata;
1315         struct device_node *np;
1316         int err;
1317
1318         if (!ds->dev)
1319                 return -ENODEV;
1320
1321         pdata = ds->dev->platform_data;
1322         np = ds->dev->of_node;
1323
1324         if (!ds->num_ports)
1325                 return -EINVAL;
1326
1327         if (np) {
1328                 err = dsa_switch_parse_of(ds, np);
1329                 if (err)
1330                         dsa_switch_release_ports(ds);
1331         } else if (pdata) {
1332                 err = dsa_switch_parse(ds, pdata);
1333                 if (err)
1334                         dsa_switch_release_ports(ds);
1335         } else {
1336                 err = -ENODEV;
1337         }
1338
1339         if (err)
1340                 return err;
1341
1342         dst = ds->dst;
1343         dsa_tree_get(dst);
1344         err = dsa_tree_setup(dst);
1345         if (err) {
1346                 dsa_switch_release_ports(ds);
1347                 dsa_tree_put(dst);
1348         }
1349
1350         return err;
1351 }
1352
1353 int dsa_register_switch(struct dsa_switch *ds)
1354 {
1355         int err;
1356
1357         mutex_lock(&dsa2_mutex);
1358         err = dsa_switch_probe(ds);
1359         dsa_tree_put(ds->dst);
1360         mutex_unlock(&dsa2_mutex);
1361
1362         return err;
1363 }
1364 EXPORT_SYMBOL_GPL(dsa_register_switch);
1365
1366 static void dsa_switch_remove(struct dsa_switch *ds)
1367 {
1368         struct dsa_switch_tree *dst = ds->dst;
1369
1370         dsa_tree_teardown(dst);
1371         dsa_switch_release_ports(ds);
1372         dsa_tree_put(dst);
1373 }
1374
1375 void dsa_unregister_switch(struct dsa_switch *ds)
1376 {
1377         mutex_lock(&dsa2_mutex);
1378         dsa_switch_remove(ds);
1379         mutex_unlock(&dsa2_mutex);
1380 }
1381 EXPORT_SYMBOL_GPL(dsa_unregister_switch);