blob: afff17341b738033c51903e91aeaba4716f420be [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>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000025#include "dsa_priv.h"
26
27char dsa_driver_version[] = "0.1";
28
29
30/* switch driver registration ***********************************************/
31static DEFINE_MUTEX(dsa_switch_drivers_mutex);
32static LIST_HEAD(dsa_switch_drivers);
33
34void register_switch_driver(struct dsa_switch_driver *drv)
35{
36 mutex_lock(&dsa_switch_drivers_mutex);
37 list_add_tail(&drv->list, &dsa_switch_drivers);
38 mutex_unlock(&dsa_switch_drivers_mutex);
39}
Ben Hutchingsad293b82011-11-25 14:34:07 +000040EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000041
42void unregister_switch_driver(struct dsa_switch_driver *drv)
43{
44 mutex_lock(&dsa_switch_drivers_mutex);
45 list_del_init(&drv->list);
46 mutex_unlock(&dsa_switch_drivers_mutex);
47}
Ben Hutchingsad293b82011-11-25 14:34:07 +000048EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000049
50static struct dsa_switch_driver *
Alexander Duyckb4d23942014-09-15 13:00:27 -040051dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000052{
53 struct dsa_switch_driver *ret;
54 struct list_head *list;
55 char *name;
56
57 ret = NULL;
58 name = NULL;
59
60 mutex_lock(&dsa_switch_drivers_mutex);
61 list_for_each(list, &dsa_switch_drivers) {
62 struct dsa_switch_driver *drv;
63
64 drv = list_entry(list, struct dsa_switch_driver, list);
65
Alexander Duyckb4d23942014-09-15 13:00:27 -040066 name = drv->probe(host_dev, sw_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000067 if (name != NULL) {
68 ret = drv;
69 break;
70 }
71 }
72 mutex_unlock(&dsa_switch_drivers_mutex);
73
74 *_name = name;
75
76 return ret;
77}
78
Guenter Roeck51579c32014-10-29 10:44:58 -070079/* hwmon support ************************************************************/
80
81#ifdef CONFIG_NET_DSA_HWMON
82
83static ssize_t temp1_input_show(struct device *dev,
84 struct device_attribute *attr, char *buf)
85{
86 struct dsa_switch *ds = dev_get_drvdata(dev);
87 int temp, ret;
88
89 ret = ds->drv->get_temp(ds, &temp);
90 if (ret < 0)
91 return ret;
92
93 return sprintf(buf, "%d\n", temp * 1000);
94}
95static DEVICE_ATTR_RO(temp1_input);
96
97static ssize_t temp1_max_show(struct device *dev,
98 struct device_attribute *attr, char *buf)
99{
100 struct dsa_switch *ds = dev_get_drvdata(dev);
101 int temp, ret;
102
103 ret = ds->drv->get_temp_limit(ds, &temp);
104 if (ret < 0)
105 return ret;
106
107 return sprintf(buf, "%d\n", temp * 1000);
108}
109
110static ssize_t temp1_max_store(struct device *dev,
111 struct device_attribute *attr, const char *buf,
112 size_t count)
113{
114 struct dsa_switch *ds = dev_get_drvdata(dev);
115 int temp, ret;
116
117 ret = kstrtoint(buf, 0, &temp);
118 if (ret < 0)
119 return ret;
120
121 ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000));
122 if (ret < 0)
123 return ret;
124
125 return count;
126}
Vivien Didelote3122b72015-04-17 15:12:25 -0400127static DEVICE_ATTR_RW(temp1_max);
Guenter Roeck51579c32014-10-29 10:44:58 -0700128
129static ssize_t temp1_max_alarm_show(struct device *dev,
130 struct device_attribute *attr, char *buf)
131{
132 struct dsa_switch *ds = dev_get_drvdata(dev);
133 bool alarm;
134 int ret;
135
136 ret = ds->drv->get_temp_alarm(ds, &alarm);
137 if (ret < 0)
138 return ret;
139
140 return sprintf(buf, "%d\n", alarm);
141}
142static DEVICE_ATTR_RO(temp1_max_alarm);
143
144static struct attribute *dsa_hwmon_attrs[] = {
145 &dev_attr_temp1_input.attr, /* 0 */
146 &dev_attr_temp1_max.attr, /* 1 */
147 &dev_attr_temp1_max_alarm.attr, /* 2 */
148 NULL
149};
150
151static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
152 struct attribute *attr, int index)
153{
154 struct device *dev = container_of(kobj, struct device, kobj);
155 struct dsa_switch *ds = dev_get_drvdata(dev);
156 struct dsa_switch_driver *drv = ds->drv;
157 umode_t mode = attr->mode;
158
159 if (index == 1) {
160 if (!drv->get_temp_limit)
161 mode = 0;
Vivien Didelote3122b72015-04-17 15:12:25 -0400162 else if (!drv->set_temp_limit)
163 mode &= ~S_IWUSR;
Guenter Roeck51579c32014-10-29 10:44:58 -0700164 } else if (index == 2 && !drv->get_temp_alarm) {
165 mode = 0;
166 }
167 return mode;
168}
169
170static const struct attribute_group dsa_hwmon_group = {
171 .attrs = dsa_hwmon_attrs,
172 .is_visible = dsa_hwmon_attrs_visible,
173};
174__ATTRIBUTE_GROUPS(dsa_hwmon);
175
176#endif /* CONFIG_NET_DSA_HWMON */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000177
178/* basic switch operations **************************************************/
Andrew Lunn39b0c702015-08-31 15:56:49 +0200179static int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct net_device *master)
180{
181 struct dsa_chip_data *cd = ds->pd;
182 struct device_node *port_dn;
183 struct phy_device *phydev;
184 int ret, port;
185
186 for (port = 0; port < DSA_MAX_PORTS; port++) {
187 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
188 continue;
189
190 port_dn = cd->port_dn[port];
191 if (of_phy_is_fixed_link(port_dn)) {
192 ret = of_phy_register_fixed_link(port_dn);
193 if (ret) {
194 netdev_err(master,
195 "failed to register fixed PHY\n");
196 return ret;
197 }
198 phydev = of_phy_find_device(port_dn);
199 genphy_config_init(phydev);
200 genphy_read_status(phydev);
201 if (ds->drv->adjust_link)
202 ds->drv->adjust_link(ds, port, phydev);
203 }
204 }
205 return 0;
206}
207
Florian Fainellidf197192015-03-05 12:35:06 -0800208static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000209{
Florian Fainellidf197192015-03-05 12:35:06 -0800210 struct dsa_switch_driver *drv = ds->drv;
211 struct dsa_switch_tree *dst = ds->dst;
212 struct dsa_chip_data *pd = ds->pd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000213 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800214 int index = ds->index;
215 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000216
217 /*
218 * Validate supplied switch configuration.
219 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000220 for (i = 0; i < DSA_MAX_PORTS; i++) {
221 char *name;
222
223 name = pd->port_names[i];
224 if (name == NULL)
225 continue;
226
227 if (!strcmp(name, "cpu")) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000228 if (dst->cpu_switch != -1) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800229 netdev_err(dst->master_netdev,
230 "multiple cpu ports?!\n");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000231 ret = -EINVAL;
232 goto out;
233 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000234 dst->cpu_switch = index;
235 dst->cpu_port = i;
236 } else if (!strcmp(name, "dsa")) {
237 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000238 } else {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000239 ds->phys_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000240 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000241 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000242 }
243
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000244 if (!valid_name_found && i == DSA_MAX_PORTS) {
245 ret = -EINVAL;
246 goto out;
247 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000248
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700249 /* Make the built-in MII bus mask match the number of ports,
250 * switch drivers can override this later
251 */
252 ds->phys_mii_mask = ds->phys_port_mask;
253
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000254 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000255 * If the CPU connects to this switch, set the switch tree
256 * tagging protocol to the preferred tagging format of this
257 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000258 */
Alexander Duyck50753142014-09-15 13:00:19 -0400259 if (dst->cpu_switch == index) {
Florian Fainelli59299032015-03-05 12:35:07 -0800260 switch (ds->tag_protocol) {
Alexander Duyck50753142014-09-15 13:00:19 -0400261#ifdef CONFIG_NET_DSA_TAG_DSA
262 case DSA_TAG_PROTO_DSA:
263 dst->rcv = dsa_netdev_ops.rcv;
264 break;
265#endif
266#ifdef CONFIG_NET_DSA_TAG_EDSA
267 case DSA_TAG_PROTO_EDSA:
268 dst->rcv = edsa_netdev_ops.rcv;
269 break;
270#endif
271#ifdef CONFIG_NET_DSA_TAG_TRAILER
272 case DSA_TAG_PROTO_TRAILER:
273 dst->rcv = trailer_netdev_ops.rcv;
274 break;
275#endif
276#ifdef CONFIG_NET_DSA_TAG_BRCM
277 case DSA_TAG_PROTO_BRCM:
278 dst->rcv = brcm_netdev_ops.rcv;
279 break;
280#endif
Andrew Lunnae439282014-10-24 23:44:04 +0200281 case DSA_TAG_PROTO_NONE:
Alexander Duyck50753142014-09-15 13:00:19 -0400282 break;
Andrew Lunnae439282014-10-24 23:44:04 +0200283 default:
284 ret = -ENOPROTOOPT;
285 goto out;
Alexander Duyck50753142014-09-15 13:00:19 -0400286 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000287
Florian Fainelli59299032015-03-05 12:35:07 -0800288 dst->tag_protocol = ds->tag_protocol;
Alexander Duyck50753142014-09-15 13:00:19 -0400289 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000290
291 /*
292 * Do basic register setup.
293 */
294 ret = drv->setup(ds);
295 if (ret < 0)
296 goto out;
297
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000298 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000299 if (ret < 0)
300 goto out;
301
302 ds->slave_mii_bus = mdiobus_alloc();
303 if (ds->slave_mii_bus == NULL) {
304 ret = -ENOMEM;
305 goto out;
306 }
307 dsa_slave_mii_bus_init(ds);
308
309 ret = mdiobus_register(ds->slave_mii_bus);
310 if (ret < 0)
311 goto out_free;
312
313
314 /*
315 * Create network devices for physical switch ports.
316 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000317 for (i = 0; i < DSA_MAX_PORTS; i++) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000318 if (!(ds->phys_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000319 continue;
320
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800321 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
322 if (ret < 0) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800323 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
324 index, i, pd->port_names[i]);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800325 ret = 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000326 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000327 }
328
Andrew Lunn39b0c702015-08-31 15:56:49 +0200329 /* Perform configuration of the CPU and DSA ports */
330 ret = dsa_cpu_dsa_setup(ds, dst->master_netdev);
331 if (ret < 0) {
332 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
333 index);
334 ret = 0;
335 }
336
Guenter Roeck51579c32014-10-29 10:44:58 -0700337#ifdef CONFIG_NET_DSA_HWMON
338 /* If the switch provides a temperature sensor,
339 * register with hardware monitoring subsystem.
340 * Treat registration error as non-fatal and ignore it.
341 */
342 if (drv->get_temp) {
343 const char *netname = netdev_name(dst->master_netdev);
344 char hname[IFNAMSIZ + 1];
345 int i, j;
346
347 /* Create valid hwmon 'name' attribute */
348 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
349 if (isalnum(netname[i]))
350 hname[j++] = netname[i];
351 }
352 hname[j] = '\0';
353 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
354 hname, index);
355 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
356 ds->hwmon_name, ds, dsa_hwmon_groups);
357 if (IS_ERR(ds->hwmon_dev))
358 ds->hwmon_dev = NULL;
359 }
360#endif /* CONFIG_NET_DSA_HWMON */
361
Florian Fainellidf197192015-03-05 12:35:06 -0800362 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000363
364out_free:
365 mdiobus_free(ds->slave_mii_bus);
366out:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000367 kfree(ds);
Florian Fainellidf197192015-03-05 12:35:06 -0800368 return ret;
369}
370
371static struct dsa_switch *
372dsa_switch_setup(struct dsa_switch_tree *dst, int index,
373 struct device *parent, struct device *host_dev)
374{
375 struct dsa_chip_data *pd = dst->pd->chip + index;
376 struct dsa_switch_driver *drv;
377 struct dsa_switch *ds;
378 int ret;
379 char *name;
380
381 /*
382 * Probe for switch model.
383 */
384 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
385 if (drv == NULL) {
386 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
387 index);
388 return ERR_PTR(-EINVAL);
389 }
390 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
391 index, name);
392
393
394 /*
395 * Allocate and initialise switch state.
396 */
397 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
398 if (ds == NULL)
Florian Fainelli24595342015-05-29 10:29:46 -0700399 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800400
401 ds->dst = dst;
402 ds->index = index;
403 ds->pd = pd;
404 ds->drv = drv;
Florian Fainelli59299032015-03-05 12:35:07 -0800405 ds->tag_protocol = drv->tag_protocol;
Florian Fainellidf197192015-03-05 12:35:06 -0800406 ds->master_dev = host_dev;
407
408 ret = dsa_switch_setup_one(ds, parent);
409 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700410 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800411
412 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000413}
414
415static void dsa_switch_destroy(struct dsa_switch *ds)
416{
Guenter Roeck51579c32014-10-29 10:44:58 -0700417#ifdef CONFIG_NET_DSA_HWMON
418 if (ds->hwmon_dev)
419 hwmon_device_unregister(ds->hwmon_dev);
420#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000421}
422
Thierry Redinge506d402014-10-01 13:59:00 +0200423#ifdef CONFIG_PM_SLEEP
Florian Fainelli24462542014-09-18 17:31:22 -0700424static int dsa_switch_suspend(struct dsa_switch *ds)
425{
426 int i, ret = 0;
427
428 /* Suspend slave network devices */
429 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800430 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700431 continue;
432
433 ret = dsa_slave_suspend(ds->ports[i]);
434 if (ret)
435 return ret;
436 }
437
438 if (ds->drv->suspend)
439 ret = ds->drv->suspend(ds);
440
441 return ret;
442}
443
444static int dsa_switch_resume(struct dsa_switch *ds)
445{
446 int i, ret = 0;
447
448 if (ds->drv->resume)
449 ret = ds->drv->resume(ds);
450
451 if (ret)
452 return ret;
453
454 /* Resume slave network devices */
455 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800456 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700457 continue;
458
459 ret = dsa_slave_resume(ds->ports[i]);
460 if (ret)
461 return ret;
462 }
463
464 return 0;
465}
Thierry Redinge506d402014-10-01 13:59:00 +0200466#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700467
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000468
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000469/* link polling *************************************************************/
470static void dsa_link_poll_work(struct work_struct *ugly)
471{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000472 struct dsa_switch_tree *dst;
473 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000474
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000475 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000476
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000477 for (i = 0; i < dst->pd->nr_chips; i++) {
478 struct dsa_switch *ds = dst->ds[i];
479
480 if (ds != NULL && ds->drv->poll_link != NULL)
481 ds->drv->poll_link(ds);
482 }
483
484 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000485}
486
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000487static void dsa_link_poll_timer(unsigned long _dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000488{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000489 struct dsa_switch_tree *dst = (void *)_dst;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000490
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000491 schedule_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000492}
493
494
495/* platform driver init and cleanup *****************************************/
496static int dev_is_class(struct device *dev, void *class)
497{
498 if (dev->class != NULL && !strcmp(dev->class->name, class))
499 return 1;
500
501 return 0;
502}
503
504static struct device *dev_find_class(struct device *parent, char *class)
505{
506 if (dev_is_class(parent, class)) {
507 get_device(parent);
508 return parent;
509 }
510
511 return device_find_child(parent, class, dev_is_class);
512}
513
Alexander Duyckb4d23942014-09-15 13:00:27 -0400514struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000515{
516 struct device *d;
517
518 d = dev_find_class(dev, "mdio_bus");
519 if (d != NULL) {
520 struct mii_bus *bus;
521
522 bus = to_mii_bus(d);
523 put_device(d);
524
525 return bus;
526 }
527
528 return NULL;
529}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400530EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000531
532static struct net_device *dev_to_net_device(struct device *dev)
533{
534 struct device *d;
535
536 d = dev_find_class(dev, "net");
537 if (d != NULL) {
538 struct net_device *nd;
539
540 nd = to_net_dev(d);
541 dev_hold(nd);
542 put_device(d);
543
544 return nd;
545 }
546
547 return NULL;
548}
549
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000550#ifdef CONFIG_OF
551static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
552 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300553 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000554 struct device_node *link)
555{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000556 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000557 int link_sw_addr;
558 struct device_node *parent_sw;
559 int len;
560
561 parent_sw = of_get_parent(link);
562 if (!parent_sw)
563 return -EINVAL;
564
565 reg = of_get_property(parent_sw, "reg", &len);
566 if (!reg || (len != sizeof(*reg) * 2))
567 return -EINVAL;
568
Pavel Nakonechny30303812015-04-05 00:46:21 +0300569 /*
570 * Get the destination switch number from the second field of its 'reg'
571 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
572 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000573 link_sw_addr = be32_to_cpup(reg + 1);
574
575 if (link_sw_addr >= pd->nr_chips)
576 return -EINVAL;
577
578 /* First time routing table allocation */
579 if (!cd->rtable) {
Fabian Frederick5bc4b462014-11-14 19:36:42 +0100580 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
581 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000582 if (!cd->rtable)
583 return -ENOMEM;
584
585 /* default to no valid uplink/downlink */
586 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
587 }
588
Pavel Nakonechny30303812015-04-05 00:46:21 +0300589 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000590
591 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000592}
593
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200594static int dsa_of_probe_links(struct dsa_platform_data *pd,
595 struct dsa_chip_data *cd,
596 int chip_index, int port_index,
597 struct device_node *port,
598 const char *port_name)
599{
600 struct device_node *link;
601 int link_index;
602 int ret;
603
604 for (link_index = 0;; link_index++) {
605 link = of_parse_phandle(port, "link", link_index);
606 if (!link)
607 break;
608
609 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
610 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
611 port_index, link);
612 if (ret)
613 return ret;
614 }
615 }
616 return 0;
617}
618
Florian Fainelli21168242013-03-25 05:03:39 +0000619static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
620{
621 int i;
622 int port_index;
623
624 for (i = 0; i < pd->nr_chips; i++) {
625 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000626 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200627 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000628 port_index++;
629 }
Florian Fainelli21168242013-03-25 05:03:39 +0000630 kfree(pd->chip[i].rtable);
631 }
632 kfree(pd->chip);
633}
634
Florian Fainellif1a26a02015-03-05 12:35:04 -0800635static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000636{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800637 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200638 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200639 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700640 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000641 struct dsa_platform_data *pd;
642 struct dsa_chip_data *cd;
643 const char *port_name;
644 int chip_index, port_index;
645 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700646 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000647 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000648
649 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
650 if (!mdio)
651 return -EINVAL;
652
653 mdio_bus = of_mdio_find_bus(mdio);
654 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800655 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000656
657 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
658 if (!ethernet)
659 return -EINVAL;
660
Florian Fainelli769a0202015-03-09 14:31:21 -0700661 ethernet_dev = of_find_net_device_by_node(ethernet);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000662 if (!ethernet_dev)
Florian Fainellib324c072015-03-05 12:35:05 -0800663 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000664
665 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
666 if (!pd)
667 return -ENOMEM;
668
Florian Fainellif1a26a02015-03-05 12:35:04 -0800669 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700670 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100671 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000672 if (pd->nr_chips > DSA_MAX_SWITCHES)
673 pd->nr_chips = DSA_MAX_SWITCHES;
674
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100675 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
676 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000677 if (!pd->chip) {
678 ret = -ENOMEM;
679 goto out_free;
680 }
681
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200682 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000683 for_each_available_child_of_node(np, child) {
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200684 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000685 cd = &pd->chip[chip_index];
686
Florian Fainellifa981d92014-08-27 17:04:49 -0700687 cd->of_node = child;
Florian Fainellic1f570a2014-09-15 14:48:08 -0700688 cd->host_dev = &mdio_bus->dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000689
690 sw_addr = of_get_property(child, "reg", NULL);
691 if (!sw_addr)
692 continue;
693
694 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700695 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000696 continue;
697
Guenter Roeck50d49642015-04-29 10:56:15 -0700698 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700699 cd->eeprom_len = eeprom_len;
700
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200701 mdio = of_parse_phandle(child, "mii-bus", 0);
702 if (mdio) {
703 mdio_bus_switch = of_mdio_find_bus(mdio);
704 if (!mdio_bus_switch) {
705 ret = -EPROBE_DEFER;
706 goto out_free_chip;
707 }
708 cd->host_dev = &mdio_bus_switch->dev;
709 }
710
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000711 for_each_available_child_of_node(child, port) {
712 port_reg = of_get_property(port, "reg", NULL);
713 if (!port_reg)
714 continue;
715
716 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700717 if (port_index >= DSA_MAX_PORTS)
718 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000719
720 port_name = of_get_property(port, "label", NULL);
721 if (!port_name)
722 continue;
723
Florian Fainellibd474972014-08-27 17:04:50 -0700724 cd->port_dn[port_index] = port;
725
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000726 cd->port_names[port_index] = kstrdup(port_name,
727 GFP_KERNEL);
728 if (!cd->port_names[port_index]) {
729 ret = -ENOMEM;
730 goto out_free_chip;
731 }
732
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200733 ret = dsa_of_probe_links(pd, cd, chip_index,
734 port_index, port, port_name);
735 if (ret)
736 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000737
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000738 }
739 }
740
741 return 0;
742
743out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000744 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000745out_free:
746 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800747 dev->platform_data = NULL;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000748 return ret;
749}
750
Florian Fainellif1a26a02015-03-05 12:35:04 -0800751static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000752{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800753 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000754
Florian Fainellif1a26a02015-03-05 12:35:04 -0800755 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000756 return;
757
Florian Fainelli21168242013-03-25 05:03:39 +0000758 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000759 kfree(pd);
760}
761#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800762static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000763{
764 return 0;
765}
766
Florian Fainellif1a26a02015-03-05 12:35:04 -0800767static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000768{
769}
770#endif
771
Florian Fainellic86e59b2015-03-05 12:35:08 -0800772static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
773 struct device *parent, struct dsa_platform_data *pd)
774{
775 int i;
776
777 dst->pd = pd;
778 dst->master_netdev = dev;
779 dst->cpu_switch = -1;
780 dst->cpu_port = -1;
781
782 for (i = 0; i < pd->nr_chips; i++) {
783 struct dsa_switch *ds;
784
785 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
786 if (IS_ERR(ds)) {
787 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
788 i, PTR_ERR(ds));
789 continue;
790 }
791
792 dst->ds[i] = ds;
793 if (ds->drv->poll_link != NULL)
794 dst->link_poll_needed = 1;
795 }
796
797 /*
798 * If we use a tagging format that doesn't have an ethertype
799 * field, make sure that all packets from this point on get
800 * sent to the tag format's receive function.
801 */
802 wmb();
803 dev->dsa_ptr = (void *)dst;
804
805 if (dst->link_poll_needed) {
806 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
807 init_timer(&dst->link_poll_timer);
808 dst->link_poll_timer.data = (unsigned long)dst;
809 dst->link_poll_timer.function = dsa_link_poll_timer;
810 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
811 add_timer(&dst->link_poll_timer);
812 }
813}
814
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000815static int dsa_probe(struct platform_device *pdev)
816{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000817 struct dsa_platform_data *pd = pdev->dev.platform_data;
818 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000819 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800820 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000821
Joe Perchesa2ae6002014-11-09 16:32:46 -0800822 pr_notice_once("Distributed Switch Architecture driver version %s\n",
823 dsa_driver_version);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000824
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000825 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800826 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000827 if (ret)
828 return ret;
829
830 pd = pdev->dev.platform_data;
831 }
832
Florian Fainelli769a0202015-03-09 14:31:21 -0700833 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000834 return -EINVAL;
835
Florian Fainelli769a0202015-03-09 14:31:21 -0700836 if (pd->of_netdev) {
837 dev = pd->of_netdev;
838 dev_hold(dev);
839 } else {
840 dev = dev_to_net_device(pd->netdev);
841 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000842 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800843 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000844 goto out;
845 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000846
847 if (dev->dsa_ptr != NULL) {
848 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000849 ret = -EEXIST;
850 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000851 }
852
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000853 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
854 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000855 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000856 ret = -ENOMEM;
857 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000858 }
859
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000860 platform_set_drvdata(pdev, dst);
861
Florian Fainellic86e59b2015-03-05 12:35:08 -0800862 dsa_setup_dst(dst, dev, &pdev->dev, pd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000863
864 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000865
866out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800867 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000868
869 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000870}
871
Florian Fainellic86e59b2015-03-05 12:35:08 -0800872static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000873{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000874 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000875
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000876 if (dst->link_poll_needed)
877 del_timer_sync(&dst->link_poll_timer);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000878
Tejun Heo43829732012-08-20 14:51:24 -0700879 flush_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000880
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000881 for (i = 0; i < dst->pd->nr_chips; i++) {
882 struct dsa_switch *ds = dst->ds[i];
883
884 if (ds != NULL)
885 dsa_switch_destroy(ds);
886 }
Florian Fainellic86e59b2015-03-05 12:35:08 -0800887}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000888
Florian Fainellic86e59b2015-03-05 12:35:08 -0800889static int dsa_remove(struct platform_device *pdev)
890{
891 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
892
893 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800894 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000895
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000896 return 0;
897}
898
899static void dsa_shutdown(struct platform_device *pdev)
900{
901}
902
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700903static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
904 struct packet_type *pt, struct net_device *orig_dev)
905{
906 struct dsa_switch_tree *dst = dev->dsa_ptr;
907
908 if (unlikely(dst == NULL)) {
909 kfree_skb(skb);
910 return 0;
911 }
912
Alexander Duyck50753142014-09-15 13:00:19 -0400913 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700914}
915
Florian Fainelli61b73632014-08-29 12:42:07 -0700916static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700917 .type = cpu_to_be16(ETH_P_XDSA),
918 .func = dsa_switch_rcv,
919};
920
Florian Fainellib73adef2015-02-24 13:15:33 -0800921static struct notifier_block dsa_netdevice_nb __read_mostly = {
922 .notifier_call = dsa_slave_netdevice_event,
923};
924
Florian Fainelli24462542014-09-18 17:31:22 -0700925#ifdef CONFIG_PM_SLEEP
926static int dsa_suspend(struct device *d)
927{
928 struct platform_device *pdev = to_platform_device(d);
929 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
930 int i, ret = 0;
931
932 for (i = 0; i < dst->pd->nr_chips; i++) {
933 struct dsa_switch *ds = dst->ds[i];
934
935 if (ds != NULL)
936 ret = dsa_switch_suspend(ds);
937 }
938
939 return ret;
940}
941
942static int dsa_resume(struct device *d)
943{
944 struct platform_device *pdev = to_platform_device(d);
945 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
946 int i, ret = 0;
947
948 for (i = 0; i < dst->pd->nr_chips; i++) {
949 struct dsa_switch *ds = dst->ds[i];
950
951 if (ds != NULL)
952 ret = dsa_switch_resume(ds);
953 }
954
955 return ret;
956}
957#endif
958
959static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
960
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000961static const struct of_device_id dsa_of_match_table[] = {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700962 { .compatible = "brcm,bcm7445-switch-v4.0" },
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000963 { .compatible = "marvell,dsa", },
964 {}
965};
966MODULE_DEVICE_TABLE(of, dsa_of_match_table);
967
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000968static struct platform_driver dsa_driver = {
969 .probe = dsa_probe,
970 .remove = dsa_remove,
971 .shutdown = dsa_shutdown,
972 .driver = {
973 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000974 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700975 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000976 },
977};
978
979static int __init dsa_init_module(void)
980{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000981 int rc;
982
Florian Fainellib73adef2015-02-24 13:15:33 -0800983 register_netdevice_notifier(&dsa_netdevice_nb);
984
Ben Hutchings7df899c2011-11-25 14:35:02 +0000985 rc = platform_driver_register(&dsa_driver);
986 if (rc)
987 return rc;
988
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700989 dev_add_pack(&dsa_pack_type);
990
Ben Hutchings7df899c2011-11-25 14:35:02 +0000991 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000992}
993module_init(dsa_init_module);
994
995static void __exit dsa_cleanup_module(void)
996{
Florian Fainellib73adef2015-02-24 13:15:33 -0800997 unregister_netdevice_notifier(&dsa_netdevice_nb);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700998 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000999 platform_driver_unregister(&dsa_driver);
1000}
1001module_exit(dsa_cleanup_module);
1002
Rusty Russell577d6a72011-01-24 14:32:52 -06001003MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001004MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1005MODULE_LICENSE("GPL");
1006MODULE_ALIAS("platform:dsa");