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 **************************************************/ |
| 178 | static struct dsa_switch * |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 179 | dsa_switch_setup(struct dsa_switch_tree *dst, int index, |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 180 | struct device *parent, struct device *host_dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 181 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 182 | struct dsa_chip_data *pd = dst->pd->chip + index; |
| 183 | struct dsa_switch_driver *drv; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 184 | struct dsa_switch *ds; |
| 185 | int ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 186 | char *name; |
| 187 | int i; |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 188 | bool valid_name_found = false; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * Probe for switch model. |
| 192 | */ |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 193 | drv = dsa_switch_probe(host_dev, pd->sw_addr, &name); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 194 | if (drv == NULL) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 195 | netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n", |
| 196 | index); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 197 | return ERR_PTR(-EINVAL); |
| 198 | } |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 199 | netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n", |
| 200 | index, name); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | /* |
| 204 | * Allocate and initialise switch state. |
| 205 | */ |
| 206 | ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL); |
| 207 | if (ds == NULL) |
| 208 | return ERR_PTR(-ENOMEM); |
| 209 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 210 | ds->dst = dst; |
| 211 | ds->index = index; |
| 212 | ds->pd = dst->pd->chip + index; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 213 | ds->drv = drv; |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 214 | ds->master_dev = host_dev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Validate supplied switch configuration. |
| 218 | */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 219 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 220 | char *name; |
| 221 | |
| 222 | name = pd->port_names[i]; |
| 223 | if (name == NULL) |
| 224 | continue; |
| 225 | |
| 226 | if (!strcmp(name, "cpu")) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 227 | if (dst->cpu_switch != -1) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 228 | netdev_err(dst->master_netdev, |
| 229 | "multiple cpu ports?!\n"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 230 | ret = -EINVAL; |
| 231 | goto out; |
| 232 | } |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 233 | dst->cpu_switch = index; |
| 234 | dst->cpu_port = i; |
| 235 | } else if (!strcmp(name, "dsa")) { |
| 236 | ds->dsa_port_mask |= 1 << i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 237 | } else { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 238 | ds->phys_port_mask |= 1 << i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 239 | } |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 240 | valid_name_found = true; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 243 | if (!valid_name_found && i == DSA_MAX_PORTS) { |
| 244 | ret = -EINVAL; |
| 245 | goto out; |
| 246 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 247 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 248 | /* Make the built-in MII bus mask match the number of ports, |
| 249 | * switch drivers can override this later |
| 250 | */ |
| 251 | ds->phys_mii_mask = ds->phys_port_mask; |
| 252 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 253 | /* |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 254 | * If the CPU connects to this switch, set the switch tree |
| 255 | * tagging protocol to the preferred tagging format of this |
| 256 | * switch. |
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 | if (dst->cpu_switch == index) { |
| 259 | switch (drv->tag_protocol) { |
| 260 | #ifdef CONFIG_NET_DSA_TAG_DSA |
| 261 | case DSA_TAG_PROTO_DSA: |
| 262 | dst->rcv = dsa_netdev_ops.rcv; |
| 263 | break; |
| 264 | #endif |
| 265 | #ifdef CONFIG_NET_DSA_TAG_EDSA |
| 266 | case DSA_TAG_PROTO_EDSA: |
| 267 | dst->rcv = edsa_netdev_ops.rcv; |
| 268 | break; |
| 269 | #endif |
| 270 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
| 271 | case DSA_TAG_PROTO_TRAILER: |
| 272 | dst->rcv = trailer_netdev_ops.rcv; |
| 273 | break; |
| 274 | #endif |
| 275 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 276 | case DSA_TAG_PROTO_BRCM: |
| 277 | dst->rcv = brcm_netdev_ops.rcv; |
| 278 | break; |
| 279 | #endif |
Andrew Lunn | ae43928 | 2014-10-24 23:44:04 +0200 | [diff] [blame] | 280 | case DSA_TAG_PROTO_NONE: |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 281 | break; |
Andrew Lunn | ae43928 | 2014-10-24 23:44:04 +0200 | [diff] [blame] | 282 | default: |
| 283 | ret = -ENOPROTOOPT; |
| 284 | goto out; |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 285 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 286 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 287 | dst->tag_protocol = drv->tag_protocol; |
| 288 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Do basic register setup. |
| 292 | */ |
| 293 | ret = drv->setup(ds); |
| 294 | if (ret < 0) |
| 295 | goto out; |
| 296 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 297 | ret = drv->set_addr(ds, dst->master_netdev->dev_addr); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 298 | if (ret < 0) |
| 299 | goto out; |
| 300 | |
| 301 | ds->slave_mii_bus = mdiobus_alloc(); |
| 302 | if (ds->slave_mii_bus == NULL) { |
| 303 | ret = -ENOMEM; |
| 304 | goto out; |
| 305 | } |
| 306 | dsa_slave_mii_bus_init(ds); |
| 307 | |
| 308 | ret = mdiobus_register(ds->slave_mii_bus); |
| 309 | if (ret < 0) |
| 310 | goto out_free; |
| 311 | |
| 312 | |
| 313 | /* |
| 314 | * Create network devices for physical switch ports. |
| 315 | */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 316 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 317 | if (!(ds->phys_port_mask & (1 << i))) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 318 | continue; |
| 319 | |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 320 | ret = dsa_slave_create(ds, parent, i, pd->port_names[i]); |
| 321 | if (ret < 0) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 322 | netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n", |
| 323 | index, i, pd->port_names[i]); |
Guenter Roeck | d87d6f4 | 2015-02-24 13:15:32 -0800 | [diff] [blame] | 324 | ret = 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 325 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 328 | #ifdef CONFIG_NET_DSA_HWMON |
| 329 | /* If the switch provides a temperature sensor, |
| 330 | * register with hardware monitoring subsystem. |
| 331 | * Treat registration error as non-fatal and ignore it. |
| 332 | */ |
| 333 | if (drv->get_temp) { |
| 334 | const char *netname = netdev_name(dst->master_netdev); |
| 335 | char hname[IFNAMSIZ + 1]; |
| 336 | int i, j; |
| 337 | |
| 338 | /* Create valid hwmon 'name' attribute */ |
| 339 | for (i = j = 0; i < IFNAMSIZ && netname[i]; i++) { |
| 340 | if (isalnum(netname[i])) |
| 341 | hname[j++] = netname[i]; |
| 342 | } |
| 343 | hname[j] = '\0'; |
| 344 | scnprintf(ds->hwmon_name, sizeof(ds->hwmon_name), "%s_dsa%d", |
| 345 | hname, index); |
| 346 | ds->hwmon_dev = hwmon_device_register_with_groups(NULL, |
| 347 | ds->hwmon_name, ds, dsa_hwmon_groups); |
| 348 | if (IS_ERR(ds->hwmon_dev)) |
| 349 | ds->hwmon_dev = NULL; |
| 350 | } |
| 351 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 352 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 353 | return ds; |
| 354 | |
| 355 | out_free: |
| 356 | mdiobus_free(ds->slave_mii_bus); |
| 357 | out: |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 358 | kfree(ds); |
| 359 | return ERR_PTR(ret); |
| 360 | } |
| 361 | |
| 362 | static void dsa_switch_destroy(struct dsa_switch *ds) |
| 363 | { |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 364 | #ifdef CONFIG_NET_DSA_HWMON |
| 365 | if (ds->hwmon_dev) |
| 366 | hwmon_device_unregister(ds->hwmon_dev); |
| 367 | #endif |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Thierry Reding | e506d40 | 2014-10-01 13:59:00 +0200 | [diff] [blame] | 370 | #ifdef CONFIG_PM_SLEEP |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 371 | static int dsa_switch_suspend(struct dsa_switch *ds) |
| 372 | { |
| 373 | int i, ret = 0; |
| 374 | |
| 375 | /* Suspend slave network devices */ |
| 376 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Guenter Roeck | d79d210 | 2015-02-24 23:02:02 -0800 | [diff] [blame] | 377 | if (!dsa_is_port_initialized(ds, i)) |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 378 | continue; |
| 379 | |
| 380 | ret = dsa_slave_suspend(ds->ports[i]); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | if (ds->drv->suspend) |
| 386 | ret = ds->drv->suspend(ds); |
| 387 | |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | static int dsa_switch_resume(struct dsa_switch *ds) |
| 392 | { |
| 393 | int i, ret = 0; |
| 394 | |
| 395 | if (ds->drv->resume) |
| 396 | ret = ds->drv->resume(ds); |
| 397 | |
| 398 | if (ret) |
| 399 | return ret; |
| 400 | |
| 401 | /* Resume slave network devices */ |
| 402 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Guenter Roeck | d79d210 | 2015-02-24 23:02:02 -0800 | [diff] [blame] | 403 | if (!dsa_is_port_initialized(ds, i)) |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 404 | continue; |
| 405 | |
| 406 | ret = dsa_slave_resume(ds->ports[i]); |
| 407 | if (ret) |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | return 0; |
| 412 | } |
Thierry Reding | e506d40 | 2014-10-01 13:59:00 +0200 | [diff] [blame] | 413 | #endif |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 414 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 415 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 416 | /* link polling *************************************************************/ |
| 417 | static void dsa_link_poll_work(struct work_struct *ugly) |
| 418 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 419 | struct dsa_switch_tree *dst; |
| 420 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 421 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 422 | dst = container_of(ugly, struct dsa_switch_tree, link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 423 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 424 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 425 | struct dsa_switch *ds = dst->ds[i]; |
| 426 | |
| 427 | if (ds != NULL && ds->drv->poll_link != NULL) |
| 428 | ds->drv->poll_link(ds); |
| 429 | } |
| 430 | |
| 431 | mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ)); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 434 | static void dsa_link_poll_timer(unsigned long _dst) |
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 | struct dsa_switch_tree *dst = (void *)_dst; |
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 | schedule_work(&dst->link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
| 442 | /* platform driver init and cleanup *****************************************/ |
| 443 | static int dev_is_class(struct device *dev, void *class) |
| 444 | { |
| 445 | if (dev->class != NULL && !strcmp(dev->class->name, class)) |
| 446 | return 1; |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static struct device *dev_find_class(struct device *parent, char *class) |
| 452 | { |
| 453 | if (dev_is_class(parent, class)) { |
| 454 | get_device(parent); |
| 455 | return parent; |
| 456 | } |
| 457 | |
| 458 | return device_find_child(parent, class, dev_is_class); |
| 459 | } |
| 460 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 461 | struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 462 | { |
| 463 | struct device *d; |
| 464 | |
| 465 | d = dev_find_class(dev, "mdio_bus"); |
| 466 | if (d != NULL) { |
| 467 | struct mii_bus *bus; |
| 468 | |
| 469 | bus = to_mii_bus(d); |
| 470 | put_device(d); |
| 471 | |
| 472 | return bus; |
| 473 | } |
| 474 | |
| 475 | return NULL; |
| 476 | } |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 477 | EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 478 | |
| 479 | static struct net_device *dev_to_net_device(struct device *dev) |
| 480 | { |
| 481 | struct device *d; |
| 482 | |
| 483 | d = dev_find_class(dev, "net"); |
| 484 | if (d != NULL) { |
| 485 | struct net_device *nd; |
| 486 | |
| 487 | nd = to_net_dev(d); |
| 488 | dev_hold(nd); |
| 489 | put_device(d); |
| 490 | |
| 491 | return nd; |
| 492 | } |
| 493 | |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 497 | #ifdef CONFIG_OF |
| 498 | static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, |
| 499 | struct dsa_chip_data *cd, |
| 500 | int chip_index, |
| 501 | struct device_node *link) |
| 502 | { |
| 503 | int ret; |
| 504 | const __be32 *reg; |
| 505 | int link_port_addr; |
| 506 | int link_sw_addr; |
| 507 | struct device_node *parent_sw; |
| 508 | int len; |
| 509 | |
| 510 | parent_sw = of_get_parent(link); |
| 511 | if (!parent_sw) |
| 512 | return -EINVAL; |
| 513 | |
| 514 | reg = of_get_property(parent_sw, "reg", &len); |
| 515 | if (!reg || (len != sizeof(*reg) * 2)) |
| 516 | return -EINVAL; |
| 517 | |
| 518 | link_sw_addr = be32_to_cpup(reg + 1); |
| 519 | |
| 520 | if (link_sw_addr >= pd->nr_chips) |
| 521 | return -EINVAL; |
| 522 | |
| 523 | /* First time routing table allocation */ |
| 524 | if (!cd->rtable) { |
Fabian Frederick | 5bc4b46 | 2014-11-14 19:36:42 +0100 | [diff] [blame] | 525 | cd->rtable = kmalloc_array(pd->nr_chips, sizeof(s8), |
| 526 | GFP_KERNEL); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 527 | if (!cd->rtable) |
| 528 | return -ENOMEM; |
| 529 | |
| 530 | /* default to no valid uplink/downlink */ |
| 531 | memset(cd->rtable, -1, pd->nr_chips * sizeof(s8)); |
| 532 | } |
| 533 | |
| 534 | reg = of_get_property(link, "reg", NULL); |
| 535 | if (!reg) { |
| 536 | ret = -EINVAL; |
| 537 | goto out; |
| 538 | } |
| 539 | |
| 540 | link_port_addr = be32_to_cpup(reg); |
| 541 | |
| 542 | cd->rtable[link_sw_addr] = link_port_addr; |
| 543 | |
| 544 | return 0; |
| 545 | out: |
| 546 | kfree(cd->rtable); |
| 547 | return ret; |
| 548 | } |
| 549 | |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 550 | static void dsa_of_free_platform_data(struct dsa_platform_data *pd) |
| 551 | { |
| 552 | int i; |
| 553 | int port_index; |
| 554 | |
| 555 | for (i = 0; i < pd->nr_chips; i++) { |
| 556 | port_index = 0; |
Florian Fainelli | 5f64a7d | 2013-03-25 05:03:40 +0000 | [diff] [blame] | 557 | while (port_index < DSA_MAX_PORTS) { |
Fabian Frederick | 1f74714 | 2014-06-23 19:32:47 +0200 | [diff] [blame] | 558 | kfree(pd->chip[i].port_names[port_index]); |
Florian Fainelli | 5f64a7d | 2013-03-25 05:03:40 +0000 | [diff] [blame] | 559 | port_index++; |
| 560 | } |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 561 | kfree(pd->chip[i].rtable); |
| 562 | } |
| 563 | kfree(pd->chip); |
| 564 | } |
| 565 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 566 | static int dsa_of_probe(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 567 | { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 568 | struct device_node *np = dev->of_node; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 569 | struct device_node *child, *mdio, *ethernet, *port, *link; |
| 570 | struct mii_bus *mdio_bus; |
| 571 | struct platform_device *ethernet_dev; |
| 572 | struct dsa_platform_data *pd; |
| 573 | struct dsa_chip_data *cd; |
| 574 | const char *port_name; |
| 575 | int chip_index, port_index; |
| 576 | const unsigned int *sw_addr, *port_reg; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 577 | u32 eeprom_len; |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 578 | int ret; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 579 | |
| 580 | mdio = of_parse_phandle(np, "dsa,mii-bus", 0); |
| 581 | if (!mdio) |
| 582 | return -EINVAL; |
| 583 | |
| 584 | mdio_bus = of_mdio_find_bus(mdio); |
| 585 | if (!mdio_bus) |
Florian Fainelli | b324c07 | 2015-03-05 12:35:05 -0800 | [diff] [blame^] | 586 | return -EPROBE_DEFER; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 587 | |
| 588 | ethernet = of_parse_phandle(np, "dsa,ethernet", 0); |
| 589 | if (!ethernet) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | ethernet_dev = of_find_device_by_node(ethernet); |
| 593 | if (!ethernet_dev) |
Florian Fainelli | b324c07 | 2015-03-05 12:35:05 -0800 | [diff] [blame^] | 594 | return -EPROBE_DEFER; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 595 | |
| 596 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 597 | if (!pd) |
| 598 | return -ENOMEM; |
| 599 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 600 | dev->platform_data = pd; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 601 | pd->netdev = ðernet_dev->dev; |
Tobias Waldekranz | e04449f | 2015-02-05 14:54:09 +0100 | [diff] [blame] | 602 | pd->nr_chips = of_get_available_child_count(np); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 603 | if (pd->nr_chips > DSA_MAX_SWITCHES) |
| 604 | pd->nr_chips = DSA_MAX_SWITCHES; |
| 605 | |
Fabian Frederick | 6f2aed6 | 2014-11-14 19:38:23 +0100 | [diff] [blame] | 606 | pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data), |
| 607 | GFP_KERNEL); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 608 | if (!pd->chip) { |
| 609 | ret = -ENOMEM; |
| 610 | goto out_free; |
| 611 | } |
| 612 | |
Fabian Godehardt | d1c0b47 | 2014-05-16 06:21:44 +0200 | [diff] [blame] | 613 | chip_index = -1; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 614 | for_each_available_child_of_node(np, child) { |
Fabian Godehardt | d1c0b47 | 2014-05-16 06:21:44 +0200 | [diff] [blame] | 615 | chip_index++; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 616 | cd = &pd->chip[chip_index]; |
| 617 | |
Florian Fainelli | fa981d9 | 2014-08-27 17:04:49 -0700 | [diff] [blame] | 618 | cd->of_node = child; |
Florian Fainelli | c1f570a | 2014-09-15 14:48:08 -0700 | [diff] [blame] | 619 | cd->host_dev = &mdio_bus->dev; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 620 | |
| 621 | sw_addr = of_get_property(child, "reg", NULL); |
| 622 | if (!sw_addr) |
| 623 | continue; |
| 624 | |
| 625 | cd->sw_addr = be32_to_cpup(sw_addr); |
| 626 | if (cd->sw_addr > PHY_MAX_ADDR) |
| 627 | continue; |
| 628 | |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 629 | if (!of_property_read_u32(np, "eeprom-length", &eeprom_len)) |
| 630 | cd->eeprom_len = eeprom_len; |
| 631 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 632 | for_each_available_child_of_node(child, port) { |
| 633 | port_reg = of_get_property(port, "reg", NULL); |
| 634 | if (!port_reg) |
| 635 | continue; |
| 636 | |
| 637 | port_index = be32_to_cpup(port_reg); |
| 638 | |
| 639 | port_name = of_get_property(port, "label", NULL); |
| 640 | if (!port_name) |
| 641 | continue; |
| 642 | |
Florian Fainelli | bd47497 | 2014-08-27 17:04:50 -0700 | [diff] [blame] | 643 | cd->port_dn[port_index] = port; |
| 644 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 645 | cd->port_names[port_index] = kstrdup(port_name, |
| 646 | GFP_KERNEL); |
| 647 | if (!cd->port_names[port_index]) { |
| 648 | ret = -ENOMEM; |
| 649 | goto out_free_chip; |
| 650 | } |
| 651 | |
| 652 | link = of_parse_phandle(port, "link", 0); |
| 653 | |
| 654 | if (!strcmp(port_name, "dsa") && link && |
| 655 | pd->nr_chips > 1) { |
| 656 | ret = dsa_of_setup_routing_table(pd, cd, |
| 657 | chip_index, link); |
| 658 | if (ret) |
| 659 | goto out_free_chip; |
| 660 | } |
| 661 | |
| 662 | if (port_index == DSA_MAX_PORTS) |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | return 0; |
| 668 | |
| 669 | out_free_chip: |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 670 | dsa_of_free_platform_data(pd); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 671 | out_free: |
| 672 | kfree(pd); |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 673 | dev->platform_data = NULL; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 674 | return ret; |
| 675 | } |
| 676 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 677 | static void dsa_of_remove(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 678 | { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 679 | struct dsa_platform_data *pd = dev->platform_data; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 680 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 681 | if (!dev->of_node) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 682 | return; |
| 683 | |
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 | kfree(pd); |
| 686 | } |
| 687 | #else |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 688 | static inline int dsa_of_probe(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 689 | { |
| 690 | return 0; |
| 691 | } |
| 692 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 693 | static inline void dsa_of_remove(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 694 | { |
| 695 | } |
| 696 | #endif |
| 697 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 698 | static int dsa_probe(struct platform_device *pdev) |
| 699 | { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 700 | struct dsa_platform_data *pd = pdev->dev.platform_data; |
| 701 | struct net_device *dev; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 702 | struct dsa_switch_tree *dst; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 703 | int i, ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 704 | |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 705 | pr_notice_once("Distributed Switch Architecture driver version %s\n", |
| 706 | dsa_driver_version); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 707 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 708 | if (pdev->dev.of_node) { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 709 | ret = dsa_of_probe(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 710 | if (ret) |
| 711 | return ret; |
| 712 | |
| 713 | pd = pdev->dev.platform_data; |
| 714 | } |
| 715 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 716 | if (pd == NULL || pd->netdev == NULL) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 717 | return -EINVAL; |
| 718 | |
| 719 | dev = dev_to_net_device(pd->netdev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 720 | if (dev == NULL) { |
Florian Fainelli | b324c07 | 2015-03-05 12:35:05 -0800 | [diff] [blame^] | 721 | ret = -EPROBE_DEFER; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 722 | goto out; |
| 723 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 724 | |
| 725 | if (dev->dsa_ptr != NULL) { |
| 726 | dev_put(dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 727 | ret = -EEXIST; |
| 728 | goto out; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 731 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 732 | if (dst == NULL) { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 733 | dev_put(dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 734 | ret = -ENOMEM; |
| 735 | goto out; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 738 | platform_set_drvdata(pdev, dst); |
| 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++) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 746 | struct dsa_switch *ds; |
| 747 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 748 | 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] | 749 | if (IS_ERR(ds)) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 750 | netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", |
| 751 | i, PTR_ERR(ds)); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 752 | continue; |
| 753 | } |
| 754 | |
| 755 | dst->ds[i] = ds; |
| 756 | if (ds->drv->poll_link != NULL) |
| 757 | dst->link_poll_needed = 1; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 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 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 776 | |
| 777 | return 0; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 778 | |
| 779 | out: |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 780 | dsa_of_remove(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 781 | |
| 782 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | static int dsa_remove(struct platform_device *pdev) |
| 786 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 787 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 788 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 789 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 790 | if (dst->link_poll_needed) |
| 791 | del_timer_sync(&dst->link_poll_timer); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 792 | |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 793 | flush_work(&dst->link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 794 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 795 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 796 | struct dsa_switch *ds = dst->ds[i]; |
| 797 | |
| 798 | if (ds != NULL) |
| 799 | dsa_switch_destroy(ds); |
| 800 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 801 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 802 | dsa_of_remove(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 803 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | static void dsa_shutdown(struct platform_device *pdev) |
| 808 | { |
| 809 | } |
| 810 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 811 | static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev, |
| 812 | struct packet_type *pt, struct net_device *orig_dev) |
| 813 | { |
| 814 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 815 | |
| 816 | if (unlikely(dst == NULL)) { |
| 817 | kfree_skb(skb); |
| 818 | return 0; |
| 819 | } |
| 820 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 821 | return dst->rcv(skb, dev, pt, orig_dev); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Florian Fainelli | 61b7363 | 2014-08-29 12:42:07 -0700 | [diff] [blame] | 824 | static struct packet_type dsa_pack_type __read_mostly = { |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 825 | .type = cpu_to_be16(ETH_P_XDSA), |
| 826 | .func = dsa_switch_rcv, |
| 827 | }; |
| 828 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 829 | static struct notifier_block dsa_netdevice_nb __read_mostly = { |
| 830 | .notifier_call = dsa_slave_netdevice_event, |
| 831 | }; |
| 832 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 833 | #ifdef CONFIG_PM_SLEEP |
| 834 | static int dsa_suspend(struct device *d) |
| 835 | { |
| 836 | struct platform_device *pdev = to_platform_device(d); |
| 837 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 838 | int i, ret = 0; |
| 839 | |
| 840 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 841 | struct dsa_switch *ds = dst->ds[i]; |
| 842 | |
| 843 | if (ds != NULL) |
| 844 | ret = dsa_switch_suspend(ds); |
| 845 | } |
| 846 | |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | static int dsa_resume(struct device *d) |
| 851 | { |
| 852 | struct platform_device *pdev = to_platform_device(d); |
| 853 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 854 | int i, ret = 0; |
| 855 | |
| 856 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 857 | struct dsa_switch *ds = dst->ds[i]; |
| 858 | |
| 859 | if (ds != NULL) |
| 860 | ret = dsa_switch_resume(ds); |
| 861 | } |
| 862 | |
| 863 | return ret; |
| 864 | } |
| 865 | #endif |
| 866 | |
| 867 | static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); |
| 868 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 869 | static const struct of_device_id dsa_of_match_table[] = { |
Florian Fainelli | 246d7f7 | 2014-08-27 17:04:56 -0700 | [diff] [blame] | 870 | { .compatible = "brcm,bcm7445-switch-v4.0" }, |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 871 | { .compatible = "marvell,dsa", }, |
| 872 | {} |
| 873 | }; |
| 874 | MODULE_DEVICE_TABLE(of, dsa_of_match_table); |
| 875 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 876 | static struct platform_driver dsa_driver = { |
| 877 | .probe = dsa_probe, |
| 878 | .remove = dsa_remove, |
| 879 | .shutdown = dsa_shutdown, |
| 880 | .driver = { |
| 881 | .name = "dsa", |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 882 | .of_match_table = dsa_of_match_table, |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 883 | .pm = &dsa_pm_ops, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 884 | }, |
| 885 | }; |
| 886 | |
| 887 | static int __init dsa_init_module(void) |
| 888 | { |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 889 | int rc; |
| 890 | |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 891 | register_netdevice_notifier(&dsa_netdevice_nb); |
| 892 | |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 893 | rc = platform_driver_register(&dsa_driver); |
| 894 | if (rc) |
| 895 | return rc; |
| 896 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 897 | dev_add_pack(&dsa_pack_type); |
| 898 | |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 899 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 900 | } |
| 901 | module_init(dsa_init_module); |
| 902 | |
| 903 | static void __exit dsa_cleanup_module(void) |
| 904 | { |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 905 | unregister_netdevice_notifier(&dsa_netdevice_nb); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 906 | dev_remove_pack(&dsa_pack_type); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 907 | platform_driver_unregister(&dsa_driver); |
| 908 | } |
| 909 | module_exit(dsa_cleanup_module); |
| 910 | |
Rusty Russell | 577d6a7 | 2011-01-24 14:32:52 -0600 | [diff] [blame] | 911 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 912 | MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); |
| 913 | MODULE_LICENSE("GPL"); |
| 914 | MODULE_ALIAS("platform:dsa"); |