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
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 struct dsa_link *dsa_link_touch(struct dsa_port *dp, struct dsa_port *link_dp)
129 struct dsa_switch *ds = dp->ds;
130 struct dsa_switch_tree *dst;
135 list_for_each_entry(dl, &dst->rtable, list)
136 if (dl->dp == dp && dl->link_dp == link_dp)
139 dl = kzalloc(sizeof(*dl), GFP_KERNEL);
144 dl->link_dp = link_dp;
146 INIT_LIST_HEAD(&dl->list);
147 list_add_tail(&dl->list, &dst->rtable);
152 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
154 struct dsa_switch *ds = dp->ds;
155 struct dsa_switch_tree *dst = ds->dst;
156 struct device_node *dn = dp->dn;
157 struct of_phandle_iterator it;
158 struct dsa_port *link_dp;
162 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
163 link_dp = dsa_tree_find_port_by_node(dst, it.node);
165 of_node_put(it.node);
169 dl = dsa_link_touch(dp, link_dp);
171 of_node_put(it.node);
179 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
181 bool complete = true;
184 list_for_each_entry(dp, &dst->ports, list) {
185 if (dsa_port_is_dsa(dp)) {
186 complete = dsa_port_setup_routing_table(dp);
195 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
199 list_for_each_entry(dp, &dst->ports, list)
200 if (dsa_port_is_cpu(dp))
206 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
208 struct dsa_port *cpu_dp, *dp;
210 cpu_dp = dsa_tree_find_first_cpu(dst);
212 pr_err("DSA: tree %d has no CPU port\n", dst->index);
216 /* Assign the default CPU port to all ports of the fabric */
217 list_for_each_entry(dp, &dst->ports, list)
218 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
224 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
228 list_for_each_entry(dp, &dst->ports, list)
229 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
233 static int dsa_port_setup(struct dsa_port *dp)
235 struct dsa_switch *ds = dp->ds;
236 struct dsa_switch_tree *dst = ds->dst;
237 const unsigned char *id = (const unsigned char *)&dst->index;
238 const unsigned char len = sizeof(dst->index);
239 struct devlink_port *dlp = &dp->devlink_port;
240 bool dsa_port_link_registered = false;
241 bool devlink_port_registered = false;
242 struct devlink *dl = ds->devlink;
243 bool dsa_port_enabled = false;
250 case DSA_PORT_TYPE_UNUSED:
251 dsa_port_disable(dp);
253 case DSA_PORT_TYPE_CPU:
254 memset(dlp, 0, sizeof(*dlp));
255 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_CPU,
256 dp->index, false, 0, id, len);
257 err = devlink_port_register(dl, dlp, dp->index);
260 devlink_port_registered = true;
262 err = dsa_port_link_register_of(dp);
265 dsa_port_link_registered = true;
267 err = dsa_port_enable(dp, NULL);
270 dsa_port_enabled = true;
273 case DSA_PORT_TYPE_DSA:
274 memset(dlp, 0, sizeof(*dlp));
275 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_DSA,
276 dp->index, false, 0, id, len);
277 err = devlink_port_register(dl, dlp, dp->index);
280 devlink_port_registered = true;
282 err = dsa_port_link_register_of(dp);
285 dsa_port_link_registered = true;
287 err = dsa_port_enable(dp, NULL);
290 dsa_port_enabled = true;
293 case DSA_PORT_TYPE_USER:
294 memset(dlp, 0, sizeof(*dlp));
295 devlink_port_attrs_set(dlp, DEVLINK_PORT_FLAVOUR_PHYSICAL,
296 dp->index, false, 0, id, len);
297 err = devlink_port_register(dl, dlp, dp->index);
300 devlink_port_registered = true;
302 dp->mac = of_get_mac_address(dp->dn);
303 err = dsa_slave_create(dp);
307 devlink_port_type_eth_set(dlp, dp->slave);
311 if (err && dsa_port_enabled)
312 dsa_port_disable(dp);
313 if (err && dsa_port_link_registered)
314 dsa_port_link_unregister_of(dp);
315 if (err && devlink_port_registered)
316 devlink_port_unregister(dlp);
325 static void dsa_port_teardown(struct dsa_port *dp)
327 struct devlink_port *dlp = &dp->devlink_port;
333 case DSA_PORT_TYPE_UNUSED:
335 case DSA_PORT_TYPE_CPU:
336 dsa_port_disable(dp);
337 dsa_tag_driver_put(dp->tag_ops);
338 devlink_port_unregister(dlp);
339 dsa_port_link_unregister_of(dp);
341 case DSA_PORT_TYPE_DSA:
342 dsa_port_disable(dp);
343 devlink_port_unregister(dlp);
344 dsa_port_link_unregister_of(dp);
346 case DSA_PORT_TYPE_USER:
347 devlink_port_unregister(dlp);
349 dsa_slave_destroy(dp->slave);
358 static int dsa_switch_setup(struct dsa_switch *ds)
360 struct dsa_devlink_priv *dl_priv;
366 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
367 * driver and before ops->setup() has run, since the switch drivers and
368 * the slave MDIO bus driver rely on these values for probing PHY
371 ds->phys_mii_mask |= dsa_user_ports(ds);
373 /* Add the switch to devlink before calling setup, so that setup can
376 ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv));
379 dl_priv = devlink_priv(ds->devlink);
382 err = devlink_register(ds->devlink, ds->dev);
386 err = dsa_switch_register_notifier(ds);
388 goto unregister_devlink;
390 err = ds->ops->setup(ds);
392 goto unregister_notifier;
394 devlink_params_publish(ds->devlink);
396 if (!ds->slave_mii_bus && ds->ops->phy_read) {
397 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
398 if (!ds->slave_mii_bus) {
400 goto unregister_notifier;
403 dsa_slave_mii_bus_init(ds);
405 err = mdiobus_register(ds->slave_mii_bus);
407 goto unregister_notifier;
415 dsa_switch_unregister_notifier(ds);
417 devlink_unregister(ds->devlink);
419 devlink_free(ds->devlink);
425 static void dsa_switch_teardown(struct dsa_switch *ds)
430 if (ds->slave_mii_bus && ds->ops->phy_read)
431 mdiobus_unregister(ds->slave_mii_bus);
433 dsa_switch_unregister_notifier(ds);
435 if (ds->ops->teardown)
436 ds->ops->teardown(ds);
439 devlink_unregister(ds->devlink);
440 devlink_free(ds->devlink);
447 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
452 list_for_each_entry(dp, &dst->ports, list) {
453 err = dsa_switch_setup(dp->ds);
458 list_for_each_entry(dp, &dst->ports, list) {
459 err = dsa_port_setup(dp);
467 list_for_each_entry(dp, &dst->ports, list)
468 dsa_port_teardown(dp);
470 list_for_each_entry(dp, &dst->ports, list)
471 dsa_switch_teardown(dp->ds);
476 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
480 list_for_each_entry(dp, &dst->ports, list)
481 dsa_port_teardown(dp);
483 list_for_each_entry(dp, &dst->ports, list)
484 dsa_switch_teardown(dp->ds);
487 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
492 list_for_each_entry(dp, &dst->ports, list) {
493 if (dsa_port_is_cpu(dp)) {
494 err = dsa_master_setup(dp->master, dp);
503 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
507 list_for_each_entry(dp, &dst->ports, list)
508 if (dsa_port_is_cpu(dp))
509 dsa_master_teardown(dp->master);
512 static int dsa_tree_setup(struct dsa_switch_tree *dst)
518 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
523 complete = dsa_tree_setup_routing_table(dst);
527 err = dsa_tree_setup_default_cpu(dst);
531 err = dsa_tree_setup_switches(dst);
533 goto teardown_default_cpu;
535 err = dsa_tree_setup_master(dst);
537 goto teardown_switches;
541 pr_info("DSA: tree %d setup\n", dst->index);
546 dsa_tree_teardown_switches(dst);
547 teardown_default_cpu:
548 dsa_tree_teardown_default_cpu(dst);
553 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
555 struct dsa_link *dl, *next;
560 dsa_tree_teardown_master(dst);
562 dsa_tree_teardown_switches(dst);
564 dsa_tree_teardown_default_cpu(dst);
566 list_for_each_entry_safe(dl, next, &dst->rtable, list) {
571 pr_info("DSA: tree %d torn down\n", dst->index);
576 static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index)
578 struct dsa_switch_tree *dst = ds->dst;
581 list_for_each_entry(dp, &dst->ports, list)
582 if (dp->ds == ds && dp->index == index)
585 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
592 INIT_LIST_HEAD(&dp->list);
593 list_add_tail(&dp->list, &dst->ports);
598 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
603 dp->type = DSA_PORT_TYPE_USER;
609 static int dsa_port_parse_dsa(struct dsa_port *dp)
611 dp->type = DSA_PORT_TYPE_DSA;
616 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
618 struct dsa_switch *ds = dp->ds;
619 struct dsa_switch_tree *dst = ds->dst;
620 const struct dsa_device_ops *tag_ops;
621 enum dsa_tag_protocol tag_protocol;
623 tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
624 tag_ops = dsa_tag_driver_get(tag_protocol);
625 if (IS_ERR(tag_ops)) {
626 if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
627 return -EPROBE_DEFER;
628 dev_warn(ds->dev, "No tagger for this switch\n");
629 return PTR_ERR(tag_ops);
632 dp->type = DSA_PORT_TYPE_CPU;
633 dp->filter = tag_ops->filter;
634 dp->rcv = tag_ops->rcv;
635 dp->tag_ops = tag_ops;
642 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
644 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
645 const char *name = of_get_property(dn, "label", NULL);
646 bool link = of_property_read_bool(dn, "link");
651 struct net_device *master;
653 master = of_find_net_device_by_node(ethernet);
655 return -EPROBE_DEFER;
657 return dsa_port_parse_cpu(dp, master);
661 return dsa_port_parse_dsa(dp);
663 return dsa_port_parse_user(dp, name);
666 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
667 struct device_node *dn)
669 struct device_node *ports, *port;
674 ports = of_get_child_by_name(dn, "ports");
676 dev_err(ds->dev, "no ports child node found\n");
680 for_each_available_child_of_node(ports, port) {
681 err = of_property_read_u32(port, "reg", ®);
685 if (reg >= ds->num_ports) {
690 dp = dsa_to_port(ds, reg);
692 err = dsa_port_parse_of(dp, port);
702 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
703 struct device_node *dn)
708 /* Don't error out if this optional property isn't found */
709 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
710 if (sz < 0 && sz != -EINVAL)
715 ds->dst = dsa_tree_touch(m[0]);
722 static int dsa_switch_touch_ports(struct dsa_switch *ds)
727 for (port = 0; port < ds->num_ports; port++) {
728 dp = dsa_port_touch(ds, port);
736 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
740 err = dsa_switch_parse_member_of(ds, dn);
744 err = dsa_switch_touch_ports(ds);
748 return dsa_switch_parse_ports_of(ds, dn);
751 static int dsa_port_parse(struct dsa_port *dp, const char *name,
754 if (!strcmp(name, "cpu")) {
755 struct net_device *master;
757 master = dsa_dev_to_net_device(dev);
759 return -EPROBE_DEFER;
763 return dsa_port_parse_cpu(dp, master);
766 if (!strcmp(name, "dsa"))
767 return dsa_port_parse_dsa(dp);
769 return dsa_port_parse_user(dp, name);
772 static int dsa_switch_parse_ports(struct dsa_switch *ds,
773 struct dsa_chip_data *cd)
775 bool valid_name_found = false;
782 for (i = 0; i < DSA_MAX_PORTS; i++) {
783 name = cd->port_names[i];
785 dp = dsa_to_port(ds, i);
790 err = dsa_port_parse(dp, name, dev);
794 valid_name_found = true;
797 if (!valid_name_found && i == DSA_MAX_PORTS)
803 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
809 /* We don't support interconnected switches nor multiple trees via
810 * platform data, so this is the unique switch of the tree.
813 ds->dst = dsa_tree_touch(0);
817 err = dsa_switch_touch_ports(ds);
821 return dsa_switch_parse_ports(ds, cd);
824 static int dsa_switch_probe(struct dsa_switch *ds)
826 struct dsa_switch_tree *dst;
827 struct dsa_chip_data *pdata;
828 struct device_node *np;
834 pdata = ds->dev->platform_data;
835 np = ds->dev->of_node;
841 err = dsa_switch_parse_of(ds, np);
843 err = dsa_switch_parse(ds, pdata);
852 err = dsa_tree_setup(dst);
859 int dsa_register_switch(struct dsa_switch *ds)
863 mutex_lock(&dsa2_mutex);
864 err = dsa_switch_probe(ds);
865 dsa_tree_put(ds->dst);
866 mutex_unlock(&dsa2_mutex);
870 EXPORT_SYMBOL_GPL(dsa_register_switch);
872 static void dsa_switch_remove(struct dsa_switch *ds)
874 struct dsa_switch_tree *dst = ds->dst;
875 struct dsa_port *dp, *next;
877 dsa_tree_teardown(dst);
879 list_for_each_entry_safe(dp, next, &dst->ports, list) {
887 void dsa_unregister_switch(struct dsa_switch *ds)
889 mutex_lock(&dsa2_mutex);
890 dsa_switch_remove(ds);
891 mutex_unlock(&dsa2_mutex);
893 EXPORT_SYMBOL_GPL(dsa_unregister_switch);