]> Git Repo - linux.git/blob - net/dsa/dsa.c
net: ipv6: Keep nexthop of multipath route on admin down
[linux.git] / net / dsa / dsa.c
1 /*
2  * net/dsa/dsa.c - Hardware switch handling
3  * Copyright (c) 2008-2009 Marvell Semiconductor
4  * Copyright (c) 2013 Florian Fainelli <[email protected]>
5  *
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.
10  */
11
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"
27
28 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
29                                             struct net_device *dev)
30 {
31         /* Just return the original SKB */
32         return skb;
33 }
34
35 static const struct dsa_device_ops none_ops = {
36         .xmit   = dsa_slave_notag_xmit,
37         .rcv    = NULL,
38 };
39
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,
57 };
58
59 /* switch driver registration ***********************************************/
60 static DEFINE_MUTEX(dsa_switch_drivers_mutex);
61 static LIST_HEAD(dsa_switch_drivers);
62
63 void register_switch_driver(struct dsa_switch_driver *drv)
64 {
65         mutex_lock(&dsa_switch_drivers_mutex);
66         list_add_tail(&drv->list, &dsa_switch_drivers);
67         mutex_unlock(&dsa_switch_drivers_mutex);
68 }
69 EXPORT_SYMBOL_GPL(register_switch_driver);
70
71 void unregister_switch_driver(struct dsa_switch_driver *drv)
72 {
73         mutex_lock(&dsa_switch_drivers_mutex);
74         list_del_init(&drv->list);
75         mutex_unlock(&dsa_switch_drivers_mutex);
76 }
77 EXPORT_SYMBOL_GPL(unregister_switch_driver);
78
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)
82 {
83         const struct dsa_switch_ops *ret;
84         struct list_head *list;
85         const char *name;
86
87         ret = NULL;
88         name = NULL;
89
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;
94
95                 drv = list_entry(list, struct dsa_switch_driver, list);
96                 ops = drv->ops;
97
98                 name = ops->probe(parent, host_dev, sw_addr, priv);
99                 if (name != NULL) {
100                         ret = ops;
101                         break;
102                 }
103         }
104         mutex_unlock(&dsa_switch_drivers_mutex);
105
106         *_name = name;
107
108         return ret;
109 }
110
111 /* basic switch operations **************************************************/
112 int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
113                       struct device_node *port_dn, int port)
114 {
115         struct phy_device *phydev;
116         int ret, mode;
117
118         if (of_phy_is_fixed_link(port_dn)) {
119                 ret = of_phy_register_fixed_link(port_dn);
120                 if (ret) {
121                         dev_err(dev, "failed to register fixed PHY\n");
122                         return ret;
123                 }
124                 phydev = of_phy_find_device(port_dn);
125
126                 mode = of_get_phy_mode(port_dn);
127                 if (mode < 0)
128                         mode = PHY_INTERFACE_MODE_NA;
129                 phydev->interface = mode;
130
131                 genphy_config_init(phydev);
132                 genphy_read_status(phydev);
133                 if (ds->ops->adjust_link)
134                         ds->ops->adjust_link(ds, port, phydev);
135
136                 put_device(&phydev->mdio.dev);
137         }
138
139         return 0;
140 }
141
142 static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
143 {
144         struct device_node *port_dn;
145         int ret, port;
146
147         for (port = 0; port < DSA_MAX_PORTS; port++) {
148                 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
149                         continue;
150
151                 port_dn = ds->ports[port].dn;
152                 ret = dsa_cpu_dsa_setup(ds, dev, port_dn, port);
153                 if (ret)
154                         return ret;
155         }
156         return 0;
157 }
158
159 const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
160 {
161         const struct dsa_device_ops *ops;
162
163         if (tag_protocol >= DSA_TAG_LAST)
164                 return ERR_PTR(-EINVAL);
165         ops = dsa_device_ops[tag_protocol];
166
167         if (!ops)
168                 return ERR_PTR(-ENOPROTOOPT);
169
170         return ops;
171 }
172
173 int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
174 {
175         struct net_device *master;
176         struct ethtool_ops *cpu_ops;
177
178         master = ds->dst->master_netdev;
179         if (ds->master_netdev)
180                 master = ds->master_netdev;
181
182         cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
183         if (!cpu_ops)
184                 return -ENOMEM;
185
186         memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
187                sizeof(struct ethtool_ops));
188         ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
189         memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
190                sizeof(struct ethtool_ops));
191         dsa_cpu_port_ethtool_init(cpu_ops);
192         master->ethtool_ops = cpu_ops;
193
194         return 0;
195 }
196
197 void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
198 {
199         struct net_device *master;
200
201         master = ds->dst->master_netdev;
202         if (ds->master_netdev)
203                 master = ds->master_netdev;
204
205         master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
206 }
207
208 static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
209 {
210         const struct dsa_switch_ops *ops = ds->ops;
211         struct dsa_switch_tree *dst = ds->dst;
212         struct dsa_chip_data *cd = ds->cd;
213         bool valid_name_found = false;
214         int index = ds->index;
215         int i, ret;
216
217         /*
218          * Validate supplied switch configuration.
219          */
220         for (i = 0; i < DSA_MAX_PORTS; i++) {
221                 char *name;
222
223                 name = cd->port_names[i];
224                 if (name == NULL)
225                         continue;
226
227                 if (!strcmp(name, "cpu")) {
228                         if (!dst->cpu_switch) {
229                                 netdev_err(dst->master_netdev,
230                                            "multiple cpu ports?!\n");
231                                 return -EINVAL;
232                         }
233                         dst->cpu_switch = ds;
234                         dst->cpu_port = i;
235                         ds->cpu_port_mask |= 1 << i;
236                 } else if (!strcmp(name, "dsa")) {
237                         ds->dsa_port_mask |= 1 << i;
238                 } else {
239                         ds->enabled_port_mask |= 1 << i;
240                 }
241                 valid_name_found = true;
242         }
243
244         if (!valid_name_found && i == DSA_MAX_PORTS)
245                 return -EINVAL;
246
247         /* Make the built-in MII bus mask match the number of ports,
248          * switch drivers can override this later
249          */
250         ds->phys_mii_mask = ds->enabled_port_mask;
251
252         /*
253          * If the CPU connects to this switch, set the switch tree
254          * tagging protocol to the preferred tagging format of this
255          * switch.
256          */
257         if (dst->cpu_switch == ds) {
258                 enum dsa_tag_protocol tag_protocol;
259
260                 tag_protocol = ops->get_tag_protocol(ds);
261                 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
262                 if (IS_ERR(dst->tag_ops))
263                         return PTR_ERR(dst->tag_ops);
264
265                 dst->rcv = dst->tag_ops->rcv;
266         }
267
268         memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
269
270         /*
271          * Do basic register setup.
272          */
273         ret = ops->setup(ds);
274         if (ret < 0)
275                 return ret;
276
277         if (ops->set_addr) {
278                 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
279                 if (ret < 0)
280                         return ret;
281         }
282
283         if (!ds->slave_mii_bus && ops->phy_read) {
284                 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
285                 if (!ds->slave_mii_bus)
286                         return -ENOMEM;
287                 dsa_slave_mii_bus_init(ds);
288
289                 ret = mdiobus_register(ds->slave_mii_bus);
290                 if (ret < 0)
291                         return ret;
292         }
293
294         /*
295          * Create network devices for physical switch ports.
296          */
297         for (i = 0; i < DSA_MAX_PORTS; i++) {
298                 ds->ports[i].dn = cd->port_dn[i];
299
300                 if (!(ds->enabled_port_mask & (1 << i)))
301                         continue;
302
303                 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
304                 if (ret < 0)
305                         netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
306                                    index, i, cd->port_names[i], ret);
307         }
308
309         /* Perform configuration of the CPU and DSA ports */
310         ret = dsa_cpu_dsa_setups(ds, parent);
311         if (ret < 0)
312                 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
313                            index);
314
315         ret = dsa_cpu_port_ethtool_setup(ds);
316         if (ret)
317                 return ret;
318
319         dsa_hwmon_register(ds);
320
321         return 0;
322 }
323
324 static struct dsa_switch *
325 dsa_switch_setup(struct dsa_switch_tree *dst, int index,
326                  struct device *parent, struct device *host_dev)
327 {
328         struct dsa_chip_data *cd = dst->pd->chip + index;
329         const struct dsa_switch_ops *ops;
330         struct dsa_switch *ds;
331         int ret;
332         const char *name;
333         void *priv;
334
335         /*
336          * Probe for switch model.
337          */
338         ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
339         if (!ops) {
340                 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
341                            index);
342                 return ERR_PTR(-EINVAL);
343         }
344         netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
345                     index, name);
346
347
348         /*
349          * Allocate and initialise switch state.
350          */
351         ds = devm_kzalloc(parent, sizeof(*ds), GFP_KERNEL);
352         if (ds == NULL)
353                 return ERR_PTR(-ENOMEM);
354
355         ds->dst = dst;
356         ds->index = index;
357         ds->cd = cd;
358         ds->ops = ops;
359         ds->priv = priv;
360         ds->dev = parent;
361
362         ret = dsa_switch_setup_one(ds, parent);
363         if (ret)
364                 return ERR_PTR(ret);
365
366         return ds;
367 }
368
369 void dsa_cpu_dsa_destroy(struct device_node *port_dn)
370 {
371         if (of_phy_is_fixed_link(port_dn))
372                 of_phy_deregister_fixed_link(port_dn);
373 }
374
375 static void dsa_switch_destroy(struct dsa_switch *ds)
376 {
377         int port;
378
379         dsa_hwmon_unregister(ds);
380
381         /* Destroy network devices for physical switch ports. */
382         for (port = 0; port < DSA_MAX_PORTS; port++) {
383                 if (!(ds->enabled_port_mask & (1 << port)))
384                         continue;
385
386                 if (!ds->ports[port].netdev)
387                         continue;
388
389                 dsa_slave_destroy(ds->ports[port].netdev);
390         }
391
392         /* Disable configuration of the CPU and DSA ports */
393         for (port = 0; port < DSA_MAX_PORTS; port++) {
394                 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
395                         continue;
396                 dsa_cpu_dsa_destroy(ds->ports[port].dn);
397
398                 /* Clearing a bit which is not set does no harm */
399                 ds->cpu_port_mask |= ~(1 << port);
400                 ds->dsa_port_mask |= ~(1 << port);
401         }
402
403         if (ds->slave_mii_bus && ds->ops->phy_read)
404                 mdiobus_unregister(ds->slave_mii_bus);
405 }
406
407 #ifdef CONFIG_PM_SLEEP
408 int dsa_switch_suspend(struct dsa_switch *ds)
409 {
410         int i, ret = 0;
411
412         /* Suspend slave network devices */
413         for (i = 0; i < DSA_MAX_PORTS; i++) {
414                 if (!dsa_is_port_initialized(ds, i))
415                         continue;
416
417                 ret = dsa_slave_suspend(ds->ports[i].netdev);
418                 if (ret)
419                         return ret;
420         }
421
422         if (ds->ops->suspend)
423                 ret = ds->ops->suspend(ds);
424
425         return ret;
426 }
427 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
428
429 int dsa_switch_resume(struct dsa_switch *ds)
430 {
431         int i, ret = 0;
432
433         if (ds->ops->resume)
434                 ret = ds->ops->resume(ds);
435
436         if (ret)
437                 return ret;
438
439         /* Resume slave network devices */
440         for (i = 0; i < DSA_MAX_PORTS; i++) {
441                 if (!dsa_is_port_initialized(ds, i))
442                         continue;
443
444                 ret = dsa_slave_resume(ds->ports[i].netdev);
445                 if (ret)
446                         return ret;
447         }
448
449         return 0;
450 }
451 EXPORT_SYMBOL_GPL(dsa_switch_resume);
452 #endif
453
454 /* platform driver init and cleanup *****************************************/
455 static int dev_is_class(struct device *dev, void *class)
456 {
457         if (dev->class != NULL && !strcmp(dev->class->name, class))
458                 return 1;
459
460         return 0;
461 }
462
463 static struct device *dev_find_class(struct device *parent, char *class)
464 {
465         if (dev_is_class(parent, class)) {
466                 get_device(parent);
467                 return parent;
468         }
469
470         return device_find_child(parent, class, dev_is_class);
471 }
472
473 struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
474 {
475         struct device *d;
476
477         d = dev_find_class(dev, "mdio_bus");
478         if (d != NULL) {
479                 struct mii_bus *bus;
480
481                 bus = to_mii_bus(d);
482                 put_device(d);
483
484                 return bus;
485         }
486
487         return NULL;
488 }
489 EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
490
491 static struct net_device *dev_to_net_device(struct device *dev)
492 {
493         struct device *d;
494
495         d = dev_find_class(dev, "net");
496         if (d != NULL) {
497                 struct net_device *nd;
498
499                 nd = to_net_dev(d);
500                 dev_hold(nd);
501                 put_device(d);
502
503                 return nd;
504         }
505
506         return NULL;
507 }
508
509 #ifdef CONFIG_OF
510 static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
511                                         struct dsa_chip_data *cd,
512                                         int chip_index, int port_index,
513                                         struct device_node *link)
514 {
515         const __be32 *reg;
516         int link_sw_addr;
517         struct device_node *parent_sw;
518         int len;
519
520         parent_sw = of_get_parent(link);
521         if (!parent_sw)
522                 return -EINVAL;
523
524         reg = of_get_property(parent_sw, "reg", &len);
525         if (!reg || (len != sizeof(*reg) * 2))
526                 return -EINVAL;
527
528         /*
529          * Get the destination switch number from the second field of its 'reg'
530          * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
531          */
532         link_sw_addr = be32_to_cpup(reg + 1);
533
534         if (link_sw_addr >= pd->nr_chips)
535                 return -EINVAL;
536
537         cd->rtable[link_sw_addr] = port_index;
538
539         return 0;
540 }
541
542 static int dsa_of_probe_links(struct dsa_platform_data *pd,
543                               struct dsa_chip_data *cd,
544                               int chip_index, int port_index,
545                               struct device_node *port,
546                               const char *port_name)
547 {
548         struct device_node *link;
549         int link_index;
550         int ret;
551
552         for (link_index = 0;; link_index++) {
553                 link = of_parse_phandle(port, "link", link_index);
554                 if (!link)
555                         break;
556
557                 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
558                         ret = dsa_of_setup_routing_table(pd, cd, chip_index,
559                                                          port_index, link);
560                         if (ret)
561                                 return ret;
562                 }
563         }
564         return 0;
565 }
566
567 static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
568 {
569         int i;
570         int port_index;
571
572         for (i = 0; i < pd->nr_chips; i++) {
573                 port_index = 0;
574                 while (port_index < DSA_MAX_PORTS) {
575                         kfree(pd->chip[i].port_names[port_index]);
576                         port_index++;
577                 }
578
579                 /* Drop our reference to the MDIO bus device */
580                 if (pd->chip[i].host_dev)
581                         put_device(pd->chip[i].host_dev);
582         }
583         kfree(pd->chip);
584 }
585
586 static int dsa_of_probe(struct device *dev)
587 {
588         struct device_node *np = dev->of_node;
589         struct device_node *child, *mdio, *ethernet, *port;
590         struct mii_bus *mdio_bus, *mdio_bus_switch;
591         struct net_device *ethernet_dev;
592         struct dsa_platform_data *pd;
593         struct dsa_chip_data *cd;
594         const char *port_name;
595         int chip_index, port_index;
596         const unsigned int *sw_addr, *port_reg;
597         u32 eeprom_len;
598         int ret;
599
600         mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
601         if (!mdio)
602                 return -EINVAL;
603
604         mdio_bus = of_mdio_find_bus(mdio);
605         if (!mdio_bus)
606                 return -EPROBE_DEFER;
607
608         ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
609         if (!ethernet) {
610                 ret = -EINVAL;
611                 goto out_put_mdio;
612         }
613
614         ethernet_dev = of_find_net_device_by_node(ethernet);
615         if (!ethernet_dev) {
616                 ret = -EPROBE_DEFER;
617                 goto out_put_mdio;
618         }
619
620         pd = kzalloc(sizeof(*pd), GFP_KERNEL);
621         if (!pd) {
622                 ret = -ENOMEM;
623                 goto out_put_ethernet;
624         }
625
626         dev->platform_data = pd;
627         pd->of_netdev = ethernet_dev;
628         pd->nr_chips = of_get_available_child_count(np);
629         if (pd->nr_chips > DSA_MAX_SWITCHES)
630                 pd->nr_chips = DSA_MAX_SWITCHES;
631
632         pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
633                            GFP_KERNEL);
634         if (!pd->chip) {
635                 ret = -ENOMEM;
636                 goto out_free;
637         }
638
639         chip_index = -1;
640         for_each_available_child_of_node(np, child) {
641                 int i;
642
643                 chip_index++;
644                 cd = &pd->chip[chip_index];
645
646                 cd->of_node = child;
647
648                 /* Initialize the routing table */
649                 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
650                         cd->rtable[i] = DSA_RTABLE_NONE;
651
652                 /* When assigning the host device, increment its refcount */
653                 cd->host_dev = get_device(&mdio_bus->dev);
654
655                 sw_addr = of_get_property(child, "reg", NULL);
656                 if (!sw_addr)
657                         continue;
658
659                 cd->sw_addr = be32_to_cpup(sw_addr);
660                 if (cd->sw_addr >= PHY_MAX_ADDR)
661                         continue;
662
663                 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
664                         cd->eeprom_len = eeprom_len;
665
666                 mdio = of_parse_phandle(child, "mii-bus", 0);
667                 if (mdio) {
668                         mdio_bus_switch = of_mdio_find_bus(mdio);
669                         if (!mdio_bus_switch) {
670                                 ret = -EPROBE_DEFER;
671                                 goto out_free_chip;
672                         }
673
674                         /* Drop the mdio_bus device ref, replacing the host
675                          * device with the mdio_bus_switch device, keeping
676                          * the refcount from of_mdio_find_bus() above.
677                          */
678                         put_device(cd->host_dev);
679                         cd->host_dev = &mdio_bus_switch->dev;
680                 }
681
682                 for_each_available_child_of_node(child, port) {
683                         port_reg = of_get_property(port, "reg", NULL);
684                         if (!port_reg)
685                                 continue;
686
687                         port_index = be32_to_cpup(port_reg);
688                         if (port_index >= DSA_MAX_PORTS)
689                                 break;
690
691                         port_name = of_get_property(port, "label", NULL);
692                         if (!port_name)
693                                 continue;
694
695                         cd->port_dn[port_index] = port;
696
697                         cd->port_names[port_index] = kstrdup(port_name,
698                                         GFP_KERNEL);
699                         if (!cd->port_names[port_index]) {
700                                 ret = -ENOMEM;
701                                 goto out_free_chip;
702                         }
703
704                         ret = dsa_of_probe_links(pd, cd, chip_index,
705                                                  port_index, port, port_name);
706                         if (ret)
707                                 goto out_free_chip;
708
709                 }
710         }
711
712         /* The individual chips hold their own refcount on the mdio bus,
713          * so drop ours */
714         put_device(&mdio_bus->dev);
715
716         return 0;
717
718 out_free_chip:
719         dsa_of_free_platform_data(pd);
720 out_free:
721         kfree(pd);
722         dev->platform_data = NULL;
723 out_put_ethernet:
724         put_device(&ethernet_dev->dev);
725 out_put_mdio:
726         put_device(&mdio_bus->dev);
727         return ret;
728 }
729
730 static void dsa_of_remove(struct device *dev)
731 {
732         struct dsa_platform_data *pd = dev->platform_data;
733
734         if (!dev->of_node)
735                 return;
736
737         dsa_of_free_platform_data(pd);
738         put_device(&pd->of_netdev->dev);
739         kfree(pd);
740 }
741 #else
742 static inline int dsa_of_probe(struct device *dev)
743 {
744         return 0;
745 }
746
747 static inline void dsa_of_remove(struct device *dev)
748 {
749 }
750 #endif
751
752 static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
753                          struct device *parent, struct dsa_platform_data *pd)
754 {
755         int i;
756         unsigned configured = 0;
757
758         dst->pd = pd;
759         dst->master_netdev = dev;
760         dst->cpu_port = -1;
761
762         for (i = 0; i < pd->nr_chips; i++) {
763                 struct dsa_switch *ds;
764
765                 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
766                 if (IS_ERR(ds)) {
767                         netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
768                                    i, PTR_ERR(ds));
769                         continue;
770                 }
771
772                 dst->ds[i] = ds;
773
774                 ++configured;
775         }
776
777         /*
778          * If no switch was found, exit cleanly
779          */
780         if (!configured)
781                 return -EPROBE_DEFER;
782
783         /*
784          * If we use a tagging format that doesn't have an ethertype
785          * field, make sure that all packets from this point on get
786          * sent to the tag format's receive function.
787          */
788         wmb();
789         dev->dsa_ptr = (void *)dst;
790
791         return 0;
792 }
793
794 static int dsa_probe(struct platform_device *pdev)
795 {
796         struct dsa_platform_data *pd = pdev->dev.platform_data;
797         struct net_device *dev;
798         struct dsa_switch_tree *dst;
799         int ret;
800
801         if (pdev->dev.of_node) {
802                 ret = dsa_of_probe(&pdev->dev);
803                 if (ret)
804                         return ret;
805
806                 pd = pdev->dev.platform_data;
807         }
808
809         if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
810                 return -EINVAL;
811
812         if (pd->of_netdev) {
813                 dev = pd->of_netdev;
814                 dev_hold(dev);
815         } else {
816                 dev = dev_to_net_device(pd->netdev);
817         }
818         if (dev == NULL) {
819                 ret = -EPROBE_DEFER;
820                 goto out;
821         }
822
823         if (dev->dsa_ptr != NULL) {
824                 dev_put(dev);
825                 ret = -EEXIST;
826                 goto out;
827         }
828
829         dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
830         if (dst == NULL) {
831                 dev_put(dev);
832                 ret = -ENOMEM;
833                 goto out;
834         }
835
836         platform_set_drvdata(pdev, dst);
837
838         ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
839         if (ret) {
840                 dev_put(dev);
841                 goto out;
842         }
843
844         return 0;
845
846 out:
847         dsa_of_remove(&pdev->dev);
848
849         return ret;
850 }
851
852 static void dsa_remove_dst(struct dsa_switch_tree *dst)
853 {
854         int i;
855
856         dst->master_netdev->dsa_ptr = NULL;
857
858         /* If we used a tagging format that doesn't have an ethertype
859          * field, make sure that all packets from this point get sent
860          * without the tag and go through the regular receive path.
861          */
862         wmb();
863
864         for (i = 0; i < dst->pd->nr_chips; i++) {
865                 struct dsa_switch *ds = dst->ds[i];
866
867                 if (ds)
868                         dsa_switch_destroy(ds);
869         }
870
871         dsa_cpu_port_ethtool_restore(dst->cpu_switch);
872
873         dev_put(dst->master_netdev);
874 }
875
876 static int dsa_remove(struct platform_device *pdev)
877 {
878         struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
879
880         dsa_remove_dst(dst);
881         dsa_of_remove(&pdev->dev);
882
883         return 0;
884 }
885
886 static void dsa_shutdown(struct platform_device *pdev)
887 {
888 }
889
890 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
891                           struct packet_type *pt, struct net_device *orig_dev)
892 {
893         struct dsa_switch_tree *dst = dev->dsa_ptr;
894
895         if (unlikely(dst == NULL)) {
896                 kfree_skb(skb);
897                 return 0;
898         }
899
900         return dst->rcv(skb, dev, pt, orig_dev);
901 }
902
903 static struct packet_type dsa_pack_type __read_mostly = {
904         .type   = cpu_to_be16(ETH_P_XDSA),
905         .func   = dsa_switch_rcv,
906 };
907
908 static struct notifier_block dsa_netdevice_nb __read_mostly = {
909         .notifier_call  = dsa_slave_netdevice_event,
910 };
911
912 #ifdef CONFIG_PM_SLEEP
913 static int dsa_suspend(struct device *d)
914 {
915         struct platform_device *pdev = to_platform_device(d);
916         struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
917         int i, ret = 0;
918
919         for (i = 0; i < dst->pd->nr_chips; i++) {
920                 struct dsa_switch *ds = dst->ds[i];
921
922                 if (ds != NULL)
923                         ret = dsa_switch_suspend(ds);
924         }
925
926         return ret;
927 }
928
929 static int dsa_resume(struct device *d)
930 {
931         struct platform_device *pdev = to_platform_device(d);
932         struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
933         int i, ret = 0;
934
935         for (i = 0; i < dst->pd->nr_chips; i++) {
936                 struct dsa_switch *ds = dst->ds[i];
937
938                 if (ds != NULL)
939                         ret = dsa_switch_resume(ds);
940         }
941
942         return ret;
943 }
944 #endif
945
946 static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
947
948 static const struct of_device_id dsa_of_match_table[] = {
949         { .compatible = "marvell,dsa", },
950         {}
951 };
952 MODULE_DEVICE_TABLE(of, dsa_of_match_table);
953
954 static struct platform_driver dsa_driver = {
955         .probe          = dsa_probe,
956         .remove         = dsa_remove,
957         .shutdown       = dsa_shutdown,
958         .driver = {
959                 .name   = "dsa",
960                 .of_match_table = dsa_of_match_table,
961                 .pm     = &dsa_pm_ops,
962         },
963 };
964
965 static int __init dsa_init_module(void)
966 {
967         int rc;
968
969         register_netdevice_notifier(&dsa_netdevice_nb);
970
971         rc = platform_driver_register(&dsa_driver);
972         if (rc)
973                 return rc;
974
975         dev_add_pack(&dsa_pack_type);
976
977         return 0;
978 }
979 module_init(dsa_init_module);
980
981 static void __exit dsa_cleanup_module(void)
982 {
983         unregister_netdevice_notifier(&dsa_netdevice_nb);
984         dev_remove_pack(&dsa_pack_type);
985         platform_driver_unregister(&dsa_driver);
986 }
987 module_exit(dsa_cleanup_module);
988
989 MODULE_AUTHOR("Lennert Buytenhek <[email protected]>");
990 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
991 MODULE_LICENSE("GPL");
992 MODULE_ALIAS("platform:dsa");
This page took 0.085731 seconds and 4 git commands to generate.