Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/dsa.c - Hardware switch handling |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 4 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 12 | #include <linux/ctype.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/hwmon.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 15 | #include <linux/list.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 19 | #include <net/dsa.h> |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 20 | #include <linux/of.h> |
| 21 | #include <linux/of_mdio.h> |
| 22 | #include <linux/of_platform.h> |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 23 | #include <linux/sysfs.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 24 | #include "dsa_priv.h" |
| 25 | |
| 26 | char dsa_driver_version[] = "0.1"; |
| 27 | |
| 28 | |
| 29 | /* switch driver registration ***********************************************/ |
| 30 | static DEFINE_MUTEX(dsa_switch_drivers_mutex); |
| 31 | static LIST_HEAD(dsa_switch_drivers); |
| 32 | |
| 33 | void register_switch_driver(struct dsa_switch_driver *drv) |
| 34 | { |
| 35 | mutex_lock(&dsa_switch_drivers_mutex); |
| 36 | list_add_tail(&drv->list, &dsa_switch_drivers); |
| 37 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 38 | } |
Ben Hutchings | ad293b8 | 2011-11-25 14:34:07 +0000 | [diff] [blame] | 39 | EXPORT_SYMBOL_GPL(register_switch_driver); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 40 | |
| 41 | void unregister_switch_driver(struct dsa_switch_driver *drv) |
| 42 | { |
| 43 | mutex_lock(&dsa_switch_drivers_mutex); |
| 44 | list_del_init(&drv->list); |
| 45 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 46 | } |
Ben Hutchings | ad293b8 | 2011-11-25 14:34:07 +0000 | [diff] [blame] | 47 | EXPORT_SYMBOL_GPL(unregister_switch_driver); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 48 | |
| 49 | static struct dsa_switch_driver * |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 50 | dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 51 | { |
| 52 | struct dsa_switch_driver *ret; |
| 53 | struct list_head *list; |
| 54 | char *name; |
| 55 | |
| 56 | ret = NULL; |
| 57 | name = NULL; |
| 58 | |
| 59 | mutex_lock(&dsa_switch_drivers_mutex); |
| 60 | list_for_each(list, &dsa_switch_drivers) { |
| 61 | struct dsa_switch_driver *drv; |
| 62 | |
| 63 | drv = list_entry(list, struct dsa_switch_driver, list); |
| 64 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 65 | name = drv->probe(host_dev, sw_addr); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 66 | if (name != NULL) { |
| 67 | ret = drv; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 72 | |
| 73 | *_name = name; |
| 74 | |
| 75 | return ret; |
| 76 | } |
| 77 | |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 78 | /* hwmon support ************************************************************/ |
| 79 | |
| 80 | #ifdef CONFIG_NET_DSA_HWMON |
| 81 | |
| 82 | static ssize_t temp1_input_show(struct device *dev, |
| 83 | struct device_attribute *attr, char *buf) |
| 84 | { |
| 85 | struct dsa_switch *ds = dev_get_drvdata(dev); |
| 86 | int temp, ret; |
| 87 | |
| 88 | ret = ds->drv->get_temp(ds, &temp); |
| 89 | if (ret < 0) |
| 90 | return ret; |
| 91 | |
| 92 | return sprintf(buf, "%d\n", temp * 1000); |
| 93 | } |
| 94 | static DEVICE_ATTR_RO(temp1_input); |
| 95 | |
| 96 | static ssize_t temp1_max_show(struct device *dev, |
| 97 | struct device_attribute *attr, char *buf) |
| 98 | { |
| 99 | struct dsa_switch *ds = dev_get_drvdata(dev); |
| 100 | int temp, ret; |
| 101 | |
| 102 | ret = ds->drv->get_temp_limit(ds, &temp); |
| 103 | if (ret < 0) |
| 104 | return ret; |
| 105 | |
| 106 | return sprintf(buf, "%d\n", temp * 1000); |
| 107 | } |
| 108 | |
| 109 | static ssize_t temp1_max_store(struct device *dev, |
| 110 | struct device_attribute *attr, const char *buf, |
| 111 | size_t count) |
| 112 | { |
| 113 | struct dsa_switch *ds = dev_get_drvdata(dev); |
| 114 | int temp, ret; |
| 115 | |
| 116 | ret = kstrtoint(buf, 0, &temp); |
| 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | |
| 120 | ret = ds->drv->set_temp_limit(ds, DIV_ROUND_CLOSEST(temp, 1000)); |
| 121 | if (ret < 0) |
| 122 | return ret; |
| 123 | |
| 124 | return count; |
| 125 | } |
| 126 | static DEVICE_ATTR(temp1_max, S_IRUGO, temp1_max_show, temp1_max_store); |
| 127 | |
| 128 | static ssize_t temp1_max_alarm_show(struct device *dev, |
| 129 | struct device_attribute *attr, char *buf) |
| 130 | { |
| 131 | struct dsa_switch *ds = dev_get_drvdata(dev); |
| 132 | bool alarm; |
| 133 | int ret; |
| 134 | |
| 135 | ret = ds->drv->get_temp_alarm(ds, &alarm); |
| 136 | if (ret < 0) |
| 137 | return ret; |
| 138 | |
| 139 | return sprintf(buf, "%d\n", alarm); |
| 140 | } |
| 141 | static DEVICE_ATTR_RO(temp1_max_alarm); |
| 142 | |
| 143 | static struct attribute *dsa_hwmon_attrs[] = { |
| 144 | &dev_attr_temp1_input.attr, /* 0 */ |
| 145 | &dev_attr_temp1_max.attr, /* 1 */ |
| 146 | &dev_attr_temp1_max_alarm.attr, /* 2 */ |
| 147 | NULL |
| 148 | }; |
| 149 | |
| 150 | static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj, |
| 151 | struct attribute *attr, int index) |
| 152 | { |
| 153 | struct device *dev = container_of(kobj, struct device, kobj); |
| 154 | struct dsa_switch *ds = dev_get_drvdata(dev); |
| 155 | struct dsa_switch_driver *drv = ds->drv; |
| 156 | umode_t mode = attr->mode; |
| 157 | |
| 158 | if (index == 1) { |
| 159 | if (!drv->get_temp_limit) |
| 160 | mode = 0; |
| 161 | else if (drv->set_temp_limit) |
| 162 | mode |= S_IWUSR; |
| 163 | } else if (index == 2 && !drv->get_temp_alarm) { |
| 164 | mode = 0; |
| 165 | } |
| 166 | return mode; |
| 167 | } |
| 168 | |
| 169 | static const struct attribute_group dsa_hwmon_group = { |
| 170 | .attrs = dsa_hwmon_attrs, |
| 171 | .is_visible = dsa_hwmon_attrs_visible, |
| 172 | }; |
| 173 | __ATTRIBUTE_GROUPS(dsa_hwmon); |
| 174 | |
| 175 | #endif /* CONFIG_NET_DSA_HWMON */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 176 | |
| 177 | /* basic switch operations **************************************************/ |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame^] | 178 | static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 179 | { |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame^] | 180 | struct dsa_switch_driver *drv = ds->drv; |
| 181 | struct dsa_switch_tree *dst = ds->dst; |
| 182 | struct dsa_chip_data *pd = ds->pd; |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 183 | bool valid_name_found = false; |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame^] | 184 | int index = ds->index; |
| 185 | int i, ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * Validate supplied switch configuration. |
| 189 | */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 190 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 191 | char *name; |
| 192 | |
| 193 | name = pd->port_names[i]; |
| 194 | if (name == NULL) |
| 195 | continue; |
| 196 | |
| 197 | if (!strcmp(name, "cpu")) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 198 | if (dst->cpu_switch != -1) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 199 | netdev_err(dst->master_netdev, |
| 200 | "multiple cpu ports?!\n"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 201 | ret = -EINVAL; |
| 202 | goto out; |
| 203 | } |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 204 | dst->cpu_switch = index; |
| 205 | dst->cpu_port = i; |
| 206 | } else if (!strcmp(name, "dsa")) { |
| 207 | ds->dsa_port_mask |= 1 << i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 208 | } else { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 209 | ds->phys_port_mask |= 1 << i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 210 | } |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 211 | valid_name_found = true; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 214 | if (!valid_name_found && i == DSA_MAX_PORTS) { |
| 215 | ret = -EINVAL; |
| 216 | goto out; |
| 217 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 218 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 219 | /* Make the built-in MII bus mask match the number of ports, |
| 220 | * switch drivers can override this later |
| 221 | */ |
| 222 | ds->phys_mii_mask = ds->phys_port_mask; |
| 223 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 224 | /* |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 225 | * If the CPU connects to this switch, set the switch tree |
| 226 | * tagging protocol to the preferred tagging format of this |
| 227 | * switch. |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 228 | */ |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 229 | if (dst->cpu_switch == index) { |
| 230 | switch (drv->tag_protocol) { |
| 231 | #ifdef CONFIG_NET_DSA_TAG_DSA |
| 232 | case DSA_TAG_PROTO_DSA: |
| 233 | dst->rcv = dsa_netdev_ops.rcv; |
| 234 | break; |
| 235 | #endif |
| 236 | #ifdef CONFIG_NET_DSA_TAG_EDSA |
| 237 | case DSA_TAG_PROTO_EDSA: |
| 238 | dst->rcv = edsa_netdev_ops.rcv; |
| 239 | break; |
| 240 | #endif |
| 241 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
| 242 | case DSA_TAG_PROTO_TRAILER: |
| 243 | dst->rcv = trailer_netdev_ops.rcv; |
| 244 | break; |
| 245 | #endif |
| 246 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 247 | case DSA_TAG_PROTO_BRCM: |
| 248 | dst->rcv = brcm_netdev_ops.rcv; |
| 249 | break; |
| 250 | #endif |
Andrew Lunn | ae43928 | 2014-10-24 23:44:04 +0200 | [diff] [blame] | 251 | case DSA_TAG_PROTO_NONE: |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 252 | break; |
Andrew Lunn | ae43928 | 2014-10-24 23:44:04 +0200 | [diff] [blame] | 253 | default: |
| 254 | ret = -ENOPROTOOPT; |
| 255 | goto out; |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 256 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 257 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 258 | dst->tag_protocol = drv->tag_protocol; |
| 259 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 260 | |
| 261 | /* |
| 262 | * Do basic register setup. |
| 263 | */ |
| 264 | ret = drv->setup(ds); |
| 265 | if (ret < 0) |
| 266 | goto out; |
| 267 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 268 | ret = drv->set_addr(ds, dst->master_netdev->dev_addr); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 269 | if (ret < 0) |
| 270 | goto out; |
| 271 | |
| 272 | ds->slave_mii_bus = mdiobus_alloc(); |
| 273 | if (ds->slave_mii_bus == NULL) { |
| 274 | ret = -ENOMEM; |
| 275 | goto out; |
| 276 | } |
| 277 | dsa_slave_mii_bus_init(ds); |
| 278 | |
| 279 | ret = mdiobus_register(ds->slave_mii_bus); |
| 280 | if (ret < 0) |
| 281 | goto out_free; |
| 282 | |
| 283 | |
| 284 | /* |
| 285 | * Create network devices for physical switch ports. |
| 286 | */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 287 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 288 | if (!(ds->phys_port_mask & (1 << i))) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 289 | continue; |
| 290 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 291 | ret = dsa_slave_create(ds, parent, i, pd->port_names[i]); |
| 292 | if (ret < 0) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 293 | netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n", |
| 294 | index, i, pd->port_names[i]); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 295 | ret = 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 296 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 299 | #ifdef CONFIG_NET_DSA_HWMON |
| 300 | /* If the switch provides a temperature sensor, |
| 301 | * register with hardware monitoring subsystem. |
| 302 | * Treat registration error as non-fatal and ignore it. |
| 303 | */ |
| 304 | if (drv->get_temp) { |
| 305 | const char *netname = netdev_name(dst->master_netdev); |
| 306 | char hname[IFNAMSIZ + 1]; |
| 307 | int i, j; |
| 308 | |
| 309 | /* Create valid hwmon 'name' attribute */ |
| 310 | for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) { |
| 311 | if (isalnum(netname[i])) |
| 312 | hname[j++] = netname[i]; |
| 313 | } |
| 314 | hname[j] = '\0'; |
| 315 | scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d", |
| 316 | hname, index); |
| 317 | ds->hwmon_dev = hwmon_device_register_with_groups(NULL, |
| 318 | ds->hwmon_name, ds, dsa_hwmon_groups); |
| 319 | if (IS_ERR(ds->hwmon_dev)) |
| 320 | ds->hwmon_dev = NULL; |
| 321 | } |
| 322 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 323 | |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame^] | 324 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 325 | |
| 326 | out_free: |
| 327 | mdiobus_free(ds->slave_mii_bus); |
| 328 | out: |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 329 | kfree(ds); |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame^] | 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | static struct dsa_switch * |
| 334 | dsa_switch_setup(struct dsa_switch_tree *dst, int index, |
| 335 | struct device *parent, struct device *host_dev) |
| 336 | { |
| 337 | struct dsa_chip_data *pd = dst->pd->chip + index; |
| 338 | struct dsa_switch_driver *drv; |
| 339 | struct dsa_switch *ds; |
| 340 | int ret; |
| 341 | char *name; |
| 342 | |
| 343 | /* |
| 344 | * Probe for switch model. |
| 345 | */ |
| 346 | drv = dsa_switch_probe(host_dev, pd->sw_addr, &name); |
| 347 | if (drv == NULL) { |
| 348 | netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n", |
| 349 | index); |
| 350 | return ERR_PTR(-EINVAL); |
| 351 | } |
| 352 | netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n", |
| 353 | index, name); |
| 354 | |
| 355 | |
| 356 | /* |
| 357 | * Allocate and initialise switch state. |
| 358 | */ |
| 359 | ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL); |
| 360 | if (ds == NULL) |
| 361 | return NULL; |
| 362 | |
| 363 | ds->dst = dst; |
| 364 | ds->index = index; |
| 365 | ds->pd = pd; |
| 366 | ds->drv = drv; |
| 367 | ds->master_dev = host_dev; |
| 368 | |
| 369 | ret = dsa_switch_setup_one(ds, parent); |
| 370 | if (ret) |
| 371 | return NULL; |
| 372 | |
| 373 | return ds; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static void dsa_switch_destroy(struct dsa_switch *ds) |
| 377 | { |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 378 | #ifdef CONFIG_NET_DSA_HWMON |
| 379 | if (ds->hwmon_dev) |
| 380 | hwmon_device_unregister(ds->hwmon_dev); |
| 381 | #endif |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Thierry Reding | e506d40 | 2014-10-01 13:59:00 +0200 | [diff] [blame] | 384 | #ifdef CONFIG_PM_SLEEP |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 385 | static int dsa_switch_suspend(struct dsa_switch *ds) |
| 386 | { |
| 387 | int i, ret = 0; |
| 388 | |
| 389 | /* Suspend slave network devices */ |
| 390 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Guenter Roeck | d79d210 | 2015-02-24 23:02:02 -0800 | [diff] [blame] | 391 | if (!dsa_is_port_initialized(ds, i)) |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 392 | continue; |
| 393 | |
| 394 | ret = dsa_slave_suspend(ds->ports[i]); |
| 395 | if (ret) |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | if (ds->drv->suspend) |
| 400 | ret = ds->drv->suspend(ds); |
| 401 | |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | static int dsa_switch_resume(struct dsa_switch *ds) |
| 406 | { |
| 407 | int i, ret = 0; |
| 408 | |
| 409 | if (ds->drv->resume) |
| 410 | ret = ds->drv->resume(ds); |
| 411 | |
| 412 | if (ret) |
| 413 | return ret; |
| 414 | |
| 415 | /* Resume slave network devices */ |
| 416 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Guenter Roeck | d79d210 | 2015-02-24 23:02:02 -0800 | [diff] [blame] | 417 | if (!dsa_is_port_initialized(ds, i)) |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 418 | continue; |
| 419 | |
| 420 | ret = dsa_slave_resume(ds->ports[i]); |
| 421 | if (ret) |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
Thierry Reding | e506d40 | 2014-10-01 13:59:00 +0200 | [diff] [blame] | 427 | #endif |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 428 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 429 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 430 | /* link polling *************************************************************/ |
| 431 | static void dsa_link_poll_work(struct work_struct *ugly) |
| 432 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 433 | struct dsa_switch_tree *dst; |
| 434 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 435 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 436 | dst = container_of(ugly, struct dsa_switch_tree, link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 437 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 438 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 439 | struct dsa_switch *ds = dst->ds[i]; |
| 440 | |
| 441 | if (ds != NULL && ds->drv->poll_link != NULL) |
| 442 | ds->drv->poll_link(ds); |
| 443 | } |
| 444 | |
| 445 | mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ)); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 448 | static void dsa_link_poll_timer(unsigned long _dst) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 449 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 450 | struct dsa_switch_tree *dst = (void *)_dst; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 451 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 452 | schedule_work(&dst->link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
| 456 | /* platform driver init and cleanup *****************************************/ |
| 457 | static int dev_is_class(struct device *dev, void *class) |
| 458 | { |
| 459 | if (dev->class != NULL && !strcmp(dev->class->name, class)) |
| 460 | return 1; |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static struct device *dev_find_class(struct device *parent, char *class) |
| 466 | { |
| 467 | if (dev_is_class(parent, class)) { |
| 468 | get_device(parent); |
| 469 | return parent; |
| 470 | } |
| 471 | |
| 472 | return device_find_child(parent, class, dev_is_class); |
| 473 | } |
| 474 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 475 | struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 476 | { |
| 477 | struct device *d; |
| 478 | |
| 479 | d = dev_find_class(dev, "mdio_bus"); |
| 480 | if (d != NULL) { |
| 481 | struct mii_bus *bus; |
| 482 | |
| 483 | bus = to_mii_bus(d); |
| 484 | put_device(d); |
| 485 | |
| 486 | return bus; |
| 487 | } |
| 488 | |
| 489 | return NULL; |
| 490 | } |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 491 | EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 492 | |
| 493 | static struct net_device *dev_to_net_device(struct device *dev) |
| 494 | { |
| 495 | struct device *d; |
| 496 | |
| 497 | d = dev_find_class(dev, "net"); |
| 498 | if (d != NULL) { |
| 499 | struct net_device *nd; |
| 500 | |
| 501 | nd = to_net_dev(d); |
| 502 | dev_hold(nd); |
| 503 | put_device(d); |
| 504 | |
| 505 | return nd; |
| 506 | } |
| 507 | |
| 508 | return NULL; |
| 509 | } |
| 510 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 511 | #ifdef CONFIG_OF |
| 512 | static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, |
| 513 | struct dsa_chip_data *cd, |
| 514 | int chip_index, |
| 515 | struct device_node *link) |
| 516 | { |
| 517 | int ret; |
| 518 | const __be32 *reg; |
| 519 | int link_port_addr; |
| 520 | 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 | |
| 532 | link_sw_addr = be32_to_cpup(reg + 1); |
| 533 | |
| 534 | if (link_sw_addr >= pd->nr_chips) |
| 535 | return -EINVAL; |
| 536 | |
| 537 | /* First time routing table allocation */ |
| 538 | if (!cd->rtable) { |
Fabian Frederick | 5bc4b46 | 2014-11-14 19:36:42 +0100 | [diff] [blame] | 539 | cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8), |
| 540 | GFP_KERNEL); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 541 | if (!cd->rtable) |
| 542 | return -ENOMEM; |
| 543 | |
| 544 | /* default to no valid uplink/downlink */ |
| 545 | memset(cd->rtable, -1, pd->nr_chips * sizeof(s8)); |
| 546 | } |
| 547 | |
| 548 | reg = of_get_property(link, "reg", NULL); |
| 549 | if (!reg) { |
| 550 | ret = -EINVAL; |
| 551 | goto out; |
| 552 | } |
| 553 | |
| 554 | link_port_addr = be32_to_cpup(reg); |
| 555 | |
| 556 | cd->rtable[link_sw_addr] = link_port_addr; |
| 557 | |
| 558 | return 0; |
| 559 | out: |
| 560 | kfree(cd->rtable); |
| 561 | return ret; |
| 562 | } |
| 563 | |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 564 | static void dsa_of_free_platform_data(struct dsa_platform_data *pd) |
| 565 | { |
| 566 | int i; |
| 567 | int port_index; |
| 568 | |
| 569 | for (i = 0; i < pd->nr_chips; i++) { |
| 570 | port_index = 0; |
Florian Fainelli | 5f64a7d | 2013-03-25 05:03:40 +0000 | [diff] [blame] | 571 | while (port_index < DSA_MAX_PORTS) { |
Fabian Frederick | 1f74714 | 2014-06-23 19:32:47 +0200 | [diff] [blame] | 572 | kfree(pd->chip[i].port_names[port_index]); |
Florian Fainelli | 5f64a7d | 2013-03-25 05:03:40 +0000 | [diff] [blame] | 573 | port_index++; |
| 574 | } |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 575 | kfree(pd->chip[i].rtable); |
| 576 | } |
| 577 | kfree(pd->chip); |
| 578 | } |
| 579 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 580 | static int dsa_of_probe(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 581 | { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 582 | struct device_node *np = dev->of_node; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 583 | struct device_node *child, *mdio, *ethernet, *port, *link; |
| 584 | struct mii_bus *mdio_bus; |
| 585 | struct platform_device *ethernet_dev; |
| 586 | struct dsa_platform_data *pd; |
| 587 | struct dsa_chip_data *cd; |
| 588 | const char *port_name; |
| 589 | int chip_index, port_index; |
| 590 | const unsigned int *sw_addr, *port_reg; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 591 | u32 eeprom_len; |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 592 | int ret; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 593 | |
| 594 | mdio = of_parse_phandle(np, "dsa,mii-bus", 0); |
| 595 | if (!mdio) |
| 596 | return -EINVAL; |
| 597 | |
| 598 | mdio_bus = of_mdio_find_bus(mdio); |
| 599 | if (!mdio_bus) |
Florian Fainelli | b324c07 | 2015-03-05 12:35:05 -0800 | [diff] [blame] | 600 | return -EPROBE_DEFER; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 601 | |
| 602 | ethernet = of_parse_phandle(np, "dsa,ethernet", 0); |
| 603 | if (!ethernet) |
| 604 | return -EINVAL; |
| 605 | |
| 606 | ethernet_dev = of_find_device_by_node(ethernet); |
| 607 | if (!ethernet_dev) |
Florian Fainelli | b324c07 | 2015-03-05 12:35:05 -0800 | [diff] [blame] | 608 | return -EPROBE_DEFER; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 609 | |
| 610 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 611 | if (!pd) |
| 612 | return -ENOMEM; |
| 613 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 614 | dev->platform_data = pd; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 615 | pd->netdev = ðernet_dev->dev; |
Tobias Waldekranz | e04449f | 2015-02-05 14:54:09 +0100 | [diff] [blame] | 616 | pd->nr_chips = of_get_available_child_count(np); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 617 | if (pd->nr_chips > DSA_MAX_SWITCHES) |
| 618 | pd->nr_chips = DSA_MAX_SWITCHES; |
| 619 | |
Fabian Frederick | 6f2aed6 | 2014-11-14 19:38:23 +0100 | [diff] [blame] | 620 | pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data), |
| 621 | GFP_KERNEL); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 622 | if (!pd->chip) { |
| 623 | ret = -ENOMEM; |
| 624 | goto out_free; |
| 625 | } |
| 626 | |
Fabian Godehardt | d1c0b47 | 2014-05-16 06:21:44 +0200 | [diff] [blame] | 627 | chip_index = -1; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 628 | for_each_available_child_of_node(np, child) { |
Fabian Godehardt | d1c0b47 | 2014-05-16 06:21:44 +0200 | [diff] [blame] | 629 | chip_index++; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 630 | cd = &pd->chip[chip_index]; |
| 631 | |
Florian Fainelli | fa981d9 | 2014-08-27 17:04:49 -0700 | [diff] [blame] | 632 | cd->of_node = child; |
Florian Fainelli | c1f570a | 2014-09-15 14:48:08 -0700 | [diff] [blame] | 633 | cd->host_dev = &mdio_bus->dev; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 634 | |
| 635 | sw_addr = of_get_property(child, "reg", NULL); |
| 636 | if (!sw_addr) |
| 637 | continue; |
| 638 | |
| 639 | cd->sw_addr = be32_to_cpup(sw_addr); |
| 640 | if (cd->sw_addr > PHY_MAX_ADDR) |
| 641 | continue; |
| 642 | |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 643 | if (!of_property_read_u32(np, "eeprom-length", &eeprom_len)) |
| 644 | cd->eeprom_len = eeprom_len; |
| 645 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 646 | for_each_available_child_of_node(child, port) { |
| 647 | port_reg = of_get_property(port, "reg", NULL); |
| 648 | if (!port_reg) |
| 649 | continue; |
| 650 | |
| 651 | port_index = be32_to_cpup(port_reg); |
| 652 | |
| 653 | port_name = of_get_property(port, "label", NULL); |
| 654 | if (!port_name) |
| 655 | continue; |
| 656 | |
Florian Fainelli | bd47497 | 2014-08-27 17:04:50 -0700 | [diff] [blame] | 657 | cd->port_dn[port_index] = port; |
| 658 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 659 | cd->port_names[port_index] = kstrdup(port_name, |
| 660 | GFP_KERNEL); |
| 661 | if (!cd->port_names[port_index]) { |
| 662 | ret = -ENOMEM; |
| 663 | goto out_free_chip; |
| 664 | } |
| 665 | |
| 666 | link = of_parse_phandle(port, "link", 0); |
| 667 | |
| 668 | if (!strcmp(port_name, "dsa") && link && |
| 669 | pd->nr_chips > 1) { |
| 670 | ret = dsa_of_setup_routing_table(pd, cd, |
| 671 | chip_index, link); |
| 672 | if (ret) |
| 673 | goto out_free_chip; |
| 674 | } |
| 675 | |
| 676 | if (port_index == DSA_MAX_PORTS) |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | |
| 683 | out_free_chip: |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 684 | dsa_of_free_platform_data(pd); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 685 | out_free: |
| 686 | kfree(pd); |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 687 | dev->platform_data = NULL; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 688 | return ret; |
| 689 | } |
| 690 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 691 | static void dsa_of_remove(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 692 | { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 693 | struct dsa_platform_data *pd = dev->platform_data; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 694 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 695 | if (!dev->of_node) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 696 | return; |
| 697 | |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 698 | dsa_of_free_platform_data(pd); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 699 | kfree(pd); |
| 700 | } |
| 701 | #else |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 702 | static inline int dsa_of_probe(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 703 | { |
| 704 | return 0; |
| 705 | } |
| 706 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 707 | static inline void dsa_of_remove(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 708 | { |
| 709 | } |
| 710 | #endif |
| 711 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 712 | static int dsa_probe(struct platform_device *pdev) |
| 713 | { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 714 | struct dsa_platform_data *pd = pdev->dev.platform_data; |
| 715 | struct net_device *dev; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 716 | struct dsa_switch_tree *dst; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 717 | int i, ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 718 | |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 719 | pr_notice_once("Distributed Switch Architecture driver version %s\n", |
| 720 | dsa_driver_version); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 721 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 722 | if (pdev->dev.of_node) { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 723 | ret = dsa_of_probe(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 724 | if (ret) |
| 725 | return ret; |
| 726 | |
| 727 | pd = pdev->dev.platform_data; |
| 728 | } |
| 729 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 730 | if (pd == NULL || pd->netdev == NULL) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 731 | return -EINVAL; |
| 732 | |
| 733 | dev = dev_to_net_device(pd->netdev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 734 | if (dev == NULL) { |
Florian Fainelli | b324c07 | 2015-03-05 12:35:05 -0800 | [diff] [blame] | 735 | ret = -EPROBE_DEFER; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 736 | goto out; |
| 737 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 738 | |
| 739 | if (dev->dsa_ptr != NULL) { |
| 740 | dev_put(dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 741 | ret = -EEXIST; |
| 742 | goto out; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 745 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 746 | if (dst == NULL) { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 747 | dev_put(dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 748 | ret = -ENOMEM; |
| 749 | goto out; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 752 | platform_set_drvdata(pdev, dst); |
| 753 | |
| 754 | dst->pd = pd; |
| 755 | dst->master_netdev = dev; |
| 756 | dst->cpu_switch = -1; |
| 757 | dst->cpu_port = -1; |
| 758 | |
| 759 | for (i = 0; i < pd->nr_chips; i++) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 760 | struct dsa_switch *ds; |
| 761 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 762 | ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 763 | if (IS_ERR(ds)) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 764 | netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", |
| 765 | i, PTR_ERR(ds)); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 766 | continue; |
| 767 | } |
| 768 | |
| 769 | dst->ds[i] = ds; |
| 770 | if (ds->drv->poll_link != NULL) |
| 771 | dst->link_poll_needed = 1; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 774 | /* |
| 775 | * If we use a tagging format that doesn't have an ethertype |
| 776 | * field, make sure that all packets from this point on get |
| 777 | * sent to the tag format's receive function. |
| 778 | */ |
| 779 | wmb(); |
| 780 | dev->dsa_ptr = (void *)dst; |
| 781 | |
| 782 | if (dst->link_poll_needed) { |
| 783 | INIT_WORK(&dst->link_poll_work, dsa_link_poll_work); |
| 784 | init_timer(&dst->link_poll_timer); |
| 785 | dst->link_poll_timer.data = (unsigned long)dst; |
| 786 | dst->link_poll_timer.function = dsa_link_poll_timer; |
| 787 | dst->link_poll_timer.expires = round_jiffies(jiffies + HZ); |
| 788 | add_timer(&dst->link_poll_timer); |
| 789 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 790 | |
| 791 | return 0; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 792 | |
| 793 | out: |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 794 | dsa_of_remove(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 795 | |
| 796 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | static int dsa_remove(struct platform_device *pdev) |
| 800 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 801 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 802 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 803 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 804 | if (dst->link_poll_needed) |
| 805 | del_timer_sync(&dst->link_poll_timer); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 806 | |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 807 | flush_work(&dst->link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 808 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 809 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 810 | struct dsa_switch *ds = dst->ds[i]; |
| 811 | |
| 812 | if (ds != NULL) |
| 813 | dsa_switch_destroy(ds); |
| 814 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 815 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 816 | dsa_of_remove(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 817 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | static void dsa_shutdown(struct platform_device *pdev) |
| 822 | { |
| 823 | } |
| 824 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 825 | static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev, |
| 826 | struct packet_type *pt, struct net_device *orig_dev) |
| 827 | { |
| 828 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 829 | |
| 830 | if (unlikely(dst == NULL)) { |
| 831 | kfree_skb(skb); |
| 832 | return 0; |
| 833 | } |
| 834 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 835 | return dst->rcv(skb, dev, pt, orig_dev); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Florian Fainelli | 61b7363 | 2014-08-29 12:42:07 -0700 | [diff] [blame] | 838 | static struct packet_type dsa_pack_type __read_mostly = { |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 839 | .type = cpu_to_be16(ETH_P_XDSA), |
| 840 | .func = dsa_switch_rcv, |
| 841 | }; |
| 842 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 843 | static struct notifier_block dsa_netdevice_nb __read_mostly = { |
| 844 | .notifier_call = dsa_slave_netdevice_event, |
| 845 | }; |
| 846 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 847 | #ifdef CONFIG_PM_SLEEP |
| 848 | static int dsa_suspend(struct device *d) |
| 849 | { |
| 850 | struct platform_device *pdev = to_platform_device(d); |
| 851 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 852 | int i, ret = 0; |
| 853 | |
| 854 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 855 | struct dsa_switch *ds = dst->ds[i]; |
| 856 | |
| 857 | if (ds != NULL) |
| 858 | ret = dsa_switch_suspend(ds); |
| 859 | } |
| 860 | |
| 861 | return ret; |
| 862 | } |
| 863 | |
| 864 | static int dsa_resume(struct device *d) |
| 865 | { |
| 866 | struct platform_device *pdev = to_platform_device(d); |
| 867 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 868 | int i, ret = 0; |
| 869 | |
| 870 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 871 | struct dsa_switch *ds = dst->ds[i]; |
| 872 | |
| 873 | if (ds != NULL) |
| 874 | ret = dsa_switch_resume(ds); |
| 875 | } |
| 876 | |
| 877 | return ret; |
| 878 | } |
| 879 | #endif |
| 880 | |
| 881 | static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); |
| 882 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 883 | static const struct of_device_id dsa_of_match_table[] = { |
Florian Fainelli | 246d7f7 | 2014-08-27 17:04:56 -0700 | [diff] [blame] | 884 | { .compatible = "brcm,bcm7445-switch-v4.0" }, |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 885 | { .compatible = "marvell,dsa", }, |
| 886 | {} |
| 887 | }; |
| 888 | MODULE_DEVICE_TABLE(of, dsa_of_match_table); |
| 889 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 890 | static struct platform_driver dsa_driver = { |
| 891 | .probe = dsa_probe, |
| 892 | .remove = dsa_remove, |
| 893 | .shutdown = dsa_shutdown, |
| 894 | .driver = { |
| 895 | .name = "dsa", |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 896 | .of_match_table = dsa_of_match_table, |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 897 | .pm = &dsa_pm_ops, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 898 | }, |
| 899 | }; |
| 900 | |
| 901 | static int __init dsa_init_module(void) |
| 902 | { |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 903 | int rc; |
| 904 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 905 | register_netdevice_notifier(&dsa_netdevice_nb); |
| 906 | |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 907 | rc = platform_driver_register(&dsa_driver); |
| 908 | if (rc) |
| 909 | return rc; |
| 910 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 911 | dev_add_pack(&dsa_pack_type); |
| 912 | |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 913 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 914 | } |
| 915 | module_init(dsa_init_module); |
| 916 | |
| 917 | static void __exit dsa_cleanup_module(void) |
| 918 | { |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 919 | unregister_netdevice_notifier(&dsa_netdevice_nb); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 920 | dev_remove_pack(&dsa_pack_type); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 921 | platform_driver_unregister(&dsa_driver); |
| 922 | } |
| 923 | module_exit(dsa_cleanup_module); |
| 924 | |
Rusty Russell | 577d6a7 | 2011-01-24 14:32:52 -0600 | [diff] [blame] | 925 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 926 | MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); |
| 927 | MODULE_LICENSE("GPL"); |
| 928 | MODULE_ALIAS("platform:dsa"); |