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/list.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
18 #include <linux/of_mdio.h>
19 #include <linux/of_platform.h>
22 char dsa_driver_version
[] = "0.1";
25 /* switch driver registration ***********************************************/
26 static DEFINE_MUTEX(dsa_switch_drivers_mutex
);
27 static LIST_HEAD(dsa_switch_drivers
);
29 void register_switch_driver(struct dsa_switch_driver
*drv
)
31 mutex_lock(&dsa_switch_drivers_mutex
);
32 list_add_tail(&drv
->list
, &dsa_switch_drivers
);
33 mutex_unlock(&dsa_switch_drivers_mutex
);
35 EXPORT_SYMBOL_GPL(register_switch_driver
);
37 void unregister_switch_driver(struct dsa_switch_driver
*drv
)
39 mutex_lock(&dsa_switch_drivers_mutex
);
40 list_del_init(&drv
->list
);
41 mutex_unlock(&dsa_switch_drivers_mutex
);
43 EXPORT_SYMBOL_GPL(unregister_switch_driver
);
45 static struct dsa_switch_driver
*
46 dsa_switch_probe(struct device
*host_dev
, int sw_addr
, char **_name
)
48 struct dsa_switch_driver
*ret
;
49 struct list_head
*list
;
55 mutex_lock(&dsa_switch_drivers_mutex
);
56 list_for_each(list
, &dsa_switch_drivers
) {
57 struct dsa_switch_driver
*drv
;
59 drv
= list_entry(list
, struct dsa_switch_driver
, list
);
61 name
= drv
->probe(host_dev
, sw_addr
);
67 mutex_unlock(&dsa_switch_drivers_mutex
);
75 /* basic switch operations **************************************************/
76 static struct dsa_switch
*
77 dsa_switch_setup(struct dsa_switch_tree
*dst
, int index
,
78 struct device
*parent
, struct device
*host_dev
)
80 struct dsa_chip_data
*pd
= dst
->pd
->chip
+ index
;
81 struct dsa_switch_driver
*drv
;
82 struct dsa_switch
*ds
;
86 bool valid_name_found
= false;
89 * Probe for switch model.
91 drv
= dsa_switch_probe(host_dev
, pd
->sw_addr
, &name
);
93 printk(KERN_ERR
"%s[%d]: could not detect attached switch\n",
94 dst
->master_netdev
->name
, index
);
95 return ERR_PTR(-EINVAL
);
97 printk(KERN_INFO
"%s[%d]: detected a %s switch\n",
98 dst
->master_netdev
->name
, index
, name
);
102 * Allocate and initialise switch state.
104 ds
= kzalloc(sizeof(*ds
) + drv
->priv_size
, GFP_KERNEL
);
106 return ERR_PTR(-ENOMEM
);
110 ds
->pd
= dst
->pd
->chip
+ index
;
112 ds
->master_dev
= host_dev
;
115 * Validate supplied switch configuration.
117 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
120 name
= pd
->port_names
[i
];
124 if (!strcmp(name
, "cpu")) {
125 if (dst
->cpu_switch
!= -1) {
126 printk(KERN_ERR
"multiple cpu ports?!\n");
130 dst
->cpu_switch
= index
;
132 } else if (!strcmp(name
, "dsa")) {
133 ds
->dsa_port_mask
|= 1 << i
;
135 ds
->phys_port_mask
|= 1 << i
;
137 valid_name_found
= true;
140 if (!valid_name_found
&& i
== DSA_MAX_PORTS
) {
145 /* Make the built-in MII bus mask match the number of ports,
146 * switch drivers can override this later
148 ds
->phys_mii_mask
= ds
->phys_port_mask
;
151 * If the CPU connects to this switch, set the switch tree
152 * tagging protocol to the preferred tagging format of this
155 if (dst
->cpu_switch
== index
) {
156 switch (drv
->tag_protocol
) {
157 #ifdef CONFIG_NET_DSA_TAG_DSA
158 case DSA_TAG_PROTO_DSA
:
159 dst
->rcv
= dsa_netdev_ops
.rcv
;
162 #ifdef CONFIG_NET_DSA_TAG_EDSA
163 case DSA_TAG_PROTO_EDSA
:
164 dst
->rcv
= edsa_netdev_ops
.rcv
;
167 #ifdef CONFIG_NET_DSA_TAG_TRAILER
168 case DSA_TAG_PROTO_TRAILER
:
169 dst
->rcv
= trailer_netdev_ops
.rcv
;
172 #ifdef CONFIG_NET_DSA_TAG_BRCM
173 case DSA_TAG_PROTO_BRCM
:
174 dst
->rcv
= brcm_netdev_ops
.rcv
;
177 case DSA_TAG_PROTO_NONE
:
184 dst
->tag_protocol
= drv
->tag_protocol
;
188 * Do basic register setup.
190 ret
= drv
->setup(ds
);
194 ret
= drv
->set_addr(ds
, dst
->master_netdev
->dev_addr
);
198 ds
->slave_mii_bus
= mdiobus_alloc();
199 if (ds
->slave_mii_bus
== NULL
) {
203 dsa_slave_mii_bus_init(ds
);
205 ret
= mdiobus_register(ds
->slave_mii_bus
);
211 * Create network devices for physical switch ports.
213 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
214 struct net_device
*slave_dev
;
216 if (!(ds
->phys_port_mask
& (1 << i
)))
219 slave_dev
= dsa_slave_create(ds
, parent
, i
, pd
->port_names
[i
]);
220 if (slave_dev
== NULL
) {
221 printk(KERN_ERR
"%s[%d]: can't create dsa "
222 "slave device for port %d(%s)\n",
223 dst
->master_netdev
->name
,
224 index
, i
, pd
->port_names
[i
]);
228 ds
->ports
[i
] = slave_dev
;
234 mdiobus_free(ds
->slave_mii_bus
);
240 static void dsa_switch_destroy(struct dsa_switch
*ds
)
244 #ifdef CONFIG_PM_SLEEP
245 static int dsa_switch_suspend(struct dsa_switch
*ds
)
249 /* Suspend slave network devices */
250 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
251 if (!(ds
->phys_port_mask
& (1 << i
)))
254 ret
= dsa_slave_suspend(ds
->ports
[i
]);
259 if (ds
->drv
->suspend
)
260 ret
= ds
->drv
->suspend(ds
);
265 static int dsa_switch_resume(struct dsa_switch
*ds
)
270 ret
= ds
->drv
->resume(ds
);
275 /* Resume slave network devices */
276 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
277 if (!(ds
->phys_port_mask
& (1 << i
)))
280 ret
= dsa_slave_resume(ds
->ports
[i
]);
290 /* link polling *************************************************************/
291 static void dsa_link_poll_work(struct work_struct
*ugly
)
293 struct dsa_switch_tree
*dst
;
296 dst
= container_of(ugly
, struct dsa_switch_tree
, link_poll_work
);
298 for (i
= 0; i
< dst
->pd
->nr_chips
; i
++) {
299 struct dsa_switch
*ds
= dst
->ds
[i
];
301 if (ds
!= NULL
&& ds
->drv
->poll_link
!= NULL
)
302 ds
->drv
->poll_link(ds
);
305 mod_timer(&dst
->link_poll_timer
, round_jiffies(jiffies
+ HZ
));
308 static void dsa_link_poll_timer(unsigned long _dst
)
310 struct dsa_switch_tree
*dst
= (void *)_dst
;
312 schedule_work(&dst
->link_poll_work
);
316 /* platform driver init and cleanup *****************************************/
317 static int dev_is_class(struct device
*dev
, void *class)
319 if (dev
->class != NULL
&& !strcmp(dev
->class->name
, class))
325 static struct device
*dev_find_class(struct device
*parent
, char *class)
327 if (dev_is_class(parent
, class)) {
332 return device_find_child(parent
, class, dev_is_class
);
335 struct mii_bus
*dsa_host_dev_to_mii_bus(struct device
*dev
)
339 d
= dev_find_class(dev
, "mdio_bus");
351 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus
);
353 static struct net_device
*dev_to_net_device(struct device
*dev
)
357 d
= dev_find_class(dev
, "net");
359 struct net_device
*nd
;
372 static int dsa_of_setup_routing_table(struct dsa_platform_data
*pd
,
373 struct dsa_chip_data
*cd
,
375 struct device_node
*link
)
381 struct device_node
*parent_sw
;
384 parent_sw
= of_get_parent(link
);
388 reg
= of_get_property(parent_sw
, "reg", &len
);
389 if (!reg
|| (len
!= sizeof(*reg
) * 2))
392 link_sw_addr
= be32_to_cpup(reg
+ 1);
394 if (link_sw_addr
>= pd
->nr_chips
)
397 /* First time routing table allocation */
399 cd
->rtable
= kmalloc(pd
->nr_chips
* sizeof(s8
), GFP_KERNEL
);
403 /* default to no valid uplink/downlink */
404 memset(cd
->rtable
, -1, pd
->nr_chips
* sizeof(s8
));
407 reg
= of_get_property(link
, "reg", NULL
);
413 link_port_addr
= be32_to_cpup(reg
);
415 cd
->rtable
[link_sw_addr
] = link_port_addr
;
423 static void dsa_of_free_platform_data(struct dsa_platform_data
*pd
)
428 for (i
= 0; i
< pd
->nr_chips
; i
++) {
430 while (port_index
< DSA_MAX_PORTS
) {
431 kfree(pd
->chip
[i
].port_names
[port_index
]);
434 kfree(pd
->chip
[i
].rtable
);
439 static int dsa_of_probe(struct platform_device
*pdev
)
441 struct device_node
*np
= pdev
->dev
.of_node
;
442 struct device_node
*child
, *mdio
, *ethernet
, *port
, *link
;
443 struct mii_bus
*mdio_bus
;
444 struct platform_device
*ethernet_dev
;
445 struct dsa_platform_data
*pd
;
446 struct dsa_chip_data
*cd
;
447 const char *port_name
;
448 int chip_index
, port_index
;
449 const unsigned int *sw_addr
, *port_reg
;
452 mdio
= of_parse_phandle(np
, "dsa,mii-bus", 0);
456 mdio_bus
= of_mdio_find_bus(mdio
);
460 ethernet
= of_parse_phandle(np
, "dsa,ethernet", 0);
464 ethernet_dev
= of_find_device_by_node(ethernet
);
468 pd
= kzalloc(sizeof(*pd
), GFP_KERNEL
);
472 pdev
->dev
.platform_data
= pd
;
473 pd
->netdev
= ðernet_dev
->dev
;
474 pd
->nr_chips
= of_get_child_count(np
);
475 if (pd
->nr_chips
> DSA_MAX_SWITCHES
)
476 pd
->nr_chips
= DSA_MAX_SWITCHES
;
478 pd
->chip
= kzalloc(pd
->nr_chips
* sizeof(struct dsa_chip_data
),
486 for_each_available_child_of_node(np
, child
) {
488 cd
= &pd
->chip
[chip_index
];
491 cd
->host_dev
= &mdio_bus
->dev
;
493 sw_addr
= of_get_property(child
, "reg", NULL
);
497 cd
->sw_addr
= be32_to_cpup(sw_addr
);
498 if (cd
->sw_addr
> PHY_MAX_ADDR
)
501 for_each_available_child_of_node(child
, port
) {
502 port_reg
= of_get_property(port
, "reg", NULL
);
506 port_index
= be32_to_cpup(port_reg
);
508 port_name
= of_get_property(port
, "label", NULL
);
512 cd
->port_dn
[port_index
] = port
;
514 cd
->port_names
[port_index
] = kstrdup(port_name
,
516 if (!cd
->port_names
[port_index
]) {
521 link
= of_parse_phandle(port
, "link", 0);
523 if (!strcmp(port_name
, "dsa") && link
&&
525 ret
= dsa_of_setup_routing_table(pd
, cd
,
531 if (port_index
== DSA_MAX_PORTS
)
539 dsa_of_free_platform_data(pd
);
542 pdev
->dev
.platform_data
= NULL
;
546 static void dsa_of_remove(struct platform_device
*pdev
)
548 struct dsa_platform_data
*pd
= pdev
->dev
.platform_data
;
550 if (!pdev
->dev
.of_node
)
553 dsa_of_free_platform_data(pd
);
557 static inline int dsa_of_probe(struct platform_device
*pdev
)
562 static inline void dsa_of_remove(struct platform_device
*pdev
)
567 static int dsa_probe(struct platform_device
*pdev
)
569 static int dsa_version_printed
;
570 struct dsa_platform_data
*pd
= pdev
->dev
.platform_data
;
571 struct net_device
*dev
;
572 struct dsa_switch_tree
*dst
;
575 if (!dsa_version_printed
++)
576 printk(KERN_NOTICE
"Distributed Switch Architecture "
577 "driver version %s\n", dsa_driver_version
);
579 if (pdev
->dev
.of_node
) {
580 ret
= dsa_of_probe(pdev
);
584 pd
= pdev
->dev
.platform_data
;
587 if (pd
== NULL
|| pd
->netdev
== NULL
)
590 dev
= dev_to_net_device(pd
->netdev
);
596 if (dev
->dsa_ptr
!= NULL
) {
602 dst
= kzalloc(sizeof(*dst
), GFP_KERNEL
);
609 platform_set_drvdata(pdev
, dst
);
612 dst
->master_netdev
= dev
;
613 dst
->cpu_switch
= -1;
616 for (i
= 0; i
< pd
->nr_chips
; i
++) {
617 struct dsa_switch
*ds
;
619 ds
= dsa_switch_setup(dst
, i
, &pdev
->dev
, pd
->chip
[i
].host_dev
);
621 printk(KERN_ERR
"%s[%d]: couldn't create dsa switch "
622 "instance (error %ld)\n", dev
->name
, i
,
628 if (ds
->drv
->poll_link
!= NULL
)
629 dst
->link_poll_needed
= 1;
633 * If we use a tagging format that doesn't have an ethertype
634 * field, make sure that all packets from this point on get
635 * sent to the tag format's receive function.
638 dev
->dsa_ptr
= (void *)dst
;
640 if (dst
->link_poll_needed
) {
641 INIT_WORK(&dst
->link_poll_work
, dsa_link_poll_work
);
642 init_timer(&dst
->link_poll_timer
);
643 dst
->link_poll_timer
.data
= (unsigned long)dst
;
644 dst
->link_poll_timer
.function
= dsa_link_poll_timer
;
645 dst
->link_poll_timer
.expires
= round_jiffies(jiffies
+ HZ
);
646 add_timer(&dst
->link_poll_timer
);
657 static int dsa_remove(struct platform_device
*pdev
)
659 struct dsa_switch_tree
*dst
= platform_get_drvdata(pdev
);
662 if (dst
->link_poll_needed
)
663 del_timer_sync(&dst
->link_poll_timer
);
665 flush_work(&dst
->link_poll_work
);
667 for (i
= 0; i
< dst
->pd
->nr_chips
; i
++) {
668 struct dsa_switch
*ds
= dst
->ds
[i
];
671 dsa_switch_destroy(ds
);
679 static void dsa_shutdown(struct platform_device
*pdev
)
683 static int dsa_switch_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
684 struct packet_type
*pt
, struct net_device
*orig_dev
)
686 struct dsa_switch_tree
*dst
= dev
->dsa_ptr
;
688 if (unlikely(dst
== NULL
)) {
693 return dst
->rcv(skb
, dev
, pt
, orig_dev
);
696 static struct packet_type dsa_pack_type __read_mostly
= {
697 .type
= cpu_to_be16(ETH_P_XDSA
),
698 .func
= dsa_switch_rcv
,
701 #ifdef CONFIG_PM_SLEEP
702 static int dsa_suspend(struct device
*d
)
704 struct platform_device
*pdev
= to_platform_device(d
);
705 struct dsa_switch_tree
*dst
= platform_get_drvdata(pdev
);
708 for (i
= 0; i
< dst
->pd
->nr_chips
; i
++) {
709 struct dsa_switch
*ds
= dst
->ds
[i
];
712 ret
= dsa_switch_suspend(ds
);
718 static int dsa_resume(struct device
*d
)
720 struct platform_device
*pdev
= to_platform_device(d
);
721 struct dsa_switch_tree
*dst
= platform_get_drvdata(pdev
);
724 for (i
= 0; i
< dst
->pd
->nr_chips
; i
++) {
725 struct dsa_switch
*ds
= dst
->ds
[i
];
728 ret
= dsa_switch_resume(ds
);
735 static SIMPLE_DEV_PM_OPS(dsa_pm_ops
, dsa_suspend
, dsa_resume
);
737 static const struct of_device_id dsa_of_match_table
[] = {
738 { .compatible
= "brcm,bcm7445-switch-v4.0" },
739 { .compatible
= "marvell,dsa", },
742 MODULE_DEVICE_TABLE(of
, dsa_of_match_table
);
744 static struct platform_driver dsa_driver
= {
746 .remove
= dsa_remove
,
747 .shutdown
= dsa_shutdown
,
750 .owner
= THIS_MODULE
,
751 .of_match_table
= dsa_of_match_table
,
756 static int __init
dsa_init_module(void)
760 rc
= platform_driver_register(&dsa_driver
);
764 dev_add_pack(&dsa_pack_type
);
768 module_init(dsa_init_module
);
770 static void __exit
dsa_cleanup_module(void)
772 dev_remove_pack(&dsa_pack_type
);
773 platform_driver_unregister(&dsa_driver
);
775 module_exit(dsa_cleanup_module
);
777 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
778 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
779 MODULE_LICENSE("GPL");
780 MODULE_ALIAS("platform:dsa");