blob: 6a9d0f50fbeea1ed5394564f2223aec0547b2348 [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 Fainelli60724d42017-10-11 10:57:48 -070017#include <linux/notifier.h>
Florian Fainelli5e95329b2013-03-22 10:50:50 +000018#include <linux/of.h>
19#include <linux/of_mdio.h>
20#include <linux/of_platform.h>
Florian Fainelli769a0202015-03-09 14:31:21 -070021#include <linux/of_net.h>
Andrew Lunncc30c162015-11-20 03:56:23 +010022#include <linux/of_gpio.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020023#include <linux/netdevice.h>
Guenter Roeck51579c32014-10-29 10:44:58 -070024#include <linux/sysfs.h>
Neil Armstrongcbc5d902015-10-06 15:40:32 +010025#include <linux/phy_fixed.h>
Arnd Bergmann85beabf2015-11-24 12:34:49 +010026#include <linux/gpio/consumer.h>
Florian Fainellia86d8be2017-04-08 08:55:23 -070027#include <linux/etherdevice.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040028
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000029#include "dsa_priv.h"
30
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020031static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
32 struct net_device *dev)
33{
34 /* Just return the original SKB */
35 return skb;
36}
37
38static const struct dsa_device_ops none_ops = {
39 .xmit = dsa_slave_notag_xmit,
40 .rcv = NULL,
41};
42
43const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
Andrew Lunneb7b7212017-05-16 22:40:07 +020044#ifdef CONFIG_NET_DSA_TAG_BRCM
45 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
46#endif
Florian Fainellib74b70c2017-11-10 15:22:54 -080047#ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND
48 [DSA_TAG_PROTO_BRCM_PREPEND] = &brcm_prepend_netdev_ops,
49#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020050#ifdef CONFIG_NET_DSA_TAG_DSA
51 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
52#endif
53#ifdef CONFIG_NET_DSA_TAG_EDSA
54 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
55#endif
Woojung Huh8b8010f2017-05-31 20:19:06 +000056#ifdef CONFIG_NET_DSA_TAG_KSZ
57 [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
58#endif
Andrew Lunneb7b7212017-05-16 22:40:07 +020059#ifdef CONFIG_NET_DSA_TAG_LAN9303
60 [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
John Crispincafdc452016-09-15 16:26:40 +020061#endif
Sean Wang5cd89852017-04-07 16:45:06 +080062#ifdef CONFIG_NET_DSA_TAG_MTK
63 [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
64#endif
Andrew Lunneb7b7212017-05-16 22:40:07 +020065#ifdef CONFIG_NET_DSA_TAG_QCA
66 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
67#endif
68#ifdef CONFIG_NET_DSA_TAG_TRAILER
69 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
Juergen Beiserte8fe1772017-04-18 10:48:24 +020070#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020071 [DSA_TAG_PROTO_NONE] = &none_ops,
72};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000073
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020074const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
75{
76 const struct dsa_device_ops *ops;
77
78 if (tag_protocol >= DSA_TAG_LAST)
79 return ERR_PTR(-EINVAL);
80 ops = dsa_device_ops[tag_protocol];
81
82 if (!ops)
83 return ERR_PTR(-ENOPROTOOPT);
84
85 return ops;
86}
87
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000088static int dev_is_class(struct device *dev, void *class)
89{
90 if (dev->class != NULL && !strcmp(dev->class->name, class))
91 return 1;
92
93 return 0;
94}
95
96static struct device *dev_find_class(struct device *parent, char *class)
97{
98 if (dev_is_class(parent, class)) {
99 get_device(parent);
100 return parent;
101 }
102
103 return device_find_child(parent, class, dev_is_class);
104}
105
Florian Fainelli14b89f32017-02-04 13:02:42 -0800106struct net_device *dsa_dev_to_net_device(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000107{
108 struct device *d;
109
110 d = dev_find_class(dev, "net");
111 if (d != NULL) {
112 struct net_device *nd;
113
114 nd = to_net_dev(d);
115 dev_hold(nd);
116 put_device(d);
117
118 return nd;
119 }
120
121 return NULL;
122}
Florian Fainelli14b89f32017-02-04 13:02:42 -0800123EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000124
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700125static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
Florian Westphal89e49502017-08-17 16:47:00 +0200126 struct packet_type *pt, struct net_device *unused)
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700127{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400128 struct dsa_port *cpu_dp = dev->dsa_ptr;
Florian Fainellia86d8be2017-04-08 08:55:23 -0700129 struct sk_buff *nskb = NULL;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700130 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700131 struct dsa_slave_priv *p;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700132
Vivien Didelot2f657a62017-09-29 17:19:20 -0400133 if (unlikely(!cpu_dp)) {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700134 kfree_skb(skb);
135 return 0;
136 }
137
Florian Fainelli16c5dcb2017-04-08 08:55:22 -0700138 skb = skb_unshare(skb, GFP_ATOMIC);
139 if (!skb)
140 return 0;
141
Vivien Didelot2f657a62017-09-29 17:19:20 -0400142 nskb = cpu_dp->rcv(skb, dev, pt);
Florian Fainellia86d8be2017-04-08 08:55:23 -0700143 if (!nskb) {
144 kfree_skb(skb);
145 return 0;
146 }
147
148 skb = nskb;
Florian Fainellif613ed62017-08-01 15:00:36 -0700149 p = netdev_priv(skb->dev);
Florian Fainellia86d8be2017-04-08 08:55:23 -0700150 skb_push(skb, ETH_HLEN);
151 skb->pkt_type = PACKET_HOST;
152 skb->protocol = eth_type_trans(skb, skb->dev);
153
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700154 s = this_cpu_ptr(p->stats64);
155 u64_stats_update_begin(&s->syncp);
156 s->rx_packets++;
157 s->rx_bytes += skb->len;
158 u64_stats_update_end(&s->syncp);
Florian Fainellia86d8be2017-04-08 08:55:23 -0700159
160 netif_receive_skb(skb);
161
162 return 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700163}
164
Florian Fainelliac2629a2017-06-01 19:53:04 -0700165#ifdef CONFIG_PM_SLEEP
Vivien Didelote7d53ad2017-07-18 16:23:56 -0400166static bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
167{
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400168 return dsa_is_user_port(ds, p) && ds->ports[p].slave;
Vivien Didelote7d53ad2017-07-18 16:23:56 -0400169}
170
Florian Fainelliac2629a2017-06-01 19:53:04 -0700171int dsa_switch_suspend(struct dsa_switch *ds)
172{
173 int i, ret = 0;
174
175 /* Suspend slave network devices */
176 for (i = 0; i < ds->num_ports; i++) {
177 if (!dsa_is_port_initialized(ds, i))
178 continue;
179
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400180 ret = dsa_slave_suspend(ds->ports[i].slave);
Florian Fainelliac2629a2017-06-01 19:53:04 -0700181 if (ret)
182 return ret;
183 }
184
185 if (ds->ops->suspend)
186 ret = ds->ops->suspend(ds);
187
188 return ret;
189}
190EXPORT_SYMBOL_GPL(dsa_switch_suspend);
191
192int dsa_switch_resume(struct dsa_switch *ds)
193{
194 int i, ret = 0;
195
196 if (ds->ops->resume)
197 ret = ds->ops->resume(ds);
198
199 if (ret)
200 return ret;
201
202 /* Resume slave network devices */
203 for (i = 0; i < ds->num_ports; i++) {
204 if (!dsa_is_port_initialized(ds, i))
205 continue;
206
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400207 ret = dsa_slave_resume(ds->ports[i].slave);
Florian Fainelliac2629a2017-06-01 19:53:04 -0700208 if (ret)
209 return ret;
210 }
211
212 return 0;
213}
214EXPORT_SYMBOL_GPL(dsa_switch_resume);
215#endif
216
Florian Fainelli61b73632014-08-29 12:42:07 -0700217static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700218 .type = cpu_to_be16(ETH_P_XDSA),
219 .func = dsa_switch_rcv,
220};
221
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +0300222static struct workqueue_struct *dsa_owq;
223
224bool dsa_schedule_work(struct work_struct *work)
225{
226 return queue_work(dsa_owq, work);
227}
228
Florian Fainelli60724d42017-10-11 10:57:48 -0700229static ATOMIC_NOTIFIER_HEAD(dsa_notif_chain);
230
231int register_dsa_notifier(struct notifier_block *nb)
232{
233 return atomic_notifier_chain_register(&dsa_notif_chain, nb);
234}
235EXPORT_SYMBOL_GPL(register_dsa_notifier);
236
237int unregister_dsa_notifier(struct notifier_block *nb)
238{
239 return atomic_notifier_chain_unregister(&dsa_notif_chain, nb);
240}
241EXPORT_SYMBOL_GPL(unregister_dsa_notifier);
242
243int call_dsa_notifiers(unsigned long val, struct net_device *dev,
244 struct dsa_notifier_info *info)
245{
246 info->dev = dev;
247 return atomic_notifier_call_chain(&dsa_notif_chain, val, info);
248}
249EXPORT_SYMBOL_GPL(call_dsa_notifiers);
250
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000251static int __init dsa_init_module(void)
252{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000253 int rc;
254
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +0300255 dsa_owq = alloc_ordered_workqueue("dsa_ordered",
256 WQ_MEM_RECLAIM);
257 if (!dsa_owq)
258 return -ENOMEM;
259
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500260 rc = dsa_slave_register_notifier();
261 if (rc)
262 return rc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800263
Vivien Didelota6a71f12017-04-12 12:45:03 -0400264 rc = dsa_legacy_register();
Ben Hutchings7df899c2011-11-25 14:35:02 +0000265 if (rc)
266 return rc;
267
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700268 dev_add_pack(&dsa_pack_type);
269
Ben Hutchings7df899c2011-11-25 14:35:02 +0000270 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000271}
272module_init(dsa_init_module);
273
274static void __exit dsa_cleanup_module(void)
275{
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500276 dsa_slave_unregister_notifier();
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700277 dev_remove_pack(&dsa_pack_type);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400278 dsa_legacy_unregister();
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +0300279 destroy_workqueue(dsa_owq);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000280}
281module_exit(dsa_cleanup_module);
282
Rusty Russell577d6a72011-01-24 14:32:52 -0600283MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000284MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
285MODULE_LICENSE("GPL");
286MODULE_ALIAS("platform:dsa");