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