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