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/ctype.h>
13 #include <linux/device.h>
14 #include <linux/hwmon.h>
15 #include <linux/list.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
21 #include <linux/of_mdio.h>
22 #include <linux/of_platform.h>
23 #include <linux/of_net.h>
24 #include <linux/of_gpio.h>
25 #include <linux/sysfs.h>
26 #include <linux/phy_fixed.h>
27 #include <linux/gpio/consumer.h>
30 char dsa_driver_version
[] = "0.1";
32 static struct sk_buff
*dsa_slave_notag_xmit(struct sk_buff
*skb
,
33 struct net_device
*dev
)
35 /* Just return the original SKB */
39 static const struct dsa_device_ops none_ops
= {
40 .xmit
= dsa_slave_notag_xmit
,
44 const struct dsa_device_ops
*dsa_device_ops
[DSA_TAG_LAST
] = {
45 #ifdef CONFIG_NET_DSA_TAG_DSA
46 [DSA_TAG_PROTO_DSA
] = &dsa_netdev_ops
,
48 #ifdef CONFIG_NET_DSA_TAG_EDSA
49 [DSA_TAG_PROTO_EDSA
] = &edsa_netdev_ops
,
51 #ifdef CONFIG_NET_DSA_TAG_TRAILER
52 [DSA_TAG_PROTO_TRAILER
] = &trailer_netdev_ops
,
54 #ifdef CONFIG_NET_DSA_TAG_BRCM
55 [DSA_TAG_PROTO_BRCM
] = &brcm_netdev_ops
,
57 #ifdef CONFIG_NET_DSA_TAG_QCA
58 [DSA_TAG_PROTO_QCA
] = &qca_netdev_ops
,
60 [DSA_TAG_PROTO_NONE
] = &none_ops
,
63 /* switch driver registration ***********************************************/
64 static DEFINE_MUTEX(dsa_switch_drivers_mutex
);
65 static LIST_HEAD(dsa_switch_drivers
);
67 void register_switch_driver(struct dsa_switch_ops
*ops
)
69 mutex_lock(&dsa_switch_drivers_mutex
);
70 list_add_tail(&ops
->list
, &dsa_switch_drivers
);
71 mutex_unlock(&dsa_switch_drivers_mutex
);
73 EXPORT_SYMBOL_GPL(register_switch_driver
);
75 void unregister_switch_driver(struct dsa_switch_ops
*ops
)
77 mutex_lock(&dsa_switch_drivers_mutex
);
78 list_del_init(&ops
->list
);
79 mutex_unlock(&dsa_switch_drivers_mutex
);
81 EXPORT_SYMBOL_GPL(unregister_switch_driver
);
83 static struct dsa_switch_ops
*
84 dsa_switch_probe(struct device
*parent
, struct device
*host_dev
, int sw_addr
,
85 const char **_name
, void **priv
)
87 struct dsa_switch_ops
*ret
;
88 struct list_head
*list
;
94 mutex_lock(&dsa_switch_drivers_mutex
);
95 list_for_each(list
, &dsa_switch_drivers
) {
96 struct dsa_switch_ops
*ops
;
98 ops
= list_entry(list
, struct dsa_switch_ops
, list
);
100 name
= ops
->probe(parent
, host_dev
, sw_addr
, priv
);
106 mutex_unlock(&dsa_switch_drivers_mutex
);
113 /* hwmon support ************************************************************/
115 #ifdef CONFIG_NET_DSA_HWMON
117 static ssize_t
temp1_input_show(struct device
*dev
,
118 struct device_attribute
*attr
, char *buf
)
120 struct dsa_switch
*ds
= dev_get_drvdata(dev
);
123 ret
= ds
->ops
->get_temp(ds
, &temp
);
127 return sprintf(buf
, "%d\n", temp
* 1000);
129 static DEVICE_ATTR_RO(temp1_input
);
131 static ssize_t
temp1_max_show(struct device
*dev
,
132 struct device_attribute
*attr
, char *buf
)
134 struct dsa_switch
*ds
= dev_get_drvdata(dev
);
137 ret
= ds
->ops
->get_temp_limit(ds
, &temp
);
141 return sprintf(buf
, "%d\n", temp
* 1000);
144 static ssize_t
temp1_max_store(struct device
*dev
,
145 struct device_attribute
*attr
, const char *buf
,
148 struct dsa_switch
*ds
= dev_get_drvdata(dev
);
151 ret
= kstrtoint(buf
, 0, &temp
);
155 ret
= ds
->ops
->set_temp_limit(ds
, DIV_ROUND_CLOSEST(temp
, 1000));
161 static DEVICE_ATTR_RW(temp1_max
);
163 static ssize_t
temp1_max_alarm_show(struct device
*dev
,
164 struct device_attribute
*attr
, char *buf
)
166 struct dsa_switch
*ds
= dev_get_drvdata(dev
);
170 ret
= ds
->ops
->get_temp_alarm(ds
, &alarm
);
174 return sprintf(buf
, "%d\n", alarm
);
176 static DEVICE_ATTR_RO(temp1_max_alarm
);
178 static struct attribute
*dsa_hwmon_attrs
[] = {
179 &dev_attr_temp1_input
.attr
, /* 0 */
180 &dev_attr_temp1_max
.attr
, /* 1 */
181 &dev_attr_temp1_max_alarm
.attr
, /* 2 */
185 static umode_t
dsa_hwmon_attrs_visible(struct kobject
*kobj
,
186 struct attribute
*attr
, int index
)
188 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
189 struct dsa_switch
*ds
= dev_get_drvdata(dev
);
190 struct dsa_switch_ops
*ops
= ds
->ops
;
191 umode_t mode
= attr
->mode
;
194 if (!ops
->get_temp_limit
)
196 else if (!ops
->set_temp_limit
)
198 } else if (index
== 2 && !ops
->get_temp_alarm
) {
204 static const struct attribute_group dsa_hwmon_group
= {
205 .attrs
= dsa_hwmon_attrs
,
206 .is_visible
= dsa_hwmon_attrs_visible
,
208 __ATTRIBUTE_GROUPS(dsa_hwmon
);
210 #endif /* CONFIG_NET_DSA_HWMON */
212 /* basic switch operations **************************************************/
213 int dsa_cpu_dsa_setup(struct dsa_switch
*ds
, struct device
*dev
,
214 struct device_node
*port_dn
, int port
)
216 struct phy_device
*phydev
;
219 if (of_phy_is_fixed_link(port_dn
)) {
220 ret
= of_phy_register_fixed_link(port_dn
);
222 dev_err(dev
, "failed to register fixed PHY\n");
225 phydev
= of_phy_find_device(port_dn
);
227 mode
= of_get_phy_mode(port_dn
);
229 mode
= PHY_INTERFACE_MODE_NA
;
230 phydev
->interface
= mode
;
232 genphy_config_init(phydev
);
233 genphy_read_status(phydev
);
234 if (ds
->ops
->adjust_link
)
235 ds
->ops
->adjust_link(ds
, port
, phydev
);
237 put_device(&phydev
->mdio
.dev
);
243 static int dsa_cpu_dsa_setups(struct dsa_switch
*ds
, struct device
*dev
)
245 struct device_node
*port_dn
;
248 for (port
= 0; port
< DSA_MAX_PORTS
; port
++) {
249 if (!(dsa_is_cpu_port(ds
, port
) || dsa_is_dsa_port(ds
, port
)))
252 port_dn
= ds
->ports
[port
].dn
;
253 ret
= dsa_cpu_dsa_setup(ds
, dev
, port_dn
, port
);
260 const struct dsa_device_ops
*dsa_resolve_tag_protocol(int tag_protocol
)
262 const struct dsa_device_ops
*ops
;
264 if (tag_protocol
>= DSA_TAG_LAST
)
265 return ERR_PTR(-EINVAL
);
266 ops
= dsa_device_ops
[tag_protocol
];
269 return ERR_PTR(-ENOPROTOOPT
);
274 int dsa_cpu_port_ethtool_setup(struct dsa_switch
*ds
)
276 struct net_device
*master
;
277 struct ethtool_ops
*cpu_ops
;
279 master
= ds
->dst
->master_netdev
;
280 if (ds
->master_netdev
)
281 master
= ds
->master_netdev
;
283 cpu_ops
= devm_kzalloc(ds
->dev
, sizeof(*cpu_ops
), GFP_KERNEL
);
287 memcpy(&ds
->dst
->master_ethtool_ops
, master
->ethtool_ops
,
288 sizeof(struct ethtool_ops
));
289 ds
->dst
->master_orig_ethtool_ops
= master
->ethtool_ops
;
290 memcpy(cpu_ops
, &ds
->dst
->master_ethtool_ops
,
291 sizeof(struct ethtool_ops
));
292 dsa_cpu_port_ethtool_init(cpu_ops
);
293 master
->ethtool_ops
= cpu_ops
;
298 void dsa_cpu_port_ethtool_restore(struct dsa_switch
*ds
)
300 struct net_device
*master
;
302 master
= ds
->dst
->master_netdev
;
303 if (ds
->master_netdev
)
304 master
= ds
->master_netdev
;
306 master
->ethtool_ops
= ds
->dst
->master_orig_ethtool_ops
;
309 static int dsa_switch_setup_one(struct dsa_switch
*ds
, struct device
*parent
)
311 struct dsa_switch_ops
*ops
= ds
->ops
;
312 struct dsa_switch_tree
*dst
= ds
->dst
;
313 struct dsa_chip_data
*cd
= ds
->cd
;
314 bool valid_name_found
= false;
315 int index
= ds
->index
;
319 * Validate supplied switch configuration.
321 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
324 name
= cd
->port_names
[i
];
328 if (!strcmp(name
, "cpu")) {
329 if (dst
->cpu_switch
!= -1) {
330 netdev_err(dst
->master_netdev
,
331 "multiple cpu ports?!\n");
335 dst
->cpu_switch
= index
;
337 ds
->cpu_port_mask
|= 1 << i
;
338 } else if (!strcmp(name
, "dsa")) {
339 ds
->dsa_port_mask
|= 1 << i
;
341 ds
->enabled_port_mask
|= 1 << i
;
343 valid_name_found
= true;
346 if (!valid_name_found
&& i
== DSA_MAX_PORTS
) {
351 /* Make the built-in MII bus mask match the number of ports,
352 * switch drivers can override this later
354 ds
->phys_mii_mask
= ds
->enabled_port_mask
;
357 * If the CPU connects to this switch, set the switch tree
358 * tagging protocol to the preferred tagging format of this
361 if (dst
->cpu_switch
== index
) {
362 enum dsa_tag_protocol tag_protocol
;
364 tag_protocol
= ops
->get_tag_protocol(ds
);
365 dst
->tag_ops
= dsa_resolve_tag_protocol(tag_protocol
);
366 if (IS_ERR(dst
->tag_ops
)) {
367 ret
= PTR_ERR(dst
->tag_ops
);
371 dst
->rcv
= dst
->tag_ops
->rcv
;
374 memcpy(ds
->rtable
, cd
->rtable
, sizeof(ds
->rtable
));
377 * Do basic register setup.
379 ret
= ops
->setup(ds
);
384 ret
= ops
->set_addr(ds
, dst
->master_netdev
->dev_addr
);
389 if (!ds
->slave_mii_bus
&& ops
->phy_read
) {
390 ds
->slave_mii_bus
= devm_mdiobus_alloc(parent
);
391 if (!ds
->slave_mii_bus
) {
395 dsa_slave_mii_bus_init(ds
);
397 ret
= mdiobus_register(ds
->slave_mii_bus
);
403 * Create network devices for physical switch ports.
405 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
406 ds
->ports
[i
].dn
= cd
->port_dn
[i
];
408 if (!(ds
->enabled_port_mask
& (1 << i
)))
411 ret
= dsa_slave_create(ds
, parent
, i
, cd
->port_names
[i
]);
413 netdev_err(dst
->master_netdev
, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
414 index
, i
, cd
->port_names
[i
], ret
);
419 /* Perform configuration of the CPU and DSA ports */
420 ret
= dsa_cpu_dsa_setups(ds
, parent
);
422 netdev_err(dst
->master_netdev
, "[%d] : can't configure CPU and DSA ports\n",
427 ret
= dsa_cpu_port_ethtool_setup(ds
);
431 #ifdef CONFIG_NET_DSA_HWMON
432 /* If the switch provides a temperature sensor,
433 * register with hardware monitoring subsystem.
434 * Treat registration error as non-fatal and ignore it.
437 const char *netname
= netdev_name(dst
->master_netdev
);
438 char hname
[IFNAMSIZ
+ 1];
441 /* Create valid hwmon 'name' attribute */
442 for (i
= j
= 0; i
< IFNAMSIZ
&& netname
[i
]; i
++) {
443 if (isalnum(netname
[i
]))
444 hname
[j
++] = netname
[i
];
447 scnprintf(ds
->hwmon_name
, sizeof(ds
->hwmon_name
), "%s_dsa%d",
449 ds
->hwmon_dev
= hwmon_device_register_with_groups(NULL
,
450 ds
->hwmon_name
, ds
, dsa_hwmon_groups
);
451 if (IS_ERR(ds
->hwmon_dev
))
452 ds
->hwmon_dev
= NULL
;
454 #endif /* CONFIG_NET_DSA_HWMON */
462 static struct dsa_switch
*
463 dsa_switch_setup(struct dsa_switch_tree
*dst
, int index
,
464 struct device
*parent
, struct device
*host_dev
)
466 struct dsa_chip_data
*cd
= dst
->pd
->chip
+ index
;
467 struct dsa_switch_ops
*ops
;
468 struct dsa_switch
*ds
;
474 * Probe for switch model.
476 ops
= dsa_switch_probe(parent
, host_dev
, cd
->sw_addr
, &name
, &priv
);
478 netdev_err(dst
->master_netdev
, "[%d]: could not detect attached switch\n",
480 return ERR_PTR(-EINVAL
);
482 netdev_info(dst
->master_netdev
, "[%d]: detected a %s switch\n",
487 * Allocate and initialise switch state.
489 ds
= devm_kzalloc(parent
, sizeof(*ds
), GFP_KERNEL
);
491 return ERR_PTR(-ENOMEM
);
500 ret
= dsa_switch_setup_one(ds
, parent
);
507 void dsa_cpu_dsa_destroy(struct device_node
*port_dn
)
509 if (of_phy_is_fixed_link(port_dn
))
510 of_phy_deregister_fixed_link(port_dn
);
513 static void dsa_switch_destroy(struct dsa_switch
*ds
)
517 #ifdef CONFIG_NET_DSA_HWMON
519 hwmon_device_unregister(ds
->hwmon_dev
);
522 /* Destroy network devices for physical switch ports. */
523 for (port
= 0; port
< DSA_MAX_PORTS
; port
++) {
524 if (!(ds
->enabled_port_mask
& (1 << port
)))
527 if (!ds
->ports
[port
].netdev
)
530 dsa_slave_destroy(ds
->ports
[port
].netdev
);
533 /* Disable configuration of the CPU and DSA ports */
534 for (port
= 0; port
< DSA_MAX_PORTS
; port
++) {
535 if (!(dsa_is_cpu_port(ds
, port
) || dsa_is_dsa_port(ds
, port
)))
537 dsa_cpu_dsa_destroy(ds
->ports
[port
].dn
);
539 /* Clearing a bit which is not set does no harm */
540 ds
->cpu_port_mask
|= ~(1 << port
);
541 ds
->dsa_port_mask
|= ~(1 << port
);
544 if (ds
->slave_mii_bus
&& ds
->ops
->phy_read
)
545 mdiobus_unregister(ds
->slave_mii_bus
);
548 #ifdef CONFIG_PM_SLEEP
549 int dsa_switch_suspend(struct dsa_switch
*ds
)
553 /* Suspend slave network devices */
554 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
555 if (!dsa_is_port_initialized(ds
, i
))
558 ret
= dsa_slave_suspend(ds
->ports
[i
].netdev
);
563 if (ds
->ops
->suspend
)
564 ret
= ds
->ops
->suspend(ds
);
568 EXPORT_SYMBOL_GPL(dsa_switch_suspend
);
570 int dsa_switch_resume(struct dsa_switch
*ds
)
575 ret
= ds
->ops
->resume(ds
);
580 /* Resume slave network devices */
581 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
582 if (!dsa_is_port_initialized(ds
, i
))
585 ret
= dsa_slave_resume(ds
->ports
[i
].netdev
);
592 EXPORT_SYMBOL_GPL(dsa_switch_resume
);
595 /* platform driver init and cleanup *****************************************/
596 static int dev_is_class(struct device
*dev
, void *class)
598 if (dev
->class != NULL
&& !strcmp(dev
->class->name
, class))
604 static struct device
*dev_find_class(struct device
*parent
, char *class)
606 if (dev_is_class(parent
, class)) {
611 return device_find_child(parent
, class, dev_is_class
);
614 struct mii_bus
*dsa_host_dev_to_mii_bus(struct device
*dev
)
618 d
= dev_find_class(dev
, "mdio_bus");
630 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus
);
632 static struct net_device
*dev_to_net_device(struct device
*dev
)
636 d
= dev_find_class(dev
, "net");
638 struct net_device
*nd
;
651 static int dsa_of_setup_routing_table(struct dsa_platform_data
*pd
,
652 struct dsa_chip_data
*cd
,
653 int chip_index
, int port_index
,
654 struct device_node
*link
)
658 struct device_node
*parent_sw
;
661 parent_sw
= of_get_parent(link
);
665 reg
= of_get_property(parent_sw
, "reg", &len
);
666 if (!reg
|| (len
!= sizeof(*reg
) * 2))
670 * Get the destination switch number from the second field of its 'reg'
671 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
673 link_sw_addr
= be32_to_cpup(reg
+ 1);
675 if (link_sw_addr
>= pd
->nr_chips
)
678 cd
->rtable
[link_sw_addr
] = port_index
;
683 static int dsa_of_probe_links(struct dsa_platform_data
*pd
,
684 struct dsa_chip_data
*cd
,
685 int chip_index
, int port_index
,
686 struct device_node
*port
,
687 const char *port_name
)
689 struct device_node
*link
;
693 for (link_index
= 0;; link_index
++) {
694 link
= of_parse_phandle(port
, "link", link_index
);
698 if (!strcmp(port_name
, "dsa") && pd
->nr_chips
> 1) {
699 ret
= dsa_of_setup_routing_table(pd
, cd
, chip_index
,
708 static void dsa_of_free_platform_data(struct dsa_platform_data
*pd
)
713 for (i
= 0; i
< pd
->nr_chips
; i
++) {
715 while (port_index
< DSA_MAX_PORTS
) {
716 kfree(pd
->chip
[i
].port_names
[port_index
]);
720 /* Drop our reference to the MDIO bus device */
721 if (pd
->chip
[i
].host_dev
)
722 put_device(pd
->chip
[i
].host_dev
);
727 static int dsa_of_probe(struct device
*dev
)
729 struct device_node
*np
= dev
->of_node
;
730 struct device_node
*child
, *mdio
, *ethernet
, *port
;
731 struct mii_bus
*mdio_bus
, *mdio_bus_switch
;
732 struct net_device
*ethernet_dev
;
733 struct dsa_platform_data
*pd
;
734 struct dsa_chip_data
*cd
;
735 const char *port_name
;
736 int chip_index
, port_index
;
737 const unsigned int *sw_addr
, *port_reg
;
741 mdio
= of_parse_phandle(np
, "dsa,mii-bus", 0);
745 mdio_bus
= of_mdio_find_bus(mdio
);
747 return -EPROBE_DEFER
;
749 ethernet
= of_parse_phandle(np
, "dsa,ethernet", 0);
755 ethernet_dev
= of_find_net_device_by_node(ethernet
);
761 pd
= kzalloc(sizeof(*pd
), GFP_KERNEL
);
764 goto out_put_ethernet
;
767 dev
->platform_data
= pd
;
768 pd
->of_netdev
= ethernet_dev
;
769 pd
->nr_chips
= of_get_available_child_count(np
);
770 if (pd
->nr_chips
> DSA_MAX_SWITCHES
)
771 pd
->nr_chips
= DSA_MAX_SWITCHES
;
773 pd
->chip
= kcalloc(pd
->nr_chips
, sizeof(struct dsa_chip_data
),
781 for_each_available_child_of_node(np
, child
) {
785 cd
= &pd
->chip
[chip_index
];
789 /* Initialize the routing table */
790 for (i
= 0; i
< DSA_MAX_SWITCHES
; ++i
)
791 cd
->rtable
[i
] = DSA_RTABLE_NONE
;
793 /* When assigning the host device, increment its refcount */
794 cd
->host_dev
= get_device(&mdio_bus
->dev
);
796 sw_addr
= of_get_property(child
, "reg", NULL
);
800 cd
->sw_addr
= be32_to_cpup(sw_addr
);
801 if (cd
->sw_addr
>= PHY_MAX_ADDR
)
804 if (!of_property_read_u32(child
, "eeprom-length", &eeprom_len
))
805 cd
->eeprom_len
= eeprom_len
;
807 mdio
= of_parse_phandle(child
, "mii-bus", 0);
809 mdio_bus_switch
= of_mdio_find_bus(mdio
);
810 if (!mdio_bus_switch
) {
815 /* Drop the mdio_bus device ref, replacing the host
816 * device with the mdio_bus_switch device, keeping
817 * the refcount from of_mdio_find_bus() above.
819 put_device(cd
->host_dev
);
820 cd
->host_dev
= &mdio_bus_switch
->dev
;
823 for_each_available_child_of_node(child
, port
) {
824 port_reg
= of_get_property(port
, "reg", NULL
);
828 port_index
= be32_to_cpup(port_reg
);
829 if (port_index
>= DSA_MAX_PORTS
)
832 port_name
= of_get_property(port
, "label", NULL
);
836 cd
->port_dn
[port_index
] = port
;
838 cd
->port_names
[port_index
] = kstrdup(port_name
,
840 if (!cd
->port_names
[port_index
]) {
845 ret
= dsa_of_probe_links(pd
, cd
, chip_index
,
846 port_index
, port
, port_name
);
853 /* The individual chips hold their own refcount on the mdio bus,
855 put_device(&mdio_bus
->dev
);
860 dsa_of_free_platform_data(pd
);
863 dev
->platform_data
= NULL
;
865 put_device(ðernet_dev
->dev
);
867 put_device(&mdio_bus
->dev
);
871 static void dsa_of_remove(struct device
*dev
)
873 struct dsa_platform_data
*pd
= dev
->platform_data
;
878 dsa_of_free_platform_data(pd
);
879 put_device(&pd
->of_netdev
->dev
);
883 static inline int dsa_of_probe(struct device
*dev
)
888 static inline void dsa_of_remove(struct device
*dev
)
893 static int dsa_setup_dst(struct dsa_switch_tree
*dst
, struct net_device
*dev
,
894 struct device
*parent
, struct dsa_platform_data
*pd
)
897 unsigned configured
= 0;
900 dst
->master_netdev
= dev
;
901 dst
->cpu_switch
= -1;
904 for (i
= 0; i
< pd
->nr_chips
; i
++) {
905 struct dsa_switch
*ds
;
907 ds
= dsa_switch_setup(dst
, i
, parent
, pd
->chip
[i
].host_dev
);
909 netdev_err(dev
, "[%d]: couldn't create dsa switch instance (error %ld)\n",
920 * If no switch was found, exit cleanly
923 return -EPROBE_DEFER
;
926 * If we use a tagging format that doesn't have an ethertype
927 * field, make sure that all packets from this point on get
928 * sent to the tag format's receive function.
931 dev
->dsa_ptr
= (void *)dst
;
936 static int dsa_probe(struct platform_device
*pdev
)
938 struct dsa_platform_data
*pd
= pdev
->dev
.platform_data
;
939 struct net_device
*dev
;
940 struct dsa_switch_tree
*dst
;
943 pr_notice_once("Distributed Switch Architecture driver version %s\n",
946 if (pdev
->dev
.of_node
) {
947 ret
= dsa_of_probe(&pdev
->dev
);
951 pd
= pdev
->dev
.platform_data
;
954 if (pd
== NULL
|| (pd
->netdev
== NULL
&& pd
->of_netdev
== NULL
))
961 dev
= dev_to_net_device(pd
->netdev
);
968 if (dev
->dsa_ptr
!= NULL
) {
974 dst
= devm_kzalloc(&pdev
->dev
, sizeof(*dst
), GFP_KERNEL
);
981 platform_set_drvdata(pdev
, dst
);
983 ret
= dsa_setup_dst(dst
, dev
, &pdev
->dev
, pd
);
992 dsa_of_remove(&pdev
->dev
);
997 static void dsa_remove_dst(struct dsa_switch_tree
*dst
)
1001 dst
->master_netdev
->dsa_ptr
= NULL
;
1003 /* If we used a tagging format that doesn't have an ethertype
1004 * field, make sure that all packets from this point get sent
1005 * without the tag and go through the regular receive path.
1009 for (i
= 0; i
< dst
->pd
->nr_chips
; i
++) {
1010 struct dsa_switch
*ds
= dst
->ds
[i
];
1013 dsa_switch_destroy(ds
);
1016 dsa_cpu_port_ethtool_restore(dst
->ds
[0]);
1018 dev_put(dst
->master_netdev
);
1021 static int dsa_remove(struct platform_device
*pdev
)
1023 struct dsa_switch_tree
*dst
= platform_get_drvdata(pdev
);
1025 dsa_remove_dst(dst
);
1026 dsa_of_remove(&pdev
->dev
);
1031 static void dsa_shutdown(struct platform_device
*pdev
)
1035 static int dsa_switch_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1036 struct packet_type
*pt
, struct net_device
*orig_dev
)
1038 struct dsa_switch_tree
*dst
= dev
->dsa_ptr
;
1040 if (unlikely(dst
== NULL
)) {
1045 return dst
->rcv(skb
, dev
, pt
, orig_dev
);
1048 static struct packet_type dsa_pack_type __read_mostly
= {
1049 .type
= cpu_to_be16(ETH_P_XDSA
),
1050 .func
= dsa_switch_rcv
,
1053 static struct notifier_block dsa_netdevice_nb __read_mostly
= {
1054 .notifier_call
= dsa_slave_netdevice_event
,
1057 #ifdef CONFIG_PM_SLEEP
1058 static int dsa_suspend(struct device
*d
)
1060 struct platform_device
*pdev
= to_platform_device(d
);
1061 struct dsa_switch_tree
*dst
= platform_get_drvdata(pdev
);
1064 for (i
= 0; i
< dst
->pd
->nr_chips
; i
++) {
1065 struct dsa_switch
*ds
= dst
->ds
[i
];
1068 ret
= dsa_switch_suspend(ds
);
1074 static int dsa_resume(struct device
*d
)
1076 struct platform_device
*pdev
= to_platform_device(d
);
1077 struct dsa_switch_tree
*dst
= platform_get_drvdata(pdev
);
1080 for (i
= 0; i
< dst
->pd
->nr_chips
; i
++) {
1081 struct dsa_switch
*ds
= dst
->ds
[i
];
1084 ret
= dsa_switch_resume(ds
);
1091 static SIMPLE_DEV_PM_OPS(dsa_pm_ops
, dsa_suspend
, dsa_resume
);
1093 static const struct of_device_id dsa_of_match_table
[] = {
1094 { .compatible
= "marvell,dsa", },
1097 MODULE_DEVICE_TABLE(of
, dsa_of_match_table
);
1099 static struct platform_driver dsa_driver
= {
1101 .remove
= dsa_remove
,
1102 .shutdown
= dsa_shutdown
,
1105 .of_match_table
= dsa_of_match_table
,
1110 static int __init
dsa_init_module(void)
1114 register_netdevice_notifier(&dsa_netdevice_nb
);
1116 rc
= platform_driver_register(&dsa_driver
);
1120 dev_add_pack(&dsa_pack_type
);
1124 module_init(dsa_init_module
);
1126 static void __exit
dsa_cleanup_module(void)
1128 unregister_netdevice_notifier(&dsa_netdevice_nb
);
1129 dev_remove_pack(&dsa_pack_type
);
1130 platform_driver_unregister(&dsa_driver
);
1132 module_exit(dsa_cleanup_module
);
1134 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
1135 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1136 MODULE_LICENSE("GPL");
1137 MODULE_ALIAS("platform:dsa");