blob: 416ac4ef9ba9c5d77f36e6902cf031628f72cc05 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/dsa.c - Hardware switch handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Florian Fainelli5e95329b2013-03-22 10:50:50 +00004 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 *
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 Roeck51579c32014-10-29 10:44:58 -070012#include <linux/device.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000013#include <linux/list.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040016#include <linux/module.h>
Florian Fainelli5e95329b2013-03-22 10:50:50 +000017#include <linux/of.h>
18#include <linux/of_mdio.h>
19#include <linux/of_platform.h>
Florian Fainelli769a0202015-03-09 14:31:21 -070020#include <linux/of_net.h>
Andrew Lunncc30c162015-11-20 03:56:23 +010021#include <linux/of_gpio.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020022#include <linux/netdevice.h>
Guenter Roeck51579c32014-10-29 10:44:58 -070023#include <linux/sysfs.h>
Neil Armstrongcbc5d902015-10-06 15:40:32 +010024#include <linux/phy_fixed.h>
Arnd Bergmann85beabf2015-11-24 12:34:49 +010025#include <linux/gpio/consumer.h>
Florian Fainellia86d8be2017-04-08 08:55:23 -070026#include <linux/etherdevice.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040027
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000028#include "dsa_priv.h"
29
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020030static 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
37static const struct dsa_device_ops none_ops = {
38 .xmit = dsa_slave_notag_xmit,
39 .rcv = NULL,
40};
41
42const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
Andrew Lunneb7b7212017-05-16 22:40:07 +020043#ifdef CONFIG_NET_DSA_TAG_BRCM
44 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
45#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020046#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 Huh8b8010f2017-05-31 20:19:06 +000052#ifdef CONFIG_NET_DSA_TAG_KSZ
53 [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
54#endif
Andrew Lunneb7b7212017-05-16 22:40:07 +020055#ifdef CONFIG_NET_DSA_TAG_LAN9303
56 [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
John Crispincafdc452016-09-15 16:26:40 +020057#endif
Sean Wang5cd89852017-04-07 16:45:06 +080058#ifdef CONFIG_NET_DSA_TAG_MTK
59 [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
60#endif
Andrew Lunneb7b7212017-05-16 22:40:07 +020061#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 Beiserte8fe1772017-04-18 10:48:24 +020066#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020067 [DSA_TAG_PROTO_NONE] = &none_ops,
68};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000069
Andrew Lunn9b8e8952016-06-04 21:17:01 +020070int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
Florian Fainelli293784a2017-01-26 10:45:52 -080071 struct dsa_port *dport, int port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +020072{
Florian Fainelli293784a2017-01-26 10:45:52 -080073 struct device_node *port_dn = dport->dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +020074 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) {
80 dev_err(dev, "failed to register fixed PHY\n");
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 Didelot9d490b42016-08-23 12:38:56 -040092 if (ds->ops->adjust_link)
93 ds->ops->adjust_link(ds, port, phydev);
Johan Hovoldfd05d7b2016-11-24 19:21:27 +010094
95 put_device(&phydev->mdio.dev);
Andrew Lunn9b8e8952016-06-04 21:17:01 +020096 }
97
98 return 0;
99}
100
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200101const 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 Fainelli937c7df2017-06-02 12:31:21 -0700115int dsa_cpu_port_ethtool_setup(struct dsa_port *cpu_dp)
Florian Fainelli0c73c522016-06-07 16:32:42 -0700116{
Florian Fainelli937c7df2017-06-02 12:31:21 -0700117 struct dsa_switch *ds = cpu_dp->ds;
Florian Fainelli0c73c522016-06-07 16:32:42 -0700118 struct net_device *master;
119 struct ethtool_ops *cpu_ops;
120
Florian Fainelli67dbb9d42017-06-13 13:27:20 -0700121 master = cpu_dp->netdev;
122
Florian Fainelli0c73c522016-06-07 16:32:42 -0700123 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
124 if (!cpu_ops)
125 return -ENOMEM;
126
Florian Fainelli67dbb9d42017-06-13 13:27:20 -0700127 memcpy(&cpu_dp->ethtool_ops, master->ethtool_ops,
Florian Fainelli0c73c522016-06-07 16:32:42 -0700128 sizeof(struct ethtool_ops));
Florian Fainelli67dbb9d42017-06-13 13:27:20 -0700129 cpu_dp->orig_ethtool_ops = master->ethtool_ops;
130 memcpy(cpu_ops, &cpu_dp->ethtool_ops,
Florian Fainelli0c73c522016-06-07 16:32:42 -0700131 sizeof(struct ethtool_ops));
132 dsa_cpu_port_ethtool_init(cpu_ops);
133 master->ethtool_ops = cpu_ops;
134
135 return 0;
136}
137
Florian Fainelli937c7df2017-06-02 12:31:21 -0700138void dsa_cpu_port_ethtool_restore(struct dsa_port *cpu_dp)
Florian Fainelli0c73c522016-06-07 16:32:42 -0700139{
Florian Fainelli67dbb9d42017-06-13 13:27:20 -0700140 cpu_dp->netdev->ethtool_ops = cpu_dp->orig_ethtool_ops;
Florian Fainelli0c73c522016-06-07 16:32:42 -0700141}
142
Florian Fainelli293784a2017-01-26 10:45:52 -0800143void dsa_cpu_dsa_destroy(struct dsa_port *port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200144{
Florian Fainelli293784a2017-01-26 10:45:52 -0800145 struct device_node *port_dn = port->dn;
146
Johan Hovold3f650472016-11-28 19:24:55 +0100147 if (of_phy_is_fixed_link(port_dn))
148 of_phy_deregister_fixed_link(port_dn);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200149}
150
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000151static int dev_is_class(struct device *dev, void *class)
152{
153 if (dev->class != NULL && !strcmp(dev->class->name, class))
154 return 1;
155
156 return 0;
157}
158
159static struct device *dev_find_class(struct device *parent, char *class)
160{
161 if (dev_is_class(parent, class)) {
162 get_device(parent);
163 return parent;
164 }
165
166 return device_find_child(parent, class, dev_is_class);
167}
168
Florian Fainelli14b89f32017-02-04 13:02:42 -0800169struct net_device *dsa_dev_to_net_device(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000170{
171 struct device *d;
172
173 d = dev_find_class(dev, "net");
174 if (d != NULL) {
175 struct net_device *nd;
176
177 nd = to_net_dev(d);
178 dev_hold(nd);
179 put_device(d);
180
181 return nd;
182 }
183
184 return NULL;
185}
Florian Fainelli14b89f32017-02-04 13:02:42 -0800186EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000187
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700188static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
189 struct packet_type *pt, struct net_device *orig_dev)
190{
191 struct dsa_switch_tree *dst = dev->dsa_ptr;
Florian Fainellia86d8be2017-04-08 08:55:23 -0700192 struct sk_buff *nskb = NULL;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700193
194 if (unlikely(dst == NULL)) {
195 kfree_skb(skb);
196 return 0;
197 }
198
Florian Fainelli16c5dcb2017-04-08 08:55:22 -0700199 skb = skb_unshare(skb, GFP_ATOMIC);
200 if (!skb)
201 return 0;
202
Florian Fainellia86d8be2017-04-08 08:55:23 -0700203 nskb = dst->rcv(skb, dev, pt, orig_dev);
204 if (!nskb) {
205 kfree_skb(skb);
206 return 0;
207 }
208
209 skb = nskb;
210 skb_push(skb, ETH_HLEN);
211 skb->pkt_type = PACKET_HOST;
212 skb->protocol = eth_type_trans(skb, skb->dev);
213
214 skb->dev->stats.rx_packets++;
215 skb->dev->stats.rx_bytes += skb->len;
216
217 netif_receive_skb(skb);
218
219 return 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700220}
221
Florian Fainelliac2629a2017-06-01 19:53:04 -0700222#ifdef CONFIG_PM_SLEEP
223int dsa_switch_suspend(struct dsa_switch *ds)
224{
225 int i, ret = 0;
226
227 /* Suspend slave network devices */
228 for (i = 0; i < ds->num_ports; i++) {
229 if (!dsa_is_port_initialized(ds, i))
230 continue;
231
232 ret = dsa_slave_suspend(ds->ports[i].netdev);
233 if (ret)
234 return ret;
235 }
236
237 if (ds->ops->suspend)
238 ret = ds->ops->suspend(ds);
239
240 return ret;
241}
242EXPORT_SYMBOL_GPL(dsa_switch_suspend);
243
244int dsa_switch_resume(struct dsa_switch *ds)
245{
246 int i, ret = 0;
247
248 if (ds->ops->resume)
249 ret = ds->ops->resume(ds);
250
251 if (ret)
252 return ret;
253
254 /* Resume slave network devices */
255 for (i = 0; i < ds->num_ports; i++) {
256 if (!dsa_is_port_initialized(ds, i))
257 continue;
258
259 ret = dsa_slave_resume(ds->ports[i].netdev);
260 if (ret)
261 return ret;
262 }
263
264 return 0;
265}
266EXPORT_SYMBOL_GPL(dsa_switch_resume);
267#endif
268
Florian Fainelli61b73632014-08-29 12:42:07 -0700269static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700270 .type = cpu_to_be16(ETH_P_XDSA),
271 .func = dsa_switch_rcv,
272};
273
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000274static int __init dsa_init_module(void)
275{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000276 int rc;
277
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500278 rc = dsa_slave_register_notifier();
279 if (rc)
280 return rc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800281
Vivien Didelota6a71f12017-04-12 12:45:03 -0400282 rc = dsa_legacy_register();
Ben Hutchings7df899c2011-11-25 14:35:02 +0000283 if (rc)
284 return rc;
285
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700286 dev_add_pack(&dsa_pack_type);
287
Ben Hutchings7df899c2011-11-25 14:35:02 +0000288 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000289}
290module_init(dsa_init_module);
291
292static void __exit dsa_cleanup_module(void)
293{
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500294 dsa_slave_unregister_notifier();
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700295 dev_remove_pack(&dsa_pack_type);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400296 dsa_legacy_unregister();
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000297}
298module_exit(dsa_cleanup_module);
299
Rusty Russell577d6a72011-01-24 14:32:52 -0600300MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000301MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
302MODULE_LICENSE("GPL");
303MODULE_ALIAS("platform:dsa");