Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/dsa.c - Hardware switch handling |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 4 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 12 | #include <linux/device.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 13 | #include <linux/list.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 17 | #include <linux/of.h> |
| 18 | #include <linux/of_mdio.h> |
| 19 | #include <linux/of_platform.h> |
Florian Fainelli | 769a020 | 2015-03-09 14:31:21 -0700 | [diff] [blame] | 20 | #include <linux/of_net.h> |
Andrew Lunn | cc30c16 | 2015-11-20 03:56:23 +0100 | [diff] [blame] | 21 | #include <linux/of_gpio.h> |
Andrew Lunn | c6e970a | 2017-03-28 23:45:06 +0200 | [diff] [blame] | 22 | #include <linux/netdevice.h> |
Guenter Roeck | 51579c3 | 2014-10-29 10:44:58 -0700 | [diff] [blame] | 23 | #include <linux/sysfs.h> |
Neil Armstrong | cbc5d90 | 2015-10-06 15:40:32 +0100 | [diff] [blame] | 24 | #include <linux/phy_fixed.h> |
Arnd Bergmann | 85beabf | 2015-11-24 12:34:49 +0100 | [diff] [blame] | 25 | #include <linux/gpio/consumer.h> |
Andrew Lunn | c6e970a | 2017-03-28 23:45:06 +0200 | [diff] [blame] | 26 | #include <net/dsa.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 27 | #include "dsa_priv.h" |
| 28 | |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 29 | static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb, |
| 30 | struct net_device *dev) |
| 31 | { |
| 32 | /* Just return the original SKB */ |
| 33 | return skb; |
| 34 | } |
| 35 | |
| 36 | static const struct dsa_device_ops none_ops = { |
| 37 | .xmit = dsa_slave_notag_xmit, |
| 38 | .rcv = NULL, |
| 39 | }; |
| 40 | |
| 41 | const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = { |
| 42 | #ifdef CONFIG_NET_DSA_TAG_DSA |
| 43 | [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops, |
| 44 | #endif |
| 45 | #ifdef CONFIG_NET_DSA_TAG_EDSA |
| 46 | [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops, |
| 47 | #endif |
| 48 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
| 49 | [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops, |
| 50 | #endif |
| 51 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 52 | [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops, |
| 53 | #endif |
John Crispin | cafdc45 | 2016-09-15 16:26:40 +0200 | [diff] [blame] | 54 | #ifdef CONFIG_NET_DSA_TAG_QCA |
| 55 | [DSA_TAG_PROTO_QCA] = &qca_netdev_ops, |
| 56 | #endif |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 57 | [DSA_TAG_PROTO_NONE] = &none_ops, |
| 58 | }; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 59 | |
| 60 | /* switch driver registration ***********************************************/ |
| 61 | static DEFINE_MUTEX(dsa_switch_drivers_mutex); |
| 62 | static LIST_HEAD(dsa_switch_drivers); |
| 63 | |
Florian Fainelli | ab3d408 | 2017-01-08 14:52:07 -0800 | [diff] [blame] | 64 | void register_switch_driver(struct dsa_switch_driver *drv) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 65 | { |
| 66 | mutex_lock(&dsa_switch_drivers_mutex); |
Florian Fainelli | ab3d408 | 2017-01-08 14:52:07 -0800 | [diff] [blame] | 67 | list_add_tail(&drv->list, &dsa_switch_drivers); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 68 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 69 | } |
Ben Hutchings | ad293b8 | 2011-11-25 14:34:07 +0000 | [diff] [blame] | 70 | EXPORT_SYMBOL_GPL(register_switch_driver); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 71 | |
Florian Fainelli | ab3d408 | 2017-01-08 14:52:07 -0800 | [diff] [blame] | 72 | void unregister_switch_driver(struct dsa_switch_driver *drv) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 73 | { |
| 74 | mutex_lock(&dsa_switch_drivers_mutex); |
Florian Fainelli | ab3d408 | 2017-01-08 14:52:07 -0800 | [diff] [blame] | 75 | list_del_init(&drv->list); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 76 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 77 | } |
Ben Hutchings | ad293b8 | 2011-11-25 14:34:07 +0000 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(unregister_switch_driver); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 79 | |
Florian Fainelli | a82f67a | 2017-01-08 14:52:08 -0800 | [diff] [blame] | 80 | static const struct dsa_switch_ops * |
Andrew Lunn | bbb8d79 | 2016-04-13 02:40:39 +0200 | [diff] [blame] | 81 | dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr, |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 82 | const char **_name, void **priv) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 83 | { |
Florian Fainelli | a82f67a | 2017-01-08 14:52:08 -0800 | [diff] [blame] | 84 | const struct dsa_switch_ops *ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 85 | struct list_head *list; |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 86 | const char *name; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 87 | |
| 88 | ret = NULL; |
| 89 | name = NULL; |
| 90 | |
| 91 | mutex_lock(&dsa_switch_drivers_mutex); |
| 92 | list_for_each(list, &dsa_switch_drivers) { |
Florian Fainelli | a82f67a | 2017-01-08 14:52:08 -0800 | [diff] [blame] | 93 | const struct dsa_switch_ops *ops; |
Florian Fainelli | ab3d408 | 2017-01-08 14:52:07 -0800 | [diff] [blame] | 94 | struct dsa_switch_driver *drv; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 95 | |
Florian Fainelli | ab3d408 | 2017-01-08 14:52:07 -0800 | [diff] [blame] | 96 | drv = list_entry(list, struct dsa_switch_driver, list); |
| 97 | ops = drv->ops; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 98 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 99 | name = ops->probe(parent, host_dev, sw_addr, priv); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 100 | if (name != NULL) { |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 101 | ret = ops; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | } |
| 105 | mutex_unlock(&dsa_switch_drivers_mutex); |
| 106 | |
| 107 | *_name = name; |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 112 | /* basic switch operations **************************************************/ |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 113 | int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev, |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 114 | struct dsa_port *dport, int port) |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 115 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 116 | struct device_node *port_dn = dport->dn; |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 117 | struct phy_device *phydev; |
| 118 | int ret, mode; |
| 119 | |
| 120 | if (of_phy_is_fixed_link(port_dn)) { |
| 121 | ret = of_phy_register_fixed_link(port_dn); |
| 122 | if (ret) { |
| 123 | dev_err(dev, "failed to register fixed PHY\n"); |
| 124 | return ret; |
| 125 | } |
| 126 | phydev = of_phy_find_device(port_dn); |
| 127 | |
| 128 | mode = of_get_phy_mode(port_dn); |
| 129 | if (mode < 0) |
| 130 | mode = PHY_INTERFACE_MODE_NA; |
| 131 | phydev->interface = mode; |
| 132 | |
| 133 | genphy_config_init(phydev); |
| 134 | genphy_read_status(phydev); |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 135 | if (ds->ops->adjust_link) |
| 136 | ds->ops->adjust_link(ds, port, phydev); |
Johan Hovold | fd05d7b | 2016-11-24 19:21:27 +0100 | [diff] [blame] | 137 | |
| 138 | put_device(&phydev->mdio.dev); |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev) |
Andrew Lunn | 39b0c70 | 2015-08-31 15:56:49 +0200 | [diff] [blame] | 145 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 146 | struct dsa_port *dport; |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 147 | int ret, port; |
Andrew Lunn | 39b0c70 | 2015-08-31 15:56:49 +0200 | [diff] [blame] | 148 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 149 | for (port = 0; port < ds->num_ports; port++) { |
Andrew Lunn | 39b0c70 | 2015-08-31 15:56:49 +0200 | [diff] [blame] | 150 | if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) |
| 151 | continue; |
| 152 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 153 | dport = &ds->ports[port]; |
| 154 | ret = dsa_cpu_dsa_setup(ds, dev, dport, port); |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 155 | if (ret) |
| 156 | return ret; |
Andrew Lunn | 39b0c70 | 2015-08-31 15:56:49 +0200 | [diff] [blame] | 157 | } |
| 158 | return 0; |
| 159 | } |
| 160 | |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 161 | const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol) |
| 162 | { |
| 163 | const struct dsa_device_ops *ops; |
| 164 | |
| 165 | if (tag_protocol >= DSA_TAG_LAST) |
| 166 | return ERR_PTR(-EINVAL); |
| 167 | ops = dsa_device_ops[tag_protocol]; |
| 168 | |
| 169 | if (!ops) |
| 170 | return ERR_PTR(-ENOPROTOOPT); |
| 171 | |
| 172 | return ops; |
| 173 | } |
| 174 | |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 175 | int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds) |
| 176 | { |
| 177 | struct net_device *master; |
| 178 | struct ethtool_ops *cpu_ops; |
| 179 | |
| 180 | master = ds->dst->master_netdev; |
| 181 | if (ds->master_netdev) |
| 182 | master = ds->master_netdev; |
| 183 | |
| 184 | cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL); |
| 185 | if (!cpu_ops) |
| 186 | return -ENOMEM; |
| 187 | |
| 188 | memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops, |
| 189 | sizeof(struct ethtool_ops)); |
| 190 | ds->dst->master_orig_ethtool_ops = master->ethtool_ops; |
| 191 | memcpy(cpu_ops, &ds->dst->master_ethtool_ops, |
| 192 | sizeof(struct ethtool_ops)); |
| 193 | dsa_cpu_port_ethtool_init(cpu_ops); |
| 194 | master->ethtool_ops = cpu_ops; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds) |
| 200 | { |
| 201 | struct net_device *master; |
| 202 | |
| 203 | master = ds->dst->master_netdev; |
| 204 | if (ds->master_netdev) |
| 205 | master = ds->master_netdev; |
| 206 | |
| 207 | master->ethtool_ops = ds->dst->master_orig_ethtool_ops; |
| 208 | } |
| 209 | |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 210 | static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 211 | { |
Florian Fainelli | a82f67a | 2017-01-08 14:52:08 -0800 | [diff] [blame] | 212 | const struct dsa_switch_ops *ops = ds->ops; |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 213 | struct dsa_switch_tree *dst = ds->dst; |
Andrew Lunn | ff04955 | 2016-05-10 23:27:24 +0200 | [diff] [blame] | 214 | struct dsa_chip_data *cd = ds->cd; |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 215 | bool valid_name_found = false; |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 216 | int index = ds->index; |
| 217 | int i, ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 218 | |
| 219 | /* |
| 220 | * Validate supplied switch configuration. |
| 221 | */ |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 222 | for (i = 0; i < ds->num_ports; i++) { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 223 | char *name; |
| 224 | |
Andrew Lunn | ff04955 | 2016-05-10 23:27:24 +0200 | [diff] [blame] | 225 | name = cd->port_names[i]; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 226 | if (name == NULL) |
| 227 | continue; |
| 228 | |
| 229 | if (!strcmp(name, "cpu")) { |
Andrew Lunn | 23e3d61 | 2017-01-22 22:16:45 +0100 | [diff] [blame] | 230 | if (dst->cpu_switch) { |
Joe Perches | a2ae600 | 2014-11-09 16:32:46 -0800 | [diff] [blame] | 231 | netdev_err(dst->master_netdev, |
| 232 | "multiple cpu ports?!\n"); |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 233 | return -EINVAL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 234 | } |
Vivien Didelot | b22de49 | 2017-01-17 20:41:38 -0500 | [diff] [blame] | 235 | dst->cpu_switch = ds; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 236 | dst->cpu_port = i; |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 237 | ds->cpu_port_mask |= 1 << i; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 238 | } else if (!strcmp(name, "dsa")) { |
| 239 | ds->dsa_port_mask |= 1 << i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 240 | } else { |
Andrew Lunn | 74c3e2a | 2016-04-13 02:40:44 +0200 | [diff] [blame] | 241 | ds->enabled_port_mask |= 1 << i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 242 | } |
Florian Fainelli | f9bf5a2 | 2013-01-21 09:58:51 +0000 | [diff] [blame] | 243 | valid_name_found = true; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 246 | if (!valid_name_found && i == ds->num_ports) |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 247 | return -EINVAL; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 248 | |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 249 | /* Make the built-in MII bus mask match the number of ports, |
| 250 | * switch drivers can override this later |
| 251 | */ |
Andrew Lunn | 74c3e2a | 2016-04-13 02:40:44 +0200 | [diff] [blame] | 252 | ds->phys_mii_mask = ds->enabled_port_mask; |
Florian Fainelli | 0d8bcdd | 2014-08-27 17:04:51 -0700 | [diff] [blame] | 253 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 254 | /* |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 255 | * If the CPU connects to this switch, set the switch tree |
| 256 | * tagging protocol to the preferred tagging format of this |
| 257 | * switch. |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 258 | */ |
Vivien Didelot | b22de49 | 2017-01-17 20:41:38 -0500 | [diff] [blame] | 259 | if (dst->cpu_switch == ds) { |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 260 | enum dsa_tag_protocol tag_protocol; |
| 261 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 262 | tag_protocol = ops->get_tag_protocol(ds); |
Andrew Lunn | 7b31436 | 2016-08-22 16:01:01 +0200 | [diff] [blame] | 263 | dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol); |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 264 | if (IS_ERR(dst->tag_ops)) |
| 265 | return PTR_ERR(dst->tag_ops); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 266 | |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 267 | dst->rcv = dst->tag_ops->rcv; |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 268 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 269 | |
Andrew Lunn | 66472fc | 2016-06-04 21:17:00 +0200 | [diff] [blame] | 270 | memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable)); |
| 271 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 272 | /* |
| 273 | * Do basic register setup. |
| 274 | */ |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 275 | ret = ops->setup(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 276 | if (ret < 0) |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 277 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 278 | |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 279 | ret = dsa_switch_register_notifier(ds); |
| 280 | if (ret) |
| 281 | return ret; |
| 282 | |
John Crispin | 092183d | 2016-09-19 15:28:01 +0200 | [diff] [blame] | 283 | if (ops->set_addr) { |
| 284 | ret = ops->set_addr(ds, dst->master_netdev->dev_addr); |
| 285 | if (ret < 0) |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 286 | return ret; |
John Crispin | 092183d | 2016-09-19 15:28:01 +0200 | [diff] [blame] | 287 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 288 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 289 | if (!ds->slave_mii_bus && ops->phy_read) { |
Andrew Lunn | e755e49 | 2016-06-04 21:17:04 +0200 | [diff] [blame] | 290 | ds->slave_mii_bus = devm_mdiobus_alloc(parent); |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 291 | if (!ds->slave_mii_bus) |
| 292 | return -ENOMEM; |
Andrew Lunn | e755e49 | 2016-06-04 21:17:04 +0200 | [diff] [blame] | 293 | dsa_slave_mii_bus_init(ds); |
| 294 | |
| 295 | ret = mdiobus_register(ds->slave_mii_bus); |
| 296 | if (ret < 0) |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 297 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 298 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | * Create network devices for physical switch ports. |
| 302 | */ |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 303 | for (i = 0; i < ds->num_ports; i++) { |
Andrew Lunn | 189b0d9 | 2016-06-04 21:16:58 +0200 | [diff] [blame] | 304 | ds->ports[i].dn = cd->port_dn[i]; |
| 305 | |
Andrew Lunn | 74c3e2a | 2016-04-13 02:40:44 +0200 | [diff] [blame] | 306 | if (!(ds->enabled_port_mask & (1 << i))) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 307 | continue; |
| 308 | |
Andrew Lunn | ff04955 | 2016-05-10 23:27:24 +0200 | [diff] [blame] | 309 | ret = dsa_slave_create(ds, parent, i, cd->port_names[i]); |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 310 | if (ret < 0) |
Russell King | d25b8e7 | 2015-10-03 18:09:07 +0100 | [diff] [blame] | 311 | netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n", |
Andrew Lunn | ff04955 | 2016-05-10 23:27:24 +0200 | [diff] [blame] | 312 | index, i, cd->port_names[i], ret); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Andrew Lunn | 39b0c70 | 2015-08-31 15:56:49 +0200 | [diff] [blame] | 315 | /* Perform configuration of the CPU and DSA ports */ |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 316 | ret = dsa_cpu_dsa_setups(ds, parent); |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 317 | if (ret < 0) |
Andrew Lunn | 39b0c70 | 2015-08-31 15:56:49 +0200 | [diff] [blame] | 318 | netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n", |
| 319 | index); |
Andrew Lunn | 39b0c70 | 2015-08-31 15:56:49 +0200 | [diff] [blame] | 320 | |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 321 | ret = dsa_cpu_port_ethtool_setup(ds); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | |
Vivien Didelot | a896eee | 2017-01-03 14:31:49 -0500 | [diff] [blame] | 325 | return 0; |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static struct dsa_switch * |
| 329 | dsa_switch_setup(struct dsa_switch_tree *dst, int index, |
| 330 | struct device *parent, struct device *host_dev) |
| 331 | { |
Andrew Lunn | ff04955 | 2016-05-10 23:27:24 +0200 | [diff] [blame] | 332 | struct dsa_chip_data *cd = dst->pd->chip + index; |
Florian Fainelli | a82f67a | 2017-01-08 14:52:08 -0800 | [diff] [blame] | 333 | const struct dsa_switch_ops *ops; |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 334 | struct dsa_switch *ds; |
| 335 | int ret; |
Vivien Didelot | 0209d14 | 2016-04-17 13:23:55 -0400 | [diff] [blame] | 336 | const char *name; |
Andrew Lunn | 7543a6d | 2016-04-13 02:40:40 +0200 | [diff] [blame] | 337 | void *priv; |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Probe for switch model. |
| 341 | */ |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 342 | ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv); |
| 343 | if (!ops) { |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 344 | netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n", |
| 345 | index); |
| 346 | return ERR_PTR(-EINVAL); |
| 347 | } |
| 348 | netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n", |
| 349 | index, name); |
| 350 | |
| 351 | |
| 352 | /* |
| 353 | * Allocate and initialise switch state. |
| 354 | */ |
Vivien Didelot | a0c0216 | 2017-01-27 15:29:36 -0500 | [diff] [blame] | 355 | ds = dsa_switch_alloc(parent, DSA_MAX_PORTS); |
| 356 | if (!ds) |
Florian Fainelli | 2459534 | 2015-05-29 10:29:46 -0700 | [diff] [blame] | 357 | return ERR_PTR(-ENOMEM); |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 358 | |
| 359 | ds->dst = dst; |
| 360 | ds->index = index; |
Andrew Lunn | ff04955 | 2016-05-10 23:27:24 +0200 | [diff] [blame] | 361 | ds->cd = cd; |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 362 | ds->ops = ops; |
Andrew Lunn | 7543a6d | 2016-04-13 02:40:40 +0200 | [diff] [blame] | 363 | ds->priv = priv; |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 364 | |
| 365 | ret = dsa_switch_setup_one(ds, parent); |
| 366 | if (ret) |
Florian Fainelli | 2459534 | 2015-05-29 10:29:46 -0700 | [diff] [blame] | 367 | return ERR_PTR(ret); |
Florian Fainelli | df19719 | 2015-03-05 12:35:06 -0800 | [diff] [blame] | 368 | |
| 369 | return ds; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 372 | void dsa_cpu_dsa_destroy(struct dsa_port *port) |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 373 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 374 | struct device_node *port_dn = port->dn; |
| 375 | |
Johan Hovold | 3f65047 | 2016-11-28 19:24:55 +0100 | [diff] [blame] | 376 | if (of_phy_is_fixed_link(port_dn)) |
| 377 | of_phy_deregister_fixed_link(port_dn); |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 380 | static void dsa_switch_destroy(struct dsa_switch *ds) |
| 381 | { |
Neil Armstrong | cbc5d90 | 2015-10-06 15:40:32 +0100 | [diff] [blame] | 382 | int port; |
| 383 | |
Andrew Lunn | 3a44514 | 2016-03-12 00:01:38 +0100 | [diff] [blame] | 384 | /* Destroy network devices for physical switch ports. */ |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 385 | for (port = 0; port < ds->num_ports; port++) { |
Andrew Lunn | 74c3e2a | 2016-04-13 02:40:44 +0200 | [diff] [blame] | 386 | if (!(ds->enabled_port_mask & (1 << port))) |
Andrew Lunn | 3a44514 | 2016-03-12 00:01:38 +0100 | [diff] [blame] | 387 | continue; |
| 388 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 389 | if (!ds->ports[port].netdev) |
Andrew Lunn | 3a44514 | 2016-03-12 00:01:38 +0100 | [diff] [blame] | 390 | continue; |
| 391 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 392 | dsa_slave_destroy(ds->ports[port].netdev); |
Andrew Lunn | 3a44514 | 2016-03-12 00:01:38 +0100 | [diff] [blame] | 393 | } |
| 394 | |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 395 | /* Disable configuration of the CPU and DSA ports */ |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 396 | for (port = 0; port < ds->num_ports; port++) { |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 397 | if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port))) |
| 398 | continue; |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 399 | dsa_cpu_dsa_destroy(&ds->ports[port]); |
Andrew Lunn | 83c0afa | 2016-06-04 21:17:07 +0200 | [diff] [blame] | 400 | |
| 401 | /* Clearing a bit which is not set does no harm */ |
| 402 | ds->cpu_port_mask |= ~(1 << port); |
| 403 | ds->dsa_port_mask |= ~(1 << port); |
Neil Armstrong | cbc5d90 | 2015-10-06 15:40:32 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 406 | if (ds->slave_mii_bus && ds->ops->phy_read) |
Andrew Lunn | e755e49 | 2016-06-04 21:17:04 +0200 | [diff] [blame] | 407 | mdiobus_unregister(ds->slave_mii_bus); |
Vivien Didelot | f515f19 | 2017-02-03 13:20:20 -0500 | [diff] [blame] | 408 | |
| 409 | dsa_switch_unregister_notifier(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Thierry Reding | e506d40 | 2014-10-01 13:59:00 +0200 | [diff] [blame] | 412 | #ifdef CONFIG_PM_SLEEP |
Florian Fainelli | ea825e7 | 2016-08-18 15:30:12 -0700 | [diff] [blame] | 413 | int dsa_switch_suspend(struct dsa_switch *ds) |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 414 | { |
| 415 | int i, ret = 0; |
| 416 | |
| 417 | /* Suspend slave network devices */ |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 418 | for (i = 0; i < ds->num_ports; i++) { |
Guenter Roeck | d79d210 | 2015-02-24 23:02:02 -0800 | [diff] [blame] | 419 | if (!dsa_is_port_initialized(ds, i)) |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 420 | continue; |
| 421 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 422 | ret = dsa_slave_suspend(ds->ports[i].netdev); |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 423 | if (ret) |
| 424 | return ret; |
| 425 | } |
| 426 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 427 | if (ds->ops->suspend) |
| 428 | ret = ds->ops->suspend(ds); |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 429 | |
| 430 | return ret; |
| 431 | } |
Florian Fainelli | ea825e7 | 2016-08-18 15:30:12 -0700 | [diff] [blame] | 432 | EXPORT_SYMBOL_GPL(dsa_switch_suspend); |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 433 | |
Florian Fainelli | ea825e7 | 2016-08-18 15:30:12 -0700 | [diff] [blame] | 434 | int dsa_switch_resume(struct dsa_switch *ds) |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 435 | { |
| 436 | int i, ret = 0; |
| 437 | |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 438 | if (ds->ops->resume) |
| 439 | ret = ds->ops->resume(ds); |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 440 | |
| 441 | if (ret) |
| 442 | return ret; |
| 443 | |
| 444 | /* Resume slave network devices */ |
Vivien Didelot | 26895e2 | 2017-01-27 15:29:37 -0500 | [diff] [blame] | 445 | for (i = 0; i < ds->num_ports; i++) { |
Guenter Roeck | d79d210 | 2015-02-24 23:02:02 -0800 | [diff] [blame] | 446 | if (!dsa_is_port_initialized(ds, i)) |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 447 | continue; |
| 448 | |
Andrew Lunn | c8b0980 | 2016-06-04 21:16:57 +0200 | [diff] [blame] | 449 | ret = dsa_slave_resume(ds->ports[i].netdev); |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 450 | if (ret) |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
Florian Fainelli | ea825e7 | 2016-08-18 15:30:12 -0700 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(dsa_switch_resume); |
Thierry Reding | e506d40 | 2014-10-01 13:59:00 +0200 | [diff] [blame] | 457 | #endif |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 458 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 459 | /* platform driver init and cleanup *****************************************/ |
| 460 | static int dev_is_class(struct device *dev, void *class) |
| 461 | { |
| 462 | if (dev->class != NULL && !strcmp(dev->class->name, class)) |
| 463 | return 1; |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | static struct device *dev_find_class(struct device *parent, char *class) |
| 469 | { |
| 470 | if (dev_is_class(parent, class)) { |
| 471 | get_device(parent); |
| 472 | return parent; |
| 473 | } |
| 474 | |
| 475 | return device_find_child(parent, class, dev_is_class); |
| 476 | } |
| 477 | |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 478 | struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 479 | { |
| 480 | struct device *d; |
| 481 | |
| 482 | d = dev_find_class(dev, "mdio_bus"); |
| 483 | if (d != NULL) { |
| 484 | struct mii_bus *bus; |
| 485 | |
| 486 | bus = to_mii_bus(d); |
| 487 | put_device(d); |
| 488 | |
| 489 | return bus; |
| 490 | } |
| 491 | |
| 492 | return NULL; |
| 493 | } |
Alexander Duyck | b4d2394 | 2014-09-15 13:00:27 -0400 | [diff] [blame] | 494 | EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 495 | |
Florian Fainelli | 14b89f3 | 2017-02-04 13:02:42 -0800 | [diff] [blame] | 496 | struct net_device *dsa_dev_to_net_device(struct device *dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 497 | { |
| 498 | struct device *d; |
| 499 | |
| 500 | d = dev_find_class(dev, "net"); |
| 501 | if (d != NULL) { |
| 502 | struct net_device *nd; |
| 503 | |
| 504 | nd = to_net_dev(d); |
| 505 | dev_hold(nd); |
| 506 | put_device(d); |
| 507 | |
| 508 | return nd; |
| 509 | } |
| 510 | |
| 511 | return NULL; |
| 512 | } |
Florian Fainelli | 14b89f3 | 2017-02-04 13:02:42 -0800 | [diff] [blame] | 513 | EXPORT_SYMBOL_GPL(dsa_dev_to_net_device); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 514 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 515 | #ifdef CONFIG_OF |
| 516 | static int dsa_of_setup_routing_table(struct dsa_platform_data *pd, |
| 517 | struct dsa_chip_data *cd, |
Pavel Nakonechny | 3030381 | 2015-04-05 00:46:21 +0300 | [diff] [blame] | 518 | int chip_index, int port_index, |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 519 | struct device_node *link) |
| 520 | { |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 521 | const __be32 *reg; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 522 | int link_sw_addr; |
| 523 | struct device_node *parent_sw; |
| 524 | int len; |
| 525 | |
| 526 | parent_sw = of_get_parent(link); |
| 527 | if (!parent_sw) |
| 528 | return -EINVAL; |
| 529 | |
| 530 | reg = of_get_property(parent_sw, "reg", &len); |
| 531 | if (!reg || (len != sizeof(*reg) * 2)) |
| 532 | return -EINVAL; |
| 533 | |
Pavel Nakonechny | 3030381 | 2015-04-05 00:46:21 +0300 | [diff] [blame] | 534 | /* |
| 535 | * Get the destination switch number from the second field of its 'reg' |
| 536 | * property, i.e. for "reg = <0x19 1>" sw_addr is '1'. |
| 537 | */ |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 538 | link_sw_addr = be32_to_cpup(reg + 1); |
| 539 | |
| 540 | if (link_sw_addr >= pd->nr_chips) |
| 541 | return -EINVAL; |
| 542 | |
Pavel Nakonechny | 3030381 | 2015-04-05 00:46:21 +0300 | [diff] [blame] | 543 | cd->rtable[link_sw_addr] = port_index; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 544 | |
| 545 | return 0; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Andrew Lunn | 1e72e6f | 2015-08-17 23:52:50 +0200 | [diff] [blame] | 548 | static int dsa_of_probe_links(struct dsa_platform_data *pd, |
| 549 | struct dsa_chip_data *cd, |
| 550 | int chip_index, int port_index, |
| 551 | struct device_node *port, |
| 552 | const char *port_name) |
| 553 | { |
| 554 | struct device_node *link; |
| 555 | int link_index; |
| 556 | int ret; |
| 557 | |
| 558 | for (link_index = 0;; link_index++) { |
| 559 | link = of_parse_phandle(port, "link", link_index); |
| 560 | if (!link) |
| 561 | break; |
| 562 | |
| 563 | if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) { |
| 564 | ret = dsa_of_setup_routing_table(pd, cd, chip_index, |
| 565 | port_index, link); |
| 566 | if (ret) |
| 567 | return ret; |
| 568 | } |
| 569 | } |
| 570 | return 0; |
| 571 | } |
| 572 | |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 573 | static void dsa_of_free_platform_data(struct dsa_platform_data *pd) |
| 574 | { |
| 575 | int i; |
| 576 | int port_index; |
| 577 | |
| 578 | for (i = 0; i < pd->nr_chips; i++) { |
| 579 | port_index = 0; |
Florian Fainelli | 5f64a7d | 2013-03-25 05:03:40 +0000 | [diff] [blame] | 580 | while (port_index < DSA_MAX_PORTS) { |
Fabian Frederick | 1f74714 | 2014-06-23 19:32:47 +0200 | [diff] [blame] | 581 | kfree(pd->chip[i].port_names[port_index]); |
Florian Fainelli | 5f64a7d | 2013-03-25 05:03:40 +0000 | [diff] [blame] | 582 | port_index++; |
| 583 | } |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 584 | |
| 585 | /* Drop our reference to the MDIO bus device */ |
| 586 | if (pd->chip[i].host_dev) |
| 587 | put_device(pd->chip[i].host_dev); |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 588 | } |
| 589 | kfree(pd->chip); |
| 590 | } |
| 591 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 592 | static int dsa_of_probe(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 593 | { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 594 | struct device_node *np = dev->of_node; |
Andrew Lunn | 1e72e6f | 2015-08-17 23:52:50 +0200 | [diff] [blame] | 595 | struct device_node *child, *mdio, *ethernet, *port; |
Andrew Lunn | 6bc6d0a | 2015-08-08 17:09:14 +0200 | [diff] [blame] | 596 | struct mii_bus *mdio_bus, *mdio_bus_switch; |
Florian Fainelli | 769a020 | 2015-03-09 14:31:21 -0700 | [diff] [blame] | 597 | struct net_device *ethernet_dev; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 598 | struct dsa_platform_data *pd; |
| 599 | struct dsa_chip_data *cd; |
| 600 | const char *port_name; |
| 601 | int chip_index, port_index; |
| 602 | const unsigned int *sw_addr, *port_reg; |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 603 | u32 eeprom_len; |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 604 | int ret; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 605 | |
| 606 | mdio = of_parse_phandle(np, "dsa,mii-bus", 0); |
| 607 | if (!mdio) |
| 608 | return -EINVAL; |
| 609 | |
| 610 | mdio_bus = of_mdio_find_bus(mdio); |
| 611 | if (!mdio_bus) |
Florian Fainelli | b324c07 | 2015-03-05 12:35:05 -0800 | [diff] [blame] | 612 | return -EPROBE_DEFER; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 613 | |
| 614 | ethernet = of_parse_phandle(np, "dsa,ethernet", 0); |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 615 | if (!ethernet) { |
| 616 | ret = -EINVAL; |
| 617 | goto out_put_mdio; |
| 618 | } |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 619 | |
Florian Fainelli | 769a020 | 2015-03-09 14:31:21 -0700 | [diff] [blame] | 620 | ethernet_dev = of_find_net_device_by_node(ethernet); |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 621 | if (!ethernet_dev) { |
| 622 | ret = -EPROBE_DEFER; |
| 623 | goto out_put_mdio; |
| 624 | } |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 625 | |
| 626 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 627 | if (!pd) { |
| 628 | ret = -ENOMEM; |
Russell King | 9861f72 | 2015-09-24 20:36:33 +0100 | [diff] [blame] | 629 | goto out_put_ethernet; |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 630 | } |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 631 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 632 | dev->platform_data = pd; |
Florian Fainelli | 769a020 | 2015-03-09 14:31:21 -0700 | [diff] [blame] | 633 | pd->of_netdev = ethernet_dev; |
Tobias Waldekranz | e04449f | 2015-02-05 14:54:09 +0100 | [diff] [blame] | 634 | pd->nr_chips = of_get_available_child_count(np); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 635 | if (pd->nr_chips > DSA_MAX_SWITCHES) |
| 636 | pd->nr_chips = DSA_MAX_SWITCHES; |
| 637 | |
Fabian Frederick | 6f2aed6 | 2014-11-14 19:38:23 +0100 | [diff] [blame] | 638 | pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data), |
| 639 | GFP_KERNEL); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 640 | if (!pd->chip) { |
| 641 | ret = -ENOMEM; |
| 642 | goto out_free; |
| 643 | } |
| 644 | |
Fabian Godehardt | d1c0b47 | 2014-05-16 06:21:44 +0200 | [diff] [blame] | 645 | chip_index = -1; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 646 | for_each_available_child_of_node(np, child) { |
Vivien Didelot | d390238 | 2016-07-06 20:03:54 -0400 | [diff] [blame] | 647 | int i; |
| 648 | |
Fabian Godehardt | d1c0b47 | 2014-05-16 06:21:44 +0200 | [diff] [blame] | 649 | chip_index++; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 650 | cd = &pd->chip[chip_index]; |
| 651 | |
Florian Fainelli | fa981d9 | 2014-08-27 17:04:49 -0700 | [diff] [blame] | 652 | cd->of_node = child; |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 653 | |
Vivien Didelot | d390238 | 2016-07-06 20:03:54 -0400 | [diff] [blame] | 654 | /* Initialize the routing table */ |
| 655 | for (i = 0; i < DSA_MAX_SWITCHES; ++i) |
| 656 | cd->rtable[i] = DSA_RTABLE_NONE; |
| 657 | |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 658 | /* When assigning the host device, increment its refcount */ |
| 659 | cd->host_dev = get_device(&mdio_bus->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 660 | |
| 661 | sw_addr = of_get_property(child, "reg", NULL); |
| 662 | if (!sw_addr) |
| 663 | continue; |
| 664 | |
| 665 | cd->sw_addr = be32_to_cpup(sw_addr); |
Florian Fainelli | c8cf89f | 2015-07-11 18:02:11 -0700 | [diff] [blame] | 666 | if (cd->sw_addr >= PHY_MAX_ADDR) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 667 | continue; |
| 668 | |
Guenter Roeck | 50d4964 | 2015-04-29 10:56:15 -0700 | [diff] [blame] | 669 | if (!of_property_read_u32(child, "eeprom-length", &eeprom_len)) |
Guenter Roeck | 6793abb | 2014-10-29 10:45:01 -0700 | [diff] [blame] | 670 | cd->eeprom_len = eeprom_len; |
| 671 | |
Andrew Lunn | 6bc6d0a | 2015-08-08 17:09:14 +0200 | [diff] [blame] | 672 | mdio = of_parse_phandle(child, "mii-bus", 0); |
| 673 | if (mdio) { |
| 674 | mdio_bus_switch = of_mdio_find_bus(mdio); |
| 675 | if (!mdio_bus_switch) { |
| 676 | ret = -EPROBE_DEFER; |
| 677 | goto out_free_chip; |
| 678 | } |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 679 | |
| 680 | /* Drop the mdio_bus device ref, replacing the host |
| 681 | * device with the mdio_bus_switch device, keeping |
| 682 | * the refcount from of_mdio_find_bus() above. |
| 683 | */ |
| 684 | put_device(cd->host_dev); |
Andrew Lunn | 6bc6d0a | 2015-08-08 17:09:14 +0200 | [diff] [blame] | 685 | cd->host_dev = &mdio_bus_switch->dev; |
| 686 | } |
| 687 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 688 | for_each_available_child_of_node(child, port) { |
| 689 | port_reg = of_get_property(port, "reg", NULL); |
| 690 | if (!port_reg) |
| 691 | continue; |
| 692 | |
| 693 | port_index = be32_to_cpup(port_reg); |
Florian Fainelli | 8f5063e | 2015-07-11 18:02:10 -0700 | [diff] [blame] | 694 | if (port_index >= DSA_MAX_PORTS) |
| 695 | break; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 696 | |
| 697 | port_name = of_get_property(port, "label", NULL); |
| 698 | if (!port_name) |
| 699 | continue; |
| 700 | |
Florian Fainelli | bd47497 | 2014-08-27 17:04:50 -0700 | [diff] [blame] | 701 | cd->port_dn[port_index] = port; |
| 702 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 703 | cd->port_names[port_index] = kstrdup(port_name, |
| 704 | GFP_KERNEL); |
| 705 | if (!cd->port_names[port_index]) { |
| 706 | ret = -ENOMEM; |
| 707 | goto out_free_chip; |
| 708 | } |
| 709 | |
Andrew Lunn | 1e72e6f | 2015-08-17 23:52:50 +0200 | [diff] [blame] | 710 | ret = dsa_of_probe_links(pd, cd, chip_index, |
| 711 | port_index, port, port_name); |
| 712 | if (ret) |
| 713 | goto out_free_chip; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 714 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 718 | /* The individual chips hold their own refcount on the mdio bus, |
| 719 | * so drop ours */ |
| 720 | put_device(&mdio_bus->dev); |
| 721 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 722 | return 0; |
| 723 | |
| 724 | out_free_chip: |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 725 | dsa_of_free_platform_data(pd); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 726 | out_free: |
| 727 | kfree(pd); |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 728 | dev->platform_data = NULL; |
Russell King | 9861f72 | 2015-09-24 20:36:33 +0100 | [diff] [blame] | 729 | out_put_ethernet: |
| 730 | put_device(ðernet_dev->dev); |
Russell King | e496ae6 | 2015-09-24 20:35:57 +0100 | [diff] [blame] | 731 | out_put_mdio: |
| 732 | put_device(&mdio_bus->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 733 | return ret; |
| 734 | } |
| 735 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 736 | static void dsa_of_remove(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 737 | { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 738 | struct dsa_platform_data *pd = dev->platform_data; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 739 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 740 | if (!dev->of_node) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 741 | return; |
| 742 | |
Florian Fainelli | 2116824 | 2013-03-25 05:03:39 +0000 | [diff] [blame] | 743 | dsa_of_free_platform_data(pd); |
Russell King | 9861f72 | 2015-09-24 20:36:33 +0100 | [diff] [blame] | 744 | put_device(&pd->of_netdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 745 | kfree(pd); |
| 746 | } |
| 747 | #else |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 748 | static inline int dsa_of_probe(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 749 | { |
| 750 | return 0; |
| 751 | } |
| 752 | |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 753 | static inline void dsa_of_remove(struct device *dev) |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 754 | { |
| 755 | } |
| 756 | #endif |
| 757 | |
Neil Armstrong | 4d7f3e7 | 2015-10-06 15:40:43 +0100 | [diff] [blame] | 758 | static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev, |
| 759 | struct device *parent, struct dsa_platform_data *pd) |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 760 | { |
| 761 | int i; |
Neil Armstrong | 4d7f3e7 | 2015-10-06 15:40:43 +0100 | [diff] [blame] | 762 | unsigned configured = 0; |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 763 | |
| 764 | dst->pd = pd; |
| 765 | dst->master_netdev = dev; |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 766 | dst->cpu_port = -1; |
| 767 | |
| 768 | for (i = 0; i < pd->nr_chips; i++) { |
| 769 | struct dsa_switch *ds; |
| 770 | |
| 771 | ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev); |
| 772 | if (IS_ERR(ds)) { |
| 773 | netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", |
| 774 | i, PTR_ERR(ds)); |
| 775 | continue; |
| 776 | } |
| 777 | |
| 778 | dst->ds[i] = ds; |
Neil Armstrong | 4d7f3e7 | 2015-10-06 15:40:43 +0100 | [diff] [blame] | 779 | |
| 780 | ++configured; |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /* |
Neil Armstrong | 4d7f3e7 | 2015-10-06 15:40:43 +0100 | [diff] [blame] | 784 | * If no switch was found, exit cleanly |
| 785 | */ |
| 786 | if (!configured) |
| 787 | return -EPROBE_DEFER; |
| 788 | |
| 789 | /* |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 790 | * If we use a tagging format that doesn't have an ethertype |
| 791 | * field, make sure that all packets from this point on get |
| 792 | * sent to the tag format's receive function. |
| 793 | */ |
| 794 | wmb(); |
| 795 | dev->dsa_ptr = (void *)dst; |
| 796 | |
Neil Armstrong | 4d7f3e7 | 2015-10-06 15:40:43 +0100 | [diff] [blame] | 797 | return 0; |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 798 | } |
| 799 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 800 | static int dsa_probe(struct platform_device *pdev) |
| 801 | { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 802 | struct dsa_platform_data *pd = pdev->dev.platform_data; |
| 803 | struct net_device *dev; |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 804 | struct dsa_switch_tree *dst; |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 805 | int ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 806 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 807 | if (pdev->dev.of_node) { |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 808 | ret = dsa_of_probe(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 809 | if (ret) |
| 810 | return ret; |
| 811 | |
| 812 | pd = pdev->dev.platform_data; |
| 813 | } |
| 814 | |
Florian Fainelli | 769a020 | 2015-03-09 14:31:21 -0700 | [diff] [blame] | 815 | if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL)) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 816 | return -EINVAL; |
| 817 | |
Florian Fainelli | 769a020 | 2015-03-09 14:31:21 -0700 | [diff] [blame] | 818 | if (pd->of_netdev) { |
| 819 | dev = pd->of_netdev; |
| 820 | dev_hold(dev); |
| 821 | } else { |
Florian Fainelli | 14b89f3 | 2017-02-04 13:02:42 -0800 | [diff] [blame] | 822 | dev = dsa_dev_to_net_device(pd->netdev); |
Florian Fainelli | 769a020 | 2015-03-09 14:31:21 -0700 | [diff] [blame] | 823 | } |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 824 | if (dev == NULL) { |
Florian Fainelli | b324c07 | 2015-03-05 12:35:05 -0800 | [diff] [blame] | 825 | ret = -EPROBE_DEFER; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 826 | goto out; |
| 827 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 828 | |
| 829 | if (dev->dsa_ptr != NULL) { |
| 830 | dev_put(dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 831 | ret = -EEXIST; |
| 832 | goto out; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Neil Armstrong | d4ac35d | 2015-10-06 15:40:37 +0100 | [diff] [blame] | 835 | dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL); |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 836 | if (dst == NULL) { |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 837 | dev_put(dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 838 | ret = -ENOMEM; |
| 839 | goto out; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 842 | platform_set_drvdata(pdev, dst); |
| 843 | |
Neil Armstrong | 4d7f3e7 | 2015-10-06 15:40:43 +0100 | [diff] [blame] | 844 | ret = dsa_setup_dst(dst, dev, &pdev->dev, pd); |
Neil Armstrong | 679fb46 | 2015-12-07 13:57:34 +0100 | [diff] [blame] | 845 | if (ret) { |
| 846 | dev_put(dev); |
Neil Armstrong | 4d7f3e7 | 2015-10-06 15:40:43 +0100 | [diff] [blame] | 847 | goto out; |
Neil Armstrong | 679fb46 | 2015-12-07 13:57:34 +0100 | [diff] [blame] | 848 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 849 | |
| 850 | return 0; |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 851 | |
| 852 | out: |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 853 | dsa_of_remove(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 854 | |
| 855 | return ret; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 858 | static void dsa_remove_dst(struct dsa_switch_tree *dst) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 859 | { |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 860 | int i; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 861 | |
Neil Armstrong | 0476189 | 2016-03-08 10:36:20 +0100 | [diff] [blame] | 862 | dst->master_netdev->dsa_ptr = NULL; |
| 863 | |
| 864 | /* If we used a tagging format that doesn't have an ethertype |
| 865 | * field, make sure that all packets from this point get sent |
| 866 | * without the tag and go through the regular receive path. |
| 867 | */ |
| 868 | wmb(); |
| 869 | |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 870 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 871 | struct dsa_switch *ds = dst->ds[i]; |
| 872 | |
Neil Armstrong | d4ac35d | 2015-10-06 15:40:37 +0100 | [diff] [blame] | 873 | if (ds) |
Lennert Buytenhek | e84665c | 2009-03-20 09:52:09 +0000 | [diff] [blame] | 874 | dsa_switch_destroy(ds); |
| 875 | } |
Neil Armstrong | 679fb46 | 2015-12-07 13:57:34 +0100 | [diff] [blame] | 876 | |
Vivien Didelot | 9520ed8 | 2017-01-17 20:41:39 -0500 | [diff] [blame] | 877 | dsa_cpu_port_ethtool_restore(dst->cpu_switch); |
Florian Fainelli | 0c73c52 | 2016-06-07 16:32:42 -0700 | [diff] [blame] | 878 | |
Neil Armstrong | 679fb46 | 2015-12-07 13:57:34 +0100 | [diff] [blame] | 879 | dev_put(dst->master_netdev); |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 880 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 881 | |
Florian Fainelli | c86e59b | 2015-03-05 12:35:08 -0800 | [diff] [blame] | 882 | static int dsa_remove(struct platform_device *pdev) |
| 883 | { |
| 884 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 885 | |
| 886 | dsa_remove_dst(dst); |
Florian Fainelli | f1a26a0 | 2015-03-05 12:35:04 -0800 | [diff] [blame] | 887 | dsa_of_remove(&pdev->dev); |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 888 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | static void dsa_shutdown(struct platform_device *pdev) |
| 893 | { |
| 894 | } |
| 895 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 896 | static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev, |
| 897 | struct packet_type *pt, struct net_device *orig_dev) |
| 898 | { |
| 899 | struct dsa_switch_tree *dst = dev->dsa_ptr; |
| 900 | |
| 901 | if (unlikely(dst == NULL)) { |
| 902 | kfree_skb(skb); |
| 903 | return 0; |
| 904 | } |
| 905 | |
Alexander Duyck | 5075314 | 2014-09-15 13:00:19 -0400 | [diff] [blame] | 906 | return dst->rcv(skb, dev, pt, orig_dev); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Florian Fainelli | 61b7363 | 2014-08-29 12:42:07 -0700 | [diff] [blame] | 909 | static struct packet_type dsa_pack_type __read_mostly = { |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 910 | .type = cpu_to_be16(ETH_P_XDSA), |
| 911 | .func = dsa_switch_rcv, |
| 912 | }; |
| 913 | |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 914 | #ifdef CONFIG_PM_SLEEP |
| 915 | static int dsa_suspend(struct device *d) |
| 916 | { |
| 917 | struct platform_device *pdev = to_platform_device(d); |
| 918 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 919 | int i, ret = 0; |
| 920 | |
| 921 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 922 | struct dsa_switch *ds = dst->ds[i]; |
| 923 | |
| 924 | if (ds != NULL) |
| 925 | ret = dsa_switch_suspend(ds); |
| 926 | } |
| 927 | |
| 928 | return ret; |
| 929 | } |
| 930 | |
| 931 | static int dsa_resume(struct device *d) |
| 932 | { |
| 933 | struct platform_device *pdev = to_platform_device(d); |
| 934 | struct dsa_switch_tree *dst = platform_get_drvdata(pdev); |
| 935 | int i, ret = 0; |
| 936 | |
| 937 | for (i = 0; i < dst->pd->nr_chips; i++) { |
| 938 | struct dsa_switch *ds = dst->ds[i]; |
| 939 | |
| 940 | if (ds != NULL) |
| 941 | ret = dsa_switch_resume(ds); |
| 942 | } |
| 943 | |
| 944 | return ret; |
| 945 | } |
| 946 | #endif |
| 947 | |
| 948 | static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume); |
| 949 | |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 950 | static const struct of_device_id dsa_of_match_table[] = { |
| 951 | { .compatible = "marvell,dsa", }, |
| 952 | {} |
| 953 | }; |
| 954 | MODULE_DEVICE_TABLE(of, dsa_of_match_table); |
| 955 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 956 | static struct platform_driver dsa_driver = { |
| 957 | .probe = dsa_probe, |
| 958 | .remove = dsa_remove, |
| 959 | .shutdown = dsa_shutdown, |
| 960 | .driver = { |
| 961 | .name = "dsa", |
Florian Fainelli | 5e95329b | 2013-03-22 10:50:50 +0000 | [diff] [blame] | 962 | .of_match_table = dsa_of_match_table, |
Florian Fainelli | 2446254 | 2014-09-18 17:31:22 -0700 | [diff] [blame] | 963 | .pm = &dsa_pm_ops, |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 964 | }, |
| 965 | }; |
| 966 | |
| 967 | static int __init dsa_init_module(void) |
| 968 | { |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 969 | int rc; |
| 970 | |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 971 | rc = dsa_slave_register_notifier(); |
| 972 | if (rc) |
| 973 | return rc; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 974 | |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 975 | rc = platform_driver_register(&dsa_driver); |
| 976 | if (rc) |
| 977 | return rc; |
| 978 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 979 | dev_add_pack(&dsa_pack_type); |
| 980 | |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 981 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 982 | } |
| 983 | module_init(dsa_init_module); |
| 984 | |
| 985 | static void __exit dsa_cleanup_module(void) |
| 986 | { |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 987 | dsa_slave_unregister_notifier(); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 988 | dev_remove_pack(&dsa_pack_type); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 989 | platform_driver_unregister(&dsa_driver); |
| 990 | } |
| 991 | module_exit(dsa_cleanup_module); |
| 992 | |
Rusty Russell | 577d6a7 | 2011-01-24 14:32:52 -0600 | [diff] [blame] | 993 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 994 | MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); |
| 995 | MODULE_LICENSE("GPL"); |
| 996 | MODULE_ALIAS("platform:dsa"); |