2 * net/dsa/dsa.c - Hardware switch handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
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.
12 #include <linux/device.h>
13 #include <linux/list.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/notifier.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_platform.h>
21 #include <linux/of_net.h>
22 #include <linux/netdevice.h>
23 #include <linux/sysfs.h>
24 #include <linux/phy_fixed.h>
25 #include <linux/ptp_classify.h>
26 #include <linux/etherdevice.h>
30 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
31 struct net_device *dev)
33 /* Just return the original SKB */
37 static const struct dsa_device_ops none_ops = {
38 .xmit = dsa_slave_notag_xmit,
42 const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
43 #ifdef CONFIG_NET_DSA_TAG_BRCM
44 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
46 #ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND
47 [DSA_TAG_PROTO_BRCM_PREPEND] = &brcm_prepend_netdev_ops,
49 #ifdef CONFIG_NET_DSA_TAG_DSA
50 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
52 #ifdef CONFIG_NET_DSA_TAG_EDSA
53 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
55 #ifdef CONFIG_NET_DSA_TAG_GSWIP
56 [DSA_TAG_PROTO_GSWIP] = &gswip_netdev_ops,
58 #ifdef CONFIG_NET_DSA_TAG_KSZ9477
59 [DSA_TAG_PROTO_KSZ9477] = &ksz9477_netdev_ops,
60 [DSA_TAG_PROTO_KSZ9893] = &ksz9893_netdev_ops,
62 #ifdef CONFIG_NET_DSA_TAG_LAN9303
63 [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
65 #ifdef CONFIG_NET_DSA_TAG_MTK
66 [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
68 #ifdef CONFIG_NET_DSA_TAG_QCA
69 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
71 #ifdef CONFIG_NET_DSA_TAG_TRAILER
72 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
74 [DSA_TAG_PROTO_NONE] = &none_ops,
77 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops)
79 const char *protocol_name[DSA_TAG_LAST] = {
80 #ifdef CONFIG_NET_DSA_TAG_BRCM
81 [DSA_TAG_PROTO_BRCM] = "brcm",
83 #ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND
84 [DSA_TAG_PROTO_BRCM_PREPEND] = "brcm-prepend",
86 #ifdef CONFIG_NET_DSA_TAG_DSA
87 [DSA_TAG_PROTO_DSA] = "dsa",
89 #ifdef CONFIG_NET_DSA_TAG_EDSA
90 [DSA_TAG_PROTO_EDSA] = "edsa",
92 #ifdef CONFIG_NET_DSA_TAG_GSWIP
93 [DSA_TAG_PROTO_GSWIP] = "gswip",
95 #ifdef CONFIG_NET_DSA_TAG_KSZ9477
96 [DSA_TAG_PROTO_KSZ9477] = "ksz9477",
97 [DSA_TAG_PROTO_KSZ9893] = "ksz9893",
99 #ifdef CONFIG_NET_DSA_TAG_LAN9303
100 [DSA_TAG_PROTO_LAN9303] = "lan9303",
102 #ifdef CONFIG_NET_DSA_TAG_MTK
103 [DSA_TAG_PROTO_MTK] = "mtk",
105 #ifdef CONFIG_NET_DSA_TAG_QCA
106 [DSA_TAG_PROTO_QCA] = "qca",
108 #ifdef CONFIG_NET_DSA_TAG_TRAILER
109 [DSA_TAG_PROTO_TRAILER] = "trailer",
111 [DSA_TAG_PROTO_NONE] = "none",
115 BUILD_BUG_ON(ARRAY_SIZE(protocol_name) != DSA_TAG_LAST);
117 for (i = 0; i < ARRAY_SIZE(dsa_device_ops); i++)
118 if (ops == dsa_device_ops[i])
119 return protocol_name[i];
121 return protocol_name[DSA_TAG_PROTO_NONE];
124 const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
126 const struct dsa_device_ops *ops;
128 if (tag_protocol >= DSA_TAG_LAST)
129 return ERR_PTR(-EINVAL);
130 ops = dsa_device_ops[tag_protocol];
133 return ERR_PTR(-ENOPROTOOPT);
138 static int dev_is_class(struct device *dev, void *class)
140 if (dev->class != NULL && !strcmp(dev->class->name, class))
146 static struct device *dev_find_class(struct device *parent, char *class)
148 if (dev_is_class(parent, class)) {
153 return device_find_child(parent, class, dev_is_class);
156 struct net_device *dsa_dev_to_net_device(struct device *dev)
160 d = dev_find_class(dev, "net");
162 struct net_device *nd;
173 EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
175 /* Determine if we should defer delivery of skb until we have a rx timestamp.
177 * Called from dsa_switch_rcv. For now, this will only work if tagging is
178 * enabled on the switch. Normally the MAC driver would retrieve the hardware
179 * timestamp when it reads the packet out of the hardware. However in a DSA
180 * switch, the DSA driver owning the interface to which the packet is
181 * delivered is never notified unless we do so here.
183 static bool dsa_skb_defer_rx_timestamp(struct dsa_slave_priv *p,
186 struct dsa_switch *ds = p->dp->ds;
189 if (skb_headroom(skb) < ETH_HLEN)
192 __skb_push(skb, ETH_HLEN);
194 type = ptp_classify_raw(skb);
196 __skb_pull(skb, ETH_HLEN);
198 if (type == PTP_CLASS_NONE)
201 if (likely(ds->ops->port_rxtstamp))
202 return ds->ops->port_rxtstamp(ds, p->dp->index, skb, type);
207 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
208 struct packet_type *pt, struct net_device *unused)
210 struct dsa_port *cpu_dp = dev->dsa_ptr;
211 struct sk_buff *nskb = NULL;
212 struct pcpu_sw_netstats *s;
213 struct dsa_slave_priv *p;
215 if (unlikely(!cpu_dp)) {
220 skb = skb_unshare(skb, GFP_ATOMIC);
224 nskb = cpu_dp->rcv(skb, dev, pt);
231 p = netdev_priv(skb->dev);
232 skb_push(skb, ETH_HLEN);
233 skb->pkt_type = PACKET_HOST;
234 skb->protocol = eth_type_trans(skb, skb->dev);
236 s = this_cpu_ptr(p->stats64);
237 u64_stats_update_begin(&s->syncp);
239 s->rx_bytes += skb->len;
240 u64_stats_update_end(&s->syncp);
242 if (dsa_skb_defer_rx_timestamp(p, skb))
245 netif_receive_skb(skb);
250 #ifdef CONFIG_PM_SLEEP
251 static bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
253 return dsa_is_user_port(ds, p) && ds->ports[p].slave;
256 int dsa_switch_suspend(struct dsa_switch *ds)
260 /* Suspend slave network devices */
261 for (i = 0; i < ds->num_ports; i++) {
262 if (!dsa_is_port_initialized(ds, i))
265 ret = dsa_slave_suspend(ds->ports[i].slave);
270 if (ds->ops->suspend)
271 ret = ds->ops->suspend(ds);
275 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
277 int dsa_switch_resume(struct dsa_switch *ds)
282 ret = ds->ops->resume(ds);
287 /* Resume slave network devices */
288 for (i = 0; i < ds->num_ports; i++) {
289 if (!dsa_is_port_initialized(ds, i))
292 ret = dsa_slave_resume(ds->ports[i].slave);
299 EXPORT_SYMBOL_GPL(dsa_switch_resume);
302 static struct packet_type dsa_pack_type __read_mostly = {
303 .type = cpu_to_be16(ETH_P_XDSA),
304 .func = dsa_switch_rcv,
307 static struct workqueue_struct *dsa_owq;
309 bool dsa_schedule_work(struct work_struct *work)
311 return queue_work(dsa_owq, work);
314 static ATOMIC_NOTIFIER_HEAD(dsa_notif_chain);
316 int register_dsa_notifier(struct notifier_block *nb)
318 return atomic_notifier_chain_register(&dsa_notif_chain, nb);
320 EXPORT_SYMBOL_GPL(register_dsa_notifier);
322 int unregister_dsa_notifier(struct notifier_block *nb)
324 return atomic_notifier_chain_unregister(&dsa_notif_chain, nb);
326 EXPORT_SYMBOL_GPL(unregister_dsa_notifier);
328 int call_dsa_notifiers(unsigned long val, struct net_device *dev,
329 struct dsa_notifier_info *info)
332 return atomic_notifier_call_chain(&dsa_notif_chain, val, info);
334 EXPORT_SYMBOL_GPL(call_dsa_notifiers);
336 static int __init dsa_init_module(void)
340 dsa_owq = alloc_ordered_workqueue("dsa_ordered",
345 rc = dsa_slave_register_notifier();
349 rc = dsa_legacy_register();
353 dev_add_pack(&dsa_pack_type);
357 module_init(dsa_init_module);
359 static void __exit dsa_cleanup_module(void)
361 dsa_slave_unregister_notifier();
362 dev_remove_pack(&dsa_pack_type);
363 dsa_legacy_unregister();
364 destroy_workqueue(dsa_owq);
366 module_exit(dsa_cleanup_module);
369 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
370 MODULE_LICENSE("GPL");
371 MODULE_ALIAS("platform:dsa");