Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 2 | /* |
| 3 | * net/dsa/dsa2.c - Hardware switch handling, binding version 2 |
| 4 | * Copyright (c) 2008-2009 Marvell Semiconductor |
| 5 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> |
| 6 | * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch> |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/list.h> |
Andrew Lunn | c6e970a | 2017-03-28 23:45:06 +0200 | [diff] [blame] | 12 | #include <linux/netdevice.h> |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 13 | #include <linux/slab.h> |
| 14 | #include <linux/rtnetlink.h> |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_net.h> |
Jiri Pirko | 402f99e5 | 2019-03-24 11:14:26 +0100 | [diff] [blame] | 17 | #include <net/devlink.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 18 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 19 | #include "dsa_priv.h" |
| 20 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 21 | static DEFINE_MUTEX(dsa2_mutex); |
Vladimir Oltean | bff33f7 | 2020-03-27 21:55:43 +0200 | [diff] [blame] | 22 | LIST_HEAD(dsa_tree_list); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 23 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 24 | static const struct devlink_ops dsa_devlink_ops = { |
| 25 | }; |
| 26 | |
Vladimir Oltean | 3b7bc1f | 2020-05-10 19:37:42 +0300 | [diff] [blame] | 27 | struct dsa_switch *dsa_switch_find(int tree_index, int sw_index) |
| 28 | { |
| 29 | struct dsa_switch_tree *dst; |
| 30 | struct dsa_port *dp; |
| 31 | |
| 32 | list_for_each_entry(dst, &dsa_tree_list, list) { |
| 33 | if (dst->index != tree_index) |
| 34 | continue; |
| 35 | |
| 36 | list_for_each_entry(dp, &dst->ports, list) { |
| 37 | if (dp->ds->index != sw_index) |
| 38 | continue; |
| 39 | |
| 40 | return dp->ds; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return NULL; |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(dsa_switch_find); |
| 47 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 48 | static struct dsa_switch_tree *dsa_tree_find(int index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 49 | { |
| 50 | struct dsa_switch_tree *dst; |
| 51 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 52 | list_for_each_entry(dst, &dsa_tree_list, list) |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 53 | if (dst->index == index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 54 | return dst; |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 55 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 56 | return NULL; |
| 57 | } |
| 58 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 59 | static struct dsa_switch_tree *dsa_tree_alloc(int index) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 60 | { |
| 61 | struct dsa_switch_tree *dst; |
| 62 | |
| 63 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 64 | if (!dst) |
| 65 | return NULL; |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 66 | |
Vivien Didelot | 49463b7 | 2017-11-03 19:05:21 -0400 | [diff] [blame] | 67 | dst->index = index; |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 68 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 69 | INIT_LIST_HEAD(&dst->rtable); |
| 70 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 71 | INIT_LIST_HEAD(&dst->ports); |
| 72 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 73 | INIT_LIST_HEAD(&dst->list); |
Vivien Didelot | 50c7d2ba | 2019-10-18 17:02:46 -0400 | [diff] [blame] | 74 | list_add_tail(&dst->list, &dsa_tree_list); |
Vivien Didelot | 8e5bf97 | 2017-11-03 19:05:22 -0400 | [diff] [blame] | 75 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 76 | kref_init(&dst->refcount); |
| 77 | |
| 78 | return dst; |
| 79 | } |
| 80 | |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 81 | static void dsa_tree_free(struct dsa_switch_tree *dst) |
| 82 | { |
| 83 | list_del(&dst->list); |
| 84 | kfree(dst); |
| 85 | } |
| 86 | |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 87 | static struct dsa_switch_tree *dsa_tree_get(struct dsa_switch_tree *dst) |
| 88 | { |
| 89 | if (dst) |
| 90 | kref_get(&dst->refcount); |
| 91 | |
| 92 | return dst; |
| 93 | } |
| 94 | |
Vivien Didelot | 1ca28ec | 2017-11-03 19:05:24 -0400 | [diff] [blame] | 95 | static struct dsa_switch_tree *dsa_tree_touch(int index) |
| 96 | { |
| 97 | struct dsa_switch_tree *dst; |
| 98 | |
| 99 | dst = dsa_tree_find(index); |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 100 | if (dst) |
| 101 | return dsa_tree_get(dst); |
| 102 | else |
| 103 | return dsa_tree_alloc(index); |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void dsa_tree_release(struct kref *ref) |
| 107 | { |
| 108 | struct dsa_switch_tree *dst; |
| 109 | |
| 110 | dst = container_of(ref, struct dsa_switch_tree, refcount); |
| 111 | |
| 112 | dsa_tree_free(dst); |
| 113 | } |
| 114 | |
| 115 | static void dsa_tree_put(struct dsa_switch_tree *dst) |
| 116 | { |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 117 | if (dst) |
| 118 | kref_put(&dst->refcount, dsa_tree_release); |
Vivien Didelot | 6525410 | 2017-11-03 19:05:23 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 121 | static bool dsa_port_is_dsa(struct dsa_port *port) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 122 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 123 | return port->type == DSA_PORT_TYPE_DSA; |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static bool dsa_port_is_cpu(struct dsa_port *port) |
| 127 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 128 | return port->type == DSA_PORT_TYPE_CPU; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 131 | static bool dsa_port_is_user(struct dsa_port *dp) |
| 132 | { |
| 133 | return dp->type == DSA_PORT_TYPE_USER; |
| 134 | } |
| 135 | |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 136 | static struct dsa_port *dsa_tree_find_port_by_node(struct dsa_switch_tree *dst, |
| 137 | struct device_node *dn) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 138 | { |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 139 | struct dsa_port *dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 140 | |
Vivien Didelot | 764b7e6 | 2019-10-21 16:51:21 -0400 | [diff] [blame] | 141 | list_for_each_entry(dp, &dst->ports, list) |
| 142 | if (dp->dn == dn) |
| 143 | return dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 144 | |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Ben Dooks (Codethink) | 4e2ce6e | 2019-12-17 11:20:38 +0000 | [diff] [blame] | 148 | static struct dsa_link *dsa_link_touch(struct dsa_port *dp, |
| 149 | struct dsa_port *link_dp) |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 150 | { |
| 151 | struct dsa_switch *ds = dp->ds; |
| 152 | struct dsa_switch_tree *dst; |
| 153 | struct dsa_link *dl; |
| 154 | |
| 155 | dst = ds->dst; |
| 156 | |
| 157 | list_for_each_entry(dl, &dst->rtable, list) |
| 158 | if (dl->dp == dp && dl->link_dp == link_dp) |
| 159 | return dl; |
| 160 | |
| 161 | dl = kzalloc(sizeof(*dl), GFP_KERNEL); |
| 162 | if (!dl) |
| 163 | return NULL; |
| 164 | |
| 165 | dl->dp = dp; |
| 166 | dl->link_dp = link_dp; |
| 167 | |
| 168 | INIT_LIST_HEAD(&dl->list); |
| 169 | list_add_tail(&dl->list, &dst->rtable); |
| 170 | |
| 171 | return dl; |
| 172 | } |
| 173 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 174 | static bool dsa_port_setup_routing_table(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 175 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 176 | struct dsa_switch *ds = dp->ds; |
| 177 | struct dsa_switch_tree *dst = ds->dst; |
| 178 | struct device_node *dn = dp->dn; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 179 | struct of_phandle_iterator it; |
Vivien Didelot | f163da8 | 2017-11-06 16:11:49 -0500 | [diff] [blame] | 180 | struct dsa_port *link_dp; |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 181 | struct dsa_link *dl; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 182 | int err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 183 | |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 184 | of_for_each_phandle(&it, err, dn, "link", NULL, 0) { |
| 185 | link_dp = dsa_tree_find_port_by_node(dst, it.node); |
| 186 | if (!link_dp) { |
| 187 | of_node_put(it.node); |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 188 | return false; |
Vivien Didelot | c528666 | 2017-11-06 16:11:50 -0500 | [diff] [blame] | 189 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 190 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 191 | dl = dsa_link_touch(dp, link_dp); |
| 192 | if (!dl) { |
| 193 | of_node_put(it.node); |
| 194 | return false; |
| 195 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 198 | return true; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Vivien Didelot | 3774ecd | 2019-10-30 22:09:15 -0400 | [diff] [blame] | 201 | static bool dsa_tree_setup_routing_table(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 202 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 203 | bool complete = true; |
| 204 | struct dsa_port *dp; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 205 | |
Vivien Didelot | 86bfb2c | 2019-10-21 16:51:20 -0400 | [diff] [blame] | 206 | list_for_each_entry(dp, &dst->ports, list) { |
Vivien Didelot | 3774ecd | 2019-10-30 22:09:15 -0400 | [diff] [blame] | 207 | if (dsa_port_is_dsa(dp)) { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 208 | complete = dsa_port_setup_routing_table(dp); |
| 209 | if (!complete) |
| 210 | break; |
| 211 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 214 | return complete; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 217 | static struct dsa_port *dsa_tree_find_first_cpu(struct dsa_switch_tree *dst) |
| 218 | { |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 219 | struct dsa_port *dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 220 | |
Vivien Didelot | c0b7362 | 2019-10-21 16:51:23 -0400 | [diff] [blame] | 221 | list_for_each_entry(dp, &dst->ports, list) |
| 222 | if (dsa_port_is_cpu(dp)) |
| 223 | return dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 224 | |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | static int dsa_tree_setup_default_cpu(struct dsa_switch_tree *dst) |
| 229 | { |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 230 | struct dsa_port *cpu_dp, *dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 231 | |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 232 | cpu_dp = dsa_tree_find_first_cpu(dst); |
| 233 | if (!cpu_dp) { |
| 234 | pr_err("DSA: tree %d has no CPU port\n", dst->index); |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 235 | return -EINVAL; |
| 236 | } |
| 237 | |
| 238 | /* Assign the default CPU port to all ports of the fabric */ |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 239 | list_for_each_entry(dp, &dst->ports, list) |
| 240 | if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp)) |
| 241 | dp->cpu_dp = cpu_dp; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static void dsa_tree_teardown_default_cpu(struct dsa_switch_tree *dst) |
| 247 | { |
Vivien Didelot | da4561c | 2019-10-21 16:51:24 -0400 | [diff] [blame] | 248 | struct dsa_port *dp; |
| 249 | |
| 250 | list_for_each_entry(dp, &dst->ports, list) |
| 251 | if (dsa_port_is_user(dp) || dsa_port_is_dsa(dp)) |
| 252 | dp->cpu_dp = NULL; |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 255 | static int dsa_port_setup(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 256 | { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 257 | struct dsa_switch *ds = dp->ds; |
Jiri Pirko | 15b04ac | 2019-04-03 14:24:26 +0200 | [diff] [blame] | 258 | struct dsa_switch_tree *dst = ds->dst; |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 259 | const unsigned char *id = (const unsigned char *)&dst->index; |
| 260 | const unsigned char len = sizeof(dst->index); |
| 261 | struct devlink_port *dlp = &dp->devlink_port; |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 262 | bool dsa_port_link_registered = false; |
| 263 | bool devlink_port_registered = false; |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 264 | struct devlink_port_attrs attrs = {}; |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 265 | struct devlink *dl = ds->devlink; |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 266 | bool dsa_port_enabled = false; |
| 267 | int err = 0; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 268 | |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 269 | attrs.phys.port_number = dp->index; |
| 270 | memcpy(attrs.switch_id.id, id, len); |
| 271 | attrs.switch_id.id_len = len; |
| 272 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 273 | if (dp->setup) |
| 274 | return 0; |
| 275 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 276 | switch (dp->type) { |
| 277 | case DSA_PORT_TYPE_UNUSED: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 278 | dsa_port_disable(dp); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 279 | break; |
| 280 | case DSA_PORT_TYPE_CPU: |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 281 | memset(dlp, 0, sizeof(*dlp)); |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 282 | attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU; |
| 283 | devlink_port_attrs_set(dlp, &attrs); |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 284 | err = devlink_port_register(dl, dlp, dp->index); |
| 285 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 286 | break; |
| 287 | devlink_port_registered = true; |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 288 | |
Jiri Pirko | da07739 | 2018-05-18 09:29:03 +0200 | [diff] [blame] | 289 | err = dsa_port_link_register_of(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 290 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 291 | break; |
| 292 | dsa_port_link_registered = true; |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 293 | |
| 294 | err = dsa_port_enable(dp, NULL); |
| 295 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 296 | break; |
| 297 | dsa_port_enabled = true; |
| 298 | |
Jiri Pirko | da07739 | 2018-05-18 09:29:03 +0200 | [diff] [blame] | 299 | break; |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 300 | case DSA_PORT_TYPE_DSA: |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 301 | memset(dlp, 0, sizeof(*dlp)); |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 302 | attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA; |
| 303 | devlink_port_attrs_set(dlp, &attrs); |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 304 | err = devlink_port_register(dl, dlp, dp->index); |
| 305 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 306 | break; |
| 307 | devlink_port_registered = true; |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 308 | |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 309 | err = dsa_port_link_register_of(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 310 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 311 | break; |
| 312 | dsa_port_link_registered = true; |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 313 | |
| 314 | err = dsa_port_enable(dp, NULL); |
| 315 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 316 | break; |
| 317 | dsa_port_enabled = true; |
| 318 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 319 | break; |
| 320 | case DSA_PORT_TYPE_USER: |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 321 | memset(dlp, 0, sizeof(*dlp)); |
Danielle Ratson | 71ad8d5 | 2020-07-09 16:18:16 +0300 | [diff] [blame] | 322 | attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; |
| 323 | devlink_port_attrs_set(dlp, &attrs); |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 324 | err = devlink_port_register(dl, dlp, dp->index); |
| 325 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 326 | break; |
| 327 | devlink_port_registered = true; |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 328 | |
| 329 | dp->mac = of_get_mac_address(dp->dn); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 330 | err = dsa_slave_create(dp); |
| 331 | if (err) |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 332 | break; |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 333 | |
| 334 | devlink_port_type_eth_set(dlp, dp->slave); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 335 | break; |
| 336 | } |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 337 | |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 338 | if (err && dsa_port_enabled) |
| 339 | dsa_port_disable(dp); |
| 340 | if (err && dsa_port_link_registered) |
| 341 | dsa_port_link_unregister_of(dp); |
| 342 | if (err && devlink_port_registered) |
| 343 | devlink_port_unregister(dlp); |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 344 | if (err) |
| 345 | return err; |
Vladimir Oltean | 4ba0ebb | 2019-08-31 15:46:19 +0300 | [diff] [blame] | 346 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 347 | dp->setup = true; |
| 348 | |
| 349 | return 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 352 | static void dsa_port_teardown(struct dsa_port *dp) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 353 | { |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 354 | struct devlink_port *dlp = &dp->devlink_port; |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 355 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 356 | if (!dp->setup) |
| 357 | return; |
| 358 | |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 359 | switch (dp->type) { |
| 360 | case DSA_PORT_TYPE_UNUSED: |
| 361 | break; |
| 362 | case DSA_PORT_TYPE_CPU: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 363 | dsa_port_disable(dp); |
Andrew Lunn | 4dad81e | 2019-04-28 19:37:19 +0200 | [diff] [blame] | 364 | dsa_tag_driver_put(dp->tag_ops); |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 365 | devlink_port_unregister(dlp); |
| 366 | dsa_port_link_unregister_of(dp); |
| 367 | break; |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 368 | case DSA_PORT_TYPE_DSA: |
Vivien Didelot | 0394a63 | 2019-08-19 16:00:50 -0400 | [diff] [blame] | 369 | dsa_port_disable(dp); |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 370 | devlink_port_unregister(dlp); |
Sebastian Reichel | 3361536 | 2018-01-23 16:03:46 +0100 | [diff] [blame] | 371 | dsa_port_link_unregister_of(dp); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 372 | break; |
| 373 | case DSA_PORT_TYPE_USER: |
Vivien Didelot | 955222c | 2019-08-19 16:00:48 -0400 | [diff] [blame] | 374 | devlink_port_unregister(dlp); |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 375 | if (dp->slave) { |
| 376 | dsa_slave_destroy(dp->slave); |
| 377 | dp->slave = NULL; |
| 378 | } |
| 379 | break; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 380 | } |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 381 | |
| 382 | dp->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 385 | static int dsa_switch_setup(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 386 | { |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 387 | struct dsa_devlink_priv *dl_priv; |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 388 | int err; |
| 389 | |
| 390 | if (ds->setup) |
| 391 | return 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 392 | |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 393 | /* Initialize ds->phys_mii_mask before registering the slave MDIO bus |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 394 | * driver and before ops->setup() has run, since the switch drivers and |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 395 | * the slave MDIO bus driver rely on these values for probing PHY |
| 396 | * devices or not |
| 397 | */ |
Vivien Didelot | 02bc6e5 | 2017-10-26 11:22:56 -0400 | [diff] [blame] | 398 | ds->phys_mii_mask |= dsa_user_ports(ds); |
Florian Fainelli | 6e830d8 | 2016-06-07 16:32:39 -0700 | [diff] [blame] | 399 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 400 | /* Add the switch to devlink before calling setup, so that setup can |
| 401 | * add dpipe tables |
| 402 | */ |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 403 | ds->devlink = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv)); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 404 | if (!ds->devlink) |
| 405 | return -ENOMEM; |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 406 | dl_priv = devlink_priv(ds->devlink); |
| 407 | dl_priv->ds = ds; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 408 | |
| 409 | err = devlink_register(ds->devlink, ds->dev); |
| 410 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 411 | goto free_devlink; |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 412 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 413 | err = dsa_switch_register_notifier(ds); |
| 414 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 415 | goto unregister_devlink; |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 416 | |
Vladimir Oltean | b2243b3 | 2019-05-05 13:19:20 +0300 | [diff] [blame] | 417 | err = ds->ops->setup(ds); |
| 418 | if (err < 0) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 419 | goto unregister_notifier; |
Vladimir Oltean | b2243b3 | 2019-05-05 13:19:20 +0300 | [diff] [blame] | 420 | |
Andrew Lunn | 6b29752 | 2019-10-25 01:03:51 +0200 | [diff] [blame] | 421 | devlink_params_publish(ds->devlink); |
| 422 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 423 | if (!ds->slave_mii_bus && ds->ops->phy_read) { |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 424 | ds->slave_mii_bus = devm_mdiobus_alloc(ds->dev); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 425 | if (!ds->slave_mii_bus) { |
| 426 | err = -ENOMEM; |
| 427 | goto unregister_notifier; |
| 428 | } |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 429 | |
| 430 | dsa_slave_mii_bus_init(ds); |
| 431 | |
| 432 | err = mdiobus_register(ds->slave_mii_bus); |
| 433 | if (err < 0) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 434 | goto unregister_notifier; |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 437 | ds->setup = true; |
| 438 | |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 439 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 440 | |
| 441 | unregister_notifier: |
| 442 | dsa_switch_unregister_notifier(ds); |
| 443 | unregister_devlink: |
| 444 | devlink_unregister(ds->devlink); |
| 445 | free_devlink: |
| 446 | devlink_free(ds->devlink); |
| 447 | ds->devlink = NULL; |
| 448 | |
| 449 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 452 | static void dsa_switch_teardown(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 453 | { |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 454 | if (!ds->setup) |
| 455 | return; |
| 456 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 457 | if (ds->slave_mii_bus && ds->ops->phy_read) |
Florian Fainelli | 1eb5944 | 2016-06-07 16:32:40 -0700 | [diff] [blame] | 458 | mdiobus_unregister(ds->slave_mii_bus); |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 459 | |
| 460 | dsa_switch_unregister_notifier(ds); |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 461 | |
Vladimir Oltean | 5e3f847 | 2019-06-08 15:04:28 +0300 | [diff] [blame] | 462 | if (ds->ops->teardown) |
| 463 | ds->ops->teardown(ds); |
| 464 | |
Andrew Lunn | 96567d5 | 2017-03-28 23:45:07 +0200 | [diff] [blame] | 465 | if (ds->devlink) { |
| 466 | devlink_unregister(ds->devlink); |
| 467 | devlink_free(ds->devlink); |
| 468 | ds->devlink = NULL; |
| 469 | } |
| 470 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 471 | ds->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 474 | static int dsa_tree_setup_switches(struct dsa_switch_tree *dst) |
| 475 | { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 476 | struct dsa_port *dp; |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 477 | int err; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 478 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 479 | list_for_each_entry(dp, &dst->ports, list) { |
| 480 | err = dsa_switch_setup(dp->ds); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 481 | if (err) |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 482 | goto teardown; |
| 483 | } |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 484 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 485 | list_for_each_entry(dp, &dst->ports, list) { |
| 486 | err = dsa_port_setup(dp); |
| 487 | if (err) |
Florian Fainelli | 86f8b1c | 2020-05-03 20:50:57 -0700 | [diff] [blame] | 488 | continue; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 492 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 493 | teardown: |
| 494 | list_for_each_entry(dp, &dst->ports, list) |
| 495 | dsa_port_teardown(dp); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 496 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 497 | list_for_each_entry(dp, &dst->ports, list) |
| 498 | dsa_switch_teardown(dp->ds); |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 499 | |
| 500 | return err; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static void dsa_tree_teardown_switches(struct dsa_switch_tree *dst) |
| 504 | { |
Vivien Didelot | 1d27732 | 2017-11-06 16:11:48 -0500 | [diff] [blame] | 505 | struct dsa_port *dp; |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 506 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 507 | list_for_each_entry(dp, &dst->ports, list) |
| 508 | dsa_port_teardown(dp); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 509 | |
Vivien Didelot | fb35c60 | 2019-10-21 16:51:19 -0400 | [diff] [blame] | 510 | list_for_each_entry(dp, &dst->ports, list) |
| 511 | dsa_switch_teardown(dp->ds); |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 512 | } |
| 513 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 514 | static int dsa_tree_setup_master(struct dsa_switch_tree *dst) |
| 515 | { |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 516 | struct dsa_port *dp; |
| 517 | int err; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 518 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 519 | list_for_each_entry(dp, &dst->ports, list) { |
| 520 | if (dsa_port_is_cpu(dp)) { |
| 521 | err = dsa_master_setup(dp->master, dp); |
| 522 | if (err) |
| 523 | return err; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return 0; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static void dsa_tree_teardown_master(struct dsa_switch_tree *dst) |
| 531 | { |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 532 | struct dsa_port *dp; |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 533 | |
Vivien Didelot | 0cfec58 | 2019-10-21 16:51:22 -0400 | [diff] [blame] | 534 | list_for_each_entry(dp, &dst->ports, list) |
| 535 | if (dsa_port_is_cpu(dp)) |
| 536 | dsa_master_teardown(dp->master); |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 539 | static int dsa_tree_setup(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 540 | { |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 541 | bool complete; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 542 | int err; |
| 543 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 544 | if (dst->setup) { |
| 545 | pr_err("DSA: tree %d already setup! Disjoint trees?\n", |
| 546 | dst->index); |
| 547 | return -EEXIST; |
| 548 | } |
| 549 | |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 550 | complete = dsa_tree_setup_routing_table(dst); |
| 551 | if (!complete) |
| 552 | return 0; |
| 553 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 554 | err = dsa_tree_setup_default_cpu(dst); |
| 555 | if (err) |
| 556 | return err; |
| 557 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 558 | err = dsa_tree_setup_switches(dst); |
| 559 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 560 | goto teardown_default_cpu; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 561 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 562 | err = dsa_tree_setup_master(dst); |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 563 | if (err) |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 564 | goto teardown_switches; |
Vivien Didelot | 1943563 | 2017-09-19 11:56:59 -0400 | [diff] [blame] | 565 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 566 | dst->setup = true; |
| 567 | |
| 568 | pr_info("DSA: tree %d setup\n", dst->index); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 569 | |
| 570 | return 0; |
Ioana Ciornei | e70c7aa | 2019-05-30 09:09:07 +0300 | [diff] [blame] | 571 | |
| 572 | teardown_switches: |
| 573 | dsa_tree_teardown_switches(dst); |
| 574 | teardown_default_cpu: |
| 575 | dsa_tree_teardown_default_cpu(dst); |
| 576 | |
| 577 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 580 | static void dsa_tree_teardown(struct dsa_switch_tree *dst) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 581 | { |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 582 | struct dsa_link *dl, *next; |
| 583 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 584 | if (!dst->setup) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 585 | return; |
| 586 | |
Vivien Didelot | 17a22fc | 2017-11-06 16:11:45 -0500 | [diff] [blame] | 587 | dsa_tree_teardown_master(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 588 | |
Vivien Didelot | 1f08f9e | 2017-11-06 16:11:47 -0500 | [diff] [blame] | 589 | dsa_tree_teardown_switches(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 590 | |
Vivien Didelot | f070464 | 2017-11-06 16:11:44 -0500 | [diff] [blame] | 591 | dsa_tree_teardown_default_cpu(dst); |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 592 | |
Vivien Didelot | c5f5176 | 2019-10-30 22:09:13 -0400 | [diff] [blame] | 593 | list_for_each_entry_safe(dl, next, &dst->rtable, list) { |
| 594 | list_del(&dl->list); |
| 595 | kfree(dl); |
| 596 | } |
| 597 | |
Vivien Didelot | ec15dd4 | 2017-11-06 16:11:46 -0500 | [diff] [blame] | 598 | pr_info("DSA: tree %d torn down\n", dst->index); |
| 599 | |
| 600 | dst->setup = false; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 603 | static struct dsa_port *dsa_port_touch(struct dsa_switch *ds, int index) |
| 604 | { |
| 605 | struct dsa_switch_tree *dst = ds->dst; |
| 606 | struct dsa_port *dp; |
| 607 | |
Vivien Didelot | 05f294a | 2019-10-21 16:51:29 -0400 | [diff] [blame] | 608 | list_for_each_entry(dp, &dst->ports, list) |
| 609 | if (dp->ds == ds && dp->index == index) |
| 610 | return dp; |
| 611 | |
| 612 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); |
| 613 | if (!dp) |
| 614 | return NULL; |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 615 | |
| 616 | dp->ds = ds; |
| 617 | dp->index = index; |
| 618 | |
| 619 | INIT_LIST_HEAD(&dp->list); |
| 620 | list_add_tail(&dp->list, &dst->ports); |
| 621 | |
| 622 | return dp; |
| 623 | } |
| 624 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 625 | static int dsa_port_parse_user(struct dsa_port *dp, const char *name) |
| 626 | { |
| 627 | if (!name) |
| 628 | name = "eth%d"; |
| 629 | |
| 630 | dp->type = DSA_PORT_TYPE_USER; |
| 631 | dp->name = name; |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | static int dsa_port_parse_dsa(struct dsa_port *dp) |
| 637 | { |
| 638 | dp->type = DSA_PORT_TYPE_DSA; |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 643 | static enum dsa_tag_protocol dsa_get_tag_protocol(struct dsa_port *dp, |
| 644 | struct net_device *master) |
| 645 | { |
| 646 | enum dsa_tag_protocol tag_protocol = DSA_TAG_PROTO_NONE; |
| 647 | struct dsa_switch *mds, *ds = dp->ds; |
| 648 | unsigned int mdp_upstream; |
| 649 | struct dsa_port *mdp; |
| 650 | |
| 651 | /* It is possible to stack DSA switches onto one another when that |
| 652 | * happens the switch driver may want to know if its tagging protocol |
| 653 | * is going to work in such a configuration. |
| 654 | */ |
| 655 | if (dsa_slave_dev_check(master)) { |
| 656 | mdp = dsa_slave_to_port(master); |
| 657 | mds = mdp->ds; |
| 658 | mdp_upstream = dsa_upstream_port(mds, mdp->index); |
| 659 | tag_protocol = mds->ops->get_tag_protocol(mds, mdp_upstream, |
| 660 | DSA_TAG_PROTO_NONE); |
| 661 | } |
| 662 | |
| 663 | /* If the master device is not itself a DSA slave in a disjoint DSA |
| 664 | * tree, then return immediately. |
| 665 | */ |
| 666 | return ds->ops->get_tag_protocol(ds, dp->index, tag_protocol); |
| 667 | } |
| 668 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 669 | static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master) |
| 670 | { |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 671 | struct dsa_switch *ds = dp->ds; |
| 672 | struct dsa_switch_tree *dst = ds->dst; |
Vivien Didelot | 62fc958 | 2017-09-29 17:19:17 -0400 | [diff] [blame] | 673 | const struct dsa_device_ops *tag_ops; |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 674 | enum dsa_tag_protocol tag_protocol; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 675 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 676 | tag_protocol = dsa_get_tag_protocol(dp, master); |
Andrew Lunn | c39e2a1 | 2019-04-28 19:37:18 +0200 | [diff] [blame] | 677 | tag_ops = dsa_tag_driver_get(tag_protocol); |
Vivien Didelot | 62fc958 | 2017-09-29 17:19:17 -0400 | [diff] [blame] | 678 | if (IS_ERR(tag_ops)) { |
Andrew Lunn | 23426a2 | 2019-09-12 15:16:45 +0200 | [diff] [blame] | 679 | if (PTR_ERR(tag_ops) == -ENOPROTOOPT) |
| 680 | return -EPROBE_DEFER; |
Florian Fainelli | 9f9e772 | 2017-07-24 10:49:23 -0700 | [diff] [blame] | 681 | dev_warn(ds->dev, "No tagger for this switch\n"); |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 682 | dp->master = NULL; |
Vivien Didelot | 62fc958 | 2017-09-29 17:19:17 -0400 | [diff] [blame] | 683 | return PTR_ERR(tag_ops); |
Florian Fainelli | 9f9e772 | 2017-07-24 10:49:23 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Florian Fainelli | 4d77648 | 2020-01-07 21:06:05 -0800 | [diff] [blame] | 686 | dp->master = master; |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 687 | dp->type = DSA_PORT_TYPE_CPU; |
Vladimir Oltean | cc1939e | 2019-05-05 13:19:23 +0300 | [diff] [blame] | 688 | dp->filter = tag_ops->filter; |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 689 | dp->rcv = tag_ops->rcv; |
| 690 | dp->tag_ops = tag_ops; |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 691 | dp->dst = dst; |
Vivien Didelot | 3e41f93 | 2017-09-29 17:19:19 -0400 | [diff] [blame] | 692 | |
Vivien Didelot | 7354fcb | 2017-11-03 19:05:30 -0400 | [diff] [blame] | 693 | return 0; |
| 694 | } |
| 695 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 696 | static int dsa_port_parse_of(struct dsa_port *dp, struct device_node *dn) |
| 697 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 698 | struct device_node *ethernet = of_parse_phandle(dn, "ethernet", 0); |
Vivien Didelot | 1838fa8 | 2017-10-27 15:55:18 -0400 | [diff] [blame] | 699 | const char *name = of_get_property(dn, "label", NULL); |
Vivien Didelot | 54df6fa | 2017-11-03 19:05:28 -0400 | [diff] [blame] | 700 | bool link = of_property_read_bool(dn, "link"); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 701 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 702 | dp->dn = dn; |
| 703 | |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 704 | if (ethernet) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 705 | struct net_device *master; |
| 706 | |
| 707 | master = of_find_net_device_by_node(ethernet); |
| 708 | if (!master) |
| 709 | return -EPROBE_DEFER; |
| 710 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 711 | return dsa_port_parse_cpu(dp, master); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 712 | } |
| 713 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 714 | if (link) |
| 715 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 716 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 717 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 718 | } |
| 719 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 720 | static int dsa_switch_parse_ports_of(struct dsa_switch *ds, |
| 721 | struct device_node *dn) |
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 | struct device_node *ports, *port; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 724 | struct dsa_port *dp; |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 725 | int err = 0; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 726 | u32 reg; |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 727 | |
| 728 | ports = of_get_child_by_name(dn, "ports"); |
| 729 | if (!ports) { |
Kurt Kanzenbach | 85e05d2 | 2020-07-20 14:49:39 +0200 | [diff] [blame^] | 730 | /* The second possibility is "ethernet-ports" */ |
| 731 | ports = of_get_child_by_name(dn, "ethernet-ports"); |
| 732 | if (!ports) { |
| 733 | dev_err(ds->dev, "no ports child node found\n"); |
| 734 | return -EINVAL; |
| 735 | } |
Vivien Didelot | 5b32fe0 | 2017-10-27 15:55:13 -0400 | [diff] [blame] | 736 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 737 | |
| 738 | for_each_available_child_of_node(ports, port) { |
| 739 | err = of_property_read_u32(port, "reg", ®); |
| 740 | if (err) |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 741 | goto out_put_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 742 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 743 | if (reg >= ds->num_ports) { |
| 744 | err = -EINVAL; |
| 745 | goto out_put_node; |
| 746 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 747 | |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 748 | dp = dsa_to_port(ds, reg); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 749 | |
| 750 | err = dsa_port_parse_of(dp, port); |
| 751 | if (err) |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 752 | goto out_put_node; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Wen Yang | 9919a36 | 2019-02-25 15:22:19 +0800 | [diff] [blame] | 755 | out_put_node: |
| 756 | of_node_put(ports); |
| 757 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 758 | } |
| 759 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 760 | static int dsa_switch_parse_member_of(struct dsa_switch *ds, |
| 761 | struct device_node *dn) |
| 762 | { |
| 763 | u32 m[2] = { 0, 0 }; |
| 764 | int sz; |
| 765 | |
| 766 | /* Don't error out if this optional property isn't found */ |
| 767 | sz = of_property_read_variable_u32_array(dn, "dsa,member", m, 2, 2); |
| 768 | if (sz < 0 && sz != -EINVAL) |
| 769 | return sz; |
| 770 | |
| 771 | ds->index = m[1]; |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 772 | |
| 773 | ds->dst = dsa_tree_touch(m[0]); |
| 774 | if (!ds->dst) |
| 775 | return -ENOMEM; |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 780 | static int dsa_switch_touch_ports(struct dsa_switch *ds) |
| 781 | { |
| 782 | struct dsa_port *dp; |
| 783 | int port; |
| 784 | |
| 785 | for (port = 0; port < ds->num_ports; port++) { |
| 786 | dp = dsa_port_touch(ds, port); |
| 787 | if (!dp) |
| 788 | return -ENOMEM; |
| 789 | } |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 794 | static int dsa_switch_parse_of(struct dsa_switch *ds, struct device_node *dn) |
| 795 | { |
| 796 | int err; |
| 797 | |
| 798 | err = dsa_switch_parse_member_of(ds, dn); |
| 799 | if (err) |
| 800 | return err; |
| 801 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 802 | err = dsa_switch_touch_ports(ds); |
| 803 | if (err) |
| 804 | return err; |
| 805 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 806 | return dsa_switch_parse_ports_of(ds, dn); |
| 807 | } |
| 808 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 809 | static int dsa_port_parse(struct dsa_port *dp, const char *name, |
| 810 | struct device *dev) |
| 811 | { |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 812 | if (!strcmp(name, "cpu")) { |
Vivien Didelot | cbabb0a | 2017-10-27 15:55:17 -0400 | [diff] [blame] | 813 | struct net_device *master; |
| 814 | |
| 815 | master = dsa_dev_to_net_device(dev); |
| 816 | if (!master) |
| 817 | return -EPROBE_DEFER; |
| 818 | |
| 819 | dev_put(master); |
| 820 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 821 | return dsa_port_parse_cpu(dp, master); |
Vivien Didelot | 6d4e5c5 | 2017-10-27 15:55:15 -0400 | [diff] [blame] | 822 | } |
| 823 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 824 | if (!strcmp(name, "dsa")) |
| 825 | return dsa_port_parse_dsa(dp); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 826 | |
Vivien Didelot | 06e24d0 | 2017-11-03 19:05:29 -0400 | [diff] [blame] | 827 | return dsa_port_parse_user(dp, name); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 828 | } |
| 829 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 830 | static int dsa_switch_parse_ports(struct dsa_switch *ds, |
| 831 | struct dsa_chip_data *cd) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 832 | { |
| 833 | bool valid_name_found = false; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 834 | struct dsa_port *dp; |
| 835 | struct device *dev; |
| 836 | const char *name; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 837 | unsigned int i; |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 838 | int err; |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 839 | |
| 840 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 841 | name = cd->port_names[i]; |
| 842 | dev = cd->netdev[i]; |
Vivien Didelot | 68bb8ea | 2019-10-21 16:51:15 -0400 | [diff] [blame] | 843 | dp = dsa_to_port(ds, i); |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 844 | |
| 845 | if (!name) |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 846 | continue; |
| 847 | |
Vivien Didelot | fd223e2 | 2017-10-27 15:55:14 -0400 | [diff] [blame] | 848 | err = dsa_port_parse(dp, name, dev); |
| 849 | if (err) |
| 850 | return err; |
| 851 | |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 852 | valid_name_found = true; |
| 853 | } |
| 854 | |
| 855 | if (!valid_name_found && i == DSA_MAX_PORTS) |
| 856 | return -EINVAL; |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 861 | 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] | 862 | { |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 863 | int err; |
| 864 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 865 | ds->cd = cd; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 866 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 867 | /* We don't support interconnected switches nor multiple trees via |
| 868 | * platform data, so this is the unique switch of the tree. |
| 869 | */ |
| 870 | ds->index = 0; |
| 871 | ds->dst = dsa_tree_touch(0); |
| 872 | if (!ds->dst) |
| 873 | return -ENOMEM; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 874 | |
Vivien Didelot | ab8ccae | 2019-10-21 16:51:16 -0400 | [diff] [blame] | 875 | err = dsa_switch_touch_ports(ds); |
| 876 | if (err) |
| 877 | return err; |
| 878 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 879 | return dsa_switch_parse_ports(ds, cd); |
Florian Fainelli | 71e0bbd | 2017-02-04 13:02:43 -0800 | [diff] [blame] | 880 | } |
| 881 | |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 882 | static void dsa_switch_release_ports(struct dsa_switch *ds) |
| 883 | { |
| 884 | struct dsa_switch_tree *dst = ds->dst; |
| 885 | struct dsa_port *dp, *next; |
| 886 | |
| 887 | list_for_each_entry_safe(dp, next, &dst->ports, list) { |
| 888 | if (dp->ds != ds) |
| 889 | continue; |
| 890 | list_del(&dp->list); |
| 891 | kfree(dp); |
| 892 | } |
| 893 | } |
| 894 | |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 895 | static int dsa_switch_probe(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 896 | { |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 897 | struct dsa_switch_tree *dst; |
Colin Ian King | 556f124 | 2019-10-24 11:32:18 +0100 | [diff] [blame] | 898 | struct dsa_chip_data *pdata; |
| 899 | struct device_node *np; |
Vivien Didelot | 34c09a8 | 2017-11-06 16:11:51 -0500 | [diff] [blame] | 900 | int err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 901 | |
Vivien Didelot | 7e99e34 | 2019-10-21 16:51:30 -0400 | [diff] [blame] | 902 | if (!ds->dev) |
| 903 | return -ENODEV; |
| 904 | |
Colin Ian King | 556f124 | 2019-10-24 11:32:18 +0100 | [diff] [blame] | 905 | pdata = ds->dev->platform_data; |
| 906 | np = ds->dev->of_node; |
| 907 | |
Vivien Didelot | 7e99e34 | 2019-10-21 16:51:30 -0400 | [diff] [blame] | 908 | if (!ds->num_ports) |
| 909 | return -EINVAL; |
| 910 | |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 911 | if (np) { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 912 | err = dsa_switch_parse_of(ds, np); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 913 | if (err) |
| 914 | dsa_switch_release_ports(ds); |
| 915 | } else if (pdata) { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 916 | err = dsa_switch_parse(ds, pdata); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 917 | if (err) |
| 918 | dsa_switch_release_ports(ds); |
| 919 | } else { |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 920 | err = -ENODEV; |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 921 | } |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 922 | |
Vivien Didelot | 975e6e3 | 2017-11-03 19:05:27 -0400 | [diff] [blame] | 923 | if (err) |
| 924 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 925 | |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 926 | dst = ds->dst; |
| 927 | dsa_tree_get(dst); |
| 928 | err = dsa_tree_setup(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 929 | if (err) { |
| 930 | dsa_switch_release_ports(ds); |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 931 | dsa_tree_put(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 932 | } |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 933 | |
| 934 | return err; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Vivien Didelot | 23c9ee4 | 2017-05-26 18:12:51 -0400 | [diff] [blame] | 937 | int dsa_register_switch(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 938 | { |
| 939 | int err; |
| 940 | |
| 941 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 942 | err = dsa_switch_probe(ds); |
Vivien Didelot | 9e74104 | 2017-11-24 11:36:06 -0500 | [diff] [blame] | 943 | dsa_tree_put(ds->dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 944 | mutex_unlock(&dsa2_mutex); |
| 945 | |
| 946 | return err; |
| 947 | } |
| 948 | EXPORT_SYMBOL_GPL(dsa_register_switch); |
| 949 | |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 950 | static void dsa_switch_remove(struct dsa_switch *ds) |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 951 | { |
| 952 | struct dsa_switch_tree *dst = ds->dst; |
Vivien Didelot | 05f294a | 2019-10-21 16:51:29 -0400 | [diff] [blame] | 953 | |
Florian Fainelli | c058f6d | 2019-11-02 20:13:26 -0700 | [diff] [blame] | 954 | dsa_tree_teardown(dst); |
Vladimir Oltean | 6dc43cd | 2020-01-25 23:01:11 +0200 | [diff] [blame] | 955 | dsa_switch_release_ports(ds); |
Vivien Didelot | 8e5cb84 | 2019-10-30 22:09:17 -0400 | [diff] [blame] | 956 | dsa_tree_put(dst); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | void dsa_unregister_switch(struct dsa_switch *ds) |
| 960 | { |
| 961 | mutex_lock(&dsa2_mutex); |
Vivien Didelot | b4fbb34 | 2017-11-06 16:11:53 -0500 | [diff] [blame] | 962 | dsa_switch_remove(ds); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 963 | mutex_unlock(&dsa2_mutex); |
| 964 | } |
| 965 | EXPORT_SYMBOL_GPL(dsa_unregister_switch); |