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