2 * net/dsa/legacy.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/netdevice.h>
22 #include <linux/sysfs.h>
23 #include <linux/phy_fixed.h>
24 #include <linux/etherdevice.h>
28 /* switch driver registration ***********************************************/
29 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
30 static LIST_HEAD(dsa_switch_drivers);
32 void register_switch_driver(struct dsa_switch_driver *drv)
34 mutex_lock(&dsa_switch_drivers_mutex);
35 list_add_tail(&drv->list, &dsa_switch_drivers);
36 mutex_unlock(&dsa_switch_drivers_mutex);
38 EXPORT_SYMBOL_GPL(register_switch_driver);
40 void unregister_switch_driver(struct dsa_switch_driver *drv)
42 mutex_lock(&dsa_switch_drivers_mutex);
43 list_del_init(&drv->list);
44 mutex_unlock(&dsa_switch_drivers_mutex);
46 EXPORT_SYMBOL_GPL(unregister_switch_driver);
48 static const struct dsa_switch_ops *
49 dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
50 const char **_name, void **priv)
52 const struct dsa_switch_ops *ret;
53 struct list_head *list;
59 mutex_lock(&dsa_switch_drivers_mutex);
60 list_for_each(list, &dsa_switch_drivers) {
61 const struct dsa_switch_ops *ops;
62 struct dsa_switch_driver *drv;
64 drv = list_entry(list, struct dsa_switch_driver, list);
67 name = ops->probe(parent, host_dev, sw_addr, priv);
73 mutex_unlock(&dsa_switch_drivers_mutex);
80 /* basic switch operations **************************************************/
81 static int dsa_cpu_dsa_setups(struct dsa_switch *ds)
85 for (port = 0; port < ds->num_ports; port++) {
86 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
89 ret = dsa_port_link_register_of(&ds->ports[port]);
96 static int dsa_switch_setup_one(struct dsa_switch *ds,
97 struct net_device *master)
99 const struct dsa_switch_ops *ops = ds->ops;
100 struct dsa_switch_tree *dst = ds->dst;
101 struct dsa_chip_data *cd = ds->cd;
102 bool valid_name_found = false;
103 int index = ds->index;
108 * Validate supplied switch configuration.
110 for (i = 0; i < ds->num_ports; i++) {
115 name = cd->port_names[i];
120 if (!strcmp(name, "cpu")) {
123 "multiple cpu ports?!\n");
126 dst->cpu_dp = &ds->ports[i];
127 dst->cpu_dp->master = master;
128 dp->type = DSA_PORT_TYPE_CPU;
129 } else if (!strcmp(name, "dsa")) {
130 dp->type = DSA_PORT_TYPE_DSA;
132 dp->type = DSA_PORT_TYPE_USER;
134 valid_name_found = true;
137 if (!valid_name_found && i == ds->num_ports)
140 /* Make the built-in MII bus mask match the number of ports,
141 * switch drivers can override this later
143 ds->phys_mii_mask |= dsa_user_ports(ds);
146 * If the CPU connects to this switch, set the switch tree
147 * tagging protocol to the preferred tagging format of this
150 if (dst->cpu_dp->ds == ds) {
151 const struct dsa_device_ops *tag_ops;
152 enum dsa_tag_protocol tag_protocol;
154 tag_protocol = ops->get_tag_protocol(ds, dst->cpu_dp->index);
155 tag_ops = dsa_tag_driver_get(tag_protocol);
157 return PTR_ERR(tag_ops);
159 dst->cpu_dp->tag_ops = tag_ops;
161 /* Few copies for faster access in master receive hot path */
162 dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv;
163 dst->cpu_dp->dst = dst;
166 dsa_tag_driver_put(dst->cpu_dp->tag_ops);
168 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
171 * Do basic register setup.
173 ret = ops->setup(ds);
177 ret = dsa_switch_register_notifier(ds);
181 if (!ds->slave_mii_bus && ops->phy_read) {
182 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
183 if (!ds->slave_mii_bus)
185 dsa_slave_mii_bus_init(ds);
187 ret = mdiobus_register(ds->slave_mii_bus);
193 * Create network devices for physical switch ports.
195 for (i = 0; i < ds->num_ports; i++) {
196 ds->ports[i].dn = cd->port_dn[i];
197 ds->ports[i].cpu_dp = dst->cpu_dp;
199 if (!dsa_is_user_port(ds, i))
202 ret = dsa_slave_create(&ds->ports[i]);
204 netdev_err(master, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
205 index, i, cd->port_names[i], ret);
208 /* Perform configuration of the CPU and DSA ports */
209 ret = dsa_cpu_dsa_setups(ds);
211 netdev_err(master, "[%d] : can't configure CPU and DSA ports\n",
217 static struct dsa_switch *
218 dsa_switch_setup(struct dsa_switch_tree *dst, struct net_device *master,
219 int index, struct device *parent, struct device *host_dev)
221 struct dsa_chip_data *cd = dst->pd->chip + index;
222 const struct dsa_switch_ops *ops;
223 struct dsa_switch *ds;
229 * Probe for switch model.
231 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
233 netdev_err(master, "[%d]: could not detect attached switch\n",
235 return ERR_PTR(-EINVAL);
237 netdev_info(master, "[%d]: detected a %s switch\n",
242 * Allocate and initialise switch state.
244 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
246 return ERR_PTR(-ENOMEM);
254 ret = dsa_switch_setup_one(ds, master);
261 static void dsa_switch_destroy(struct dsa_switch *ds)
265 /* Destroy network devices for physical switch ports. */
266 for (port = 0; port < ds->num_ports; port++) {
267 if (!dsa_is_user_port(ds, port))
270 if (!ds->ports[port].slave)
273 dsa_slave_destroy(ds->ports[port].slave);
276 /* Disable configuration of the CPU and DSA ports */
277 for (port = 0; port < ds->num_ports; port++) {
278 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
280 dsa_port_link_unregister_of(&ds->ports[port]);
283 if (ds->slave_mii_bus && ds->ops->phy_read)
284 mdiobus_unregister(ds->slave_mii_bus);
286 dsa_switch_unregister_notifier(ds);
289 /* platform driver init and cleanup *****************************************/
290 static int dev_is_class(struct device *dev, void *class)
292 if (dev->class != NULL && !strcmp(dev->class->name, class))
298 static struct device *dev_find_class(struct device *parent, char *class)
300 if (dev_is_class(parent, class)) {
305 return device_find_child(parent, class, dev_is_class);
308 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
312 d = dev_find_class(dev, "mdio_bus");
324 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
327 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
328 struct dsa_chip_data *cd,
329 int chip_index, int port_index,
330 struct device_node *link)
334 struct device_node *parent_sw;
337 parent_sw = of_get_parent(link);
341 reg = of_get_property(parent_sw, "reg", &len);
342 if (!reg || (len != sizeof(*reg) * 2))
346 * Get the destination switch number from the second field of its 'reg'
347 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
349 link_sw_addr = be32_to_cpup(reg + 1);
351 if (link_sw_addr >= pd->nr_chips)
354 cd->rtable[link_sw_addr] = port_index;
359 static int dsa_of_probe_links(struct dsa_platform_data *pd,
360 struct dsa_chip_data *cd,
361 int chip_index, int port_index,
362 struct device_node *port,
363 const char *port_name)
365 struct device_node *link;
369 for (link_index = 0;; link_index++) {
370 link = of_parse_phandle(port, "link", link_index);
374 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
375 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
384 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
389 for (i = 0; i < pd->nr_chips; i++) {
391 while (port_index < DSA_MAX_PORTS) {
392 kfree(pd->chip[i].port_names[port_index]);
396 /* Drop our reference to the MDIO bus device */
397 put_device(pd->chip[i].host_dev);
402 static int dsa_of_probe(struct device *dev)
404 struct device_node *np = dev->of_node;
405 struct device_node *child, *mdio, *ethernet, *port;
406 struct mii_bus *mdio_bus, *mdio_bus_switch;
407 struct net_device *ethernet_dev;
408 struct dsa_platform_data *pd;
409 struct dsa_chip_data *cd;
410 const char *port_name;
411 int chip_index, port_index;
412 const unsigned int *sw_addr, *port_reg;
416 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
420 mdio_bus = of_mdio_find_bus(mdio);
422 return -EPROBE_DEFER;
424 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
430 ethernet_dev = of_find_net_device_by_node(ethernet);
436 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
439 goto out_put_ethernet;
442 dev->platform_data = pd;
443 pd->of_netdev = ethernet_dev;
444 pd->nr_chips = of_get_available_child_count(np);
445 if (pd->nr_chips > DSA_MAX_SWITCHES)
446 pd->nr_chips = DSA_MAX_SWITCHES;
448 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
456 for_each_available_child_of_node(np, child) {
460 cd = &pd->chip[chip_index];
464 /* Initialize the routing table */
465 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
466 cd->rtable[i] = DSA_RTABLE_NONE;
468 /* When assigning the host device, increment its refcount */
469 cd->host_dev = get_device(&mdio_bus->dev);
471 sw_addr = of_get_property(child, "reg", NULL);
475 cd->sw_addr = be32_to_cpup(sw_addr);
476 if (cd->sw_addr >= PHY_MAX_ADDR)
479 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
480 cd->eeprom_len = eeprom_len;
482 mdio = of_parse_phandle(child, "mii-bus", 0);
484 mdio_bus_switch = of_mdio_find_bus(mdio);
485 if (!mdio_bus_switch) {
490 /* Drop the mdio_bus device ref, replacing the host
491 * device with the mdio_bus_switch device, keeping
492 * the refcount from of_mdio_find_bus() above.
494 put_device(cd->host_dev);
495 cd->host_dev = &mdio_bus_switch->dev;
498 for_each_available_child_of_node(child, port) {
499 port_reg = of_get_property(port, "reg", NULL);
503 port_index = be32_to_cpup(port_reg);
504 if (port_index >= DSA_MAX_PORTS)
507 port_name = of_get_property(port, "label", NULL);
511 cd->port_dn[port_index] = port;
513 cd->port_names[port_index] = kstrdup(port_name,
515 if (!cd->port_names[port_index]) {
520 ret = dsa_of_probe_links(pd, cd, chip_index,
521 port_index, port, port_name);
528 /* The individual chips hold their own refcount on the mdio bus,
530 put_device(&mdio_bus->dev);
535 dsa_of_free_platform_data(pd);
538 dev->platform_data = NULL;
540 put_device(ðernet_dev->dev);
542 put_device(&mdio_bus->dev);
546 static void dsa_of_remove(struct device *dev)
548 struct dsa_platform_data *pd = dev->platform_data;
553 dsa_of_free_platform_data(pd);
554 put_device(&pd->of_netdev->dev);
558 static inline int dsa_of_probe(struct device *dev)
563 static inline void dsa_of_remove(struct device *dev)
568 static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
569 struct device *parent, struct dsa_platform_data *pd)
572 unsigned configured = 0;
576 for (i = 0; i < pd->nr_chips; i++) {
577 struct dsa_switch *ds;
579 ds = dsa_switch_setup(dst, dev, i, parent, pd->chip[i].host_dev);
581 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
592 * If no switch was found, exit cleanly
595 return -EPROBE_DEFER;
597 return dsa_master_setup(dst->cpu_dp->master, dst->cpu_dp);
600 static int dsa_probe(struct platform_device *pdev)
602 struct dsa_platform_data *pd = pdev->dev.platform_data;
603 struct net_device *dev;
604 struct dsa_switch_tree *dst;
607 if (pdev->dev.of_node) {
608 ret = dsa_of_probe(&pdev->dev);
612 pd = pdev->dev.platform_data;
615 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
622 dev = dsa_dev_to_net_device(pd->netdev);
629 if (dev->dsa_ptr != NULL) {
635 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
642 platform_set_drvdata(pdev, dst);
644 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
653 dsa_of_remove(&pdev->dev);
658 static void dsa_remove_dst(struct dsa_switch_tree *dst)
662 dsa_master_teardown(dst->cpu_dp->master);
664 for (i = 0; i < dst->pd->nr_chips; i++) {
665 struct dsa_switch *ds = dst->ds[i];
668 dsa_switch_destroy(ds);
671 dev_put(dst->cpu_dp->master);
674 static int dsa_remove(struct platform_device *pdev)
676 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
679 dsa_of_remove(&pdev->dev);
684 static void dsa_shutdown(struct platform_device *pdev)
688 #ifdef CONFIG_PM_SLEEP
689 static int dsa_suspend(struct device *d)
691 struct dsa_switch_tree *dst = dev_get_drvdata(d);
694 for (i = 0; i < dst->pd->nr_chips; i++) {
695 struct dsa_switch *ds = dst->ds[i];
698 ret = dsa_switch_suspend(ds);
704 static int dsa_resume(struct device *d)
706 struct dsa_switch_tree *dst = dev_get_drvdata(d);
709 for (i = 0; i < dst->pd->nr_chips; i++) {
710 struct dsa_switch *ds = dst->ds[i];
713 ret = dsa_switch_resume(ds);
720 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
722 static const struct of_device_id dsa_of_match_table[] = {
723 { .compatible = "marvell,dsa", },
726 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
728 static struct platform_driver dsa_driver = {
730 .remove = dsa_remove,
731 .shutdown = dsa_shutdown,
734 .of_match_table = dsa_of_match_table,
739 int dsa_legacy_register(void)
741 return platform_driver_register(&dsa_driver);
744 void dsa_legacy_unregister(void)
746 platform_driver_unregister(&dsa_driver);