2 * net/dsa/dsa2.c - Hardware switch handling, binding version 2
3 * Copyright (c) 2008-2009 Marvell Semiconductor
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/slab.h>
17 #include <linux/rtnetlink.h>
20 #include <linux/of_net.h>
23 static LIST_HEAD(dsa_switch_trees);
24 static DEFINE_MUTEX(dsa2_mutex);
26 static struct dsa_switch_tree *dsa_get_dst(u32 tree)
28 struct dsa_switch_tree *dst;
30 list_for_each_entry(dst, &dsa_switch_trees, list)
31 if (dst->tree == tree) {
32 kref_get(&dst->refcount);
38 static void dsa_free_dst(struct kref *ref)
40 struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
47 static void dsa_put_dst(struct dsa_switch_tree *dst)
49 kref_put(&dst->refcount, dsa_free_dst);
52 static struct dsa_switch_tree *dsa_add_dst(u32 tree)
54 struct dsa_switch_tree *dst;
56 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
60 INIT_LIST_HEAD(&dst->list);
61 list_add_tail(&dsa_switch_trees, &dst->list);
62 kref_init(&dst->refcount);
67 static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
68 struct dsa_switch *ds, u32 index)
70 kref_get(&dst->refcount);
74 static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
75 struct dsa_switch *ds, u32 index)
77 dst->ds[index] = NULL;
78 kref_put(&dst->refcount, dsa_free_dst);
81 static bool dsa_port_is_dsa(struct device_node *port)
83 return !!of_parse_phandle(port, "link", 0);
86 static bool dsa_port_is_cpu(struct device_node *port)
88 return !!of_parse_phandle(port, "ethernet", 0);
91 static bool dsa_ds_find_port(struct dsa_switch *ds,
92 struct device_node *port)
96 for (index = 0; index < DSA_MAX_PORTS; index++)
97 if (ds->ports[index].dn == port)
102 static struct dsa_switch *dsa_dst_find_port(struct dsa_switch_tree *dst,
103 struct device_node *port)
105 struct dsa_switch *ds;
108 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
113 if (dsa_ds_find_port(ds, port))
120 static int dsa_port_complete(struct dsa_switch_tree *dst,
121 struct dsa_switch *src_ds,
122 struct device_node *port,
125 struct device_node *link;
127 struct dsa_switch *dst_ds;
129 for (index = 0;; index++) {
130 link = of_parse_phandle(port, "link", index);
134 dst_ds = dsa_dst_find_port(dst, link);
140 src_ds->rtable[dst_ds->index] = src_port;
146 /* A switch is complete if all the DSA ports phandles point to ports
147 * known in the tree. A return value of 1 means the tree is not
148 * complete. This is not an error condition. A value of 0 is
151 static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
153 struct device_node *port;
157 for (index = 0; index < DSA_MAX_PORTS; index++) {
158 port = ds->ports[index].dn;
162 if (!dsa_port_is_dsa(port))
165 err = dsa_port_complete(dst, ds, port, index);
169 ds->dsa_port_mask |= BIT(index);
175 /* A tree is complete if all the DSA ports phandles point to ports
176 * known in the tree. A return value of 1 means the tree is not
177 * complete. This is not an error condition. A value of 0 is
180 static int dsa_dst_complete(struct dsa_switch_tree *dst)
182 struct dsa_switch *ds;
186 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
191 err = dsa_ds_complete(dst, ds);
199 static int dsa_dsa_port_apply(struct device_node *port, u32 index,
200 struct dsa_switch *ds)
204 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
206 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
214 static void dsa_dsa_port_unapply(struct device_node *port, u32 index,
215 struct dsa_switch *ds)
217 dsa_cpu_dsa_destroy(port);
220 static int dsa_cpu_port_apply(struct device_node *port, u32 index,
221 struct dsa_switch *ds)
225 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
227 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
232 ds->cpu_port_mask |= BIT(index);
237 static void dsa_cpu_port_unapply(struct device_node *port, u32 index,
238 struct dsa_switch *ds)
240 dsa_cpu_dsa_destroy(port);
241 ds->cpu_port_mask &= ~BIT(index);
245 static int dsa_user_port_apply(struct device_node *port, u32 index,
246 struct dsa_switch *ds)
251 name = of_get_property(port, "label", NULL);
255 err = dsa_slave_create(ds, ds->dev, index, name);
257 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
265 static void dsa_user_port_unapply(struct device_node *port, u32 index,
266 struct dsa_switch *ds)
268 if (ds->ports[index].netdev) {
269 dsa_slave_destroy(ds->ports[index].netdev);
270 ds->ports[index].netdev = NULL;
271 ds->enabled_port_mask &= ~(1 << index);
275 static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
277 struct device_node *port;
281 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
282 * driver and before ops->setup() has run, since the switch drivers and
283 * the slave MDIO bus driver rely on these values for probing PHY
286 ds->phys_mii_mask = ds->enabled_port_mask;
288 err = ds->ops->setup(ds);
292 if (ds->ops->set_addr) {
293 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
298 if (!ds->slave_mii_bus && ds->ops->phy_read) {
299 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
300 if (!ds->slave_mii_bus)
303 dsa_slave_mii_bus_init(ds);
305 err = mdiobus_register(ds->slave_mii_bus);
310 for (index = 0; index < DSA_MAX_PORTS; index++) {
311 port = ds->ports[index].dn;
315 if (dsa_port_is_dsa(port)) {
316 err = dsa_dsa_port_apply(port, index, ds);
322 if (dsa_port_is_cpu(port)) {
323 err = dsa_cpu_port_apply(port, index, ds);
329 err = dsa_user_port_apply(port, index, ds);
337 static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
339 struct device_node *port;
342 for (index = 0; index < DSA_MAX_PORTS; index++) {
343 port = ds->ports[index].dn;
347 if (dsa_port_is_dsa(port)) {
348 dsa_dsa_port_unapply(port, index, ds);
352 if (dsa_port_is_cpu(port)) {
353 dsa_cpu_port_unapply(port, index, ds);
357 dsa_user_port_unapply(port, index, ds);
360 if (ds->slave_mii_bus && ds->ops->phy_read)
361 mdiobus_unregister(ds->slave_mii_bus);
364 static int dsa_dst_apply(struct dsa_switch_tree *dst)
366 struct dsa_switch *ds;
370 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
375 err = dsa_ds_apply(dst, ds);
380 if (dst->cpu_switch) {
381 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
386 /* If we use a tagging format that doesn't have an ethertype
387 * field, make sure that all packets from this point on get
388 * sent to the tag format's receive function.
391 dst->master_netdev->dsa_ptr = (void *)dst;
397 static void dsa_dst_unapply(struct dsa_switch_tree *dst)
399 struct dsa_switch *ds;
405 dst->master_netdev->dsa_ptr = NULL;
407 /* If we used a tagging format that doesn't have an ethertype
408 * field, make sure that all packets from this point get sent
409 * without the tag and go through the regular receive path.
413 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
418 dsa_ds_unapply(dst, ds);
422 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
424 pr_info("DSA: tree %d unapplied\n", dst->tree);
425 dst->applied = false;
428 static int dsa_cpu_parse(struct device_node *port, u32 index,
429 struct dsa_switch_tree *dst,
430 struct dsa_switch *ds)
432 enum dsa_tag_protocol tag_protocol;
433 struct net_device *ethernet_dev;
434 struct device_node *ethernet;
436 ethernet = of_parse_phandle(port, "ethernet", 0);
440 ethernet_dev = of_find_net_device_by_node(ethernet);
442 return -EPROBE_DEFER;
444 if (!ds->master_netdev)
445 ds->master_netdev = ethernet_dev;
447 if (!dst->master_netdev)
448 dst->master_netdev = ethernet_dev;
450 if (!dst->cpu_switch) {
451 dst->cpu_switch = ds;
452 dst->cpu_port = index;
455 tag_protocol = ds->ops->get_tag_protocol(ds);
456 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
457 if (IS_ERR(dst->tag_ops)) {
458 dev_warn(ds->dev, "No tagger for this switch\n");
459 return PTR_ERR(dst->tag_ops);
462 dst->rcv = dst->tag_ops->rcv;
467 static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
469 struct device_node *port;
473 for (index = 0; index < DSA_MAX_PORTS; index++) {
474 port = ds->ports[index].dn;
478 if (dsa_port_is_cpu(port)) {
479 err = dsa_cpu_parse(port, index, dst, ds);
485 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
490 static int dsa_dst_parse(struct dsa_switch_tree *dst)
492 struct dsa_switch *ds;
496 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
501 err = dsa_ds_parse(dst, ds);
506 if (!dst->master_netdev) {
507 pr_warn("Tree has no master device\n");
511 pr_info("DSA: tree %d parsed\n", dst->tree);
516 static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
518 struct device_node *port;
522 for_each_available_child_of_node(ports, port) {
523 err = of_property_read_u32(port, "reg", ®);
527 if (reg >= DSA_MAX_PORTS)
530 ds->ports[reg].dn = port;
532 /* Initialize enabled_port_mask now for ops->setup()
533 * to have access to a correct value, just like what
534 * net/dsa/dsa.c::dsa_switch_setup_one does.
536 if (!dsa_port_is_cpu(port))
537 ds->enabled_port_mask |= 1 << reg;
543 static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
549 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
551 /* Does not exist, but it is optional */
557 err = of_property_read_u32_index(np, "dsa,member", 1, index);
561 if (*index >= DSA_MAX_SWITCHES)
567 static struct device_node *dsa_get_ports(struct dsa_switch *ds,
568 struct device_node *np)
570 struct device_node *ports;
572 ports = of_get_child_by_name(np, "ports");
574 dev_err(ds->dev, "no ports child node found\n");
575 return ERR_PTR(-EINVAL);
581 static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
583 struct device_node *ports = dsa_get_ports(ds, np);
584 struct dsa_switch_tree *dst;
588 err = dsa_parse_member(np, &tree, &index);
593 return PTR_ERR(ports);
595 err = dsa_parse_ports_dn(ports, ds);
599 dst = dsa_get_dst(tree);
601 dst = dsa_add_dst(tree);
606 if (dst->ds[index]) {
614 /* Initialize the routing table */
615 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
616 ds->rtable[i] = DSA_RTABLE_NONE;
618 dsa_dst_add_ds(dst, ds, index);
620 err = dsa_dst_complete(dst);
625 /* Not all switches registered yet */
631 pr_info("DSA: Disjoint trees?\n");
635 err = dsa_dst_parse(dst);
637 if (err == -EPROBE_DEFER) {
638 dsa_dst_del_ds(dst, ds, ds->index);
645 err = dsa_dst_apply(dst);
647 dsa_dst_unapply(dst);
655 dsa_dst_del_ds(dst, ds, ds->index);
662 int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
666 mutex_lock(&dsa2_mutex);
667 err = _dsa_register_switch(ds, np);
668 mutex_unlock(&dsa2_mutex);
672 EXPORT_SYMBOL_GPL(dsa_register_switch);
674 static void _dsa_unregister_switch(struct dsa_switch *ds)
676 struct dsa_switch_tree *dst = ds->dst;
678 dsa_dst_unapply(dst);
680 dsa_dst_del_ds(dst, ds, ds->index);
683 void dsa_unregister_switch(struct dsa_switch *ds)
685 mutex_lock(&dsa2_mutex);
686 _dsa_unregister_switch(ds);
687 mutex_unlock(&dsa2_mutex);
689 EXPORT_SYMBOL_GPL(dsa_unregister_switch);