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