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