Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/legacy.c - Hardware switch handling |
| 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
| 4 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> |
| 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/device.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_mdio.h> |
| 19 | #include <linux/of_platform.h> |
| 20 | #include <linux/of_net.h> |
| 21 | #include <linux/netdevice.h> |
| 22 | #include <linux/sysfs.h> |
| 23 | #include <linux/phy_fixed.h> |
| 24 | #include <linux/etherdevice.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 25 | |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 26 | #include "dsa_priv.h" |
| 27 | |
| 28 | /* switch driver registration ***********************************************/ |
| 29 | static DEFINE_MUTEX(dsa_switch_drivers_mutex); |
| 30 | static LIST_HEAD(dsa_switch_drivers); |
| 31 | |
| 32 | void register_switch_driver(struct dsa_switch_driver *drv) |
| 33 | { |
| 34 | mutex_lock(&dsa_switch_drivers_mutex); |
| 35 | list_add_tail(&drv->list, &dsa_switch_drivers); |
| 36 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 37 | } |
| 38 | EXPORT_SYMBOL_GPL(register_switch_driver); |
| 39 | |
| 40 | void unregister_switch_driver(struct dsa_switch_driver *drv) |
| 41 | { |
| 42 | mutex_lock(&dsa_switch_drivers_mutex); |
| 43 | list_del_init(&drv->list); |
| 44 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(unregister_switch_driver); |
| 47 | |
| 48 | static const struct dsa_switch_ops * |
| 49 | dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr, |
| 50 | const char **_name, void **priv) |
| 51 | { |
| 52 | const struct dsa_switch_ops *ret; |
| 53 | struct list_head *list; |
| 54 | const char *name; |
| 55 | |
| 56 | ret = NULL; |
| 57 | name = NULL; |
| 58 | |
| 59 | mutex_lock(&dsa_switch_drivers_mutex); |
| 60 | list_for_each(list, &dsa_switch_drivers) { |
| 61 | const struct dsa_switch_ops *ops; |
| 62 | struct dsa_switch_driver *drv; |
| 63 | |
| 64 | drv = list_entry(list, struct dsa_switch_driver, list); |
| 65 | ops = drv->ops; |
| 66 | |
| 67 | name = ops->probe(parent, host_dev, sw_addr, priv); |
| 68 | if (name != NULL) { |
| 69 | ret = ops; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 74 | |
| 75 | *_name = name; |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | /* basic switch operations **************************************************/ |
Vivien Didelot | 206e41f | 2017-08-05 16:20:17 -0400 | [diff] [blame] | 81 | static int dsa_cpu_dsa_setups(struct dsa_switch *ds) |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 82 | { |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 83 | int ret, port; |
| 84 | |
| 85 | for (port = 0; port < ds->num_ports; port++) { |
| 86 | if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) |
| 87 | continue; |
| 88 | |
Vivien Didelot | 47d0dcc | 2017-08-05 16:20:18 -0400 | [diff] [blame] | 89 | ret = dsa_cpu_dsa_setup(&ds->ports[port]); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 90 | if (ret) |
| 91 | return ret; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
Vivien Didelot | 206e41f | 2017-08-05 16:20:17 -0400 | [diff] [blame] | 96 | static int dsa_switch_setup_one(struct dsa_switch *ds, |
| 97 | struct net_device *master) |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 98 | { |
| 99 | const struct dsa_switch_ops *ops = ds->ops; |
| 100 | struct dsa_switch_tree *dst = ds->dst; |
| 101 | struct dsa_chip_data *cd = ds->cd; |
| 102 | bool valid_name_found = false; |
| 103 | int index = ds->index; |
| 104 | int i, ret; |
| 105 | |
| 106 | /* |
| 107 | * Validate supplied switch configuration. |
| 108 | */ |
| 109 | for (i = 0; i < ds->num_ports; i++) { |
| 110 | char *name; |
| 111 | |
| 112 | name = cd->port_names[i]; |
| 113 | if (name == NULL) |
| 114 | continue; |
| 115 | |
| 116 | if (!strcmp(name, "cpu")) { |
Vivien Didelot | 8b0d3ea | 2017-05-16 14:10:33 -0400 | [diff] [blame] | 117 | if (dst->cpu_dp) { |
Florian Fainelli | 6d3c8c0 | 2017-06-13 13:27:19 -0700 | [diff] [blame] | 118 | netdev_err(master, |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 119 | "multiple cpu ports?!\n"); |
| 120 | return -EINVAL; |
| 121 | } |
Vivien Didelot | 8b0d3ea | 2017-05-16 14:10:33 -0400 | [diff] [blame] | 122 | dst->cpu_dp = &ds->ports[i]; |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 123 | dst->cpu_dp->master = master; |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 124 | ds->cpu_port_mask |= 1 << i; |
| 125 | } else if (!strcmp(name, "dsa")) { |
| 126 | ds->dsa_port_mask |= 1 << i; |
| 127 | } else { |
| 128 | ds->enabled_port_mask |= 1 << i; |
| 129 | } |
| 130 | valid_name_found = true; |
| 131 | } |
| 132 | |
| 133 | if (!valid_name_found && i == ds->num_ports) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | /* Make the built-in MII bus mask match the number of ports, |
| 137 | * switch drivers can override this later |
| 138 | */ |
| 139 | ds->phys_mii_mask = ds->enabled_port_mask; |
| 140 | |
| 141 | /* |
| 142 | * If the CPU connects to this switch, set the switch tree |
| 143 | * tagging protocol to the preferred tagging format of this |
| 144 | * switch. |
| 145 | */ |
Vivien Didelot | 8b0d3ea | 2017-05-16 14:10:33 -0400 | [diff] [blame] | 146 | if (dst->cpu_dp->ds == ds) { |
Vivien Didelot | 62fc958 | 2017-09-29 17:19:17 -0400 | [diff] [blame] | 147 | const struct dsa_device_ops *tag_ops; |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 148 | enum dsa_tag_protocol tag_protocol; |
| 149 | |
| 150 | tag_protocol = ops->get_tag_protocol(ds); |
Vivien Didelot | 62fc958 | 2017-09-29 17:19:17 -0400 | [diff] [blame] | 151 | tag_ops = dsa_resolve_tag_protocol(tag_protocol); |
| 152 | if (IS_ERR(tag_ops)) |
| 153 | return PTR_ERR(tag_ops); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 154 | |
Vivien Didelot | 1524024 | 2017-09-29 17:19:18 -0400 | [diff] [blame] | 155 | dst->cpu_dp->tag_ops = tag_ops; |
Vivien Didelot | 3e41f93 | 2017-09-29 17:19:19 -0400 | [diff] [blame] | 156 | |
| 157 | /* Few copies for faster access in master receive hot path */ |
| 158 | dst->cpu_dp->rcv = dst->cpu_dp->tag_ops->rcv; |
Vivien Didelot | 3e41f93 | 2017-09-29 17:19:19 -0400 | [diff] [blame] | 159 | dst->cpu_dp->dst = dst; |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable)); |
| 163 | |
| 164 | /* |
| 165 | * Do basic register setup. |
| 166 | */ |
| 167 | ret = ops->setup(ds); |
| 168 | if (ret < 0) |
| 169 | return ret; |
| 170 | |
| 171 | ret = dsa_switch_register_notifier(ds); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 175 | if (!ds->slave_mii_bus && ops->phy_read) { |
Vivien Didelot | 206e41f | 2017-08-05 16:20:17 -0400 | [diff] [blame] | 176 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 177 | if (!ds->slave_mii_bus) |
| 178 | return -ENOMEM; |
| 179 | dsa_slave_mii_bus_init(ds); |
| 180 | |
| 181 | ret = mdiobus_register(ds->slave_mii_bus); |
| 182 | if (ret < 0) |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Create network devices for physical switch ports. |
| 188 | */ |
| 189 | for (i = 0; i < ds->num_ports; i++) { |
| 190 | ds->ports[i].dn = cd->port_dn[i]; |
Florian Fainelli | 06d4d45 | 2017-06-16 16:42:11 -0700 | [diff] [blame] | 191 | ds->ports[i].cpu_dp = dst->cpu_dp; |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 192 | |
Vivien Didelot | 4a5b85f | 2017-10-26 11:22:55 -0400 | [diff] [blame^] | 193 | if (dsa_is_user_port(ds, i)) |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 194 | continue; |
| 195 | |
Vivien Didelot | 4cfbf09 | 2017-08-05 16:20:19 -0400 | [diff] [blame] | 196 | ret = dsa_slave_create(&ds->ports[i], cd->port_names[i]); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 197 | if (ret < 0) |
Florian Fainelli | 6d3c8c0 | 2017-06-13 13:27:19 -0700 | [diff] [blame] | 198 | netdev_err(master, "[%d]: can't create dsa slave device for port %d(%s): %d\n", |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 199 | index, i, cd->port_names[i], ret); |
| 200 | } |
| 201 | |
| 202 | /* Perform configuration of the CPU and DSA ports */ |
Vivien Didelot | 206e41f | 2017-08-05 16:20:17 -0400 | [diff] [blame] | 203 | ret = dsa_cpu_dsa_setups(ds); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 204 | if (ret < 0) |
Florian Fainelli | 6d3c8c0 | 2017-06-13 13:27:19 -0700 | [diff] [blame] | 205 | netdev_err(master, "[%d] : can't configure CPU and DSA ports\n", |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 206 | index); |
| 207 | |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static struct dsa_switch * |
Florian Fainelli | 06d4d45 | 2017-06-16 16:42:11 -0700 | [diff] [blame] | 212 | dsa_switch_setup(struct dsa_switch_tree *dst, struct net_device *master, |
| 213 | int index, struct device *parent, struct device *host_dev) |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 214 | { |
| 215 | struct dsa_chip_data *cd = dst->pd->chip + index; |
| 216 | const struct dsa_switch_ops *ops; |
| 217 | struct dsa_switch *ds; |
| 218 | int ret; |
| 219 | const char *name; |
| 220 | void *priv; |
| 221 | |
| 222 | /* |
| 223 | * Probe for switch model. |
| 224 | */ |
| 225 | ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv); |
| 226 | if (!ops) { |
Florian Fainelli | 6d3c8c0 | 2017-06-13 13:27:19 -0700 | [diff] [blame] | 227 | netdev_err(master, "[%d]: could not detect attached switch\n", |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 228 | index); |
| 229 | return ERR_PTR(-EINVAL); |
| 230 | } |
Florian Fainelli | 6d3c8c0 | 2017-06-13 13:27:19 -0700 | [diff] [blame] | 231 | netdev_info(master, "[%d]: detected a %s switch\n", |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 232 | index, name); |
| 233 | |
| 234 | |
| 235 | /* |
| 236 | * Allocate and initialise switch state. |
| 237 | */ |
| 238 | ds = dsa_switch_alloc(parent, DSA_MAX_PORTS); |
| 239 | if (!ds) |
| 240 | return ERR_PTR(-ENOMEM); |
| 241 | |
| 242 | ds->dst = dst; |
| 243 | ds->index = index; |
| 244 | ds->cd = cd; |
| 245 | ds->ops = ops; |
| 246 | ds->priv = priv; |
| 247 | |
Vivien Didelot | 206e41f | 2017-08-05 16:20:17 -0400 | [diff] [blame] | 248 | ret = dsa_switch_setup_one(ds, master); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 249 | if (ret) |
| 250 | return ERR_PTR(ret); |
| 251 | |
| 252 | return ds; |
| 253 | } |
| 254 | |
| 255 | static void dsa_switch_destroy(struct dsa_switch *ds) |
| 256 | { |
| 257 | int port; |
| 258 | |
| 259 | /* Destroy network devices for physical switch ports. */ |
| 260 | for (port = 0; port < ds->num_ports; port++) { |
Vivien Didelot | 4a5b85f | 2017-10-26 11:22:55 -0400 | [diff] [blame^] | 261 | if (!dsa_is_user_port(ds, port)) |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 262 | continue; |
| 263 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 264 | if (!ds->ports[port].slave) |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 265 | continue; |
| 266 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 267 | dsa_slave_destroy(ds->ports[port].slave); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* Disable configuration of the CPU and DSA ports */ |
| 271 | for (port = 0; port < ds->num_ports; port++) { |
| 272 | if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) |
| 273 | continue; |
| 274 | dsa_cpu_dsa_destroy(&ds->ports[port]); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | if (ds->slave_mii_bus && ds->ops->phy_read) |
| 278 | mdiobus_unregister(ds->slave_mii_bus); |
| 279 | |
| 280 | dsa_switch_unregister_notifier(ds); |
| 281 | } |
| 282 | |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 283 | /* platform driver init and cleanup *****************************************/ |
| 284 | static int dev_is_class(struct device *dev, void *class) |
| 285 | { |
| 286 | if (dev->class != NULL && !strcmp(dev->class->name, class)) |
| 287 | return 1; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static struct device *dev_find_class(struct device *parent, char *class) |
| 293 | { |
| 294 | if (dev_is_class(parent, class)) { |
| 295 | get_device(parent); |
| 296 | return parent; |
| 297 | } |
| 298 | |
| 299 | return device_find_child(parent, class, dev_is_class); |
| 300 | } |
| 301 | |
| 302 | struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) |
| 303 | { |
| 304 | struct device *d; |
| 305 | |
| 306 | d = dev_find_class(dev, "mdio_bus"); |
| 307 | if (d != NULL) { |
| 308 | struct mii_bus *bus; |
| 309 | |
| 310 | bus = to_mii_bus(d); |
| 311 | put_device(d); |
| 312 | |
| 313 | return bus; |
| 314 | } |
| 315 | |
| 316 | return NULL; |
| 317 | } |
| 318 | EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); |
| 319 | |
| 320 | #ifdef CONFIG_OF |
| 321 | static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, |
| 322 | struct dsa_chip_data *cd, |
| 323 | int chip_index, int port_index, |
| 324 | struct device_node *link) |
| 325 | { |
| 326 | const __be32 *reg; |
| 327 | int link_sw_addr; |
| 328 | struct device_node *parent_sw; |
| 329 | int len; |
| 330 | |
| 331 | parent_sw = of_get_parent(link); |
| 332 | if (!parent_sw) |
| 333 | return -EINVAL; |
| 334 | |
| 335 | reg = of_get_property(parent_sw, "reg", &len); |
| 336 | if (!reg || (len != sizeof(*reg) * 2)) |
| 337 | return -EINVAL; |
| 338 | |
| 339 | /* |
| 340 | * Get the destination switch number from the second field of its 'reg' |
| 341 | * property, i.e. for "reg = <0x19 1>" sw_addr is '1'. |
| 342 | */ |
| 343 | link_sw_addr = be32_to_cpup(reg + 1); |
| 344 | |
| 345 | if (link_sw_addr >= pd->nr_chips) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | cd->rtable[link_sw_addr] = port_index; |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int dsa_of_probe_links(struct dsa_platform_data *pd, |
| 354 | struct dsa_chip_data *cd, |
| 355 | int chip_index, int port_index, |
| 356 | struct device_node *port, |
| 357 | const char *port_name) |
| 358 | { |
| 359 | struct device_node *link; |
| 360 | int link_index; |
| 361 | int ret; |
| 362 | |
| 363 | for (link_index = 0;; link_index++) { |
| 364 | link = of_parse_phandle(port, "link", link_index); |
| 365 | if (!link) |
| 366 | break; |
| 367 | |
| 368 | if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) { |
| 369 | ret = dsa_of_setup_routing_table(pd, cd, chip_index, |
| 370 | port_index, link); |
| 371 | if (ret) |
| 372 | return ret; |
| 373 | } |
| 374 | } |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static void dsa_of_free_platform_data(struct dsa_platform_data *pd) |
| 379 | { |
| 380 | int i; |
| 381 | int port_index; |
| 382 | |
| 383 | for (i = 0; i < pd->nr_chips; i++) { |
| 384 | port_index = 0; |
| 385 | while (port_index < DSA_MAX_PORTS) { |
| 386 | kfree(pd->chip[i].port_names[port_index]); |
| 387 | port_index++; |
| 388 | } |
| 389 | |
| 390 | /* Drop our reference to the MDIO bus device */ |
| 391 | if (pd->chip[i].host_dev) |
| 392 | put_device(pd->chip[i].host_dev); |
| 393 | } |
| 394 | kfree(pd->chip); |
| 395 | } |
| 396 | |
| 397 | static int dsa_of_probe(struct device *dev) |
| 398 | { |
| 399 | struct device_node *np = dev->of_node; |
| 400 | struct device_node *child, *mdio, *ethernet, *port; |
| 401 | struct mii_bus *mdio_bus, *mdio_bus_switch; |
| 402 | struct net_device *ethernet_dev; |
| 403 | struct dsa_platform_data *pd; |
| 404 | struct dsa_chip_data *cd; |
| 405 | const char *port_name; |
| 406 | int chip_index, port_index; |
| 407 | const unsigned int *sw_addr, *port_reg; |
| 408 | u32 eeprom_len; |
| 409 | int ret; |
| 410 | |
| 411 | mdio = of_parse_phandle(np, "dsa,mii-bus", 0); |
| 412 | if (!mdio) |
| 413 | return -EINVAL; |
| 414 | |
| 415 | mdio_bus = of_mdio_find_bus(mdio); |
| 416 | if (!mdio_bus) |
| 417 | return -EPROBE_DEFER; |
| 418 | |
| 419 | ethernet = of_parse_phandle(np, "dsa,ethernet", 0); |
| 420 | if (!ethernet) { |
| 421 | ret = -EINVAL; |
| 422 | goto out_put_mdio; |
| 423 | } |
| 424 | |
| 425 | ethernet_dev = of_find_net_device_by_node(ethernet); |
| 426 | if (!ethernet_dev) { |
| 427 | ret = -EPROBE_DEFER; |
| 428 | goto out_put_mdio; |
| 429 | } |
| 430 | |
| 431 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 432 | if (!pd) { |
| 433 | ret = -ENOMEM; |
| 434 | goto out_put_ethernet; |
| 435 | } |
| 436 | |
| 437 | dev->platform_data = pd; |
| 438 | pd->of_netdev = ethernet_dev; |
| 439 | pd->nr_chips = of_get_available_child_count(np); |
| 440 | if (pd->nr_chips > DSA_MAX_SWITCHES) |
| 441 | pd->nr_chips = DSA_MAX_SWITCHES; |
| 442 | |
| 443 | pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data), |
| 444 | GFP_KERNEL); |
| 445 | if (!pd->chip) { |
| 446 | ret = -ENOMEM; |
| 447 | goto out_free; |
| 448 | } |
| 449 | |
| 450 | chip_index = -1; |
| 451 | for_each_available_child_of_node(np, child) { |
| 452 | int i; |
| 453 | |
| 454 | chip_index++; |
| 455 | cd = &pd->chip[chip_index]; |
| 456 | |
| 457 | cd->of_node = child; |
| 458 | |
| 459 | /* Initialize the routing table */ |
| 460 | for (i = 0; i < DSA_MAX_SWITCHES; ++i) |
| 461 | cd->rtable[i] = DSA_RTABLE_NONE; |
| 462 | |
| 463 | /* When assigning the host device, increment its refcount */ |
| 464 | cd->host_dev = get_device(&mdio_bus->dev); |
| 465 | |
| 466 | sw_addr = of_get_property(child, "reg", NULL); |
| 467 | if (!sw_addr) |
| 468 | continue; |
| 469 | |
| 470 | cd->sw_addr = be32_to_cpup(sw_addr); |
| 471 | if (cd->sw_addr >= PHY_MAX_ADDR) |
| 472 | continue; |
| 473 | |
| 474 | if (!of_property_read_u32(child, "eeprom-length", &eeprom_len)) |
| 475 | cd->eeprom_len = eeprom_len; |
| 476 | |
| 477 | mdio = of_parse_phandle(child, "mii-bus", 0); |
| 478 | if (mdio) { |
| 479 | mdio_bus_switch = of_mdio_find_bus(mdio); |
| 480 | if (!mdio_bus_switch) { |
| 481 | ret = -EPROBE_DEFER; |
| 482 | goto out_free_chip; |
| 483 | } |
| 484 | |
| 485 | /* Drop the mdio_bus device ref, replacing the host |
| 486 | * device with the mdio_bus_switch device, keeping |
| 487 | * the refcount from of_mdio_find_bus() above. |
| 488 | */ |
| 489 | put_device(cd->host_dev); |
| 490 | cd->host_dev = &mdio_bus_switch->dev; |
| 491 | } |
| 492 | |
| 493 | for_each_available_child_of_node(child, port) { |
| 494 | port_reg = of_get_property(port, "reg", NULL); |
| 495 | if (!port_reg) |
| 496 | continue; |
| 497 | |
| 498 | port_index = be32_to_cpup(port_reg); |
| 499 | if (port_index >= DSA_MAX_PORTS) |
| 500 | break; |
| 501 | |
| 502 | port_name = of_get_property(port, "label", NULL); |
| 503 | if (!port_name) |
| 504 | continue; |
| 505 | |
| 506 | cd->port_dn[port_index] = port; |
| 507 | |
| 508 | cd->port_names[port_index] = kstrdup(port_name, |
| 509 | GFP_KERNEL); |
| 510 | if (!cd->port_names[port_index]) { |
| 511 | ret = -ENOMEM; |
| 512 | goto out_free_chip; |
| 513 | } |
| 514 | |
| 515 | ret = dsa_of_probe_links(pd, cd, chip_index, |
| 516 | port_index, port, port_name); |
| 517 | if (ret) |
| 518 | goto out_free_chip; |
| 519 | |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* The individual chips hold their own refcount on the mdio bus, |
| 524 | * so drop ours */ |
| 525 | put_device(&mdio_bus->dev); |
| 526 | |
| 527 | return 0; |
| 528 | |
| 529 | out_free_chip: |
| 530 | dsa_of_free_platform_data(pd); |
| 531 | out_free: |
| 532 | kfree(pd); |
| 533 | dev->platform_data = NULL; |
| 534 | out_put_ethernet: |
| 535 | put_device(ðernet_dev->dev); |
| 536 | out_put_mdio: |
| 537 | put_device(&mdio_bus->dev); |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | static void dsa_of_remove(struct device *dev) |
| 542 | { |
| 543 | struct dsa_platform_data *pd = dev->platform_data; |
| 544 | |
| 545 | if (!dev->of_node) |
| 546 | return; |
| 547 | |
| 548 | dsa_of_free_platform_data(pd); |
| 549 | put_device(&pd->of_netdev->dev); |
| 550 | kfree(pd); |
| 551 | } |
| 552 | #else |
| 553 | static inline int dsa_of_probe(struct device *dev) |
| 554 | { |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static inline void dsa_of_remove(struct device *dev) |
| 559 | { |
| 560 | } |
| 561 | #endif |
| 562 | |
| 563 | static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev, |
| 564 | struct device *parent, struct dsa_platform_data *pd) |
| 565 | { |
| 566 | int i; |
| 567 | unsigned configured = 0; |
| 568 | |
| 569 | dst->pd = pd; |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 570 | |
| 571 | for (i = 0; i < pd->nr_chips; i++) { |
| 572 | struct dsa_switch *ds; |
| 573 | |
Florian Fainelli | 06d4d45 | 2017-06-16 16:42:11 -0700 | [diff] [blame] | 574 | ds = dsa_switch_setup(dst, dev, i, parent, pd->chip[i].host_dev); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 575 | if (IS_ERR(ds)) { |
| 576 | netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", |
| 577 | i, PTR_ERR(ds)); |
| 578 | continue; |
| 579 | } |
| 580 | |
| 581 | dst->ds[i] = ds; |
| 582 | |
| 583 | ++configured; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * If no switch was found, exit cleanly |
| 588 | */ |
| 589 | if (!configured) |
| 590 | return -EPROBE_DEFER; |
| 591 | |
| 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(); |
Vivien Didelot | 2f657a6 | 2017-09-29 17:19:20 -0400 | [diff] [blame] | 598 | dev->dsa_ptr = dst->cpu_dp; |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 599 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 600 | return dsa_master_ethtool_setup(dst->cpu_dp->master); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static int dsa_probe(struct platform_device *pdev) |
| 604 | { |
| 605 | struct dsa_platform_data *pd = pdev->dev.platform_data; |
| 606 | struct net_device *dev; |
| 607 | struct dsa_switch_tree *dst; |
| 608 | int ret; |
| 609 | |
| 610 | if (pdev->dev.of_node) { |
| 611 | ret = dsa_of_probe(&pdev->dev); |
| 612 | if (ret) |
| 613 | return ret; |
| 614 | |
| 615 | pd = pdev->dev.platform_data; |
| 616 | } |
| 617 | |
| 618 | if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL)) |
| 619 | return -EINVAL; |
| 620 | |
| 621 | if (pd->of_netdev) { |
| 622 | dev = pd->of_netdev; |
| 623 | dev_hold(dev); |
| 624 | } else { |
| 625 | dev = dsa_dev_to_net_device(pd->netdev); |
| 626 | } |
| 627 | if (dev == NULL) { |
| 628 | ret = -EPROBE_DEFER; |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | if (dev->dsa_ptr != NULL) { |
| 633 | dev_put(dev); |
| 634 | ret = -EEXIST; |
| 635 | goto out; |
| 636 | } |
| 637 | |
| 638 | dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL); |
| 639 | if (dst == NULL) { |
| 640 | dev_put(dev); |
| 641 | ret = -ENOMEM; |
| 642 | goto out; |
| 643 | } |
| 644 | |
| 645 | platform_set_drvdata(pdev, dst); |
| 646 | |
| 647 | ret = dsa_setup_dst(dst, dev, &pdev->dev, pd); |
| 648 | if (ret) { |
| 649 | dev_put(dev); |
| 650 | goto out; |
| 651 | } |
| 652 | |
| 653 | return 0; |
| 654 | |
| 655 | out: |
| 656 | dsa_of_remove(&pdev->dev); |
| 657 | |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | static void dsa_remove_dst(struct dsa_switch_tree *dst) |
| 662 | { |
| 663 | int i; |
| 664 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 665 | dsa_master_ethtool_restore(dst->cpu_dp->master); |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 666 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 667 | dst->cpu_dp->master->dsa_ptr = NULL; |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 668 | |
| 669 | /* If we used a tagging format that doesn't have an ethertype |
| 670 | * field, make sure that all packets from this point get sent |
| 671 | * without the tag and go through the regular receive path. |
| 672 | */ |
| 673 | wmb(); |
| 674 | |
| 675 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 676 | struct dsa_switch *ds = dst->ds[i]; |
| 677 | |
| 678 | if (ds) |
| 679 | dsa_switch_destroy(ds); |
| 680 | } |
| 681 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 682 | dev_put(dst->cpu_dp->master); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static int dsa_remove(struct platform_device *pdev) |
| 686 | { |
| 687 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 688 | |
| 689 | dsa_remove_dst(dst); |
| 690 | dsa_of_remove(&pdev->dev); |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | static void dsa_shutdown(struct platform_device *pdev) |
| 696 | { |
| 697 | } |
| 698 | |
| 699 | #ifdef CONFIG_PM_SLEEP |
| 700 | static int dsa_suspend(struct device *d) |
| 701 | { |
| 702 | struct platform_device *pdev = to_platform_device(d); |
| 703 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 704 | int i, ret = 0; |
| 705 | |
| 706 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 707 | struct dsa_switch *ds = dst->ds[i]; |
| 708 | |
| 709 | if (ds != NULL) |
| 710 | ret = dsa_switch_suspend(ds); |
| 711 | } |
| 712 | |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | static int dsa_resume(struct device *d) |
| 717 | { |
| 718 | struct platform_device *pdev = to_platform_device(d); |
| 719 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 720 | int i, ret = 0; |
| 721 | |
| 722 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 723 | struct dsa_switch *ds = dst->ds[i]; |
| 724 | |
| 725 | if (ds != NULL) |
| 726 | ret = dsa_switch_resume(ds); |
| 727 | } |
| 728 | |
| 729 | return ret; |
| 730 | } |
| 731 | #endif |
| 732 | |
Arkadi Sharshevsky | 37b8da1 | 2017-08-06 16:15:43 +0300 | [diff] [blame] | 733 | /* legacy way, bypassing the bridge *****************************************/ |
| 734 | int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], |
| 735 | struct net_device *dev, |
| 736 | const unsigned char *addr, u16 vid, |
| 737 | u16 flags) |
| 738 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 739 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Arkadi Sharshevsky | 37b8da1 | 2017-08-06 16:15:43 +0300 | [diff] [blame] | 740 | |
| 741 | return dsa_port_fdb_add(dp, addr, vid); |
| 742 | } |
| 743 | |
| 744 | int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], |
| 745 | struct net_device *dev, |
| 746 | const unsigned char *addr, u16 vid) |
| 747 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 748 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Arkadi Sharshevsky | 37b8da1 | 2017-08-06 16:15:43 +0300 | [diff] [blame] | 749 | |
| 750 | return dsa_port_fdb_del(dp, addr, vid); |
| 751 | } |
| 752 | |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 753 | static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); |
| 754 | |
| 755 | static const struct of_device_id dsa_of_match_table[] = { |
| 756 | { .compatible = "marvell,dsa", }, |
| 757 | {} |
| 758 | }; |
| 759 | MODULE_DEVICE_TABLE(of, dsa_of_match_table); |
| 760 | |
| 761 | static struct platform_driver dsa_driver = { |
| 762 | .probe = dsa_probe, |
| 763 | .remove = dsa_remove, |
| 764 | .shutdown = dsa_shutdown, |
| 765 | .driver = { |
| 766 | .name = "dsa", |
| 767 | .of_match_table = dsa_of_match_table, |
| 768 | .pm = &dsa_pm_ops, |
| 769 | }, |
| 770 | }; |
| 771 | |
| 772 | int dsa_legacy_register(void) |
| 773 | { |
| 774 | return platform_driver_register(&dsa_driver); |
| 775 | } |
| 776 | |
| 777 | void dsa_legacy_unregister(void) |
| 778 | { |
| 779 | platform_driver_unregister(&dsa_driver); |
| 780 | } |