blob: 9f3209ff7ffde754230720ca9117111fdbd7c3bc [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 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>
Brandon Streiff90af1052018-02-14 01:07:49 +010025#include <linux/ptp_classify.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
Florian Fainellib74b70c2017-11-10 15:22:54 -080046#ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND
47 [DSA_TAG_PROTO_BRCM_PREPEND] = &brcm_prepend_netdev_ops,
48#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020049#ifdef CONFIG_NET_DSA_TAG_DSA
50 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
51#endif
52#ifdef CONFIG_NET_DSA_TAG_EDSA
53 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
54#endif
Woojung Huh8b8010f2017-05-31 20:19:06 +000055#ifdef CONFIG_NET_DSA_TAG_KSZ
56 [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
57#endif
Andrew Lunneb7b7212017-05-16 22:40:07 +020058#ifdef CONFIG_NET_DSA_TAG_LAN9303
59 [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
John Crispincafdc452016-09-15 16:26:40 +020060#endif
Sean Wang5cd89852017-04-07 16:45:06 +080061#ifdef CONFIG_NET_DSA_TAG_MTK
62 [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
63#endif
Andrew Lunneb7b7212017-05-16 22:40:07 +020064#ifdef CONFIG_NET_DSA_TAG_QCA
65 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
66#endif
67#ifdef CONFIG_NET_DSA_TAG_TRAILER
68 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
Juergen Beiserte8fe1772017-04-18 10:48:24 +020069#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020070 [DSA_TAG_PROTO_NONE] = &none_ops,
71};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020073const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
74{
75 const struct dsa_device_ops *ops;
76
77 if (tag_protocol >= DSA_TAG_LAST)
78 return ERR_PTR(-EINVAL);
79 ops = dsa_device_ops[tag_protocol];
80
81 if (!ops)
82 return ERR_PTR(-ENOPROTOOPT);
83
84 return ops;
85}
86
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000087static int dev_is_class(struct device *dev, void *class)
88{
89 if (dev->class != NULL && !strcmp(dev->class->name, class))
90 return 1;
91
92 return 0;
93}
94
95static struct device *dev_find_class(struct device *parent, char *class)
96{
97 if (dev_is_class(parent, class)) {
98 get_device(parent);
99 return parent;
100 }
101
102 return device_find_child(parent, class, dev_is_class);
103}
104
Florian Fainelli14b89f32017-02-04 13:02:42 -0800105struct net_device *dsa_dev_to_net_device(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000106{
107 struct device *d;
108
109 d = dev_find_class(dev, "net");
110 if (d != NULL) {
111 struct net_device *nd;
112
113 nd = to_net_dev(d);
114 dev_hold(nd);
115 put_device(d);
116
117 return nd;
118 }
119
120 return NULL;
121}
Florian Fainelli14b89f32017-02-04 13:02:42 -0800122EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000123
Brandon Streiff90af1052018-02-14 01:07:49 +0100124/* Determine if we should defer delivery of skb until we have a rx timestamp.
125 *
126 * Called from dsa_switch_rcv. For now, this will only work if tagging is
127 * enabled on the switch. Normally the MAC driver would retrieve the hardware
128 * timestamp when it reads the packet out of the hardware. However in a DSA
129 * switch, the DSA driver owning the interface to which the packet is
130 * delivered is never notified unless we do so here.
131 */
132static bool dsa_skb_defer_rx_timestamp(struct dsa_slave_priv *p,
133 struct sk_buff *skb)
134{
135 struct dsa_switch *ds = p->dp->ds;
136 unsigned int type;
137
138 if (skb_headroom(skb) < ETH_HLEN)
139 return false;
140
141 __skb_push(skb, ETH_HLEN);
142
143 type = ptp_classify_raw(skb);
144
145 __skb_pull(skb, ETH_HLEN);
146
147 if (type == PTP_CLASS_NONE)
148 return false;
149
150 if (likely(ds->ops->port_rxtstamp))
151 return ds->ops->port_rxtstamp(ds, p->dp->index, skb, type);
152
153 return false;
154}
155
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700156static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
Florian Westphal89e49502017-08-17 16:47:00 +0200157 struct packet_type *pt, struct net_device *unused)
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700158{
Vivien Didelot2f657a62017-09-29 17:19:20 -0400159 struct dsa_port *cpu_dp = dev->dsa_ptr;
Florian Fainellia86d8be2017-04-08 08:55:23 -0700160 struct sk_buff *nskb = NULL;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700161 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700162 struct dsa_slave_priv *p;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700163
Vivien Didelot2f657a62017-09-29 17:19:20 -0400164 if (unlikely(!cpu_dp)) {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700165 kfree_skb(skb);
166 return 0;
167 }
168
Florian Fainelli16c5dcb2017-04-08 08:55:22 -0700169 skb = skb_unshare(skb, GFP_ATOMIC);
170 if (!skb)
171 return 0;
172
Vivien Didelot2f657a62017-09-29 17:19:20 -0400173 nskb = cpu_dp->rcv(skb, dev, pt);
Florian Fainellia86d8be2017-04-08 08:55:23 -0700174 if (!nskb) {
175 kfree_skb(skb);
176 return 0;
177 }
178
179 skb = nskb;
Florian Fainellif613ed62017-08-01 15:00:36 -0700180 p = netdev_priv(skb->dev);
Florian Fainellia86d8be2017-04-08 08:55:23 -0700181 skb_push(skb, ETH_HLEN);
182 skb->pkt_type = PACKET_HOST;
183 skb->protocol = eth_type_trans(skb, skb->dev);
184
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700185 s = this_cpu_ptr(p->stats64);
186 u64_stats_update_begin(&s->syncp);
187 s->rx_packets++;
188 s->rx_bytes += skb->len;
189 u64_stats_update_end(&s->syncp);
Florian Fainellia86d8be2017-04-08 08:55:23 -0700190
Brandon Streiff90af1052018-02-14 01:07:49 +0100191 if (dsa_skb_defer_rx_timestamp(p, skb))
192 return 0;
193
Florian Fainellia86d8be2017-04-08 08:55:23 -0700194 netif_receive_skb(skb);
195
196 return 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700197}
198
Florian Fainelliac2629a2017-06-01 19:53:04 -0700199#ifdef CONFIG_PM_SLEEP
Vivien Didelote7d53ad2017-07-18 16:23:56 -0400200static bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
201{
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400202 return dsa_is_user_port(ds, p) && ds->ports[p].slave;
Vivien Didelote7d53ad2017-07-18 16:23:56 -0400203}
204
Florian Fainelliac2629a2017-06-01 19:53:04 -0700205int dsa_switch_suspend(struct dsa_switch *ds)
206{
207 int i, ret = 0;
208
209 /* Suspend slave network devices */
210 for (i = 0; i < ds->num_ports; i++) {
211 if (!dsa_is_port_initialized(ds, i))
212 continue;
213
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400214 ret = dsa_slave_suspend(ds->ports[i].slave);
Florian Fainelliac2629a2017-06-01 19:53:04 -0700215 if (ret)
216 return ret;
217 }
218
219 if (ds->ops->suspend)
220 ret = ds->ops->suspend(ds);
221
222 return ret;
223}
224EXPORT_SYMBOL_GPL(dsa_switch_suspend);
225
226int dsa_switch_resume(struct dsa_switch *ds)
227{
228 int i, ret = 0;
229
230 if (ds->ops->resume)
231 ret = ds->ops->resume(ds);
232
233 if (ret)
234 return ret;
235
236 /* Resume slave network devices */
237 for (i = 0; i < ds->num_ports; i++) {
238 if (!dsa_is_port_initialized(ds, i))
239 continue;
240
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400241 ret = dsa_slave_resume(ds->ports[i].slave);
Florian Fainelliac2629a2017-06-01 19:53:04 -0700242 if (ret)
243 return ret;
244 }
245
246 return 0;
247}
248EXPORT_SYMBOL_GPL(dsa_switch_resume);
249#endif
250
Florian Fainelli61b73632014-08-29 12:42:07 -0700251static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700252 .type = cpu_to_be16(ETH_P_XDSA),
253 .func = dsa_switch_rcv,
254};
255
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +0300256static struct workqueue_struct *dsa_owq;
257
258bool dsa_schedule_work(struct work_struct *work)
259{
260 return queue_work(dsa_owq, work);
261}
262
Florian Fainelli60724d42017-10-11 10:57:48 -0700263static ATOMIC_NOTIFIER_HEAD(dsa_notif_chain);
264
265int register_dsa_notifier(struct notifier_block *nb)
266{
267 return atomic_notifier_chain_register(&dsa_notif_chain, nb);
268}
269EXPORT_SYMBOL_GPL(register_dsa_notifier);
270
271int unregister_dsa_notifier(struct notifier_block *nb)
272{
273 return atomic_notifier_chain_unregister(&dsa_notif_chain, nb);
274}
275EXPORT_SYMBOL_GPL(unregister_dsa_notifier);
276
277int call_dsa_notifiers(unsigned long val, struct net_device *dev,
278 struct dsa_notifier_info *info)
279{
280 info->dev = dev;
281 return atomic_notifier_call_chain(&dsa_notif_chain, val, info);
282}
283EXPORT_SYMBOL_GPL(call_dsa_notifiers);
284
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000285static int __init dsa_init_module(void)
286{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000287 int rc;
288
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +0300289 dsa_owq = alloc_ordered_workqueue("dsa_ordered",
290 WQ_MEM_RECLAIM);
291 if (!dsa_owq)
292 return -ENOMEM;
293
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500294 rc = dsa_slave_register_notifier();
295 if (rc)
296 return rc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800297
Vivien Didelota6a71f12017-04-12 12:45:03 -0400298 rc = dsa_legacy_register();
Ben Hutchings7df899c2011-11-25 14:35:02 +0000299 if (rc)
300 return rc;
301
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700302 dev_add_pack(&dsa_pack_type);
303
Ben Hutchings7df899c2011-11-25 14:35:02 +0000304 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000305}
306module_init(dsa_init_module);
307
308static void __exit dsa_cleanup_module(void)
309{
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500310 dsa_slave_unregister_notifier();
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700311 dev_remove_pack(&dsa_pack_type);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400312 dsa_legacy_unregister();
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +0300313 destroy_workqueue(dsa_owq);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000314}
315module_exit(dsa_cleanup_module);
316
Rusty Russell577d6a72011-01-24 14:32:52 -0600317MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000318MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
319MODULE_LICENSE("GPL");
320MODULE_ALIAS("platform:dsa");