blob: 4d374541815ac50ccfb290073582017dc4adf338 [file] [log] [blame]
Vivien Didelota6a71f12017-04-12 12:45:03 -04001/*
2 * net/dsa/legacy.c - Hardware switch handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
4 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
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 <linux/of.h>
18#include <linux/of_mdio.h>
19#include <linux/of_platform.h>
20#include <linux/of_net.h>
21#include <linux/netdevice.h>
22#include <linux/sysfs.h>
23#include <linux/phy_fixed.h>
24#include <linux/etherdevice.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040025
Vivien Didelota6a71f12017-04-12 12:45:03 -040026#include "dsa_priv.h"
27
28/* switch driver registration ***********************************************/
29static DEFINE_MUTEX(dsa_switch_drivers_mutex);
30static LIST_HEAD(dsa_switch_drivers);
31
32void register_switch_driver(struct dsa_switch_driver *drv)
33{
34 mutex_lock(&dsa_switch_drivers_mutex);
35 list_add_tail(&drv->list, &dsa_switch_drivers);
36 mutex_unlock(&dsa_switch_drivers_mutex);
37}
38EXPORT_SYMBOL_GPL(register_switch_driver);
39
40void unregister_switch_driver(struct dsa_switch_driver *drv)
41{
42 mutex_lock(&dsa_switch_drivers_mutex);
43 list_del_init(&drv->list);
44 mutex_unlock(&dsa_switch_drivers_mutex);
45}
46EXPORT_SYMBOL_GPL(unregister_switch_driver);
47
48static const struct dsa_switch_ops *
49dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
50 const char **_name, void **priv)
51{
52 const struct dsa_switch_ops *ret;
53 struct list_head *list;
54 const char *name;
55
56 ret = NULL;
57 name = NULL;
58
59 mutex_lock(&dsa_switch_drivers_mutex);
60 list_for_each(list, &dsa_switch_drivers) {
61 const struct dsa_switch_ops *ops;
62 struct dsa_switch_driver *drv;
63
64 drv = list_entry(list, struct dsa_switch_driver, list);
65 ops = drv->ops;
66
67 name = ops->probe(parent, host_dev, sw_addr, priv);
68 if (name != NULL) {
69 ret = ops;
70 break;
71 }
72 }
73 mutex_unlock(&dsa_switch_drivers_mutex);
74
75 *_name = name;
76
77 return ret;
78}
79
80/* basic switch operations **************************************************/
Vivien Didelot206e41f2017-08-05 16:20:17 -040081static int dsa_cpu_dsa_setups(struct dsa_switch *ds)
Vivien Didelota6a71f12017-04-12 12:45:03 -040082{
Vivien Didelota6a71f12017-04-12 12:45:03 -040083 int ret, port;
84
85 for (port = 0; port < ds->num_ports; port++) {
86 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
87 continue;
88
Vivien Didelot47d0dcc2017-08-05 16:20:18 -040089 ret = dsa_cpu_dsa_setup(&ds->ports[port]);
Vivien Didelota6a71f12017-04-12 12:45:03 -040090 if (ret)
91 return ret;
92 }
93 return 0;
94}
95
Vivien Didelot206e41f2017-08-05 16:20:17 -040096static int dsa_switch_setup_one(struct dsa_switch *ds,
97 struct net_device *master)
Vivien Didelota6a71f12017-04-12 12:45:03 -040098{
99 const struct dsa_switch_ops *ops = ds->ops;
100 struct dsa_switch_tree *dst = ds->dst;
101 struct dsa_chip_data *cd = ds->cd;
102 bool valid_name_found = false;
103 int index = ds->index;
104 int i, ret;
105
106 /*
107 * Validate supplied switch configuration.
108 */
109 for (i = 0; i < ds->num_ports; i++) {
110 char *name;
111
112 name = cd->port_names[i];
113 if (name == NULL)
114 continue;
115
116 if (!strcmp(name, "cpu")) {
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400117 if (dst->cpu_dp) {
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700118 netdev_err(master,
Vivien Didelota6a71f12017-04-12 12:45:03 -0400119 "multiple cpu ports?!\n");
120 return -EINVAL;
121 }
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400122 dst->cpu_dp = &ds->ports[i];
Florian Fainelli06d4d452017-06-16 16:42:11 -0700123 dst->cpu_dp->netdev = master;
Vivien Didelota6a71f12017-04-12 12:45:03 -0400124 ds->cpu_port_mask |= 1 << i;
125 } else if (!strcmp(name, "dsa")) {
126 ds->dsa_port_mask |= 1 << i;
127 } else {
128 ds->enabled_port_mask |= 1 << i;
129 }
130 valid_name_found = true;
131 }
132
133 if (!valid_name_found && i == ds->num_ports)
134 return -EINVAL;
135
136 /* Make the built-in MII bus mask match the number of ports,
137 * switch drivers can override this later
138 */
139 ds->phys_mii_mask = ds->enabled_port_mask;
140
141 /*
142 * If the CPU connects to this switch, set the switch tree
143 * tagging protocol to the preferred tagging format of this
144 * switch.
145 */
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400146 if (dst->cpu_dp->ds == ds) {
Vivien Didelot62fc9582017-09-29 17:19:17 -0400147 const struct dsa_device_ops *tag_ops;
Vivien Didelota6a71f12017-04-12 12:45:03 -0400148 enum dsa_tag_protocol tag_protocol;
149
150 tag_protocol = ops->get_tag_protocol(ds);
Vivien Didelot62fc9582017-09-29 17:19:17 -0400151 tag_ops = dsa_resolve_tag_protocol(tag_protocol);
152 if (IS_ERR(tag_ops))
153 return PTR_ERR(tag_ops);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400154
Vivien Didelot15240242017-09-29 17:19:18 -0400155 dst->cpu_dp->tag_ops = tag_ops;
Vivien Didelot62fc9582017-09-29 17:19:17 -0400156 dst->tag_ops = tag_ops;
Vivien Didelota6a71f12017-04-12 12:45:03 -0400157 dst->rcv = dst->tag_ops->rcv;
158 }
159
160 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
161
162 /*
163 * Do basic register setup.
164 */
165 ret = ops->setup(ds);
166 if (ret < 0)
167 return ret;
168
169 ret = dsa_switch_register_notifier(ds);
170 if (ret)
171 return ret;
172
173 if (ops->set_addr) {
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700174 ret = ops->set_addr(ds, master->dev_addr);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400175 if (ret < 0)
176 return ret;
177 }
178
179 if (!ds->slave_mii_bus && ops->phy_read) {
Vivien Didelot206e41f2017-08-05 16:20:17 -0400180 ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400181 if (!ds->slave_mii_bus)
182 return -ENOMEM;
183 dsa_slave_mii_bus_init(ds);
184
185 ret = mdiobus_register(ds->slave_mii_bus);
186 if (ret < 0)
187 return ret;
188 }
189
190 /*
191 * Create network devices for physical switch ports.
192 */
193 for (i = 0; i < ds->num_ports; i++) {
194 ds->ports[i].dn = cd->port_dn[i];
Florian Fainelli06d4d452017-06-16 16:42:11 -0700195 ds->ports[i].cpu_dp = dst->cpu_dp;
Vivien Didelota6a71f12017-04-12 12:45:03 -0400196
197 if (!(ds->enabled_port_mask & (1 << i)))
198 continue;
199
Vivien Didelot4cfbf092017-08-05 16:20:19 -0400200 ret = dsa_slave_create(&ds->ports[i], cd->port_names[i]);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400201 if (ret < 0)
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700202 netdev_err(master, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
Vivien Didelota6a71f12017-04-12 12:45:03 -0400203 index, i, cd->port_names[i], ret);
204 }
205
206 /* Perform configuration of the CPU and DSA ports */
Vivien Didelot206e41f2017-08-05 16:20:17 -0400207 ret = dsa_cpu_dsa_setups(ds);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400208 if (ret < 0)
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700209 netdev_err(master, "[%d] : can't configure CPU and DSA ports\n",
Vivien Didelota6a71f12017-04-12 12:45:03 -0400210 index);
211
Vivien Didelota6a71f12017-04-12 12:45:03 -0400212 return 0;
213}
214
215static struct dsa_switch *
Florian Fainelli06d4d452017-06-16 16:42:11 -0700216dsa_switch_setup(struct dsa_switch_tree *dst, struct net_device *master,
217 int index, struct device *parent, struct device *host_dev)
Vivien Didelota6a71f12017-04-12 12:45:03 -0400218{
219 struct dsa_chip_data *cd = dst->pd->chip + index;
220 const struct dsa_switch_ops *ops;
221 struct dsa_switch *ds;
222 int ret;
223 const char *name;
224 void *priv;
225
226 /*
227 * Probe for switch model.
228 */
229 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
230 if (!ops) {
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700231 netdev_err(master, "[%d]: could not detect attached switch\n",
Vivien Didelota6a71f12017-04-12 12:45:03 -0400232 index);
233 return ERR_PTR(-EINVAL);
234 }
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700235 netdev_info(master, "[%d]: detected a %s switch\n",
Vivien Didelota6a71f12017-04-12 12:45:03 -0400236 index, name);
237
238
239 /*
240 * Allocate and initialise switch state.
241 */
242 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
243 if (!ds)
244 return ERR_PTR(-ENOMEM);
245
246 ds->dst = dst;
247 ds->index = index;
248 ds->cd = cd;
249 ds->ops = ops;
250 ds->priv = priv;
251
Vivien Didelot206e41f2017-08-05 16:20:17 -0400252 ret = dsa_switch_setup_one(ds, master);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400253 if (ret)
254 return ERR_PTR(ret);
255
256 return ds;
257}
258
259static void dsa_switch_destroy(struct dsa_switch *ds)
260{
261 int port;
262
263 /* Destroy network devices for physical switch ports. */
264 for (port = 0; port < ds->num_ports; port++) {
265 if (!(ds->enabled_port_mask & (1 << port)))
266 continue;
267
268 if (!ds->ports[port].netdev)
269 continue;
270
271 dsa_slave_destroy(ds->ports[port].netdev);
272 }
273
274 /* Disable configuration of the CPU and DSA ports */
275 for (port = 0; port < ds->num_ports; port++) {
276 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
277 continue;
278 dsa_cpu_dsa_destroy(&ds->ports[port]);
279
280 /* Clearing a bit which is not set does no harm */
281 ds->cpu_port_mask |= ~(1 << port);
282 ds->dsa_port_mask |= ~(1 << port);
283 }
284
285 if (ds->slave_mii_bus && ds->ops->phy_read)
286 mdiobus_unregister(ds->slave_mii_bus);
287
288 dsa_switch_unregister_notifier(ds);
289}
290
Vivien Didelota6a71f12017-04-12 12:45:03 -0400291/* platform driver init and cleanup *****************************************/
292static int dev_is_class(struct device *dev, void *class)
293{
294 if (dev->class != NULL && !strcmp(dev->class->name, class))
295 return 1;
296
297 return 0;
298}
299
300static struct device *dev_find_class(struct device *parent, char *class)
301{
302 if (dev_is_class(parent, class)) {
303 get_device(parent);
304 return parent;
305 }
306
307 return device_find_child(parent, class, dev_is_class);
308}
309
310struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
311{
312 struct device *d;
313
314 d = dev_find_class(dev, "mdio_bus");
315 if (d != NULL) {
316 struct mii_bus *bus;
317
318 bus = to_mii_bus(d);
319 put_device(d);
320
321 return bus;
322 }
323
324 return NULL;
325}
326EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
327
328#ifdef CONFIG_OF
329static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
330 struct dsa_chip_data *cd,
331 int chip_index, int port_index,
332 struct device_node *link)
333{
334 const __be32 *reg;
335 int link_sw_addr;
336 struct device_node *parent_sw;
337 int len;
338
339 parent_sw = of_get_parent(link);
340 if (!parent_sw)
341 return -EINVAL;
342
343 reg = of_get_property(parent_sw, "reg", &len);
344 if (!reg || (len != sizeof(*reg) * 2))
345 return -EINVAL;
346
347 /*
348 * Get the destination switch number from the second field of its 'reg'
349 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
350 */
351 link_sw_addr = be32_to_cpup(reg + 1);
352
353 if (link_sw_addr >= pd->nr_chips)
354 return -EINVAL;
355
356 cd->rtable[link_sw_addr] = port_index;
357
358 return 0;
359}
360
361static int dsa_of_probe_links(struct dsa_platform_data *pd,
362 struct dsa_chip_data *cd,
363 int chip_index, int port_index,
364 struct device_node *port,
365 const char *port_name)
366{
367 struct device_node *link;
368 int link_index;
369 int ret;
370
371 for (link_index = 0;; link_index++) {
372 link = of_parse_phandle(port, "link", link_index);
373 if (!link)
374 break;
375
376 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
377 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
378 port_index, link);
379 if (ret)
380 return ret;
381 }
382 }
383 return 0;
384}
385
386static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
387{
388 int i;
389 int port_index;
390
391 for (i = 0; i < pd->nr_chips; i++) {
392 port_index = 0;
393 while (port_index < DSA_MAX_PORTS) {
394 kfree(pd->chip[i].port_names[port_index]);
395 port_index++;
396 }
397
398 /* Drop our reference to the MDIO bus device */
399 if (pd->chip[i].host_dev)
400 put_device(pd->chip[i].host_dev);
401 }
402 kfree(pd->chip);
403}
404
405static int dsa_of_probe(struct device *dev)
406{
407 struct device_node *np = dev->of_node;
408 struct device_node *child, *mdio, *ethernet, *port;
409 struct mii_bus *mdio_bus, *mdio_bus_switch;
410 struct net_device *ethernet_dev;
411 struct dsa_platform_data *pd;
412 struct dsa_chip_data *cd;
413 const char *port_name;
414 int chip_index, port_index;
415 const unsigned int *sw_addr, *port_reg;
416 u32 eeprom_len;
417 int ret;
418
419 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
420 if (!mdio)
421 return -EINVAL;
422
423 mdio_bus = of_mdio_find_bus(mdio);
424 if (!mdio_bus)
425 return -EPROBE_DEFER;
426
427 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
428 if (!ethernet) {
429 ret = -EINVAL;
430 goto out_put_mdio;
431 }
432
433 ethernet_dev = of_find_net_device_by_node(ethernet);
434 if (!ethernet_dev) {
435 ret = -EPROBE_DEFER;
436 goto out_put_mdio;
437 }
438
439 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
440 if (!pd) {
441 ret = -ENOMEM;
442 goto out_put_ethernet;
443 }
444
445 dev->platform_data = pd;
446 pd->of_netdev = ethernet_dev;
447 pd->nr_chips = of_get_available_child_count(np);
448 if (pd->nr_chips > DSA_MAX_SWITCHES)
449 pd->nr_chips = DSA_MAX_SWITCHES;
450
451 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
452 GFP_KERNEL);
453 if (!pd->chip) {
454 ret = -ENOMEM;
455 goto out_free;
456 }
457
458 chip_index = -1;
459 for_each_available_child_of_node(np, child) {
460 int i;
461
462 chip_index++;
463 cd = &pd->chip[chip_index];
464
465 cd->of_node = child;
466
467 /* Initialize the routing table */
468 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
469 cd->rtable[i] = DSA_RTABLE_NONE;
470
471 /* When assigning the host device, increment its refcount */
472 cd->host_dev = get_device(&mdio_bus->dev);
473
474 sw_addr = of_get_property(child, "reg", NULL);
475 if (!sw_addr)
476 continue;
477
478 cd->sw_addr = be32_to_cpup(sw_addr);
479 if (cd->sw_addr >= PHY_MAX_ADDR)
480 continue;
481
482 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
483 cd->eeprom_len = eeprom_len;
484
485 mdio = of_parse_phandle(child, "mii-bus", 0);
486 if (mdio) {
487 mdio_bus_switch = of_mdio_find_bus(mdio);
488 if (!mdio_bus_switch) {
489 ret = -EPROBE_DEFER;
490 goto out_free_chip;
491 }
492
493 /* Drop the mdio_bus device ref, replacing the host
494 * device with the mdio_bus_switch device, keeping
495 * the refcount from of_mdio_find_bus() above.
496 */
497 put_device(cd->host_dev);
498 cd->host_dev = &mdio_bus_switch->dev;
499 }
500
501 for_each_available_child_of_node(child, port) {
502 port_reg = of_get_property(port, "reg", NULL);
503 if (!port_reg)
504 continue;
505
506 port_index = be32_to_cpup(port_reg);
507 if (port_index >= DSA_MAX_PORTS)
508 break;
509
510 port_name = of_get_property(port, "label", NULL);
511 if (!port_name)
512 continue;
513
514 cd->port_dn[port_index] = port;
515
516 cd->port_names[port_index] = kstrdup(port_name,
517 GFP_KERNEL);
518 if (!cd->port_names[port_index]) {
519 ret = -ENOMEM;
520 goto out_free_chip;
521 }
522
523 ret = dsa_of_probe_links(pd, cd, chip_index,
524 port_index, port, port_name);
525 if (ret)
526 goto out_free_chip;
527
528 }
529 }
530
531 /* The individual chips hold their own refcount on the mdio bus,
532 * so drop ours */
533 put_device(&mdio_bus->dev);
534
535 return 0;
536
537out_free_chip:
538 dsa_of_free_platform_data(pd);
539out_free:
540 kfree(pd);
541 dev->platform_data = NULL;
542out_put_ethernet:
543 put_device(&ethernet_dev->dev);
544out_put_mdio:
545 put_device(&mdio_bus->dev);
546 return ret;
547}
548
549static void dsa_of_remove(struct device *dev)
550{
551 struct dsa_platform_data *pd = dev->platform_data;
552
553 if (!dev->of_node)
554 return;
555
556 dsa_of_free_platform_data(pd);
557 put_device(&pd->of_netdev->dev);
558 kfree(pd);
559}
560#else
561static inline int dsa_of_probe(struct device *dev)
562{
563 return 0;
564}
565
566static inline void dsa_of_remove(struct device *dev)
567{
568}
569#endif
570
571static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
572 struct device *parent, struct dsa_platform_data *pd)
573{
574 int i;
575 unsigned configured = 0;
576
577 dst->pd = pd;
Vivien Didelota6a71f12017-04-12 12:45:03 -0400578
579 for (i = 0; i < pd->nr_chips; i++) {
580 struct dsa_switch *ds;
581
Florian Fainelli06d4d452017-06-16 16:42:11 -0700582 ds = dsa_switch_setup(dst, dev, i, parent, pd->chip[i].host_dev);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400583 if (IS_ERR(ds)) {
584 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
585 i, PTR_ERR(ds));
586 continue;
587 }
588
589 dst->ds[i] = ds;
590
591 ++configured;
592 }
593
594 /*
595 * If no switch was found, exit cleanly
596 */
597 if (!configured)
598 return -EPROBE_DEFER;
599
600 /*
601 * If we use a tagging format that doesn't have an ethertype
602 * field, make sure that all packets from this point on get
603 * sent to the tag format's receive function.
604 */
605 wmb();
Vivien Didelot02f840c2017-06-01 16:07:12 -0400606 dev->dsa_ptr = dst;
Vivien Didelota6a71f12017-04-12 12:45:03 -0400607
Vivien Didelotf2f23562017-09-19 11:57:00 -0400608 return dsa_master_ethtool_setup(dst->cpu_dp->netdev);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400609}
610
611static int dsa_probe(struct platform_device *pdev)
612{
613 struct dsa_platform_data *pd = pdev->dev.platform_data;
614 struct net_device *dev;
615 struct dsa_switch_tree *dst;
616 int ret;
617
618 if (pdev->dev.of_node) {
619 ret = dsa_of_probe(&pdev->dev);
620 if (ret)
621 return ret;
622
623 pd = pdev->dev.platform_data;
624 }
625
626 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
627 return -EINVAL;
628
629 if (pd->of_netdev) {
630 dev = pd->of_netdev;
631 dev_hold(dev);
632 } else {
633 dev = dsa_dev_to_net_device(pd->netdev);
634 }
635 if (dev == NULL) {
636 ret = -EPROBE_DEFER;
637 goto out;
638 }
639
640 if (dev->dsa_ptr != NULL) {
641 dev_put(dev);
642 ret = -EEXIST;
643 goto out;
644 }
645
646 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
647 if (dst == NULL) {
648 dev_put(dev);
649 ret = -ENOMEM;
650 goto out;
651 }
652
653 platform_set_drvdata(pdev, dst);
654
655 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
656 if (ret) {
657 dev_put(dev);
658 goto out;
659 }
660
661 return 0;
662
663out:
664 dsa_of_remove(&pdev->dev);
665
666 return ret;
667}
668
669static void dsa_remove_dst(struct dsa_switch_tree *dst)
670{
671 int i;
672
Vivien Didelotf2f23562017-09-19 11:57:00 -0400673 dsa_master_ethtool_restore(dst->cpu_dp->netdev);
Vivien Didelot19435632017-09-19 11:56:59 -0400674
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700675 dst->cpu_dp->netdev->dsa_ptr = NULL;
Vivien Didelota6a71f12017-04-12 12:45:03 -0400676
677 /* If we used a tagging format that doesn't have an ethertype
678 * field, make sure that all packets from this point get sent
679 * without the tag and go through the regular receive path.
680 */
681 wmb();
682
683 for (i = 0; i < dst->pd->nr_chips; i++) {
684 struct dsa_switch *ds = dst->ds[i];
685
686 if (ds)
687 dsa_switch_destroy(ds);
688 }
689
Florian Fainelli6d3c8c02017-06-13 13:27:19 -0700690 dev_put(dst->cpu_dp->netdev);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400691}
692
693static int dsa_remove(struct platform_device *pdev)
694{
695 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
696
697 dsa_remove_dst(dst);
698 dsa_of_remove(&pdev->dev);
699
700 return 0;
701}
702
703static void dsa_shutdown(struct platform_device *pdev)
704{
705}
706
707#ifdef CONFIG_PM_SLEEP
708static int dsa_suspend(struct device *d)
709{
710 struct platform_device *pdev = to_platform_device(d);
711 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
712 int i, ret = 0;
713
714 for (i = 0; i < dst->pd->nr_chips; i++) {
715 struct dsa_switch *ds = dst->ds[i];
716
717 if (ds != NULL)
718 ret = dsa_switch_suspend(ds);
719 }
720
721 return ret;
722}
723
724static int dsa_resume(struct device *d)
725{
726 struct platform_device *pdev = to_platform_device(d);
727 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
728 int i, ret = 0;
729
730 for (i = 0; i < dst->pd->nr_chips; i++) {
731 struct dsa_switch *ds = dst->ds[i];
732
733 if (ds != NULL)
734 ret = dsa_switch_resume(ds);
735 }
736
737 return ret;
738}
739#endif
740
Arkadi Sharshevsky37b8da12017-08-06 16:15:43 +0300741/* legacy way, bypassing the bridge *****************************************/
742int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
743 struct net_device *dev,
744 const unsigned char *addr, u16 vid,
745 u16 flags)
746{
747 struct dsa_slave_priv *p = netdev_priv(dev);
748 struct dsa_port *dp = p->dp;
749
750 return dsa_port_fdb_add(dp, addr, vid);
751}
752
753int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
754 struct net_device *dev,
755 const unsigned char *addr, u16 vid)
756{
757 struct dsa_slave_priv *p = netdev_priv(dev);
758 struct dsa_port *dp = p->dp;
759
760 return dsa_port_fdb_del(dp, addr, vid);
761}
762
Vivien Didelota6a71f12017-04-12 12:45:03 -0400763static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
764
765static const struct of_device_id dsa_of_match_table[] = {
766 { .compatible = "marvell,dsa", },
767 {}
768};
769MODULE_DEVICE_TABLE(of, dsa_of_match_table);
770
771static struct platform_driver dsa_driver = {
772 .probe = dsa_probe,
773 .remove = dsa_remove,
774 .shutdown = dsa_shutdown,
775 .driver = {
776 .name = "dsa",
777 .of_match_table = dsa_of_match_table,
778 .pm = &dsa_pm_ops,
779 },
780};
781
782int dsa_legacy_register(void)
783{
784 return platform_driver_register(&dsa_driver);
785}
786
787void dsa_legacy_unregister(void)
788{
789 platform_driver_unregister(&dsa_driver);
790}