1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
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>
16 #include <linux/of_net.h>
17 #include <net/devlink.h>
21 static LIST_HEAD(dsa_tree_list
);
22 static DEFINE_MUTEX(dsa2_mutex
);
24 static const struct devlink_ops dsa_devlink_ops
= {
27 static struct dsa_switch_tree
*dsa_tree_find(int index
)
29 struct dsa_switch_tree
*dst
;
31 list_for_each_entry(dst
, &dsa_tree_list
, list
)
32 if (dst
->index
== index
)
38 static struct dsa_switch_tree
*dsa_tree_alloc(int index
)
40 struct dsa_switch_tree
*dst
;
42 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
48 INIT_LIST_HEAD(&dst
->rtable
);
50 INIT_LIST_HEAD(&dst
->ports
);
52 INIT_LIST_HEAD(&dst
->list
);
53 list_add_tail(&dst
->list
, &dsa_tree_list
);
55 kref_init(&dst
->refcount
);
60 static void dsa_tree_free(struct dsa_switch_tree
*dst
)
66 static struct dsa_switch_tree
*dsa_tree_get(struct dsa_switch_tree
*dst
)
69 kref_get(&dst
->refcount
);
74 static struct dsa_switch_tree
*dsa_tree_touch(int index
)
76 struct dsa_switch_tree
*dst
;
78 dst
= dsa_tree_find(index
);
80 return dsa_tree_get(dst
);
82 return dsa_tree_alloc(index
);
85 static void dsa_tree_release(struct kref
*ref
)
87 struct dsa_switch_tree
*dst
;
89 dst
= container_of(ref
, struct dsa_switch_tree
, refcount
);
94 static void dsa_tree_put(struct dsa_switch_tree
*dst
)
97 kref_put(&dst
->refcount
, dsa_tree_release
);
100 static bool dsa_port_is_dsa(struct dsa_port
*port
)
102 return port
->type
== DSA_PORT_TYPE_DSA
;
105 static bool dsa_port_is_cpu(struct dsa_port
*port
)
107 return port
->type
== DSA_PORT_TYPE_CPU
;
110 static bool dsa_port_is_user(struct dsa_port
*dp
)
112 return dp
->type
== DSA_PORT_TYPE_USER
;
115 static struct dsa_port
*dsa_tree_find_port_by_node(struct dsa_switch_tree
*dst
,
116 struct device_node
*dn
)
120 list_for_each_entry(dp
, &dst
->ports
, list
)
127 static struct dsa_link
*dsa_link_touch(struct dsa_port
*dp
,
128 struct dsa_port
*link_dp
)
130 struct dsa_switch
*ds
= dp
->ds
;
131 struct dsa_switch_tree
*dst
;
136 list_for_each_entry(dl
, &dst
->rtable
, list
)
137 if (dl
->dp
== dp
&& dl
->link_dp
== link_dp
)
140 dl
= kzalloc(sizeof(*dl
), GFP_KERNEL
);
145 dl
->link_dp
= link_dp
;
147 INIT_LIST_HEAD(&dl
->list
);
148 list_add_tail(&dl
->list
, &dst
->rtable
);
153 static bool dsa_port_setup_routing_table(struct dsa_port
*dp
)
155 struct dsa_switch
*ds
= dp
->ds
;
156 struct dsa_switch_tree
*dst
= ds
->dst
;
157 struct device_node
*dn
= dp
->dn
;
158 struct of_phandle_iterator it
;
159 struct dsa_port
*link_dp
;
163 of_for_each_phandle(&it
, err
, dn
, "link", NULL
, 0) {
164 link_dp
= dsa_tree_find_port_by_node(dst
, it
.node
);
166 of_node_put(it
.node
);
170 dl
= dsa_link_touch(dp
, link_dp
);
172 of_node_put(it
.node
);
180 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree
*dst
)
182 bool complete
= true;
185 list_for_each_entry(dp
, &dst
->ports
, list
) {
186 if (dsa_port_is_dsa(dp
)) {
187 complete
= dsa_port_setup_routing_table(dp
);
196 static struct dsa_port
*dsa_tree_find_first_cpu(struct dsa_switch_tree
*dst
)
200 list_for_each_entry(dp
, &dst
->ports
, list
)
201 if (dsa_port_is_cpu(dp
))
207 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree
*dst
)
209 struct dsa_port
*cpu_dp
, *dp
;
211 cpu_dp
= dsa_tree_find_first_cpu(dst
);
213 pr_err("DSA: tree %d has no CPU port\n", dst
->index
);
217 /* Assign the default CPU port to all ports of the fabric */
218 list_for_each_entry(dp
, &dst
->ports
, list
)
219 if (dsa_port_is_user(dp
) || dsa_port_is_dsa(dp
))
225 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree
*dst
)
229 list_for_each_entry(dp
, &dst
->ports
, list
)
230 if (dsa_port_is_user(dp
) || dsa_port_is_dsa(dp
))
234 static int dsa_port_setup(struct dsa_port
*dp
)
236 struct dsa_switch
*ds
= dp
->ds
;
237 struct dsa_switch_tree
*dst
= ds
->dst
;
238 const unsigned char *id
= (const unsigned char *)&dst
->index
;
239 const unsigned char len
= sizeof(dst
->index
);
240 struct devlink_port
*dlp
= &dp
->devlink_port
;
241 bool dsa_port_link_registered
= false;
242 bool devlink_port_registered
= false;
243 struct devlink
*dl
= ds
->devlink
;
244 bool dsa_port_enabled
= false;
251 case DSA_PORT_TYPE_UNUSED
:
252 dsa_port_disable(dp
);
254 case DSA_PORT_TYPE_CPU
:
255 memset(dlp
, 0, sizeof(*dlp
));
256 devlink_port_attrs_set(dlp
, DEVLINK_PORT_FLAVOUR_CPU
,
257 dp
->index
, false, 0, id
, len
);
258 err
= devlink_port_register(dl
, dlp
, dp
->index
);
261 devlink_port_registered
= true;
263 err
= dsa_port_link_register_of(dp
);
266 dsa_port_link_registered
= true;
268 err
= dsa_port_enable(dp
, NULL
);
271 dsa_port_enabled
= true;
274 case DSA_PORT_TYPE_DSA
:
275 memset(dlp
, 0, sizeof(*dlp
));
276 devlink_port_attrs_set(dlp
, DEVLINK_PORT_FLAVOUR_DSA
,
277 dp
->index
, false, 0, id
, len
);
278 err
= devlink_port_register(dl
, dlp
, dp
->index
);
281 devlink_port_registered
= true;
283 err
= dsa_port_link_register_of(dp
);
286 dsa_port_link_registered
= true;
288 err
= dsa_port_enable(dp
, NULL
);
291 dsa_port_enabled
= true;
294 case DSA_PORT_TYPE_USER
:
295 memset(dlp
, 0, sizeof(*dlp
));
296 devlink_port_attrs_set(dlp
, DEVLINK_PORT_FLAVOUR_PHYSICAL
,
297 dp
->index
, false, 0, id
, len
);
298 err
= devlink_port_register(dl
, dlp
, dp
->index
);
301 devlink_port_registered
= true;
303 dp
->mac
= of_get_mac_address(dp
->dn
);
304 err
= dsa_slave_create(dp
);
308 devlink_port_type_eth_set(dlp
, dp
->slave
);
312 if (err
&& dsa_port_enabled
)
313 dsa_port_disable(dp
);
314 if (err
&& dsa_port_link_registered
)
315 dsa_port_link_unregister_of(dp
);
316 if (err
&& devlink_port_registered
)
317 devlink_port_unregister(dlp
);
326 static void dsa_port_teardown(struct dsa_port
*dp
)
328 struct devlink_port
*dlp
= &dp
->devlink_port
;
334 case DSA_PORT_TYPE_UNUSED
:
336 case DSA_PORT_TYPE_CPU
:
337 dsa_port_disable(dp
);
338 dsa_tag_driver_put(dp
->tag_ops
);
339 devlink_port_unregister(dlp
);
340 dsa_port_link_unregister_of(dp
);
342 case DSA_PORT_TYPE_DSA
:
343 dsa_port_disable(dp
);
344 devlink_port_unregister(dlp
);
345 dsa_port_link_unregister_of(dp
);
347 case DSA_PORT_TYPE_USER
:
348 devlink_port_unregister(dlp
);
350 dsa_slave_destroy(dp
->slave
);
359 static int dsa_switch_setup(struct dsa_switch
*ds
)
361 struct dsa_devlink_priv
*dl_priv
;
367 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
368 * driver and before ops->setup() has run, since the switch drivers and
369 * the slave MDIO bus driver rely on these values for probing PHY
372 ds
->phys_mii_mask
|= dsa_user_ports(ds
);
374 /* Add the switch to devlink before calling setup, so that setup can
377 ds
->devlink
= devlink_alloc(&dsa_devlink_ops
, sizeof(*dl_priv
));
380 dl_priv
= devlink_priv(ds
->devlink
);
383 err
= devlink_register(ds
->devlink
, ds
->dev
);
387 err
= dsa_switch_register_notifier(ds
);
389 goto unregister_devlink
;
391 err
= ds
->ops
->setup(ds
);
393 goto unregister_notifier
;
395 devlink_params_publish(ds
->devlink
);
397 if (!ds
->slave_mii_bus
&& ds
->ops
->phy_read
) {
398 ds
->slave_mii_bus
= devm_mdiobus_alloc(ds
->dev
);
399 if (!ds
->slave_mii_bus
) {
401 goto unregister_notifier
;
404 dsa_slave_mii_bus_init(ds
);
406 err
= mdiobus_register(ds
->slave_mii_bus
);
408 goto unregister_notifier
;
416 dsa_switch_unregister_notifier(ds
);
418 devlink_unregister(ds
->devlink
);
420 devlink_free(ds
->devlink
);
426 static void dsa_switch_teardown(struct dsa_switch
*ds
)
431 if (ds
->slave_mii_bus
&& ds
->ops
->phy_read
)
432 mdiobus_unregister(ds
->slave_mii_bus
);
434 dsa_switch_unregister_notifier(ds
);
436 if (ds
->ops
->teardown
)
437 ds
->ops
->teardown(ds
);
440 devlink_unregister(ds
->devlink
);
441 devlink_free(ds
->devlink
);
448 static int dsa_tree_setup_switches(struct dsa_switch_tree
*dst
)
453 list_for_each_entry(dp
, &dst
->ports
, list
) {
454 err
= dsa_switch_setup(dp
->ds
);
459 list_for_each_entry(dp
, &dst
->ports
, list
) {
460 err
= dsa_port_setup(dp
);
468 list_for_each_entry(dp
, &dst
->ports
, list
)
469 dsa_port_teardown(dp
);
471 list_for_each_entry(dp
, &dst
->ports
, list
)
472 dsa_switch_teardown(dp
->ds
);
477 static void dsa_tree_teardown_switches(struct dsa_switch_tree
*dst
)
481 list_for_each_entry(dp
, &dst
->ports
, list
)
482 dsa_port_teardown(dp
);
484 list_for_each_entry(dp
, &dst
->ports
, list
)
485 dsa_switch_teardown(dp
->ds
);
488 static int dsa_tree_setup_master(struct dsa_switch_tree
*dst
)
493 list_for_each_entry(dp
, &dst
->ports
, list
) {
494 if (dsa_port_is_cpu(dp
)) {
495 err
= dsa_master_setup(dp
->master
, dp
);
504 static void dsa_tree_teardown_master(struct dsa_switch_tree
*dst
)
508 list_for_each_entry(dp
, &dst
->ports
, list
)
509 if (dsa_port_is_cpu(dp
))
510 dsa_master_teardown(dp
->master
);
513 static int dsa_tree_setup(struct dsa_switch_tree
*dst
)
519 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
524 complete
= dsa_tree_setup_routing_table(dst
);
528 err
= dsa_tree_setup_default_cpu(dst
);
532 err
= dsa_tree_setup_switches(dst
);
534 goto teardown_default_cpu
;
536 err
= dsa_tree_setup_master(dst
);
538 goto teardown_switches
;
542 pr_info("DSA: tree %d setup\n", dst
->index
);
547 dsa_tree_teardown_switches(dst
);
548 teardown_default_cpu
:
549 dsa_tree_teardown_default_cpu(dst
);
554 static void dsa_tree_teardown(struct dsa_switch_tree
*dst
)
556 struct dsa_link
*dl
, *next
;
561 dsa_tree_teardown_master(dst
);
563 dsa_tree_teardown_switches(dst
);
565 dsa_tree_teardown_default_cpu(dst
);
567 list_for_each_entry_safe(dl
, next
, &dst
->rtable
, list
) {
572 pr_info("DSA: tree %d torn down\n", dst
->index
);
577 static struct dsa_port
*dsa_port_touch(struct dsa_switch
*ds
, int index
)
579 struct dsa_switch_tree
*dst
= ds
->dst
;
582 list_for_each_entry(dp
, &dst
->ports
, list
)
583 if (dp
->ds
== ds
&& dp
->index
== index
)
586 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
593 INIT_LIST_HEAD(&dp
->list
);
594 list_add_tail(&dp
->list
, &dst
->ports
);
599 static int dsa_port_parse_user(struct dsa_port
*dp
, const char *name
)
604 dp
->type
= DSA_PORT_TYPE_USER
;
610 static int dsa_port_parse_dsa(struct dsa_port
*dp
)
612 dp
->type
= DSA_PORT_TYPE_DSA
;
617 static int dsa_port_parse_cpu(struct dsa_port
*dp
, struct net_device
*master
)
619 struct dsa_switch
*ds
= dp
->ds
;
620 struct dsa_switch_tree
*dst
= ds
->dst
;
621 const struct dsa_device_ops
*tag_ops
;
622 enum dsa_tag_protocol tag_protocol
;
624 tag_protocol
= ds
->ops
->get_tag_protocol(ds
, dp
->index
);
625 tag_ops
= dsa_tag_driver_get(tag_protocol
);
626 if (IS_ERR(tag_ops
)) {
627 if (PTR_ERR(tag_ops
) == -ENOPROTOOPT
)
628 return -EPROBE_DEFER
;
629 dev_warn(ds
->dev
, "No tagger for this switch\n");
630 return PTR_ERR(tag_ops
);
633 dp
->type
= DSA_PORT_TYPE_CPU
;
634 dp
->filter
= tag_ops
->filter
;
635 dp
->rcv
= tag_ops
->rcv
;
636 dp
->tag_ops
= tag_ops
;
643 static int dsa_port_parse_of(struct dsa_port
*dp
, struct device_node
*dn
)
645 struct device_node
*ethernet
= of_parse_phandle(dn
, "ethernet", 0);
646 const char *name
= of_get_property(dn
, "label", NULL
);
647 bool link
= of_property_read_bool(dn
, "link");
652 struct net_device
*master
;
654 master
= of_find_net_device_by_node(ethernet
);
656 return -EPROBE_DEFER
;
658 return dsa_port_parse_cpu(dp
, master
);
662 return dsa_port_parse_dsa(dp
);
664 return dsa_port_parse_user(dp
, name
);
667 static int dsa_switch_parse_ports_of(struct dsa_switch
*ds
,
668 struct device_node
*dn
)
670 struct device_node
*ports
, *port
;
675 ports
= of_get_child_by_name(dn
, "ports");
677 dev_err(ds
->dev
, "no ports child node found\n");
681 for_each_available_child_of_node(ports
, port
) {
682 err
= of_property_read_u32(port
, "reg", ®
);
686 if (reg
>= ds
->num_ports
) {
691 dp
= dsa_to_port(ds
, reg
);
693 err
= dsa_port_parse_of(dp
, port
);
703 static int dsa_switch_parse_member_of(struct dsa_switch
*ds
,
704 struct device_node
*dn
)
709 /* Don't error out if this optional property isn't found */
710 sz
= of_property_read_variable_u32_array(dn
, "dsa,member", m
, 2, 2);
711 if (sz
< 0 && sz
!= -EINVAL
)
716 ds
->dst
= dsa_tree_touch(m
[0]);
723 static int dsa_switch_touch_ports(struct dsa_switch
*ds
)
728 for (port
= 0; port
< ds
->num_ports
; port
++) {
729 dp
= dsa_port_touch(ds
, port
);
737 static int dsa_switch_parse_of(struct dsa_switch
*ds
, struct device_node
*dn
)
741 err
= dsa_switch_parse_member_of(ds
, dn
);
745 err
= dsa_switch_touch_ports(ds
);
749 return dsa_switch_parse_ports_of(ds
, dn
);
752 static int dsa_port_parse(struct dsa_port
*dp
, const char *name
,
755 if (!strcmp(name
, "cpu")) {
756 struct net_device
*master
;
758 master
= dsa_dev_to_net_device(dev
);
760 return -EPROBE_DEFER
;
764 return dsa_port_parse_cpu(dp
, master
);
767 if (!strcmp(name
, "dsa"))
768 return dsa_port_parse_dsa(dp
);
770 return dsa_port_parse_user(dp
, name
);
773 static int dsa_switch_parse_ports(struct dsa_switch
*ds
,
774 struct dsa_chip_data
*cd
)
776 bool valid_name_found
= false;
783 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
784 name
= cd
->port_names
[i
];
786 dp
= dsa_to_port(ds
, i
);
791 err
= dsa_port_parse(dp
, name
, dev
);
795 valid_name_found
= true;
798 if (!valid_name_found
&& i
== DSA_MAX_PORTS
)
804 static int dsa_switch_parse(struct dsa_switch
*ds
, struct dsa_chip_data
*cd
)
810 /* We don't support interconnected switches nor multiple trees via
811 * platform data, so this is the unique switch of the tree.
814 ds
->dst
= dsa_tree_touch(0);
818 err
= dsa_switch_touch_ports(ds
);
822 return dsa_switch_parse_ports(ds
, cd
);
825 static int dsa_switch_probe(struct dsa_switch
*ds
)
827 struct dsa_switch_tree
*dst
;
828 struct dsa_chip_data
*pdata
;
829 struct device_node
*np
;
835 pdata
= ds
->dev
->platform_data
;
836 np
= ds
->dev
->of_node
;
842 err
= dsa_switch_parse_of(ds
, np
);
844 err
= dsa_switch_parse(ds
, pdata
);
853 err
= dsa_tree_setup(dst
);
860 int dsa_register_switch(struct dsa_switch
*ds
)
864 mutex_lock(&dsa2_mutex
);
865 err
= dsa_switch_probe(ds
);
866 dsa_tree_put(ds
->dst
);
867 mutex_unlock(&dsa2_mutex
);
871 EXPORT_SYMBOL_GPL(dsa_register_switch
);
873 static void dsa_switch_remove(struct dsa_switch
*ds
)
875 struct dsa_switch_tree
*dst
= ds
->dst
;
876 struct dsa_port
*dp
, *next
;
878 dsa_tree_teardown(dst
);
880 list_for_each_entry_safe(dp
, next
, &dst
->ports
, list
) {
888 void dsa_unregister_switch(struct dsa_switch
*ds
)
890 mutex_lock(&dsa2_mutex
);
891 dsa_switch_remove(ds
);
892 mutex_unlock(&dsa2_mutex
);
894 EXPORT_SYMBOL_GPL(dsa_unregister_switch
);