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