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/ctype.h>
13 #include <linux/device.h>
14 #include <linux/hwmon.h>
15 #include <linux/list.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
21 #include <linux/of_mdio.h>
22 #include <linux/of_platform.h>
23 #include <linux/of_net.h>
24 #include <linux/sysfs.h>
27 char dsa_driver_version[] = "0.1";
30 /* switch driver registration ***********************************************/
31 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
32 static LIST_HEAD(dsa_switch_drivers);
34 void register_switch_driver(struct dsa_switch_driver *drv)
36 mutex_lock(&dsa_switch_drivers_mutex);
37 list_add_tail(&drv->list, &dsa_switch_drivers);
38 mutex_unlock(&dsa_switch_drivers_mutex);
40 EXPORT_SYMBOL_GPL(register_switch_driver);
42 void unregister_switch_driver(struct dsa_switch_driver *drv)
44 mutex_lock(&dsa_switch_drivers_mutex);
45 list_del_init(&drv->list);
46 mutex_unlock(&dsa_switch_drivers_mutex);
48 EXPORT_SYMBOL_GPL(unregister_switch_driver);
50 static struct dsa_switch_driver *
51 dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
53 struct dsa_switch_driver *ret;
54 struct list_head *list;
60 mutex_lock(&dsa_switch_drivers_mutex);
61 list_for_each(list, &dsa_switch_drivers) {
62 struct dsa_switch_driver *drv;
64 drv = list_entry(list, struct dsa_switch_driver, list);
66 name = drv->probe(host_dev, sw_addr);
72 mutex_unlock(&dsa_switch_drivers_mutex);
79 /* hwmon support ************************************************************/
81 #ifdef CONFIG_NET_DSA_HWMON
83 static ssize_t temp1_input_show(struct device *dev,
84 struct device_attribute *attr, char *buf)
86 struct dsa_switch *ds = dev_get_drvdata(dev);
89 ret = ds->drv->get_temp(ds, &temp);
93 return sprintf(buf, "%d\n", temp * 1000);
95 static DEVICE_ATTR_RO(temp1_input);
97 static ssize_t temp1_max_show(struct device *dev,
98 struct device_attribute *attr, char *buf)
100 struct dsa_switch *ds = dev_get_drvdata(dev);
103 ret = ds->drv->get_temp_limit(ds, &temp);
107 return sprintf(buf, "%d\n", temp * 1000);
110 static ssize_t temp1_max_store(struct device *dev,
111 struct device_attribute *attr, const char *buf,
114 struct dsa_switch *ds = dev_get_drvdata(dev);
117 ret = kstrtoint(buf, 0, &temp);
121 ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
127 static DEVICE_ATTR(temp1_max, S_IRUGO, temp1_max_show, temp1_max_store);
129 static ssize_t temp1_max_alarm_show(struct device *dev,
130 struct device_attribute *attr, char *buf)
132 struct dsa_switch *ds = dev_get_drvdata(dev);
136 ret = ds->drv->get_temp_alarm(ds, &alarm);
140 return sprintf(buf, "%d\n", alarm);
142 static DEVICE_ATTR_RO(temp1_max_alarm);
144 static struct attribute *dsa_hwmon_attrs[] = {
145 &dev_attr_temp1_input.attr, /* 0 */
146 &dev_attr_temp1_max.attr, /* 1 */
147 &dev_attr_temp1_max_alarm.attr, /* 2 */
151 static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
152 struct attribute *attr, int index)
154 struct device *dev = container_of(kobj, struct device, kobj);
155 struct dsa_switch *ds = dev_get_drvdata(dev);
156 struct dsa_switch_driver *drv = ds->drv;
157 umode_t mode = attr->mode;
160 if (!drv->get_temp_limit)
162 else if (drv->set_temp_limit)
164 } else if (index == 2 && !drv->get_temp_alarm) {
170 static const struct attribute_group dsa_hwmon_group = {
171 .attrs = dsa_hwmon_attrs,
172 .is_visible = dsa_hwmon_attrs_visible,
174 __ATTRIBUTE_GROUPS(dsa_hwmon);
176 #endif /* CONFIG_NET_DSA_HWMON */
178 /* basic switch operations **************************************************/
179 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
181 struct dsa_switch_driver *drv = ds->drv;
182 struct dsa_switch_tree *dst = ds->dst;
183 struct dsa_chip_data *pd = ds->pd;
184 bool valid_name_found = false;
185 int index = ds->index;
189 * Validate supplied switch configuration.
191 for (i = 0; i < DSA_MAX_PORTS; i++) {
194 name = pd->port_names[i];
198 if (!strcmp(name, "cpu")) {
199 if (dst->cpu_switch != -1) {
200 netdev_err(dst->master_netdev,
201 "multiple cpu ports?!\n");
205 dst->cpu_switch = index;
207 } else if (!strcmp(name, "dsa")) {
208 ds->dsa_port_mask |= 1 << i;
210 ds->phys_port_mask |= 1 << i;
212 valid_name_found = true;
215 if (!valid_name_found && i == DSA_MAX_PORTS) {
220 /* Make the built-in MII bus mask match the number of ports,
221 * switch drivers can override this later
223 ds->phys_mii_mask = ds->phys_port_mask;
226 * If the CPU connects to this switch, set the switch tree
227 * tagging protocol to the preferred tagging format of this
230 if (dst->cpu_switch == index) {
231 switch (ds->tag_protocol) {
232 #ifdef CONFIG_NET_DSA_TAG_DSA
233 case DSA_TAG_PROTO_DSA:
234 dst->rcv = dsa_netdev_ops.rcv;
237 #ifdef CONFIG_NET_DSA_TAG_EDSA
238 case DSA_TAG_PROTO_EDSA:
239 dst->rcv = edsa_netdev_ops.rcv;
242 #ifdef CONFIG_NET_DSA_TAG_TRAILER
243 case DSA_TAG_PROTO_TRAILER:
244 dst->rcv = trailer_netdev_ops.rcv;
247 #ifdef CONFIG_NET_DSA_TAG_BRCM
248 case DSA_TAG_PROTO_BRCM:
249 dst->rcv = brcm_netdev_ops.rcv;
252 case DSA_TAG_PROTO_NONE:
259 dst->tag_protocol = ds->tag_protocol;
263 * Do basic register setup.
265 ret = drv->setup(ds);
269 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
273 ds->slave_mii_bus = mdiobus_alloc();
274 if (ds->slave_mii_bus == NULL) {
278 dsa_slave_mii_bus_init(ds);
280 ret = mdiobus_register(ds->slave_mii_bus);
286 * Create network devices for physical switch ports.
288 for (i = 0; i < DSA_MAX_PORTS; i++) {
289 if (!(ds->phys_port_mask & (1 << i)))
292 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
294 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
295 index, i, pd->port_names[i]);
300 #ifdef CONFIG_NET_DSA_HWMON
301 /* If the switch provides a temperature sensor,
302 * register with hardware monitoring subsystem.
303 * Treat registration error as non-fatal and ignore it.
306 const char *netname = netdev_name(dst->master_netdev);
307 char hname[IFNAMSIZ + 1];
310 /* Create valid hwmon 'name' attribute */
311 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
312 if (isalnum(netname[i]))
313 hname[j++] = netname[i];
316 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
318 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
319 ds->hwmon_name, ds, dsa_hwmon_groups);
320 if (IS_ERR(ds->hwmon_dev))
321 ds->hwmon_dev = NULL;
323 #endif /* CONFIG_NET_DSA_HWMON */
328 mdiobus_free(ds->slave_mii_bus);
334 static struct dsa_switch *
335 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
336 struct device *parent, struct device *host_dev)
338 struct dsa_chip_data *pd = dst->pd->chip + index;
339 struct dsa_switch_driver *drv;
340 struct dsa_switch *ds;
345 * Probe for switch model.
347 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
349 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
351 return ERR_PTR(-EINVAL);
353 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
358 * Allocate and initialise switch state.
360 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
368 ds->tag_protocol = drv->tag_protocol;
369 ds->master_dev = host_dev;
371 ret = dsa_switch_setup_one(ds, parent);
378 static void dsa_switch_destroy(struct dsa_switch *ds)
380 #ifdef CONFIG_NET_DSA_HWMON
382 hwmon_device_unregister(ds->hwmon_dev);
386 #ifdef CONFIG_PM_SLEEP
387 static int dsa_switch_suspend(struct dsa_switch *ds)
391 /* Suspend slave network devices */
392 for (i = 0; i < DSA_MAX_PORTS; i++) {
393 if (!dsa_is_port_initialized(ds, i))
396 ret = dsa_slave_suspend(ds->ports[i]);
401 if (ds->drv->suspend)
402 ret = ds->drv->suspend(ds);
407 static int dsa_switch_resume(struct dsa_switch *ds)
412 ret = ds->drv->resume(ds);
417 /* Resume slave network devices */
418 for (i = 0; i < DSA_MAX_PORTS; i++) {
419 if (!dsa_is_port_initialized(ds, i))
422 ret = dsa_slave_resume(ds->ports[i]);
432 /* link polling *************************************************************/
433 static void dsa_link_poll_work(struct work_struct *ugly)
435 struct dsa_switch_tree *dst;
438 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
440 for (i = 0; i < dst->pd->nr_chips; i++) {
441 struct dsa_switch *ds = dst->ds[i];
443 if (ds != NULL && ds->drv->poll_link != NULL)
444 ds->drv->poll_link(ds);
447 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
450 static void dsa_link_poll_timer(unsigned long _dst)
452 struct dsa_switch_tree *dst = (void *)_dst;
454 schedule_work(&dst->link_poll_work);
458 /* platform driver init and cleanup *****************************************/
459 static int dev_is_class(struct device *dev, void *class)
461 if (dev->class != NULL && !strcmp(dev->class->name, class))
467 static struct device *dev_find_class(struct device *parent, char *class)
469 if (dev_is_class(parent, class)) {
474 return device_find_child(parent, class, dev_is_class);
477 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
481 d = dev_find_class(dev, "mdio_bus");
493 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
495 static struct net_device *dev_to_net_device(struct device *dev)
499 d = dev_find_class(dev, "net");
501 struct net_device *nd;
514 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
515 struct dsa_chip_data *cd,
517 struct device_node *link)
523 struct device_node *parent_sw;
526 parent_sw = of_get_parent(link);
530 reg = of_get_property(parent_sw, "reg", &len);
531 if (!reg || (len != sizeof(*reg) * 2))
534 link_sw_addr = be32_to_cpup(reg + 1);
536 if (link_sw_addr >= pd->nr_chips)
539 /* First time routing table allocation */
541 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
546 /* default to no valid uplink/downlink */
547 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
550 reg = of_get_property(link, "reg", NULL);
556 link_port_addr = be32_to_cpup(reg);
558 cd->rtable[link_sw_addr] = link_port_addr;
566 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
571 for (i = 0; i < pd->nr_chips; i++) {
573 while (port_index < DSA_MAX_PORTS) {
574 kfree(pd->chip[i].port_names[port_index]);
577 kfree(pd->chip[i].rtable);
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, *link;
586 struct mii_bus *mdio_bus;
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);
608 ethernet_dev = of_find_net_device_by_node(ethernet);
610 return -EPROBE_DEFER;
612 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
616 dev->platform_data = pd;
617 pd->of_netdev = ethernet_dev;
618 pd->nr_chips = of_get_available_child_count(np);
619 if (pd->nr_chips > DSA_MAX_SWITCHES)
620 pd->nr_chips = DSA_MAX_SWITCHES;
622 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
630 for_each_available_child_of_node(np, child) {
632 cd = &pd->chip[chip_index];
635 cd->host_dev = &mdio_bus->dev;
637 sw_addr = of_get_property(child, "reg", NULL);
641 cd->sw_addr = be32_to_cpup(sw_addr);
642 if (cd->sw_addr > PHY_MAX_ADDR)
645 if (!of_property_read_u32(np, "eeprom-length", &eeprom_len))
646 cd->eeprom_len = eeprom_len;
648 for_each_available_child_of_node(child, port) {
649 port_reg = of_get_property(port, "reg", NULL);
653 port_index = be32_to_cpup(port_reg);
655 port_name = of_get_property(port, "label", NULL);
659 cd->port_dn[port_index] = port;
661 cd->port_names[port_index] = kstrdup(port_name,
663 if (!cd->port_names[port_index]) {
668 link = of_parse_phandle(port, "link", 0);
670 if (!strcmp(port_name, "dsa") && link &&
672 ret = dsa_of_setup_routing_table(pd, cd,
678 if (port_index == DSA_MAX_PORTS)
686 dsa_of_free_platform_data(pd);
689 dev->platform_data = NULL;
693 static void dsa_of_remove(struct device *dev)
695 struct dsa_platform_data *pd = dev->platform_data;
700 dsa_of_free_platform_data(pd);
704 static inline int dsa_of_probe(struct device *dev)
709 static inline void dsa_of_remove(struct device *dev)
714 static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
715 struct device *parent, struct dsa_platform_data *pd)
720 dst->master_netdev = dev;
721 dst->cpu_switch = -1;
724 for (i = 0; i < pd->nr_chips; i++) {
725 struct dsa_switch *ds;
727 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
729 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
735 if (ds->drv->poll_link != NULL)
736 dst->link_poll_needed = 1;
740 * If we use a tagging format that doesn't have an ethertype
741 * field, make sure that all packets from this point on get
742 * sent to the tag format's receive function.
745 dev->dsa_ptr = (void *)dst;
747 if (dst->link_poll_needed) {
748 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
749 init_timer(&dst->link_poll_timer);
750 dst->link_poll_timer.data = (unsigned long)dst;
751 dst->link_poll_timer.function = dsa_link_poll_timer;
752 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
753 add_timer(&dst->link_poll_timer);
757 static int dsa_probe(struct platform_device *pdev)
759 struct dsa_platform_data *pd = pdev->dev.platform_data;
760 struct net_device *dev;
761 struct dsa_switch_tree *dst;
764 pr_notice_once("Distributed Switch Architecture driver version %s\n",
767 if (pdev->dev.of_node) {
768 ret = dsa_of_probe(&pdev->dev);
772 pd = pdev->dev.platform_data;
775 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
782 dev = dev_to_net_device(pd->netdev);
789 if (dev->dsa_ptr != NULL) {
795 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
802 platform_set_drvdata(pdev, dst);
804 dsa_setup_dst(dst, dev, &pdev->dev, pd);
809 dsa_of_remove(&pdev->dev);
814 static void dsa_remove_dst(struct dsa_switch_tree *dst)
818 if (dst->link_poll_needed)
819 del_timer_sync(&dst->link_poll_timer);
821 flush_work(&dst->link_poll_work);
823 for (i = 0; i < dst->pd->nr_chips; i++) {
824 struct dsa_switch *ds = dst->ds[i];
827 dsa_switch_destroy(ds);
831 static int dsa_remove(struct platform_device *pdev)
833 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
836 dsa_of_remove(&pdev->dev);
841 static void dsa_shutdown(struct platform_device *pdev)
845 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
846 struct packet_type *pt, struct net_device *orig_dev)
848 struct dsa_switch_tree *dst = dev->dsa_ptr;
850 if (unlikely(dst == NULL)) {
855 return dst->rcv(skb, dev, pt, orig_dev);
858 static struct packet_type dsa_pack_type __read_mostly = {
859 .type = cpu_to_be16(ETH_P_XDSA),
860 .func = dsa_switch_rcv,
863 static struct notifier_block dsa_netdevice_nb __read_mostly = {
864 .notifier_call = dsa_slave_netdevice_event,
867 #ifdef CONFIG_PM_SLEEP
868 static int dsa_suspend(struct device *d)
870 struct platform_device *pdev = to_platform_device(d);
871 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
874 for (i = 0; i < dst->pd->nr_chips; i++) {
875 struct dsa_switch *ds = dst->ds[i];
878 ret = dsa_switch_suspend(ds);
884 static int dsa_resume(struct device *d)
886 struct platform_device *pdev = to_platform_device(d);
887 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
890 for (i = 0; i < dst->pd->nr_chips; i++) {
891 struct dsa_switch *ds = dst->ds[i];
894 ret = dsa_switch_resume(ds);
901 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
903 static const struct of_device_id dsa_of_match_table[] = {
904 { .compatible = "brcm,bcm7445-switch-v4.0" },
905 { .compatible = "marvell,dsa", },
908 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
910 static struct platform_driver dsa_driver = {
912 .remove = dsa_remove,
913 .shutdown = dsa_shutdown,
916 .of_match_table = dsa_of_match_table,
921 static int __init dsa_init_module(void)
925 register_netdevice_notifier(&dsa_netdevice_nb);
927 rc = platform_driver_register(&dsa_driver);
931 dev_add_pack(&dsa_pack_type);
935 module_init(dsa_init_module);
937 static void __exit dsa_cleanup_module(void)
939 unregister_netdevice_notifier(&dsa_netdevice_nb);
940 dev_remove_pack(&dsa_pack_type);
941 platform_driver_unregister(&dsa_driver);
943 module_exit(dsa_cleanup_module);
945 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
946 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
947 MODULE_LICENSE("GPL");
948 MODULE_ALIAS("platform:dsa");