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