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/sysfs.h>
26 char dsa_driver_version[] = "0.1";
29 /* switch driver registration ***********************************************/
30 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
31 static LIST_HEAD(dsa_switch_drivers);
33 void register_switch_driver(struct dsa_switch_driver *drv)
35 mutex_lock(&dsa_switch_drivers_mutex);
36 list_add_tail(&drv->list, &dsa_switch_drivers);
37 mutex_unlock(&dsa_switch_drivers_mutex);
39 EXPORT_SYMBOL_GPL(register_switch_driver);
41 void unregister_switch_driver(struct dsa_switch_driver *drv)
43 mutex_lock(&dsa_switch_drivers_mutex);
44 list_del_init(&drv->list);
45 mutex_unlock(&dsa_switch_drivers_mutex);
47 EXPORT_SYMBOL_GPL(unregister_switch_driver);
49 static struct dsa_switch_driver *
50 dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
52 struct dsa_switch_driver *ret;
53 struct list_head *list;
59 mutex_lock(&dsa_switch_drivers_mutex);
60 list_for_each(list, &dsa_switch_drivers) {
61 struct dsa_switch_driver *drv;
63 drv = list_entry(list, struct dsa_switch_driver, list);
65 name = drv->probe(host_dev, sw_addr);
71 mutex_unlock(&dsa_switch_drivers_mutex);
78 /* hwmon support ************************************************************/
80 #ifdef CONFIG_NET_DSA_HWMON
82 static ssize_t temp1_input_show(struct device *dev,
83 struct device_attribute *attr, char *buf)
85 struct dsa_switch *ds = dev_get_drvdata(dev);
88 ret = ds->drv->get_temp(ds, &temp);
92 return sprintf(buf, "%d\n", temp * 1000);
94 static DEVICE_ATTR_RO(temp1_input);
96 static ssize_t temp1_max_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
99 struct dsa_switch *ds = dev_get_drvdata(dev);
102 ret = ds->drv->get_temp_limit(ds, &temp);
106 return sprintf(buf, "%d\n", temp * 1000);
109 static ssize_t temp1_max_store(struct device *dev,
110 struct device_attribute *attr, const char *buf,
113 struct dsa_switch *ds = dev_get_drvdata(dev);
116 ret = kstrtoint(buf, 0, &temp);
120 ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
126 static DEVICE_ATTR(temp1_max, S_IRUGO, temp1_max_show, temp1_max_store);
128 static ssize_t temp1_max_alarm_show(struct device *dev,
129 struct device_attribute *attr, char *buf)
131 struct dsa_switch *ds = dev_get_drvdata(dev);
135 ret = ds->drv->get_temp_alarm(ds, &alarm);
139 return sprintf(buf, "%d\n", alarm);
141 static DEVICE_ATTR_RO(temp1_max_alarm);
143 static struct attribute *dsa_hwmon_attrs[] = {
144 &dev_attr_temp1_input.attr, /* 0 */
145 &dev_attr_temp1_max.attr, /* 1 */
146 &dev_attr_temp1_max_alarm.attr, /* 2 */
150 static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
151 struct attribute *attr, int index)
153 struct device *dev = container_of(kobj, struct device, kobj);
154 struct dsa_switch *ds = dev_get_drvdata(dev);
155 struct dsa_switch_driver *drv = ds->drv;
156 umode_t mode = attr->mode;
159 if (!drv->get_temp_limit)
161 else if (drv->set_temp_limit)
163 } else if (index == 2 && !drv->get_temp_alarm) {
169 static const struct attribute_group dsa_hwmon_group = {
170 .attrs = dsa_hwmon_attrs,
171 .is_visible = dsa_hwmon_attrs_visible,
173 __ATTRIBUTE_GROUPS(dsa_hwmon);
175 #endif /* CONFIG_NET_DSA_HWMON */
177 /* basic switch operations **************************************************/
178 static struct dsa_switch *
179 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
180 struct device *parent, struct device *host_dev)
182 struct dsa_chip_data *pd = dst->pd->chip + index;
183 struct dsa_switch_driver *drv;
184 struct dsa_switch *ds;
188 bool valid_name_found = false;
191 * Probe for switch model.
193 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
195 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
197 return ERR_PTR(-EINVAL);
199 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
204 * Allocate and initialise switch state.
206 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
208 return ERR_PTR(-ENOMEM);
212 ds->pd = dst->pd->chip + index;
214 ds->master_dev = host_dev;
217 * Validate supplied switch configuration.
219 for (i = 0; i < DSA_MAX_PORTS; i++) {
222 name = pd->port_names[i];
226 if (!strcmp(name, "cpu")) {
227 if (dst->cpu_switch != -1) {
228 netdev_err(dst->master_netdev,
229 "multiple cpu ports?!\n");
233 dst->cpu_switch = index;
235 } else if (!strcmp(name, "dsa")) {
236 ds->dsa_port_mask |= 1 << i;
238 ds->phys_port_mask |= 1 << i;
240 valid_name_found = true;
243 if (!valid_name_found && i == DSA_MAX_PORTS) {
248 /* Make the built-in MII bus mask match the number of ports,
249 * switch drivers can override this later
251 ds->phys_mii_mask = ds->phys_port_mask;
254 * If the CPU connects to this switch, set the switch tree
255 * tagging protocol to the preferred tagging format of this
258 if (dst->cpu_switch == index) {
259 switch (drv->tag_protocol) {
260 #ifdef CONFIG_NET_DSA_TAG_DSA
261 case DSA_TAG_PROTO_DSA:
262 dst->rcv = dsa_netdev_ops.rcv;
265 #ifdef CONFIG_NET_DSA_TAG_EDSA
266 case DSA_TAG_PROTO_EDSA:
267 dst->rcv = edsa_netdev_ops.rcv;
270 #ifdef CONFIG_NET_DSA_TAG_TRAILER
271 case DSA_TAG_PROTO_TRAILER:
272 dst->rcv = trailer_netdev_ops.rcv;
275 #ifdef CONFIG_NET_DSA_TAG_BRCM
276 case DSA_TAG_PROTO_BRCM:
277 dst->rcv = brcm_netdev_ops.rcv;
280 case DSA_TAG_PROTO_NONE:
287 dst->tag_protocol = drv->tag_protocol;
291 * Do basic register setup.
293 ret = drv->setup(ds);
297 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
301 ds->slave_mii_bus = mdiobus_alloc();
302 if (ds->slave_mii_bus == NULL) {
306 dsa_slave_mii_bus_init(ds);
308 ret = mdiobus_register(ds->slave_mii_bus);
314 * Create network devices for physical switch ports.
316 for (i = 0; i < DSA_MAX_PORTS; i++) {
317 if (!(ds->phys_port_mask & (1 << i)))
320 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
322 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
323 index, i, pd->port_names[i]);
328 #ifdef CONFIG_NET_DSA_HWMON
329 /* If the switch provides a temperature sensor,
330 * register with hardware monitoring subsystem.
331 * Treat registration error as non-fatal and ignore it.
334 const char *netname = netdev_name(dst->master_netdev);
335 char hname[IFNAMSIZ + 1];
338 /* Create valid hwmon 'name' attribute */
339 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
340 if (isalnum(netname[i]))
341 hname[j++] = netname[i];
344 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
346 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
347 ds->hwmon_name, ds, dsa_hwmon_groups);
348 if (IS_ERR(ds->hwmon_dev))
349 ds->hwmon_dev = NULL;
351 #endif /* CONFIG_NET_DSA_HWMON */
356 mdiobus_free(ds->slave_mii_bus);
362 static void dsa_switch_destroy(struct dsa_switch *ds)
364 #ifdef CONFIG_NET_DSA_HWMON
366 hwmon_device_unregister(ds->hwmon_dev);
370 #ifdef CONFIG_PM_SLEEP
371 static int dsa_switch_suspend(struct dsa_switch *ds)
375 /* Suspend slave network devices */
376 for (i = 0; i < DSA_MAX_PORTS; i++) {
377 if (!dsa_is_port_initialized(ds, i))
380 ret = dsa_slave_suspend(ds->ports[i]);
385 if (ds->drv->suspend)
386 ret = ds->drv->suspend(ds);
391 static int dsa_switch_resume(struct dsa_switch *ds)
396 ret = ds->drv->resume(ds);
401 /* Resume slave network devices */
402 for (i = 0; i < DSA_MAX_PORTS; i++) {
403 if (!dsa_is_port_initialized(ds, i))
406 ret = dsa_slave_resume(ds->ports[i]);
416 /* link polling *************************************************************/
417 static void dsa_link_poll_work(struct work_struct *ugly)
419 struct dsa_switch_tree *dst;
422 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
424 for (i = 0; i < dst->pd->nr_chips; i++) {
425 struct dsa_switch *ds = dst->ds[i];
427 if (ds != NULL && ds->drv->poll_link != NULL)
428 ds->drv->poll_link(ds);
431 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
434 static void dsa_link_poll_timer(unsigned long _dst)
436 struct dsa_switch_tree *dst = (void *)_dst;
438 schedule_work(&dst->link_poll_work);
442 /* platform driver init and cleanup *****************************************/
443 static int dev_is_class(struct device *dev, void *class)
445 if (dev->class != NULL && !strcmp(dev->class->name, class))
451 static struct device *dev_find_class(struct device *parent, char *class)
453 if (dev_is_class(parent, class)) {
458 return device_find_child(parent, class, dev_is_class);
461 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
465 d = dev_find_class(dev, "mdio_bus");
477 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
479 static struct net_device *dev_to_net_device(struct device *dev)
483 d = dev_find_class(dev, "net");
485 struct net_device *nd;
498 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
499 struct dsa_chip_data *cd,
501 struct device_node *link)
507 struct device_node *parent_sw;
510 parent_sw = of_get_parent(link);
514 reg = of_get_property(parent_sw, "reg", &len);
515 if (!reg || (len != sizeof(*reg) * 2))
518 link_sw_addr = be32_to_cpup(reg + 1);
520 if (link_sw_addr >= pd->nr_chips)
523 /* First time routing table allocation */
525 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
530 /* default to no valid uplink/downlink */
531 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
534 reg = of_get_property(link, "reg", NULL);
540 link_port_addr = be32_to_cpup(reg);
542 cd->rtable[link_sw_addr] = link_port_addr;
550 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
555 for (i = 0; i < pd->nr_chips; i++) {
557 while (port_index < DSA_MAX_PORTS) {
558 kfree(pd->chip[i].port_names[port_index]);
561 kfree(pd->chip[i].rtable);
566 static int dsa_of_probe(struct device *dev)
568 struct device_node *np = dev->of_node;
569 struct device_node *child, *mdio, *ethernet, *port, *link;
570 struct mii_bus *mdio_bus;
571 struct platform_device *ethernet_dev;
572 struct dsa_platform_data *pd;
573 struct dsa_chip_data *cd;
574 const char *port_name;
575 int chip_index, port_index;
576 const unsigned int *sw_addr, *port_reg;
580 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
584 mdio_bus = of_mdio_find_bus(mdio);
586 return -EPROBE_DEFER;
588 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
592 ethernet_dev = of_find_device_by_node(ethernet);
594 return -EPROBE_DEFER;
596 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
600 dev->platform_data = pd;
601 pd->netdev = ðernet_dev->dev;
602 pd->nr_chips = of_get_available_child_count(np);
603 if (pd->nr_chips > DSA_MAX_SWITCHES)
604 pd->nr_chips = DSA_MAX_SWITCHES;
606 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
614 for_each_available_child_of_node(np, child) {
616 cd = &pd->chip[chip_index];
619 cd->host_dev = &mdio_bus->dev;
621 sw_addr = of_get_property(child, "reg", NULL);
625 cd->sw_addr = be32_to_cpup(sw_addr);
626 if (cd->sw_addr > PHY_MAX_ADDR)
629 if (!of_property_read_u32(np, "eeprom-length", &eeprom_len))
630 cd->eeprom_len = eeprom_len;
632 for_each_available_child_of_node(child, port) {
633 port_reg = of_get_property(port, "reg", NULL);
637 port_index = be32_to_cpup(port_reg);
639 port_name = of_get_property(port, "label", NULL);
643 cd->port_dn[port_index] = port;
645 cd->port_names[port_index] = kstrdup(port_name,
647 if (!cd->port_names[port_index]) {
652 link = of_parse_phandle(port, "link", 0);
654 if (!strcmp(port_name, "dsa") && link &&
656 ret = dsa_of_setup_routing_table(pd, cd,
662 if (port_index == DSA_MAX_PORTS)
670 dsa_of_free_platform_data(pd);
673 dev->platform_data = NULL;
677 static void dsa_of_remove(struct device *dev)
679 struct dsa_platform_data *pd = dev->platform_data;
684 dsa_of_free_platform_data(pd);
688 static inline int dsa_of_probe(struct device *dev)
693 static inline void dsa_of_remove(struct device *dev)
698 static int dsa_probe(struct platform_device *pdev)
700 struct dsa_platform_data *pd = pdev->dev.platform_data;
701 struct net_device *dev;
702 struct dsa_switch_tree *dst;
705 pr_notice_once("Distributed Switch Architecture driver version %s\n",
708 if (pdev->dev.of_node) {
709 ret = dsa_of_probe(&pdev->dev);
713 pd = pdev->dev.platform_data;
716 if (pd == NULL || pd->netdev == NULL)
719 dev = dev_to_net_device(pd->netdev);
725 if (dev->dsa_ptr != NULL) {
731 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
738 platform_set_drvdata(pdev, dst);
741 dst->master_netdev = dev;
742 dst->cpu_switch = -1;
745 for (i = 0; i < pd->nr_chips; i++) {
746 struct dsa_switch *ds;
748 ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev);
750 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
756 if (ds->drv->poll_link != NULL)
757 dst->link_poll_needed = 1;
761 * If we use a tagging format that doesn't have an ethertype
762 * field, make sure that all packets from this point on get
763 * sent to the tag format's receive function.
766 dev->dsa_ptr = (void *)dst;
768 if (dst->link_poll_needed) {
769 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
770 init_timer(&dst->link_poll_timer);
771 dst->link_poll_timer.data = (unsigned long)dst;
772 dst->link_poll_timer.function = dsa_link_poll_timer;
773 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
774 add_timer(&dst->link_poll_timer);
780 dsa_of_remove(&pdev->dev);
785 static int dsa_remove(struct platform_device *pdev)
787 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
790 if (dst->link_poll_needed)
791 del_timer_sync(&dst->link_poll_timer);
793 flush_work(&dst->link_poll_work);
795 for (i = 0; i < dst->pd->nr_chips; i++) {
796 struct dsa_switch *ds = dst->ds[i];
799 dsa_switch_destroy(ds);
802 dsa_of_remove(&pdev->dev);
807 static void dsa_shutdown(struct platform_device *pdev)
811 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
812 struct packet_type *pt, struct net_device *orig_dev)
814 struct dsa_switch_tree *dst = dev->dsa_ptr;
816 if (unlikely(dst == NULL)) {
821 return dst->rcv(skb, dev, pt, orig_dev);
824 static struct packet_type dsa_pack_type __read_mostly = {
825 .type = cpu_to_be16(ETH_P_XDSA),
826 .func = dsa_switch_rcv,
829 static struct notifier_block dsa_netdevice_nb __read_mostly = {
830 .notifier_call = dsa_slave_netdevice_event,
833 #ifdef CONFIG_PM_SLEEP
834 static int dsa_suspend(struct device *d)
836 struct platform_device *pdev = to_platform_device(d);
837 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
840 for (i = 0; i < dst->pd->nr_chips; i++) {
841 struct dsa_switch *ds = dst->ds[i];
844 ret = dsa_switch_suspend(ds);
850 static int dsa_resume(struct device *d)
852 struct platform_device *pdev = to_platform_device(d);
853 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
856 for (i = 0; i < dst->pd->nr_chips; i++) {
857 struct dsa_switch *ds = dst->ds[i];
860 ret = dsa_switch_resume(ds);
867 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
869 static const struct of_device_id dsa_of_match_table[] = {
870 { .compatible = "brcm,bcm7445-switch-v4.0" },
871 { .compatible = "marvell,dsa", },
874 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
876 static struct platform_driver dsa_driver = {
878 .remove = dsa_remove,
879 .shutdown = dsa_shutdown,
882 .of_match_table = dsa_of_match_table,
887 static int __init dsa_init_module(void)
891 register_netdevice_notifier(&dsa_netdevice_nb);
893 rc = platform_driver_register(&dsa_driver);
897 dev_add_pack(&dsa_pack_type);
901 module_init(dsa_init_module);
903 static void __exit dsa_cleanup_module(void)
905 unregister_netdevice_notifier(&dsa_netdevice_nb);
906 dev_remove_pack(&dsa_pack_type);
907 platform_driver_unregister(&dsa_driver);
909 module_exit(dsa_cleanup_module);
911 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
912 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
913 MODULE_LICENSE("GPL");
914 MODULE_ALIAS("platform:dsa");