blob: bfe1d03d4730546e8c96bc196acfb9164397e05c [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>
Andrew Lunncc30c162015-11-20 03:56:23 +010024#include <linux/of_gpio.h>
Guenter Roeck51579c32014-10-29 10:44:58 -070025#include <linux/sysfs.h>
Neil Armstrongcbc5d902015-10-06 15:40:32 +010026#include <linux/phy_fixed.h>
Arnd Bergmann85beabf2015-11-24 12:34:49 +010027#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000028#include "dsa_priv.h"
29
30char dsa_driver_version[] = "0.1";
31
32
33/* switch driver registration ***********************************************/
34static DEFINE_MUTEX(dsa_switch_drivers_mutex);
35static LIST_HEAD(dsa_switch_drivers);
36
37void register_switch_driver(struct dsa_switch_driver *drv)
38{
39 mutex_lock(&dsa_switch_drivers_mutex);
40 list_add_tail(&drv->list, &dsa_switch_drivers);
41 mutex_unlock(&dsa_switch_drivers_mutex);
42}
Ben Hutchingsad293b82011-11-25 14:34:07 +000043EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000044
45void unregister_switch_driver(struct dsa_switch_driver *drv)
46{
47 mutex_lock(&dsa_switch_drivers_mutex);
48 list_del_init(&drv->list);
49 mutex_unlock(&dsa_switch_drivers_mutex);
50}
Ben Hutchingsad293b82011-11-25 14:34:07 +000051EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000052
53static struct dsa_switch_driver *
Andrew Lunnbbb8d792016-04-13 02:40:39 +020054dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
Vivien Didelot0209d142016-04-17 13:23:55 -040055 const char **_name, void **priv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000056{
57 struct dsa_switch_driver *ret;
58 struct list_head *list;
Vivien Didelot0209d142016-04-17 13:23:55 -040059 const char *name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000060
61 ret = NULL;
62 name = NULL;
63
64 mutex_lock(&dsa_switch_drivers_mutex);
65 list_for_each(list, &dsa_switch_drivers) {
66 struct dsa_switch_driver *drv;
67
68 drv = list_entry(list, struct dsa_switch_driver, list);
69
Andrew Lunn7543a6d2016-04-13 02:40:40 +020070 name = drv->probe(parent, host_dev, sw_addr, priv);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071 if (name != NULL) {
72 ret = drv;
73 break;
74 }
75 }
76 mutex_unlock(&dsa_switch_drivers_mutex);
77
78 *_name = name;
79
80 return ret;
81}
82
Guenter Roeck51579c32014-10-29 10:44:58 -070083/* hwmon support ************************************************************/
84
85#ifdef CONFIG_NET_DSA_HWMON
86
87static ssize_t temp1_input_show(struct device *dev,
88 struct device_attribute *attr, char *buf)
89{
90 struct dsa_switch *ds = dev_get_drvdata(dev);
91 int temp, ret;
92
93 ret = ds->drv->get_temp(ds, &temp);
94 if (ret < 0)
95 return ret;
96
97 return sprintf(buf, "%d\n", temp * 1000);
98}
99static DEVICE_ATTR_RO(temp1_input);
100
101static ssize_t temp1_max_show(struct device *dev,
102 struct device_attribute *attr, char *buf)
103{
104 struct dsa_switch *ds = dev_get_drvdata(dev);
105 int temp, ret;
106
107 ret = ds->drv->get_temp_limit(ds, &temp);
108 if (ret < 0)
109 return ret;
110
111 return sprintf(buf, "%d\n", temp * 1000);
112}
113
114static ssize_t temp1_max_store(struct device *dev,
115 struct device_attribute *attr, const char *buf,
116 size_t count)
117{
118 struct dsa_switch *ds = dev_get_drvdata(dev);
119 int temp, ret;
120
121 ret = kstrtoint(buf, 0, &temp);
122 if (ret < 0)
123 return ret;
124
125 ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
126 if (ret < 0)
127 return ret;
128
129 return count;
130}
Vivien Didelote3122b72015-04-17 15:12:25 -0400131static DEVICE_ATTR_RW(temp1_max);
Guenter Roeck51579c32014-10-29 10:44:58 -0700132
133static ssize_t temp1_max_alarm_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
135{
136 struct dsa_switch *ds = dev_get_drvdata(dev);
137 bool alarm;
138 int ret;
139
140 ret = ds->drv->get_temp_alarm(ds, &alarm);
141 if (ret < 0)
142 return ret;
143
144 return sprintf(buf, "%d\n", alarm);
145}
146static DEVICE_ATTR_RO(temp1_max_alarm);
147
148static struct attribute *dsa_hwmon_attrs[] = {
149 &dev_attr_temp1_input.attr, /* 0 */
150 &dev_attr_temp1_max.attr, /* 1 */
151 &dev_attr_temp1_max_alarm.attr, /* 2 */
152 NULL
153};
154
155static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
156 struct attribute *attr, int index)
157{
158 struct device *dev = container_of(kobj, struct device, kobj);
159 struct dsa_switch *ds = dev_get_drvdata(dev);
160 struct dsa_switch_driver *drv = ds->drv;
161 umode_t mode = attr->mode;
162
163 if (index == 1) {
164 if (!drv->get_temp_limit)
165 mode = 0;
Vivien Didelote3122b72015-04-17 15:12:25 -0400166 else if (!drv->set_temp_limit)
167 mode &= ~S_IWUSR;
Guenter Roeck51579c32014-10-29 10:44:58 -0700168 } else if (index == 2 && !drv->get_temp_alarm) {
169 mode = 0;
170 }
171 return mode;
172}
173
174static const struct attribute_group dsa_hwmon_group = {
175 .attrs = dsa_hwmon_attrs,
176 .is_visible = dsa_hwmon_attrs_visible,
177};
178__ATTRIBUTE_GROUPS(dsa_hwmon);
179
180#endif /* CONFIG_NET_DSA_HWMON */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000181
182/* basic switch operations **************************************************/
Andrew Lunn39b0c702015-08-31 15:56:49 +0200183static int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct net_device *master)
184{
Andrew Lunn39b0c702015-08-31 15:56:49 +0200185 struct device_node *port_dn;
186 struct phy_device *phydev;
Andrew Lunne4485342015-08-31 15:56:50 +0200187 int ret, port, mode;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200188
189 for (port = 0; port < DSA_MAX_PORTS; port++) {
190 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
191 continue;
192
Andrew Lunn189b0d92016-06-04 21:16:58 +0200193 port_dn = ds->ports[port].dn;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200194 if (of_phy_is_fixed_link(port_dn)) {
195 ret = of_phy_register_fixed_link(port_dn);
196 if (ret) {
197 netdev_err(master,
198 "failed to register fixed PHY\n");
199 return ret;
200 }
201 phydev = of_phy_find_device(port_dn);
Andrew Lunne4485342015-08-31 15:56:50 +0200202
203 mode = of_get_phy_mode(port_dn);
204 if (mode < 0)
205 mode = PHY_INTERFACE_MODE_NA;
206 phydev->interface = mode;
207
Andrew Lunn39b0c702015-08-31 15:56:49 +0200208 genphy_config_init(phydev);
209 genphy_read_status(phydev);
210 if (ds->drv->adjust_link)
211 ds->drv->adjust_link(ds, port, phydev);
212 }
213 }
214 return 0;
215}
216
Florian Fainellidf197192015-03-05 12:35:06 -0800217static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218{
Florian Fainellidf197192015-03-05 12:35:06 -0800219 struct dsa_switch_driver *drv = ds->drv;
220 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunnff049552016-05-10 23:27:24 +0200221 struct dsa_chip_data *cd = ds->cd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000222 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800223 int index = ds->index;
224 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000225
226 /*
227 * Validate supplied switch configuration.
228 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000229 for (i = 0; i < DSA_MAX_PORTS; i++) {
230 char *name;
231
Andrew Lunnff049552016-05-10 23:27:24 +0200232 name = cd->port_names[i];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000233 if (name == NULL)
234 continue;
235
236 if (!strcmp(name, "cpu")) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000237 if (dst->cpu_switch != -1) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800238 netdev_err(dst->master_netdev,
239 "multiple cpu ports?!\n");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000240 ret = -EINVAL;
241 goto out;
242 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000243 dst->cpu_switch = index;
244 dst->cpu_port = i;
245 } else if (!strcmp(name, "dsa")) {
246 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000247 } else {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200248 ds->enabled_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000249 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000250 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000251 }
252
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000253 if (!valid_name_found && i == DSA_MAX_PORTS) {
254 ret = -EINVAL;
255 goto out;
256 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000257
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700258 /* Make the built-in MII bus mask match the number of ports,
259 * switch drivers can override this later
260 */
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200261 ds->phys_mii_mask = ds->enabled_port_mask;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700262
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000263 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000264 * If the CPU connects to this switch, set the switch tree
265 * tagging protocol to the preferred tagging format of this
266 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000267 */
Alexander Duyck50753142014-09-15 13:00:19 -0400268 if (dst->cpu_switch == index) {
Vivien Didelotc60c9842016-04-18 18:24:04 -0400269 switch (drv->tag_protocol) {
Alexander Duyck50753142014-09-15 13:00:19 -0400270#ifdef CONFIG_NET_DSA_TAG_DSA
271 case DSA_TAG_PROTO_DSA:
272 dst->rcv = dsa_netdev_ops.rcv;
273 break;
274#endif
275#ifdef CONFIG_NET_DSA_TAG_EDSA
276 case DSA_TAG_PROTO_EDSA:
277 dst->rcv = edsa_netdev_ops.rcv;
278 break;
279#endif
280#ifdef CONFIG_NET_DSA_TAG_TRAILER
281 case DSA_TAG_PROTO_TRAILER:
282 dst->rcv = trailer_netdev_ops.rcv;
283 break;
284#endif
285#ifdef CONFIG_NET_DSA_TAG_BRCM
286 case DSA_TAG_PROTO_BRCM:
287 dst->rcv = brcm_netdev_ops.rcv;
288 break;
289#endif
Andrew Lunnae439282014-10-24 23:44:04 +0200290 case DSA_TAG_PROTO_NONE:
Alexander Duyck50753142014-09-15 13:00:19 -0400291 break;
Andrew Lunnae439282014-10-24 23:44:04 +0200292 default:
293 ret = -ENOPROTOOPT;
294 goto out;
Alexander Duyck50753142014-09-15 13:00:19 -0400295 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000296
Vivien Didelotc60c9842016-04-18 18:24:04 -0400297 dst->tag_protocol = drv->tag_protocol;
Alexander Duyck50753142014-09-15 13:00:19 -0400298 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000299
Andrew Lunn66472fc2016-06-04 21:17:00 +0200300 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
301
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000302 /*
303 * Do basic register setup.
304 */
305 ret = drv->setup(ds);
306 if (ret < 0)
307 goto out;
308
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000309 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000310 if (ret < 0)
311 goto out;
312
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100313 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000314 if (ds->slave_mii_bus == NULL) {
315 ret = -ENOMEM;
316 goto out;
317 }
318 dsa_slave_mii_bus_init(ds);
319
320 ret = mdiobus_register(ds->slave_mii_bus);
321 if (ret < 0)
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100322 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000323
324
325 /*
326 * Create network devices for physical switch ports.
327 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000328 for (i = 0; i < DSA_MAX_PORTS; i++) {
Andrew Lunn189b0d92016-06-04 21:16:58 +0200329 ds->ports[i].dn = cd->port_dn[i];
330
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200331 if (!(ds->enabled_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000332 continue;
333
Andrew Lunnff049552016-05-10 23:27:24 +0200334 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800335 if (ret < 0) {
Russell Kingd25b8e72015-10-03 18:09:07 +0100336 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
Andrew Lunnff049552016-05-10 23:27:24 +0200337 index, i, cd->port_names[i], ret);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800338 ret = 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000339 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000340 }
341
Andrew Lunn39b0c702015-08-31 15:56:49 +0200342 /* Perform configuration of the CPU and DSA ports */
343 ret = dsa_cpu_dsa_setup(ds, dst->master_netdev);
344 if (ret < 0) {
345 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
346 index);
347 ret = 0;
348 }
349
Guenter Roeck51579c32014-10-29 10:44:58 -0700350#ifdef CONFIG_NET_DSA_HWMON
351 /* If the switch provides a temperature sensor,
352 * register with hardware monitoring subsystem.
353 * Treat registration error as non-fatal and ignore it.
354 */
355 if (drv->get_temp) {
356 const char *netname = netdev_name(dst->master_netdev);
357 char hname[IFNAMSIZ + 1];
358 int i, j;
359
360 /* Create valid hwmon 'name' attribute */
361 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
362 if (isalnum(netname[i]))
363 hname[j++] = netname[i];
364 }
365 hname[j] = '\0';
366 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
367 hname, index);
368 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
369 ds->hwmon_name, ds, dsa_hwmon_groups);
370 if (IS_ERR(ds->hwmon_dev))
371 ds->hwmon_dev = NULL;
372 }
373#endif /* CONFIG_NET_DSA_HWMON */
374
Florian Fainellidf197192015-03-05 12:35:06 -0800375 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000376
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000377out:
Florian Fainellidf197192015-03-05 12:35:06 -0800378 return ret;
379}
380
381static struct dsa_switch *
382dsa_switch_setup(struct dsa_switch_tree *dst, int index,
383 struct device *parent, struct device *host_dev)
384{
Andrew Lunnff049552016-05-10 23:27:24 +0200385 struct dsa_chip_data *cd = dst->pd->chip + index;
Florian Fainellidf197192015-03-05 12:35:06 -0800386 struct dsa_switch_driver *drv;
387 struct dsa_switch *ds;
388 int ret;
Vivien Didelot0209d142016-04-17 13:23:55 -0400389 const char *name;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200390 void *priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800391
392 /*
393 * Probe for switch model.
394 */
Andrew Lunnff049552016-05-10 23:27:24 +0200395 drv = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
Florian Fainellidf197192015-03-05 12:35:06 -0800396 if (drv == NULL) {
397 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
398 index);
399 return ERR_PTR(-EINVAL);
400 }
401 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
402 index, name);
403
404
405 /*
406 * Allocate and initialise switch state.
407 */
Andrew Lunn5feebd02016-04-13 02:40:41 +0200408 ds = devm_kzalloc(parent, sizeof(*ds), GFP_KERNEL);
Florian Fainellidf197192015-03-05 12:35:06 -0800409 if (ds == NULL)
Florian Fainelli24595342015-05-29 10:29:46 -0700410 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800411
412 ds->dst = dst;
413 ds->index = index;
Andrew Lunnff049552016-05-10 23:27:24 +0200414 ds->cd = cd;
Florian Fainellidf197192015-03-05 12:35:06 -0800415 ds->drv = drv;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200416 ds->priv = priv;
Andrew Lunnc33063d2016-05-10 23:27:23 +0200417 ds->dev = parent;
Florian Fainellidf197192015-03-05 12:35:06 -0800418
419 ret = dsa_switch_setup_one(ds, parent);
420 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700421 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800422
423 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000424}
425
426static void dsa_switch_destroy(struct dsa_switch *ds)
427{
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100428 struct device_node *port_dn;
429 struct phy_device *phydev;
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100430 int port;
431
Guenter Roeck51579c32014-10-29 10:44:58 -0700432#ifdef CONFIG_NET_DSA_HWMON
433 if (ds->hwmon_dev)
434 hwmon_device_unregister(ds->hwmon_dev);
435#endif
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100436
Andrew Lunn3a445142016-03-12 00:01:38 +0100437 /* Destroy network devices for physical switch ports. */
438 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200439 if (!(ds->enabled_port_mask & (1 << port)))
Andrew Lunn3a445142016-03-12 00:01:38 +0100440 continue;
441
Andrew Lunnc8b09802016-06-04 21:16:57 +0200442 if (!ds->ports[port].netdev)
Andrew Lunn3a445142016-03-12 00:01:38 +0100443 continue;
444
Andrew Lunnc8b09802016-06-04 21:16:57 +0200445 dsa_slave_destroy(ds->ports[port].netdev);
Andrew Lunn3a445142016-03-12 00:01:38 +0100446 }
447
448 /* Remove any fixed link PHYs */
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100449 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn189b0d92016-06-04 21:16:58 +0200450 port_dn = ds->ports[port].dn;
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100451 if (of_phy_is_fixed_link(port_dn)) {
452 phydev = of_phy_find_device(port_dn);
453 if (phydev) {
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100454 phy_device_free(phydev);
455 of_node_put(port_dn);
Andrew Lunn5bcbe0f2016-03-12 00:01:40 +0100456 fixed_phy_unregister(phydev);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100457 }
458 }
459 }
460
Neil Armstronge410ddb2015-10-06 15:40:25 +0100461 mdiobus_unregister(ds->slave_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000462}
463
Thierry Redinge506d402014-10-01 13:59:00 +0200464#ifdef CONFIG_PM_SLEEP
Florian Fainelli24462542014-09-18 17:31:22 -0700465static int dsa_switch_suspend(struct dsa_switch *ds)
466{
467 int i, ret = 0;
468
469 /* Suspend slave network devices */
470 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800471 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700472 continue;
473
Andrew Lunnc8b09802016-06-04 21:16:57 +0200474 ret = dsa_slave_suspend(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700475 if (ret)
476 return ret;
477 }
478
479 if (ds->drv->suspend)
480 ret = ds->drv->suspend(ds);
481
482 return ret;
483}
484
485static int dsa_switch_resume(struct dsa_switch *ds)
486{
487 int i, ret = 0;
488
489 if (ds->drv->resume)
490 ret = ds->drv->resume(ds);
491
492 if (ret)
493 return ret;
494
495 /* Resume slave network devices */
496 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800497 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700498 continue;
499
Andrew Lunnc8b09802016-06-04 21:16:57 +0200500 ret = dsa_slave_resume(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700501 if (ret)
502 return ret;
503 }
504
505 return 0;
506}
Thierry Redinge506d402014-10-01 13:59:00 +0200507#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700508
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000509/* platform driver init and cleanup *****************************************/
510static int dev_is_class(struct device *dev, void *class)
511{
512 if (dev->class != NULL && !strcmp(dev->class->name, class))
513 return 1;
514
515 return 0;
516}
517
518static struct device *dev_find_class(struct device *parent, char *class)
519{
520 if (dev_is_class(parent, class)) {
521 get_device(parent);
522 return parent;
523 }
524
525 return device_find_child(parent, class, dev_is_class);
526}
527
Alexander Duyckb4d23942014-09-15 13:00:27 -0400528struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000529{
530 struct device *d;
531
532 d = dev_find_class(dev, "mdio_bus");
533 if (d != NULL) {
534 struct mii_bus *bus;
535
536 bus = to_mii_bus(d);
537 put_device(d);
538
539 return bus;
540 }
541
542 return NULL;
543}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400544EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000545
546static struct net_device *dev_to_net_device(struct device *dev)
547{
548 struct device *d;
549
550 d = dev_find_class(dev, "net");
551 if (d != NULL) {
552 struct net_device *nd;
553
554 nd = to_net_dev(d);
555 dev_hold(nd);
556 put_device(d);
557
558 return nd;
559 }
560
561 return NULL;
562}
563
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000564#ifdef CONFIG_OF
565static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
566 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300567 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000568 struct device_node *link)
569{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000570 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000571 int link_sw_addr;
572 struct device_node *parent_sw;
573 int len;
574
575 parent_sw = of_get_parent(link);
576 if (!parent_sw)
577 return -EINVAL;
578
579 reg = of_get_property(parent_sw, "reg", &len);
580 if (!reg || (len != sizeof(*reg) * 2))
581 return -EINVAL;
582
Pavel Nakonechny30303812015-04-05 00:46:21 +0300583 /*
584 * Get the destination switch number from the second field of its 'reg'
585 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
586 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000587 link_sw_addr = be32_to_cpup(reg + 1);
588
589 if (link_sw_addr >= pd->nr_chips)
590 return -EINVAL;
591
Pavel Nakonechny30303812015-04-05 00:46:21 +0300592 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000593
594 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000595}
596
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200597static int dsa_of_probe_links(struct dsa_platform_data *pd,
598 struct dsa_chip_data *cd,
599 int chip_index, int port_index,
600 struct device_node *port,
601 const char *port_name)
602{
603 struct device_node *link;
604 int link_index;
605 int ret;
606
607 for (link_index = 0;; link_index++) {
608 link = of_parse_phandle(port, "link", link_index);
609 if (!link)
610 break;
611
612 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
613 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
614 port_index, link);
615 if (ret)
616 return ret;
617 }
618 }
619 return 0;
620}
621
Florian Fainelli21168242013-03-25 05:03:39 +0000622static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
623{
624 int i;
625 int port_index;
626
627 for (i = 0; i < pd->nr_chips; i++) {
628 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000629 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200630 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000631 port_index++;
632 }
Russell Kinge496ae62015-09-24 20:35:57 +0100633
634 /* Drop our reference to the MDIO bus device */
635 if (pd->chip[i].host_dev)
636 put_device(pd->chip[i].host_dev);
Florian Fainelli21168242013-03-25 05:03:39 +0000637 }
638 kfree(pd->chip);
639}
640
Florian Fainellif1a26a02015-03-05 12:35:04 -0800641static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000642{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800643 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200644 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200645 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700646 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000647 struct dsa_platform_data *pd;
648 struct dsa_chip_data *cd;
649 const char *port_name;
650 int chip_index, port_index;
651 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700652 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000653 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000654
655 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
656 if (!mdio)
657 return -EINVAL;
658
659 mdio_bus = of_mdio_find_bus(mdio);
660 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800661 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000662
663 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
Russell Kinge496ae62015-09-24 20:35:57 +0100664 if (!ethernet) {
665 ret = -EINVAL;
666 goto out_put_mdio;
667 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000668
Florian Fainelli769a0202015-03-09 14:31:21 -0700669 ethernet_dev = of_find_net_device_by_node(ethernet);
Russell Kinge496ae62015-09-24 20:35:57 +0100670 if (!ethernet_dev) {
671 ret = -EPROBE_DEFER;
672 goto out_put_mdio;
673 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000674
675 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Russell Kinge496ae62015-09-24 20:35:57 +0100676 if (!pd) {
677 ret = -ENOMEM;
Russell King9861f722015-09-24 20:36:33 +0100678 goto out_put_ethernet;
Russell Kinge496ae62015-09-24 20:35:57 +0100679 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000680
Florian Fainellif1a26a02015-03-05 12:35:04 -0800681 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700682 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100683 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000684 if (pd->nr_chips > DSA_MAX_SWITCHES)
685 pd->nr_chips = DSA_MAX_SWITCHES;
686
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100687 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
688 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000689 if (!pd->chip) {
690 ret = -ENOMEM;
691 goto out_free;
692 }
693
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200694 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000695 for_each_available_child_of_node(np, child) {
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200696 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000697 cd = &pd->chip[chip_index];
698
Florian Fainellifa981d92014-08-27 17:04:49 -0700699 cd->of_node = child;
Russell Kinge496ae62015-09-24 20:35:57 +0100700
701 /* When assigning the host device, increment its refcount */
702 cd->host_dev = get_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000703
704 sw_addr = of_get_property(child, "reg", NULL);
705 if (!sw_addr)
706 continue;
707
708 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700709 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000710 continue;
711
Guenter Roeck50d49642015-04-29 10:56:15 -0700712 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700713 cd->eeprom_len = eeprom_len;
714
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200715 mdio = of_parse_phandle(child, "mii-bus", 0);
716 if (mdio) {
717 mdio_bus_switch = of_mdio_find_bus(mdio);
718 if (!mdio_bus_switch) {
719 ret = -EPROBE_DEFER;
720 goto out_free_chip;
721 }
Russell Kinge496ae62015-09-24 20:35:57 +0100722
723 /* Drop the mdio_bus device ref, replacing the host
724 * device with the mdio_bus_switch device, keeping
725 * the refcount from of_mdio_find_bus() above.
726 */
727 put_device(cd->host_dev);
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200728 cd->host_dev = &mdio_bus_switch->dev;
729 }
730
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000731 for_each_available_child_of_node(child, port) {
732 port_reg = of_get_property(port, "reg", NULL);
733 if (!port_reg)
734 continue;
735
736 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700737 if (port_index >= DSA_MAX_PORTS)
738 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000739
740 port_name = of_get_property(port, "label", NULL);
741 if (!port_name)
742 continue;
743
Florian Fainellibd474972014-08-27 17:04:50 -0700744 cd->port_dn[port_index] = port;
745
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000746 cd->port_names[port_index] = kstrdup(port_name,
747 GFP_KERNEL);
748 if (!cd->port_names[port_index]) {
749 ret = -ENOMEM;
750 goto out_free_chip;
751 }
752
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200753 ret = dsa_of_probe_links(pd, cd, chip_index,
754 port_index, port, port_name);
755 if (ret)
756 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000757
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000758 }
759 }
760
Russell Kinge496ae62015-09-24 20:35:57 +0100761 /* The individual chips hold their own refcount on the mdio bus,
762 * so drop ours */
763 put_device(&mdio_bus->dev);
764
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000765 return 0;
766
767out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000768 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000769out_free:
770 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800771 dev->platform_data = NULL;
Russell King9861f722015-09-24 20:36:33 +0100772out_put_ethernet:
773 put_device(&ethernet_dev->dev);
Russell Kinge496ae62015-09-24 20:35:57 +0100774out_put_mdio:
775 put_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000776 return ret;
777}
778
Florian Fainellif1a26a02015-03-05 12:35:04 -0800779static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000780{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800781 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000782
Florian Fainellif1a26a02015-03-05 12:35:04 -0800783 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000784 return;
785
Florian Fainelli21168242013-03-25 05:03:39 +0000786 dsa_of_free_platform_data(pd);
Russell King9861f722015-09-24 20:36:33 +0100787 put_device(&pd->of_netdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000788 kfree(pd);
789}
790#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800791static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000792{
793 return 0;
794}
795
Florian Fainellif1a26a02015-03-05 12:35:04 -0800796static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000797{
798}
799#endif
800
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100801static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
802 struct device *parent, struct dsa_platform_data *pd)
Florian Fainellic86e59b2015-03-05 12:35:08 -0800803{
804 int i;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100805 unsigned configured = 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800806
807 dst->pd = pd;
808 dst->master_netdev = dev;
809 dst->cpu_switch = -1;
810 dst->cpu_port = -1;
811
812 for (i = 0; i < pd->nr_chips; i++) {
813 struct dsa_switch *ds;
814
815 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
816 if (IS_ERR(ds)) {
817 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
818 i, PTR_ERR(ds));
819 continue;
820 }
821
822 dst->ds[i] = ds;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100823
824 ++configured;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800825 }
826
827 /*
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100828 * If no switch was found, exit cleanly
829 */
830 if (!configured)
831 return -EPROBE_DEFER;
832
833 /*
Florian Fainellic86e59b2015-03-05 12:35:08 -0800834 * If we use a tagging format that doesn't have an ethertype
835 * field, make sure that all packets from this point on get
836 * sent to the tag format's receive function.
837 */
838 wmb();
839 dev->dsa_ptr = (void *)dst;
840
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100841 return 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800842}
843
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000844static int dsa_probe(struct platform_device *pdev)
845{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000846 struct dsa_platform_data *pd = pdev->dev.platform_data;
847 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000848 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800849 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000850
Joe Perchesa2ae6002014-11-09 16:32:46 -0800851 pr_notice_once("Distributed Switch Architecture driver version %s\n",
852 dsa_driver_version);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000853
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000854 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800855 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000856 if (ret)
857 return ret;
858
859 pd = pdev->dev.platform_data;
860 }
861
Florian Fainelli769a0202015-03-09 14:31:21 -0700862 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000863 return -EINVAL;
864
Florian Fainelli769a0202015-03-09 14:31:21 -0700865 if (pd->of_netdev) {
866 dev = pd->of_netdev;
867 dev_hold(dev);
868 } else {
869 dev = dev_to_net_device(pd->netdev);
870 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000871 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800872 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000873 goto out;
874 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000875
876 if (dev->dsa_ptr != NULL) {
877 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000878 ret = -EEXIST;
879 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000880 }
881
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100882 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000883 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000884 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000885 ret = -ENOMEM;
886 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000887 }
888
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000889 platform_set_drvdata(pdev, dst);
890
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100891 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
Neil Armstrong679fb462015-12-07 13:57:34 +0100892 if (ret) {
893 dev_put(dev);
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100894 goto out;
Neil Armstrong679fb462015-12-07 13:57:34 +0100895 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000896
897 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000898
899out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800900 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000901
902 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000903}
904
Florian Fainellic86e59b2015-03-05 12:35:08 -0800905static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000906{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000907 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000908
Neil Armstrong04761892016-03-08 10:36:20 +0100909 dst->master_netdev->dsa_ptr = NULL;
910
911 /* If we used a tagging format that doesn't have an ethertype
912 * field, make sure that all packets from this point get sent
913 * without the tag and go through the regular receive path.
914 */
915 wmb();
916
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000917 for (i = 0; i < dst->pd->nr_chips; i++) {
918 struct dsa_switch *ds = dst->ds[i];
919
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100920 if (ds)
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000921 dsa_switch_destroy(ds);
922 }
Neil Armstrong679fb462015-12-07 13:57:34 +0100923
924 dev_put(dst->master_netdev);
Florian Fainellic86e59b2015-03-05 12:35:08 -0800925}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000926
Florian Fainellic86e59b2015-03-05 12:35:08 -0800927static int dsa_remove(struct platform_device *pdev)
928{
929 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
930
931 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800932 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000933
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000934 return 0;
935}
936
937static void dsa_shutdown(struct platform_device *pdev)
938{
939}
940
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700941static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
942 struct packet_type *pt, struct net_device *orig_dev)
943{
944 struct dsa_switch_tree *dst = dev->dsa_ptr;
945
946 if (unlikely(dst == NULL)) {
947 kfree_skb(skb);
948 return 0;
949 }
950
Alexander Duyck50753142014-09-15 13:00:19 -0400951 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700952}
953
Florian Fainelli61b73632014-08-29 12:42:07 -0700954static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700955 .type = cpu_to_be16(ETH_P_XDSA),
956 .func = dsa_switch_rcv,
957};
958
Florian Fainellib73adef2015-02-24 13:15:33 -0800959static struct notifier_block dsa_netdevice_nb __read_mostly = {
960 .notifier_call = dsa_slave_netdevice_event,
961};
962
Florian Fainelli24462542014-09-18 17:31:22 -0700963#ifdef CONFIG_PM_SLEEP
964static int dsa_suspend(struct device *d)
965{
966 struct platform_device *pdev = to_platform_device(d);
967 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
968 int i, ret = 0;
969
970 for (i = 0; i < dst->pd->nr_chips; i++) {
971 struct dsa_switch *ds = dst->ds[i];
972
973 if (ds != NULL)
974 ret = dsa_switch_suspend(ds);
975 }
976
977 return ret;
978}
979
980static int dsa_resume(struct device *d)
981{
982 struct platform_device *pdev = to_platform_device(d);
983 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
984 int i, ret = 0;
985
986 for (i = 0; i < dst->pd->nr_chips; i++) {
987 struct dsa_switch *ds = dst->ds[i];
988
989 if (ds != NULL)
990 ret = dsa_switch_resume(ds);
991 }
992
993 return ret;
994}
995#endif
996
997static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
998
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000999static const struct of_device_id dsa_of_match_table[] = {
Florian Fainelli246d7f72014-08-27 17:04:56 -07001000 { .compatible = "brcm,bcm7445-switch-v4.0" },
Florian Fainelli5e95329b2013-03-22 10:50:50 +00001001 { .compatible = "marvell,dsa", },
1002 {}
1003};
1004MODULE_DEVICE_TABLE(of, dsa_of_match_table);
1005
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001006static struct platform_driver dsa_driver = {
1007 .probe = dsa_probe,
1008 .remove = dsa_remove,
1009 .shutdown = dsa_shutdown,
1010 .driver = {
1011 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +00001012 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -07001013 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001014 },
1015};
1016
1017static int __init dsa_init_module(void)
1018{
Ben Hutchings7df899c2011-11-25 14:35:02 +00001019 int rc;
1020
Florian Fainellib73adef2015-02-24 13:15:33 -08001021 register_netdevice_notifier(&dsa_netdevice_nb);
1022
Ben Hutchings7df899c2011-11-25 14:35:02 +00001023 rc = platform_driver_register(&dsa_driver);
1024 if (rc)
1025 return rc;
1026
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001027 dev_add_pack(&dsa_pack_type);
1028
Ben Hutchings7df899c2011-11-25 14:35:02 +00001029 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001030}
1031module_init(dsa_init_module);
1032
1033static void __exit dsa_cleanup_module(void)
1034{
Florian Fainellib73adef2015-02-24 13:15:33 -08001035 unregister_netdevice_notifier(&dsa_netdevice_nb);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001036 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001037 platform_driver_unregister(&dsa_driver);
1038}
1039module_exit(dsa_cleanup_module);
1040
Rusty Russell577d6a72011-01-24 14:32:52 -06001041MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001042MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1043MODULE_LICENSE("GPL");
1044MODULE_ALIAS("platform:dsa");