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->list);
49 list_add_tail(&dsa_tree_list, &dst->list);
51 kref_init(&dst->refcount);
56 static void dsa_tree_free(struct dsa_switch_tree *dst)
62 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
65 kref_get(&dst->refcount);
70 static struct dsa_switch_tree *dsa_tree_touch(int index)
72 struct dsa_switch_tree *dst;
74 dst = dsa_tree_find(index);
76 return dsa_tree_get(dst);
78 return dsa_tree_alloc(index);
81 static void dsa_tree_release(struct kref *ref)
83 struct dsa_switch_tree *dst;
85 dst = container_of(ref, struct dsa_switch_tree, refcount);
90 static void dsa_tree_put(struct dsa_switch_tree *dst)
93 kref_put(&dst->refcount, dsa_tree_release);
96 static bool dsa_port_is_dsa(struct dsa_port *port)
98 return port->type == DSA_PORT_TYPE_DSA;
101 static bool dsa_port_is_cpu(struct dsa_port *port)
103 return port->type == DSA_PORT_TYPE_CPU;
106 static bool dsa_port_is_user(struct dsa_port *dp)
108 return dp->type == DSA_PORT_TYPE_USER;
111 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
112 struct device_node *dn)
114 struct dsa_switch *ds;
118 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
119 ds = dst->ds[device];
123 for (port = 0; port < ds->num_ports; port++) {
124 dp = &ds->ports[port];
134 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
136 struct dsa_switch *ds = dp->ds;
137 struct dsa_switch_tree *dst = ds->dst;
138 struct device_node *dn = dp->dn;
139 struct of_phandle_iterator it;
140 struct dsa_port *link_dp;
143 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
144 link_dp = dsa_tree_find_port_by_node(dst, it.node);
146 of_node_put(it.node);
150 ds->rtable[link_dp->ds->index] = dp->index;
156 static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
158 bool complete = true;
162 for (i = 0; i < DSA_MAX_SWITCHES; i++)
163 ds->rtable[i] = DSA_RTABLE_NONE;
165 for (i = 0; i < ds->num_ports; i++) {
168 if (dsa_port_is_dsa(dp)) {
169 complete = dsa_port_setup_routing_table(dp);
178 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
180 struct dsa_switch *ds;
181 bool complete = true;
184 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
185 ds = dst->ds[device];
189 complete = dsa_switch_setup_routing_table(ds);
197 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
199 struct dsa_switch *ds;
203 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
204 ds = dst->ds[device];
208 for (port = 0; port < ds->num_ports; port++) {
209 dp = &ds->ports[port];
211 if (dsa_port_is_cpu(dp))
219 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
221 struct dsa_switch *ds;
225 /* DSA currently only supports a single CPU port */
226 dst->cpu_dp = dsa_tree_find_first_cpu(dst);
228 pr_warn("Tree has no master device\n");
232 /* Assign the default CPU port to all ports of the fabric */
233 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
234 ds = dst->ds[device];
238 for (port = 0; port < ds->num_ports; port++) {
239 dp = &ds->ports[port];
241 if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp))
242 dp->cpu_dp = dst->cpu_dp;
249 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
251 /* DSA currently only supports a single CPU port */
255 static int dsa_port_setup(struct dsa_port *dp)
257 enum devlink_port_flavour flavour;
258 struct dsa_switch *ds = dp->ds;
259 struct dsa_switch_tree *dst = ds->dst;
262 if (dp->type == DSA_PORT_TYPE_UNUSED)
265 memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
266 dp->mac = of_get_mac_address(dp->dn);
269 case DSA_PORT_TYPE_CPU:
270 flavour = DEVLINK_PORT_FLAVOUR_CPU;
272 case DSA_PORT_TYPE_DSA:
273 flavour = DEVLINK_PORT_FLAVOUR_DSA;
275 case DSA_PORT_TYPE_USER: /* fall-through */
277 flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
281 /* dp->index is used now as port_number. However
282 * CPU and DSA ports should have separate numbering
283 * independent from front panel port numbers.
285 devlink_port_attrs_set(&dp->devlink_port, flavour,
287 (const char *) &dst->index, sizeof(dst->index));
288 err = devlink_port_register(ds->devlink, &dp->devlink_port,
294 case DSA_PORT_TYPE_UNUSED:
296 case DSA_PORT_TYPE_CPU:
297 err = dsa_port_link_register_of(dp);
299 dev_err(ds->dev, "failed to setup link for port %d.%d\n",
300 ds->index, dp->index);
302 case DSA_PORT_TYPE_DSA:
303 err = dsa_port_link_register_of(dp);
305 dev_err(ds->dev, "failed to setup link for port %d.%d\n",
306 ds->index, dp->index);
308 case DSA_PORT_TYPE_USER:
309 err = dsa_slave_create(dp);
311 dev_err(ds->dev, "failed to create slave for port %d.%d\n",
312 ds->index, dp->index);
314 devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
319 devlink_port_unregister(&dp->devlink_port);
324 static void dsa_port_teardown(struct dsa_port *dp)
326 if (dp->type != DSA_PORT_TYPE_UNUSED)
327 devlink_port_unregister(&dp->devlink_port);
330 case DSA_PORT_TYPE_UNUSED:
332 case DSA_PORT_TYPE_CPU:
333 dsa_tag_driver_put(dp->tag_ops);
335 case DSA_PORT_TYPE_DSA:
336 dsa_port_link_unregister_of(dp);
338 case DSA_PORT_TYPE_USER:
340 dsa_slave_destroy(dp->slave);
347 static int dsa_switch_setup(struct dsa_switch *ds)
351 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
352 * driver and before ops->setup() has run, since the switch drivers and
353 * the slave MDIO bus driver rely on these values for probing PHY
356 ds->phys_mii_mask |= dsa_user_ports(ds);
358 /* Add the switch to devlink before calling setup, so that setup can
361 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
365 err = devlink_register(ds->devlink, ds->dev);
369 err = dsa_switch_register_notifier(ds);
371 goto unregister_devlink;
373 err = ds->ops->setup(ds);
375 goto unregister_notifier;
377 if (!ds->slave_mii_bus && ds->ops->phy_read) {
378 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
379 if (!ds->slave_mii_bus) {
381 goto unregister_notifier;
384 dsa_slave_mii_bus_init(ds);
386 err = mdiobus_register(ds->slave_mii_bus);
388 goto unregister_notifier;
394 dsa_switch_unregister_notifier(ds);
396 devlink_unregister(ds->devlink);
398 devlink_free(ds->devlink);
404 static void dsa_switch_teardown(struct dsa_switch *ds)
406 if (ds->slave_mii_bus && ds->ops->phy_read)
407 mdiobus_unregister(ds->slave_mii_bus);
409 dsa_switch_unregister_notifier(ds);
412 devlink_unregister(ds->devlink);
413 devlink_free(ds->devlink);
419 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
421 struct dsa_switch *ds;
426 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
427 ds = dst->ds[device];
431 err = dsa_switch_setup(ds);
433 goto switch_teardown;
435 for (port = 0; port < ds->num_ports; port++) {
436 dp = &ds->ports[port];
438 err = dsa_port_setup(dp);
447 for (i = 0; i < port; i++)
448 dsa_port_teardown(&ds->ports[i]);
450 dsa_switch_teardown(ds);
453 for (i = 0; i < device; i++) {
458 for (port = 0; port < ds->num_ports; port++) {
459 dp = &ds->ports[port];
461 dsa_port_teardown(dp);
464 dsa_switch_teardown(ds);
470 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
472 struct dsa_switch *ds;
476 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
477 ds = dst->ds[device];
481 for (port = 0; port < ds->num_ports; port++) {
482 dp = &ds->ports[port];
484 dsa_port_teardown(dp);
487 dsa_switch_teardown(ds);
491 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
493 struct dsa_port *cpu_dp = dst->cpu_dp;
494 struct net_device *master = cpu_dp->master;
496 /* DSA currently supports a single pair of CPU port and master device */
497 return dsa_master_setup(master, cpu_dp);
500 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
502 struct dsa_port *cpu_dp = dst->cpu_dp;
503 struct net_device *master = cpu_dp->master;
505 return dsa_master_teardown(master);
508 static int dsa_tree_setup(struct dsa_switch_tree *dst)
514 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
519 complete = dsa_tree_setup_routing_table(dst);
523 err = dsa_tree_setup_default_cpu(dst);
527 err = dsa_tree_setup_switches(dst);
529 goto teardown_default_cpu;
531 err = dsa_tree_setup_master(dst);
533 goto teardown_switches;
537 pr_info("DSA: tree %d setup\n", dst->index);
542 dsa_tree_teardown_switches(dst);
543 teardown_default_cpu:
544 dsa_tree_teardown_default_cpu(dst);
549 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
554 dsa_tree_teardown_master(dst);
556 dsa_tree_teardown_switches(dst);
558 dsa_tree_teardown_default_cpu(dst);
560 pr_info("DSA: tree %d torn down\n", dst->index);
565 static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
568 dsa_tree_teardown(dst);
570 dst->ds[index] = NULL;
574 static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
575 struct dsa_switch *ds)
577 unsigned int index = ds->index;
586 err = dsa_tree_setup(dst);
588 dst->ds[index] = NULL;
595 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
600 dp->type = DSA_PORT_TYPE_USER;
606 static int dsa_port_parse_dsa(struct dsa_port *dp)
608 dp->type = DSA_PORT_TYPE_DSA;
613 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
615 struct dsa_switch *ds = dp->ds;
616 struct dsa_switch_tree *dst = ds->dst;
617 const struct dsa_device_ops *tag_ops;
618 enum dsa_tag_protocol tag_protocol;
620 tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
621 tag_ops = dsa_tag_driver_get(tag_protocol);
622 if (IS_ERR(tag_ops)) {
623 dev_warn(ds->dev, "No tagger for this switch\n");
624 return PTR_ERR(tag_ops);
627 dp->type = DSA_PORT_TYPE_CPU;
628 dp->filter = tag_ops->filter;
629 dp->rcv = tag_ops->rcv;
630 dp->tag_ops = tag_ops;
637 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
639 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
640 const char *name = of_get_property(dn, "label", NULL);
641 bool link = of_property_read_bool(dn, "link");
646 struct net_device *master;
648 master = of_find_net_device_by_node(ethernet);
650 return -EPROBE_DEFER;
652 return dsa_port_parse_cpu(dp, master);
656 return dsa_port_parse_dsa(dp);
658 return dsa_port_parse_user(dp, name);
661 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
662 struct device_node *dn)
664 struct device_node *ports, *port;
669 ports = of_get_child_by_name(dn, "ports");
671 dev_err(ds->dev, "no ports child node found\n");
675 for_each_available_child_of_node(ports, port) {
676 err = of_property_read_u32(port, "reg", ®);
680 if (reg >= ds->num_ports) {
685 dp = &ds->ports[reg];
687 err = dsa_port_parse_of(dp, port);
697 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
698 struct device_node *dn)
703 /* Don't error out if this optional property isn't found */
704 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
705 if (sz < 0 && sz != -EINVAL)
709 if (ds->index >= DSA_MAX_SWITCHES)
712 ds->dst = dsa_tree_touch(m[0]);
719 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
723 err = dsa_switch_parse_member_of(ds, dn);
727 return dsa_switch_parse_ports_of(ds, dn);
730 static int dsa_port_parse(struct dsa_port *dp, const char *name,
733 if (!strcmp(name, "cpu")) {
734 struct net_device *master;
736 master = dsa_dev_to_net_device(dev);
738 return -EPROBE_DEFER;
742 return dsa_port_parse_cpu(dp, master);
745 if (!strcmp(name, "dsa"))
746 return dsa_port_parse_dsa(dp);
748 return dsa_port_parse_user(dp, name);
751 static int dsa_switch_parse_ports(struct dsa_switch *ds,
752 struct dsa_chip_data *cd)
754 bool valid_name_found = false;
761 for (i = 0; i < DSA_MAX_PORTS; i++) {
762 name = cd->port_names[i];
769 err = dsa_port_parse(dp, name, dev);
773 valid_name_found = true;
776 if (!valid_name_found && i == DSA_MAX_PORTS)
782 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
786 /* We don't support interconnected switches nor multiple trees via
787 * platform data, so this is the unique switch of the tree.
790 ds->dst = dsa_tree_touch(0);
794 return dsa_switch_parse_ports(ds, cd);
797 static int dsa_switch_add(struct dsa_switch *ds)
799 struct dsa_switch_tree *dst = ds->dst;
801 return dsa_tree_add_switch(dst, ds);
804 static int dsa_switch_probe(struct dsa_switch *ds)
806 struct dsa_chip_data *pdata = ds->dev->platform_data;
807 struct device_node *np = ds->dev->of_node;
811 err = dsa_switch_parse_of(ds, np);
813 err = dsa_switch_parse(ds, pdata);
820 return dsa_switch_add(ds);
823 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
825 struct dsa_switch *ds;
828 ds = devm_kzalloc(dev, struct_size(ds, ports, n), GFP_KERNEL);
832 /* We avoid allocating memory outside dsa_switch
833 * if it is not needed.
835 if (n <= sizeof(ds->_bitmap) * 8) {
836 ds->bitmap = &ds->_bitmap;
838 ds->bitmap = devm_kcalloc(dev,
840 sizeof(unsigned long),
842 if (unlikely(!ds->bitmap))
849 for (i = 0; i < ds->num_ports; ++i) {
850 ds->ports[i].index = i;
851 ds->ports[i].ds = ds;
856 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
858 int dsa_register_switch(struct dsa_switch *ds)
862 mutex_lock(&dsa2_mutex);
863 err = dsa_switch_probe(ds);
864 dsa_tree_put(ds->dst);
865 mutex_unlock(&dsa2_mutex);
869 EXPORT_SYMBOL_GPL(dsa_register_switch);
871 static void dsa_switch_remove(struct dsa_switch *ds)
873 struct dsa_switch_tree *dst = ds->dst;
874 unsigned int index = ds->index;
876 dsa_tree_remove_switch(dst, index);
879 void dsa_unregister_switch(struct dsa_switch *ds)
881 mutex_lock(&dsa2_mutex);
882 dsa_switch_remove(ds);
883 mutex_unlock(&dsa2_mutex);
885 EXPORT_SYMBOL_GPL(dsa_unregister_switch);