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>
18 #include <linux/of_mdio.h>
19 #include <linux/of_platform.h>
20 #include <linux/of_net.h>
21 #include <linux/of_gpio.h>
22 #include <linux/netdevice.h>
23 #include <linux/sysfs.h>
24 #include <linux/phy_fixed.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/etherdevice.h>
30 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
31 struct net_device *dev)
33 /* Just return the original SKB */
37 static const struct dsa_device_ops none_ops = {
38 .xmit = dsa_slave_notag_xmit,
42 const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
43 #ifdef CONFIG_NET_DSA_TAG_DSA
44 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
46 #ifdef CONFIG_NET_DSA_TAG_EDSA
47 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
49 #ifdef CONFIG_NET_DSA_TAG_TRAILER
50 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
52 #ifdef CONFIG_NET_DSA_TAG_BRCM
53 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
55 #ifdef CONFIG_NET_DSA_TAG_QCA
56 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
58 #ifdef CONFIG_NET_DSA_TAG_MTK
59 [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
61 [DSA_TAG_PROTO_NONE] = &none_ops,
64 /* switch driver registration ***********************************************/
65 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
66 static LIST_HEAD(dsa_switch_drivers);
68 void register_switch_driver(struct dsa_switch_driver *drv)
70 mutex_lock(&dsa_switch_drivers_mutex);
71 list_add_tail(&drv->list, &dsa_switch_drivers);
72 mutex_unlock(&dsa_switch_drivers_mutex);
74 EXPORT_SYMBOL_GPL(register_switch_driver);
76 void unregister_switch_driver(struct dsa_switch_driver *drv)
78 mutex_lock(&dsa_switch_drivers_mutex);
79 list_del_init(&drv->list);
80 mutex_unlock(&dsa_switch_drivers_mutex);
82 EXPORT_SYMBOL_GPL(unregister_switch_driver);
84 static const struct dsa_switch_ops *
85 dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
86 const char **_name, void **priv)
88 const struct dsa_switch_ops *ret;
89 struct list_head *list;
95 mutex_lock(&dsa_switch_drivers_mutex);
96 list_for_each(list, &dsa_switch_drivers) {
97 const struct dsa_switch_ops *ops;
98 struct dsa_switch_driver *drv;
100 drv = list_entry(list, struct dsa_switch_driver, list);
103 name = ops->probe(parent, host_dev, sw_addr, priv);
109 mutex_unlock(&dsa_switch_drivers_mutex);
116 /* basic switch operations **************************************************/
117 int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
118 struct dsa_port *dport, int port)
120 struct device_node *port_dn = dport->dn;
121 struct phy_device *phydev;
124 if (of_phy_is_fixed_link(port_dn)) {
125 ret = of_phy_register_fixed_link(port_dn);
127 dev_err(dev, "failed to register fixed PHY\n");
130 phydev = of_phy_find_device(port_dn);
132 mode = of_get_phy_mode(port_dn);
134 mode = PHY_INTERFACE_MODE_NA;
135 phydev->interface = mode;
137 genphy_config_init(phydev);
138 genphy_read_status(phydev);
139 if (ds->ops->adjust_link)
140 ds->ops->adjust_link(ds, port, phydev);
142 put_device(&phydev->mdio.dev);
148 static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
150 struct dsa_port *dport;
153 for (port = 0; port < ds->num_ports; port++) {
154 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
157 dport = &ds->ports[port];
158 ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
165 const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
167 const struct dsa_device_ops *ops;
169 if (tag_protocol >= DSA_TAG_LAST)
170 return ERR_PTR(-EINVAL);
171 ops = dsa_device_ops[tag_protocol];
174 return ERR_PTR(-ENOPROTOOPT);
179 int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
181 struct net_device *master;
182 struct ethtool_ops *cpu_ops;
184 master = ds->dst->master_netdev;
185 if (ds->master_netdev)
186 master = ds->master_netdev;
188 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
192 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
193 sizeof(struct ethtool_ops));
194 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
195 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
196 sizeof(struct ethtool_ops));
197 dsa_cpu_port_ethtool_init(cpu_ops);
198 master->ethtool_ops = cpu_ops;
203 void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
205 struct net_device *master;
207 master = ds->dst->master_netdev;
208 if (ds->master_netdev)
209 master = ds->master_netdev;
211 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
214 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
216 const struct dsa_switch_ops *ops = ds->ops;
217 struct dsa_switch_tree *dst = ds->dst;
218 struct dsa_chip_data *cd = ds->cd;
219 bool valid_name_found = false;
220 int index = ds->index;
224 * Validate supplied switch configuration.
226 for (i = 0; i < ds->num_ports; i++) {
229 name = cd->port_names[i];
233 if (!strcmp(name, "cpu")) {
234 if (dst->cpu_switch) {
235 netdev_err(dst->master_netdev,
236 "multiple cpu ports?!\n");
239 dst->cpu_switch = ds;
241 ds->cpu_port_mask |= 1 << i;
242 } else if (!strcmp(name, "dsa")) {
243 ds->dsa_port_mask |= 1 << i;
245 ds->enabled_port_mask |= 1 << i;
247 valid_name_found = true;
250 if (!valid_name_found && i == ds->num_ports)
253 /* Make the built-in MII bus mask match the number of ports,
254 * switch drivers can override this later
256 ds->phys_mii_mask = ds->enabled_port_mask;
259 * If the CPU connects to this switch, set the switch tree
260 * tagging protocol to the preferred tagging format of this
263 if (dst->cpu_switch == ds) {
264 enum dsa_tag_protocol tag_protocol;
266 tag_protocol = ops->get_tag_protocol(ds);
267 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
268 if (IS_ERR(dst->tag_ops))
269 return PTR_ERR(dst->tag_ops);
271 dst->rcv = dst->tag_ops->rcv;
274 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
277 * Do basic register setup.
279 ret = ops->setup(ds);
283 ret = dsa_switch_register_notifier(ds);
288 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
293 if (!ds->slave_mii_bus && ops->phy_read) {
294 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
295 if (!ds->slave_mii_bus)
297 dsa_slave_mii_bus_init(ds);
299 ret = mdiobus_register(ds->slave_mii_bus);
305 * Create network devices for physical switch ports.
307 for (i = 0; i < ds->num_ports; i++) {
308 ds->ports[i].dn = cd->port_dn[i];
310 if (!(ds->enabled_port_mask & (1 << i)))
313 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
315 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
316 index, i, cd->port_names[i], ret);
319 /* Perform configuration of the CPU and DSA ports */
320 ret = dsa_cpu_dsa_setups(ds, parent);
322 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
325 ret = dsa_cpu_port_ethtool_setup(ds);
332 static struct dsa_switch *
333 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
334 struct device *parent, struct device *host_dev)
336 struct dsa_chip_data *cd = dst->pd->chip + index;
337 const struct dsa_switch_ops *ops;
338 struct dsa_switch *ds;
344 * Probe for switch model.
346 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
348 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
350 return ERR_PTR(-EINVAL);
352 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
357 * Allocate and initialise switch state.
359 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
361 return ERR_PTR(-ENOMEM);
369 ret = dsa_switch_setup_one(ds, parent);
376 void dsa_cpu_dsa_destroy(struct dsa_port *port)
378 struct device_node *port_dn = port->dn;
380 if (of_phy_is_fixed_link(port_dn))
381 of_phy_deregister_fixed_link(port_dn);
384 static void dsa_switch_destroy(struct dsa_switch *ds)
388 /* Destroy network devices for physical switch ports. */
389 for (port = 0; port < ds->num_ports; port++) {
390 if (!(ds->enabled_port_mask & (1 << port)))
393 if (!ds->ports[port].netdev)
396 dsa_slave_destroy(ds->ports[port].netdev);
399 /* Disable configuration of the CPU and DSA ports */
400 for (port = 0; port < ds->num_ports; port++) {
401 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
403 dsa_cpu_dsa_destroy(&ds->ports[port]);
405 /* Clearing a bit which is not set does no harm */
406 ds->cpu_port_mask |= ~(1 << port);
407 ds->dsa_port_mask |= ~(1 << port);
410 if (ds->slave_mii_bus && ds->ops->phy_read)
411 mdiobus_unregister(ds->slave_mii_bus);
413 dsa_switch_unregister_notifier(ds);
416 #ifdef CONFIG_PM_SLEEP
417 int dsa_switch_suspend(struct dsa_switch *ds)
421 /* Suspend slave network devices */
422 for (i = 0; i < ds->num_ports; i++) {
423 if (!dsa_is_port_initialized(ds, i))
426 ret = dsa_slave_suspend(ds->ports[i].netdev);
431 if (ds->ops->suspend)
432 ret = ds->ops->suspend(ds);
436 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
438 int dsa_switch_resume(struct dsa_switch *ds)
443 ret = ds->ops->resume(ds);
448 /* Resume slave network devices */
449 for (i = 0; i < ds->num_ports; i++) {
450 if (!dsa_is_port_initialized(ds, i))
453 ret = dsa_slave_resume(ds->ports[i].netdev);
460 EXPORT_SYMBOL_GPL(dsa_switch_resume);
463 /* platform driver init and cleanup *****************************************/
464 static int dev_is_class(struct device *dev, void *class)
466 if (dev->class != NULL && !strcmp(dev->class->name, class))
472 static struct device *dev_find_class(struct device *parent, char *class)
474 if (dev_is_class(parent, class)) {
479 return device_find_child(parent, class, dev_is_class);
482 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
486 d = dev_find_class(dev, "mdio_bus");
498 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
500 struct net_device *dsa_dev_to_net_device(struct device *dev)
504 d = dev_find_class(dev, "net");
506 struct net_device *nd;
517 EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
520 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
521 struct dsa_chip_data *cd,
522 int chip_index, int port_index,
523 struct device_node *link)
527 struct device_node *parent_sw;
530 parent_sw = of_get_parent(link);
534 reg = of_get_property(parent_sw, "reg", &len);
535 if (!reg || (len != sizeof(*reg) * 2))
539 * Get the destination switch number from the second field of its 'reg'
540 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
542 link_sw_addr = be32_to_cpup(reg + 1);
544 if (link_sw_addr >= pd->nr_chips)
547 cd->rtable[link_sw_addr] = port_index;
552 static int dsa_of_probe_links(struct dsa_platform_data *pd,
553 struct dsa_chip_data *cd,
554 int chip_index, int port_index,
555 struct device_node *port,
556 const char *port_name)
558 struct device_node *link;
562 for (link_index = 0;; link_index++) {
563 link = of_parse_phandle(port, "link", link_index);
567 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
568 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
577 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
582 for (i = 0; i < pd->nr_chips; i++) {
584 while (port_index < DSA_MAX_PORTS) {
585 kfree(pd->chip[i].port_names[port_index]);
589 /* Drop our reference to the MDIO bus device */
590 if (pd->chip[i].host_dev)
591 put_device(pd->chip[i].host_dev);
596 static int dsa_of_probe(struct device *dev)
598 struct device_node *np = dev->of_node;
599 struct device_node *child, *mdio, *ethernet, *port;
600 struct mii_bus *mdio_bus, *mdio_bus_switch;
601 struct net_device *ethernet_dev;
602 struct dsa_platform_data *pd;
603 struct dsa_chip_data *cd;
604 const char *port_name;
605 int chip_index, port_index;
606 const unsigned int *sw_addr, *port_reg;
610 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
614 mdio_bus = of_mdio_find_bus(mdio);
616 return -EPROBE_DEFER;
618 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
624 ethernet_dev = of_find_net_device_by_node(ethernet);
630 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
633 goto out_put_ethernet;
636 dev->platform_data = pd;
637 pd->of_netdev = ethernet_dev;
638 pd->nr_chips = of_get_available_child_count(np);
639 if (pd->nr_chips > DSA_MAX_SWITCHES)
640 pd->nr_chips = DSA_MAX_SWITCHES;
642 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
650 for_each_available_child_of_node(np, child) {
654 cd = &pd->chip[chip_index];
658 /* Initialize the routing table */
659 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
660 cd->rtable[i] = DSA_RTABLE_NONE;
662 /* When assigning the host device, increment its refcount */
663 cd->host_dev = get_device(&mdio_bus->dev);
665 sw_addr = of_get_property(child, "reg", NULL);
669 cd->sw_addr = be32_to_cpup(sw_addr);
670 if (cd->sw_addr >= PHY_MAX_ADDR)
673 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
674 cd->eeprom_len = eeprom_len;
676 mdio = of_parse_phandle(child, "mii-bus", 0);
678 mdio_bus_switch = of_mdio_find_bus(mdio);
679 if (!mdio_bus_switch) {
684 /* Drop the mdio_bus device ref, replacing the host
685 * device with the mdio_bus_switch device, keeping
686 * the refcount from of_mdio_find_bus() above.
688 put_device(cd->host_dev);
689 cd->host_dev = &mdio_bus_switch->dev;
692 for_each_available_child_of_node(child, port) {
693 port_reg = of_get_property(port, "reg", NULL);
697 port_index = be32_to_cpup(port_reg);
698 if (port_index >= DSA_MAX_PORTS)
701 port_name = of_get_property(port, "label", NULL);
705 cd->port_dn[port_index] = port;
707 cd->port_names[port_index] = kstrdup(port_name,
709 if (!cd->port_names[port_index]) {
714 ret = dsa_of_probe_links(pd, cd, chip_index,
715 port_index, port, port_name);
722 /* The individual chips hold their own refcount on the mdio bus,
724 put_device(&mdio_bus->dev);
729 dsa_of_free_platform_data(pd);
732 dev->platform_data = NULL;
734 put_device(ðernet_dev->dev);
736 put_device(&mdio_bus->dev);
740 static void dsa_of_remove(struct device *dev)
742 struct dsa_platform_data *pd = dev->platform_data;
747 dsa_of_free_platform_data(pd);
748 put_device(&pd->of_netdev->dev);
752 static inline int dsa_of_probe(struct device *dev)
757 static inline void dsa_of_remove(struct device *dev)
762 static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
763 struct device *parent, struct dsa_platform_data *pd)
766 unsigned configured = 0;
769 dst->master_netdev = dev;
772 for (i = 0; i < pd->nr_chips; i++) {
773 struct dsa_switch *ds;
775 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
777 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
788 * If no switch was found, exit cleanly
791 return -EPROBE_DEFER;
794 * If we use a tagging format that doesn't have an ethertype
795 * field, make sure that all packets from this point on get
796 * sent to the tag format's receive function.
799 dev->dsa_ptr = (void *)dst;
804 static int dsa_probe(struct platform_device *pdev)
806 struct dsa_platform_data *pd = pdev->dev.platform_data;
807 struct net_device *dev;
808 struct dsa_switch_tree *dst;
811 if (pdev->dev.of_node) {
812 ret = dsa_of_probe(&pdev->dev);
816 pd = pdev->dev.platform_data;
819 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
826 dev = dsa_dev_to_net_device(pd->netdev);
833 if (dev->dsa_ptr != NULL) {
839 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
846 platform_set_drvdata(pdev, dst);
848 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
857 dsa_of_remove(&pdev->dev);
862 static void dsa_remove_dst(struct dsa_switch_tree *dst)
866 dst->master_netdev->dsa_ptr = NULL;
868 /* If we used a tagging format that doesn't have an ethertype
869 * field, make sure that all packets from this point get sent
870 * without the tag and go through the regular receive path.
874 for (i = 0; i < dst->pd->nr_chips; i++) {
875 struct dsa_switch *ds = dst->ds[i];
878 dsa_switch_destroy(ds);
881 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
883 dev_put(dst->master_netdev);
886 static int dsa_remove(struct platform_device *pdev)
888 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
891 dsa_of_remove(&pdev->dev);
896 static void dsa_shutdown(struct platform_device *pdev)
900 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
901 struct packet_type *pt, struct net_device *orig_dev)
903 struct dsa_switch_tree *dst = dev->dsa_ptr;
904 struct sk_buff *nskb = NULL;
906 if (unlikely(dst == NULL)) {
911 skb = skb_unshare(skb, GFP_ATOMIC);
915 nskb = dst->rcv(skb, dev, pt, orig_dev);
922 skb_push(skb, ETH_HLEN);
923 skb->pkt_type = PACKET_HOST;
924 skb->protocol = eth_type_trans(skb, skb->dev);
926 skb->dev->stats.rx_packets++;
927 skb->dev->stats.rx_bytes += skb->len;
929 netif_receive_skb(skb);
934 static struct packet_type dsa_pack_type __read_mostly = {
935 .type = cpu_to_be16(ETH_P_XDSA),
936 .func = dsa_switch_rcv,
939 #ifdef CONFIG_PM_SLEEP
940 static int dsa_suspend(struct device *d)
942 struct platform_device *pdev = to_platform_device(d);
943 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
946 for (i = 0; i < dst->pd->nr_chips; i++) {
947 struct dsa_switch *ds = dst->ds[i];
950 ret = dsa_switch_suspend(ds);
956 static int dsa_resume(struct device *d)
958 struct platform_device *pdev = to_platform_device(d);
959 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
962 for (i = 0; i < dst->pd->nr_chips; i++) {
963 struct dsa_switch *ds = dst->ds[i];
966 ret = dsa_switch_resume(ds);
973 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
975 static const struct of_device_id dsa_of_match_table[] = {
976 { .compatible = "marvell,dsa", },
979 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
981 static struct platform_driver dsa_driver = {
983 .remove = dsa_remove,
984 .shutdown = dsa_shutdown,
987 .of_match_table = dsa_of_match_table,
992 static int __init dsa_init_module(void)
996 rc = dsa_slave_register_notifier();
1000 rc = platform_driver_register(&dsa_driver);
1004 dev_add_pack(&dsa_pack_type);
1008 module_init(dsa_init_module);
1010 static void __exit dsa_cleanup_module(void)
1012 dsa_slave_unregister_notifier();
1013 dev_remove_pack(&dsa_pack_type);
1014 platform_driver_unregister(&dsa_driver);
1016 module_exit(dsa_cleanup_module);
1018 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
1019 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1020 MODULE_LICENSE("GPL");
1021 MODULE_ALIAS("platform:dsa");