x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / net / dsa / dsa.c
blob03c58b0eb082a5e5c125e2184bb29c80023cf017
1 /*
2 * net/dsa/dsa.c - Hardware switch handling
3 * Copyright (c) 2008-2009 Marvell Semiconductor
4 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
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/of.h>
18 #include <linux/of_mdio.h>
19 #include <linux/of_platform.h>
20 #include <linux/of_net.h>
21 #include <linux/of_gpio.h>
22 #include <linux/netdevice.h>
23 #include <linux/sysfs.h>
24 #include <linux/phy_fixed.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/etherdevice.h>
28 #include "dsa_priv.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 */
34 return skb;
37 static const struct dsa_device_ops none_ops = {
38 .xmit = dsa_slave_notag_xmit,
39 .rcv = NULL,
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,
45 #endif
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
52 #ifdef CONFIG_NET_DSA_TAG_KSZ
53 [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops,
54 #endif
55 #ifdef CONFIG_NET_DSA_TAG_LAN9303
56 [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops,
57 #endif
58 #ifdef CONFIG_NET_DSA_TAG_MTK
59 [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
60 #endif
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,
66 #endif
67 [DSA_TAG_PROTO_NONE] = &none_ops,
70 int dsa_cpu_dsa_setup(struct dsa_port *port)
72 struct device_node *port_dn = port->dn;
73 struct dsa_switch *ds = port->ds;
74 struct phy_device *phydev;
75 int ret, mode;
77 if (of_phy_is_fixed_link(port_dn)) {
78 ret = of_phy_register_fixed_link(port_dn);
79 if (ret) {
80 dev_err(ds->dev, "failed to register fixed PHY\n");
81 return ret;
83 phydev = of_phy_find_device(port_dn);
85 mode = of_get_phy_mode(port_dn);
86 if (mode < 0)
87 mode = PHY_INTERFACE_MODE_NA;
88 phydev->interface = mode;
90 genphy_config_init(phydev);
91 genphy_read_status(phydev);
92 if (ds->ops->adjust_link)
93 ds->ops->adjust_link(ds, port->index, phydev);
95 put_device(&phydev->mdio.dev);
98 return 0;
101 const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
103 const struct dsa_device_ops *ops;
105 if (tag_protocol >= DSA_TAG_LAST)
106 return ERR_PTR(-EINVAL);
107 ops = dsa_device_ops[tag_protocol];
109 if (!ops)
110 return ERR_PTR(-ENOPROTOOPT);
112 return ops;
115 int dsa_cpu_port_ethtool_setup(struct dsa_port *cpu_dp)
117 struct dsa_switch *ds = cpu_dp->ds;
118 struct net_device *master;
119 struct ethtool_ops *cpu_ops;
121 master = cpu_dp->netdev;
123 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
124 if (!cpu_ops)
125 return -ENOMEM;
127 memcpy(&cpu_dp->ethtool_ops, master->ethtool_ops,
128 sizeof(struct ethtool_ops));
129 cpu_dp->orig_ethtool_ops = master->ethtool_ops;
130 memcpy(cpu_ops, &cpu_dp->ethtool_ops,
131 sizeof(struct ethtool_ops));
132 dsa_cpu_port_ethtool_init(cpu_ops);
133 master->ethtool_ops = cpu_ops;
135 return 0;
138 void dsa_cpu_port_ethtool_restore(struct dsa_port *cpu_dp)
140 cpu_dp->netdev->ethtool_ops = cpu_dp->orig_ethtool_ops;
143 void dsa_cpu_dsa_destroy(struct dsa_port *port)
145 struct device_node *port_dn = port->dn;
147 if (of_phy_is_fixed_link(port_dn))
148 of_phy_deregister_fixed_link(port_dn);
151 static int dev_is_class(struct device *dev, void *class)
153 if (dev->class != NULL && !strcmp(dev->class->name, class))
154 return 1;
156 return 0;
159 static struct device *dev_find_class(struct device *parent, char *class)
161 if (dev_is_class(parent, class)) {
162 get_device(parent);
163 return parent;
166 return device_find_child(parent, class, dev_is_class);
169 struct net_device *dsa_dev_to_net_device(struct device *dev)
171 struct device *d;
173 d = dev_find_class(dev, "net");
174 if (d != NULL) {
175 struct net_device *nd;
177 nd = to_net_dev(d);
178 dev_hold(nd);
179 put_device(d);
181 return nd;
184 return NULL;
186 EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
188 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
189 struct packet_type *pt, struct net_device *unused)
191 struct dsa_switch_tree *dst = dev->dsa_ptr;
192 struct sk_buff *nskb = NULL;
193 struct pcpu_sw_netstats *s;
194 struct dsa_slave_priv *p;
196 if (unlikely(dst == NULL)) {
197 kfree_skb(skb);
198 return 0;
201 skb = skb_unshare(skb, GFP_ATOMIC);
202 if (!skb)
203 return 0;
205 nskb = dst->rcv(skb, dev, pt);
206 if (!nskb) {
207 kfree_skb(skb);
208 return 0;
211 skb = nskb;
212 p = netdev_priv(skb->dev);
213 skb_push(skb, ETH_HLEN);
214 skb->pkt_type = PACKET_HOST;
215 skb->protocol = eth_type_trans(skb, skb->dev);
217 s = this_cpu_ptr(p->stats64);
218 u64_stats_update_begin(&s->syncp);
219 s->rx_packets++;
220 s->rx_bytes += skb->len;
221 u64_stats_update_end(&s->syncp);
223 netif_receive_skb(skb);
225 return 0;
228 #ifdef CONFIG_PM_SLEEP
229 static bool dsa_is_port_initialized(struct dsa_switch *ds, int p)
231 return ds->enabled_port_mask & (1 << p) && ds->ports[p].netdev;
234 int dsa_switch_suspend(struct dsa_switch *ds)
236 int i, ret = 0;
238 /* Suspend slave network devices */
239 for (i = 0; i < ds->num_ports; i++) {
240 if (!dsa_is_port_initialized(ds, i))
241 continue;
243 ret = dsa_slave_suspend(ds->ports[i].netdev);
244 if (ret)
245 return ret;
248 if (ds->ops->suspend)
249 ret = ds->ops->suspend(ds);
251 return ret;
253 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
255 int dsa_switch_resume(struct dsa_switch *ds)
257 int i, ret = 0;
259 if (ds->ops->resume)
260 ret = ds->ops->resume(ds);
262 if (ret)
263 return ret;
265 /* Resume slave network devices */
266 for (i = 0; i < ds->num_ports; i++) {
267 if (!dsa_is_port_initialized(ds, i))
268 continue;
270 ret = dsa_slave_resume(ds->ports[i].netdev);
271 if (ret)
272 return ret;
275 return 0;
277 EXPORT_SYMBOL_GPL(dsa_switch_resume);
278 #endif
280 static struct packet_type dsa_pack_type __read_mostly = {
281 .type = cpu_to_be16(ETH_P_XDSA),
282 .func = dsa_switch_rcv,
285 static struct workqueue_struct *dsa_owq;
287 bool dsa_schedule_work(struct work_struct *work)
289 return queue_work(dsa_owq, work);
292 static int __init dsa_init_module(void)
294 int rc;
296 dsa_owq = alloc_ordered_workqueue("dsa_ordered",
297 WQ_MEM_RECLAIM);
298 if (!dsa_owq)
299 return -ENOMEM;
301 rc = dsa_slave_register_notifier();
302 if (rc)
303 return rc;
305 rc = dsa_legacy_register();
306 if (rc)
307 return rc;
309 dev_add_pack(&dsa_pack_type);
311 return 0;
313 module_init(dsa_init_module);
315 static void __exit dsa_cleanup_module(void)
317 dsa_slave_unregister_notifier();
318 dev_remove_pack(&dsa_pack_type);
319 dsa_legacy_unregister();
320 destroy_workqueue(dsa_owq);
322 module_exit(dsa_cleanup_module);
324 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
325 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS("platform:dsa");