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