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