x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / net / dsa / dsa.c
blobb6d4f6a23f06c9d794a5eedc4c9f79810d5b06e5
1 /*
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/device.h>
13 #include <linux/list.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <net/dsa.h>
18 #include <linux/of.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_platform.h>
21 #include <linux/of_net.h>
22 #include <linux/of_gpio.h>
23 #include <linux/sysfs.h>
24 #include <linux/phy_fixed.h>
25 #include <linux/gpio/consumer.h>
26 #include "dsa_priv.h"
28 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
29 struct net_device *dev)
31 /* Just return the original SKB */
32 return skb;
35 static const struct dsa_device_ops none_ops = {
36 .xmit = dsa_slave_notag_xmit,
37 .rcv = NULL,
40 const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
41 #ifdef CONFIG_NET_DSA_TAG_DSA
42 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
43 #endif
44 #ifdef CONFIG_NET_DSA_TAG_EDSA
45 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
46 #endif
47 #ifdef CONFIG_NET_DSA_TAG_TRAILER
48 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
49 #endif
50 #ifdef CONFIG_NET_DSA_TAG_BRCM
51 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
52 #endif
53 #ifdef CONFIG_NET_DSA_TAG_QCA
54 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
55 #endif
56 [DSA_TAG_PROTO_NONE] = &none_ops,
59 /* switch driver registration ***********************************************/
60 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
61 static LIST_HEAD(dsa_switch_drivers);
63 void register_switch_driver(struct dsa_switch_driver *drv)
65 mutex_lock(&dsa_switch_drivers_mutex);
66 list_add_tail(&drv->list, &dsa_switch_drivers);
67 mutex_unlock(&dsa_switch_drivers_mutex);
69 EXPORT_SYMBOL_GPL(register_switch_driver);
71 void unregister_switch_driver(struct dsa_switch_driver *drv)
73 mutex_lock(&dsa_switch_drivers_mutex);
74 list_del_init(&drv->list);
75 mutex_unlock(&dsa_switch_drivers_mutex);
77 EXPORT_SYMBOL_GPL(unregister_switch_driver);
79 static const struct dsa_switch_ops *
80 dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
81 const char **_name, void **priv)
83 const struct dsa_switch_ops *ret;
84 struct list_head *list;
85 const char *name;
87 ret = NULL;
88 name = NULL;
90 mutex_lock(&dsa_switch_drivers_mutex);
91 list_for_each(list, &dsa_switch_drivers) {
92 const struct dsa_switch_ops *ops;
93 struct dsa_switch_driver *drv;
95 drv = list_entry(list, struct dsa_switch_driver, list);
96 ops = drv->ops;
98 name = ops->probe(parent, host_dev, sw_addr, priv);
99 if (name != NULL) {
100 ret = ops;
101 break;
104 mutex_unlock(&dsa_switch_drivers_mutex);
106 *_name = name;
108 return ret;
111 /* basic switch operations **************************************************/
112 int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
113 struct dsa_port *dport, int port)
115 struct device_node *port_dn = dport->dn;
116 struct phy_device *phydev;
117 int ret, mode;
119 if (of_phy_is_fixed_link(port_dn)) {
120 ret = of_phy_register_fixed_link(port_dn);
121 if (ret) {
122 dev_err(dev, "failed to register fixed PHY\n");
123 return ret;
125 phydev = of_phy_find_device(port_dn);
127 mode = of_get_phy_mode(port_dn);
128 if (mode < 0)
129 mode = PHY_INTERFACE_MODE_NA;
130 phydev->interface = mode;
132 genphy_config_init(phydev);
133 genphy_read_status(phydev);
134 if (ds->ops->adjust_link)
135 ds->ops->adjust_link(ds, port, phydev);
137 put_device(&phydev->mdio.dev);
140 return 0;
143 static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
145 struct dsa_port *dport;
146 int ret, port;
148 for (port = 0; port < ds->num_ports; port++) {
149 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
150 continue;
152 dport = &ds->ports[port];
153 ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
154 if (ret)
155 return ret;
157 return 0;
160 const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
162 const struct dsa_device_ops *ops;
164 if (tag_protocol >= DSA_TAG_LAST)
165 return ERR_PTR(-EINVAL);
166 ops = dsa_device_ops[tag_protocol];
168 if (!ops)
169 return ERR_PTR(-ENOPROTOOPT);
171 return ops;
174 int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
176 struct net_device *master;
177 struct ethtool_ops *cpu_ops;
179 master = ds->dst->master_netdev;
180 if (ds->master_netdev)
181 master = ds->master_netdev;
183 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
184 if (!cpu_ops)
185 return -ENOMEM;
187 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
188 sizeof(struct ethtool_ops));
189 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
190 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
191 sizeof(struct ethtool_ops));
192 dsa_cpu_port_ethtool_init(cpu_ops);
193 master->ethtool_ops = cpu_ops;
195 return 0;
198 void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
200 struct net_device *master;
202 master = ds->dst->master_netdev;
203 if (ds->master_netdev)
204 master = ds->master_netdev;
206 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
209 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
211 const struct dsa_switch_ops *ops = ds->ops;
212 struct dsa_switch_tree *dst = ds->dst;
213 struct dsa_chip_data *cd = ds->cd;
214 bool valid_name_found = false;
215 int index = ds->index;
216 int i, ret;
219 * Validate supplied switch configuration.
221 for (i = 0; i < ds->num_ports; i++) {
222 char *name;
224 name = cd->port_names[i];
225 if (name == NULL)
226 continue;
228 if (!strcmp(name, "cpu")) {
229 if (dst->cpu_switch) {
230 netdev_err(dst->master_netdev,
231 "multiple cpu ports?!\n");
232 return -EINVAL;
234 dst->cpu_switch = ds;
235 dst->cpu_port = i;
236 ds->cpu_port_mask |= 1 << i;
237 } else if (!strcmp(name, "dsa")) {
238 ds->dsa_port_mask |= 1 << i;
239 } else {
240 ds->enabled_port_mask |= 1 << i;
242 valid_name_found = true;
245 if (!valid_name_found && i == ds->num_ports)
246 return -EINVAL;
248 /* Make the built-in MII bus mask match the number of ports,
249 * switch drivers can override this later
251 ds->phys_mii_mask = ds->enabled_port_mask;
254 * If the CPU connects to this switch, set the switch tree
255 * tagging protocol to the preferred tagging format of this
256 * switch.
258 if (dst->cpu_switch == ds) {
259 enum dsa_tag_protocol tag_protocol;
261 tag_protocol = ops->get_tag_protocol(ds);
262 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
263 if (IS_ERR(dst->tag_ops))
264 return PTR_ERR(dst->tag_ops);
266 dst->rcv = dst->tag_ops->rcv;
269 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
272 * Do basic register setup.
274 ret = ops->setup(ds);
275 if (ret < 0)
276 return ret;
278 ret = dsa_switch_register_notifier(ds);
279 if (ret)
280 return ret;
282 if (ops->set_addr) {
283 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
284 if (ret < 0)
285 return ret;
288 if (!ds->slave_mii_bus && ops->phy_read) {
289 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
290 if (!ds->slave_mii_bus)
291 return -ENOMEM;
292 dsa_slave_mii_bus_init(ds);
294 ret = mdiobus_register(ds->slave_mii_bus);
295 if (ret < 0)
296 return ret;
300 * Create network devices for physical switch ports.
302 for (i = 0; i < ds->num_ports; i++) {
303 ds->ports[i].dn = cd->port_dn[i];
305 if (!(ds->enabled_port_mask & (1 << i)))
306 continue;
308 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
309 if (ret < 0)
310 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
311 index, i, cd->port_names[i], ret);
314 /* Perform configuration of the CPU and DSA ports */
315 ret = dsa_cpu_dsa_setups(ds, parent);
316 if (ret < 0)
317 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
318 index);
320 ret = dsa_cpu_port_ethtool_setup(ds);
321 if (ret)
322 return ret;
324 return 0;
327 static struct dsa_switch *
328 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
329 struct device *parent, struct device *host_dev)
331 struct dsa_chip_data *cd = dst->pd->chip + index;
332 const struct dsa_switch_ops *ops;
333 struct dsa_switch *ds;
334 int ret;
335 const char *name;
336 void *priv;
339 * Probe for switch model.
341 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
342 if (!ops) {
343 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
344 index);
345 return ERR_PTR(-EINVAL);
347 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
348 index, name);
352 * Allocate and initialise switch state.
354 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
355 if (!ds)
356 return ERR_PTR(-ENOMEM);
358 ds->dst = dst;
359 ds->index = index;
360 ds->cd = cd;
361 ds->ops = ops;
362 ds->priv = priv;
364 ret = dsa_switch_setup_one(ds, parent);
365 if (ret)
366 return ERR_PTR(ret);
368 return ds;
371 void dsa_cpu_dsa_destroy(struct dsa_port *port)
373 struct device_node *port_dn = port->dn;
375 if (of_phy_is_fixed_link(port_dn))
376 of_phy_deregister_fixed_link(port_dn);
379 static void dsa_switch_destroy(struct dsa_switch *ds)
381 int port;
383 /* Destroy network devices for physical switch ports. */
384 for (port = 0; port < ds->num_ports; port++) {
385 if (!(ds->enabled_port_mask & (1 << port)))
386 continue;
388 if (!ds->ports[port].netdev)
389 continue;
391 dsa_slave_destroy(ds->ports[port].netdev);
394 /* Disable configuration of the CPU and DSA ports */
395 for (port = 0; port < ds->num_ports; port++) {
396 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
397 continue;
398 dsa_cpu_dsa_destroy(&ds->ports[port]);
400 /* Clearing a bit which is not set does no harm */
401 ds->cpu_port_mask |= ~(1 << port);
402 ds->dsa_port_mask |= ~(1 << port);
405 if (ds->slave_mii_bus && ds->ops->phy_read)
406 mdiobus_unregister(ds->slave_mii_bus);
408 dsa_switch_unregister_notifier(ds);
411 #ifdef CONFIG_PM_SLEEP
412 int dsa_switch_suspend(struct dsa_switch *ds)
414 int i, ret = 0;
416 /* Suspend slave network devices */
417 for (i = 0; i < ds->num_ports; i++) {
418 if (!dsa_is_port_initialized(ds, i))
419 continue;
421 ret = dsa_slave_suspend(ds->ports[i].netdev);
422 if (ret)
423 return ret;
426 if (ds->ops->suspend)
427 ret = ds->ops->suspend(ds);
429 return ret;
431 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
433 int dsa_switch_resume(struct dsa_switch *ds)
435 int i, ret = 0;
437 if (ds->ops->resume)
438 ret = ds->ops->resume(ds);
440 if (ret)
441 return ret;
443 /* Resume slave network devices */
444 for (i = 0; i < ds->num_ports; i++) {
445 if (!dsa_is_port_initialized(ds, i))
446 continue;
448 ret = dsa_slave_resume(ds->ports[i].netdev);
449 if (ret)
450 return ret;
453 return 0;
455 EXPORT_SYMBOL_GPL(dsa_switch_resume);
456 #endif
458 /* platform driver init and cleanup *****************************************/
459 static int dev_is_class(struct device *dev, void *class)
461 if (dev->class != NULL && !strcmp(dev->class->name, class))
462 return 1;
464 return 0;
467 static struct device *dev_find_class(struct device *parent, char *class)
469 if (dev_is_class(parent, class)) {
470 get_device(parent);
471 return parent;
474 return device_find_child(parent, class, dev_is_class);
477 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
479 struct device *d;
481 d = dev_find_class(dev, "mdio_bus");
482 if (d != NULL) {
483 struct mii_bus *bus;
485 bus = to_mii_bus(d);
486 put_device(d);
488 return bus;
491 return NULL;
493 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
495 struct net_device *dsa_dev_to_net_device(struct device *dev)
497 struct device *d;
499 d = dev_find_class(dev, "net");
500 if (d != NULL) {
501 struct net_device *nd;
503 nd = to_net_dev(d);
504 dev_hold(nd);
505 put_device(d);
507 return nd;
510 return NULL;
512 EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
514 #ifdef CONFIG_OF
515 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
516 struct dsa_chip_data *cd,
517 int chip_index, int port_index,
518 struct device_node *link)
520 const __be32 *reg;
521 int link_sw_addr;
522 struct device_node *parent_sw;
523 int len;
525 parent_sw = of_get_parent(link);
526 if (!parent_sw)
527 return -EINVAL;
529 reg = of_get_property(parent_sw, "reg", &len);
530 if (!reg || (len != sizeof(*reg) * 2))
531 return -EINVAL;
534 * Get the destination switch number from the second field of its 'reg'
535 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
537 link_sw_addr = be32_to_cpup(reg + 1);
539 if (link_sw_addr >= pd->nr_chips)
540 return -EINVAL;
542 cd->rtable[link_sw_addr] = port_index;
544 return 0;
547 static int dsa_of_probe_links(struct dsa_platform_data *pd,
548 struct dsa_chip_data *cd,
549 int chip_index, int port_index,
550 struct device_node *port,
551 const char *port_name)
553 struct device_node *link;
554 int link_index;
555 int ret;
557 for (link_index = 0;; link_index++) {
558 link = of_parse_phandle(port, "link", link_index);
559 if (!link)
560 break;
562 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
563 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
564 port_index, link);
565 if (ret)
566 return ret;
569 return 0;
572 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
574 int i;
575 int port_index;
577 for (i = 0; i < pd->nr_chips; i++) {
578 port_index = 0;
579 while (port_index < DSA_MAX_PORTS) {
580 kfree(pd->chip[i].port_names[port_index]);
581 port_index++;
584 /* Drop our reference to the MDIO bus device */
585 if (pd->chip[i].host_dev)
586 put_device(pd->chip[i].host_dev);
588 kfree(pd->chip);
591 static int dsa_of_probe(struct device *dev)
593 struct device_node *np = dev->of_node;
594 struct device_node *child, *mdio, *ethernet, *port;
595 struct mii_bus *mdio_bus, *mdio_bus_switch;
596 struct net_device *ethernet_dev;
597 struct dsa_platform_data *pd;
598 struct dsa_chip_data *cd;
599 const char *port_name;
600 int chip_index, port_index;
601 const unsigned int *sw_addr, *port_reg;
602 u32 eeprom_len;
603 int ret;
605 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
606 if (!mdio)
607 return -EINVAL;
609 mdio_bus = of_mdio_find_bus(mdio);
610 if (!mdio_bus)
611 return -EPROBE_DEFER;
613 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
614 if (!ethernet) {
615 ret = -EINVAL;
616 goto out_put_mdio;
619 ethernet_dev = of_find_net_device_by_node(ethernet);
620 if (!ethernet_dev) {
621 ret = -EPROBE_DEFER;
622 goto out_put_mdio;
625 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
626 if (!pd) {
627 ret = -ENOMEM;
628 goto out_put_ethernet;
631 dev->platform_data = pd;
632 pd->of_netdev = ethernet_dev;
633 pd->nr_chips = of_get_available_child_count(np);
634 if (pd->nr_chips > DSA_MAX_SWITCHES)
635 pd->nr_chips = DSA_MAX_SWITCHES;
637 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
638 GFP_KERNEL);
639 if (!pd->chip) {
640 ret = -ENOMEM;
641 goto out_free;
644 chip_index = -1;
645 for_each_available_child_of_node(np, child) {
646 int i;
648 chip_index++;
649 cd = &pd->chip[chip_index];
651 cd->of_node = child;
653 /* Initialize the routing table */
654 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
655 cd->rtable[i] = DSA_RTABLE_NONE;
657 /* When assigning the host device, increment its refcount */
658 cd->host_dev = get_device(&mdio_bus->dev);
660 sw_addr = of_get_property(child, "reg", NULL);
661 if (!sw_addr)
662 continue;
664 cd->sw_addr = be32_to_cpup(sw_addr);
665 if (cd->sw_addr >= PHY_MAX_ADDR)
666 continue;
668 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
669 cd->eeprom_len = eeprom_len;
671 mdio = of_parse_phandle(child, "mii-bus", 0);
672 if (mdio) {
673 mdio_bus_switch = of_mdio_find_bus(mdio);
674 if (!mdio_bus_switch) {
675 ret = -EPROBE_DEFER;
676 goto out_free_chip;
679 /* Drop the mdio_bus device ref, replacing the host
680 * device with the mdio_bus_switch device, keeping
681 * the refcount from of_mdio_find_bus() above.
683 put_device(cd->host_dev);
684 cd->host_dev = &mdio_bus_switch->dev;
687 for_each_available_child_of_node(child, port) {
688 port_reg = of_get_property(port, "reg", NULL);
689 if (!port_reg)
690 continue;
692 port_index = be32_to_cpup(port_reg);
693 if (port_index >= DSA_MAX_PORTS)
694 break;
696 port_name = of_get_property(port, "label", NULL);
697 if (!port_name)
698 continue;
700 cd->port_dn[port_index] = port;
702 cd->port_names[port_index] = kstrdup(port_name,
703 GFP_KERNEL);
704 if (!cd->port_names[port_index]) {
705 ret = -ENOMEM;
706 goto out_free_chip;
709 ret = dsa_of_probe_links(pd, cd, chip_index,
710 port_index, port, port_name);
711 if (ret)
712 goto out_free_chip;
717 /* The individual chips hold their own refcount on the mdio bus,
718 * so drop ours */
719 put_device(&mdio_bus->dev);
721 return 0;
723 out_free_chip:
724 dsa_of_free_platform_data(pd);
725 out_free:
726 kfree(pd);
727 dev->platform_data = NULL;
728 out_put_ethernet:
729 put_device(&ethernet_dev->dev);
730 out_put_mdio:
731 put_device(&mdio_bus->dev);
732 return ret;
735 static void dsa_of_remove(struct device *dev)
737 struct dsa_platform_data *pd = dev->platform_data;
739 if (!dev->of_node)
740 return;
742 dsa_of_free_platform_data(pd);
743 put_device(&pd->of_netdev->dev);
744 kfree(pd);
746 #else
747 static inline int dsa_of_probe(struct device *dev)
749 return 0;
752 static inline void dsa_of_remove(struct device *dev)
755 #endif
757 static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
758 struct device *parent, struct dsa_platform_data *pd)
760 int i;
761 unsigned configured = 0;
763 dst->pd = pd;
764 dst->master_netdev = dev;
765 dst->cpu_port = -1;
767 for (i = 0; i < pd->nr_chips; i++) {
768 struct dsa_switch *ds;
770 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
771 if (IS_ERR(ds)) {
772 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
773 i, PTR_ERR(ds));
774 continue;
777 dst->ds[i] = ds;
779 ++configured;
783 * If no switch was found, exit cleanly
785 if (!configured)
786 return -EPROBE_DEFER;
789 * If we use a tagging format that doesn't have an ethertype
790 * field, make sure that all packets from this point on get
791 * sent to the tag format's receive function.
793 wmb();
794 dev->dsa_ptr = (void *)dst;
796 return 0;
799 static int dsa_probe(struct platform_device *pdev)
801 struct dsa_platform_data *pd = pdev->dev.platform_data;
802 struct net_device *dev;
803 struct dsa_switch_tree *dst;
804 int ret;
806 if (pdev->dev.of_node) {
807 ret = dsa_of_probe(&pdev->dev);
808 if (ret)
809 return ret;
811 pd = pdev->dev.platform_data;
814 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
815 return -EINVAL;
817 if (pd->of_netdev) {
818 dev = pd->of_netdev;
819 dev_hold(dev);
820 } else {
821 dev = dsa_dev_to_net_device(pd->netdev);
823 if (dev == NULL) {
824 ret = -EPROBE_DEFER;
825 goto out;
828 if (dev->dsa_ptr != NULL) {
829 dev_put(dev);
830 ret = -EEXIST;
831 goto out;
834 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
835 if (dst == NULL) {
836 dev_put(dev);
837 ret = -ENOMEM;
838 goto out;
841 platform_set_drvdata(pdev, dst);
843 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
844 if (ret) {
845 dev_put(dev);
846 goto out;
849 return 0;
851 out:
852 dsa_of_remove(&pdev->dev);
854 return ret;
857 static void dsa_remove_dst(struct dsa_switch_tree *dst)
859 int i;
861 dst->master_netdev->dsa_ptr = NULL;
863 /* If we used a tagging format that doesn't have an ethertype
864 * field, make sure that all packets from this point get sent
865 * without the tag and go through the regular receive path.
867 wmb();
869 for (i = 0; i < dst->pd->nr_chips; i++) {
870 struct dsa_switch *ds = dst->ds[i];
872 if (ds)
873 dsa_switch_destroy(ds);
876 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
878 dev_put(dst->master_netdev);
881 static int dsa_remove(struct platform_device *pdev)
883 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
885 dsa_remove_dst(dst);
886 dsa_of_remove(&pdev->dev);
888 return 0;
891 static void dsa_shutdown(struct platform_device *pdev)
895 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
896 struct packet_type *pt, struct net_device *orig_dev)
898 struct dsa_switch_tree *dst = dev->dsa_ptr;
900 if (unlikely(dst == NULL)) {
901 kfree_skb(skb);
902 return 0;
905 return dst->rcv(skb, dev, pt, orig_dev);
908 static struct packet_type dsa_pack_type __read_mostly = {
909 .type = cpu_to_be16(ETH_P_XDSA),
910 .func = dsa_switch_rcv,
913 #ifdef CONFIG_PM_SLEEP
914 static int dsa_suspend(struct device *d)
916 struct platform_device *pdev = to_platform_device(d);
917 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
918 int i, ret = 0;
920 for (i = 0; i < dst->pd->nr_chips; i++) {
921 struct dsa_switch *ds = dst->ds[i];
923 if (ds != NULL)
924 ret = dsa_switch_suspend(ds);
927 return ret;
930 static int dsa_resume(struct device *d)
932 struct platform_device *pdev = to_platform_device(d);
933 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
934 int i, ret = 0;
936 for (i = 0; i < dst->pd->nr_chips; i++) {
937 struct dsa_switch *ds = dst->ds[i];
939 if (ds != NULL)
940 ret = dsa_switch_resume(ds);
943 return ret;
945 #endif
947 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
949 static const struct of_device_id dsa_of_match_table[] = {
950 { .compatible = "marvell,dsa", },
953 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
955 static struct platform_driver dsa_driver = {
956 .probe = dsa_probe,
957 .remove = dsa_remove,
958 .shutdown = dsa_shutdown,
959 .driver = {
960 .name = "dsa",
961 .of_match_table = dsa_of_match_table,
962 .pm = &dsa_pm_ops,
966 static int __init dsa_init_module(void)
968 int rc;
970 rc = dsa_slave_register_notifier();
971 if (rc)
972 return rc;
974 rc = platform_driver_register(&dsa_driver);
975 if (rc)
976 return rc;
978 dev_add_pack(&dsa_pack_type);
980 return 0;
982 module_init(dsa_init_module);
984 static void __exit dsa_cleanup_module(void)
986 dsa_slave_unregister_notifier();
987 dev_remove_pack(&dsa_pack_type);
988 platform_driver_unregister(&dsa_driver);
990 module_exit(dsa_cleanup_module);
992 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
993 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
994 MODULE_LICENSE("GPL");
995 MODULE_ALIAS("platform:dsa");