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