kmod-switch: vlan parser rewrite, some api changes, minor fixes
[openwrt/svn-archive/archive.git] / openwrt / target / linux / 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 * Based on 'robocfg' by Oleg I. Vdovikin
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 */
22
23 #include <linux/config.h>
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 <asm/uaccess.h>
32
33 #include "switch-core.h"
34 #include "etc53xx.h"
35
36 #define DRIVER_NAME "bcm53xx"
37
38 #define ROBO_PHY_ADDR 0x1E /* robo switch phy address */
39
40 /* MII registers */
41 #define REG_MII_PAGE 0x10 /* MII Page register */
42 #define REG_MII_ADDR 0x11 /* MII Address register */
43 #define REG_MII_DATA0 0x18 /* MII Data register 0 */
44
45 #define REG_MII_PAGE_ENABLE 1
46 #define REG_MII_ADDR_WRITE 1
47 #define REG_MII_ADDR_READ 2
48
49 /* Private et.o ioctls */
50 #define SIOCGETCPHYRD (SIOCDEVPRIVATE + 9)
51 #define SIOCSETCPHYWR (SIOCDEVPRIVATE + 10)
52
53 static int use_et = 0;
54 static int is_5350 = 0;
55 static int max_vlans, max_ports;
56 static struct ifreq ifr;
57 static struct net_device *dev;
58
59 static int do_ioctl(int cmd, void *buf)
60 {
61 mm_segment_t old_fs = get_fs();
62 int ret;
63
64 if (buf != NULL)
65 ifr.ifr_data = (caddr_t) buf;
66
67 set_fs(KERNEL_DS);
68 ret = dev->do_ioctl(dev, &ifr, cmd);
69 set_fs(old_fs);
70
71 return ret;
72 }
73
74 static u16 mdio_read(__u16 phy_id, __u8 reg)
75 {
76 if (use_et) {
77 int args[2] = { reg };
78
79 if (phy_id != ROBO_PHY_ADDR) {
80 printk(
81 "Access to real 'phy' registers unavaliable.\n"
82 "Upgrade kernel driver.\n");
83
84 return 0xffff;
85 }
86
87
88 if (do_ioctl(SIOCGETCPHYRD, &args) < 0) {
89 printk("[%s:%d] SIOCGETCPHYRD failed!\n", __FILE__, __LINE__);
90 return 0xffff;
91 }
92
93 return args[1];
94 } else {
95 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &ifr.ifr_data;
96 mii->phy_id = phy_id;
97 mii->reg_num = reg;
98
99 if (do_ioctl(SIOCGMIIREG, NULL) < 0) {
100 printk("[%s:%d] SIOCGMIIREG failed!\n", __FILE__, __LINE__);
101
102 return 0xffff;
103 }
104
105 return mii->val_out;
106 }
107 }
108
109 static void mdio_write(__u16 phy_id, __u8 reg, __u16 val)
110 {
111 if (use_et) {
112 int args[2] = { reg, val };
113
114 if (phy_id != ROBO_PHY_ADDR) {
115 printk(
116 "Access to real 'phy' registers unavaliable.\n"
117 "Upgrade kernel driver.\n");
118
119 return;
120 }
121
122 if (do_ioctl(SIOCSETCPHYWR, args) < 0) {
123 printk("[%s:%d] SIOCGETCPHYWR failed!\n", __FILE__, __LINE__);
124 return;
125 }
126 } else {
127 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&ifr.ifr_data;
128
129 mii->phy_id = phy_id;
130 mii->reg_num = reg;
131 mii->val_in = val;
132
133 if (do_ioctl(SIOCSMIIREG, NULL) < 0) {
134 printk("[%s:%d] SIOCSMIIREG failed!\n", __FILE__, __LINE__);
135 return;
136 }
137 }
138 }
139
140 static int robo_reg(__u8 page, __u8 reg, __u8 op)
141 {
142 int i = 3;
143
144 /* set page number */
145 mdio_write(ROBO_PHY_ADDR, REG_MII_PAGE,
146 (page << 8) | REG_MII_PAGE_ENABLE);
147
148 /* set register address */
149 mdio_write(ROBO_PHY_ADDR, REG_MII_ADDR,
150 (reg << 8) | op);
151
152 /* check if operation completed */
153 while (i--) {
154 if ((mdio_read(ROBO_PHY_ADDR, REG_MII_ADDR) & 3) == 0)
155 return 0;
156 }
157
158 printk("[%s:%d] timeout in robo_reg!\n", __FILE__, __LINE__);
159
160 return 0;
161 }
162
163 static void robo_read(__u8 page, __u8 reg, __u16 *val, int count)
164 {
165 int i;
166
167 robo_reg(page, reg, REG_MII_ADDR_READ);
168
169 for (i = 0; i < count; i++)
170 val[i] = mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + i);
171 }
172
173 static __u16 robo_read16(__u8 page, __u8 reg)
174 {
175 robo_reg(page, reg, REG_MII_ADDR_READ);
176
177 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0);
178 }
179
180 static __u32 robo_read32(__u8 page, __u8 reg)
181 {
182 robo_reg(page, reg, REG_MII_ADDR_READ);
183
184 return mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0) +
185 (mdio_read(ROBO_PHY_ADDR, REG_MII_DATA0 + 1) << 16);
186 }
187
188 static void robo_write16(__u8 page, __u8 reg, __u16 val16)
189 {
190 /* write data */
191 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val16);
192
193 robo_reg(page, reg, REG_MII_ADDR_WRITE);
194 }
195
196 static void robo_write32(__u8 page, __u8 reg, __u32 val32)
197 {
198 /* write data */
199 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0, val32 & 65535);
200 mdio_write(ROBO_PHY_ADDR, REG_MII_DATA0 + 1, val32 >> 16);
201
202 robo_reg(page, reg, REG_MII_ADDR_WRITE);
203 }
204
205 /* checks that attached switch is 5325E/5350 */
206 static int robo_vlan5350()
207 {
208 /* set vlan access id to 15 and read it back */
209 __u16 val16 = 15;
210 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
211
212 /* 5365 will refuse this as it does not have this reg */
213 return (robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350) == val16);
214 }
215
216
217
218 static int robo_probe(char *devname)
219 {
220 struct ethtool_drvinfo info;
221 int i;
222 __u32 phyid;
223
224 printk("Probing device %s: ", devname);
225 strcpy(ifr.ifr_name, devname);
226
227 if ((dev = dev_get_by_name(devname)) == NULL) {
228 printk("No such device\n");
229 return 1;
230 }
231
232 info.cmd = ETHTOOL_GDRVINFO;
233 if (do_ioctl(SIOCETHTOOL, (void *) &info) < 0) {
234 printk("SIOCETHTOOL: not supported\n");
235 return 1;
236 }
237
238 /* try access using MII ioctls - get phy address */
239 if (do_ioctl(SIOCGMIIPHY, NULL) < 0) {
240 use_et = 1;
241 } else {
242 /* got phy address check for robo address */
243 struct mii_ioctl_data *mii = (struct mii_ioctl_data *) &ifr.ifr_data;
244 if (mii->phy_id != ROBO_PHY_ADDR) {
245 printk("Invalid phy address (%d)\n", mii->phy_id);
246 return 1;
247 }
248 }
249
250 phyid = mdio_read(ROBO_PHY_ADDR, 0x2) |
251 (mdio_read(ROBO_PHY_ADDR, 0x3) << 16);
252
253 if (phyid == 0xffffffff || phyid == 0x55210022) {
254 printk("No Robo switch in managed mode found\n");
255 return 1;
256 }
257
258 is_5350 = robo_vlan5350();
259 max_ports = 6;
260
261 for (i = 0; i <= (is_5350 ? VLAN_ID_MAX5350 : VLAN_ID_MAX); i++) {
262 /* issue read */
263 __u16 val16 = (i) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
264
265 if (is_5350) {
266 u32 val32;
267 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
268 /* actual read */
269 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
270 if ((val32 & (1 << 20)) /* valid */) {
271 max_vlans = i + 1;
272 }
273 } else {
274 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
275 /* actual read */
276 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
277 if ((val16 & (1 << 14)) /* valid */) {
278 max_vlans = i + 1;
279 }
280 }
281 }
282
283 printk("found!\n");
284 return 0;
285 }
286
287
288 static int handle_vlan_port_read(void *driver, char *buf, int nr)
289 {
290 __u16 val16;
291 int len = 0;
292 int j;
293
294 val16 = (nr) /* vlan */ | (0 << 12) /* read */ | (1 << 13) /* enable */;
295
296 if (is_5350) {
297 u32 val32;
298 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
299 /* actual read */
300 val32 = robo_read32(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
301 if ((val32 & (1 << 20)) /* valid */) {
302 for (j = 0; j < 6; j++) {
303 if (val32 & (1 << j)) {
304 len += sprintf(buf + len, "%d%s\t", j,
305 (val32 & (1 << (j + 6))) ? (j == 5 ? "u" : "") : "t");
306 }
307 }
308 len += sprintf(buf + len, "\n");
309 }
310 } else {
311 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
312 /* actual read */
313 val16 = robo_read16(ROBO_VLAN_PAGE, ROBO_VLAN_READ);
314 if ((val16 & (1 << 14)) /* valid */) {
315 for (j = 0; j < 6; j++) {
316 if (val16 & (1 << j)) {
317 len += sprintf(buf + len, "%d%s\t", j, (val16 & (1 << (j + 7))) ?
318 (j == 5 ? "u" : "") : "t");
319 }
320 }
321 len += sprintf(buf + len, "\n");
322 }
323 }
324
325 return len;
326 }
327
328 static int handle_vlan_port_write(void *driver, char *buf, int nr)
329 {
330 switch_driver *d = (switch_driver *) driver;
331 switch_vlan_config *c = switch_parse_vlan(d, buf);
332 int j;
333 __u16 val16;
334
335 if (c == NULL)
336 return -EINVAL;
337
338 for (j = 0; j < d->ports; j++) {
339 if ((c->untag | c->pvid) & (1 << j))
340 /* change default vlan tag */
341 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_PORT0_DEF_TAG + (j << 1), nr);
342 }
343
344 /* write config now */
345 val16 = (nr) /* vlan */ | (1 << 12) /* write */ | (1 << 13) /* enable */;
346 if (is_5350) {
347 robo_write32(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE_5350,
348 (1 << 20) /* valid */ | (c->untag << 6) | c->port);
349 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS_5350, val16);
350 } else {
351 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_WRITE,
352 (1 << 14) /* valid */ | (c->untag << 7) | c->port);
353 robo_write16(ROBO_VLAN_PAGE, ROBO_VLAN_TABLE_ACCESS, val16);
354 }
355
356 return 0;
357 }
358
359 static int __init robo_init()
360 {
361 char *device = "ethX";
362 int notfound = 1;
363
364 for (device[3] = '0'; (device[3] <= '3') && notfound; device[3]++) {
365 notfound = robo_probe(device);
366 }
367 device[3]--;
368
369 if (notfound)
370 return -ENODEV;
371 else {
372 switch_config vlan[] = {
373 {"ports", handle_vlan_port_read, handle_vlan_port_write},
374 {NULL, NULL, NULL}
375 };
376 switch_driver driver = {
377 name: DRIVER_NAME,
378 interface: device,
379 cpuport: max_ports - 1,
380 ports: max_ports,
381 vlans: max_vlans,
382 driver_handlers: NULL,
383 port_handlers: NULL,
384 vlan_handlers: vlan,
385 };
386
387 return switch_register_driver(&driver);
388 }
389 }
390
391 static void __exit robo_exit()
392 {
393 switch_unregister_driver(DRIVER_NAME);
394 }
395
396
397 MODULE_AUTHOR("Felix Fietkau <openwrt@nbd.name>");
398 MODULE_LICENSE("GPL");
399
400 module_init(robo_init);
401 module_exit(robo_exit);