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