Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/dsa2.c - Hardware switch handling, binding version 2 |
| 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
| 4 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> |
| 5 | * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/list.h> |
Andrew Lunn | c6e970a | 2017-03-28 23:45:06 +0200 | [diff] [blame] | 16 | #include <linux/netdevice.h> |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/rtnetlink.h> |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 19 | #include <linux/of.h> |
| 20 | #include <linux/of_net.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 21 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 22 | #include "dsa_priv.h" |
| 23 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 24 | static LIST_HEAD(dsa_tree_list); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 25 | static DEFINE_MUTEX(dsa2_mutex); |
| 26 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 27 | static const struct devlink_ops dsa_devlink_ops = { |
| 28 | }; |
| 29 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 30 | static struct dsa_switch_tree *dsa_tree_find(int index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 31 | { |
| 32 | struct dsa_switch_tree *dst; |
| 33 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 34 | list_for_each_entry(dst, &dsa_tree_list, list) |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 35 | if (dst->index == index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 36 | return dst; |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 37 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 38 | return NULL; |
| 39 | } |
| 40 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 41 | static struct dsa_switch_tree *dsa_tree_alloc(int index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 42 | { |
| 43 | struct dsa_switch_tree *dst; |
| 44 | |
| 45 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 46 | if (!dst) |
| 47 | return NULL; |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 48 | |
Vivien Didelot | 49463b7 | 2017-11-03 19:05:21 -0400 | [diff] [blame] | 49 | dst->index = index; |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 50 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 51 | INIT_LIST_HEAD(&dst->list); |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 52 | list_add_tail(&dsa_tree_list, &dst->list); |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 53 | |
| 54 | /* Initialize the reference counter to the number of switches, not 1 */ |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 55 | kref_init(&dst->refcount); |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 56 | refcount_set(&dst->refcount.refcount, 0); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 57 | |
| 58 | return dst; |
| 59 | } |
| 60 | |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 61 | static void dsa_tree_free(struct dsa_switch_tree *dst) |
| 62 | { |
| 63 | list_del(&dst->list); |
| 64 | kfree(dst); |
| 65 | } |
| 66 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 67 | static struct dsa_switch_tree *dsa_tree_touch(int index) |
| 68 | { |
| 69 | struct dsa_switch_tree *dst; |
| 70 | |
| 71 | dst = dsa_tree_find(index); |
| 72 | if (!dst) |
| 73 | dst = dsa_tree_alloc(index); |
| 74 | |
| 75 | return dst; |
| 76 | } |
| 77 | |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 78 | static void dsa_tree_get(struct dsa_switch_tree *dst) |
| 79 | { |
| 80 | kref_get(&dst->refcount); |
| 81 | } |
| 82 | |
| 83 | static void dsa_tree_release(struct kref *ref) |
| 84 | { |
| 85 | struct dsa_switch_tree *dst; |
| 86 | |
| 87 | dst = container_of(ref, struct dsa_switch_tree, refcount); |
| 88 | |
| 89 | dsa_tree_free(dst); |
| 90 | } |
| 91 | |
| 92 | static void dsa_tree_put(struct dsa_switch_tree *dst) |
| 93 | { |
| 94 | kref_put(&dst->refcount, dsa_tree_release); |
| 95 | } |
| 96 | |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 97 | /* For platform data configurations, we need to have a valid name argument to |
| 98 | * differentiate a disabled port from an enabled one |
| 99 | */ |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 100 | static bool dsa_port_is_valid(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 101 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 102 | return port->type != DSA_PORT_TYPE_UNUSED; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 105 | static bool dsa_port_is_dsa(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 106 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 107 | return port->type == DSA_PORT_TYPE_DSA; |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static bool dsa_port_is_cpu(struct dsa_port *port) |
| 111 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 112 | return port->type == DSA_PORT_TYPE_CPU; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame^] | 115 | static bool dsa_port_is_user(struct dsa_port *dp) |
| 116 | { |
| 117 | return dp->type == DSA_PORT_TYPE_USER; |
| 118 | } |
| 119 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 120 | static bool dsa_ds_find_port_dn(struct dsa_switch *ds, |
| 121 | struct device_node *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 122 | { |
| 123 | u32 index; |
| 124 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 125 | for (index = 0; index < ds->num_ports; index++) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 126 | if (ds->ports[index].dn == port) |
| 127 | return true; |
| 128 | return false; |
| 129 | } |
| 130 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 131 | static struct dsa_switch *dsa_dst_find_port_dn(struct dsa_switch_tree *dst, |
| 132 | struct device_node *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 133 | { |
| 134 | struct dsa_switch *ds; |
| 135 | u32 index; |
| 136 | |
| 137 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 138 | ds = dst->ds[index]; |
| 139 | if (!ds) |
| 140 | continue; |
| 141 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 142 | if (dsa_ds_find_port_dn(ds, port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 143 | return ds; |
| 144 | } |
| 145 | |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | static int dsa_port_complete(struct dsa_switch_tree *dst, |
| 150 | struct dsa_switch *src_ds, |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 151 | struct dsa_port *port, |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 152 | u32 src_port) |
| 153 | { |
| 154 | struct device_node *link; |
| 155 | int index; |
| 156 | struct dsa_switch *dst_ds; |
| 157 | |
| 158 | for (index = 0;; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 159 | link = of_parse_phandle(port->dn, "link", index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 160 | if (!link) |
| 161 | break; |
| 162 | |
Florian Fainelli | 3512a8e | 2017-01-26 10:45:53 -0800 | [diff] [blame] | 163 | dst_ds = dsa_dst_find_port_dn(dst, link); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 164 | of_node_put(link); |
| 165 | |
| 166 | if (!dst_ds) |
| 167 | return 1; |
| 168 | |
| 169 | src_ds->rtable[dst_ds->index] = src_port; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* A switch is complete if all the DSA ports phandles point to ports |
| 176 | * known in the tree. A return value of 1 means the tree is not |
| 177 | * complete. This is not an error condition. A value of 0 is |
| 178 | * success. |
| 179 | */ |
| 180 | static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 181 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 182 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 183 | u32 index; |
| 184 | int err; |
| 185 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 186 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 187 | port = &ds->ports[index]; |
| 188 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 189 | continue; |
| 190 | |
| 191 | if (!dsa_port_is_dsa(port)) |
| 192 | continue; |
| 193 | |
| 194 | err = dsa_port_complete(dst, ds, port, index); |
| 195 | if (err != 0) |
| 196 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* A tree is complete if all the DSA ports phandles point to ports |
| 203 | * known in the tree. A return value of 1 means the tree is not |
| 204 | * complete. This is not an error condition. A value of 0 is |
| 205 | * success. |
| 206 | */ |
| 207 | static int dsa_dst_complete(struct dsa_switch_tree *dst) |
| 208 | { |
| 209 | struct dsa_switch *ds; |
| 210 | u32 index; |
| 211 | int err; |
| 212 | |
| 213 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 214 | ds = dst->ds[index]; |
| 215 | if (!ds) |
| 216 | continue; |
| 217 | |
| 218 | err = dsa_ds_complete(dst, ds); |
| 219 | if (err != 0) |
| 220 | return err; |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame^] | 226 | static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst) |
| 227 | { |
| 228 | struct dsa_switch *ds; |
| 229 | struct dsa_port *dp; |
| 230 | int device, port; |
| 231 | |
| 232 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { |
| 233 | ds = dst->ds[device]; |
| 234 | if (!ds) |
| 235 | continue; |
| 236 | |
| 237 | for (port = 0; port < ds->num_ports; port++) { |
| 238 | dp = &ds->ports[port]; |
| 239 | |
| 240 | if (dsa_port_is_cpu(dp)) |
| 241 | return dp; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst) |
| 249 | { |
| 250 | struct dsa_switch *ds; |
| 251 | struct dsa_port *dp; |
| 252 | int device, port; |
| 253 | |
| 254 | /* DSA currently only supports a single CPU port */ |
| 255 | dst->cpu_dp = dsa_tree_find_first_cpu(dst); |
| 256 | if (!dst->cpu_dp) { |
| 257 | pr_warn("Tree has no master device\n"); |
| 258 | return -EINVAL; |
| 259 | } |
| 260 | |
| 261 | /* Assign the default CPU port to all ports of the fabric */ |
| 262 | for (device = 0; device < DSA_MAX_SWITCHES; device++) { |
| 263 | ds = dst->ds[device]; |
| 264 | if (!ds) |
| 265 | continue; |
| 266 | |
| 267 | for (port = 0; port < ds->num_ports; port++) { |
| 268 | dp = &ds->ports[port]; |
| 269 | |
| 270 | if (dsa_port_is_user(dp)) |
| 271 | dp->cpu_dp = dst->cpu_dp; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst) |
| 279 | { |
| 280 | /* DSA currently only supports a single CPU port */ |
| 281 | dst->cpu_dp = NULL; |
| 282 | } |
| 283 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 284 | static int dsa_dsa_port_apply(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 285 | { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 286 | struct dsa_switch *ds = port->ds; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 287 | int err; |
| 288 | |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 289 | err = dsa_port_fixed_link_register_of(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 290 | if (err) { |
| 291 | dev_warn(ds->dev, "Failed to setup dsa port %d: %d\n", |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 292 | port->index, err); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 293 | return err; |
| 294 | } |
| 295 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 296 | memset(&port->devlink_port, 0, sizeof(port->devlink_port)); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 297 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 298 | return devlink_port_register(ds->devlink, &port->devlink_port, |
| 299 | port->index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 302 | static void dsa_dsa_port_unapply(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 303 | { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 304 | devlink_port_unregister(&port->devlink_port); |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 305 | dsa_port_fixed_link_unregister_of(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 308 | static int dsa_cpu_port_apply(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 309 | { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 310 | struct dsa_switch *ds = port->ds; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 311 | int err; |
| 312 | |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 313 | err = dsa_port_fixed_link_register_of(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 314 | if (err) { |
| 315 | dev_warn(ds->dev, "Failed to setup cpu port %d: %d\n", |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 316 | port->index, err); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 317 | return err; |
| 318 | } |
| 319 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 320 | memset(&port->devlink_port, 0, sizeof(port->devlink_port)); |
| 321 | err = devlink_port_register(ds->devlink, &port->devlink_port, |
| 322 | port->index); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 323 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 326 | static void dsa_cpu_port_unapply(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 327 | { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 328 | devlink_port_unregister(&port->devlink_port); |
Vivien Didelot | 57ab1ca | 2017-10-26 10:50:07 -0400 | [diff] [blame] | 329 | dsa_port_fixed_link_unregister_of(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 332 | static int dsa_user_port_apply(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 333 | { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 334 | struct dsa_switch *ds = port->ds; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 335 | int err; |
| 336 | |
Vivien Didelot | 951259aa | 2017-10-27 15:55:19 -0400 | [diff] [blame] | 337 | err = dsa_slave_create(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 338 | if (err) { |
| 339 | dev_warn(ds->dev, "Failed to create slave %d: %d\n", |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 340 | port->index, err); |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 341 | port->slave = NULL; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 342 | return err; |
| 343 | } |
| 344 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 345 | memset(&port->devlink_port, 0, sizeof(port->devlink_port)); |
| 346 | err = devlink_port_register(ds->devlink, &port->devlink_port, |
| 347 | port->index); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 348 | if (err) |
| 349 | return err; |
| 350 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 351 | devlink_port_type_eth_set(&port->devlink_port, port->slave); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 352 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 356 | static void dsa_user_port_unapply(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 357 | { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 358 | devlink_port_unregister(&port->devlink_port); |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 359 | if (port->slave) { |
| 360 | dsa_slave_destroy(port->slave); |
| 361 | port->slave = NULL; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 366 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 367 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 368 | u32 index; |
| 369 | int err; |
| 370 | |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 371 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 372 | * driver and before ops->setup() has run, since the switch drivers and |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 373 | * the slave MDIO bus driver rely on these values for probing PHY |
| 374 | * devices or not |
| 375 | */ |
Vivien Didelot | 02bc6e5 | 2017-10-26 11:22:56 -0400 | [diff] [blame] | 376 | ds->phys_mii_mask |= dsa_user_ports(ds); |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 377 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 378 | /* Add the switch to devlink before calling setup, so that setup can |
| 379 | * add dpipe tables |
| 380 | */ |
| 381 | ds->devlink = devlink_alloc(&dsa_devlink_ops, 0); |
| 382 | if (!ds->devlink) |
| 383 | return -ENOMEM; |
| 384 | |
| 385 | err = devlink_register(ds->devlink, ds->dev); |
| 386 | if (err) |
| 387 | return err; |
| 388 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 389 | err = ds->ops->setup(ds); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 390 | if (err < 0) |
| 391 | return err; |
| 392 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 393 | err = dsa_switch_register_notifier(ds); |
| 394 | if (err) |
| 395 | return err; |
| 396 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 397 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 398 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
| 399 | if (!ds->slave_mii_bus) |
| 400 | return -ENOMEM; |
| 401 | |
| 402 | dsa_slave_mii_bus_init(ds); |
| 403 | |
| 404 | err = mdiobus_register(ds->slave_mii_bus); |
| 405 | if (err < 0) |
| 406 | return err; |
| 407 | } |
| 408 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 409 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 410 | port = &ds->ports[index]; |
| 411 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 412 | continue; |
| 413 | |
| 414 | if (dsa_port_is_dsa(port)) { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 415 | err = dsa_dsa_port_apply(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 416 | if (err) |
| 417 | return err; |
| 418 | continue; |
| 419 | } |
| 420 | |
| 421 | if (dsa_port_is_cpu(port)) { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 422 | err = dsa_cpu_port_apply(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 423 | if (err) |
| 424 | return err; |
| 425 | continue; |
| 426 | } |
| 427 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 428 | err = dsa_user_port_apply(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 429 | if (err) |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds) |
| 437 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 438 | struct dsa_port *port; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 439 | u32 index; |
| 440 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 441 | for (index = 0; index < ds->num_ports; index++) { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 442 | port = &ds->ports[index]; |
| 443 | if (!dsa_port_is_valid(port)) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 444 | continue; |
| 445 | |
| 446 | if (dsa_port_is_dsa(port)) { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 447 | dsa_dsa_port_unapply(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 448 | continue; |
| 449 | } |
| 450 | |
| 451 | if (dsa_port_is_cpu(port)) { |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 452 | dsa_cpu_port_unapply(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 453 | continue; |
| 454 | } |
| 455 | |
Florian Fainelli | e41c1b5 | 2017-06-02 12:31:22 -0700 | [diff] [blame] | 456 | dsa_user_port_unapply(port); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 457 | } |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 458 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 459 | if (ds->slave_mii_bus && ds->ops->phy_read) |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 460 | mdiobus_unregister(ds->slave_mii_bus); |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 461 | |
| 462 | dsa_switch_unregister_notifier(ds); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 463 | |
| 464 | if (ds->devlink) { |
| 465 | devlink_unregister(ds->devlink); |
| 466 | devlink_free(ds->devlink); |
| 467 | ds->devlink = NULL; |
| 468 | } |
| 469 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static int dsa_dst_apply(struct dsa_switch_tree *dst) |
| 473 | { |
| 474 | struct dsa_switch *ds; |
| 475 | u32 index; |
| 476 | int err; |
| 477 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame^] | 478 | err = dsa_tree_setup_default_cpu(dst); |
| 479 | if (err) |
| 480 | return err; |
| 481 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 482 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 483 | ds = dst->ds[index]; |
| 484 | if (!ds) |
| 485 | continue; |
| 486 | |
| 487 | err = dsa_ds_apply(dst, ds); |
| 488 | if (err) |
| 489 | return err; |
| 490 | } |
| 491 | |
| 492 | /* If we use a tagging format that doesn't have an ethertype |
| 493 | * field, make sure that all packets from this point on get |
| 494 | * sent to the tag format's receive function. |
| 495 | */ |
| 496 | wmb(); |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 497 | dst->cpu_dp->master->dsa_ptr = dst->cpu_dp; |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 498 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 499 | err = dsa_master_ethtool_setup(dst->cpu_dp->master); |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 500 | if (err) |
| 501 | return err; |
| 502 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 503 | dst->applied = true; |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static void dsa_dst_unapply(struct dsa_switch_tree *dst) |
| 509 | { |
| 510 | struct dsa_switch *ds; |
| 511 | u32 index; |
| 512 | |
| 513 | if (!dst->applied) |
| 514 | return; |
| 515 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 516 | dsa_master_ethtool_restore(dst->cpu_dp->master); |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 517 | |
Vivien Didelot | f8b8b1c | 2017-10-16 11:12:18 -0400 | [diff] [blame] | 518 | dst->cpu_dp->master->dsa_ptr = NULL; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 519 | |
| 520 | /* If we used a tagging format that doesn't have an ethertype |
| 521 | * field, make sure that all packets from this point get sent |
| 522 | * without the tag and go through the regular receive path. |
| 523 | */ |
| 524 | wmb(); |
| 525 | |
| 526 | for (index = 0; index < DSA_MAX_SWITCHES; index++) { |
| 527 | ds = dst->ds[index]; |
| 528 | if (!ds) |
| 529 | continue; |
| 530 | |
| 531 | dsa_ds_unapply(dst, ds); |
| 532 | } |
| 533 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame^] | 534 | dsa_tree_teardown_default_cpu(dst); |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 535 | |
Vivien Didelot | 49463b7 | 2017-11-03 19:05:21 -0400 | [diff] [blame] | 536 | pr_info("DSA: tree %d unapplied\n", dst->index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 537 | dst->applied = false; |
| 538 | } |
| 539 | |
Vivien Didelot | 6da2a94 | 2017-11-03 19:05:25 -0400 | [diff] [blame] | 540 | static void dsa_tree_remove_switch(struct dsa_switch_tree *dst, |
| 541 | unsigned int index) |
| 542 | { |
| 543 | dst->ds[index] = NULL; |
| 544 | dsa_tree_put(dst); |
| 545 | } |
| 546 | |
| 547 | static int dsa_tree_add_switch(struct dsa_switch_tree *dst, |
| 548 | struct dsa_switch *ds) |
| 549 | { |
| 550 | unsigned int index = ds->index; |
| 551 | |
| 552 | if (dst->ds[index]) |
| 553 | return -EBUSY; |
| 554 | |
| 555 | dsa_tree_get(dst); |
| 556 | dst->ds[index] = ds; |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 561 | static int dsa_port_parse_user(struct dsa_port *dp, const char *name) |
| 562 | { |
| 563 | if (!name) |
| 564 | name = "eth%d"; |
| 565 | |
| 566 | dp->type = DSA_PORT_TYPE_USER; |
| 567 | dp->name = name; |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static int dsa_port_parse_dsa(struct dsa_port *dp) |
| 573 | { |
| 574 | dp->type = DSA_PORT_TYPE_DSA; |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master) |
| 580 | { |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 581 | struct dsa_switch *ds = dp->ds; |
| 582 | struct dsa_switch_tree *dst = ds->dst; |
Vivien Didelot | 62fc958 | 2017-09-29 17:19:17 -0400 | [diff] [blame] | 583 | const struct dsa_device_ops *tag_ops; |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 584 | enum dsa_tag_protocol tag_protocol; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 585 | |
Florian Fainelli | 9f9e772 | 2017-07-24 10:49:23 -0700 | [diff] [blame] | 586 | tag_protocol = ds->ops->get_tag_protocol(ds); |
Vivien Didelot | 62fc958 | 2017-09-29 17:19:17 -0400 | [diff] [blame] | 587 | tag_ops = dsa_resolve_tag_protocol(tag_protocol); |
| 588 | if (IS_ERR(tag_ops)) { |
Florian Fainelli | 9f9e772 | 2017-07-24 10:49:23 -0700 | [diff] [blame] | 589 | dev_warn(ds->dev, "No tagger for this switch\n"); |
Vivien Didelot | 62fc958 | 2017-09-29 17:19:17 -0400 | [diff] [blame] | 590 | return PTR_ERR(tag_ops); |
Florian Fainelli | 9f9e772 | 2017-07-24 10:49:23 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 593 | dp->type = DSA_PORT_TYPE_CPU; |
| 594 | dp->rcv = tag_ops->rcv; |
| 595 | dp->tag_ops = tag_ops; |
| 596 | dp->master = master; |
| 597 | dp->dst = dst; |
Vivien Didelot | 3e41f93 | 2017-09-29 17:19:19 -0400 | [diff] [blame] | 598 | |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 602 | static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn) |
| 603 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 604 | struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0); |
Vivien Didelot | 1838fa8 | 2017-10-27 15:55:18 -0400 | [diff] [blame] | 605 | const char *name = of_get_property(dn, "label", NULL); |
Vivien Didelot | 54df6fa | 2017-11-03 19:05:28 -0400 | [diff] [blame] | 606 | bool link = of_property_read_bool(dn, "link"); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 607 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 608 | dp->dn = dn; |
| 609 | |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 610 | if (ethernet) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 611 | struct net_device *master; |
| 612 | |
| 613 | master = of_find_net_device_by_node(ethernet); |
| 614 | if (!master) |
| 615 | return -EPROBE_DEFER; |
| 616 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 617 | return dsa_port_parse_cpu(dp, master); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 618 | } |
| 619 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 620 | if (link) |
| 621 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 622 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 623 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 624 | } |
| 625 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 626 | static int dsa_switch_parse_ports_of(struct dsa_switch *ds, |
| 627 | struct device_node *dn) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 628 | { |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 629 | struct device_node *ports, *port; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 630 | struct dsa_port *dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 631 | u32 reg; |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 632 | int err; |
| 633 | |
| 634 | ports = of_get_child_by_name(dn, "ports"); |
| 635 | if (!ports) { |
| 636 | dev_err(ds->dev, "no ports child node found\n"); |
| 637 | return -EINVAL; |
| 638 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 639 | |
| 640 | for_each_available_child_of_node(ports, port) { |
| 641 | err = of_property_read_u32(port, "reg", ®); |
| 642 | if (err) |
| 643 | return err; |
| 644 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 645 | if (reg >= ds->num_ports) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 646 | return -EINVAL; |
| 647 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 648 | dp = &ds->ports[reg]; |
| 649 | |
| 650 | err = dsa_port_parse_of(dp, port); |
| 651 | if (err) |
| 652 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 658 | static int dsa_switch_parse_member_of(struct dsa_switch *ds, |
| 659 | struct device_node *dn) |
| 660 | { |
| 661 | u32 m[2] = { 0, 0 }; |
| 662 | int sz; |
| 663 | |
| 664 | /* Don't error out if this optional property isn't found */ |
| 665 | sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2); |
| 666 | if (sz < 0 && sz != -EINVAL) |
| 667 | return sz; |
| 668 | |
| 669 | ds->index = m[1]; |
| 670 | if (ds->index >= DSA_MAX_SWITCHES) |
| 671 | return -EINVAL; |
| 672 | |
| 673 | ds->dst = dsa_tree_touch(m[0]); |
| 674 | if (!ds->dst) |
| 675 | return -ENOMEM; |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn) |
| 681 | { |
| 682 | int err; |
| 683 | |
| 684 | err = dsa_switch_parse_member_of(ds, dn); |
| 685 | if (err) |
| 686 | return err; |
| 687 | |
| 688 | return dsa_switch_parse_ports_of(ds, dn); |
| 689 | } |
| 690 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 691 | static int dsa_port_parse(struct dsa_port *dp, const char *name, |
| 692 | struct device *dev) |
| 693 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 694 | if (!strcmp(name, "cpu")) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 695 | struct net_device *master; |
| 696 | |
| 697 | master = dsa_dev_to_net_device(dev); |
| 698 | if (!master) |
| 699 | return -EPROBE_DEFER; |
| 700 | |
| 701 | dev_put(master); |
| 702 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 703 | return dsa_port_parse_cpu(dp, master); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 704 | } |
| 705 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 706 | if (!strcmp(name, "dsa")) |
| 707 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 708 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 709 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 710 | } |
| 711 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 712 | static int dsa_switch_parse_ports(struct dsa_switch *ds, |
| 713 | struct dsa_chip_data *cd) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 714 | { |
| 715 | bool valid_name_found = false; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 716 | struct dsa_port *dp; |
| 717 | struct device *dev; |
| 718 | const char *name; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 719 | unsigned int i; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 720 | int err; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 721 | |
| 722 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 723 | name = cd->port_names[i]; |
| 724 | dev = cd->netdev[i]; |
| 725 | dp = &ds->ports[i]; |
| 726 | |
| 727 | if (!name) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 728 | continue; |
| 729 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 730 | err = dsa_port_parse(dp, name, dev); |
| 731 | if (err) |
| 732 | return err; |
| 733 | |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 734 | valid_name_found = true; |
| 735 | } |
| 736 | |
| 737 | if (!valid_name_found && i == DSA_MAX_PORTS) |
| 738 | return -EINVAL; |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 743 | static int dsa_switch_parse(struct dsa_switch *ds, struct dsa_chip_data *cd) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 744 | { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 745 | ds->cd = cd; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 746 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 747 | /* We don't support interconnected switches nor multiple trees via |
| 748 | * platform data, so this is the unique switch of the tree. |
| 749 | */ |
| 750 | ds->index = 0; |
| 751 | ds->dst = dsa_tree_touch(0); |
| 752 | if (!ds->dst) |
| 753 | return -ENOMEM; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 754 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 755 | return dsa_switch_parse_ports(ds, cd); |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 758 | static int _dsa_register_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 759 | { |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 760 | struct dsa_chip_data *pdata = ds->dev->platform_data; |
| 761 | struct device_node *np = ds->dev->of_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 762 | struct dsa_switch_tree *dst; |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 763 | unsigned int index; |
Vivien Didelot | d390238 | 2016-07-06 20:03:54 -0400 | [diff] [blame] | 764 | int i, err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 765 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 766 | if (np) |
| 767 | err = dsa_switch_parse_of(ds, np); |
| 768 | else if (pdata) |
| 769 | err = dsa_switch_parse(ds, pdata); |
| 770 | else |
| 771 | err = -ENODEV; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 772 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 773 | if (err) |
| 774 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 775 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 776 | index = ds->index; |
| 777 | dst = ds->dst; |
Vivien Didelot | 0eefe2c | 2017-11-03 19:05:26 -0400 | [diff] [blame] | 778 | |
Vivien Didelot | d390238 | 2016-07-06 20:03:54 -0400 | [diff] [blame] | 779 | /* Initialize the routing table */ |
| 780 | for (i = 0; i < DSA_MAX_SWITCHES; ++i) |
| 781 | ds->rtable[i] = DSA_RTABLE_NONE; |
| 782 | |
Vivien Didelot | 6da2a94 | 2017-11-03 19:05:25 -0400 | [diff] [blame] | 783 | err = dsa_tree_add_switch(dst, ds); |
| 784 | if (err) |
| 785 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 786 | |
| 787 | err = dsa_dst_complete(dst); |
| 788 | if (err < 0) |
| 789 | goto out_del_dst; |
| 790 | |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 791 | /* Not all switches registered yet */ |
| 792 | if (err == 1) |
| 793 | return 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 794 | |
| 795 | if (dst->applied) { |
| 796 | pr_info("DSA: Disjoint trees?\n"); |
| 797 | return -EINVAL; |
| 798 | } |
| 799 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 800 | err = dsa_dst_apply(dst); |
| 801 | if (err) { |
| 802 | dsa_dst_unapply(dst); |
| 803 | goto out_del_dst; |
| 804 | } |
| 805 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 806 | return 0; |
| 807 | |
| 808 | out_del_dst: |
Vivien Didelot | 6da2a94 | 2017-11-03 19:05:25 -0400 | [diff] [blame] | 809 | dsa_tree_remove_switch(dst, index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 810 | |
| 811 | return err; |
| 812 | } |
| 813 | |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 814 | struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n) |
| 815 | { |
| 816 | size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port); |
| 817 | struct dsa_switch *ds; |
Vivien Didelot | 818be84 | 2017-01-27 15:29:38 -0500 | [diff] [blame] | 818 | int i; |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 819 | |
| 820 | ds = devm_kzalloc(dev, size, GFP_KERNEL); |
| 821 | if (!ds) |
| 822 | return NULL; |
| 823 | |
| 824 | ds->dev = dev; |
| 825 | ds->num_ports = n; |
| 826 | |
Vivien Didelot | 818be84 | 2017-01-27 15:29:38 -0500 | [diff] [blame] | 827 | for (i = 0; i < ds->num_ports; ++i) { |
| 828 | ds->ports[i].index = i; |
| 829 | ds->ports[i].ds = ds; |
| 830 | } |
| 831 | |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 832 | return ds; |
| 833 | } |
| 834 | EXPORT_SYMBOL_GPL(dsa_switch_alloc); |
| 835 | |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 836 | int dsa_register_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 837 | { |
| 838 | int err; |
| 839 | |
| 840 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 841 | err = _dsa_register_switch(ds); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 842 | mutex_unlock(&dsa2_mutex); |
| 843 | |
| 844 | return err; |
| 845 | } |
| 846 | EXPORT_SYMBOL_GPL(dsa_register_switch); |
| 847 | |
Wei Yongjun | 85c22ba | 2016-07-12 15:24:10 +0000 | [diff] [blame] | 848 | static void _dsa_unregister_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 849 | { |
| 850 | struct dsa_switch_tree *dst = ds->dst; |
Vivien Didelot | 6da2a94 | 2017-11-03 19:05:25 -0400 | [diff] [blame] | 851 | unsigned int index = ds->index; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 852 | |
| 853 | dsa_dst_unapply(dst); |
| 854 | |
Vivien Didelot | 6da2a94 | 2017-11-03 19:05:25 -0400 | [diff] [blame] | 855 | dsa_tree_remove_switch(dst, index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | void dsa_unregister_switch(struct dsa_switch *ds) |
| 859 | { |
| 860 | mutex_lock(&dsa2_mutex); |
| 861 | _dsa_unregister_switch(ds); |
| 862 | mutex_unlock(&dsa2_mutex); |
| 863 | } |
| 864 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |