2 * net/dsa/dsa.c - Hardware switch handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
19 char dsa_driver_version[] = "0.1";
22 /* switch driver registration ***********************************************/
23 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
24 static LIST_HEAD(dsa_switch_drivers);
26 void register_switch_driver(struct dsa_switch_driver *drv)
28 mutex_lock(&dsa_switch_drivers_mutex);
29 list_add_tail(&drv->list, &dsa_switch_drivers);
30 mutex_unlock(&dsa_switch_drivers_mutex);
33 void unregister_switch_driver(struct dsa_switch_driver *drv)
35 mutex_lock(&dsa_switch_drivers_mutex);
36 list_del_init(&drv->list);
37 mutex_unlock(&dsa_switch_drivers_mutex);
40 static struct dsa_switch_driver *
41 dsa_switch_probe(struct mii_bus *bus, int sw_addr, char **_name)
43 struct dsa_switch_driver *ret;
44 struct list_head *list;
50 mutex_lock(&dsa_switch_drivers_mutex);
51 list_for_each(list, &dsa_switch_drivers) {
52 struct dsa_switch_driver *drv;
54 drv = list_entry(list, struct dsa_switch_driver, list);
56 name = drv->probe(bus, sw_addr);
62 mutex_unlock(&dsa_switch_drivers_mutex);
70 /* basic switch operations **************************************************/
71 static struct dsa_switch *
72 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
73 struct device *parent, struct mii_bus *bus)
75 struct dsa_chip_data *pd = dst->pd->chip + index;
76 struct dsa_switch_driver *drv;
77 struct dsa_switch *ds;
83 * Probe for switch model.
85 drv = dsa_switch_probe(bus, pd->sw_addr, &name);
87 printk(KERN_ERR "%s[%d]: could not detect attached switch\n",
88 dst->master_netdev->name, index);
89 return ERR_PTR(-EINVAL);
91 printk(KERN_INFO "%s[%d]: detected a %s switch\n",
92 dst->master_netdev->name, index, name);
96 * Allocate and initialise switch state.
98 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
100 return ERR_PTR(-ENOMEM);
104 ds->pd = dst->pd->chip + index;
106 ds->master_mii_bus = bus;
110 * Validate supplied switch configuration.
112 for (i = 0; i < DSA_MAX_PORTS; i++) {
115 name = pd->port_names[i];
119 if (!strcmp(name, "cpu")) {
120 if (dst->cpu_switch != -1) {
121 printk(KERN_ERR "multiple cpu ports?!\n");
125 dst->cpu_switch = index;
127 } else if (!strcmp(name, "dsa")) {
128 ds->dsa_port_mask |= 1 << i;
130 ds->phys_port_mask |= 1 << i;
136 * If the CPU connects to this switch, set the switch tree
137 * tagging protocol to the preferred tagging format of this
140 if (ds->dst->cpu_switch == index)
141 ds->dst->tag_protocol = drv->tag_protocol;
145 * Do basic register setup.
147 ret = drv->setup(ds);
151 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
155 ds->slave_mii_bus = mdiobus_alloc();
156 if (ds->slave_mii_bus == NULL) {
160 dsa_slave_mii_bus_init(ds);
162 ret = mdiobus_register(ds->slave_mii_bus);
168 * Create network devices for physical switch ports.
170 for (i = 0; i < DSA_MAX_PORTS; i++) {
171 struct net_device *slave_dev;
173 if (!(ds->phys_port_mask & (1 << i)))
176 slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
177 if (slave_dev == NULL) {
178 printk(KERN_ERR "%s[%d]: can't create dsa "
179 "slave device for port %d(%s)\n",
180 dst->master_netdev->name,
181 index, i, pd->port_names[i]);
185 ds->ports[i] = slave_dev;
191 mdiobus_free(ds->slave_mii_bus);
197 static void dsa_switch_destroy(struct dsa_switch *ds)
202 /* link polling *************************************************************/
203 static void dsa_link_poll_work(struct work_struct *ugly)
205 struct dsa_switch_tree *dst;
208 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
210 for (i = 0; i < dst->pd->nr_chips; i++) {
211 struct dsa_switch *ds = dst->ds[i];
213 if (ds != NULL && ds->drv->poll_link != NULL)
214 ds->drv->poll_link(ds);
217 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
220 static void dsa_link_poll_timer(unsigned long _dst)
222 struct dsa_switch_tree *dst = (void *)_dst;
224 schedule_work(&dst->link_poll_work);
228 /* platform driver init and cleanup *****************************************/
229 static int dev_is_class(struct device *dev, void *class)
231 if (dev->class != NULL && !strcmp(dev->class->name, class))
237 static struct device *dev_find_class(struct device *parent, char *class)
239 if (dev_is_class(parent, class)) {
244 return device_find_child(parent, class, dev_is_class);
247 static struct mii_bus *dev_to_mii_bus(struct device *dev)
251 d = dev_find_class(dev, "mdio_bus");
264 static struct net_device *dev_to_net_device(struct device *dev)
268 d = dev_find_class(dev, "net");
270 struct net_device *nd;
282 static int dsa_probe(struct platform_device *pdev)
284 static int dsa_version_printed;
285 struct dsa_platform_data *pd = pdev->dev.platform_data;
286 struct net_device *dev;
287 struct dsa_switch_tree *dst;
290 if (!dsa_version_printed++)
291 printk(KERN_NOTICE "Distributed Switch Architecture "
292 "driver version %s\n", dsa_driver_version);
294 if (pd == NULL || pd->netdev == NULL)
297 dev = dev_to_net_device(pd->netdev);
301 if (dev->dsa_ptr != NULL) {
306 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
312 platform_set_drvdata(pdev, dst);
315 dst->master_netdev = dev;
316 dst->cpu_switch = -1;
319 for (i = 0; i < pd->nr_chips; i++) {
321 struct dsa_switch *ds;
323 bus = dev_to_mii_bus(pd->chip[i].mii_bus);
325 printk(KERN_ERR "%s[%d]: no mii bus found for "
326 "dsa switch\n", dev->name, i);
330 ds = dsa_switch_setup(dst, i, &pdev->dev, bus);
332 printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
333 "instance (error %ld)\n", dev->name, i,
339 if (ds->drv->poll_link != NULL)
340 dst->link_poll_needed = 1;
344 * If we use a tagging format that doesn't have an ethertype
345 * field, make sure that all packets from this point on get
346 * sent to the tag format's receive function.
349 dev->dsa_ptr = (void *)dst;
351 if (dst->link_poll_needed) {
352 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
353 init_timer(&dst->link_poll_timer);
354 dst->link_poll_timer.data = (unsigned long)dst;
355 dst->link_poll_timer.function = dsa_link_poll_timer;
356 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
357 add_timer(&dst->link_poll_timer);
363 static int dsa_remove(struct platform_device *pdev)
365 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
368 if (dst->link_poll_needed)
369 del_timer_sync(&dst->link_poll_timer);
371 flush_work_sync(&dst->link_poll_work);
373 for (i = 0; i < dst->pd->nr_chips; i++) {
374 struct dsa_switch *ds = dst->ds[i];
377 dsa_switch_destroy(ds);
383 static void dsa_shutdown(struct platform_device *pdev)
387 static struct platform_driver dsa_driver = {
389 .remove = dsa_remove,
390 .shutdown = dsa_shutdown,
393 .owner = THIS_MODULE,
397 static int __init dsa_init_module(void)
399 return platform_driver_register(&dsa_driver);
401 module_init(dsa_init_module);
403 static void __exit dsa_cleanup_module(void)
405 platform_driver_unregister(&dsa_driver);
407 module_exit(dsa_cleanup_module);
409 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
410 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
411 MODULE_LICENSE("GPL");
412 MODULE_ALIAS("platform:dsa");