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