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