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);
61 INIT_LIST_HEAD(&dst->list);
62 list_add_tail(&dsa_switch_trees, &dst->list);
63 kref_init(&dst->refcount);
68 static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
69 struct dsa_switch *ds, u32 index)
71 kref_get(&dst->refcount);
75 static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
76 struct dsa_switch *ds, u32 index)
78 dst->ds[index] = NULL;
79 kref_put(&dst->refcount, dsa_free_dst);
82 static bool dsa_port_is_dsa(struct device_node *port)
86 name = of_get_property(port, "label", NULL);
90 if (!strcmp(name, "dsa"))
96 static bool dsa_port_is_cpu(struct device_node *port)
100 name = of_get_property(port, "label", NULL);
104 if (!strcmp(name, "cpu"))
110 static bool dsa_ds_find_port(struct dsa_switch *ds,
111 struct device_node *port)
115 for (index = 0; index < DSA_MAX_PORTS; index++)
116 if (ds->ports[index].dn == port)
121 static struct dsa_switch *dsa_dst_find_port(struct dsa_switch_tree *dst,
122 struct device_node *port)
124 struct dsa_switch *ds;
127 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
132 if (dsa_ds_find_port(ds, port))
139 static int dsa_port_complete(struct dsa_switch_tree *dst,
140 struct dsa_switch *src_ds,
141 struct device_node *port,
144 struct device_node *link;
146 struct dsa_switch *dst_ds;
148 for (index = 0;; index++) {
149 link = of_parse_phandle(port, "link", index);
153 dst_ds = dsa_dst_find_port(dst, link);
159 src_ds->rtable[dst_ds->index] = src_port;
165 /* A switch is complete if all the DSA ports phandles point to ports
166 * known in the tree. A return value of 1 means the tree is not
167 * complete. This is not an error condition. A value of 0 is
170 static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
172 struct device_node *port;
176 for (index = 0; index < DSA_MAX_PORTS; index++) {
177 port = ds->ports[index].dn;
181 if (!dsa_port_is_dsa(port))
184 err = dsa_port_complete(dst, ds, port, index);
188 ds->dsa_port_mask |= BIT(index);
194 /* A tree is complete if all the DSA ports phandles point to ports
195 * known in the tree. A return value of 1 means the tree is not
196 * complete. This is not an error condition. A value of 0 is
199 static int dsa_dst_complete(struct dsa_switch_tree *dst)
201 struct dsa_switch *ds;
205 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
210 err = dsa_ds_complete(dst, ds);
218 static int dsa_dsa_port_apply(struct device_node *port, u32 index,
219 struct dsa_switch *ds)
223 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
225 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
233 static void dsa_dsa_port_unapply(struct device_node *port, u32 index,
234 struct dsa_switch *ds)
236 dsa_cpu_dsa_destroy(port);
239 static int dsa_cpu_port_apply(struct device_node *port, u32 index,
240 struct dsa_switch *ds)
244 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
246 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
251 ds->cpu_port_mask |= BIT(index);
256 static void dsa_cpu_port_unapply(struct device_node *port, u32 index,
257 struct dsa_switch *ds)
259 dsa_cpu_dsa_destroy(port);
260 ds->cpu_port_mask &= ~BIT(index);
264 static int dsa_user_port_apply(struct device_node *port, u32 index,
265 struct dsa_switch *ds)
270 name = of_get_property(port, "label", NULL);
272 err = dsa_slave_create(ds, ds->dev, index, name);
274 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
276 ds->ports[index].netdev = NULL;
283 static void dsa_user_port_unapply(struct device_node *port, u32 index,
284 struct dsa_switch *ds)
286 if (ds->ports[index].netdev) {
287 dsa_slave_destroy(ds->ports[index].netdev);
288 ds->ports[index].netdev = NULL;
289 ds->enabled_port_mask &= ~(1 << index);
293 static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
295 struct device_node *port;
299 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
300 * driver and before ops->setup() has run, since the switch drivers and
301 * the slave MDIO bus driver rely on these values for probing PHY
304 ds->phys_mii_mask = ds->enabled_port_mask;
306 err = ds->ops->setup(ds);
310 if (ds->ops->set_addr) {
311 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
316 if (!ds->slave_mii_bus && ds->ops->phy_read) {
317 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
318 if (!ds->slave_mii_bus)
321 dsa_slave_mii_bus_init(ds);
323 err = mdiobus_register(ds->slave_mii_bus);
328 for (index = 0; index < DSA_MAX_PORTS; index++) {
329 port = ds->ports[index].dn;
333 if (dsa_port_is_dsa(port)) {
334 err = dsa_dsa_port_apply(port, index, ds);
340 if (dsa_port_is_cpu(port)) {
341 err = dsa_cpu_port_apply(port, index, ds);
347 err = dsa_user_port_apply(port, index, ds);
355 static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
357 struct device_node *port;
360 for (index = 0; index < DSA_MAX_PORTS; index++) {
361 port = ds->ports[index].dn;
365 if (dsa_port_is_dsa(port)) {
366 dsa_dsa_port_unapply(port, index, ds);
370 if (dsa_port_is_cpu(port)) {
371 dsa_cpu_port_unapply(port, index, ds);
375 dsa_user_port_unapply(port, index, ds);
378 if (ds->slave_mii_bus && ds->ops->phy_read)
379 mdiobus_unregister(ds->slave_mii_bus);
382 static int dsa_dst_apply(struct dsa_switch_tree *dst)
384 struct dsa_switch *ds;
388 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
393 err = dsa_ds_apply(dst, ds);
399 err = dsa_cpu_port_ethtool_setup(dst->ds[0]);
404 /* If we use a tagging format that doesn't have an ethertype
405 * field, make sure that all packets from this point on get
406 * sent to the tag format's receive function.
409 dst->master_netdev->dsa_ptr = (void *)dst;
415 static void dsa_dst_unapply(struct dsa_switch_tree *dst)
417 struct dsa_switch *ds;
423 dst->master_netdev->dsa_ptr = NULL;
425 /* If we used a tagging format that doesn't have an ethertype
426 * field, make sure that all packets from this point get sent
427 * without the tag and go through the regular receive path.
431 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
436 dsa_ds_unapply(dst, ds);
440 dsa_cpu_port_ethtool_restore(dst->ds[0]);
442 pr_info("DSA: tree %d unapplied\n", dst->tree);
443 dst->applied = false;
446 static int dsa_cpu_parse(struct device_node *port, u32 index,
447 struct dsa_switch_tree *dst,
448 struct dsa_switch *ds)
450 enum dsa_tag_protocol tag_protocol;
451 struct net_device *ethernet_dev;
452 struct device_node *ethernet;
454 ethernet = of_parse_phandle(port, "ethernet", 0);
458 ethernet_dev = of_find_net_device_by_node(ethernet);
460 return -EPROBE_DEFER;
462 if (!ds->master_netdev)
463 ds->master_netdev = ethernet_dev;
465 if (!dst->master_netdev)
466 dst->master_netdev = ethernet_dev;
468 if (dst->cpu_switch == -1) {
469 dst->cpu_switch = ds->index;
470 dst->cpu_port = index;
473 tag_protocol = ds->ops->get_tag_protocol(ds);
474 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
475 if (IS_ERR(dst->tag_ops)) {
476 dev_warn(ds->dev, "No tagger for this switch\n");
477 return PTR_ERR(dst->tag_ops);
480 dst->rcv = dst->tag_ops->rcv;
485 static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
487 struct device_node *port;
491 for (index = 0; index < DSA_MAX_PORTS; index++) {
492 port = ds->ports[index].dn;
496 if (dsa_port_is_cpu(port)) {
497 err = dsa_cpu_parse(port, index, dst, ds);
503 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
508 static int dsa_dst_parse(struct dsa_switch_tree *dst)
510 struct dsa_switch *ds;
514 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
519 err = dsa_ds_parse(dst, ds);
524 if (!dst->master_netdev) {
525 pr_warn("Tree has no master device\n");
529 pr_info("DSA: tree %d parsed\n", dst->tree);
534 static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
536 struct device_node *port;
540 for_each_available_child_of_node(ports, port) {
541 err = of_property_read_u32(port, "reg", ®);
545 if (reg >= DSA_MAX_PORTS)
548 ds->ports[reg].dn = port;
550 /* Initialize enabled_port_mask now for ops->setup()
551 * to have access to a correct value, just like what
552 * net/dsa/dsa.c::dsa_switch_setup_one does.
554 if (!dsa_port_is_cpu(port))
555 ds->enabled_port_mask |= 1 << reg;
561 static int dsa_parse_member(struct device_node *np, u32 *tree, u32 *index)
567 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
569 /* Does not exist, but it is optional */
575 err = of_property_read_u32_index(np, "dsa,member", 1, index);
579 if (*index >= DSA_MAX_SWITCHES)
585 static struct device_node *dsa_get_ports(struct dsa_switch *ds,
586 struct device_node *np)
588 struct device_node *ports;
590 ports = of_get_child_by_name(np, "ports");
592 dev_err(ds->dev, "no ports child node found\n");
593 return ERR_PTR(-EINVAL);
599 static int _dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
601 struct device_node *ports = dsa_get_ports(ds, np);
602 struct dsa_switch_tree *dst;
606 err = dsa_parse_member(np, &tree, &index);
611 return PTR_ERR(ports);
613 err = dsa_parse_ports_dn(ports, ds);
617 dst = dsa_get_dst(tree);
619 dst = dsa_add_dst(tree);
624 if (dst->ds[index]) {
632 /* Initialize the routing table */
633 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
634 ds->rtable[i] = DSA_RTABLE_NONE;
636 dsa_dst_add_ds(dst, ds, index);
638 err = dsa_dst_complete(dst);
643 /* Not all switches registered yet */
649 pr_info("DSA: Disjoint trees?\n");
653 err = dsa_dst_parse(dst);
657 err = dsa_dst_apply(dst);
659 dsa_dst_unapply(dst);
667 dsa_dst_del_ds(dst, ds, ds->index);
674 int dsa_register_switch(struct dsa_switch *ds, struct device_node *np)
678 mutex_lock(&dsa2_mutex);
679 err = _dsa_register_switch(ds, np);
680 mutex_unlock(&dsa2_mutex);
684 EXPORT_SYMBOL_GPL(dsa_register_switch);
686 static void _dsa_unregister_switch(struct dsa_switch *ds)
688 struct dsa_switch_tree *dst = ds->dst;
690 dsa_dst_unapply(dst);
692 dsa_dst_del_ds(dst, ds, ds->index);
695 void dsa_unregister_switch(struct dsa_switch *ds)
697 mutex_lock(&dsa2_mutex);
698 _dsa_unregister_switch(ds);
699 mutex_unlock(&dsa2_mutex);
701 EXPORT_SYMBOL_GPL(dsa_unregister_switch);