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_switch_trees);
25 static DEFINE_MUTEX(dsa2_mutex);
27 static const struct devlink_ops dsa_devlink_ops = {
30 static struct dsa_switch_tree *dsa_get_dst(u32 tree)
32 struct dsa_switch_tree *dst;
34 list_for_each_entry(dst, &dsa_switch_trees, list)
35 if (dst->tree == tree) {
36 kref_get(&dst->refcount);
42 static void dsa_free_dst(struct kref *ref)
44 struct dsa_switch_tree *dst = container_of(ref, struct dsa_switch_tree,
51 static void dsa_put_dst(struct dsa_switch_tree *dst)
53 kref_put(&dst->refcount, dsa_free_dst);
56 static struct dsa_switch_tree *dsa_add_dst(u32 tree)
58 struct dsa_switch_tree *dst;
60 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
64 INIT_LIST_HEAD(&dst->list);
65 list_add_tail(&dsa_switch_trees, &dst->list);
66 kref_init(&dst->refcount);
71 static void dsa_dst_add_ds(struct dsa_switch_tree *dst,
72 struct dsa_switch *ds, u32 index)
74 kref_get(&dst->refcount);
78 static void dsa_dst_del_ds(struct dsa_switch_tree *dst,
79 struct dsa_switch *ds, u32 index)
81 dst->ds[index] = NULL;
82 kref_put(&dst->refcount, dsa_free_dst);
85 /* For platform data configurations, we need to have a valid name argument to
86 * differentiate a disabled port from an enabled one
88 static bool dsa_port_is_valid(struct dsa_port *port)
90 return port->type != DSA_PORT_TYPE_UNUSED;
93 static bool dsa_port_is_dsa(struct dsa_port *port)
95 return port->type == DSA_PORT_TYPE_DSA;
98 static bool dsa_port_is_cpu(struct dsa_port *port)
100 return port->type == DSA_PORT_TYPE_CPU;
103 static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
104 struct device_node *port)
108 for (index = 0; index < ds->num_ports; index++)
109 if (ds->ports[index].dn == port)
114 static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst,
115 struct device_node *port)
117 struct dsa_switch *ds;
120 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
125 if (dsa_ds_find_port_dn(ds, port))
132 static int dsa_port_complete(struct dsa_switch_tree *dst,
133 struct dsa_switch *src_ds,
134 struct dsa_port *port,
137 struct device_node *link;
139 struct dsa_switch *dst_ds;
141 for (index = 0;; index++) {
142 link = of_parse_phandle(port->dn, "link", index);
146 dst_ds = dsa_dst_find_port_dn(dst, link);
152 src_ds->rtable[dst_ds->index] = src_port;
158 /* A switch is complete if all the DSA ports phandles point to ports
159 * known in the tree. A return value of 1 means the tree is not
160 * complete. This is not an error condition. A value of 0 is
163 static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
165 struct dsa_port *port;
169 for (index = 0; index < ds->num_ports; index++) {
170 port = &ds->ports[index];
171 if (!dsa_port_is_valid(port))
174 if (!dsa_port_is_dsa(port))
177 err = dsa_port_complete(dst, ds, port, index);
185 /* A tree is complete if all the DSA ports phandles point to ports
186 * known in the tree. A return value of 1 means the tree is not
187 * complete. This is not an error condition. A value of 0 is
190 static int dsa_dst_complete(struct dsa_switch_tree *dst)
192 struct dsa_switch *ds;
196 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
201 err = dsa_ds_complete(dst, ds);
209 static int dsa_dsa_port_apply(struct dsa_port *port)
211 struct dsa_switch *ds = port->ds;
214 err = dsa_port_fixed_link_register_of(port);
216 dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n",
221 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
223 return devlink_port_register(ds->devlink, &port->devlink_port,
227 static void dsa_dsa_port_unapply(struct dsa_port *port)
229 devlink_port_unregister(&port->devlink_port);
230 dsa_port_fixed_link_unregister_of(port);
233 static int dsa_cpu_port_apply(struct dsa_port *port)
235 struct dsa_switch *ds = port->ds;
238 err = dsa_port_fixed_link_register_of(port);
240 dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n",
245 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
246 err = devlink_port_register(ds->devlink, &port->devlink_port,
251 static void dsa_cpu_port_unapply(struct dsa_port *port)
253 devlink_port_unregister(&port->devlink_port);
254 dsa_port_fixed_link_unregister_of(port);
257 static int dsa_user_port_apply(struct dsa_port *port)
259 struct dsa_switch *ds = port->ds;
260 const char *name = port->name;
263 err = dsa_slave_create(port, name);
265 dev_warn(ds->dev, "Failed to create slave %d: %d\n",
271 memset(&port->devlink_port, 0, sizeof(port->devlink_port));
272 err = devlink_port_register(ds->devlink, &port->devlink_port,
277 devlink_port_type_eth_set(&port->devlink_port, port->slave);
282 static void dsa_user_port_unapply(struct dsa_port *port)
284 devlink_port_unregister(&port->devlink_port);
286 dsa_slave_destroy(port->slave);
291 static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
293 struct dsa_port *port;
297 /* Initialize ds->phys_mii_mask before registering the slave MDIO bus
298 * driver and before ops->setup() has run, since the switch drivers and
299 * the slave MDIO bus driver rely on these values for probing PHY
302 ds->phys_mii_mask |= dsa_user_ports(ds);
304 /* Add the switch to devlink before calling setup, so that setup can
307 ds->devlink = devlink_alloc(&dsa_devlink_ops, 0);
311 err = devlink_register(ds->devlink, ds->dev);
315 err = ds->ops->setup(ds);
319 err = dsa_switch_register_notifier(ds);
323 if (!ds->slave_mii_bus && ds->ops->phy_read) {
324 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
325 if (!ds->slave_mii_bus)
328 dsa_slave_mii_bus_init(ds);
330 err = mdiobus_register(ds->slave_mii_bus);
335 for (index = 0; index < ds->num_ports; index++) {
336 port = &ds->ports[index];
337 if (!dsa_port_is_valid(port))
340 if (dsa_port_is_dsa(port)) {
341 err = dsa_dsa_port_apply(port);
347 if (dsa_port_is_cpu(port)) {
348 err = dsa_cpu_port_apply(port);
354 err = dsa_user_port_apply(port);
362 static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
364 struct dsa_port *port;
367 for (index = 0; index < ds->num_ports; index++) {
368 port = &ds->ports[index];
369 if (!dsa_port_is_valid(port))
372 if (dsa_port_is_dsa(port)) {
373 dsa_dsa_port_unapply(port);
377 if (dsa_port_is_cpu(port)) {
378 dsa_cpu_port_unapply(port);
382 dsa_user_port_unapply(port);
385 if (ds->slave_mii_bus && ds->ops->phy_read)
386 mdiobus_unregister(ds->slave_mii_bus);
388 dsa_switch_unregister_notifier(ds);
391 devlink_unregister(ds->devlink);
392 devlink_free(ds->devlink);
398 static int dsa_dst_apply(struct dsa_switch_tree *dst)
400 struct dsa_switch *ds;
404 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
409 err = dsa_ds_apply(dst, ds);
414 /* If we use a tagging format that doesn't have an ethertype
415 * field, make sure that all packets from this point on get
416 * sent to the tag format's receive function.
419 dst->cpu_dp->master->dsa_ptr = dst->cpu_dp;
421 err = dsa_master_ethtool_setup(dst->cpu_dp->master);
430 static void dsa_dst_unapply(struct dsa_switch_tree *dst)
432 struct dsa_switch *ds;
438 dsa_master_ethtool_restore(dst->cpu_dp->master);
440 dst->cpu_dp->master->dsa_ptr = NULL;
442 /* If we used a tagging format that doesn't have an ethertype
443 * field, make sure that all packets from this point get sent
444 * without the tag and go through the regular receive path.
448 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
453 dsa_ds_unapply(dst, ds);
458 pr_info("DSA: tree %d unapplied\n", dst->tree);
459 dst->applied = false;
462 static int dsa_cpu_parse(struct dsa_port *port, u32 index,
463 struct dsa_switch_tree *dst,
464 struct dsa_switch *ds)
466 const struct dsa_device_ops *tag_ops;
467 enum dsa_tag_protocol tag_protocol;
472 tag_protocol = ds->ops->get_tag_protocol(ds);
473 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
474 if (IS_ERR(tag_ops)) {
475 dev_warn(ds->dev, "No tagger for this switch\n");
476 return PTR_ERR(tag_ops);
479 dst->cpu_dp->tag_ops = tag_ops;
481 /* Make a few copies for faster access in master receive hot path */
482 dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
483 dst->cpu_dp->dst = dst;
488 static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
490 struct dsa_port *port;
494 for (index = 0; index < ds->num_ports; index++) {
495 port = &ds->ports[index];
496 if (!dsa_port_is_valid(port) ||
497 dsa_port_is_dsa(port))
500 if (dsa_port_is_cpu(port)) {
501 err = dsa_cpu_parse(port, index, dst, ds);
508 pr_info("DSA: switch %d %d parsed\n", dst->tree, ds->index);
513 static int dsa_dst_parse(struct dsa_switch_tree *dst)
515 struct dsa_switch *ds;
521 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
526 err = dsa_ds_parse(dst, ds);
532 pr_warn("Tree has no master device\n");
536 /* Assign the default CPU port to all ports of the fabric */
537 for (index = 0; index < DSA_MAX_SWITCHES; index++) {
542 for (port = 0; port < ds->num_ports; port++) {
543 dp = &ds->ports[port];
544 if (!dsa_port_is_valid(dp) ||
545 dsa_port_is_dsa(dp) ||
549 dp->cpu_dp = dst->cpu_dp;
553 pr_info("DSA: tree %d parsed\n", dst->tree);
558 static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn)
560 struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0);
561 struct device_node *link = of_parse_phandle(dn, "link", 0);
562 const char *name = of_get_property(dn, "label", NULL);
565 struct net_device *master;
567 master = of_find_net_device_by_node(ethernet);
569 return -EPROBE_DEFER;
571 dp->type = DSA_PORT_TYPE_CPU;
574 dp->type = DSA_PORT_TYPE_DSA;
579 dp->type = DSA_PORT_TYPE_USER;
588 static int dsa_parse_ports_of(struct device_node *dn, struct dsa_switch *ds)
590 struct device_node *ports, *port;
595 ports = of_get_child_by_name(dn, "ports");
597 dev_err(ds->dev, "no ports child node found\n");
601 for_each_available_child_of_node(ports, port) {
602 err = of_property_read_u32(port, "reg", ®);
606 if (reg >= ds->num_ports)
609 dp = &ds->ports[reg];
611 err = dsa_port_parse_of(dp, port);
619 static int dsa_port_parse(struct dsa_port *dp, const char *name,
622 if (!strcmp(name, "cpu")) {
623 struct net_device *master;
625 master = dsa_dev_to_net_device(dev);
627 return -EPROBE_DEFER;
631 dp->type = DSA_PORT_TYPE_CPU;
633 } else if (!strcmp(name, "dsa")) {
634 dp->type = DSA_PORT_TYPE_DSA;
636 dp->type = DSA_PORT_TYPE_USER;
644 static int dsa_parse_ports(struct dsa_chip_data *cd, struct dsa_switch *ds)
646 bool valid_name_found = false;
653 for (i = 0; i < DSA_MAX_PORTS; i++) {
654 name = cd->port_names[i];
661 err = dsa_port_parse(dp, name, dev);
665 valid_name_found = true;
668 if (!valid_name_found && i == DSA_MAX_PORTS)
674 static int dsa_parse_member_dn(struct device_node *np, u32 *tree, u32 *index)
680 err = of_property_read_u32_index(np, "dsa,member", 0, tree);
682 /* Does not exist, but it is optional */
688 err = of_property_read_u32_index(np, "dsa,member", 1, index);
692 if (*index >= DSA_MAX_SWITCHES)
698 static int dsa_parse_member(struct dsa_chip_data *pd, u32 *tree, u32 *index)
703 /* We do not support complex trees with dsa_chip_data */
710 static int _dsa_register_switch(struct dsa_switch *ds)
712 struct dsa_chip_data *pdata = ds->dev->platform_data;
713 struct device_node *np = ds->dev->of_node;
714 struct dsa_switch_tree *dst;
719 err = dsa_parse_member_dn(np, &tree, &index);
723 err = dsa_parse_ports_of(np, ds);
727 err = dsa_parse_member(pdata, &tree, &index);
731 err = dsa_parse_ports(pdata, ds);
736 dst = dsa_get_dst(tree);
738 dst = dsa_add_dst(tree);
743 if (dst->ds[index]) {
752 /* Initialize the routing table */
753 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
754 ds->rtable[i] = DSA_RTABLE_NONE;
756 dsa_dst_add_ds(dst, ds, index);
758 err = dsa_dst_complete(dst);
763 /* Not all switches registered yet */
769 pr_info("DSA: Disjoint trees?\n");
773 err = dsa_dst_parse(dst);
777 err = dsa_dst_apply(dst);
779 dsa_dst_unapply(dst);
787 dsa_dst_del_ds(dst, ds, ds->index);
794 struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
796 size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
797 struct dsa_switch *ds;
800 ds = devm_kzalloc(dev, size, GFP_KERNEL);
807 for (i = 0; i < ds->num_ports; ++i) {
808 ds->ports[i].index = i;
809 ds->ports[i].ds = ds;
814 EXPORT_SYMBOL_GPL(dsa_switch_alloc);
816 int dsa_register_switch(struct dsa_switch *ds)
820 mutex_lock(&dsa2_mutex);
821 err = _dsa_register_switch(ds);
822 mutex_unlock(&dsa2_mutex);
826 EXPORT_SYMBOL_GPL(dsa_register_switch);
828 static void _dsa_unregister_switch(struct dsa_switch *ds)
830 struct dsa_switch_tree *dst = ds->dst;
832 dsa_dst_unapply(dst);
834 dsa_dst_del_ds(dst, ds, ds->index);
837 void dsa_unregister_switch(struct dsa_switch *ds)
839 mutex_lock(&dsa2_mutex);
840 _dsa_unregister_switch(ds);
841 mutex_unlock(&dsa2_mutex);
843 EXPORT_SYMBOL_GPL(dsa_unregister_switch);