2 * net/dsa/legacy.c - Hardware switch handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
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, struct device *dev)
83 struct dsa_port *dport;
86 for (port = 0; port < ds->num_ports; port++) {
87 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
90 dport = &ds->ports[port];
91 ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
98 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
100 const struct dsa_switch_ops *ops = ds->ops;
101 struct dsa_switch_tree *dst = ds->dst;
102 struct dsa_chip_data *cd = ds->cd;
103 bool valid_name_found = false;
104 int index = ds->index;
108 * Validate supplied switch configuration.
110 for (i = 0; i < ds->num_ports; i++) {
113 name = cd->port_names[i];
117 if (!strcmp(name, "cpu")) {
118 if (dst->cpu_switch) {
119 netdev_err(dst->master_netdev,
120 "multiple cpu ports?!\n");
123 dst->cpu_switch = ds;
125 ds->cpu_port_mask |= 1 << i;
126 } else if (!strcmp(name, "dsa")) {
127 ds->dsa_port_mask |= 1 << i;
129 ds->enabled_port_mask |= 1 << i;
131 valid_name_found = true;
134 if (!valid_name_found && i == ds->num_ports)
137 /* Make the built-in MII bus mask match the number of ports,
138 * switch drivers can override this later
140 ds->phys_mii_mask = ds->enabled_port_mask;
143 * If the CPU connects to this switch, set the switch tree
144 * tagging protocol to the preferred tagging format of this
147 if (dst->cpu_switch == ds) {
148 enum dsa_tag_protocol tag_protocol;
150 tag_protocol = ops->get_tag_protocol(ds);
151 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
152 if (IS_ERR(dst->tag_ops))
153 return PTR_ERR(dst->tag_ops);
155 dst->rcv = dst->tag_ops->rcv;
158 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
161 * Do basic register setup.
163 ret = ops->setup(ds);
167 ret = dsa_switch_register_notifier(ds);
172 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
177 if (!ds->slave_mii_bus && ops->phy_read) {
178 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
179 if (!ds->slave_mii_bus)
181 dsa_slave_mii_bus_init(ds);
183 ret = mdiobus_register(ds->slave_mii_bus);
189 * Create network devices for physical switch ports.
191 for (i = 0; i < ds->num_ports; i++) {
192 ds->ports[i].dn = cd->port_dn[i];
194 if (!(ds->enabled_port_mask & (1 << i)))
197 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
199 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
200 index, i, cd->port_names[i], ret);
203 /* Perform configuration of the CPU and DSA ports */
204 ret = dsa_cpu_dsa_setups(ds, parent);
206 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
209 ret = dsa_cpu_port_ethtool_setup(ds);
216 static struct dsa_switch *
217 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
218 struct device *parent, struct device *host_dev)
220 struct dsa_chip_data *cd = dst->pd->chip + index;
221 const struct dsa_switch_ops *ops;
222 struct dsa_switch *ds;
228 * Probe for switch model.
230 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
232 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
234 return ERR_PTR(-EINVAL);
236 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
241 * Allocate and initialise switch state.
243 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
245 return ERR_PTR(-ENOMEM);
253 ret = dsa_switch_setup_one(ds, parent);
260 static void dsa_switch_destroy(struct dsa_switch *ds)
264 /* Destroy network devices for physical switch ports. */
265 for (port = 0; port < ds->num_ports; port++) {
266 if (!(ds->enabled_port_mask & (1 << port)))
269 if (!ds->ports[port].netdev)
272 dsa_slave_destroy(ds->ports[port].netdev);
275 /* Disable configuration of the CPU and DSA ports */
276 for (port = 0; port < ds->num_ports; port++) {
277 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
279 dsa_cpu_dsa_destroy(&ds->ports[port]);
281 /* Clearing a bit which is not set does no harm */
282 ds->cpu_port_mask |= ~(1 << port);
283 ds->dsa_port_mask |= ~(1 << port);
286 if (ds->slave_mii_bus && ds->ops->phy_read)
287 mdiobus_unregister(ds->slave_mii_bus);
289 dsa_switch_unregister_notifier(ds);
292 /* platform driver init and cleanup *****************************************/
293 static int dev_is_class(struct device *dev, void *class)
295 if (dev->class != NULL && !strcmp(dev->class->name, class))
301 static struct device *dev_find_class(struct device *parent, char *class)
303 if (dev_is_class(parent, class)) {
308 return device_find_child(parent, class, dev_is_class);
311 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
315 d = dev_find_class(dev, "mdio_bus");
327 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
330 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
331 struct dsa_chip_data *cd,
332 int chip_index, int port_index,
333 struct device_node *link)
337 struct device_node *parent_sw;
340 parent_sw = of_get_parent(link);
344 reg = of_get_property(parent_sw, "reg", &len);
345 if (!reg || (len != sizeof(*reg) * 2))
349 * Get the destination switch number from the second field of its 'reg'
350 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
352 link_sw_addr = be32_to_cpup(reg + 1);
354 if (link_sw_addr >= pd->nr_chips)
357 cd->rtable[link_sw_addr] = port_index;
362 static int dsa_of_probe_links(struct dsa_platform_data *pd,
363 struct dsa_chip_data *cd,
364 int chip_index, int port_index,
365 struct device_node *port,
366 const char *port_name)
368 struct device_node *link;
372 for (link_index = 0;; link_index++) {
373 link = of_parse_phandle(port, "link", link_index);
377 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
378 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
387 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
392 for (i = 0; i < pd->nr_chips; i++) {
394 while (port_index < DSA_MAX_PORTS) {
395 kfree(pd->chip[i].port_names[port_index]);
399 /* Drop our reference to the MDIO bus device */
400 if (pd->chip[i].host_dev)
401 put_device(pd->chip[i].host_dev);
406 static int dsa_of_probe(struct device *dev)
408 struct device_node *np = dev->of_node;
409 struct device_node *child, *mdio, *ethernet, *port;
410 struct mii_bus *mdio_bus, *mdio_bus_switch;
411 struct net_device *ethernet_dev;
412 struct dsa_platform_data *pd;
413 struct dsa_chip_data *cd;
414 const char *port_name;
415 int chip_index, port_index;
416 const unsigned int *sw_addr, *port_reg;
420 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
424 mdio_bus = of_mdio_find_bus(mdio);
426 return -EPROBE_DEFER;
428 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
434 ethernet_dev = of_find_net_device_by_node(ethernet);
440 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
443 goto out_put_ethernet;
446 dev->platform_data = pd;
447 pd->of_netdev = ethernet_dev;
448 pd->nr_chips = of_get_available_child_count(np);
449 if (pd->nr_chips > DSA_MAX_SWITCHES)
450 pd->nr_chips = DSA_MAX_SWITCHES;
452 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
460 for_each_available_child_of_node(np, child) {
464 cd = &pd->chip[chip_index];
468 /* Initialize the routing table */
469 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
470 cd->rtable[i] = DSA_RTABLE_NONE;
472 /* When assigning the host device, increment its refcount */
473 cd->host_dev = get_device(&mdio_bus->dev);
475 sw_addr = of_get_property(child, "reg", NULL);
479 cd->sw_addr = be32_to_cpup(sw_addr);
480 if (cd->sw_addr >= PHY_MAX_ADDR)
483 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
484 cd->eeprom_len = eeprom_len;
486 mdio = of_parse_phandle(child, "mii-bus", 0);
488 mdio_bus_switch = of_mdio_find_bus(mdio);
489 if (!mdio_bus_switch) {
494 /* Drop the mdio_bus device ref, replacing the host
495 * device with the mdio_bus_switch device, keeping
496 * the refcount from of_mdio_find_bus() above.
498 put_device(cd->host_dev);
499 cd->host_dev = &mdio_bus_switch->dev;
502 for_each_available_child_of_node(child, port) {
503 port_reg = of_get_property(port, "reg", NULL);
507 port_index = be32_to_cpup(port_reg);
508 if (port_index >= DSA_MAX_PORTS)
511 port_name = of_get_property(port, "label", NULL);
515 cd->port_dn[port_index] = port;
517 cd->port_names[port_index] = kstrdup(port_name,
519 if (!cd->port_names[port_index]) {
524 ret = dsa_of_probe_links(pd, cd, chip_index,
525 port_index, port, port_name);
532 /* The individual chips hold their own refcount on the mdio bus,
534 put_device(&mdio_bus->dev);
539 dsa_of_free_platform_data(pd);
542 dev->platform_data = NULL;
544 put_device(ðernet_dev->dev);
546 put_device(&mdio_bus->dev);
550 static void dsa_of_remove(struct device *dev)
552 struct dsa_platform_data *pd = dev->platform_data;
557 dsa_of_free_platform_data(pd);
558 put_device(&pd->of_netdev->dev);
562 static inline int dsa_of_probe(struct device *dev)
567 static inline void dsa_of_remove(struct device *dev)
572 static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
573 struct device *parent, struct dsa_platform_data *pd)
576 unsigned configured = 0;
579 dst->master_netdev = dev;
582 for (i = 0; i < pd->nr_chips; i++) {
583 struct dsa_switch *ds;
585 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
587 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
598 * If no switch was found, exit cleanly
601 return -EPROBE_DEFER;
604 * If we use a tagging format that doesn't have an ethertype
605 * field, make sure that all packets from this point on get
606 * sent to the tag format's receive function.
609 dev->dsa_ptr = (void *)dst;
614 static int dsa_probe(struct platform_device *pdev)
616 struct dsa_platform_data *pd = pdev->dev.platform_data;
617 struct net_device *dev;
618 struct dsa_switch_tree *dst;
621 if (pdev->dev.of_node) {
622 ret = dsa_of_probe(&pdev->dev);
626 pd = pdev->dev.platform_data;
629 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
636 dev = dsa_dev_to_net_device(pd->netdev);
643 if (dev->dsa_ptr != NULL) {
649 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
656 platform_set_drvdata(pdev, dst);
658 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
667 dsa_of_remove(&pdev->dev);
672 static void dsa_remove_dst(struct dsa_switch_tree *dst)
676 dst->master_netdev->dsa_ptr = NULL;
678 /* If we used a tagging format that doesn't have an ethertype
679 * field, make sure that all packets from this point get sent
680 * without the tag and go through the regular receive path.
684 for (i = 0; i < dst->pd->nr_chips; i++) {
685 struct dsa_switch *ds = dst->ds[i];
688 dsa_switch_destroy(ds);
691 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
693 dev_put(dst->master_netdev);
696 static int dsa_remove(struct platform_device *pdev)
698 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
701 dsa_of_remove(&pdev->dev);
706 static void dsa_shutdown(struct platform_device *pdev)
710 #ifdef CONFIG_PM_SLEEP
711 static int dsa_suspend(struct device *d)
713 struct platform_device *pdev = to_platform_device(d);
714 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
717 for (i = 0; i < dst->pd->nr_chips; i++) {
718 struct dsa_switch *ds = dst->ds[i];
721 ret = dsa_switch_suspend(ds);
727 static int dsa_resume(struct device *d)
729 struct platform_device *pdev = to_platform_device(d);
730 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
733 for (i = 0; i < dst->pd->nr_chips; i++) {
734 struct dsa_switch *ds = dst->ds[i];
737 ret = dsa_switch_resume(ds);
744 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
746 static const struct of_device_id dsa_of_match_table[] = {
747 { .compatible = "marvell,dsa", },
750 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
752 static struct platform_driver dsa_driver = {
754 .remove = dsa_remove,
755 .shutdown = dsa_shutdown,
758 .of_match_table = dsa_of_match_table,
763 int dsa_legacy_register(void)
765 return platform_driver_register(&dsa_driver);
768 void dsa_legacy_unregister(void)
770 platform_driver_unregister(&dsa_driver);