2 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
3 * Copyright (c) 2008-2009 Marvell Semiconductor
4 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
5 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/list.h>
16 #include <linux/netdevice.h>
17 #include <linux/slab.h>
18 #include <linux/rtnetlink.h>
20 #include <linux/of_net.h>
24 static LIST_HEAD(dsa_tree_list);
25 static DEFINE_MUTEX(dsa2_mutex);
27 static const struct devlink_ops dsa_devlink_ops = {
30 static struct dsa_switch_tree *dsa_tree_find(int index)
32 struct dsa_switch_tree *dst;
34 list_for_each_entry(dst, &dsa_tree_list, list)
35 if (dst->index == index)
41 static struct dsa_switch_tree *dsa_tree_alloc(int index)
43 struct dsa_switch_tree *dst;
45 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
51 INIT_LIST_HEAD(&dst->list);
52 list_add_tail(&dsa_tree_list, &dst->list);
54 kref_init(&dst->refcount);
59 static void dsa_tree_free(struct dsa_switch_tree *dst)
65 static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst)
68 kref_get(&dst->refcount);
73 static struct dsa_switch_tree *dsa_tree_touch(int index)
75 struct dsa_switch_tree *dst;
77 dst = dsa_tree_find(index);
79 return dsa_tree_get(dst);
81 return dsa_tree_alloc(index);
84 static void dsa_tree_release(struct kref *ref)
86 struct dsa_switch_tree *dst;
88 dst = container_of(ref, struct dsa_switch_tree, refcount);
93 static void dsa_tree_put(struct dsa_switch_tree *dst)
96 kref_put(&dst->refcount, dsa_tree_release);
99 static bool dsa_port_is_dsa(struct dsa_port *port)
101 return port->type == DSA_PORT_TYPE_DSA;
104 static bool dsa_port_is_cpu(struct dsa_port *port)
106 return port->type == DSA_PORT_TYPE_CPU;
109 static bool dsa_port_is_user(struct dsa_port *dp)
111 return dp->type == DSA_PORT_TYPE_USER;
114 static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst,
115 struct device_node *dn)
117 struct dsa_switch *ds;
121 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
122 ds = dst->ds[device];
126 for (port = 0; port < ds->num_ports; port++) {
127 dp = &ds->ports[port];
137 static bool dsa_port_setup_routing_table(struct dsa_port *dp)
139 struct dsa_switch *ds = dp->ds;
140 struct dsa_switch_tree *dst = ds->dst;
141 struct device_node *dn = dp->dn;
142 struct of_phandle_iterator it;
143 struct dsa_port *link_dp;
146 of_for_each_phandle(&it, err, dn, "link", NULL, 0) {
147 link_dp = dsa_tree_find_port_by_node(dst, it.node);
149 of_node_put(it.node);
153 ds->rtable[link_dp->ds->index] = dp->index;
159 static bool dsa_switch_setup_routing_table(struct dsa_switch *ds)
161 bool complete = true;
165 for (i = 0; i < DSA_MAX_SWITCHES; i++)
166 ds->rtable[i] = DSA_RTABLE_NONE;
168 for (i = 0; i < ds->num_ports; i++) {
171 if (dsa_port_is_dsa(dp)) {
172 complete = dsa_port_setup_routing_table(dp);
181 static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst)
183 struct dsa_switch *ds;
184 bool complete = true;
187 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
188 ds = dst->ds[device];
192 complete = dsa_switch_setup_routing_table(ds);
200 static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst)
202 struct dsa_switch *ds;
206 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
207 ds = dst->ds[device];
211 for (port = 0; port < ds->num_ports; port++) {
212 dp = &ds->ports[port];
214 if (dsa_port_is_cpu(dp))
222 static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst)
224 struct dsa_switch *ds;
228 /* DSA currently only supports a single CPU port */
229 dst->cpu_dp = dsa_tree_find_first_cpu(dst);
231 pr_warn("Tree has no master device\n");
235 /* Assign the default CPU port to all ports of the fabric */
236 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
237 ds = dst->ds[device];
241 for (port = 0; port < ds->num_ports; port++) {
242 dp = &ds->ports[port];
244 if (dsa_port_is_user(dp))
245 dp->cpu_dp = dst->cpu_dp;
252 static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst)
254 /* DSA currently only supports a single CPU port */
258 static int dsa_port_setup(struct dsa_port *dp)
260 struct dsa_switch *ds = dp->ds;
263 memset(&dp->devlink_port, 0, sizeof(dp->devlink_port));
265 err = devlink_port_register(ds->devlink, &dp->devlink_port, dp->index);
270 case DSA_PORT_TYPE_UNUSED:
272 case DSA_PORT_TYPE_CPU:
273 case DSA_PORT_TYPE_DSA:
274 err = dsa_port_fixed_link_register_of(dp);
276 dev_err(ds->dev, "failed to register fixed link for port %d.%d\n",
277 ds->index, dp->index);
282 case DSA_PORT_TYPE_USER:
283 err = dsa_slave_create(dp);
285 dev_err(ds->dev, "failed to create slave for port %d.%d\n",
286 ds->index, dp->index);
288 devlink_port_type_eth_set(&dp->devlink_port, dp->slave);
295 static void dsa_port_teardown(struct dsa_port *dp)
297 devlink_port_unregister(&dp->devlink_port);
300 case DSA_PORT_TYPE_UNUSED:
302 case DSA_PORT_TYPE_CPU:
303 case DSA_PORT_TYPE_DSA:
304 dsa_port_fixed_link_unregister_of(dp);
306 case DSA_PORT_TYPE_USER:
308 dsa_slave_destroy(dp->slave);
315 static int dsa_switch_setup(struct dsa_switch *ds)
319 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
320 * driver and before ops->setup() has run, since the switch drivers and
321 * the slave MDIO bus driver rely on these values for probing PHY
324 ds->phys_mii_mask |= dsa_user_ports(ds);
326 /* Add the switch to devlink before calling setup, so that setup can
329 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
333 err = devlink_register(ds->devlink, ds->dev);
337 err = ds->ops->setup(ds);
341 err = dsa_switch_register_notifier(ds);
345 if (!ds->slave_mii_bus && ds->ops->phy_read) {
346 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
347 if (!ds->slave_mii_bus)
350 dsa_slave_mii_bus_init(ds);
352 err = mdiobus_register(ds->slave_mii_bus);
360 static void dsa_switch_teardown(struct dsa_switch *ds)
362 if (ds->slave_mii_bus && ds->ops->phy_read)
363 mdiobus_unregister(ds->slave_mii_bus);
365 dsa_switch_unregister_notifier(ds);
368 devlink_unregister(ds->devlink);
369 devlink_free(ds->devlink);
375 static int dsa_tree_setup_switches(struct dsa_switch_tree *dst)
377 struct dsa_switch *ds;
382 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
383 ds = dst->ds[device];
387 err = dsa_switch_setup(ds);
391 for (port = 0; port < ds->num_ports; port++) {
392 dp = &ds->ports[port];
394 err = dsa_port_setup(dp);
403 static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst)
405 struct dsa_switch *ds;
409 for (device = 0; device < DSA_MAX_SWITCHES; device++) {
410 ds = dst->ds[device];
414 for (port = 0; port < ds->num_ports; port++) {
415 dp = &ds->ports[port];
417 dsa_port_teardown(dp);
420 dsa_switch_teardown(ds);
424 static int dsa_tree_setup_master(struct dsa_switch_tree *dst)
426 struct dsa_port *cpu_dp = dst->cpu_dp;
427 struct net_device *master = cpu_dp->master;
429 /* DSA currently supports a single pair of CPU port and master device */
430 return dsa_master_setup(master, cpu_dp);
433 static void dsa_tree_teardown_master(struct dsa_switch_tree *dst)
435 struct dsa_port *cpu_dp = dst->cpu_dp;
436 struct net_device *master = cpu_dp->master;
438 return dsa_master_teardown(master);
441 static int dsa_tree_setup(struct dsa_switch_tree *dst)
447 pr_err("DSA: tree %d already setup! Disjoint trees?\n",
452 complete = dsa_tree_setup_routing_table(dst);
456 err = dsa_tree_setup_default_cpu(dst);
460 err = dsa_tree_setup_switches(dst);
464 err = dsa_tree_setup_master(dst);
470 pr_info("DSA: tree %d setup\n", dst->index);
475 static void dsa_tree_teardown(struct dsa_switch_tree *dst)
480 dsa_tree_teardown_master(dst);
482 dsa_tree_teardown_switches(dst);
484 dsa_tree_teardown_default_cpu(dst);
486 pr_info("DSA: tree %d torn down\n", dst->index);
491 static void dsa_tree_remove_switch(struct dsa_switch_tree *dst,
494 dsa_tree_teardown(dst);
496 dst->ds[index] = NULL;
500 static int dsa_tree_add_switch(struct dsa_switch_tree *dst,
501 struct dsa_switch *ds)
503 unsigned int index = ds->index;
512 err = dsa_tree_setup(dst);
514 dsa_tree_remove_switch(dst, index);
519 static int dsa_port_parse_user(struct dsa_port *dp, const char *name)
524 dp->type = DSA_PORT_TYPE_USER;
530 static int dsa_port_parse_dsa(struct dsa_port *dp)
532 dp->type = DSA_PORT_TYPE_DSA;
537 static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
539 struct dsa_switch *ds = dp->ds;
540 struct dsa_switch_tree *dst = ds->dst;
541 const struct dsa_device_ops *tag_ops;
542 enum dsa_tag_protocol tag_protocol;
544 tag_protocol = ds->ops->get_tag_protocol(ds, dp->index);
545 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
546 if (IS_ERR(tag_ops)) {
547 dev_warn(ds->dev, "No tagger for this switch\n");
548 return PTR_ERR(tag_ops);
551 dp->type = DSA_PORT_TYPE_CPU;
552 dp->rcv = tag_ops->rcv;
553 dp->tag_ops = tag_ops;
560 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
562 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
563 const char *name = of_get_property(dn, "label", NULL);
564 bool link = of_property_read_bool(dn, "link");
569 struct net_device *master;
571 master = of_find_net_device_by_node(ethernet);
573 return -EPROBE_DEFER;
575 return dsa_port_parse_cpu(dp, master);
579 return dsa_port_parse_dsa(dp);
581 return dsa_port_parse_user(dp, name);
584 static int dsa_switch_parse_ports_of(struct dsa_switch *ds,
585 struct device_node *dn)
587 struct device_node *ports, *port;
592 ports = of_get_child_by_name(dn, "ports");
594 dev_err(ds->dev, "no ports child node found\n");
598 for_each_available_child_of_node(ports, port) {
599 err = of_property_read_u32(port, "reg", ®);
603 if (reg >= ds->num_ports)
606 dp = &ds->ports[reg];
608 err = dsa_port_parse_of(dp, port);
616 static int dsa_switch_parse_member_of(struct dsa_switch *ds,
617 struct device_node *dn)
622 /* Don't error out if this optional property isn't found */
623 sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2);
624 if (sz < 0 && sz != -EINVAL)
628 if (ds->index >= DSA_MAX_SWITCHES)
631 ds->dst = dsa_tree_touch(m[0]);
638 static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn)
642 err = dsa_switch_parse_member_of(ds, dn);
646 return dsa_switch_parse_ports_of(ds, dn);
649 static int dsa_port_parse(struct dsa_port *dp, const char *name,
652 if (!strcmp(name, "cpu")) {
653 struct net_device *master;
655 master = dsa_dev_to_net_device(dev);
657 return -EPROBE_DEFER;
661 return dsa_port_parse_cpu(dp, master);
664 if (!strcmp(name, "dsa"))
665 return dsa_port_parse_dsa(dp);
667 return dsa_port_parse_user(dp, name);
670 static int dsa_switch_parse_ports(struct dsa_switch *ds,
671 struct dsa_chip_data *cd)
673 bool valid_name_found = false;
680 for (i = 0; i < DSA_MAX_PORTS; i++) {
681 name = cd->port_names[i];
688 err = dsa_port_parse(dp, name, dev);
692 valid_name_found = true;
695 if (!valid_name_found && i == DSA_MAX_PORTS)
701 static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd)
705 /* We don't support interconnected switches nor multiple trees via
706 * platform data, so this is the unique switch of the tree.
709 ds->dst = dsa_tree_touch(0);
713 return dsa_switch_parse_ports(ds, cd);
716 static int dsa_switch_add(struct dsa_switch *ds)
718 struct dsa_switch_tree *dst = ds->dst;
720 return dsa_tree_add_switch(dst, ds);
723 static int dsa_switch_probe(struct dsa_switch *ds)
725 struct dsa_chip_data *pdata = ds->dev->platform_data;
726 struct device_node *np = ds->dev->of_node;
730 err = dsa_switch_parse_of(ds, np);
732 err = dsa_switch_parse(ds, pdata);
739 return dsa_switch_add(ds);
742 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
744 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
745 struct dsa_switch *ds;
748 ds = devm_kzalloc(dev, size, GFP_KERNEL);
755 for (i = 0; i < ds->num_ports; ++i) {
756 ds->ports[i].index = i;
757 ds->ports[i].ds = ds;
762 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
764 int dsa_register_switch(struct dsa_switch *ds)
768 mutex_lock(&dsa2_mutex);
769 err = dsa_switch_probe(ds);
770 dsa_tree_put(ds->dst);
771 mutex_unlock(&dsa2_mutex);
775 EXPORT_SYMBOL_GPL(dsa_register_switch);
777 static void dsa_switch_remove(struct dsa_switch *ds)
779 struct dsa_switch_tree *dst = ds->dst;
780 unsigned int index = ds->index;
782 dsa_tree_remove_switch(dst, index);
785 void dsa_unregister_switch(struct dsa_switch *ds)
787 mutex_lock(&dsa2_mutex);
788 dsa_switch_remove(ds);
789 mutex_unlock(&dsa2_mutex);
791 EXPORT_SYMBOL_GPL(dsa_unregister_switch);