projects/linux·blob master · refs

1914 lines · 40.8 KiB

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