allow bigger packets in the marvell switch (does not fix the mtu problems yet)
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / files / drivers / net / phy / mvswitch.c
1 /*
2 * Marvell 88E6060 switch driver
3 * Copyright (c) 2008 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License v2 as published by the
7 * Free Software Foundation
8 */
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/unistd.h>
13 #include <linux/slab.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/skbuff.h>
20 #include <linux/spinlock.h>
21 #include <linux/mm.h>
22 #include <linux/module.h>
23 #include <linux/mii.h>
24 #include <linux/ethtool.h>
25 #include <linux/phy.h>
26 #include <linux/if_vlan.h>
27
28 #include <asm/io.h>
29 #include <asm/irq.h>
30 #include <asm/uaccess.h>
31 #include "mvswitch.h"
32
33 MODULE_DESCRIPTION("Marvell 88E6060 Switch driver");
34 MODULE_AUTHOR("Felix Fietkau");
35 MODULE_LICENSE("GPL");
36
37 struct mvswitch_priv {
38 /* the driver's tx function */
39 int (*hardstart)(struct sk_buff *skb, struct net_device *dev);
40 struct vlan_group *grp;
41 u8 vlans[16];
42 };
43
44 #define to_mvsw(_phy) ((struct mvswitch_priv *) (_phy)->priv)
45
46 static inline u16
47 r16(struct phy_device *phydev, int addr, int reg)
48 {
49 return phydev->bus->read(phydev->bus, addr, reg);
50 }
51
52 static inline void
53 w16(struct phy_device *phydev, int addr, int reg, u16 val)
54 {
55 phydev->bus->write(phydev->bus, addr, reg, val);
56 }
57
58 static int
59 mvswitch_mangle_tx(struct sk_buff *skb, struct net_device *dev)
60 {
61 struct mvswitch_priv *priv;
62 struct vlan_ethhdr *eh;
63 char *buf = NULL;
64 u16 vid;
65
66 priv = dev->phy_ptr;
67 if (unlikely(!priv))
68 goto error;
69
70 if (unlikely(skb->len < 16))
71 goto error;
72
73 eh = (struct vlan_ethhdr *) skb->data;
74 if (be16_to_cpu(eh->h_vlan_proto) != 0x8100)
75 goto error;
76
77 vid = be16_to_cpu(eh->h_vlan_TCI) & VLAN_VID_MASK;
78 if (unlikely((vid > 15 || !priv->vlans[vid])))
79 goto error;
80
81 if (skb->len <= 64) {
82 if (pskb_expand_head(skb, 0, 68 - skb->len, GFP_ATOMIC)) {
83 if (net_ratelimit())
84 printk("%s: failed to expand/update skb for the switch\n", dev->name);
85 goto error;
86 }
87
88 buf = skb->data + 64;
89 skb->len = 68;
90 } else {
91 if (skb_cloned(skb) || unlikely(skb_tailroom(skb) < 4)) {
92 if (pskb_expand_head(skb, 0, 4, GFP_ATOMIC)) {
93 if (net_ratelimit())
94 printk("%s: failed to expand/update skb for the switch\n", dev->name);
95 goto error;
96 }
97 }
98 buf = skb_put(skb, 4);
99 }
100
101 /* move the ethernet header 4 bytes forward, overwriting the vlan tag */
102 memmove(skb->data + 4, skb->data, 12);
103 skb->data += 4;
104 skb->len -= 4;
105 skb->mac_header += 4;
106
107 if (!buf)
108 goto error;
109
110 /* append the tag */
111 *((u32 *) buf) = (
112 (0x80 << 24) |
113 ((priv->vlans[vid] & 0x1f) << 16)
114 );
115
116 return priv->hardstart(skb, dev);
117
118 error:
119 /* any errors? drop the packet! */
120 dev_kfree_skb_any(skb);
121 return 0;
122 }
123
124 static int
125 mvswitch_mangle_rx(struct sk_buff *skb, int napi)
126 {
127 struct mvswitch_priv *priv;
128 struct net_device *dev;
129 int vlan = -1;
130 unsigned char *buf;
131 int i;
132
133 dev = skb->dev;
134 if (!dev)
135 goto error;
136
137 priv = dev->phy_ptr;
138 if (!priv)
139 goto error;
140
141 if (!priv->grp)
142 goto error;
143
144 buf = skb->data + skb->len - 4;
145 if (buf[0] != 0x80)
146 goto error;
147
148 /* look for the vlan matching the incoming port */
149 for (i = 0; i < ARRAY_SIZE(priv->vlans); i++) {
150 if ((1 << buf[1]) & priv->vlans[i])
151 vlan = i;
152 }
153
154 if (vlan == -1)
155 goto error;
156
157 if (napi)
158 return vlan_hwaccel_receive_skb(skb, priv->grp, vlan);
159 else
160 return vlan_hwaccel_rx(skb, priv->grp, vlan);
161
162 error:
163 /* no vlan? eat the packet! */
164 dev_kfree_skb_any(skb);
165 return 0;
166 }
167
168
169 static int
170 mvswitch_netif_rx(struct sk_buff *skb)
171 {
172 return mvswitch_mangle_rx(skb, 0);
173 }
174
175 static int
176 mvswitch_netif_receive_skb(struct sk_buff *skb)
177 {
178 return mvswitch_mangle_rx(skb, 1);
179 }
180
181
182 static void
183 mvswitch_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
184 {
185 struct mvswitch_priv *priv = dev->phy_ptr;
186 priv->grp = grp;
187 }
188
189
190 static int
191 mvswitch_config_init(struct phy_device *pdev)
192 {
193 struct mvswitch_priv *priv = to_mvsw(pdev);
194 struct net_device *dev = pdev->attached_dev;
195 u8 vlmap = 0;
196 int i;
197
198 if (!dev)
199 return -EINVAL;
200
201 printk("%s: Marvell 88E6060 PHY driver attached.\n", dev->name);
202 pdev->supported = ADVERTISED_100baseT_Full;
203 pdev->advertising = ADVERTISED_100baseT_Full;
204 dev->phy_ptr = priv;
205
206 /* initialize default vlans */
207 for (i = 0; i < MV_PORTS; i++)
208 priv->vlans[(i == MV_WANPORT ? 1 : 0)] |= (1 << i);
209
210 /* before entering reset, disable all ports */
211 for (i = 0; i < MV_PORTS; i++)
212 w16(pdev, MV_PORTREG(CONTROL, i), 0x00);
213
214 msleep(2); /* wait for the status change to settle in */
215
216 /* put the device in reset and set ATU flags */
217 w16(pdev, MV_SWITCHREG(ATU_CTRL),
218 MV_ATUCTL_RESET |
219 MV_ATUCTL_ATU_1K |
220 MV_ATUCTL_AGETIME(4080) /* maximum */
221 );
222
223 i = 100; /* timeout */
224 do {
225 if (!(r16(pdev, MV_SWITCHREG(ATU_CTRL)) & MV_ATUCTL_RESET))
226 break;
227 msleep(1);
228 } while (--i > 0);
229
230 if (!i) {
231 printk("%s: Timeout waiting for the switch to reset.\n", dev->name);
232 return -ETIMEDOUT;
233 }
234
235 /* initialize the cpu port */
236 w16(pdev, MV_PORTREG(CONTROL, MV_CPUPORT),
237 MV_PORTCTRL_ENABLED |
238 MV_PORTCTRL_RXTR |
239 MV_PORTCTRL_TXTR
240 );
241 /* wait for the phy change to settle in */
242 msleep(2);
243 for (i = 0; i < MV_PORTS; i++) {
244 u8 pvid = 0;
245 int j;
246
247 vlmap = 0;
248
249 /* look for the matching vlan */
250 for (j = 0; j < ARRAY_SIZE(priv->vlans); j++) {
251 if (priv->vlans[j] & (1 << i)) {
252 vlmap = priv->vlans[j];
253 pvid = j;
254 }
255 }
256 /* leave port unconfigured if it's not part of a vlan */
257 if (!vlmap)
258 break;
259
260 /* add the cpu port to the allowed destinations list */
261 vlmap |= (1 << MV_CPUPORT);
262
263 /* take port out of its own vlan destination map */
264 vlmap &= ~(1 << i);
265
266 /* apply vlan settings */
267 w16(pdev, MV_PORTREG(VLANMAP, i),
268 MV_PORTVLAN_PORTS(vlmap) |
269 MV_PORTVLAN_ID(pvid)
270 );
271
272 /* re-enable port */
273 w16(pdev, MV_PORTREG(CONTROL, i), MV_PORTCTRL_ENABLED);
274 }
275
276 /* build the target list for the cpu port */
277 for (i = 0; i < MV_PORTS; i++)
278 vlmap |= (1 << i);
279
280 w16(pdev, MV_PORTREG(VLANMAP, MV_CPUPORT),
281 MV_PORTVLAN_PORTS(vlmap)
282 );
283
284 /* set the port association vector */
285 for (i = 0; i <= MV_PORTS; i++) {
286 w16(pdev, MV_PORTREG(ASSOC, i),
287 MV_PORTASSOC_PORTS(1 << i)
288 );
289 }
290
291 /* init switch control */
292 w16(pdev, MV_SWITCHREG(CTRL),
293 MV_SWITCHCTL_MSIZE |
294 MV_SWITCHCTL_DROP
295 );
296
297 /* hook into the tx function */
298 priv->hardstart = dev->hard_start_xmit;
299 pdev->netif_receive_skb = mvswitch_netif_receive_skb;
300 pdev->netif_rx = mvswitch_netif_rx;
301 dev->hard_start_xmit = mvswitch_mangle_tx;
302 dev->vlan_rx_register = mvswitch_vlan_rx_register;
303 dev->features |= NETIF_F_HW_VLAN_RX;
304
305 return 0;
306 }
307
308 static int
309 mvswitch_read_status(struct phy_device *phydev)
310 {
311 phydev->speed = SPEED_100;
312 phydev->duplex = DUPLEX_FULL;
313 phydev->state = PHY_UP;
314 return 0;
315 }
316
317 static int
318 mvswitch_config_aneg(struct phy_device *phydev)
319 {
320 return 0;
321 }
322
323 static void
324 mvswitch_remove(struct phy_device *pdev)
325 {
326 struct mvswitch_priv *priv = to_mvsw(pdev);
327 struct net_device *dev = pdev->attached_dev;
328
329 /* restore old xmit handler */
330 if (priv->hardstart && dev)
331 dev->hard_start_xmit = priv->hardstart;
332 dev->vlan_rx_register = NULL;
333 dev->vlan_rx_kill_vid = NULL;
334 dev->phy_ptr = NULL;
335 dev->features &= ~NETIF_F_HW_VLAN_RX;
336 kfree(priv);
337 }
338
339 static bool
340 mvswitch_detect(struct mii_bus *bus, int addr)
341 {
342 u16 reg;
343 int i;
344
345 /* we attach to phy id 31 to make sure that the late probe works */
346 if (addr != 31)
347 return false;
348
349 /* look for the switch on the bus */
350 reg = bus->read(bus, MV_PORTREG(IDENT, 0)) & MV_IDENT_MASK;
351 if (reg != MV_IDENT_VALUE)
352 return false;
353
354 /*
355 * Now that we've established that the switch actually exists, let's
356 * get rid of the competition :)
357 */
358 for (i = 0; i < 31; i++) {
359 if (!bus->phy_map[i])
360 continue;
361
362 device_unregister(&bus->phy_map[i]->dev);
363 kfree(bus->phy_map[i]);
364 bus->phy_map[i] = NULL;
365 }
366
367 return true;
368 }
369
370 static int
371 mvswitch_probe(struct phy_device *pdev)
372 {
373 struct mvswitch_priv *priv;
374
375 priv = kzalloc(sizeof(struct mvswitch_priv), GFP_KERNEL);
376 if (priv == NULL)
377 return -ENOMEM;
378
379 pdev->priv = priv;
380
381 return 0;
382 }
383
384
385 static struct phy_driver mvswitch_driver = {
386 .name = "Marvell 88E6060",
387 .features = PHY_BASIC_FEATURES,
388 .detect = &mvswitch_detect,
389 .probe = &mvswitch_probe,
390 .remove = &mvswitch_remove,
391 .config_init = &mvswitch_config_init,
392 .config_aneg = &mvswitch_config_aneg,
393 .read_status = &mvswitch_read_status,
394 .driver = { .owner = THIS_MODULE,},
395 };
396
397 static int __init
398 mvswitch_init(void)
399 {
400 return phy_driver_register(&mvswitch_driver);
401 }
402
403 static void __exit
404 mvswitch_exit(void)
405 {
406 phy_driver_unregister(&mvswitch_driver);
407 }
408
409 module_init(mvswitch_init);
410 module_exit(mvswitch_exit);