2 * net/dsa/dsa.c - Hardware switch handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
4 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/device.h>
13 #include <linux/list.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_platform.h>
21 #include <linux/of_net.h>
22 #include <linux/of_gpio.h>
23 #include <linux/sysfs.h>
24 #include <linux/phy_fixed.h>
25 #include <linux/gpio/consumer.h>
28 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
29 struct net_device *dev)
31 /* Just return the original SKB */
35 static const struct dsa_device_ops none_ops = {
36 .xmit = dsa_slave_notag_xmit,
40 const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
41 #ifdef CONFIG_NET_DSA_TAG_DSA
42 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
44 #ifdef CONFIG_NET_DSA_TAG_EDSA
45 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
47 #ifdef CONFIG_NET_DSA_TAG_TRAILER
48 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
50 #ifdef CONFIG_NET_DSA_TAG_BRCM
51 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
53 #ifdef CONFIG_NET_DSA_TAG_QCA
54 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
56 [DSA_TAG_PROTO_NONE] = &none_ops,
59 /* switch driver registration ***********************************************/
60 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
61 static LIST_HEAD(dsa_switch_drivers);
63 void register_switch_driver(struct dsa_switch_driver *drv)
65 mutex_lock(&dsa_switch_drivers_mutex);
66 list_add_tail(&drv->list, &dsa_switch_drivers);
67 mutex_unlock(&dsa_switch_drivers_mutex);
69 EXPORT_SYMBOL_GPL(register_switch_driver);
71 void unregister_switch_driver(struct dsa_switch_driver *drv)
73 mutex_lock(&dsa_switch_drivers_mutex);
74 list_del_init(&drv->list);
75 mutex_unlock(&dsa_switch_drivers_mutex);
77 EXPORT_SYMBOL_GPL(unregister_switch_driver);
79 static const struct dsa_switch_ops *
80 dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
81 const char **_name, void **priv)
83 const struct dsa_switch_ops *ret;
84 struct list_head *list;
90 mutex_lock(&dsa_switch_drivers_mutex);
91 list_for_each(list, &dsa_switch_drivers) {
92 const struct dsa_switch_ops *ops;
93 struct dsa_switch_driver *drv;
95 drv = list_entry(list, struct dsa_switch_driver, list);
98 name = ops->probe(parent, host_dev, sw_addr, priv);
104 mutex_unlock(&dsa_switch_drivers_mutex);
111 /* basic switch operations **************************************************/
112 int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
113 struct device_node *port_dn, int port)
115 struct phy_device *phydev;
118 if (of_phy_is_fixed_link(port_dn)) {
119 ret = of_phy_register_fixed_link(port_dn);
121 dev_err(dev, "failed to register fixed PHY\n");
124 phydev = of_phy_find_device(port_dn);
126 mode = of_get_phy_mode(port_dn);
128 mode = PHY_INTERFACE_MODE_NA;
129 phydev->interface = mode;
131 genphy_config_init(phydev);
132 genphy_read_status(phydev);
133 if (ds->ops->adjust_link)
134 ds->ops->adjust_link(ds, port, phydev);
136 put_device(&phydev->mdio.dev);
142 static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
144 struct device_node *port_dn;
147 for (port = 0; port < DSA_MAX_PORTS; port++) {
148 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
151 port_dn = ds->ports[port].dn;
152 ret = dsa_cpu_dsa_setup(ds, dev, port_dn, port);
159 const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
161 const struct dsa_device_ops *ops;
163 if (tag_protocol >= DSA_TAG_LAST)
164 return ERR_PTR(-EINVAL);
165 ops = dsa_device_ops[tag_protocol];
168 return ERR_PTR(-ENOPROTOOPT);
173 int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
175 struct net_device *master;
176 struct ethtool_ops *cpu_ops;
178 master = ds->dst->master_netdev;
179 if (ds->master_netdev)
180 master = ds->master_netdev;
182 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
186 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
187 sizeof(struct ethtool_ops));
188 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
189 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
190 sizeof(struct ethtool_ops));
191 dsa_cpu_port_ethtool_init(cpu_ops);
192 master->ethtool_ops = cpu_ops;
197 void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
199 struct net_device *master;
201 master = ds->dst->master_netdev;
202 if (ds->master_netdev)
203 master = ds->master_netdev;
205 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
208 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
210 const struct dsa_switch_ops *ops = ds->ops;
211 struct dsa_switch_tree *dst = ds->dst;
212 struct dsa_chip_data *cd = ds->cd;
213 bool valid_name_found = false;
214 int index = ds->index;
218 * Validate supplied switch configuration.
220 for (i = 0; i < DSA_MAX_PORTS; i++) {
223 name = cd->port_names[i];
227 if (!strcmp(name, "cpu")) {
228 if (dst->cpu_switch) {
229 netdev_err(dst->master_netdev,
230 "multiple cpu ports?!\n");
233 dst->cpu_switch = ds;
235 ds->cpu_port_mask |= 1 << i;
236 } else if (!strcmp(name, "dsa")) {
237 ds->dsa_port_mask |= 1 << i;
239 ds->enabled_port_mask |= 1 << i;
241 valid_name_found = true;
244 if (!valid_name_found && i == DSA_MAX_PORTS)
247 /* Make the built-in MII bus mask match the number of ports,
248 * switch drivers can override this later
250 ds->phys_mii_mask = ds->enabled_port_mask;
253 * If the CPU connects to this switch, set the switch tree
254 * tagging protocol to the preferred tagging format of this
257 if (dst->cpu_switch == ds) {
258 enum dsa_tag_protocol tag_protocol;
260 tag_protocol = ops->get_tag_protocol(ds);
261 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
262 if (IS_ERR(dst->tag_ops))
263 return PTR_ERR(dst->tag_ops);
265 dst->rcv = dst->tag_ops->rcv;
268 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
271 * Do basic register setup.
273 ret = ops->setup(ds);
278 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
283 if (!ds->slave_mii_bus && ops->phy_read) {
284 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
285 if (!ds->slave_mii_bus)
287 dsa_slave_mii_bus_init(ds);
289 ret = mdiobus_register(ds->slave_mii_bus);
295 * Create network devices for physical switch ports.
297 for (i = 0; i < DSA_MAX_PORTS; i++) {
298 ds->ports[i].dn = cd->port_dn[i];
300 if (!(ds->enabled_port_mask & (1 << i)))
303 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
305 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
306 index, i, cd->port_names[i], ret);
309 /* Perform configuration of the CPU and DSA ports */
310 ret = dsa_cpu_dsa_setups(ds, parent);
312 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
315 ret = dsa_cpu_port_ethtool_setup(ds);
322 static struct dsa_switch *
323 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
324 struct device *parent, struct device *host_dev)
326 struct dsa_chip_data *cd = dst->pd->chip + index;
327 const struct dsa_switch_ops *ops;
328 struct dsa_switch *ds;
334 * Probe for switch model.
336 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
338 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
340 return ERR_PTR(-EINVAL);
342 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
347 * Allocate and initialise switch state.
349 ds = devm_kzalloc(parent, sizeof(*ds), GFP_KERNEL);
351 return ERR_PTR(-ENOMEM);
360 ret = dsa_switch_setup_one(ds, parent);
367 void dsa_cpu_dsa_destroy(struct device_node *port_dn)
369 if (of_phy_is_fixed_link(port_dn))
370 of_phy_deregister_fixed_link(port_dn);
373 static void dsa_switch_destroy(struct dsa_switch *ds)
377 /* Destroy network devices for physical switch ports. */
378 for (port = 0; port < DSA_MAX_PORTS; port++) {
379 if (!(ds->enabled_port_mask & (1 << port)))
382 if (!ds->ports[port].netdev)
385 dsa_slave_destroy(ds->ports[port].netdev);
388 /* Disable configuration of the CPU and DSA ports */
389 for (port = 0; port < DSA_MAX_PORTS; port++) {
390 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
392 dsa_cpu_dsa_destroy(ds->ports[port].dn);
394 /* Clearing a bit which is not set does no harm */
395 ds->cpu_port_mask |= ~(1 << port);
396 ds->dsa_port_mask |= ~(1 << port);
399 if (ds->slave_mii_bus && ds->ops->phy_read)
400 mdiobus_unregister(ds->slave_mii_bus);
403 #ifdef CONFIG_PM_SLEEP
404 int dsa_switch_suspend(struct dsa_switch *ds)
408 /* Suspend slave network devices */
409 for (i = 0; i < DSA_MAX_PORTS; i++) {
410 if (!dsa_is_port_initialized(ds, i))
413 ret = dsa_slave_suspend(ds->ports[i].netdev);
418 if (ds->ops->suspend)
419 ret = ds->ops->suspend(ds);
423 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
425 int dsa_switch_resume(struct dsa_switch *ds)
430 ret = ds->ops->resume(ds);
435 /* Resume slave network devices */
436 for (i = 0; i < DSA_MAX_PORTS; i++) {
437 if (!dsa_is_port_initialized(ds, i))
440 ret = dsa_slave_resume(ds->ports[i].netdev);
447 EXPORT_SYMBOL_GPL(dsa_switch_resume);
450 /* platform driver init and cleanup *****************************************/
451 static int dev_is_class(struct device *dev, void *class)
453 if (dev->class != NULL && !strcmp(dev->class->name, class))
459 static struct device *dev_find_class(struct device *parent, char *class)
461 if (dev_is_class(parent, class)) {
466 return device_find_child(parent, class, dev_is_class);
469 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
473 d = dev_find_class(dev, "mdio_bus");
485 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
487 static struct net_device *dev_to_net_device(struct device *dev)
491 d = dev_find_class(dev, "net");
493 struct net_device *nd;
506 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
507 struct dsa_chip_data *cd,
508 int chip_index, int port_index,
509 struct device_node *link)
513 struct device_node *parent_sw;
516 parent_sw = of_get_parent(link);
520 reg = of_get_property(parent_sw, "reg", &len);
521 if (!reg || (len != sizeof(*reg) * 2))
525 * Get the destination switch number from the second field of its 'reg'
526 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
528 link_sw_addr = be32_to_cpup(reg + 1);
530 if (link_sw_addr >= pd->nr_chips)
533 cd->rtable[link_sw_addr] = port_index;
538 static int dsa_of_probe_links(struct dsa_platform_data *pd,
539 struct dsa_chip_data *cd,
540 int chip_index, int port_index,
541 struct device_node *port,
542 const char *port_name)
544 struct device_node *link;
548 for (link_index = 0;; link_index++) {
549 link = of_parse_phandle(port, "link", link_index);
553 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
554 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
563 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
568 for (i = 0; i < pd->nr_chips; i++) {
570 while (port_index < DSA_MAX_PORTS) {
571 kfree(pd->chip[i].port_names[port_index]);
575 /* Drop our reference to the MDIO bus device */
576 if (pd->chip[i].host_dev)
577 put_device(pd->chip[i].host_dev);
582 static int dsa_of_probe(struct device *dev)
584 struct device_node *np = dev->of_node;
585 struct device_node *child, *mdio, *ethernet, *port;
586 struct mii_bus *mdio_bus, *mdio_bus_switch;
587 struct net_device *ethernet_dev;
588 struct dsa_platform_data *pd;
589 struct dsa_chip_data *cd;
590 const char *port_name;
591 int chip_index, port_index;
592 const unsigned int *sw_addr, *port_reg;
596 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
600 mdio_bus = of_mdio_find_bus(mdio);
602 return -EPROBE_DEFER;
604 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
610 ethernet_dev = of_find_net_device_by_node(ethernet);
616 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
619 goto out_put_ethernet;
622 dev->platform_data = pd;
623 pd->of_netdev = ethernet_dev;
624 pd->nr_chips = of_get_available_child_count(np);
625 if (pd->nr_chips > DSA_MAX_SWITCHES)
626 pd->nr_chips = DSA_MAX_SWITCHES;
628 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
636 for_each_available_child_of_node(np, child) {
640 cd = &pd->chip[chip_index];
644 /* Initialize the routing table */
645 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
646 cd->rtable[i] = DSA_RTABLE_NONE;
648 /* When assigning the host device, increment its refcount */
649 cd->host_dev = get_device(&mdio_bus->dev);
651 sw_addr = of_get_property(child, "reg", NULL);
655 cd->sw_addr = be32_to_cpup(sw_addr);
656 if (cd->sw_addr >= PHY_MAX_ADDR)
659 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
660 cd->eeprom_len = eeprom_len;
662 mdio = of_parse_phandle(child, "mii-bus", 0);
664 mdio_bus_switch = of_mdio_find_bus(mdio);
665 if (!mdio_bus_switch) {
670 /* Drop the mdio_bus device ref, replacing the host
671 * device with the mdio_bus_switch device, keeping
672 * the refcount from of_mdio_find_bus() above.
674 put_device(cd->host_dev);
675 cd->host_dev = &mdio_bus_switch->dev;
678 for_each_available_child_of_node(child, port) {
679 port_reg = of_get_property(port, "reg", NULL);
683 port_index = be32_to_cpup(port_reg);
684 if (port_index >= DSA_MAX_PORTS)
687 port_name = of_get_property(port, "label", NULL);
691 cd->port_dn[port_index] = port;
693 cd->port_names[port_index] = kstrdup(port_name,
695 if (!cd->port_names[port_index]) {
700 ret = dsa_of_probe_links(pd, cd, chip_index,
701 port_index, port, port_name);
708 /* The individual chips hold their own refcount on the mdio bus,
710 put_device(&mdio_bus->dev);
715 dsa_of_free_platform_data(pd);
718 dev->platform_data = NULL;
720 put_device(ðernet_dev->dev);
722 put_device(&mdio_bus->dev);
726 static void dsa_of_remove(struct device *dev)
728 struct dsa_platform_data *pd = dev->platform_data;
733 dsa_of_free_platform_data(pd);
734 put_device(&pd->of_netdev->dev);
738 static inline int dsa_of_probe(struct device *dev)
743 static inline void dsa_of_remove(struct device *dev)
748 static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
749 struct device *parent, struct dsa_platform_data *pd)
752 unsigned configured = 0;
755 dst->master_netdev = dev;
758 for (i = 0; i < pd->nr_chips; i++) {
759 struct dsa_switch *ds;
761 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
763 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
774 * If no switch was found, exit cleanly
777 return -EPROBE_DEFER;
780 * If we use a tagging format that doesn't have an ethertype
781 * field, make sure that all packets from this point on get
782 * sent to the tag format's receive function.
785 dev->dsa_ptr = (void *)dst;
790 static int dsa_probe(struct platform_device *pdev)
792 struct dsa_platform_data *pd = pdev->dev.platform_data;
793 struct net_device *dev;
794 struct dsa_switch_tree *dst;
797 if (pdev->dev.of_node) {
798 ret = dsa_of_probe(&pdev->dev);
802 pd = pdev->dev.platform_data;
805 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
812 dev = dev_to_net_device(pd->netdev);
819 if (dev->dsa_ptr != NULL) {
825 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
832 platform_set_drvdata(pdev, dst);
834 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
843 dsa_of_remove(&pdev->dev);
848 static void dsa_remove_dst(struct dsa_switch_tree *dst)
852 dst->master_netdev->dsa_ptr = NULL;
854 /* If we used a tagging format that doesn't have an ethertype
855 * field, make sure that all packets from this point get sent
856 * without the tag and go through the regular receive path.
860 for (i = 0; i < dst->pd->nr_chips; i++) {
861 struct dsa_switch *ds = dst->ds[i];
864 dsa_switch_destroy(ds);
867 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
869 dev_put(dst->master_netdev);
872 static int dsa_remove(struct platform_device *pdev)
874 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
877 dsa_of_remove(&pdev->dev);
882 static void dsa_shutdown(struct platform_device *pdev)
886 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
887 struct packet_type *pt, struct net_device *orig_dev)
889 struct dsa_switch_tree *dst = dev->dsa_ptr;
891 if (unlikely(dst == NULL)) {
896 return dst->rcv(skb, dev, pt, orig_dev);
899 static struct packet_type dsa_pack_type __read_mostly = {
900 .type = cpu_to_be16(ETH_P_XDSA),
901 .func = dsa_switch_rcv,
904 static struct notifier_block dsa_netdevice_nb __read_mostly = {
905 .notifier_call = dsa_slave_netdevice_event,
908 #ifdef CONFIG_PM_SLEEP
909 static int dsa_suspend(struct device *d)
911 struct platform_device *pdev = to_platform_device(d);
912 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
915 for (i = 0; i < dst->pd->nr_chips; i++) {
916 struct dsa_switch *ds = dst->ds[i];
919 ret = dsa_switch_suspend(ds);
925 static int dsa_resume(struct device *d)
927 struct platform_device *pdev = to_platform_device(d);
928 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
931 for (i = 0; i < dst->pd->nr_chips; i++) {
932 struct dsa_switch *ds = dst->ds[i];
935 ret = dsa_switch_resume(ds);
942 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
944 static const struct of_device_id dsa_of_match_table[] = {
945 { .compatible = "marvell,dsa", },
948 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
950 static struct platform_driver dsa_driver = {
952 .remove = dsa_remove,
953 .shutdown = dsa_shutdown,
956 .of_match_table = dsa_of_match_table,
961 static int __init dsa_init_module(void)
965 register_netdevice_notifier(&dsa_netdevice_nb);
967 rc = platform_driver_register(&dsa_driver);
971 dev_add_pack(&dsa_pack_type);
975 module_init(dsa_init_module);
977 static void __exit dsa_cleanup_module(void)
979 unregister_netdevice_notifier(&dsa_netdevice_nb);
980 dev_remove_pack(&dsa_pack_type);
981 platform_driver_unregister(&dsa_driver);
983 module_exit(dsa_cleanup_module);
985 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
986 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
987 MODULE_LICENSE("GPL");
988 MODULE_ALIAS("platform:dsa");