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