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