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 | |
| 12 | #include <linux/list.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 16 | #include <net/dsa.h> |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 17 | #include <linux/of.h> |
| 18 | #include <linux/of_mdio.h> |
| 19 | #include <linux/of_platform.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 20 | #include "dsa_priv.h" |
| 21 | |
| 22 | char dsa_driver_version[] = "0.1"; |
| 23 | |
| 24 | |
| 25 | /* switch driver registration ***********************************************/ |
| 26 | static DEFINE_MUTEX(dsa_switch_drivers_mutex); |
| 27 | static LIST_HEAD(dsa_switch_drivers); |
| 28 | |
| 29 | void register_switch_driver(struct dsa_switch_driver *drv) |
| 30 | { |
| 31 | mutex_lock(&dsa_switch_drivers_mutex); |
| 32 | list_add_tail(&drv->list, &dsa_switch_drivers); |
| 33 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 34 | } |
Ben Hutchings | ad293b8 | 2011-11-25 14:34:07 +0000 | [diff] [blame] | 35 | EXPORT_SYMBOL_GPL(register_switch_driver); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 36 | |
| 37 | void unregister_switch_driver(struct dsa_switch_driver *drv) |
| 38 | { |
| 39 | mutex_lock(&dsa_switch_drivers_mutex); |
| 40 | list_del_init(&drv->list); |
| 41 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 42 | } |
Ben Hutchings | ad293b8 | 2011-11-25 14:34:07 +0000 | [diff] [blame] | 43 | EXPORT_SYMBOL_GPL(unregister_switch_driver); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 44 | |
| 45 | static struct dsa_switch_driver * |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 46 | dsa_switch_probe(struct device *host_dev, int sw_addr, char **_name) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 47 | { |
| 48 | struct dsa_switch_driver *ret; |
| 49 | struct list_head *list; |
| 50 | char *name; |
| 51 | |
| 52 | ret = NULL; |
| 53 | name = NULL; |
| 54 | |
| 55 | mutex_lock(&dsa_switch_drivers_mutex); |
| 56 | list_for_each(list, &dsa_switch_drivers) { |
| 57 | struct dsa_switch_driver *drv; |
| 58 | |
| 59 | drv = list_entry(list, struct dsa_switch_driver, list); |
| 60 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 61 | name = drv->probe(host_dev, sw_addr); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 62 | if (name != NULL) { |
| 63 | ret = drv; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 68 | |
| 69 | *_name = name; |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* basic switch operations **************************************************/ |
| 76 | static struct dsa_switch * |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 77 | dsa_switch_setup(struct dsa_switch_tree *dst, int index, |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 78 | struct device *parent, struct device *host_dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 79 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 80 | struct dsa_chip_data *pd = dst->pd->chip + index; |
| 81 | struct dsa_switch_driver *drv; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 82 | struct dsa_switch *ds; |
| 83 | int ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 84 | char *name; |
| 85 | int i; |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 86 | bool valid_name_found = false; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Probe for switch model. |
| 90 | */ |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 91 | drv = dsa_switch_probe(host_dev, pd->sw_addr, &name); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 92 | if (drv == NULL) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 93 | printk(KERN_ERR "%s[%d]: could not detect attached switch\n", |
| 94 | dst->master_netdev->name, index); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 95 | return ERR_PTR(-EINVAL); |
| 96 | } |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 97 | printk(KERN_INFO "%s[%d]: detected a %s switch\n", |
| 98 | dst->master_netdev->name, index, name); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | /* |
| 102 | * Allocate and initialise switch state. |
| 103 | */ |
| 104 | ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL); |
| 105 | if (ds == NULL) |
| 106 | return ERR_PTR(-ENOMEM); |
| 107 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 108 | ds->dst = dst; |
| 109 | ds->index = index; |
| 110 | ds->pd = dst->pd->chip + index; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 111 | ds->drv = drv; |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 112 | ds->master_dev = host_dev; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * Validate supplied switch configuration. |
| 116 | */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 117 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 118 | char *name; |
| 119 | |
| 120 | name = pd->port_names[i]; |
| 121 | if (name == NULL) |
| 122 | continue; |
| 123 | |
| 124 | if (!strcmp(name, "cpu")) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 125 | if (dst->cpu_switch != -1) { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 126 | printk(KERN_ERR "multiple cpu ports?!\n"); |
| 127 | ret = -EINVAL; |
| 128 | goto out; |
| 129 | } |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 130 | dst->cpu_switch = index; |
| 131 | dst->cpu_port = i; |
| 132 | } else if (!strcmp(name, "dsa")) { |
| 133 | ds->dsa_port_mask |= 1 << i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 134 | } else { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 135 | ds->phys_port_mask |= 1 << i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 136 | } |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 137 | valid_name_found = true; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 140 | if (!valid_name_found && i == DSA_MAX_PORTS) { |
| 141 | ret = -EINVAL; |
| 142 | goto out; |
| 143 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 144 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 145 | /* Make the built-in MII bus mask match the number of ports, |
| 146 | * switch drivers can override this later |
| 147 | */ |
| 148 | ds->phys_mii_mask = ds->phys_port_mask; |
| 149 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 150 | /* |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 151 | * If the CPU connects to this switch, set the switch tree |
| 152 | * tagging protocol to the preferred tagging format of this |
| 153 | * switch. |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 154 | */ |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 155 | if (dst->cpu_switch == index) { |
| 156 | switch (drv->tag_protocol) { |
| 157 | #ifdef CONFIG_NET_DSA_TAG_DSA |
| 158 | case DSA_TAG_PROTO_DSA: |
| 159 | dst->rcv = dsa_netdev_ops.rcv; |
| 160 | break; |
| 161 | #endif |
| 162 | #ifdef CONFIG_NET_DSA_TAG_EDSA |
| 163 | case DSA_TAG_PROTO_EDSA: |
| 164 | dst->rcv = edsa_netdev_ops.rcv; |
| 165 | break; |
| 166 | #endif |
| 167 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
| 168 | case DSA_TAG_PROTO_TRAILER: |
| 169 | dst->rcv = trailer_netdev_ops.rcv; |
| 170 | break; |
| 171 | #endif |
| 172 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 173 | case DSA_TAG_PROTO_BRCM: |
| 174 | dst->rcv = brcm_netdev_ops.rcv; |
| 175 | break; |
| 176 | #endif |
| 177 | default: |
| 178 | break; |
| 179 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 180 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 181 | dst->tag_protocol = drv->tag_protocol; |
| 182 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * Do basic register setup. |
| 186 | */ |
| 187 | ret = drv->setup(ds); |
| 188 | if (ret < 0) |
| 189 | goto out; |
| 190 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 191 | ret = drv->set_addr(ds, dst->master_netdev->dev_addr); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 192 | if (ret < 0) |
| 193 | goto out; |
| 194 | |
| 195 | ds->slave_mii_bus = mdiobus_alloc(); |
| 196 | if (ds->slave_mii_bus == NULL) { |
| 197 | ret = -ENOMEM; |
| 198 | goto out; |
| 199 | } |
| 200 | dsa_slave_mii_bus_init(ds); |
| 201 | |
| 202 | ret = mdiobus_register(ds->slave_mii_bus); |
| 203 | if (ret < 0) |
| 204 | goto out_free; |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | * Create network devices for physical switch ports. |
| 209 | */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 210 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 211 | struct net_device *slave_dev; |
| 212 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 213 | if (!(ds->phys_port_mask & (1 << i))) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 214 | continue; |
| 215 | |
| 216 | slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]); |
| 217 | if (slave_dev == NULL) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 218 | printk(KERN_ERR "%s[%d]: can't create dsa " |
| 219 | "slave device for port %d(%s)\n", |
| 220 | dst->master_netdev->name, |
| 221 | index, i, pd->port_names[i]); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 222 | continue; |
| 223 | } |
| 224 | |
| 225 | ds->ports[i] = slave_dev; |
| 226 | } |
| 227 | |
| 228 | return ds; |
| 229 | |
| 230 | out_free: |
| 231 | mdiobus_free(ds->slave_mii_bus); |
| 232 | out: |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 233 | kfree(ds); |
| 234 | return ERR_PTR(ret); |
| 235 | } |
| 236 | |
| 237 | static void dsa_switch_destroy(struct dsa_switch *ds) |
| 238 | { |
| 239 | } |
| 240 | |
Thierry Reding | e506d40 | 2014-10-01 13:59:00 +0200 | [diff] [blame^] | 241 | #ifdef CONFIG_PM_SLEEP |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 242 | static int dsa_switch_suspend(struct dsa_switch *ds) |
| 243 | { |
| 244 | int i, ret = 0; |
| 245 | |
| 246 | /* Suspend slave network devices */ |
| 247 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 248 | if (!(ds->phys_port_mask & (1 << i))) |
| 249 | continue; |
| 250 | |
| 251 | ret = dsa_slave_suspend(ds->ports[i]); |
| 252 | if (ret) |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | if (ds->drv->suspend) |
| 257 | ret = ds->drv->suspend(ds); |
| 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static int dsa_switch_resume(struct dsa_switch *ds) |
| 263 | { |
| 264 | int i, ret = 0; |
| 265 | |
| 266 | if (ds->drv->resume) |
| 267 | ret = ds->drv->resume(ds); |
| 268 | |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | |
| 272 | /* Resume slave network devices */ |
| 273 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 274 | if (!(ds->phys_port_mask & (1 << i))) |
| 275 | continue; |
| 276 | |
| 277 | ret = dsa_slave_resume(ds->ports[i]); |
| 278 | if (ret) |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
Thierry Reding | e506d40 | 2014-10-01 13:59:00 +0200 | [diff] [blame^] | 284 | #endif |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 285 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 286 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 287 | /* link polling *************************************************************/ |
| 288 | static void dsa_link_poll_work(struct work_struct *ugly) |
| 289 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 290 | struct dsa_switch_tree *dst; |
| 291 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 292 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 293 | dst = container_of(ugly, struct dsa_switch_tree, link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 294 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 295 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 296 | struct dsa_switch *ds = dst->ds[i]; |
| 297 | |
| 298 | if (ds != NULL && ds->drv->poll_link != NULL) |
| 299 | ds->drv->poll_link(ds); |
| 300 | } |
| 301 | |
| 302 | mod_timer(&dst->link_poll_timer, round_jiffies(jiffies + HZ)); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 305 | static void dsa_link_poll_timer(unsigned long _dst) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 306 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 307 | struct dsa_switch_tree *dst = (void *)_dst; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 308 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 309 | schedule_work(&dst->link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
| 313 | /* platform driver init and cleanup *****************************************/ |
| 314 | static int dev_is_class(struct device *dev, void *class) |
| 315 | { |
| 316 | if (dev->class != NULL && !strcmp(dev->class->name, class)) |
| 317 | return 1; |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static struct device *dev_find_class(struct device *parent, char *class) |
| 323 | { |
| 324 | if (dev_is_class(parent, class)) { |
| 325 | get_device(parent); |
| 326 | return parent; |
| 327 | } |
| 328 | |
| 329 | return device_find_child(parent, class, dev_is_class); |
| 330 | } |
| 331 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 332 | struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 333 | { |
| 334 | struct device *d; |
| 335 | |
| 336 | d = dev_find_class(dev, "mdio_bus"); |
| 337 | if (d != NULL) { |
| 338 | struct mii_bus *bus; |
| 339 | |
| 340 | bus = to_mii_bus(d); |
| 341 | put_device(d); |
| 342 | |
| 343 | return bus; |
| 344 | } |
| 345 | |
| 346 | return NULL; |
| 347 | } |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 348 | EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 349 | |
| 350 | static struct net_device *dev_to_net_device(struct device *dev) |
| 351 | { |
| 352 | struct device *d; |
| 353 | |
| 354 | d = dev_find_class(dev, "net"); |
| 355 | if (d != NULL) { |
| 356 | struct net_device *nd; |
| 357 | |
| 358 | nd = to_net_dev(d); |
| 359 | dev_hold(nd); |
| 360 | put_device(d); |
| 361 | |
| 362 | return nd; |
| 363 | } |
| 364 | |
| 365 | return NULL; |
| 366 | } |
| 367 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 368 | #ifdef CONFIG_OF |
| 369 | static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, |
| 370 | struct dsa_chip_data *cd, |
| 371 | int chip_index, |
| 372 | struct device_node *link) |
| 373 | { |
| 374 | int ret; |
| 375 | const __be32 *reg; |
| 376 | int link_port_addr; |
| 377 | int link_sw_addr; |
| 378 | struct device_node *parent_sw; |
| 379 | int len; |
| 380 | |
| 381 | parent_sw = of_get_parent(link); |
| 382 | if (!parent_sw) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | reg = of_get_property(parent_sw, "reg", &len); |
| 386 | if (!reg || (len != sizeof(*reg) * 2)) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | link_sw_addr = be32_to_cpup(reg + 1); |
| 390 | |
| 391 | if (link_sw_addr >= pd->nr_chips) |
| 392 | return -EINVAL; |
| 393 | |
| 394 | /* First time routing table allocation */ |
| 395 | if (!cd->rtable) { |
| 396 | cd->rtable = kmalloc(pd->nr_chips * sizeof(s8), GFP_KERNEL); |
| 397 | if (!cd->rtable) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | /* default to no valid uplink/downlink */ |
| 401 | memset(cd->rtable, -1, pd->nr_chips * sizeof(s8)); |
| 402 | } |
| 403 | |
| 404 | reg = of_get_property(link, "reg", NULL); |
| 405 | if (!reg) { |
| 406 | ret = -EINVAL; |
| 407 | goto out; |
| 408 | } |
| 409 | |
| 410 | link_port_addr = be32_to_cpup(reg); |
| 411 | |
| 412 | cd->rtable[link_sw_addr] = link_port_addr; |
| 413 | |
| 414 | return 0; |
| 415 | out: |
| 416 | kfree(cd->rtable); |
| 417 | return ret; |
| 418 | } |
| 419 | |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 420 | static void dsa_of_free_platform_data(struct dsa_platform_data *pd) |
| 421 | { |
| 422 | int i; |
| 423 | int port_index; |
| 424 | |
| 425 | for (i = 0; i < pd->nr_chips; i++) { |
| 426 | port_index = 0; |
Florian Fainelli | 5f64a7d | 2013-03-25 05:03:40 +0000 | [diff] [blame] | 427 | while (port_index < DSA_MAX_PORTS) { |
Fabian Frederick | 1f74714 | 2014-06-23 19:32:47 +0200 | [diff] [blame] | 428 | kfree(pd->chip[i].port_names[port_index]); |
Florian Fainelli | 5f64a7d | 2013-03-25 05:03:40 +0000 | [diff] [blame] | 429 | port_index++; |
| 430 | } |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 431 | kfree(pd->chip[i].rtable); |
| 432 | } |
| 433 | kfree(pd->chip); |
| 434 | } |
| 435 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 436 | static int dsa_of_probe(struct platform_device *pdev) |
| 437 | { |
| 438 | struct device_node *np = pdev->dev.of_node; |
| 439 | struct device_node *child, *mdio, *ethernet, *port, *link; |
| 440 | struct mii_bus *mdio_bus; |
| 441 | struct platform_device *ethernet_dev; |
| 442 | struct dsa_platform_data *pd; |
| 443 | struct dsa_chip_data *cd; |
| 444 | const char *port_name; |
| 445 | int chip_index, port_index; |
| 446 | const unsigned int *sw_addr, *port_reg; |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 447 | int ret; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 448 | |
| 449 | mdio = of_parse_phandle(np, "dsa,mii-bus", 0); |
| 450 | if (!mdio) |
| 451 | return -EINVAL; |
| 452 | |
| 453 | mdio_bus = of_mdio_find_bus(mdio); |
| 454 | if (!mdio_bus) |
| 455 | return -EINVAL; |
| 456 | |
| 457 | ethernet = of_parse_phandle(np, "dsa,ethernet", 0); |
| 458 | if (!ethernet) |
| 459 | return -EINVAL; |
| 460 | |
| 461 | ethernet_dev = of_find_device_by_node(ethernet); |
| 462 | if (!ethernet_dev) |
| 463 | return -ENODEV; |
| 464 | |
| 465 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 466 | if (!pd) |
| 467 | return -ENOMEM; |
| 468 | |
| 469 | pdev->dev.platform_data = pd; |
| 470 | pd->netdev = ðernet_dev->dev; |
| 471 | pd->nr_chips = of_get_child_count(np); |
| 472 | if (pd->nr_chips > DSA_MAX_SWITCHES) |
| 473 | pd->nr_chips = DSA_MAX_SWITCHES; |
| 474 | |
| 475 | pd->chip = kzalloc(pd->nr_chips * sizeof(struct dsa_chip_data), |
| 476 | GFP_KERNEL); |
| 477 | if (!pd->chip) { |
| 478 | ret = -ENOMEM; |
| 479 | goto out_free; |
| 480 | } |
| 481 | |
Fabian Godehardt | d1c0b47 | 2014-05-16 06:21:44 +0200 | [diff] [blame] | 482 | chip_index = -1; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 483 | for_each_available_child_of_node(np, child) { |
Fabian Godehardt | d1c0b47 | 2014-05-16 06:21:44 +0200 | [diff] [blame] | 484 | chip_index++; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 485 | cd = &pd->chip[chip_index]; |
| 486 | |
Florian Fainelli | fa981d9 | 2014-08-27 17:04:49 -0700 | [diff] [blame] | 487 | cd->of_node = child; |
Florian Fainelli | c1f570a | 2014-09-15 14:48:08 -0700 | [diff] [blame] | 488 | cd->host_dev = &mdio_bus->dev; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 489 | |
| 490 | sw_addr = of_get_property(child, "reg", NULL); |
| 491 | if (!sw_addr) |
| 492 | continue; |
| 493 | |
| 494 | cd->sw_addr = be32_to_cpup(sw_addr); |
| 495 | if (cd->sw_addr > PHY_MAX_ADDR) |
| 496 | continue; |
| 497 | |
| 498 | for_each_available_child_of_node(child, port) { |
| 499 | port_reg = of_get_property(port, "reg", NULL); |
| 500 | if (!port_reg) |
| 501 | continue; |
| 502 | |
| 503 | port_index = be32_to_cpup(port_reg); |
| 504 | |
| 505 | port_name = of_get_property(port, "label", NULL); |
| 506 | if (!port_name) |
| 507 | continue; |
| 508 | |
Florian Fainelli | bd47497 | 2014-08-27 17:04:50 -0700 | [diff] [blame] | 509 | cd->port_dn[port_index] = port; |
| 510 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 511 | cd->port_names[port_index] = kstrdup(port_name, |
| 512 | GFP_KERNEL); |
| 513 | if (!cd->port_names[port_index]) { |
| 514 | ret = -ENOMEM; |
| 515 | goto out_free_chip; |
| 516 | } |
| 517 | |
| 518 | link = of_parse_phandle(port, "link", 0); |
| 519 | |
| 520 | if (!strcmp(port_name, "dsa") && link && |
| 521 | pd->nr_chips > 1) { |
| 522 | ret = dsa_of_setup_routing_table(pd, cd, |
| 523 | chip_index, link); |
| 524 | if (ret) |
| 525 | goto out_free_chip; |
| 526 | } |
| 527 | |
| 528 | if (port_index == DSA_MAX_PORTS) |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | |
| 535 | out_free_chip: |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 536 | dsa_of_free_platform_data(pd); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 537 | out_free: |
| 538 | kfree(pd); |
| 539 | pdev->dev.platform_data = NULL; |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | static void dsa_of_remove(struct platform_device *pdev) |
| 544 | { |
| 545 | struct dsa_platform_data *pd = pdev->dev.platform_data; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 546 | |
| 547 | if (!pdev->dev.of_node) |
| 548 | return; |
| 549 | |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 550 | dsa_of_free_platform_data(pd); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 551 | kfree(pd); |
| 552 | } |
| 553 | #else |
| 554 | static inline int dsa_of_probe(struct platform_device *pdev) |
| 555 | { |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | static inline void dsa_of_remove(struct platform_device *pdev) |
| 560 | { |
| 561 | } |
| 562 | #endif |
| 563 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 564 | static int dsa_probe(struct platform_device *pdev) |
| 565 | { |
| 566 | static int dsa_version_printed; |
| 567 | struct dsa_platform_data *pd = pdev->dev.platform_data; |
| 568 | struct net_device *dev; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 569 | struct dsa_switch_tree *dst; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 570 | int i, ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 571 | |
| 572 | if (!dsa_version_printed++) |
| 573 | printk(KERN_NOTICE "Distributed Switch Architecture " |
| 574 | "driver version %s\n", dsa_driver_version); |
| 575 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 576 | if (pdev->dev.of_node) { |
| 577 | ret = dsa_of_probe(pdev); |
| 578 | if (ret) |
| 579 | return ret; |
| 580 | |
| 581 | pd = pdev->dev.platform_data; |
| 582 | } |
| 583 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 584 | if (pd == NULL || pd->netdev == NULL) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 585 | return -EINVAL; |
| 586 | |
| 587 | dev = dev_to_net_device(pd->netdev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 588 | if (dev == NULL) { |
| 589 | ret = -EINVAL; |
| 590 | goto out; |
| 591 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 592 | |
| 593 | if (dev->dsa_ptr != NULL) { |
| 594 | dev_put(dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 595 | ret = -EEXIST; |
| 596 | goto out; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 599 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 600 | if (dst == NULL) { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 601 | dev_put(dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 602 | ret = -ENOMEM; |
| 603 | goto out; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 606 | platform_set_drvdata(pdev, dst); |
| 607 | |
| 608 | dst->pd = pd; |
| 609 | dst->master_netdev = dev; |
| 610 | dst->cpu_switch = -1; |
| 611 | dst->cpu_port = -1; |
| 612 | |
| 613 | for (i = 0; i < pd->nr_chips; i++) { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 614 | struct dsa_switch *ds; |
| 615 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 616 | ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 617 | if (IS_ERR(ds)) { |
| 618 | printk(KERN_ERR "%s[%d]: couldn't create dsa switch " |
| 619 | "instance (error %ld)\n", dev->name, i, |
| 620 | PTR_ERR(ds)); |
| 621 | continue; |
| 622 | } |
| 623 | |
| 624 | dst->ds[i] = ds; |
| 625 | if (ds->drv->poll_link != NULL) |
| 626 | dst->link_poll_needed = 1; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 629 | /* |
| 630 | * If we use a tagging format that doesn't have an ethertype |
| 631 | * field, make sure that all packets from this point on get |
| 632 | * sent to the tag format's receive function. |
| 633 | */ |
| 634 | wmb(); |
| 635 | dev->dsa_ptr = (void *)dst; |
| 636 | |
| 637 | if (dst->link_poll_needed) { |
| 638 | INIT_WORK(&dst->link_poll_work, dsa_link_poll_work); |
| 639 | init_timer(&dst->link_poll_timer); |
| 640 | dst->link_poll_timer.data = (unsigned long)dst; |
| 641 | dst->link_poll_timer.function = dsa_link_poll_timer; |
| 642 | dst->link_poll_timer.expires = round_jiffies(jiffies + HZ); |
| 643 | add_timer(&dst->link_poll_timer); |
| 644 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 645 | |
| 646 | return 0; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 647 | |
| 648 | out: |
| 649 | dsa_of_remove(pdev); |
| 650 | |
| 651 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static int dsa_remove(struct platform_device *pdev) |
| 655 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 656 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 657 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 658 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 659 | if (dst->link_poll_needed) |
| 660 | del_timer_sync(&dst->link_poll_timer); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 661 | |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 662 | flush_work(&dst->link_poll_work); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 663 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 664 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 665 | struct dsa_switch *ds = dst->ds[i]; |
| 666 | |
| 667 | if (ds != NULL) |
| 668 | dsa_switch_destroy(ds); |
| 669 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 670 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 671 | dsa_of_remove(pdev); |
| 672 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | static void dsa_shutdown(struct platform_device *pdev) |
| 677 | { |
| 678 | } |
| 679 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 680 | static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev, |
| 681 | struct packet_type *pt, struct net_device *orig_dev) |
| 682 | { |
| 683 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 684 | |
| 685 | if (unlikely(dst == NULL)) { |
| 686 | kfree_skb(skb); |
| 687 | return 0; |
| 688 | } |
| 689 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 690 | return dst->rcv(skb, dev, pt, orig_dev); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Florian Fainelli | 61b7363 | 2014-08-29 12:42:07 -0700 | [diff] [blame] | 693 | static struct packet_type dsa_pack_type __read_mostly = { |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 694 | .type = cpu_to_be16(ETH_P_XDSA), |
| 695 | .func = dsa_switch_rcv, |
| 696 | }; |
| 697 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 698 | #ifdef CONFIG_PM_SLEEP |
| 699 | static int dsa_suspend(struct device *d) |
| 700 | { |
| 701 | struct platform_device *pdev = to_platform_device(d); |
| 702 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 703 | int i, ret = 0; |
| 704 | |
| 705 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 706 | struct dsa_switch *ds = dst->ds[i]; |
| 707 | |
| 708 | if (ds != NULL) |
| 709 | ret = dsa_switch_suspend(ds); |
| 710 | } |
| 711 | |
| 712 | return ret; |
| 713 | } |
| 714 | |
| 715 | static int dsa_resume(struct device *d) |
| 716 | { |
| 717 | struct platform_device *pdev = to_platform_device(d); |
| 718 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 719 | int i, ret = 0; |
| 720 | |
| 721 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 722 | struct dsa_switch *ds = dst->ds[i]; |
| 723 | |
| 724 | if (ds != NULL) |
| 725 | ret = dsa_switch_resume(ds); |
| 726 | } |
| 727 | |
| 728 | return ret; |
| 729 | } |
| 730 | #endif |
| 731 | |
| 732 | static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); |
| 733 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 734 | static const struct of_device_id dsa_of_match_table[] = { |
Florian Fainelli | 246d7f7 | 2014-08-27 17:04:56 -0700 | [diff] [blame] | 735 | { .compatible = "brcm,bcm7445-switch-v4.0" }, |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 736 | { .compatible = "marvell,dsa", }, |
| 737 | {} |
| 738 | }; |
| 739 | MODULE_DEVICE_TABLE(of, dsa_of_match_table); |
| 740 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 741 | static struct platform_driver dsa_driver = { |
| 742 | .probe = dsa_probe, |
| 743 | .remove = dsa_remove, |
| 744 | .shutdown = dsa_shutdown, |
| 745 | .driver = { |
| 746 | .name = "dsa", |
| 747 | .owner = THIS_MODULE, |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 748 | .of_match_table = dsa_of_match_table, |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 749 | .pm = &dsa_pm_ops, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 750 | }, |
| 751 | }; |
| 752 | |
| 753 | static int __init dsa_init_module(void) |
| 754 | { |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 755 | int rc; |
| 756 | |
| 757 | rc = platform_driver_register(&dsa_driver); |
| 758 | if (rc) |
| 759 | return rc; |
| 760 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 761 | dev_add_pack(&dsa_pack_type); |
| 762 | |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 763 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 764 | } |
| 765 | module_init(dsa_init_module); |
| 766 | |
| 767 | static void __exit dsa_cleanup_module(void) |
| 768 | { |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 769 | dev_remove_pack(&dsa_pack_type); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 770 | platform_driver_unregister(&dsa_driver); |
| 771 | } |
| 772 | module_exit(dsa_cleanup_module); |
| 773 | |
Rusty Russell | 577d6a7 | 2011-01-24 14:32:52 -0600 | [diff] [blame] | 774 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 775 | MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); |
| 776 | MODULE_LICENSE("GPL"); |
| 777 | MODULE_ALIAS("platform:dsa"); |