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> |
Florian Fainelli | a86d8be | 2017-04-08 08:55:23 -0700 | [diff] [blame] | 26 | #include <linux/etherdevice.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 27 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 28 | #include "dsa_priv.h" |
| 29 | |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 30 | static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb, |
| 31 | struct net_device *dev) |
| 32 | { |
| 33 | /* Just return the original SKB */ |
| 34 | return skb; |
| 35 | } |
| 36 | |
| 37 | static const struct dsa_device_ops none_ops = { |
| 38 | .xmit = dsa_slave_notag_xmit, |
| 39 | .rcv = NULL, |
| 40 | }; |
| 41 | |
| 42 | const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = { |
Andrew Lunn | eb7b721 | 2017-05-16 22:40:07 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 44 | [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops, |
| 45 | #endif |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 46 | #ifdef CONFIG_NET_DSA_TAG_DSA |
| 47 | [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops, |
| 48 | #endif |
| 49 | #ifdef CONFIG_NET_DSA_TAG_EDSA |
| 50 | [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops, |
| 51 | #endif |
Woojung Huh | 8b8010f | 2017-05-31 20:19:06 +0000 | [diff] [blame] | 52 | #ifdef CONFIG_NET_DSA_TAG_KSZ |
| 53 | [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops, |
| 54 | #endif |
Andrew Lunn | eb7b721 | 2017-05-16 22:40:07 +0200 | [diff] [blame] | 55 | #ifdef CONFIG_NET_DSA_TAG_LAN9303 |
| 56 | [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops, |
John Crispin | cafdc45 | 2016-09-15 16:26:40 +0200 | [diff] [blame] | 57 | #endif |
Sean Wang | 5cd8985 | 2017-04-07 16:45:06 +0800 | [diff] [blame] | 58 | #ifdef CONFIG_NET_DSA_TAG_MTK |
| 59 | [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops, |
| 60 | #endif |
Andrew Lunn | eb7b721 | 2017-05-16 22:40:07 +0200 | [diff] [blame] | 61 | #ifdef CONFIG_NET_DSA_TAG_QCA |
| 62 | [DSA_TAG_PROTO_QCA] = &qca_netdev_ops, |
| 63 | #endif |
| 64 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
| 65 | [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops, |
Juergen Beisert | e8fe177 | 2017-04-18 10:48:24 +0200 | [diff] [blame] | 66 | #endif |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 67 | [DSA_TAG_PROTO_NONE] = &none_ops, |
| 68 | }; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 69 | |
Vivien Didelot | 47d0dcc | 2017-08-05 16:20:18 -0400 | [diff] [blame] | 70 | int dsa_cpu_dsa_setup(struct dsa_port *port) |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 71 | { |
Vivien Didelot | 47d0dcc | 2017-08-05 16:20:18 -0400 | [diff] [blame] | 72 | struct device_node *port_dn = port->dn; |
| 73 | struct dsa_switch *ds = port->ds; |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 74 | struct phy_device *phydev; |
| 75 | int ret, mode; |
| 76 | |
| 77 | if (of_phy_is_fixed_link(port_dn)) { |
| 78 | ret = of_phy_register_fixed_link(port_dn); |
| 79 | if (ret) { |
Vivien Didelot | 47d0dcc | 2017-08-05 16:20:18 -0400 | [diff] [blame] | 80 | dev_err(ds->dev, "failed to register fixed PHY\n"); |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 81 | return ret; |
| 82 | } |
| 83 | phydev = of_phy_find_device(port_dn); |
| 84 | |
| 85 | mode = of_get_phy_mode(port_dn); |
| 86 | if (mode < 0) |
| 87 | mode = PHY_INTERFACE_MODE_NA; |
| 88 | phydev->interface = mode; |
| 89 | |
| 90 | genphy_config_init(phydev); |
| 91 | genphy_read_status(phydev); |
Vivien Didelot | 9d490b4 | 2016-08-23 12:38:56 -0400 | [diff] [blame] | 92 | if (ds->ops->adjust_link) |
Vivien Didelot | 47d0dcc | 2017-08-05 16:20:18 -0400 | [diff] [blame] | 93 | ds->ops->adjust_link(ds, port->index, phydev); |
Johan Hovold | fd05d7b | 2016-11-24 19:21:27 +0100 | [diff] [blame] | 94 | |
| 95 | put_device(&phydev->mdio.dev); |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
Andrew Lunn | 39a7f2a | 2016-06-04 21:17:03 +0200 | [diff] [blame] | 101 | const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol) |
| 102 | { |
| 103 | const struct dsa_device_ops *ops; |
| 104 | |
| 105 | if (tag_protocol >= DSA_TAG_LAST) |
| 106 | return ERR_PTR(-EINVAL); |
| 107 | ops = dsa_device_ops[tag_protocol]; |
| 108 | |
| 109 | if (!ops) |
| 110 | return ERR_PTR(-ENOPROTOOPT); |
| 111 | |
| 112 | return ops; |
| 113 | } |
| 114 | |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 115 | void dsa_cpu_dsa_destroy(struct dsa_port *port) |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 116 | { |
Florian Fainelli | 293784a | 2017-01-26 10:45:52 -0800 | [diff] [blame] | 117 | struct device_node *port_dn = port->dn; |
| 118 | |
Johan Hovold | 3f65047 | 2016-11-28 19:24:55 +0100 | [diff] [blame] | 119 | if (of_phy_is_fixed_link(port_dn)) |
| 120 | of_phy_deregister_fixed_link(port_dn); |
Andrew Lunn | 9b8e895 | 2016-06-04 21:17:01 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 123 | static int dev_is_class(struct device *dev, void *class) |
| 124 | { |
| 125 | if (dev->class != NULL && !strcmp(dev->class->name, class)) |
| 126 | return 1; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static struct device *dev_find_class(struct device *parent, char *class) |
| 132 | { |
| 133 | if (dev_is_class(parent, class)) { |
| 134 | get_device(parent); |
| 135 | return parent; |
| 136 | } |
| 137 | |
| 138 | return device_find_child(parent, class, dev_is_class); |
| 139 | } |
| 140 | |
Florian Fainelli | 14b89f3 | 2017-02-04 13:02:42 -0800 | [diff] [blame] | 141 | struct net_device *dsa_dev_to_net_device(struct device *dev) |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 142 | { |
| 143 | struct device *d; |
| 144 | |
| 145 | d = dev_find_class(dev, "net"); |
| 146 | if (d != NULL) { |
| 147 | struct net_device *nd; |
| 148 | |
| 149 | nd = to_net_dev(d); |
| 150 | dev_hold(nd); |
| 151 | put_device(d); |
| 152 | |
| 153 | return nd; |
| 154 | } |
| 155 | |
| 156 | return NULL; |
| 157 | } |
Florian Fainelli | 14b89f3 | 2017-02-04 13:02:42 -0800 | [diff] [blame] | 158 | EXPORT_SYMBOL_GPL(dsa_dev_to_net_device); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 159 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 160 | static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev, |
Florian Westphal | 89e4950 | 2017-08-17 16:47:00 +0200 | [diff] [blame] | 161 | struct packet_type *pt, struct net_device *unused) |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 162 | { |
Vivien Didelot | 2f657a6 | 2017-09-29 17:19:20 -0400 | [diff] [blame] | 163 | struct dsa_port *cpu_dp = dev->dsa_ptr; |
Florian Fainelli | a86d8be | 2017-04-08 08:55:23 -0700 | [diff] [blame] | 164 | struct sk_buff *nskb = NULL; |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 165 | struct pcpu_sw_netstats *s; |
Florian Fainelli | f613ed6 | 2017-08-01 15:00:36 -0700 | [diff] [blame] | 166 | struct dsa_slave_priv *p; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 167 | |
Vivien Didelot | 2f657a6 | 2017-09-29 17:19:20 -0400 | [diff] [blame] | 168 | if (unlikely(!cpu_dp)) { |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 169 | kfree_skb(skb); |
| 170 | return 0; |
| 171 | } |
| 172 | |
Florian Fainelli | 16c5dcb | 2017-04-08 08:55:22 -0700 | [diff] [blame] | 173 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 174 | if (!skb) |
| 175 | return 0; |
| 176 | |
Vivien Didelot | 2f657a6 | 2017-09-29 17:19:20 -0400 | [diff] [blame] | 177 | nskb = cpu_dp->rcv(skb, dev, pt); |
Florian Fainelli | a86d8be | 2017-04-08 08:55:23 -0700 | [diff] [blame] | 178 | if (!nskb) { |
| 179 | kfree_skb(skb); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | skb = nskb; |
Florian Fainelli | f613ed6 | 2017-08-01 15:00:36 -0700 | [diff] [blame] | 184 | p = netdev_priv(skb->dev); |
Florian Fainelli | a86d8be | 2017-04-08 08:55:23 -0700 | [diff] [blame] | 185 | skb_push(skb, ETH_HLEN); |
| 186 | skb->pkt_type = PACKET_HOST; |
| 187 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 188 | |
Florian Fainelli | 5f6b4e14 | 2017-08-03 21:33:27 -0700 | [diff] [blame] | 189 | s = this_cpu_ptr(p->stats64); |
| 190 | u64_stats_update_begin(&s->syncp); |
| 191 | s->rx_packets++; |
| 192 | s->rx_bytes += skb->len; |
| 193 | u64_stats_update_end(&s->syncp); |
Florian Fainelli | a86d8be | 2017-04-08 08:55:23 -0700 | [diff] [blame] | 194 | |
| 195 | netif_receive_skb(skb); |
| 196 | |
| 197 | return 0; |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Florian Fainelli | ac2629a | 2017-06-01 19:53:04 -0700 | [diff] [blame] | 200 | #ifdef CONFIG_PM_SLEEP |
Vivien Didelot | e7d53ad | 2017-07-18 16:23:56 -0400 | [diff] [blame] | 201 | static bool dsa_is_port_initialized(struct dsa_switch *ds, int p) |
| 202 | { |
| 203 | return ds->enabled_port_mask & (1 << p) && ds->ports[p].netdev; |
| 204 | } |
| 205 | |
Florian Fainelli | ac2629a | 2017-06-01 19:53:04 -0700 | [diff] [blame] | 206 | int dsa_switch_suspend(struct dsa_switch *ds) |
| 207 | { |
| 208 | int i, ret = 0; |
| 209 | |
| 210 | /* Suspend slave network devices */ |
| 211 | for (i = 0; i < ds->num_ports; i++) { |
| 212 | if (!dsa_is_port_initialized(ds, i)) |
| 213 | continue; |
| 214 | |
| 215 | ret = dsa_slave_suspend(ds->ports[i].netdev); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | if (ds->ops->suspend) |
| 221 | ret = ds->ops->suspend(ds); |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(dsa_switch_suspend); |
| 226 | |
| 227 | int dsa_switch_resume(struct dsa_switch *ds) |
| 228 | { |
| 229 | int i, ret = 0; |
| 230 | |
| 231 | if (ds->ops->resume) |
| 232 | ret = ds->ops->resume(ds); |
| 233 | |
| 234 | if (ret) |
| 235 | return ret; |
| 236 | |
| 237 | /* Resume slave network devices */ |
| 238 | for (i = 0; i < ds->num_ports; i++) { |
| 239 | if (!dsa_is_port_initialized(ds, i)) |
| 240 | continue; |
| 241 | |
| 242 | ret = dsa_slave_resume(ds->ports[i].netdev); |
| 243 | if (ret) |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(dsa_switch_resume); |
| 250 | #endif |
| 251 | |
Florian Fainelli | 61b7363 | 2014-08-29 12:42:07 -0700 | [diff] [blame] | 252 | static struct packet_type dsa_pack_type __read_mostly = { |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 253 | .type = cpu_to_be16(ETH_P_XDSA), |
| 254 | .func = dsa_switch_rcv, |
| 255 | }; |
| 256 | |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 257 | static struct workqueue_struct *dsa_owq; |
| 258 | |
| 259 | bool dsa_schedule_work(struct work_struct *work) |
| 260 | { |
| 261 | return queue_work(dsa_owq, work); |
| 262 | } |
| 263 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 264 | static int __init dsa_init_module(void) |
| 265 | { |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 266 | int rc; |
| 267 | |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 268 | dsa_owq = alloc_ordered_workqueue("dsa_ordered", |
| 269 | WQ_MEM_RECLAIM); |
| 270 | if (!dsa_owq) |
| 271 | return -ENOMEM; |
| 272 | |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 273 | rc = dsa_slave_register_notifier(); |
| 274 | if (rc) |
| 275 | return rc; |
Florian Fainelli | b73adef | 2015-02-24 13:15:33 -0800 | [diff] [blame] | 276 | |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 277 | rc = dsa_legacy_register(); |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 278 | if (rc) |
| 279 | return rc; |
| 280 | |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 281 | dev_add_pack(&dsa_pack_type); |
| 282 | |
Ben Hutchings | 7df899c | 2011-11-25 14:35:02 +0000 | [diff] [blame] | 283 | return 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 284 | } |
| 285 | module_init(dsa_init_module); |
| 286 | |
| 287 | static void __exit dsa_cleanup_module(void) |
| 288 | { |
Vivien Didelot | 88e4f0c | 2017-02-03 13:20:16 -0500 | [diff] [blame] | 289 | dsa_slave_unregister_notifier(); |
Florian Fainelli | 3e8a72d | 2014-08-27 17:04:46 -0700 | [diff] [blame] | 290 | dev_remove_pack(&dsa_pack_type); |
Vivien Didelot | a6a71f1 | 2017-04-12 12:45:03 -0400 | [diff] [blame] | 291 | dsa_legacy_unregister(); |
Arkadi Sharshevsky | c9eb3e0 | 2017-08-06 16:15:42 +0300 | [diff] [blame] | 292 | destroy_workqueue(dsa_owq); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 293 | } |
| 294 | module_exit(dsa_cleanup_module); |
| 295 | |
Rusty Russell | 577d6a7 | 2011-01-24 14:32:52 -0600 | [diff] [blame] | 296 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 297 | MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); |
| 298 | MODULE_LICENSE("GPL"); |
| 299 | MODULE_ALIAS("platform:dsa"); |