switch: improve error messages
[openwrt/staging/yousong.git] / package / switch / src / switch-robo.c
1 /*
2 * Broadcom BCM5325E/536x switch configuration module
3 *
4 * Copyright (C) 2005 Felix Fietkau <nbd@nbd.name>
5 * Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
6 * Based on 'robocfg' by Oleg I. Vdovikin
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/if.h>
27 #include <linux/if_arp.h>
28 #include <linux/sockios.h>
29 #include <linux/ethtool.h>
30 #include <linux/mii.h>
31 #include <linux/delay.h>
32 #include <asm/uaccess.h>
33
34 #include "switch-core.h"
35 #include "etc53xx.h"
36
37 #ifdef CONFIG_BCM47XX
38 #include <bcm47xx_nvram.h>
39 #endif
40
41 #define DRIVER_NAME "bcm53xx"
42 #define DRIVER_VERSION "0.02"
43 #define PFX "roboswitch: "
44
45 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
46 #define ROBO_PHY_ADDR_TG3 0x01 /* Tigon3 PHY address */
47 #define ROBO_PHY_ADDR_BCM63XX 0x00 /* BCM63XX PHY address */
48
49 /* MII registers */
50 #define REG_MII_PAGE 0x10 /* MII Page register */
51 #define REG_MII_ADDR 0x11 /* MII Address register */
52 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
53
54 #define REG_MII_PAGE_ENABLE 1
55 #define REG_MII_ADDR_WRITE 1
56 #define REG_MII_ADDR_READ 2
57
58 /* Robo device ID register (in ROBO_MGMT_PAGE) */
59 #define ROBO_DEVICE_ID 0x30
60 #define ROBO_DEVICE_ID_5325 0x25 /* Faked */
61 #define ROBO_DEVICE_ID_5395 0x95
62 #define ROBO_DEVICE_ID_5397 0x97
63 #define ROBO_DEVICE_ID_5398 0x98
64 #define ROBO_DEVICE_ID_53115 0x3115
65
66 /* Private et.o ioctls */
67 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
68 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
69
70 /* Data structure for a Roboswitch device. */
71 struct robo_switch {
72 char *device; /* The device name string (ethX) */
73 u16 devid; /* ROBO_DEVICE_ID_53xx */
74 bool is_5350;
75 struct ifreq ifr;
76 struct net_device *dev;
77 unsigned char port[6];
78 };
79
80 /* Currently we can only have one device in the system. */
81 static struct robo_switch robo;
82
83
84 static int do_ioctl(int cmd)
85 {
86 mm_segment_t old_fs = get_fs();
87 int ret;
88
89 set_fs(KERNEL_DS);
90 ret = robo.dev->netdev_ops->ndo_do_ioctl(robo.dev, &robo.ifr, cmd);
91 set_fs(old_fs);
92
93 return ret;
94 }
95
96 static u16 mdio_read(__u16 phy_id, __u8 reg)
97 {
98 struct mii_ioctl_data *mii = if_mii(&robo.ifr);
99 int err;
100
101 mii->phy_id = phy_id;
102 mii->reg_num = reg;
103
104 err = do_ioctl(SIOCGMIIREG);
105 if (err < 0) {
106 printk(KERN_ERR PFX "failed to read mdio reg %i with err %i.\n", reg, err);
107
108 return 0xffff;
109 }
110
111 return mii->val_out;
112 }
113
114 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
115 {
116 struct mii_ioctl_data *mii = if_mii(&robo.ifr);
117 int err;
118
119 mii->phy_id = phy_id;
120 mii->reg_num = reg;
121 mii->val_in = val;
122
123 err = do_ioctl(SIOCSMIIREG);
124 if (err < 0) {
125 printk(KERN_ERR PFX "failed to write mdio reg: %i with err %i.\n", reg, err);
126 return;
127 }
128 }
129
130 static int robo_reg(__u8 page, __u8 reg, __u8 op)
131 {
132 int i = 3;
133
134 /* set page number */
135 mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
136 (page << 8) | REG_MII_PAGE_ENABLE);
137
138 /* set register address */
139 mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
140 (reg << 8) | op);
141
142 /* check if operation completed */
143 while (i--) {
144 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
145 return 0;
146 }
147
148 printk(KERN_ERR PFX "timeout in robo_reg on page %i and reg %i with op %i.\n", page, reg, op);
149
150 return 0;
151 }
152
153 /*
154 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
155 {
156 int i;
157
158 robo_reg(page, reg, REG_MII_ADDR_READ);
159
160 for (i = 0; i < count; i++)
161 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
162 }
163 */
164
165 static __u16 robo_read16(__u8 page, __u8 reg)
166 {
167 robo_reg(page, reg, REG_MII_ADDR_READ);
168
169 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
170 }
171
172 static __u32 robo_read32(__u8 page, __u8 reg)
173 {
174 robo_reg(page, reg, REG_MII_ADDR_READ);
175
176 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) +
177 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
178 }
179
180 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
181 {
182 /* write data */
183 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
184
185 robo_reg(page, reg, REG_MII_ADDR_WRITE);
186 }
187
188 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
189 {
190 /* write data */
191 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 65535);
192 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
193
194 robo_reg(page, reg, REG_MII_ADDR_WRITE);
195 }
196
197 /* checks that attached switch is 5325E/5350 */
198 static int robo_vlan5350(void)
199 {
200 /* set vlan access id to 15 and read it back */
201 __u16 val16 = 15;
202 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
203
204 /* 5365 will refuse this as it does not have this reg */
205 return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == val16);
206 }
207
208 static int robo_switch_enable(void)
209 {
210 unsigned int i, last_port;
211 u16 val;
212 #ifdef CONFIG_BCM47XX
213 char buf[20];
214 #endif
215
216 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
217 if (!(val & (1 << 1))) {
218 /* Unmanaged mode */
219 val &= ~(1 << 0);
220 /* With forwarding */
221 val |= (1 << 1);
222 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
223 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
224 if (!(val & (1 << 1))) {
225 printk(KERN_ERR PFX "Failed to enable switch\n");
226 return -EBUSY;
227 }
228
229 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
230 ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
231 for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
232 robo_write16(ROBO_CTRL_PAGE, i, 0);
233 }
234
235 #ifdef CONFIG_BCM47XX
236 /* WAN port LED, except for Netgear WGT634U */
237 if (bcm47xx_nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
238 if (strcmp(buf, "cfe") != 0)
239 robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
240 }
241 #endif
242 return 0;
243 }
244
245 static void robo_switch_reset(void)
246 {
247 if ((robo.devid == ROBO_DEVICE_ID_5395) ||
248 (robo.devid == ROBO_DEVICE_ID_5397) ||
249 (robo.devid == ROBO_DEVICE_ID_5398)) {
250 /* Trigger a software reset. */
251 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
252 mdelay(500);
253 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
254 }
255 }
256
257 static int robo_probe(char *devname)
258 {
259 __u32 phyid;
260 unsigned int i;
261 int err = 1;
262
263 printk(KERN_INFO PFX "Probing device '%s'\n", devname);
264 strcpy(robo.ifr.ifr_name, devname);
265
266 if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
267 printk(KERN_ERR PFX "No such device\n");
268 return 1;
269 }
270 if (!robo.dev->netdev_ops || !robo.dev->netdev_ops->ndo_do_ioctl) {
271 printk(KERN_ERR PFX "ndo_do_ioctl not implemented in ethernet driver\n");
272 return 1;
273 }
274
275 robo.device = devname;
276 for (i = 0; i < 5; i++)
277 robo.port[i] = i;
278 robo.port[5] = 8;
279
280 /* try access using MII ioctls - get phy address */
281 err = do_ioctl(SIOCGMIIPHY);
282 if (err < 0) {
283 printk(KERN_ERR PFX "error (%i) while accessing MII phy registers with ioctls\n", err);
284 goto done;
285 }
286
287 /* got phy address check for robo address */
288 struct mii_ioctl_data *mii = if_mii(&robo.ifr);
289 if ((mii->phy_id != ROBO_PHY_ADDR) &&
290 (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
291 (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
292 printk(KERN_ERR PFX "Invalid phy address (%d)\n", mii->phy_id);
293 goto done;
294 }
295
296 phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
297 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
298
299 if (phyid == 0xffffffff || phyid == 0x55210022) {
300 printk(KERN_ERR PFX "No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
301 goto done;
302 }
303
304 /* Get the device ID */
305 for (i = 0; i < 10; i++) {
306 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
307 if (robo.devid)
308 break;
309 udelay(10);
310 }
311 if (!robo.devid)
312 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
313 robo.is_5350 = robo_vlan5350();
314
315 robo_switch_reset();
316 err = robo_switch_enable();
317 if (err)
318 goto done;
319 err = 0;
320
321 printk(KERN_INFO PFX "found a 5%s%x!%s at %s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
322 robo.is_5350 ? " It's a 5350." : "", devname);
323
324 done:
325 if (err) {
326 dev_put(robo.dev);
327 robo.dev = NULL;
328 }
329 return err;
330 }
331
332
333 static int handle_vlan_port_read(void *driver, char *buf, int nr)
334 {
335 __u16 val16;
336 int len = 0;
337 int j;
338
339 val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
340
341 if (robo.is_5350) {
342 u32 val32;
343 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
344 /* actual read */
345 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
346 if ((val32 & (1 << 20)) /* valid */) {
347 for (j = 0; j < 6; j++) {
348 if (val32 & (1 << j)) {
349 len += sprintf(buf + len, "%d", j);
350 if (val32 & (1 << (j + 6))) {
351 if (j == 5) buf[len++] = 'u';
352 } else {
353 buf[len++] = 't';
354 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
355 buf[len++] = '*';
356 }
357 buf[len++] = '\t';
358 }
359 }
360 len += sprintf(buf + len, "\n");
361 }
362 } else {
363 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
364 /* actual read */
365 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
366 if ((val16 & (1 << 14)) /* valid */) {
367 for (j = 0; j < 6; j++) {
368 if (val16 & (1 << j)) {
369 len += sprintf(buf + len, "%d", j);
370 if (val16 & (1 << (j + 7))) {
371 if (j == 5) buf[len++] = 'u';
372 } else {
373 buf[len++] = 't';
374 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
375 buf[len++] = '*';
376 }
377 buf[len++] = '\t';
378 }
379 }
380 len += sprintf(buf + len, "\n");
381 }
382 }
383
384 buf[len] = '\0';
385
386 return len;
387 }
388
389 static int handle_vlan_port_write(void *driver, char *buf, int nr)
390 {
391 switch_driver *d = (switch_driver *) driver;
392 switch_vlan_config *c = switch_parse_vlan(d, buf);
393 int j;
394 __u16 val16;
395
396 if (c == NULL)
397 return -EINVAL;
398
399 for (j = 0; j < d->ports; j++) {
400 if ((c->untag | c->pvid) & (1 << j))
401 /* change default vlan tag */
402 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
403 }
404
405 /* write config now */
406
407 if (robo.devid != ROBO_DEVICE_ID_5325) {
408 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
409 (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
410
411 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
412 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
413 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
414 kfree(c);
415 return 0;
416 }
417
418 val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
419 if (robo.is_5350) {
420 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
421 (1 << 20) /* valid */ | (c->untag << 6) | c->port);
422 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
423 } else {
424 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
425 (1 << 14) /* valid */ | (c->untag << 7) | c->port);
426 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
427 }
428
429 kfree(c);
430 return 0;
431 }
432
433 #define set_switch(state) \
434 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
435
436 static int handle_enable_read(void *driver, char *buf, int nr)
437 {
438 return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
439 }
440
441 static int handle_enable_write(void *driver, char *buf, int nr)
442 {
443 set_switch(buf[0] == '1');
444
445 return 0;
446 }
447
448 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
449 {
450 return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
451 }
452
453 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
454 {
455 int disable = ((buf[0] != '1') ? 1 : 0);
456
457 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
458 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
459 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
460 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
461 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
462
463 if (robo.devid != ROBO_DEVICE_ID_5325)
464 return 0;
465
466 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
467 (1 << 6) /* drop invalid VID frames */);
468 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
469 (1 << 3) /* drop miss V table frames */);
470
471 return 0;
472 }
473
474 static int handle_reset(void *driver, char *buf, int nr)
475 {
476 switch_driver *d = (switch_driver *) driver;
477 int j;
478 __u16 val16;
479
480 /* disable switching */
481 set_switch(0);
482
483 /* reset vlans */
484 for (j = 0; j <= ((robo.is_5350) ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
485 /* write config now */
486 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
487 if (robo.is_5350)
488 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
489 else
490 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
491 robo_write16(ROBO_VLAN_PAGE, robo.is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 :
492 ROBO_VLAN_TABLE_ACCESS,
493 val16);
494 }
495
496 /* reset ports to a known good state */
497 for (j = 0; j < d->ports; j++) {
498 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
499 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
500 }
501
502 /* enable switching */
503 set_switch(1);
504
505 /* enable vlans */
506 handle_enable_vlan_write(driver, "1", 0);
507
508 return 0;
509 }
510
511 static int __init robo_init(void)
512 {
513 int notfound = 1;
514 char *device;
515
516 device = strdup("ethX");
517 for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
518 if (! switch_device_registered (device))
519 notfound = robo_probe(device);
520 }
521 device[3]--;
522
523 if (notfound) {
524 kfree(device);
525 return -ENODEV;
526 } else {
527 static const switch_config cfg[] = {
528 {
529 .name = "enable",
530 .read = handle_enable_read,
531 .write = handle_enable_write
532 }, {
533 .name = "enable_vlan",
534 .read = handle_enable_vlan_read,
535 .write = handle_enable_vlan_write
536 }, {
537 .name = "reset",
538 .read = NULL,
539 .write = handle_reset
540 }, { NULL, },
541 };
542 static const switch_config vlan[] = {
543 {
544 .name = "ports",
545 .read = handle_vlan_port_read,
546 .write = handle_vlan_port_write
547 }, { NULL, },
548 };
549 switch_driver driver = {
550 .name = DRIVER_NAME,
551 .version = DRIVER_VERSION,
552 .interface = device,
553 .cpuport = 5,
554 .ports = 6,
555 .vlans = 16,
556 .driver_handlers = cfg,
557 .port_handlers = NULL,
558 .vlan_handlers = vlan,
559 };
560 if (robo.devid != ROBO_DEVICE_ID_5325) {
561 driver.ports = 9;
562 driver.cpuport = 8;
563 }
564
565 return switch_register_driver(&driver);
566 }
567 }
568
569 static void __exit robo_exit(void)
570 {
571 switch_unregister_driver(DRIVER_NAME);
572 if (robo.dev)
573 dev_put(robo.dev);
574 kfree(robo.device);
575 }
576
577
578 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
579 MODULE_LICENSE("GPL");
580
581 module_init(robo_init);
582 module_exit(robo_exit);