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/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 /* For platform data configurations, we need to have a valid name argument to
82 * differentiate a disabled port from an enabled one
84 static bool dsa_port_is_valid(struct dsa_port *port)
86 return !!(port->dn || port->name);
89 static bool dsa_port_is_dsa(struct dsa_port *port)
91 if (port->name && !strcmp(port->name, "dsa"))
94 return !!of_parse_phandle(port->dn, "link", 0);
97 static bool dsa_port_is_cpu(struct dsa_port *port)
99 if (port->name && !strcmp(port->name, "cpu"))
102 return !!of_parse_phandle(port->dn, "ethernet", 0);
105 static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
106 struct device_node *port)
110 for (index = 0; index < ds->num_ports; index++)
111 if (ds->ports[index].dn == port)
116 static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
117 struct device_node *port)
119 struct dsa_switch *ds;
122 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
127 if (dsa_ds_find_port_dn(ds, port))
134 static int dsa_port_complete(struct dsa_switch_tree *dst,
135 struct dsa_switch *src_ds,
136 struct dsa_port *port,
139 struct device_node *link;
141 struct dsa_switch *dst_ds;
143 for (index = 0;; index++) {
144 link = of_parse_phandle(port->dn, "link", index);
148 dst_ds = dsa_dst_find_port_dn(dst, link);
154 src_ds->rtable[dst_ds->index] = src_port;
160 /* A switch is complete if all the DSA ports phandles point to ports
161 * known in the tree. A return value of 1 means the tree is not
162 * complete. This is not an error condition. A value of 0 is
165 static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
167 struct dsa_port *port;
171 for (index = 0; index < ds->num_ports; index++) {
172 port = &ds->ports[index];
173 if (!dsa_port_is_valid(port))
176 if (!dsa_port_is_dsa(port))
179 err = dsa_port_complete(dst, ds, port, index);
183 ds->dsa_port_mask |= BIT(index);
189 /* A tree is complete if all the DSA ports phandles point to ports
190 * known in the tree. A return value of 1 means the tree is not
191 * complete. This is not an error condition. A value of 0 is
194 static int dsa_dst_complete(struct dsa_switch_tree *dst)
196 struct dsa_switch *ds;
200 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
205 err = dsa_ds_complete(dst, ds);
213 static int dsa_dsa_port_apply(struct dsa_port *port, u32 index,
214 struct dsa_switch *ds)
218 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
220 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
228 static void dsa_dsa_port_unapply(struct dsa_port *port, u32 index,
229 struct dsa_switch *ds)
231 dsa_cpu_dsa_destroy(port);
234 static int dsa_cpu_port_apply(struct dsa_port *port, u32 index,
235 struct dsa_switch *ds)
239 err = dsa_cpu_dsa_setup(ds, ds->dev, port, index);
241 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
246 ds->cpu_port_mask |= BIT(index);
251 static void dsa_cpu_port_unapply(struct dsa_port *port, u32 index,
252 struct dsa_switch *ds)
254 dsa_cpu_dsa_destroy(port);
255 ds->cpu_port_mask &= ~BIT(index);
259 static int dsa_user_port_apply(struct dsa_port *port, u32 index,
260 struct dsa_switch *ds)
262 const char *name = port->name;
266 name = of_get_property(port->dn, "label", NULL);
270 err = dsa_slave_create(ds, ds->dev, index, name);
272 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
280 static void dsa_user_port_unapply(struct dsa_port *port, u32 index,
281 struct dsa_switch *ds)
283 if (ds->ports[index].netdev) {
284 dsa_slave_destroy(ds->ports[index].netdev);
285 ds->ports[index].netdev = NULL;
286 ds->enabled_port_mask &= ~(1 << index);
290 static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
292 struct dsa_port *port;
296 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
297 * driver and before ops->setup() has run, since the switch drivers and
298 * the slave MDIO bus driver rely on these values for probing PHY
301 ds->phys_mii_mask = ds->enabled_port_mask;
303 err = ds->ops->setup(ds);
307 err = dsa_switch_register_notifier(ds);
311 if (ds->ops->set_addr) {
312 err = ds->ops->set_addr(ds, dst->master_netdev->dev_addr);
317 if (!ds->slave_mii_bus && ds->ops->phy_read) {
318 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
319 if (!ds->slave_mii_bus)
322 dsa_slave_mii_bus_init(ds);
324 err = mdiobus_register(ds->slave_mii_bus);
329 for (index = 0; index < ds->num_ports; index++) {
330 port = &ds->ports[index];
331 if (!dsa_port_is_valid(port))
334 if (dsa_port_is_dsa(port)) {
335 err = dsa_dsa_port_apply(port, index, ds);
341 if (dsa_port_is_cpu(port)) {
342 err = dsa_cpu_port_apply(port, index, ds);
348 err = dsa_user_port_apply(port, index, ds);
356 static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
358 struct dsa_port *port;
361 for (index = 0; index < ds->num_ports; index++) {
362 port = &ds->ports[index];
363 if (!dsa_port_is_valid(port))
366 if (dsa_port_is_dsa(port)) {
367 dsa_dsa_port_unapply(port, index, ds);
371 if (dsa_port_is_cpu(port)) {
372 dsa_cpu_port_unapply(port, index, ds);
376 dsa_user_port_unapply(port, index, ds);
379 if (ds->slave_mii_bus && ds->ops->phy_read)
380 mdiobus_unregister(ds->slave_mii_bus);
382 dsa_switch_unregister_notifier(ds);
385 static int dsa_dst_apply(struct dsa_switch_tree *dst)
387 struct dsa_switch *ds;
391 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
396 err = dsa_ds_apply(dst, ds);
401 if (dst->cpu_switch) {
402 err = dsa_cpu_port_ethtool_setup(dst->cpu_switch);
407 /* If we use a tagging format that doesn't have an ethertype
408 * field, make sure that all packets from this point on get
409 * sent to the tag format's receive function.
412 dst->master_netdev->dsa_ptr = (void *)dst;
418 static void dsa_dst_unapply(struct dsa_switch_tree *dst)
420 struct dsa_switch *ds;
426 dst->master_netdev->dsa_ptr = NULL;
428 /* If we used a tagging format that doesn't have an ethertype
429 * field, make sure that all packets from this point get sent
430 * without the tag and go through the regular receive path.
434 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
439 dsa_ds_unapply(dst, ds);
443 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
445 pr_info("DSA: tree %d unapplied\n", dst->tree);
446 dst->applied = false;
449 static int dsa_cpu_parse(struct dsa_port *port, u32 index,
450 struct dsa_switch_tree *dst,
451 struct dsa_switch *ds)
453 enum dsa_tag_protocol tag_protocol;
454 struct net_device *ethernet_dev;
455 struct device_node *ethernet;
458 ethernet = of_parse_phandle(port->dn, "ethernet", 0);
461 ethernet_dev = of_find_net_device_by_node(ethernet);
463 ethernet_dev = dsa_dev_to_net_device(ds->cd->netdev[index]);
464 dev_put(ethernet_dev);
468 return -EPROBE_DEFER;
470 if (!ds->master_netdev)
471 ds->master_netdev = ethernet_dev;
473 if (!dst->master_netdev)
474 dst->master_netdev = ethernet_dev;
476 if (!dst->cpu_switch) {
477 dst->cpu_switch = ds;
478 dst->cpu_port = index;
481 tag_protocol = ds->ops->get_tag_protocol(ds);
482 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
483 if (IS_ERR(dst->tag_ops)) {
484 dev_warn(ds->dev, "No tagger for this switch\n");
485 return PTR_ERR(dst->tag_ops);
488 dst->rcv = dst->tag_ops->rcv;
493 static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
495 struct dsa_port *port;
499 for (index = 0; index < ds->num_ports; index++) {
500 port = &ds->ports[index];
501 if (!dsa_port_is_valid(port))
504 if (dsa_port_is_cpu(port)) {
505 err = dsa_cpu_parse(port, index, dst, ds);
511 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
516 static int dsa_dst_parse(struct dsa_switch_tree *dst)
518 struct dsa_switch *ds;
522 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
527 err = dsa_ds_parse(dst, ds);
532 if (!dst->master_netdev) {
533 pr_warn("Tree has no master device\n");
537 pr_info("DSA: tree %d parsed\n", dst->tree);
542 static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
544 struct device_node *port;
548 for_each_available_child_of_node(ports, port) {
549 err = of_property_read_u32(port, "reg", ®);
553 if (reg >= ds->num_ports)
556 ds->ports[reg].dn = port;
558 /* Initialize enabled_port_mask now for ops->setup()
559 * to have access to a correct value, just like what
560 * net/dsa/dsa.c::dsa_switch_setup_one does.
562 if (!dsa_port_is_cpu(&ds->ports[reg]))
563 ds->enabled_port_mask |= 1 << reg;
569 static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
571 bool valid_name_found = false;
574 for (i = 0; i < DSA_MAX_PORTS; i++) {
575 if (!cd->port_names[i])
578 ds->ports[i].name = cd->port_names[i];
580 /* Initialize enabled_port_mask now for drv->setup()
581 * to have access to a correct value, just like what
582 * net/dsa/dsa.c::dsa_switch_setup_one does.
584 if (!dsa_port_is_cpu(&ds->ports[i]))
585 ds->enabled_port_mask |= 1 << i;
587 valid_name_found = true;
590 if (!valid_name_found && i == DSA_MAX_PORTS)
596 static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
602 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
604 /* Does not exist, but it is optional */
610 err = of_property_read_u32_index(np, "dsa,member", 1, index);
614 if (*index >= DSA_MAX_SWITCHES)
620 static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
625 /* We do not support complex trees with dsa_chip_data */
632 static struct device_node *dsa_get_ports(struct dsa_switch *ds,
633 struct device_node *np)
635 struct device_node *ports;
637 ports = of_get_child_by_name(np, "ports");
639 dev_err(ds->dev, "no ports child node found\n");
640 return ERR_PTR(-EINVAL);
646 static int _dsa_register_switch(struct dsa_switch *ds, struct device *dev)
648 struct dsa_chip_data *pdata = dev->platform_data;
649 struct device_node *np = dev->of_node;
650 struct dsa_switch_tree *dst;
651 struct device_node *ports;
656 err = dsa_parse_member_dn(np, &tree, &index);
660 ports = dsa_get_ports(ds, np);
662 return PTR_ERR(ports);
664 err = dsa_parse_ports_dn(ports, ds);
668 err = dsa_parse_member(pdata, &tree, &index);
672 err = dsa_parse_ports(pdata, ds);
677 dst = dsa_get_dst(tree);
679 dst = dsa_add_dst(tree);
684 if (dst->ds[index]) {
693 /* Initialize the routing table */
694 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
695 ds->rtable[i] = DSA_RTABLE_NONE;
697 dsa_dst_add_ds(dst, ds, index);
699 err = dsa_dst_complete(dst);
704 /* Not all switches registered yet */
710 pr_info("DSA: Disjoint trees?\n");
714 err = dsa_dst_parse(dst);
716 if (err == -EPROBE_DEFER) {
717 dsa_dst_del_ds(dst, ds, ds->index);
724 err = dsa_dst_apply(dst);
726 dsa_dst_unapply(dst);
734 dsa_dst_del_ds(dst, ds, ds->index);
741 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
743 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
744 struct dsa_switch *ds;
747 ds = devm_kzalloc(dev, size, GFP_KERNEL);
754 for (i = 0; i < ds->num_ports; ++i) {
755 ds->ports[i].index = i;
756 ds->ports[i].ds = ds;
761 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
763 int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
767 mutex_lock(&dsa2_mutex);
768 err = _dsa_register_switch(ds, dev);
769 mutex_unlock(&dsa2_mutex);
773 EXPORT_SYMBOL_GPL(dsa_register_switch);
775 static void _dsa_unregister_switch(struct dsa_switch *ds)
777 struct dsa_switch_tree *dst = ds->dst;
779 dsa_dst_unapply(dst);
781 dsa_dst_del_ds(dst, ds, ds->index);
784 void dsa_unregister_switch(struct dsa_switch *ds)
786 mutex_lock(&dsa2_mutex);
787 _dsa_unregister_switch(ds);
788 mutex_unlock(&dsa2_mutex);
790 EXPORT_SYMBOL_GPL(dsa_unregister_switch);