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