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_RW(temp1_max);
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_cpu_dsa_setup(struct dsa_switch *ds, struct net_device *master)
181 struct dsa_chip_data *cd = ds->pd;
182 struct device_node *port_dn;
183 struct phy_device *phydev;
186 for (port = 0; port < DSA_MAX_PORTS; port++) {
187 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
190 port_dn = cd->port_dn[port];
191 if (of_phy_is_fixed_link(port_dn)) {
192 ret = of_phy_register_fixed_link(port_dn);
195 "failed to register fixed PHY\n");
198 phydev = of_phy_find_device(port_dn);
200 mode = of_get_phy_mode(port_dn);
202 mode = PHY_INTERFACE_MODE_NA;
203 phydev->interface = mode;
205 genphy_config_init(phydev);
206 genphy_read_status(phydev);
207 if (ds->drv->adjust_link)
208 ds->drv->adjust_link(ds, port, phydev);
214 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
216 struct dsa_switch_driver *drv = ds->drv;
217 struct dsa_switch_tree *dst = ds->dst;
218 struct dsa_chip_data *pd = ds->pd;
219 bool valid_name_found = false;
220 int index = ds->index;
224 * Validate supplied switch configuration.
226 for (i = 0; i < DSA_MAX_PORTS; i++) {
229 name = pd->port_names[i];
233 if (!strcmp(name, "cpu")) {
234 if (dst->cpu_switch != -1) {
235 netdev_err(dst->master_netdev,
236 "multiple cpu ports?!\n");
240 dst->cpu_switch = index;
242 } else if (!strcmp(name, "dsa")) {
243 ds->dsa_port_mask |= 1 << i;
245 ds->phys_port_mask |= 1 << i;
247 valid_name_found = true;
250 if (!valid_name_found && i == DSA_MAX_PORTS) {
255 /* Make the built-in MII bus mask match the number of ports,
256 * switch drivers can override this later
258 ds->phys_mii_mask = ds->phys_port_mask;
261 * If the CPU connects to this switch, set the switch tree
262 * tagging protocol to the preferred tagging format of this
265 if (dst->cpu_switch == index) {
266 switch (ds->tag_protocol) {
267 #ifdef CONFIG_NET_DSA_TAG_DSA
268 case DSA_TAG_PROTO_DSA:
269 dst->rcv = dsa_netdev_ops.rcv;
272 #ifdef CONFIG_NET_DSA_TAG_EDSA
273 case DSA_TAG_PROTO_EDSA:
274 dst->rcv = edsa_netdev_ops.rcv;
277 #ifdef CONFIG_NET_DSA_TAG_TRAILER
278 case DSA_TAG_PROTO_TRAILER:
279 dst->rcv = trailer_netdev_ops.rcv;
282 #ifdef CONFIG_NET_DSA_TAG_BRCM
283 case DSA_TAG_PROTO_BRCM:
284 dst->rcv = brcm_netdev_ops.rcv;
287 case DSA_TAG_PROTO_NONE:
294 dst->tag_protocol = ds->tag_protocol;
298 * Do basic register setup.
300 ret = drv->setup(ds);
304 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
308 ds->slave_mii_bus = mdiobus_alloc();
309 if (ds->slave_mii_bus == NULL) {
313 dsa_slave_mii_bus_init(ds);
315 ret = mdiobus_register(ds->slave_mii_bus);
321 * Create network devices for physical switch ports.
323 for (i = 0; i < DSA_MAX_PORTS; i++) {
324 if (!(ds->phys_port_mask & (1 << i)))
327 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
329 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
330 index, i, pd->port_names[i]);
335 /* Perform configuration of the CPU and DSA ports */
336 ret = dsa_cpu_dsa_setup(ds, dst->master_netdev);
338 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
343 #ifdef CONFIG_NET_DSA_HWMON
344 /* If the switch provides a temperature sensor,
345 * register with hardware monitoring subsystem.
346 * Treat registration error as non-fatal and ignore it.
349 const char *netname = netdev_name(dst->master_netdev);
350 char hname[IFNAMSIZ + 1];
353 /* Create valid hwmon 'name' attribute */
354 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
355 if (isalnum(netname[i]))
356 hname[j++] = netname[i];
359 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
361 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
362 ds->hwmon_name, ds, dsa_hwmon_groups);
363 if (IS_ERR(ds->hwmon_dev))
364 ds->hwmon_dev = NULL;
366 #endif /* CONFIG_NET_DSA_HWMON */
371 mdiobus_free(ds->slave_mii_bus);
377 static struct dsa_switch *
378 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
379 struct device *parent, struct device *host_dev)
381 struct dsa_chip_data *pd = dst->pd->chip + index;
382 struct dsa_switch_driver *drv;
383 struct dsa_switch *ds;
388 * Probe for switch model.
390 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
392 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
394 return ERR_PTR(-EINVAL);
396 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
401 * Allocate and initialise switch state.
403 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
405 return ERR_PTR(-ENOMEM);
411 ds->tag_protocol = drv->tag_protocol;
412 ds->master_dev = host_dev;
414 ret = dsa_switch_setup_one(ds, parent);
421 static void dsa_switch_destroy(struct dsa_switch *ds)
423 #ifdef CONFIG_NET_DSA_HWMON
425 hwmon_device_unregister(ds->hwmon_dev);
427 mdiobus_unregister(ds->slave_mii_bus);
428 mdiobus_free(ds->slave_mii_bus);
431 #ifdef CONFIG_PM_SLEEP
432 static int dsa_switch_suspend(struct dsa_switch *ds)
436 /* Suspend slave network devices */
437 for (i = 0; i < DSA_MAX_PORTS; i++) {
438 if (!dsa_is_port_initialized(ds, i))
441 ret = dsa_slave_suspend(ds->ports[i]);
446 if (ds->drv->suspend)
447 ret = ds->drv->suspend(ds);
452 static int dsa_switch_resume(struct dsa_switch *ds)
457 ret = ds->drv->resume(ds);
462 /* Resume slave network devices */
463 for (i = 0; i < DSA_MAX_PORTS; i++) {
464 if (!dsa_is_port_initialized(ds, i))
467 ret = dsa_slave_resume(ds->ports[i]);
477 /* link polling *************************************************************/
478 static void dsa_link_poll_work(struct work_struct *ugly)
480 struct dsa_switch_tree *dst;
483 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
485 for (i = 0; i < dst->pd->nr_chips; i++) {
486 struct dsa_switch *ds = dst->ds[i];
488 if (ds != NULL && ds->drv->poll_link != NULL)
489 ds->drv->poll_link(ds);
492 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
495 static void dsa_link_poll_timer(unsigned long _dst)
497 struct dsa_switch_tree *dst = (void *)_dst;
499 schedule_work(&dst->link_poll_work);
503 /* platform driver init and cleanup *****************************************/
504 static int dev_is_class(struct device *dev, void *class)
506 if (dev->class != NULL && !strcmp(dev->class->name, class))
512 static struct device *dev_find_class(struct device *parent, char *class)
514 if (dev_is_class(parent, class)) {
519 return device_find_child(parent, class, dev_is_class);
522 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
526 d = dev_find_class(dev, "mdio_bus");
538 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
540 static struct net_device *dev_to_net_device(struct device *dev)
544 d = dev_find_class(dev, "net");
546 struct net_device *nd;
559 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
560 struct dsa_chip_data *cd,
561 int chip_index, int port_index,
562 struct device_node *link)
566 struct device_node *parent_sw;
569 parent_sw = of_get_parent(link);
573 reg = of_get_property(parent_sw, "reg", &len);
574 if (!reg || (len != sizeof(*reg) * 2))
578 * Get the destination switch number from the second field of its 'reg'
579 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
581 link_sw_addr = be32_to_cpup(reg + 1);
583 if (link_sw_addr >= pd->nr_chips)
586 /* First time routing table allocation */
588 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
593 /* default to no valid uplink/downlink */
594 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
597 cd->rtable[link_sw_addr] = port_index;
602 static int dsa_of_probe_links(struct dsa_platform_data *pd,
603 struct dsa_chip_data *cd,
604 int chip_index, int port_index,
605 struct device_node *port,
606 const char *port_name)
608 struct device_node *link;
612 for (link_index = 0;; link_index++) {
613 link = of_parse_phandle(port, "link", link_index);
617 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
618 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
627 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
632 for (i = 0; i < pd->nr_chips; i++) {
634 while (port_index < DSA_MAX_PORTS) {
635 kfree(pd->chip[i].port_names[port_index]);
638 kfree(pd->chip[i].rtable);
640 /* Drop our reference to the MDIO bus device */
641 if (pd->chip[i].host_dev)
642 put_device(pd->chip[i].host_dev);
647 static int dsa_of_probe(struct device *dev)
649 struct device_node *np = dev->of_node;
650 struct device_node *child, *mdio, *ethernet, *port;
651 struct mii_bus *mdio_bus, *mdio_bus_switch;
652 struct net_device *ethernet_dev;
653 struct dsa_platform_data *pd;
654 struct dsa_chip_data *cd;
655 const char *port_name;
656 int chip_index, port_index;
657 const unsigned int *sw_addr, *port_reg;
661 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
665 mdio_bus = of_mdio_find_bus(mdio);
667 return -EPROBE_DEFER;
669 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
675 ethernet_dev = of_find_net_device_by_node(ethernet);
681 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
684 goto out_put_ethernet;
687 dev->platform_data = pd;
688 pd->of_netdev = ethernet_dev;
689 pd->nr_chips = of_get_available_child_count(np);
690 if (pd->nr_chips > DSA_MAX_SWITCHES)
691 pd->nr_chips = DSA_MAX_SWITCHES;
693 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
701 for_each_available_child_of_node(np, child) {
703 cd = &pd->chip[chip_index];
707 /* When assigning the host device, increment its refcount */
708 cd->host_dev = get_device(&mdio_bus->dev);
710 sw_addr = of_get_property(child, "reg", NULL);
714 cd->sw_addr = be32_to_cpup(sw_addr);
715 if (cd->sw_addr >= PHY_MAX_ADDR)
718 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
719 cd->eeprom_len = eeprom_len;
721 mdio = of_parse_phandle(child, "mii-bus", 0);
723 mdio_bus_switch = of_mdio_find_bus(mdio);
724 if (!mdio_bus_switch) {
729 /* Drop the mdio_bus device ref, replacing the host
730 * device with the mdio_bus_switch device, keeping
731 * the refcount from of_mdio_find_bus() above.
733 put_device(cd->host_dev);
734 cd->host_dev = &mdio_bus_switch->dev;
737 for_each_available_child_of_node(child, port) {
738 port_reg = of_get_property(port, "reg", NULL);
742 port_index = be32_to_cpup(port_reg);
743 if (port_index >= DSA_MAX_PORTS)
746 port_name = of_get_property(port, "label", NULL);
750 cd->port_dn[port_index] = port;
752 cd->port_names[port_index] = kstrdup(port_name,
754 if (!cd->port_names[port_index]) {
759 ret = dsa_of_probe_links(pd, cd, chip_index,
760 port_index, port, port_name);
767 /* The individual chips hold their own refcount on the mdio bus,
769 put_device(&mdio_bus->dev);
774 dsa_of_free_platform_data(pd);
777 dev->platform_data = NULL;
779 put_device(ðernet_dev->dev);
781 put_device(&mdio_bus->dev);
785 static void dsa_of_remove(struct device *dev)
787 struct dsa_platform_data *pd = dev->platform_data;
792 dsa_of_free_platform_data(pd);
793 put_device(&pd->of_netdev->dev);
797 static inline int dsa_of_probe(struct device *dev)
802 static inline void dsa_of_remove(struct device *dev)
807 static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
808 struct device *parent, struct dsa_platform_data *pd)
813 dst->master_netdev = dev;
814 dst->cpu_switch = -1;
817 for (i = 0; i < pd->nr_chips; i++) {
818 struct dsa_switch *ds;
820 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
822 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
828 if (ds->drv->poll_link != NULL)
829 dst->link_poll_needed = 1;
833 * If we use a tagging format that doesn't have an ethertype
834 * field, make sure that all packets from this point on get
835 * sent to the tag format's receive function.
838 dev->dsa_ptr = (void *)dst;
840 if (dst->link_poll_needed) {
841 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
842 init_timer(&dst->link_poll_timer);
843 dst->link_poll_timer.data = (unsigned long)dst;
844 dst->link_poll_timer.function = dsa_link_poll_timer;
845 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
846 add_timer(&dst->link_poll_timer);
850 static int dsa_probe(struct platform_device *pdev)
852 struct dsa_platform_data *pd = pdev->dev.platform_data;
853 struct net_device *dev;
854 struct dsa_switch_tree *dst;
857 pr_notice_once("Distributed Switch Architecture driver version %s\n",
860 if (pdev->dev.of_node) {
861 ret = dsa_of_probe(&pdev->dev);
865 pd = pdev->dev.platform_data;
868 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
875 dev = dev_to_net_device(pd->netdev);
882 if (dev->dsa_ptr != NULL) {
888 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
895 platform_set_drvdata(pdev, dst);
897 dsa_setup_dst(dst, dev, &pdev->dev, pd);
902 dsa_of_remove(&pdev->dev);
907 static void dsa_remove_dst(struct dsa_switch_tree *dst)
911 if (dst->link_poll_needed)
912 del_timer_sync(&dst->link_poll_timer);
914 flush_work(&dst->link_poll_work);
916 for (i = 0; i < dst->pd->nr_chips; i++) {
917 struct dsa_switch *ds = dst->ds[i];
920 dsa_switch_destroy(ds);
926 static int dsa_remove(struct platform_device *pdev)
928 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
932 dsa_of_remove(&pdev->dev);
937 static void dsa_shutdown(struct platform_device *pdev)
941 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
942 struct packet_type *pt, struct net_device *orig_dev)
944 struct dsa_switch_tree *dst = dev->dsa_ptr;
946 if (unlikely(dst == NULL)) {
951 return dst->rcv(skb, dev, pt, orig_dev);
954 static struct packet_type dsa_pack_type __read_mostly = {
955 .type = cpu_to_be16(ETH_P_XDSA),
956 .func = dsa_switch_rcv,
959 static struct notifier_block dsa_netdevice_nb __read_mostly = {
960 .notifier_call = dsa_slave_netdevice_event,
963 #ifdef CONFIG_PM_SLEEP
964 static int dsa_suspend(struct device *d)
966 struct platform_device *pdev = to_platform_device(d);
967 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
970 for (i = 0; i < dst->pd->nr_chips; i++) {
971 struct dsa_switch *ds = dst->ds[i];
974 ret = dsa_switch_suspend(ds);
980 static int dsa_resume(struct device *d)
982 struct platform_device *pdev = to_platform_device(d);
983 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
986 for (i = 0; i < dst->pd->nr_chips; i++) {
987 struct dsa_switch *ds = dst->ds[i];
990 ret = dsa_switch_resume(ds);
997 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
999 static const struct of_device_id dsa_of_match_table[] = {
1000 { .compatible = "brcm,bcm7445-switch-v4.0" },
1001 { .compatible = "marvell,dsa", },
1004 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
1006 static struct platform_driver dsa_driver = {
1008 .remove = dsa_remove,
1009 .shutdown = dsa_shutdown,
1012 .of_match_table = dsa_of_match_table,
1017 static int __init dsa_init_module(void)
1021 register_netdevice_notifier(&dsa_netdevice_nb);
1023 rc = platform_driver_register(&dsa_driver);
1027 dev_add_pack(&dsa_pack_type);
1031 module_init(dsa_init_module);
1033 static void __exit dsa_cleanup_module(void)
1035 unregister_netdevice_notifier(&dsa_netdevice_nb);
1036 dev_remove_pack(&dsa_pack_type);
1037 platform_driver_unregister(&dsa_driver);
1039 module_exit(dsa_cleanup_module);
1041 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
1042 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1043 MODULE_LICENSE("GPL");
1044 MODULE_ALIAS("platform:dsa");