blob: e117047174fc5b368d7341f7e56b515ace3a39b9 [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>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020027#include <net/dsa.h>
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] = {
43#ifdef CONFIG_NET_DSA_TAG_DSA
44 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
45#endif
46#ifdef CONFIG_NET_DSA_TAG_EDSA
47 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
48#endif
49#ifdef CONFIG_NET_DSA_TAG_TRAILER
50 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
51#endif
52#ifdef CONFIG_NET_DSA_TAG_BRCM
53 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
54#endif
John Crispincafdc452016-09-15 16:26:40 +020055#ifdef CONFIG_NET_DSA_TAG_QCA
56 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
57#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 Lunn39a7f2a2016-06-04 21:17:03 +020061 [DSA_TAG_PROTO_NONE] = &none_ops,
62};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000063
Andrew Lunn9b8e8952016-06-04 21:17:01 +020064int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
Florian Fainelli293784a2017-01-26 10:45:52 -080065 struct dsa_port *dport, int port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +020066{
Florian Fainelli293784a2017-01-26 10:45:52 -080067 struct device_node *port_dn = dport->dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +020068 struct phy_device *phydev;
69 int ret, mode;
70
71 if (of_phy_is_fixed_link(port_dn)) {
72 ret = of_phy_register_fixed_link(port_dn);
73 if (ret) {
74 dev_err(dev, "failed to register fixed PHY\n");
75 return ret;
76 }
77 phydev = of_phy_find_device(port_dn);
78
79 mode = of_get_phy_mode(port_dn);
80 if (mode < 0)
81 mode = PHY_INTERFACE_MODE_NA;
82 phydev->interface = mode;
83
84 genphy_config_init(phydev);
85 genphy_read_status(phydev);
Vivien Didelot9d490b42016-08-23 12:38:56 -040086 if (ds->ops->adjust_link)
87 ds->ops->adjust_link(ds, port, phydev);
Johan Hovoldfd05d7b2016-11-24 19:21:27 +010088
89 put_device(&phydev->mdio.dev);
Andrew Lunn9b8e8952016-06-04 21:17:01 +020090 }
91
92 return 0;
93}
94
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020095const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
96{
97 const struct dsa_device_ops *ops;
98
99 if (tag_protocol >= DSA_TAG_LAST)
100 return ERR_PTR(-EINVAL);
101 ops = dsa_device_ops[tag_protocol];
102
103 if (!ops)
104 return ERR_PTR(-ENOPROTOOPT);
105
106 return ops;
107}
108
Florian Fainelli0c73c522016-06-07 16:32:42 -0700109int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
110{
111 struct net_device *master;
112 struct ethtool_ops *cpu_ops;
113
114 master = ds->dst->master_netdev;
115 if (ds->master_netdev)
116 master = ds->master_netdev;
117
118 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
119 if (!cpu_ops)
120 return -ENOMEM;
121
122 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
123 sizeof(struct ethtool_ops));
124 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
125 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
126 sizeof(struct ethtool_ops));
127 dsa_cpu_port_ethtool_init(cpu_ops);
128 master->ethtool_ops = cpu_ops;
129
130 return 0;
131}
132
133void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
134{
135 struct net_device *master;
136
137 master = ds->dst->master_netdev;
138 if (ds->master_netdev)
139 master = ds->master_netdev;
140
141 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
142}
143
Florian Fainelli293784a2017-01-26 10:45:52 -0800144void dsa_cpu_dsa_destroy(struct dsa_port *port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200145{
Florian Fainelli293784a2017-01-26 10:45:52 -0800146 struct device_node *port_dn = port->dn;
147
Johan Hovold3f650472016-11-28 19:24:55 +0100148 if (of_phy_is_fixed_link(port_dn))
149 of_phy_deregister_fixed_link(port_dn);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200150}
151
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152static int dev_is_class(struct device *dev, void *class)
153{
154 if (dev->class != NULL && !strcmp(dev->class->name, class))
155 return 1;
156
157 return 0;
158}
159
160static struct device *dev_find_class(struct device *parent, char *class)
161{
162 if (dev_is_class(parent, class)) {
163 get_device(parent);
164 return parent;
165 }
166
167 return device_find_child(parent, class, dev_is_class);
168}
169
Florian Fainelli14b89f32017-02-04 13:02:42 -0800170struct net_device *dsa_dev_to_net_device(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000171{
172 struct device *d;
173
174 d = dev_find_class(dev, "net");
175 if (d != NULL) {
176 struct net_device *nd;
177
178 nd = to_net_dev(d);
179 dev_hold(nd);
180 put_device(d);
181
182 return nd;
183 }
184
185 return NULL;
186}
Florian Fainelli14b89f32017-02-04 13:02:42 -0800187EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000188
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700189static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
190 struct packet_type *pt, struct net_device *orig_dev)
191{
192 struct dsa_switch_tree *dst = dev->dsa_ptr;
Florian Fainellia86d8be2017-04-08 08:55:23 -0700193 struct sk_buff *nskb = NULL;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700194
195 if (unlikely(dst == NULL)) {
196 kfree_skb(skb);
197 return 0;
198 }
199
Florian Fainelli16c5dcb2017-04-08 08:55:22 -0700200 skb = skb_unshare(skb, GFP_ATOMIC);
201 if (!skb)
202 return 0;
203
Florian Fainellia86d8be2017-04-08 08:55:23 -0700204 nskb = dst->rcv(skb, dev, pt, orig_dev);
205 if (!nskb) {
206 kfree_skb(skb);
207 return 0;
208 }
209
210 skb = nskb;
211 skb_push(skb, ETH_HLEN);
212 skb->pkt_type = PACKET_HOST;
213 skb->protocol = eth_type_trans(skb, skb->dev);
214
215 skb->dev->stats.rx_packets++;
216 skb->dev->stats.rx_bytes += skb->len;
217
218 netif_receive_skb(skb);
219
220 return 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700221}
222
Florian Fainelli61b73632014-08-29 12:42:07 -0700223static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700224 .type = cpu_to_be16(ETH_P_XDSA),
225 .func = dsa_switch_rcv,
226};
227
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000228static int __init dsa_init_module(void)
229{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000230 int rc;
231
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500232 rc = dsa_slave_register_notifier();
233 if (rc)
234 return rc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800235
Vivien Didelota6a71f12017-04-12 12:45:03 -0400236 rc = dsa_legacy_register();
Ben Hutchings7df899c2011-11-25 14:35:02 +0000237 if (rc)
238 return rc;
239
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700240 dev_add_pack(&dsa_pack_type);
241
Ben Hutchings7df899c2011-11-25 14:35:02 +0000242 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000243}
244module_init(dsa_init_module);
245
246static void __exit dsa_cleanup_module(void)
247{
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500248 dsa_slave_unregister_notifier();
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700249 dev_remove_pack(&dsa_pack_type);
Vivien Didelota6a71f12017-04-12 12:45:03 -0400250 dsa_legacy_unregister();
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000251}
252module_exit(dsa_cleanup_module);
253
Rusty Russell577d6a72011-01-24 14:32:52 -0600254MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000255MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
256MODULE_LICENSE("GPL");
257MODULE_ALIAS("platform:dsa");