blob: 61559232861be76f61284de44701a8a317cc70f3 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/dsa.c - Hardware switch handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Florian Fainelli5e95329b2013-03-22 10:50:50 +00004 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 *
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
Guenter Roeck51579c32014-10-29 10:44:58 -070012#include <linux/ctype.h>
13#include <linux/device.h>
14#include <linux/hwmon.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000015#include <linux/list.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000016#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040018#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000019#include <net/dsa.h>
Florian Fainelli5e95329b2013-03-22 10:50:50 +000020#include <linux/of.h>
21#include <linux/of_mdio.h>
22#include <linux/of_platform.h>
Florian Fainelli769a0202015-03-09 14:31:21 -070023#include <linux/of_net.h>
Guenter Roeck51579c32014-10-29 10:44:58 -070024#include <linux/sysfs.h>
Neil Armstrongcbc5d902015-10-06 15:40:32 +010025#include <linux/phy_fixed.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000026#include "dsa_priv.h"
27
28char dsa_driver_version[] = "0.1";
29
30
31/* switch driver registration ***********************************************/
32static DEFINE_MUTEX(dsa_switch_drivers_mutex);
33static LIST_HEAD(dsa_switch_drivers);
34
35void register_switch_driver(struct dsa_switch_driver *drv)
36{
37 mutex_lock(&dsa_switch_drivers_mutex);
38 list_add_tail(&drv->list, &dsa_switch_drivers);
39 mutex_unlock(&dsa_switch_drivers_mutex);
40}
Ben Hutchingsad293b82011-11-25 14:34:07 +000041EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000042
43void unregister_switch_driver(struct dsa_switch_driver *drv)
44{
45 mutex_lock(&dsa_switch_drivers_mutex);
46 list_del_init(&drv->list);
47 mutex_unlock(&dsa_switch_drivers_mutex);
48}
Ben Hutchingsad293b82011-11-25 14:34:07 +000049EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000050
51static struct dsa_switch_driver *
Alexander Duyckb4d23942014-09-15 13:00:27 -040052dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000053{
54 struct dsa_switch_driver *ret;
55 struct list_head *list;
56 char *name;
57
58 ret = NULL;
59 name = NULL;
60
61 mutex_lock(&dsa_switch_drivers_mutex);
62 list_for_each(list, &dsa_switch_drivers) {
63 struct dsa_switch_driver *drv;
64
65 drv = list_entry(list, struct dsa_switch_driver, list);
66
Alexander Duyckb4d23942014-09-15 13:00:27 -040067 name = drv->probe(host_dev, sw_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000068 if (name != NULL) {
69 ret = drv;
70 break;
71 }
72 }
73 mutex_unlock(&dsa_switch_drivers_mutex);
74
75 *_name = name;
76
77 return ret;
78}
79
Guenter Roeck51579c32014-10-29 10:44:58 -070080/* hwmon support ************************************************************/
81
82#ifdef CONFIG_NET_DSA_HWMON
83
84static ssize_t temp1_input_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
86{
87 struct dsa_switch *ds = dev_get_drvdata(dev);
88 int temp, ret;
89
90 ret = ds->drv->get_temp(ds, &temp);
91 if (ret < 0)
92 return ret;
93
94 return sprintf(buf, "%d\n", temp * 1000);
95}
96static DEVICE_ATTR_RO(temp1_input);
97
98static ssize_t temp1_max_show(struct device *dev,
99 struct device_attribute *attr, char *buf)
100{
101 struct dsa_switch *ds = dev_get_drvdata(dev);
102 int temp, ret;
103
104 ret = ds->drv->get_temp_limit(ds, &temp);
105 if (ret < 0)
106 return ret;
107
108 return sprintf(buf, "%d\n", temp * 1000);
109}
110
111static ssize_t temp1_max_store(struct device *dev,
112 struct device_attribute *attr, const char *buf,
113 size_t count)
114{
115 struct dsa_switch *ds = dev_get_drvdata(dev);
116 int temp, ret;
117
118 ret = kstrtoint(buf, 0, &temp);
119 if (ret < 0)
120 return ret;
121
122 ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
123 if (ret < 0)
124 return ret;
125
126 return count;
127}
Vivien Didelote3122b72015-04-17 15:12:25 -0400128static DEVICE_ATTR_RW(temp1_max);
Guenter Roeck51579c32014-10-29 10:44:58 -0700129
130static ssize_t temp1_max_alarm_show(struct device *dev,
131 struct device_attribute *attr, char *buf)
132{
133 struct dsa_switch *ds = dev_get_drvdata(dev);
134 bool alarm;
135 int ret;
136
137 ret = ds->drv->get_temp_alarm(ds, &alarm);
138 if (ret < 0)
139 return ret;
140
141 return sprintf(buf, "%d\n", alarm);
142}
143static DEVICE_ATTR_RO(temp1_max_alarm);
144
145static struct attribute *dsa_hwmon_attrs[] = {
146 &dev_attr_temp1_input.attr, /* 0 */
147 &dev_attr_temp1_max.attr, /* 1 */
148 &dev_attr_temp1_max_alarm.attr, /* 2 */
149 NULL
150};
151
152static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
153 struct attribute *attr, int index)
154{
155 struct device *dev = container_of(kobj, struct device, kobj);
156 struct dsa_switch *ds = dev_get_drvdata(dev);
157 struct dsa_switch_driver *drv = ds->drv;
158 umode_t mode = attr->mode;
159
160 if (index == 1) {
161 if (!drv->get_temp_limit)
162 mode = 0;
Vivien Didelote3122b72015-04-17 15:12:25 -0400163 else if (!drv->set_temp_limit)
164 mode &= ~S_IWUSR;
Guenter Roeck51579c32014-10-29 10:44:58 -0700165 } else if (index == 2 && !drv->get_temp_alarm) {
166 mode = 0;
167 }
168 return mode;
169}
170
171static const struct attribute_group dsa_hwmon_group = {
172 .attrs = dsa_hwmon_attrs,
173 .is_visible = dsa_hwmon_attrs_visible,
174};
175__ATTRIBUTE_GROUPS(dsa_hwmon);
176
177#endif /* CONFIG_NET_DSA_HWMON */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000178
179/* basic switch operations **************************************************/
Andrew Lunn39b0c702015-08-31 15:56:49 +0200180static int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct net_device *master)
181{
182 struct dsa_chip_data *cd = ds->pd;
183 struct device_node *port_dn;
184 struct phy_device *phydev;
Andrew Lunne4485342015-08-31 15:56:50 +0200185 int ret, port, mode;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200186
187 for (port = 0; port < DSA_MAX_PORTS; port++) {
188 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
189 continue;
190
191 port_dn = cd->port_dn[port];
192 if (of_phy_is_fixed_link(port_dn)) {
193 ret = of_phy_register_fixed_link(port_dn);
194 if (ret) {
195 netdev_err(master,
196 "failed to register fixed PHY\n");
197 return ret;
198 }
199 phydev = of_phy_find_device(port_dn);
Andrew Lunne4485342015-08-31 15:56:50 +0200200
201 mode = of_get_phy_mode(port_dn);
202 if (mode < 0)
203 mode = PHY_INTERFACE_MODE_NA;
204 phydev->interface = mode;
205
Andrew Lunn39b0c702015-08-31 15:56:49 +0200206 genphy_config_init(phydev);
207 genphy_read_status(phydev);
208 if (ds->drv->adjust_link)
209 ds->drv->adjust_link(ds, port, phydev);
210 }
211 }
212 return 0;
213}
214
Florian Fainellidf197192015-03-05 12:35:06 -0800215static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000216{
Florian Fainellidf197192015-03-05 12:35:06 -0800217 struct dsa_switch_driver *drv = ds->drv;
218 struct dsa_switch_tree *dst = ds->dst;
219 struct dsa_chip_data *pd = ds->pd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000220 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800221 int index = ds->index;
222 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223
224 /*
225 * Validate supplied switch configuration.
226 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000227 for (i = 0; i < DSA_MAX_PORTS; i++) {
228 char *name;
229
230 name = pd->port_names[i];
231 if (name == NULL)
232 continue;
233
234 if (!strcmp(name, "cpu")) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000235 if (dst->cpu_switch != -1) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800236 netdev_err(dst->master_netdev,
237 "multiple cpu ports?!\n");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000238 ret = -EINVAL;
239 goto out;
240 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000241 dst->cpu_switch = index;
242 dst->cpu_port = i;
243 } else if (!strcmp(name, "dsa")) {
244 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000245 } else {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000246 ds->phys_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000247 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000248 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000249 }
250
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000251 if (!valid_name_found && i == DSA_MAX_PORTS) {
252 ret = -EINVAL;
253 goto out;
254 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000255
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700256 /* Make the built-in MII bus mask match the number of ports,
257 * switch drivers can override this later
258 */
259 ds->phys_mii_mask = ds->phys_port_mask;
260
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000261 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000262 * If the CPU connects to this switch, set the switch tree
263 * tagging protocol to the preferred tagging format of this
264 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000265 */
Alexander Duyck50753142014-09-15 13:00:19 -0400266 if (dst->cpu_switch == index) {
Florian Fainelli59299032015-03-05 12:35:07 -0800267 switch (ds->tag_protocol) {
Alexander Duyck50753142014-09-15 13:00:19 -0400268#ifdef CONFIG_NET_DSA_TAG_DSA
269 case DSA_TAG_PROTO_DSA:
270 dst->rcv = dsa_netdev_ops.rcv;
271 break;
272#endif
273#ifdef CONFIG_NET_DSA_TAG_EDSA
274 case DSA_TAG_PROTO_EDSA:
275 dst->rcv = edsa_netdev_ops.rcv;
276 break;
277#endif
278#ifdef CONFIG_NET_DSA_TAG_TRAILER
279 case DSA_TAG_PROTO_TRAILER:
280 dst->rcv = trailer_netdev_ops.rcv;
281 break;
282#endif
283#ifdef CONFIG_NET_DSA_TAG_BRCM
284 case DSA_TAG_PROTO_BRCM:
285 dst->rcv = brcm_netdev_ops.rcv;
286 break;
287#endif
Andrew Lunnae439282014-10-24 23:44:04 +0200288 case DSA_TAG_PROTO_NONE:
Alexander Duyck50753142014-09-15 13:00:19 -0400289 break;
Andrew Lunnae439282014-10-24 23:44:04 +0200290 default:
291 ret = -ENOPROTOOPT;
292 goto out;
Alexander Duyck50753142014-09-15 13:00:19 -0400293 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000294
Florian Fainelli59299032015-03-05 12:35:07 -0800295 dst->tag_protocol = ds->tag_protocol;
Alexander Duyck50753142014-09-15 13:00:19 -0400296 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000297
298 /*
299 * Do basic register setup.
300 */
301 ret = drv->setup(ds);
302 if (ret < 0)
303 goto out;
304
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000305 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000306 if (ret < 0)
307 goto out;
308
309 ds->slave_mii_bus = mdiobus_alloc();
310 if (ds->slave_mii_bus == NULL) {
311 ret = -ENOMEM;
312 goto out;
313 }
314 dsa_slave_mii_bus_init(ds);
315
316 ret = mdiobus_register(ds->slave_mii_bus);
317 if (ret < 0)
318 goto out_free;
319
320
321 /*
322 * Create network devices for physical switch ports.
323 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000324 for (i = 0; i < DSA_MAX_PORTS; i++) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000325 if (!(ds->phys_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000326 continue;
327
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800328 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
329 if (ret < 0) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800330 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
331 index, i, pd->port_names[i]);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800332 ret = 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000333 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000334 }
335
Andrew Lunn39b0c702015-08-31 15:56:49 +0200336 /* Perform configuration of the CPU and DSA ports */
337 ret = dsa_cpu_dsa_setup(ds, dst->master_netdev);
338 if (ret < 0) {
339 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
340 index);
341 ret = 0;
342 }
343
Guenter Roeck51579c32014-10-29 10:44:58 -0700344#ifdef CONFIG_NET_DSA_HWMON
345 /* If the switch provides a temperature sensor,
346 * register with hardware monitoring subsystem.
347 * Treat registration error as non-fatal and ignore it.
348 */
349 if (drv->get_temp) {
350 const char *netname = netdev_name(dst->master_netdev);
351 char hname[IFNAMSIZ + 1];
352 int i, j;
353
354 /* Create valid hwmon 'name' attribute */
355 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
356 if (isalnum(netname[i]))
357 hname[j++] = netname[i];
358 }
359 hname[j] = '\0';
360 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
361 hname, index);
362 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
363 ds->hwmon_name, ds, dsa_hwmon_groups);
364 if (IS_ERR(ds->hwmon_dev))
365 ds->hwmon_dev = NULL;
366 }
367#endif /* CONFIG_NET_DSA_HWMON */
368
Florian Fainellidf197192015-03-05 12:35:06 -0800369 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000370
371out_free:
372 mdiobus_free(ds->slave_mii_bus);
373out:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000374 kfree(ds);
Florian Fainellidf197192015-03-05 12:35:06 -0800375 return ret;
376}
377
378static struct dsa_switch *
379dsa_switch_setup(struct dsa_switch_tree *dst, int index,
380 struct device *parent, struct device *host_dev)
381{
382 struct dsa_chip_data *pd = dst->pd->chip + index;
383 struct dsa_switch_driver *drv;
384 struct dsa_switch *ds;
385 int ret;
386 char *name;
387
388 /*
389 * Probe for switch model.
390 */
391 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
392 if (drv == NULL) {
393 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
394 index);
395 return ERR_PTR(-EINVAL);
396 }
397 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
398 index, name);
399
400
401 /*
402 * Allocate and initialise switch state.
403 */
404 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
405 if (ds == NULL)
Florian Fainelli24595342015-05-29 10:29:46 -0700406 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800407
408 ds->dst = dst;
409 ds->index = index;
410 ds->pd = pd;
411 ds->drv = drv;
Florian Fainelli59299032015-03-05 12:35:07 -0800412 ds->tag_protocol = drv->tag_protocol;
Florian Fainellidf197192015-03-05 12:35:06 -0800413 ds->master_dev = host_dev;
414
415 ret = dsa_switch_setup_one(ds, parent);
416 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700417 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800418
419 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000420}
421
422static void dsa_switch_destroy(struct dsa_switch *ds)
423{
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100424 struct device_node *port_dn;
425 struct phy_device *phydev;
426 struct dsa_chip_data *cd = ds->pd;
427 int port;
428
Guenter Roeck51579c32014-10-29 10:44:58 -0700429#ifdef CONFIG_NET_DSA_HWMON
430 if (ds->hwmon_dev)
431 hwmon_device_unregister(ds->hwmon_dev);
432#endif
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100433
434 /* Disable configuration of the CPU and DSA ports */
435 for (port = 0; port < DSA_MAX_PORTS; port++) {
436 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
437 continue;
438
439 port_dn = cd->port_dn[port];
440 if (of_phy_is_fixed_link(port_dn)) {
441 phydev = of_phy_find_device(port_dn);
442 if (phydev) {
443 int addr = phydev->addr;
444
445 phy_device_free(phydev);
446 of_node_put(port_dn);
447 fixed_phy_del(addr);
448 }
449 }
450 }
451
452 /* Destroy network devices for physical switch ports. */
453 for (port = 0; port < DSA_MAX_PORTS; port++) {
454 if (!(ds->phys_port_mask & (1 << port)))
455 continue;
456
457 if (!ds->ports[port])
458 continue;
459
460 unregister_netdev(ds->ports[port]);
461 free_netdev(ds->ports[port]);
462 }
463
Neil Armstronge410ddb2015-10-06 15:40:25 +0100464 mdiobus_unregister(ds->slave_mii_bus);
465 mdiobus_free(ds->slave_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000466}
467
Thierry Redinge506d402014-10-01 13:59:00 +0200468#ifdef CONFIG_PM_SLEEP
Florian Fainelli24462542014-09-18 17:31:22 -0700469static int dsa_switch_suspend(struct dsa_switch *ds)
470{
471 int i, ret = 0;
472
473 /* Suspend slave network devices */
474 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800475 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700476 continue;
477
478 ret = dsa_slave_suspend(ds->ports[i]);
479 if (ret)
480 return ret;
481 }
482
483 if (ds->drv->suspend)
484 ret = ds->drv->suspend(ds);
485
486 return ret;
487}
488
489static int dsa_switch_resume(struct dsa_switch *ds)
490{
491 int i, ret = 0;
492
493 if (ds->drv->resume)
494 ret = ds->drv->resume(ds);
495
496 if (ret)
497 return ret;
498
499 /* Resume slave network devices */
500 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800501 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700502 continue;
503
504 ret = dsa_slave_resume(ds->ports[i]);
505 if (ret)
506 return ret;
507 }
508
509 return 0;
510}
Thierry Redinge506d402014-10-01 13:59:00 +0200511#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700512
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000513
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000514/* link polling *************************************************************/
515static void dsa_link_poll_work(struct work_struct *ugly)
516{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000517 struct dsa_switch_tree *dst;
518 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000519
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000520 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000521
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000522 for (i = 0; i < dst->pd->nr_chips; i++) {
523 struct dsa_switch *ds = dst->ds[i];
524
525 if (ds != NULL && ds->drv->poll_link != NULL)
526 ds->drv->poll_link(ds);
527 }
528
529 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000530}
531
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000532static void dsa_link_poll_timer(unsigned long _dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000533{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000534 struct dsa_switch_tree *dst = (void *)_dst;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000535
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000536 schedule_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000537}
538
539
540/* platform driver init and cleanup *****************************************/
541static int dev_is_class(struct device *dev, void *class)
542{
543 if (dev->class != NULL && !strcmp(dev->class->name, class))
544 return 1;
545
546 return 0;
547}
548
549static struct device *dev_find_class(struct device *parent, char *class)
550{
551 if (dev_is_class(parent, class)) {
552 get_device(parent);
553 return parent;
554 }
555
556 return device_find_child(parent, class, dev_is_class);
557}
558
Alexander Duyckb4d23942014-09-15 13:00:27 -0400559struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000560{
561 struct device *d;
562
563 d = dev_find_class(dev, "mdio_bus");
564 if (d != NULL) {
565 struct mii_bus *bus;
566
567 bus = to_mii_bus(d);
568 put_device(d);
569
570 return bus;
571 }
572
573 return NULL;
574}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400575EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000576
577static struct net_device *dev_to_net_device(struct device *dev)
578{
579 struct device *d;
580
581 d = dev_find_class(dev, "net");
582 if (d != NULL) {
583 struct net_device *nd;
584
585 nd = to_net_dev(d);
586 dev_hold(nd);
587 put_device(d);
588
589 return nd;
590 }
591
592 return NULL;
593}
594
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000595#ifdef CONFIG_OF
596static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
597 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300598 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000599 struct device_node *link)
600{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000601 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000602 int link_sw_addr;
603 struct device_node *parent_sw;
604 int len;
605
606 parent_sw = of_get_parent(link);
607 if (!parent_sw)
608 return -EINVAL;
609
610 reg = of_get_property(parent_sw, "reg", &len);
611 if (!reg || (len != sizeof(*reg) * 2))
612 return -EINVAL;
613
Pavel Nakonechny30303812015-04-05 00:46:21 +0300614 /*
615 * Get the destination switch number from the second field of its 'reg'
616 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
617 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000618 link_sw_addr = be32_to_cpup(reg + 1);
619
620 if (link_sw_addr >= pd->nr_chips)
621 return -EINVAL;
622
623 /* First time routing table allocation */
624 if (!cd->rtable) {
Fabian Frederick5bc4b462014-11-14 19:36:42 +0100625 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
626 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000627 if (!cd->rtable)
628 return -ENOMEM;
629
630 /* default to no valid uplink/downlink */
631 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
632 }
633
Pavel Nakonechny30303812015-04-05 00:46:21 +0300634 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000635
636 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000637}
638
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200639static int dsa_of_probe_links(struct dsa_platform_data *pd,
640 struct dsa_chip_data *cd,
641 int chip_index, int port_index,
642 struct device_node *port,
643 const char *port_name)
644{
645 struct device_node *link;
646 int link_index;
647 int ret;
648
649 for (link_index = 0;; link_index++) {
650 link = of_parse_phandle(port, "link", link_index);
651 if (!link)
652 break;
653
654 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
655 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
656 port_index, link);
657 if (ret)
658 return ret;
659 }
660 }
661 return 0;
662}
663
Florian Fainelli21168242013-03-25 05:03:39 +0000664static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
665{
666 int i;
667 int port_index;
668
669 for (i = 0; i < pd->nr_chips; i++) {
670 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000671 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200672 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000673 port_index++;
674 }
Florian Fainelli21168242013-03-25 05:03:39 +0000675 kfree(pd->chip[i].rtable);
Russell Kinge496ae62015-09-24 20:35:57 +0100676
677 /* Drop our reference to the MDIO bus device */
678 if (pd->chip[i].host_dev)
679 put_device(pd->chip[i].host_dev);
Florian Fainelli21168242013-03-25 05:03:39 +0000680 }
681 kfree(pd->chip);
682}
683
Florian Fainellif1a26a02015-03-05 12:35:04 -0800684static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000685{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800686 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200687 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200688 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700689 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000690 struct dsa_platform_data *pd;
691 struct dsa_chip_data *cd;
692 const char *port_name;
693 int chip_index, port_index;
694 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700695 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000696 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000697
698 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
699 if (!mdio)
700 return -EINVAL;
701
702 mdio_bus = of_mdio_find_bus(mdio);
703 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800704 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000705
706 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
Russell Kinge496ae62015-09-24 20:35:57 +0100707 if (!ethernet) {
708 ret = -EINVAL;
709 goto out_put_mdio;
710 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000711
Florian Fainelli769a0202015-03-09 14:31:21 -0700712 ethernet_dev = of_find_net_device_by_node(ethernet);
Russell Kinge496ae62015-09-24 20:35:57 +0100713 if (!ethernet_dev) {
714 ret = -EPROBE_DEFER;
715 goto out_put_mdio;
716 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000717
718 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Russell Kinge496ae62015-09-24 20:35:57 +0100719 if (!pd) {
720 ret = -ENOMEM;
Russell King9861f722015-09-24 20:36:33 +0100721 goto out_put_ethernet;
Russell Kinge496ae62015-09-24 20:35:57 +0100722 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000723
Florian Fainellif1a26a02015-03-05 12:35:04 -0800724 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700725 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100726 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000727 if (pd->nr_chips > DSA_MAX_SWITCHES)
728 pd->nr_chips = DSA_MAX_SWITCHES;
729
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100730 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
731 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000732 if (!pd->chip) {
733 ret = -ENOMEM;
734 goto out_free;
735 }
736
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200737 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000738 for_each_available_child_of_node(np, child) {
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200739 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000740 cd = &pd->chip[chip_index];
741
Florian Fainellifa981d92014-08-27 17:04:49 -0700742 cd->of_node = child;
Russell Kinge496ae62015-09-24 20:35:57 +0100743
744 /* When assigning the host device, increment its refcount */
745 cd->host_dev = get_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000746
747 sw_addr = of_get_property(child, "reg", NULL);
748 if (!sw_addr)
749 continue;
750
751 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700752 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000753 continue;
754
Guenter Roeck50d49642015-04-29 10:56:15 -0700755 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700756 cd->eeprom_len = eeprom_len;
757
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200758 mdio = of_parse_phandle(child, "mii-bus", 0);
759 if (mdio) {
760 mdio_bus_switch = of_mdio_find_bus(mdio);
761 if (!mdio_bus_switch) {
762 ret = -EPROBE_DEFER;
763 goto out_free_chip;
764 }
Russell Kinge496ae62015-09-24 20:35:57 +0100765
766 /* Drop the mdio_bus device ref, replacing the host
767 * device with the mdio_bus_switch device, keeping
768 * the refcount from of_mdio_find_bus() above.
769 */
770 put_device(cd->host_dev);
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200771 cd->host_dev = &mdio_bus_switch->dev;
772 }
773
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000774 for_each_available_child_of_node(child, port) {
775 port_reg = of_get_property(port, "reg", NULL);
776 if (!port_reg)
777 continue;
778
779 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700780 if (port_index >= DSA_MAX_PORTS)
781 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000782
783 port_name = of_get_property(port, "label", NULL);
784 if (!port_name)
785 continue;
786
Florian Fainellibd474972014-08-27 17:04:50 -0700787 cd->port_dn[port_index] = port;
788
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000789 cd->port_names[port_index] = kstrdup(port_name,
790 GFP_KERNEL);
791 if (!cd->port_names[port_index]) {
792 ret = -ENOMEM;
793 goto out_free_chip;
794 }
795
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200796 ret = dsa_of_probe_links(pd, cd, chip_index,
797 port_index, port, port_name);
798 if (ret)
799 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000800
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000801 }
802 }
803
Russell Kinge496ae62015-09-24 20:35:57 +0100804 /* The individual chips hold their own refcount on the mdio bus,
805 * so drop ours */
806 put_device(&mdio_bus->dev);
807
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000808 return 0;
809
810out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000811 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000812out_free:
813 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800814 dev->platform_data = NULL;
Russell King9861f722015-09-24 20:36:33 +0100815out_put_ethernet:
816 put_device(&ethernet_dev->dev);
Russell Kinge496ae62015-09-24 20:35:57 +0100817out_put_mdio:
818 put_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000819 return ret;
820}
821
Florian Fainellif1a26a02015-03-05 12:35:04 -0800822static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000823{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800824 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000825
Florian Fainellif1a26a02015-03-05 12:35:04 -0800826 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000827 return;
828
Florian Fainelli21168242013-03-25 05:03:39 +0000829 dsa_of_free_platform_data(pd);
Russell King9861f722015-09-24 20:36:33 +0100830 put_device(&pd->of_netdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000831 kfree(pd);
832}
833#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800834static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000835{
836 return 0;
837}
838
Florian Fainellif1a26a02015-03-05 12:35:04 -0800839static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000840{
841}
842#endif
843
Florian Fainellic86e59b2015-03-05 12:35:08 -0800844static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
845 struct device *parent, struct dsa_platform_data *pd)
846{
847 int i;
848
849 dst->pd = pd;
850 dst->master_netdev = dev;
851 dst->cpu_switch = -1;
852 dst->cpu_port = -1;
853
854 for (i = 0; i < pd->nr_chips; i++) {
855 struct dsa_switch *ds;
856
857 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
858 if (IS_ERR(ds)) {
859 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
860 i, PTR_ERR(ds));
861 continue;
862 }
863
864 dst->ds[i] = ds;
865 if (ds->drv->poll_link != NULL)
866 dst->link_poll_needed = 1;
867 }
868
869 /*
870 * If we use a tagging format that doesn't have an ethertype
871 * field, make sure that all packets from this point on get
872 * sent to the tag format's receive function.
873 */
874 wmb();
875 dev->dsa_ptr = (void *)dst;
876
877 if (dst->link_poll_needed) {
878 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
879 init_timer(&dst->link_poll_timer);
880 dst->link_poll_timer.data = (unsigned long)dst;
881 dst->link_poll_timer.function = dsa_link_poll_timer;
882 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
883 add_timer(&dst->link_poll_timer);
884 }
885}
886
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000887static int dsa_probe(struct platform_device *pdev)
888{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000889 struct dsa_platform_data *pd = pdev->dev.platform_data;
890 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000891 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800892 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000893
Joe Perchesa2ae6002014-11-09 16:32:46 -0800894 pr_notice_once("Distributed Switch Architecture driver version %s\n",
895 dsa_driver_version);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000896
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000897 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800898 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000899 if (ret)
900 return ret;
901
902 pd = pdev->dev.platform_data;
903 }
904
Florian Fainelli769a0202015-03-09 14:31:21 -0700905 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000906 return -EINVAL;
907
Florian Fainelli769a0202015-03-09 14:31:21 -0700908 if (pd->of_netdev) {
909 dev = pd->of_netdev;
910 dev_hold(dev);
911 } else {
912 dev = dev_to_net_device(pd->netdev);
913 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000914 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800915 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000916 goto out;
917 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000918
919 if (dev->dsa_ptr != NULL) {
920 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000921 ret = -EEXIST;
922 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000923 }
924
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000925 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
926 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000927 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000928 ret = -ENOMEM;
929 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000930 }
931
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000932 platform_set_drvdata(pdev, dst);
933
Florian Fainellic86e59b2015-03-05 12:35:08 -0800934 dsa_setup_dst(dst, dev, &pdev->dev, pd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000935
936 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000937
938out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800939 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000940
941 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000942}
943
Florian Fainellic86e59b2015-03-05 12:35:08 -0800944static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000945{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000946 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000947
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000948 if (dst->link_poll_needed)
949 del_timer_sync(&dst->link_poll_timer);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000950
Tejun Heo43829732012-08-20 14:51:24 -0700951 flush_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000952
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000953 for (i = 0; i < dst->pd->nr_chips; i++) {
954 struct dsa_switch *ds = dst->ds[i];
955
Neil Armstrong1023d2e2015-10-06 15:39:53 +0100956 if (ds) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000957 dsa_switch_destroy(ds);
Neil Armstrong1023d2e2015-10-06 15:39:53 +0100958 kfree(ds);
959 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000960 }
Florian Fainellic86e59b2015-03-05 12:35:08 -0800961}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000962
Florian Fainellic86e59b2015-03-05 12:35:08 -0800963static int dsa_remove(struct platform_device *pdev)
964{
965 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
966
967 dsa_remove_dst(dst);
Neil Armstrong1023d2e2015-10-06 15:39:53 +0100968 kfree(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800969 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000970
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000971 return 0;
972}
973
974static void dsa_shutdown(struct platform_device *pdev)
975{
976}
977
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700978static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
979 struct packet_type *pt, struct net_device *orig_dev)
980{
981 struct dsa_switch_tree *dst = dev->dsa_ptr;
982
983 if (unlikely(dst == NULL)) {
984 kfree_skb(skb);
985 return 0;
986 }
987
Alexander Duyck50753142014-09-15 13:00:19 -0400988 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700989}
990
Florian Fainelli61b73632014-08-29 12:42:07 -0700991static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700992 .type = cpu_to_be16(ETH_P_XDSA),
993 .func = dsa_switch_rcv,
994};
995
Florian Fainellib73adef2015-02-24 13:15:33 -0800996static struct notifier_block dsa_netdevice_nb __read_mostly = {
997 .notifier_call = dsa_slave_netdevice_event,
998};
999
Florian Fainelli24462542014-09-18 17:31:22 -07001000#ifdef CONFIG_PM_SLEEP
1001static int dsa_suspend(struct device *d)
1002{
1003 struct platform_device *pdev = to_platform_device(d);
1004 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
1005 int i, ret = 0;
1006
1007 for (i = 0; i < dst->pd->nr_chips; i++) {
1008 struct dsa_switch *ds = dst->ds[i];
1009
1010 if (ds != NULL)
1011 ret = dsa_switch_suspend(ds);
1012 }
1013
1014 return ret;
1015}
1016
1017static int dsa_resume(struct device *d)
1018{
1019 struct platform_device *pdev = to_platform_device(d);
1020 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
1021 int i, ret = 0;
1022
1023 for (i = 0; i < dst->pd->nr_chips; i++) {
1024 struct dsa_switch *ds = dst->ds[i];
1025
1026 if (ds != NULL)
1027 ret = dsa_switch_resume(ds);
1028 }
1029
1030 return ret;
1031}
1032#endif
1033
1034static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
1035
Florian Fainelli5e95329b2013-03-22 10:50:50 +00001036static const struct of_device_id dsa_of_match_table[] = {
Florian Fainelli246d7f72014-08-27 17:04:56 -07001037 { .compatible = "brcm,bcm7445-switch-v4.0" },
Florian Fainelli5e95329b2013-03-22 10:50:50 +00001038 { .compatible = "marvell,dsa", },
1039 {}
1040};
1041MODULE_DEVICE_TABLE(of, dsa_of_match_table);
1042
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001043static struct platform_driver dsa_driver = {
1044 .probe = dsa_probe,
1045 .remove = dsa_remove,
1046 .shutdown = dsa_shutdown,
1047 .driver = {
1048 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +00001049 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -07001050 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001051 },
1052};
1053
1054static int __init dsa_init_module(void)
1055{
Ben Hutchings7df899c2011-11-25 14:35:02 +00001056 int rc;
1057
Florian Fainellib73adef2015-02-24 13:15:33 -08001058 register_netdevice_notifier(&dsa_netdevice_nb);
1059
Ben Hutchings7df899c2011-11-25 14:35:02 +00001060 rc = platform_driver_register(&dsa_driver);
1061 if (rc)
1062 return rc;
1063
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001064 dev_add_pack(&dsa_pack_type);
1065
Ben Hutchings7df899c2011-11-25 14:35:02 +00001066 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001067}
1068module_init(dsa_init_module);
1069
1070static void __exit dsa_cleanup_module(void)
1071{
Florian Fainellib73adef2015-02-24 13:15:33 -08001072 unregister_netdevice_notifier(&dsa_netdevice_nb);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001073 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001074 platform_driver_unregister(&dsa_driver);
1075}
1076module_exit(dsa_cleanup_module);
1077
Rusty Russell577d6a72011-01-24 14:32:52 -06001078MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001079MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1080MODULE_LICENSE("GPL");
1081MODULE_ALIAS("platform:dsa");