switch: improve error message
[librecmc/librecmc.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 <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
107                        "[%s:%d] SIOCGMIIREG failed! err: %i\n", __FILE__, __LINE__, err);
108
109                 return 0xffff;
110         }
111
112         return mii->val_out;
113 }
114
115 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
116 {
117         struct mii_ioctl_data *mii = if_mii(&robo.ifr);
118         int err;
119
120         mii->phy_id = phy_id;
121         mii->reg_num = reg;
122         mii->val_in = val;
123
124         err = do_ioctl(SIOCSMIIREG);
125         if (err < 0) {
126                 printk(KERN_ERR PFX
127                        "[%s:%d] SIOCSMIIREG failed! err: %i\n", __FILE__, __LINE__, err);
128                 return;
129         }
130 }
131
132 static int robo_reg(__u8 page, __u8 reg, __u8 op)
133 {
134         int i = 3;
135
136         /* set page number */
137         mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
138                 (page << 8) | REG_MII_PAGE_ENABLE);
139
140         /* set register address */
141         mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
142                 (reg << 8) | op);
143
144         /* check if operation completed */
145         while (i--) {
146                 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
147                         return 0;
148         }
149
150         printk(KERN_ERR PFX "[%s:%d] timeout in robo_reg!\n", __FILE__, __LINE__);
151
152         return 0;
153 }
154
155 /*
156 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
157 {
158         int i;
159
160         robo_reg(page, reg, REG_MII_ADDR_READ);
161
162         for (i = 0; i < count; i++)
163                 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
164 }
165 */
166
167 static __u16 robo_read16(__u8 page, __u8 reg)
168 {
169         robo_reg(page, reg, REG_MII_ADDR_READ);
170
171         return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
172 }
173
174 static __u32 robo_read32(__u8 page, __u8 reg)
175 {
176         robo_reg(page, reg, REG_MII_ADDR_READ);
177
178         return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) +
179                 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
180 }
181
182 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
183 {
184         /* write data */
185         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
186
187         robo_reg(page, reg, REG_MII_ADDR_WRITE);
188 }
189
190 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
191 {
192         /* write data */
193         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 65535);
194         mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
195
196         robo_reg(page, reg, REG_MII_ADDR_WRITE);
197 }
198
199 /* checks that attached switch is 5325E/5350 */
200 static int robo_vlan5350(void)
201 {
202         /* set vlan access id to 15 and read it back */
203         __u16 val16 = 15;
204         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
205
206         /* 5365 will refuse this as it does not have this reg */
207         return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == val16);
208 }
209
210 static int robo_switch_enable(void)
211 {
212         unsigned int i, last_port;
213         u16 val;
214 #ifdef CONFIG_BCM47XX
215         char buf[20];
216 #endif
217
218         val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
219         if (!(val & (1 << 1))) {
220                 /* Unmanaged mode */
221                 val &= ~(1 << 0);
222                 /* With forwarding */
223                 val |= (1 << 1);
224                 robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, val);
225                 val = robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE);
226                 if (!(val & (1 << 1))) {
227                         printk("Failed to enable switch\n");
228                         return -EBUSY;
229                 }
230
231                 last_port = (robo.devid == ROBO_DEVICE_ID_5398) ?
232                                 ROBO_PORT6_CTRL : ROBO_PORT3_CTRL;
233                 for (i = ROBO_PORT0_CTRL; i < last_port + 1; i++)
234                         robo_write16(ROBO_CTRL_PAGE, i, 0);
235         }
236
237 #ifdef CONFIG_BCM47XX
238         /* WAN port LED, except for Netgear WGT634U */
239         if (nvram_getenv("nvram_type", buf, sizeof(buf)) >= 0) {
240                 if (strcmp(buf, "cfe") != 0)
241                         robo_write16(ROBO_CTRL_PAGE, 0x16, 0x1F);
242         }
243 #endif
244         return 0;
245 }
246
247 static void robo_switch_reset(void)
248 {
249         if ((robo.devid == ROBO_DEVICE_ID_5395) ||
250             (robo.devid == ROBO_DEVICE_ID_5397) ||
251             (robo.devid == ROBO_DEVICE_ID_5398)) {
252                 /* Trigger a software reset. */
253                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0x83);
254                 mdelay(500);
255                 robo_write16(ROBO_CTRL_PAGE, 0x79, 0);
256         }
257 }
258
259 static int robo_probe(char *devname)
260 {
261         __u32 phyid;
262         unsigned int i;
263         int err = 1;
264
265         printk(KERN_INFO PFX "Probing device %s: ", devname);
266         strcpy(robo.ifr.ifr_name, devname);
267
268         if ((robo.dev = dev_get_by_name(&init_net, devname)) == NULL) {
269                 printk("No such device\n");
270                 return 1;
271         }
272
273         robo.device = devname;
274         for (i = 0; i < 5; i++)
275                 robo.port[i] = i;
276         robo.port[5] = 8;
277
278         /* try access using MII ioctls - get phy address */
279         if (do_ioctl(SIOCGMIIPHY) < 0) {
280                 printk("error while accessing MII phy registers with ioctls\n");
281                 goto done;
282         }
283
284         /* got phy address check for robo address */
285         struct mii_ioctl_data *mii = if_mii(&robo.ifr);
286         if ((mii->phy_id != ROBO_PHY_ADDR) &&
287             (mii->phy_id != ROBO_PHY_ADDR_BCM63XX) &&
288             (mii->phy_id != ROBO_PHY_ADDR_TG3)) {
289                 printk("Invalid phy address (%d)\n", mii->phy_id);
290                 goto done;
291         }
292
293         phyid = mdio_read(ROBO_PHY_ADDR, 0x2) | 
294                 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
295
296         if (phyid == 0xffffffff || phyid == 0x55210022) {
297                 printk("No Robo switch in managed mode found, phy_id = 0x%08x\n", phyid);
298                 goto done;
299         }
300
301         /* Get the device ID */
302         for (i = 0; i < 10; i++) {
303                 robo.devid = robo_read16(ROBO_MGMT_PAGE, ROBO_DEVICE_ID);
304                 if (robo.devid)
305                         break;
306                 udelay(10);
307         }
308         if (!robo.devid)
309                 robo.devid = ROBO_DEVICE_ID_5325; /* Fake it */
310         robo.is_5350 = robo_vlan5350();
311
312         robo_switch_reset();
313         err = robo_switch_enable();
314         if (err)
315                 goto done;
316         err = 0;
317
318         printk("found a 5%s%x!%s\n", robo.devid & 0xff00 ? "" : "3", robo.devid,
319                 robo.is_5350 ? " It's a 5350." : "");
320
321 done:
322         if (err) {
323                 dev_put(robo.dev);
324                 robo.dev = NULL;
325         }
326         return err;
327 }
328
329
330 static int handle_vlan_port_read(void *driver, char *buf, int nr)
331 {
332         __u16 val16;
333         int len = 0;
334         int j;
335
336         val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
337
338         if (robo.is_5350) {
339                 u32 val32;
340                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
341                 /* actual read */
342                 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
343                 if ((val32 & (1 << 20)) /* valid */) {
344                         for (j = 0; j < 6; j++) {
345                                 if (val32 & (1 << j)) {
346                                         len += sprintf(buf + len, "%d", j);
347                                         if (val32 & (1 << (j + 6))) {
348                                                 if (j == 5) buf[len++] = 'u';
349                                         } else {
350                                                 buf[len++] = 't';
351                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
352                                                         buf[len++] = '*';
353                                         }
354                                         buf[len++] = '\t';
355                                 }
356                         }
357                         len += sprintf(buf + len, "\n");
358                 }
359         } else {
360                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
361                 /* actual read */
362                 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
363                 if ((val16 & (1 << 14)) /* valid */) {
364                         for (j = 0; j < 6; j++) {
365                                 if (val16 & (1 << j)) {
366                                         len += sprintf(buf + len, "%d", j);
367                                         if (val16 & (1 << (j + 7))) {
368                                                 if (j == 5) buf[len++] = 'u';
369                                         } else {
370                                                 buf[len++] = 't';
371                                                 if (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1)) == nr)
372                                                         buf[len++] = '*';
373                                         }
374                                         buf[len++] = '\t';
375                                 }
376                         }
377                         len += sprintf(buf + len, "\n");
378                 }
379         }
380
381         buf[len] = '\0';
382
383         return len;
384 }
385
386 static int handle_vlan_port_write(void *driver, char *buf, int nr)
387 {
388         switch_driver *d = (switch_driver *) driver;
389         switch_vlan_config *c = switch_parse_vlan(d, buf);
390         int j;
391         __u16 val16;
392
393         if (c == NULL)
394                 return -EINVAL;
395
396         for (j = 0; j < d->ports; j++) {
397                 if ((c->untag | c->pvid) & (1 << j))
398                         /* change default vlan tag */
399                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
400         }
401
402         /* write config now */
403
404         if (robo.devid != ROBO_DEVICE_ID_5325) {
405                 __u8 regoff = ((robo.devid == ROBO_DEVICE_ID_5395) ||
406                         (robo.devid == ROBO_DEVICE_ID_53115)) ? 0x20 : 0;
407
408                 robo_write32(ROBO_ARLIO_PAGE, 0x63 + regoff, (c->untag << 9) | c->port);
409                 robo_write16(ROBO_ARLIO_PAGE, 0x61 + regoff, nr);
410                 robo_write16(ROBO_ARLIO_PAGE, 0x60 + regoff, 1 << 7);
411                 kfree(c);
412                 return 0;
413         }
414
415         val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
416         if (robo.is_5350) {
417                 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
418                         (1 << 20) /* valid */ | (c->untag << 6) | c->port);
419                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
420         } else {
421                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
422                         (1 << 14)  /* valid */ | (c->untag << 7) | c->port);
423                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
424         }
425
426         kfree(c);
427         return 0;
428 }
429
430 #define set_switch(state) \
431         robo_write16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE, (robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & ~2) | (state ? 2 : 0));
432
433 static int handle_enable_read(void *driver, char *buf, int nr)
434 {
435         return sprintf(buf, "%d\n", (((robo_read16(ROBO_CTRL_PAGE, ROBO_SWITCH_MODE) & 2) == 2) ? 1 : 0));
436 }
437
438 static int handle_enable_write(void *driver, char *buf, int nr)
439 {
440         set_switch(buf[0] == '1');
441
442         return 0;
443 }
444
445 static int handle_enable_vlan_read(void *driver, char *buf, int nr)
446 {
447         return sprintf(buf, "%d\n", (((robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0) & (1 << 7)) == (1 << 7)) ? 1 : 0));
448 }
449
450 static int handle_enable_vlan_write(void *driver, char *buf, int nr)
451 {
452         int disable = ((buf[0] != '1') ? 1 : 0);
453
454         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL0, disable ? 0 :
455                 (1 << 7) /* 802.1Q VLAN */ | (3 << 5) /* mac check and hash */);
456         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL1, disable ? 0 :
457                 (robo.devid == ROBO_DEVICE_ID_5325 ? (1 << 1) :
458                 0) | (1 << 2) | (1 << 3)); /* RSV multicast */
459
460         if (robo.devid != ROBO_DEVICE_ID_5325)
461                 return 0;
462
463         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL4, disable ? 0 :
464                 (1 << 6) /* drop invalid VID frames */);
465         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_CTRL5, disable ? 0 :
466                 (1 << 3) /* drop miss V table frames */);
467
468         return 0;
469 }
470
471 static int handle_reset(void *driver, char *buf, int nr)
472 {
473         switch_driver *d = (switch_driver *) driver;
474         int j;
475         __u16 val16;
476
477         /* disable switching */
478         set_switch(0);
479
480         /* reset vlans */
481         for (j = 0; j <= ((robo.is_5350) ? VLAN_ID_MAX5350 : VLAN_ID_MAX); j++) {
482                 /* write config now */
483                 val16 = (j) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
484                 if (robo.is_5350)
485                         robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350, 0);
486                 else
487                         robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE, 0);
488                 robo_write16(ROBO_VLAN_PAGE, robo.is_5350 ? ROBO_VLAN_TABLE_ACCESS_5350 :
489                                                             ROBO_VLAN_TABLE_ACCESS,
490                              val16);
491         }
492
493         /* reset ports to a known good state */
494         for (j = 0; j < d->ports; j++) {
495                 robo_write16(ROBO_CTRL_PAGE, robo.port[j], 0x0000);
496                 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), 0);
497         }
498
499         /* enable switching */
500         set_switch(1);
501
502         /* enable vlans */
503         handle_enable_vlan_write(driver, "1", 0);
504
505         return 0;
506 }
507
508 static int __init robo_init(void)
509 {
510         int notfound = 1;
511         char *device;
512
513         device = strdup("ethX");
514         for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
515                 if (! switch_device_registered (device))
516                         notfound = robo_probe(device);
517         }
518         device[3]--;
519
520         if (notfound) {
521                 kfree(device);
522                 return -ENODEV;
523         } else {
524                 static const switch_config cfg[] = {
525                         {
526                                 .name   = "enable",
527                                 .read   = handle_enable_read,
528                                 .write  = handle_enable_write
529                         }, {
530                                 .name   = "enable_vlan",
531                                 .read   = handle_enable_vlan_read,
532                                 .write  = handle_enable_vlan_write
533                         }, {
534                                 .name   = "reset",
535                                 .read   = NULL,
536                                 .write  = handle_reset
537                         }, { NULL, },
538                 };
539                 static const switch_config vlan[] = {
540                         {
541                                 .name   = "ports",
542                                 .read   = handle_vlan_port_read,
543                                 .write  = handle_vlan_port_write
544                         }, { NULL, },
545                 };
546                 switch_driver driver = {
547                         .name                   = DRIVER_NAME,
548                         .version                = DRIVER_VERSION,
549                         .interface              = device,
550                         .cpuport                = 5,
551                         .ports                  = 6,
552                         .vlans                  = 16,
553                         .driver_handlers        = cfg,
554                         .port_handlers          = NULL,
555                         .vlan_handlers          = vlan,
556                 };
557                 if (robo.devid != ROBO_DEVICE_ID_5325) {
558                         driver.ports = 9;
559                         driver.cpuport = 8;
560                 }
561
562                 return switch_register_driver(&driver);
563         }
564 }
565
566 static void __exit robo_exit(void)
567 {
568         switch_unregister_driver(DRIVER_NAME);
569         if (robo.dev)
570                 dev_put(robo.dev);
571         kfree(robo.device);
572 }
573
574
575 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
576 MODULE_LICENSE("GPL");
577
578 module_init(robo_init);
579 module_exit(robo_exit);