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