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>
17 char dsa_driver_version[] = "0.1";
20 /* switch driver registration ***********************************************/
21 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
22 static LIST_HEAD(dsa_switch_drivers);
24 void register_switch_driver(struct dsa_switch_driver *drv)
26 mutex_lock(&dsa_switch_drivers_mutex);
27 list_add_tail(&drv->list, &dsa_switch_drivers);
28 mutex_unlock(&dsa_switch_drivers_mutex);
31 void unregister_switch_driver(struct dsa_switch_driver *drv)
33 mutex_lock(&dsa_switch_drivers_mutex);
34 list_del_init(&drv->list);
35 mutex_unlock(&dsa_switch_drivers_mutex);
38 static struct dsa_switch_driver *
39 dsa_switch_probe(struct mii_bus *bus, int sw_addr, char **_name)
41 struct dsa_switch_driver *ret;
42 struct list_head *list;
48 mutex_lock(&dsa_switch_drivers_mutex);
49 list_for_each(list, &dsa_switch_drivers) {
50 struct dsa_switch_driver *drv;
52 drv = list_entry(list, struct dsa_switch_driver, list);
54 name = drv->probe(bus, sw_addr);
60 mutex_unlock(&dsa_switch_drivers_mutex);
68 /* basic switch operations **************************************************/
69 static struct dsa_switch *
70 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
71 struct device *parent, struct mii_bus *bus)
73 struct dsa_chip_data *pd = dst->pd->chip + index;
74 struct dsa_switch_driver *drv;
75 struct dsa_switch *ds;
81 * Probe for switch model.
83 drv = dsa_switch_probe(bus, pd->sw_addr, &name);
85 printk(KERN_ERR "%s[%d]: could not detect attached switch\n",
86 dst->master_netdev->name, index);
87 return ERR_PTR(-EINVAL);
89 printk(KERN_INFO "%s[%d]: detected a %s switch\n",
90 dst->master_netdev->name, index, name);
94 * Allocate and initialise switch state.
96 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
98 return ERR_PTR(-ENOMEM);
102 ds->pd = dst->pd->chip + index;
104 ds->master_mii_bus = bus;
108 * Validate supplied switch configuration.
110 for (i = 0; i < DSA_MAX_PORTS; i++) {
113 name = pd->port_names[i];
117 if (!strcmp(name, "cpu")) {
118 if (dst->cpu_switch != -1) {
119 printk(KERN_ERR "multiple cpu ports?!\n");
123 dst->cpu_switch = index;
125 } else if (!strcmp(name, "dsa")) {
126 ds->dsa_port_mask |= 1 << i;
128 ds->phys_port_mask |= 1 << i;
134 * If the CPU connects to this switch, set the switch tree
135 * tagging protocol to the preferred tagging format of this
138 if (ds->dst->cpu_switch == index)
139 ds->dst->tag_protocol = drv->tag_protocol;
143 * Do basic register setup.
145 ret = drv->setup(ds);
149 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
153 ds->slave_mii_bus = mdiobus_alloc();
154 if (ds->slave_mii_bus == NULL) {
158 dsa_slave_mii_bus_init(ds);
160 ret = mdiobus_register(ds->slave_mii_bus);
166 * Create network devices for physical switch ports.
168 for (i = 0; i < DSA_MAX_PORTS; i++) {
169 struct net_device *slave_dev;
171 if (!(ds->phys_port_mask & (1 << i)))
174 slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]);
175 if (slave_dev == NULL) {
176 printk(KERN_ERR "%s[%d]: can't create dsa "
177 "slave device for port %d(%s)\n",
178 dst->master_netdev->name,
179 index, i, pd->port_names[i]);
183 ds->ports[i] = slave_dev;
189 mdiobus_free(ds->slave_mii_bus);
195 static void dsa_switch_destroy(struct dsa_switch *ds)
200 /* hooks for ethertype-less tagging formats *********************************/
202 * The original DSA tag format and some other tag formats have no
203 * ethertype, which means that we need to add a little hack to the
204 * networking receive path to make sure that received frames get
205 * the right ->protocol assigned to them when one of those tag
208 bool dsa_uses_dsa_tags(void *dsa_ptr)
210 struct dsa_switch_tree *dst = dsa_ptr;
212 return !!(dst->tag_protocol == htons(ETH_P_DSA));
215 bool dsa_uses_trailer_tags(void *dsa_ptr)
217 struct dsa_switch_tree *dst = dsa_ptr;
219 return !!(dst->tag_protocol == htons(ETH_P_TRAILER));
223 /* link polling *************************************************************/
224 static void dsa_link_poll_work(struct work_struct *ugly)
226 struct dsa_switch_tree *dst;
229 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
231 for (i = 0; i < dst->pd->nr_chips; i++) {
232 struct dsa_switch *ds = dst->ds[i];
234 if (ds != NULL && ds->drv->poll_link != NULL)
235 ds->drv->poll_link(ds);
238 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
241 static void dsa_link_poll_timer(unsigned long _dst)
243 struct dsa_switch_tree *dst = (void *)_dst;
245 schedule_work(&dst->link_poll_work);
249 /* platform driver init and cleanup *****************************************/
250 static int dev_is_class(struct device *dev, void *class)
252 if (dev->class != NULL && !strcmp(dev->class->name, class))
258 static struct device *dev_find_class(struct device *parent, char *class)
260 if (dev_is_class(parent, class)) {
265 return device_find_child(parent, class, dev_is_class);
268 static struct mii_bus *dev_to_mii_bus(struct device *dev)
272 d = dev_find_class(dev, "mdio_bus");
285 static struct net_device *dev_to_net_device(struct device *dev)
289 d = dev_find_class(dev, "net");
291 struct net_device *nd;
303 static int dsa_probe(struct platform_device *pdev)
305 static int dsa_version_printed;
306 struct dsa_platform_data *pd = pdev->dev.platform_data;
307 struct net_device *dev;
308 struct dsa_switch_tree *dst;
311 if (!dsa_version_printed++)
312 printk(KERN_NOTICE "Distributed Switch Architecture "
313 "driver version %s\n", dsa_driver_version);
315 if (pd == NULL || pd->netdev == NULL)
318 dev = dev_to_net_device(pd->netdev);
322 if (dev->dsa_ptr != NULL) {
327 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
333 platform_set_drvdata(pdev, dst);
336 dst->master_netdev = dev;
337 dst->cpu_switch = -1;
340 for (i = 0; i < pd->nr_chips; i++) {
342 struct dsa_switch *ds;
344 bus = dev_to_mii_bus(pd->chip[i].mii_bus);
346 printk(KERN_ERR "%s[%d]: no mii bus found for "
347 "dsa switch\n", dev->name, i);
351 ds = dsa_switch_setup(dst, i, &pdev->dev, bus);
353 printk(KERN_ERR "%s[%d]: couldn't create dsa switch "
354 "instance (error %ld)\n", dev->name, i,
360 if (ds->drv->poll_link != NULL)
361 dst->link_poll_needed = 1;
365 * If we use a tagging format that doesn't have an ethertype
366 * field, make sure that all packets from this point on get
367 * sent to the tag format's receive function.
370 dev->dsa_ptr = (void *)dst;
372 if (dst->link_poll_needed) {
373 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
374 init_timer(&dst->link_poll_timer);
375 dst->link_poll_timer.data = (unsigned long)dst;
376 dst->link_poll_timer.function = dsa_link_poll_timer;
377 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
378 add_timer(&dst->link_poll_timer);
384 static int dsa_remove(struct platform_device *pdev)
386 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
389 if (dst->link_poll_needed)
390 del_timer_sync(&dst->link_poll_timer);
392 flush_scheduled_work();
394 for (i = 0; i < dst->pd->nr_chips; i++) {
395 struct dsa_switch *ds = dst->ds[i];
398 dsa_switch_destroy(ds);
404 static void dsa_shutdown(struct platform_device *pdev)
408 static struct platform_driver dsa_driver = {
410 .remove = dsa_remove,
411 .shutdown = dsa_shutdown,
414 .owner = THIS_MODULE,
418 static int __init dsa_init_module(void)
420 return platform_driver_register(&dsa_driver);
422 module_init(dsa_init_module);
424 static void __exit dsa_cleanup_module(void)
426 platform_driver_unregister(&dsa_driver);
428 module_exit(dsa_cleanup_module);
430 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>")
431 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
432 MODULE_LICENSE("GPL");
433 MODULE_ALIAS("platform:dsa");