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