]>
Commit | Line | Data |
---|---|---|
91da11f8 LB |
1 | /* |
2 | * net/dsa/dsa.c - Hardware switch handling | |
e84665c9 | 3 | * Copyright (c) 2008-2009 Marvell Semiconductor |
5e95329b | 4 | * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org> |
91da11f8 LB |
5 | * |
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 | ||
51579c3f | 12 | #include <linux/device.h> |
91da11f8 | 13 | #include <linux/list.h> |
91da11f8 | 14 | #include <linux/platform_device.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
3a9a231d | 16 | #include <linux/module.h> |
5e95329b FF |
17 | #include <linux/of.h> |
18 | #include <linux/of_mdio.h> | |
19 | #include <linux/of_platform.h> | |
769a0202 | 20 | #include <linux/of_net.h> |
cc30c163 | 21 | #include <linux/of_gpio.h> |
c6e970a0 | 22 | #include <linux/netdevice.h> |
51579c3f | 23 | #include <linux/sysfs.h> |
cbc5d90b | 24 | #include <linux/phy_fixed.h> |
85beabfe | 25 | #include <linux/gpio/consumer.h> |
a86d8bec | 26 | #include <linux/etherdevice.h> |
ea5dd34b | 27 | |
91da11f8 LB |
28 | #include "dsa_priv.h" |
29 | ||
39a7f2a4 AL |
30 | static 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 | ||
37 | static const struct dsa_device_ops none_ops = { | |
38 | .xmit = dsa_slave_notag_xmit, | |
39 | .rcv = NULL, | |
40 | }; | |
41 | ||
42 | const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = { | |
eb7b7211 AL |
43 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
44 | [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops, | |
45 | #endif | |
39a7f2a4 AL |
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 | |
8b8010fb WH |
52 | #ifdef CONFIG_NET_DSA_TAG_KSZ |
53 | [DSA_TAG_PROTO_KSZ] = &ksz_netdev_ops, | |
54 | #endif | |
eb7b7211 AL |
55 | #ifdef CONFIG_NET_DSA_TAG_LAN9303 |
56 | [DSA_TAG_PROTO_LAN9303] = &lan9303_netdev_ops, | |
39a7f2a4 | 57 | #endif |
eb7b7211 AL |
58 | #ifdef CONFIG_NET_DSA_TAG_MTK |
59 | [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops, | |
cafdc45c JC |
60 | #endif |
61 | #ifdef CONFIG_NET_DSA_TAG_QCA | |
62 | [DSA_TAG_PROTO_QCA] = &qca_netdev_ops, | |
5cd8985a | 63 | #endif |
eb7b7211 AL |
64 | #ifdef CONFIG_NET_DSA_TAG_TRAILER |
65 | [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops, | |
39a7f2a4 AL |
66 | #endif |
67 | [DSA_TAG_PROTO_NONE] = &none_ops, | |
68 | }; | |
91da11f8 | 69 | |
9b8e895c | 70 | int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev, |
293784a8 | 71 | struct dsa_port *dport, int port) |
39b0c705 | 72 | { |
293784a8 | 73 | struct device_node *port_dn = dport->dn; |
39b0c705 | 74 | struct phy_device *phydev; |
9b8e895c AL |
75 | int ret, mode; |
76 | ||
77 | if (of_phy_is_fixed_link(port_dn)) { | |
78 | ret = of_phy_register_fixed_link(port_dn); | |
79 | if (ret) { | |
80 | dev_err(dev, "failed to register fixed PHY\n"); | |
81 | return ret; | |
82 | } | |
83 | phydev = of_phy_find_device(port_dn); | |
84 | ||
85 | mode = of_get_phy_mode(port_dn); | |
86 | if (mode < 0) | |
87 | mode = PHY_INTERFACE_MODE_NA; | |
88 | phydev->interface = mode; | |
89 | ||
90 | genphy_config_init(phydev); | |
91 | genphy_read_status(phydev); | |
9d490b4e VD |
92 | if (ds->ops->adjust_link) |
93 | ds->ops->adjust_link(ds, port, phydev); | |
fd05d7b1 JH |
94 | |
95 | put_device(&phydev->mdio.dev); | |
9b8e895c AL |
96 | } |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
39a7f2a4 AL |
101 | const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol) |
102 | { | |
103 | const struct dsa_device_ops *ops; | |
104 | ||
105 | if (tag_protocol >= DSA_TAG_LAST) | |
106 | return ERR_PTR(-EINVAL); | |
107 | ops = dsa_device_ops[tag_protocol]; | |
108 | ||
109 | if (!ops) | |
110 | return ERR_PTR(-ENOPROTOOPT); | |
111 | ||
112 | return ops; | |
113 | } | |
114 | ||
937c7df8 | 115 | int dsa_cpu_port_ethtool_setup(struct dsa_port *cpu_dp) |
0c73c523 | 116 | { |
937c7df8 | 117 | struct dsa_switch *ds = cpu_dp->ds; |
0c73c523 FF |
118 | struct net_device *master; |
119 | struct ethtool_ops *cpu_ops; | |
120 | ||
67dbb9d4 FF |
121 | master = cpu_dp->netdev; |
122 | ||
0c73c523 FF |
123 | cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL); |
124 | if (!cpu_ops) | |
125 | return -ENOMEM; | |
126 | ||
67dbb9d4 | 127 | memcpy(&cpu_dp->ethtool_ops, master->ethtool_ops, |
0c73c523 | 128 | sizeof(struct ethtool_ops)); |
67dbb9d4 FF |
129 | cpu_dp->orig_ethtool_ops = master->ethtool_ops; |
130 | memcpy(cpu_ops, &cpu_dp->ethtool_ops, | |
0c73c523 FF |
131 | sizeof(struct ethtool_ops)); |
132 | dsa_cpu_port_ethtool_init(cpu_ops); | |
133 | master->ethtool_ops = cpu_ops; | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
937c7df8 | 138 | void dsa_cpu_port_ethtool_restore(struct dsa_port *cpu_dp) |
0c73c523 | 139 | { |
67dbb9d4 | 140 | cpu_dp->netdev->ethtool_ops = cpu_dp->orig_ethtool_ops; |
0c73c523 FF |
141 | } |
142 | ||
293784a8 | 143 | void dsa_cpu_dsa_destroy(struct dsa_port *port) |
91da11f8 | 144 | { |
293784a8 FF |
145 | struct device_node *port_dn = port->dn; |
146 | ||
3f65047c JH |
147 | if (of_phy_is_fixed_link(port_dn)) |
148 | of_phy_deregister_fixed_link(port_dn); | |
9b8e895c AL |
149 | } |
150 | ||
91da11f8 LB |
151 | static int dev_is_class(struct device *dev, void *class) |
152 | { | |
153 | if (dev->class != NULL && !strcmp(dev->class->name, class)) | |
154 | return 1; | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | static struct device *dev_find_class(struct device *parent, char *class) | |
160 | { | |
161 | if (dev_is_class(parent, class)) { | |
162 | get_device(parent); | |
163 | return parent; | |
164 | } | |
165 | ||
166 | return device_find_child(parent, class, dev_is_class); | |
167 | } | |
168 | ||
14b89f36 | 169 | struct net_device *dsa_dev_to_net_device(struct device *dev) |
91da11f8 LB |
170 | { |
171 | struct device *d; | |
172 | ||
173 | d = dev_find_class(dev, "net"); | |
174 | if (d != NULL) { | |
175 | struct net_device *nd; | |
176 | ||
177 | nd = to_net_dev(d); | |
178 | dev_hold(nd); | |
179 | put_device(d); | |
180 | ||
181 | return nd; | |
182 | } | |
183 | ||
184 | return NULL; | |
185 | } | |
14b89f36 | 186 | EXPORT_SYMBOL_GPL(dsa_dev_to_net_device); |
91da11f8 | 187 | |
3e8a72d1 FF |
188 | static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev, |
189 | struct packet_type *pt, struct net_device *orig_dev) | |
190 | { | |
191 | struct dsa_switch_tree *dst = dev->dsa_ptr; | |
a86d8bec | 192 | struct sk_buff *nskb = NULL; |
3e8a72d1 FF |
193 | |
194 | if (unlikely(dst == NULL)) { | |
195 | kfree_skb(skb); | |
196 | return 0; | |
197 | } | |
198 | ||
16c5dcb1 FF |
199 | skb = skb_unshare(skb, GFP_ATOMIC); |
200 | if (!skb) | |
201 | return 0; | |
202 | ||
a86d8bec FF |
203 | nskb = dst->rcv(skb, dev, pt, orig_dev); |
204 | if (!nskb) { | |
205 | kfree_skb(skb); | |
206 | return 0; | |
207 | } | |
208 | ||
209 | skb = nskb; | |
210 | skb_push(skb, ETH_HLEN); | |
211 | skb->pkt_type = PACKET_HOST; | |
212 | skb->protocol = eth_type_trans(skb, skb->dev); | |
213 | ||
214 | skb->dev->stats.rx_packets++; | |
215 | skb->dev->stats.rx_bytes += skb->len; | |
216 | ||
217 | netif_receive_skb(skb); | |
218 | ||
219 | return 0; | |
3e8a72d1 FF |
220 | } |
221 | ||
ac2629a4 FF |
222 | #ifdef CONFIG_PM_SLEEP |
223 | int dsa_switch_suspend(struct dsa_switch *ds) | |
224 | { | |
225 | int i, ret = 0; | |
226 | ||
227 | /* Suspend slave network devices */ | |
228 | for (i = 0; i < ds->num_ports; i++) { | |
229 | if (!dsa_is_port_initialized(ds, i)) | |
230 | continue; | |
231 | ||
232 | ret = dsa_slave_suspend(ds->ports[i].netdev); | |
233 | if (ret) | |
234 | return ret; | |
235 | } | |
236 | ||
237 | if (ds->ops->suspend) | |
238 | ret = ds->ops->suspend(ds); | |
239 | ||
240 | return ret; | |
241 | } | |
242 | EXPORT_SYMBOL_GPL(dsa_switch_suspend); | |
243 | ||
244 | int dsa_switch_resume(struct dsa_switch *ds) | |
245 | { | |
246 | int i, ret = 0; | |
247 | ||
248 | if (ds->ops->resume) | |
249 | ret = ds->ops->resume(ds); | |
250 | ||
251 | if (ret) | |
252 | return ret; | |
253 | ||
254 | /* Resume slave network devices */ | |
255 | for (i = 0; i < ds->num_ports; i++) { | |
256 | if (!dsa_is_port_initialized(ds, i)) | |
257 | continue; | |
258 | ||
259 | ret = dsa_slave_resume(ds->ports[i].netdev); | |
260 | if (ret) | |
261 | return ret; | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } | |
266 | EXPORT_SYMBOL_GPL(dsa_switch_resume); | |
267 | #endif | |
268 | ||
61b7363f | 269 | static struct packet_type dsa_pack_type __read_mostly = { |
3e8a72d1 FF |
270 | .type = cpu_to_be16(ETH_P_XDSA), |
271 | .func = dsa_switch_rcv, | |
272 | }; | |
273 | ||
91da11f8 LB |
274 | static int __init dsa_init_module(void) |
275 | { | |
7df899c3 BH |
276 | int rc; |
277 | ||
88e4f0ca VD |
278 | rc = dsa_slave_register_notifier(); |
279 | if (rc) | |
280 | return rc; | |
b73adef6 | 281 | |
a6a71f19 | 282 | rc = dsa_legacy_register(); |
7df899c3 BH |
283 | if (rc) |
284 | return rc; | |
285 | ||
3e8a72d1 FF |
286 | dev_add_pack(&dsa_pack_type); |
287 | ||
7df899c3 | 288 | return 0; |
91da11f8 LB |
289 | } |
290 | module_init(dsa_init_module); | |
291 | ||
292 | static void __exit dsa_cleanup_module(void) | |
293 | { | |
88e4f0ca | 294 | dsa_slave_unregister_notifier(); |
3e8a72d1 | 295 | dev_remove_pack(&dsa_pack_type); |
a6a71f19 | 296 | dsa_legacy_unregister(); |
91da11f8 LB |
297 | } |
298 | module_exit(dsa_cleanup_module); | |
299 | ||
577d6a7c | 300 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
91da11f8 LB |
301 | MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips"); |
302 | MODULE_LICENSE("GPL"); | |
303 | MODULE_ALIAS("platform:dsa"); |