blob: 053eb2b8e68281e778f8f605bb3d5ad9b83ae473 [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 **************************************************/
Florian Fainellidf197192015-03-05 12:35:06 -0800179static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000180{
Florian Fainellidf197192015-03-05 12:35:06 -0800181 struct dsa_switch_driver *drv = ds->drv;
182 struct dsa_switch_tree *dst = ds->dst;
183 struct dsa_chip_data *pd = ds->pd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000184 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800185 int index = ds->index;
186 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000187
188 /*
189 * Validate supplied switch configuration.
190 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000191 for (i = 0; i < DSA_MAX_PORTS; i++) {
192 char *name;
193
194 name = pd->port_names[i];
195 if (name == NULL)
196 continue;
197
198 if (!strcmp(name, "cpu")) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000199 if (dst->cpu_switch != -1) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800200 netdev_err(dst->master_netdev,
201 "multiple cpu ports?!\n");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000202 ret = -EINVAL;
203 goto out;
204 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000205 dst->cpu_switch = index;
206 dst->cpu_port = i;
207 } else if (!strcmp(name, "dsa")) {
208 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000209 } else {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000210 ds->phys_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000211 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000212 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000213 }
214
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000215 if (!valid_name_found && i == DSA_MAX_PORTS) {
216 ret = -EINVAL;
217 goto out;
218 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000219
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700220 /* Make the built-in MII bus mask match the number of ports,
221 * switch drivers can override this later
222 */
223 ds->phys_mii_mask = ds->phys_port_mask;
224
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000225 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000226 * If the CPU connects to this switch, set the switch tree
227 * tagging protocol to the preferred tagging format of this
228 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000229 */
Alexander Duyck50753142014-09-15 13:00:19 -0400230 if (dst->cpu_switch == index) {
Florian Fainelli59299032015-03-05 12:35:07 -0800231 switch (ds->tag_protocol) {
Alexander Duyck50753142014-09-15 13:00:19 -0400232#ifdef CONFIG_NET_DSA_TAG_DSA
233 case DSA_TAG_PROTO_DSA:
234 dst->rcv = dsa_netdev_ops.rcv;
235 break;
236#endif
237#ifdef CONFIG_NET_DSA_TAG_EDSA
238 case DSA_TAG_PROTO_EDSA:
239 dst->rcv = edsa_netdev_ops.rcv;
240 break;
241#endif
242#ifdef CONFIG_NET_DSA_TAG_TRAILER
243 case DSA_TAG_PROTO_TRAILER:
244 dst->rcv = trailer_netdev_ops.rcv;
245 break;
246#endif
247#ifdef CONFIG_NET_DSA_TAG_BRCM
248 case DSA_TAG_PROTO_BRCM:
249 dst->rcv = brcm_netdev_ops.rcv;
250 break;
251#endif
Andrew Lunnae439282014-10-24 23:44:04 +0200252 case DSA_TAG_PROTO_NONE:
Alexander Duyck50753142014-09-15 13:00:19 -0400253 break;
Andrew Lunnae439282014-10-24 23:44:04 +0200254 default:
255 ret = -ENOPROTOOPT;
256 goto out;
Alexander Duyck50753142014-09-15 13:00:19 -0400257 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000258
Florian Fainelli59299032015-03-05 12:35:07 -0800259 dst->tag_protocol = ds->tag_protocol;
Alexander Duyck50753142014-09-15 13:00:19 -0400260 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000261
262 /*
263 * Do basic register setup.
264 */
265 ret = drv->setup(ds);
266 if (ret < 0)
267 goto out;
268
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000269 ret = drv->set_addr(ds, dst->master_netdev->dev_addr);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000270 if (ret < 0)
271 goto out;
272
273 ds->slave_mii_bus = mdiobus_alloc();
274 if (ds->slave_mii_bus == NULL) {
275 ret = -ENOMEM;
276 goto out;
277 }
278 dsa_slave_mii_bus_init(ds);
279
280 ret = mdiobus_register(ds->slave_mii_bus);
281 if (ret < 0)
282 goto out_free;
283
284
285 /*
286 * Create network devices for physical switch ports.
287 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000288 for (i = 0; i < DSA_MAX_PORTS; i++) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000289 if (!(ds->phys_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000290 continue;
291
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800292 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
293 if (ret < 0) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800294 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
295 index, i, pd->port_names[i]);
Guenter Roeckd87d6f42015-02-24 13:15:32 -0800296 ret = 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000297 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000298 }
299
Guenter Roeck51579c32014-10-29 10:44:58 -0700300#ifdef CONFIG_NET_DSA_HWMON
301 /* If the switch provides a temperature sensor,
302 * register with hardware monitoring subsystem.
303 * Treat registration error as non-fatal and ignore it.
304 */
305 if (drv->get_temp) {
306 const char *netname = netdev_name(dst->master_netdev);
307 char hname[IFNAMSIZ + 1];
308 int i, j;
309
310 /* Create valid hwmon 'name' attribute */
311 for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) {
312 if (isalnum(netname[i]))
313 hname[j++] = netname[i];
314 }
315 hname[j] = '\0';
316 scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d",
317 hname, index);
318 ds->hwmon_dev = hwmon_device_register_with_groups(NULL,
319 ds->hwmon_name, ds, dsa_hwmon_groups);
320 if (IS_ERR(ds->hwmon_dev))
321 ds->hwmon_dev = NULL;
322 }
323#endif /* CONFIG_NET_DSA_HWMON */
324
Florian Fainellidf197192015-03-05 12:35:06 -0800325 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000326
327out_free:
328 mdiobus_free(ds->slave_mii_bus);
329out:
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000330 kfree(ds);
Florian Fainellidf197192015-03-05 12:35:06 -0800331 return ret;
332}
333
334static struct dsa_switch *
335dsa_switch_setup(struct dsa_switch_tree *dst, int index,
336 struct device *parent, struct device *host_dev)
337{
338 struct dsa_chip_data *pd = dst->pd->chip + index;
339 struct dsa_switch_driver *drv;
340 struct dsa_switch *ds;
341 int ret;
342 char *name;
343
344 /*
345 * Probe for switch model.
346 */
347 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
348 if (drv == NULL) {
349 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
350 index);
351 return ERR_PTR(-EINVAL);
352 }
353 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
354 index, name);
355
356
357 /*
358 * Allocate and initialise switch state.
359 */
360 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
361 if (ds == NULL)
Florian Fainelli24595342015-05-29 10:29:46 -0700362 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800363
364 ds->dst = dst;
365 ds->index = index;
366 ds->pd = pd;
367 ds->drv = drv;
Florian Fainelli59299032015-03-05 12:35:07 -0800368 ds->tag_protocol = drv->tag_protocol;
Florian Fainellidf197192015-03-05 12:35:06 -0800369 ds->master_dev = host_dev;
370
371 ret = dsa_switch_setup_one(ds, parent);
372 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700373 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800374
375 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000376}
377
378static void dsa_switch_destroy(struct dsa_switch *ds)
379{
Guenter Roeck51579c32014-10-29 10:44:58 -0700380#ifdef CONFIG_NET_DSA_HWMON
381 if (ds->hwmon_dev)
382 hwmon_device_unregister(ds->hwmon_dev);
383#endif
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000384}
385
Thierry Redinge506d402014-10-01 13:59:00 +0200386#ifdef CONFIG_PM_SLEEP
Florian Fainelli24462542014-09-18 17:31:22 -0700387static int dsa_switch_suspend(struct dsa_switch *ds)
388{
389 int i, ret = 0;
390
391 /* Suspend slave network devices */
392 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800393 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700394 continue;
395
396 ret = dsa_slave_suspend(ds->ports[i]);
397 if (ret)
398 return ret;
399 }
400
401 if (ds->drv->suspend)
402 ret = ds->drv->suspend(ds);
403
404 return ret;
405}
406
407static int dsa_switch_resume(struct dsa_switch *ds)
408{
409 int i, ret = 0;
410
411 if (ds->drv->resume)
412 ret = ds->drv->resume(ds);
413
414 if (ret)
415 return ret;
416
417 /* Resume slave network devices */
418 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800419 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700420 continue;
421
422 ret = dsa_slave_resume(ds->ports[i]);
423 if (ret)
424 return ret;
425 }
426
427 return 0;
428}
Thierry Redinge506d402014-10-01 13:59:00 +0200429#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700430
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000431
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000432/* link polling *************************************************************/
433static void dsa_link_poll_work(struct work_struct *ugly)
434{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000435 struct dsa_switch_tree *dst;
436 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000437
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000438 dst = container_of(ugly, struct dsa_switch_tree, link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000439
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000440 for (i = 0; i < dst->pd->nr_chips; i++) {
441 struct dsa_switch *ds = dst->ds[i];
442
443 if (ds != NULL && ds->drv->poll_link != NULL)
444 ds->drv->poll_link(ds);
445 }
446
447 mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000448}
449
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000450static void dsa_link_poll_timer(unsigned long _dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000451{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000452 struct dsa_switch_tree *dst = (void *)_dst;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000453
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000454 schedule_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000455}
456
457
458/* platform driver init and cleanup *****************************************/
459static int dev_is_class(struct device *dev, void *class)
460{
461 if (dev->class != NULL && !strcmp(dev->class->name, class))
462 return 1;
463
464 return 0;
465}
466
467static struct device *dev_find_class(struct device *parent, char *class)
468{
469 if (dev_is_class(parent, class)) {
470 get_device(parent);
471 return parent;
472 }
473
474 return device_find_child(parent, class, dev_is_class);
475}
476
Alexander Duyckb4d23942014-09-15 13:00:27 -0400477struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000478{
479 struct device *d;
480
481 d = dev_find_class(dev, "mdio_bus");
482 if (d != NULL) {
483 struct mii_bus *bus;
484
485 bus = to_mii_bus(d);
486 put_device(d);
487
488 return bus;
489 }
490
491 return NULL;
492}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400493EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000494
495static struct net_device *dev_to_net_device(struct device *dev)
496{
497 struct device *d;
498
499 d = dev_find_class(dev, "net");
500 if (d != NULL) {
501 struct net_device *nd;
502
503 nd = to_net_dev(d);
504 dev_hold(nd);
505 put_device(d);
506
507 return nd;
508 }
509
510 return NULL;
511}
512
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000513#ifdef CONFIG_OF
514static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
515 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300516 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000517 struct device_node *link)
518{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000519 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000520 int link_sw_addr;
521 struct device_node *parent_sw;
522 int len;
523
524 parent_sw = of_get_parent(link);
525 if (!parent_sw)
526 return -EINVAL;
527
528 reg = of_get_property(parent_sw, "reg", &len);
529 if (!reg || (len != sizeof(*reg) * 2))
530 return -EINVAL;
531
Pavel Nakonechny30303812015-04-05 00:46:21 +0300532 /*
533 * Get the destination switch number from the second field of its 'reg'
534 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
535 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000536 link_sw_addr = be32_to_cpup(reg + 1);
537
538 if (link_sw_addr >= pd->nr_chips)
539 return -EINVAL;
540
541 /* First time routing table allocation */
542 if (!cd->rtable) {
Fabian Frederick5bc4b462014-11-14 19:36:42 +0100543 cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8),
544 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000545 if (!cd->rtable)
546 return -ENOMEM;
547
548 /* default to no valid uplink/downlink */
549 memset(cd->rtable, -1, pd->nr_chips * sizeof(s8));
550 }
551
Pavel Nakonechny30303812015-04-05 00:46:21 +0300552 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000553
554 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000555}
556
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200557static int dsa_of_probe_links(struct dsa_platform_data *pd,
558 struct dsa_chip_data *cd,
559 int chip_index, int port_index,
560 struct device_node *port,
561 const char *port_name)
562{
563 struct device_node *link;
564 int link_index;
565 int ret;
566
567 for (link_index = 0;; link_index++) {
568 link = of_parse_phandle(port, "link", link_index);
569 if (!link)
570 break;
571
572 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
573 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
574 port_index, link);
575 if (ret)
576 return ret;
577 }
578 }
579 return 0;
580}
581
Florian Fainelli21168242013-03-25 05:03:39 +0000582static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
583{
584 int i;
585 int port_index;
586
587 for (i = 0; i < pd->nr_chips; i++) {
588 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000589 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200590 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000591 port_index++;
592 }
Florian Fainelli21168242013-03-25 05:03:39 +0000593 kfree(pd->chip[i].rtable);
594 }
595 kfree(pd->chip);
596}
597
Florian Fainellif1a26a02015-03-05 12:35:04 -0800598static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000599{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800600 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200601 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200602 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700603 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000604 struct dsa_platform_data *pd;
605 struct dsa_chip_data *cd;
606 const char *port_name;
607 int chip_index, port_index;
608 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700609 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000610 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000611
612 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
613 if (!mdio)
614 return -EINVAL;
615
616 mdio_bus = of_mdio_find_bus(mdio);
617 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800618 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000619
620 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
621 if (!ethernet)
622 return -EINVAL;
623
Florian Fainelli769a0202015-03-09 14:31:21 -0700624 ethernet_dev = of_find_net_device_by_node(ethernet);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000625 if (!ethernet_dev)
Florian Fainellib324c072015-03-05 12:35:05 -0800626 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000627
628 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
629 if (!pd)
630 return -ENOMEM;
631
Florian Fainellif1a26a02015-03-05 12:35:04 -0800632 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700633 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100634 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000635 if (pd->nr_chips > DSA_MAX_SWITCHES)
636 pd->nr_chips = DSA_MAX_SWITCHES;
637
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100638 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
639 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000640 if (!pd->chip) {
641 ret = -ENOMEM;
642 goto out_free;
643 }
644
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200645 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000646 for_each_available_child_of_node(np, child) {
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200647 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000648 cd = &pd->chip[chip_index];
649
Florian Fainellifa981d92014-08-27 17:04:49 -0700650 cd->of_node = child;
Florian Fainellic1f570a2014-09-15 14:48:08 -0700651 cd->host_dev = &mdio_bus->dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000652
653 sw_addr = of_get_property(child, "reg", NULL);
654 if (!sw_addr)
655 continue;
656
657 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700658 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000659 continue;
660
Guenter Roeck50d49642015-04-29 10:56:15 -0700661 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700662 cd->eeprom_len = eeprom_len;
663
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200664 mdio = of_parse_phandle(child, "mii-bus", 0);
665 if (mdio) {
666 mdio_bus_switch = of_mdio_find_bus(mdio);
667 if (!mdio_bus_switch) {
668 ret = -EPROBE_DEFER;
669 goto out_free_chip;
670 }
671 cd->host_dev = &mdio_bus_switch->dev;
672 }
673
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000674 for_each_available_child_of_node(child, port) {
675 port_reg = of_get_property(port, "reg", NULL);
676 if (!port_reg)
677 continue;
678
679 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700680 if (port_index >= DSA_MAX_PORTS)
681 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000682
683 port_name = of_get_property(port, "label", NULL);
684 if (!port_name)
685 continue;
686
Florian Fainellibd474972014-08-27 17:04:50 -0700687 cd->port_dn[port_index] = port;
688
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000689 cd->port_names[port_index] = kstrdup(port_name,
690 GFP_KERNEL);
691 if (!cd->port_names[port_index]) {
692 ret = -ENOMEM;
693 goto out_free_chip;
694 }
695
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200696 ret = dsa_of_probe_links(pd, cd, chip_index,
697 port_index, port, port_name);
698 if (ret)
699 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000700
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000701 }
702 }
703
704 return 0;
705
706out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000707 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000708out_free:
709 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800710 dev->platform_data = NULL;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000711 return ret;
712}
713
Florian Fainellif1a26a02015-03-05 12:35:04 -0800714static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000715{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800716 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000717
Florian Fainellif1a26a02015-03-05 12:35:04 -0800718 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000719 return;
720
Florian Fainelli21168242013-03-25 05:03:39 +0000721 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000722 kfree(pd);
723}
724#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800725static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000726{
727 return 0;
728}
729
Florian Fainellif1a26a02015-03-05 12:35:04 -0800730static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000731{
732}
733#endif
734
Florian Fainellic86e59b2015-03-05 12:35:08 -0800735static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
736 struct device *parent, struct dsa_platform_data *pd)
737{
738 int i;
739
740 dst->pd = pd;
741 dst->master_netdev = dev;
742 dst->cpu_switch = -1;
743 dst->cpu_port = -1;
744
745 for (i = 0; i < pd->nr_chips; i++) {
746 struct dsa_switch *ds;
747
748 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
749 if (IS_ERR(ds)) {
750 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
751 i, PTR_ERR(ds));
752 continue;
753 }
754
755 dst->ds[i] = ds;
756 if (ds->drv->poll_link != NULL)
757 dst->link_poll_needed = 1;
758 }
759
760 /*
761 * If we use a tagging format that doesn't have an ethertype
762 * field, make sure that all packets from this point on get
763 * sent to the tag format's receive function.
764 */
765 wmb();
766 dev->dsa_ptr = (void *)dst;
767
768 if (dst->link_poll_needed) {
769 INIT_WORK(&dst->link_poll_work, dsa_link_poll_work);
770 init_timer(&dst->link_poll_timer);
771 dst->link_poll_timer.data = (unsigned long)dst;
772 dst->link_poll_timer.function = dsa_link_poll_timer;
773 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
774 add_timer(&dst->link_poll_timer);
775 }
776}
777
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000778static int dsa_probe(struct platform_device *pdev)
779{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000780 struct dsa_platform_data *pd = pdev->dev.platform_data;
781 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000782 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800783 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000784
Joe Perchesa2ae6002014-11-09 16:32:46 -0800785 pr_notice_once("Distributed Switch Architecture driver version %s\n",
786 dsa_driver_version);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000787
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000788 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800789 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000790 if (ret)
791 return ret;
792
793 pd = pdev->dev.platform_data;
794 }
795
Florian Fainelli769a0202015-03-09 14:31:21 -0700796 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000797 return -EINVAL;
798
Florian Fainelli769a0202015-03-09 14:31:21 -0700799 if (pd->of_netdev) {
800 dev = pd->of_netdev;
801 dev_hold(dev);
802 } else {
803 dev = dev_to_net_device(pd->netdev);
804 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000805 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800806 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000807 goto out;
808 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000809
810 if (dev->dsa_ptr != NULL) {
811 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000812 ret = -EEXIST;
813 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000814 }
815
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000816 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
817 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000818 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000819 ret = -ENOMEM;
820 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000821 }
822
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000823 platform_set_drvdata(pdev, dst);
824
Florian Fainellic86e59b2015-03-05 12:35:08 -0800825 dsa_setup_dst(dst, dev, &pdev->dev, pd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000826
827 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000828
829out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800830 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000831
832 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000833}
834
Florian Fainellic86e59b2015-03-05 12:35:08 -0800835static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000836{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000837 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000838
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000839 if (dst->link_poll_needed)
840 del_timer_sync(&dst->link_poll_timer);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000841
Tejun Heo43829732012-08-20 14:51:24 -0700842 flush_work(&dst->link_poll_work);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000843
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000844 for (i = 0; i < dst->pd->nr_chips; i++) {
845 struct dsa_switch *ds = dst->ds[i];
846
847 if (ds != NULL)
848 dsa_switch_destroy(ds);
849 }
Florian Fainellic86e59b2015-03-05 12:35:08 -0800850}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000851
Florian Fainellic86e59b2015-03-05 12:35:08 -0800852static int dsa_remove(struct platform_device *pdev)
853{
854 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
855
856 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800857 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000858
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000859 return 0;
860}
861
862static void dsa_shutdown(struct platform_device *pdev)
863{
864}
865
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700866static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
867 struct packet_type *pt, struct net_device *orig_dev)
868{
869 struct dsa_switch_tree *dst = dev->dsa_ptr;
870
871 if (unlikely(dst == NULL)) {
872 kfree_skb(skb);
873 return 0;
874 }
875
Alexander Duyck50753142014-09-15 13:00:19 -0400876 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700877}
878
Florian Fainelli61b73632014-08-29 12:42:07 -0700879static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700880 .type = cpu_to_be16(ETH_P_XDSA),
881 .func = dsa_switch_rcv,
882};
883
Florian Fainellib73adef2015-02-24 13:15:33 -0800884static struct notifier_block dsa_netdevice_nb __read_mostly = {
885 .notifier_call = dsa_slave_netdevice_event,
886};
887
Florian Fainelli24462542014-09-18 17:31:22 -0700888#ifdef CONFIG_PM_SLEEP
889static int dsa_suspend(struct device *d)
890{
891 struct platform_device *pdev = to_platform_device(d);
892 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
893 int i, ret = 0;
894
895 for (i = 0; i < dst->pd->nr_chips; i++) {
896 struct dsa_switch *ds = dst->ds[i];
897
898 if (ds != NULL)
899 ret = dsa_switch_suspend(ds);
900 }
901
902 return ret;
903}
904
905static int dsa_resume(struct device *d)
906{
907 struct platform_device *pdev = to_platform_device(d);
908 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
909 int i, ret = 0;
910
911 for (i = 0; i < dst->pd->nr_chips; i++) {
912 struct dsa_switch *ds = dst->ds[i];
913
914 if (ds != NULL)
915 ret = dsa_switch_resume(ds);
916 }
917
918 return ret;
919}
920#endif
921
922static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
923
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000924static const struct of_device_id dsa_of_match_table[] = {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700925 { .compatible = "brcm,bcm7445-switch-v4.0" },
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000926 { .compatible = "marvell,dsa", },
927 {}
928};
929MODULE_DEVICE_TABLE(of, dsa_of_match_table);
930
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000931static struct platform_driver dsa_driver = {
932 .probe = dsa_probe,
933 .remove = dsa_remove,
934 .shutdown = dsa_shutdown,
935 .driver = {
936 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000937 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700938 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000939 },
940};
941
942static int __init dsa_init_module(void)
943{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000944 int rc;
945
Florian Fainellib73adef2015-02-24 13:15:33 -0800946 register_netdevice_notifier(&dsa_netdevice_nb);
947
Ben Hutchings7df899c2011-11-25 14:35:02 +0000948 rc = platform_driver_register(&dsa_driver);
949 if (rc)
950 return rc;
951
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700952 dev_add_pack(&dsa_pack_type);
953
Ben Hutchings7df899c2011-11-25 14:35:02 +0000954 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000955}
956module_init(dsa_init_module);
957
958static void __exit dsa_cleanup_module(void)
959{
Florian Fainellib73adef2015-02-24 13:15:33 -0800960 unregister_netdevice_notifier(&dsa_netdevice_nb);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700961 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000962 platform_driver_unregister(&dsa_driver);
963}
964module_exit(dsa_cleanup_module);
965
Rusty Russell577d6a72011-01-24 14:32:52 -0600966MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000967MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
968MODULE_LICENSE("GPL");
969MODULE_ALIAS("platform:dsa");